Trying to user registerUser function in controller with ajax

9 years 11 months ago - 9 years 11 months ago #244271 by sfraise
I'm trying to use the registerUser function within a component controller class in order to register users using ajax via index.php?option=com_mycomponent&task=registerUser&format=raw.

The function works fine outside of the controller class, but I can't get it to work within the controller class. The ajax call is hitting the function ok, if I remove all of the actual register code and replace it with a simple echo it works fine and the values are all being passed properly.

My first thought is it's possibly an issue loading the api? I've tried loading the api above the class at the top of the file as well as trying to load it within the method but neither work.

Is it not possible to use the registerUser function as a class method for some reason?

Here's my code:
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// import Joomla controller library
jimport('joomla.application.component.controller');

// INCLUDE CB API
global $_CB_framework, $mainframe;

if (defined('JPATH_ADMINISTRATOR')) {
    if (!file_exists(JPATH_ADMINISTRATOR . '/components/com_comprofiler/plugin.foundation.php')) {
        echo 'CB not installed!';
        return;
    }

    include_once(JPATH_ADMINISTRATOR . '/components/com_comprofiler/plugin.foundation.php');
} else {
    if (!file_exists($mainframe->getCfg('absolute_path') . '/administrator/components/com_comprofiler/plugin.foundation.php')) {
        echo 'CB not installed!';
        return;
    }

    include_once($mainframe->getCfg('absolute_path') . '/administrator/components/com_comprofiler/plugin.foundation.php');
}
cbimport('cb.database');
cbimport('cb.html');
cbimport('cb.plugins');

/**
 * Member Manager Component Controller
 */
class membermanagerController extends JControllerLegacy
{
    // REGISTERATION USER
    public function registerUser()
    {
        global $_CB_framework, $_CB_database, $ueConfig, $_PLUGINS;

        // GET VALUES
        $jinput = JFactory::getApplication()->input;
        $name = $jinput->get('name');
        $email = $jinput->get('email');
        $password = $jinput->get('password');
        $address = $jinput->get('address');
        $address2 = $jinput->get('address2');
        $city = $jinput->get('city');
        $state = $jinput->get('state');
        $zip = $jinput->get('zip');
        $phone = $jinput->get('phone');
        $website = $jinput->get('website');
        $facebook = $jinput->get('facebook');
        $hours = $jinput->get('hours');
        $category = $jinput->get('category');
        $approve = 0;
        $confirm = 0;

        $approval = ($approve == 2 ? $ueConfig['reg_admin_approval'] : $approve);
        $confirmation = ($confirm == 2 ? $ueConfig['reg_confirmation'] : $confirm);
        $usertype = $_CB_framework->getCfg('new_usertype');

        $user = new moscomprofilerUser($_CB_database);
        $user->usertype = ($usertype ? $usertype : 'Registered');
        $user->gid = $_CB_framework->acl->get_group_id($user->usertype, 'ARO');
        $user->gids = array($user->gid);
        $user->sendEmail = 0;
        $user->registerDate = date('Y-m-d H:i:s', $_CB_framework->now());
        $user->name = $name;
        $user->username = $email;
        $user->email = $email;
        $user->password = $user->hashAndSaltPassword($password);
        $user->cb_address = $address;
        $user->cb_address2 = $address2;
        $user->cb_city = $city;
        $user->cb_state = $state;
        $user->cb_zip = $zip;
        $user->cb_phone = $phone;
        $user->cb_website = $website;
        $user->cb_facebook = $facebook;
        $user->cb_hours = $hours;
        $user->cb_category = $category;
        $user->registeripaddr = cbGetIPlist();

        if ($approval == 0) {
            $user->approved = 1;
        } else {
            $user->approved = 0;
        }

        if ($confirmation == 0) {
            $user->confirmed = 1;
        } else {
            $user->confirmed = 0;
        }

        if (($user->confirmed == 1) && ($user->approved == 1)) {
            $user->block = 0;
        } else {
            $user->block = 1;
        }

        $_PLUGINS->trigger('onBeforeUserRegistration', array(&$user, &$user));

        if ($user->store()) {
            if (($user->confirmed == 0) && ($confirmation != 0)) {
                $user->_setActivationCode();

                if (!$user->store()) {
                    return false;
                }
            }

            $_PLUGINS->trigger('onAfterUserRegistration', array(&$user, &$user, true));

            echo $name . ' successfully added!';
            return true;
        }

        return false;
    }

    // UPDATE USER
    public function editUser($user, $name)
    {
        global $_CB_database, $_PLUGINS;

        $oldUserComplete = new moscomprofilerUser($_CB_database);

        foreach (array_keys(get_object_vars($user)) as $k) {
            $oldUserComplete->$k = $user->$k;
        }

        $user->name = $name;

        $_PLUGINS->trigger('onBeforeUserUpdate', array(&$user, &$user, &$oldUserComplete, &$oldUserComplete));

        if ($user->store()) {
            return true;
        }

        $_PLUGINS->trigger('onAfterUserUpdate', array(&$user, &$user, $oldUserComplete));

        return false;
    }
}

Please Log in to join the conversation.

9 years 11 months ago - 9 years 11 months ago #244279 by sfraise
Well, I would say it's not an issue invoking the api as I can var dump $user after
$user = new moscomprofilerUser($_CB_database);

The code itself doesn't seem to break, however it won't store the data.
I did the following which gave me "Not Stored":
if($user->store()) {
    echo 'Stored';
} else {
    echo 'Not Stored';
}

So if the cb api is invoked ok, the code doesn't break, and this same exact function works ok outside of the controller, why would it not store the data here?

Please Log in to join the conversation.

9 years 11 months ago #244326 by sfraise
I've verified that the editUser function works fine within the controller class using ajax which really has me scratching my head as it also uses store().

Please Log in to join the conversation.

9 years 11 months ago #244352 by sfraise
3 days I've been banging my head on this thing now lol.

I've gotten the rest of the ajax functions built and working fine in the controller class including using the editUser function where store() works just fine, a searchUsers function that works great, and a deleteUser function that works fine.

Here's what I have at this point for this registerUser function in the controller class:
class membermanagerController extends JControllerLegacy
{
    // REGISTERATION USER
    public function registerUser($name, $email, $password, $address, $address2, $city, $state, $zip, $phone, $website, $facebook, $hours, $category, $approve = 0, $confirm = 0)
    {
        global $_CB_framework, $mainframe, $_CB_database, $ueConfig, $_PLUGINS;

        if (defined('JPATH_ADMINISTRATOR')) {
            if (!file_exists(JPATH_ADMINISTRATOR . '/components/com_comprofiler/plugin.foundation.php')) {
                echo 'CB not installed!';
                return;
            }

            include_once(JPATH_ADMINISTRATOR . '/components/com_comprofiler/plugin.foundation.php');
        } else {
            if (!file_exists($mainframe->getCfg('absolute_path') . '/administrator/components/com_comprofiler/plugin.foundation.php')) {
                echo 'CB not installed!';
                return;
            }

            include_once($mainframe->getCfg('absolute_path') . '/administrator/components/com_comprofiler/plugin.foundation.php');
        }
        cbimport('cb.database');
        cbimport('cb.html');
        cbimport('cb.plugins');

        $approval = ($approve == 2 ? $ueConfig['reg_admin_approval'] : $approve);
        $confirmation = ($confirm == 2 ? $ueConfig['reg_confirmation'] : $confirm);
        $usertype = $_CB_framework->getCfg('new_usertype');

        $user = new moscomprofilerUser($_CB_database);
        $user->usertype = ($usertype ? $usertype : 'Registered');
        $user->gid = $_CB_framework->acl->get_group_id($user->usertype, 'ARO');
        $user->gids = array($user->gid);
        $user->sendEmail = 0;
        $user->registerDate = date('Y-m-d H:i:s', $_CB_framework->now());
        $user->name = $name;
        $user->username = $email;
        $user->email = $email;
        $user->password = $user->hashAndSaltPassword($password);
        $user->cb_address = $address;
        $user->cb_address2 = $address2;
        $user->cb_city = $city;
        $user->cb_state = $state;
        $user->cb_zip = $zip;
        $user->cb_phone = $phone;
        $user->cb_website = $website;
        $user->cb_facebook = $facebook;
        $user->cb_hours = $hours;
        $user->cb_category = $category;
        $user->registeripaddr = cbGetIPlist();

        if ($approval == 0) {
            $user->approved = 1;
        } else {
            $user->approved = 0;
        }

        if ($confirmation == 0) {
            $user->confirmed = 1;
        } else {
            $user->confirmed = 0;
        }

        if (($user->confirmed == 1) && ($user->approved == 1)) {
            $user->block = 0;
        } else {
            $user->block = 1;
        }

        $_PLUGINS->trigger('onBeforeUserRegistration', array(&$user, &$user));

        if ($user->store()) {
            if (($user->confirmed == 0) && ($confirmation !== 0)) {
                $user->_setActivationCode();

                if (!$user->store()) {
                    echo '<div class="error_notice">Registration Failed!</div>';
                    return false;
                }
            }

            $_PLUGINS->trigger('onAfterUserRegistration', array(&$user, &$user, true));

            echo $name . ' successfully added!';
            return true;
        } else {
            echo '<div class="error_notice">Registration Failed!</div>';
            return false;
        }

        return false;
    }

    public function addMember() {
        // GET VALUES
        $jinput = JFactory::getApplication()->input;
        $name = $jinput->get('name');
        $email = $jinput->get('email');
        $password = $jinput->get('password');
        $address = $jinput->get('address');
        $address2 = $jinput->get('address2');
        $city = $jinput->get('city');
        $state = $jinput->get('state');
        $zip = $jinput->get('zip');
        $phone = $jinput->get('phone');
        $website = $jinput->get('website');
        $facebook = $jinput->get('facebook');
        $hours = $jinput->get('hours');
        $category = $jinput->get('category');

        self::registerUser($name, $email, $password, $address, $address2, $city, $state, $zip, $phone, $website, $facebook, $hours, $category);
    }
}

I've tried stripping it down to it's bare essentials by removing all of the extra fields and it still doesn't work. I've verified that all of the values are being passed. I've var_dumped the $user object to ensure that it's being created properly. I've set break points all the way down through the function and it all seems to be perfectly fine, it just simply won't store the user object in the db.

The really baffling thing is if I use this function outside of the controller/ajax method it works fine. I'm convinced there's a ghost in the machine at this point lol.

Please Log in to join the conversation.

9 years 11 months ago #244353 by sfraise
I var_dumped the $user object right before the store function, here's what I get:
object(moscomprofilerUser)#141 (59) { ["name"]=> string(14) "CodeMonkeysLLC" ["username"]=> string(25) "spencercodemonkeysllc.com" ["email"]=> string(25) "spencercodemonkeysllc.com" ["password"]=> string(34) "$P$DUyTw4/p80LMSvRCLdABhwEPZ398Zf." ["usertype"]=> string(10) "Registered" ["block"]=> int(0) ["sendEmail"]=> int(0) ["gid"]=> string(1) "2" ["gids"]=> array(1) { [0]=> string(1) "2" } ["registerDate"]=> string(19) "2014-04-24 11:43:22" ["lastvisitDate"]=> NULL ["activation"]=> NULL ["params"]=> NULL ["_cmsUserTable"]=> string(8) "#__users" ["_cmsUserTableKey"]=> string(2) "id" ["_cmsUserTableUsername"]=> string(8) "username" ["_cmsUserTableEmail"]=> string(5) "email" ["_cmsUserTableGid"]=> NULL ["_cmsUser"]=> NULL ["_comprofilerUser"]=> NULL ["_cbTabs"]=> NULL ["_nonComprofilerVars"]=> array(17) { [0]=> string(2) "id" [1]=> string(4) "name" [2]=> string(8) "username" [3]=> string(5) "email" [4]=> string(8) "password" [5]=> string(5) "block" [6]=> string(9) "sendEmail" [7]=> string(12) "registerDate" [8]=> string(13) "lastvisitDate" [9]=> string(10) "activation" [10]=> string(6) "params" [11]=> string(13) "lastResetTime" [12]=> string(10) "resetCount" [13]=> string(6) "otpKey" [14]=> string(4) "otep" [15]=> string(3) "gid" [16]=> string(4) "gids" } ["_frontendNonComprofilerVars"]=> array(5) { [0]=> string(4) "name" [1]=> string(8) "username" [2]=> string(5) "email" [3]=> string(8) "password" [4]=> string(6) "params" } ["id"]=> NULL ["user_id"]=> NULL ["firstname"]=> NULL ["middlename"]=> NULL ["lastname"]=> NULL ["hits"]=> NULL ["message_last_sent"]=> NULL ["message_number_sent"]=> NULL ["avatar"]=> NULL ["avatarapproved"]=> NULL ["approved"]=> int(1) ["confirmed"]=> int(1) ["lastupdate"]=> NULL ["registeripaddr"]=> string(3) "::1" ["cbactivation"]=> NULL ["banned"]=> NULL ["banneddate"]=> NULL ["unbanneddate"]=> NULL ["bannedby"]=> NULL ["unbannedby"]=> NULL ["bannedreason"]=> NULL ["acceptedterms"]=> NULL ["_tbl"]=> string(14) "#__comprofiler" ["_tbl_key"]=> string(2) "id" ["_db"]=> &object(CBdatabase)#144 (2) { ["_db"]=> &object(JDatabaseDriverMysqli)#24 (21) { ["name"]=> string(6) "mysqli" ["nameQuote":protected]=> string(1) "`" ["nullDate":protected]=> string(19) "0000-00-00 00:00:00" ["_database":"JDatabaseDriver":private]=> string(16) "codemon3_chamber" ["connection":protected]=> object(mysqli)#28 (19) { ["affected_rows"]=> int(1) ["client_info"]=> string(79) "mysqlnd 5.0.11-dev - 20120503 - $Id: 1514feb3700aa52d513182fcdc87f2c66f06d152 $" ["client_version"]=> int(50011) ["connect_errno"]=> int(0) ["connect_error"]=> NULL ["errno"]=> int(0) ["error"]=> string(0) "" ["error_list"]=> array(0) { } ["field_count"]=> int(1) ["host_info"]=> string(23) "198.1.66.206 via TCP/IP" ["info"]=> NULL ["insert_id"]=> int(0) ["server_info"]=> string(10) "5.5.36-cll" ["server_version"]=> int(50536) ["stat"]=> string(143) "Uptime: 4291293 Threads: 3 Questions: 6927504 Slow queries: 5 Opens: 9389 Flush tables: 1 Open tables: 386 Queries per second avg: 1.614" ["sqlstate"]=> string(5) "00000" ["protocol_version"]=> int(10) ["thread_id"]=> int(241708) ["warning_count"]=> int(0) } ["count":protected]=> int(10) ["cursor":protected]=> NULL ["debug":protected]=> bool(false) ["limit":protected]=> int(0) ["log":protected]=> array(0) { } ["timings":protected]=> array(0) { } ["callStacks":protected]=> array(0) { } ["offset":protected]=> int(0) ["options":protected]=> array(9) { ["driver"]=> string(6) "mysqli" ["host"]=> string(12) "198.1.66.206" ["user"]=> string(13) "codemon3_joey" ["password"]=> string(8) "London10" ["database"]=> string(16) "codemon3_chamber" ["prefix"]=> string(6) "tqnck_" ["select"]=> bool(true) ["port"]=> int(3306) ["socket"]=> NULL } ["sql":protected]=> string(63) "SELECT `id` FROM `#__usergroups` WHERE `title` = 'Registered'" ["tablePrefix":protected]=> string(6) "tqnck_" ["utf":protected]=> bool(true) ["errorNum":protected]=> int(0) ["errorMsg":protected]=> string(0) "" ["transactionDepth":protected]=> int(0) ["disconnectHandlers":protected]=> array(0) { } } ["_table_prefix"]=> string(6) "tqnck_" } ["_error"]=> string(0) "" ["cb_address"]=> string(12) "2071117thAve" ["cb_address2"]=> string(0) "" ["cb_city"]=> string(7) "Baldwin" ["cb_state"]=> string(9) "Wisconsin" ["cb_zip"]=> string(5) "54002" ["cb_phone"]=> string(12) "715-441-2919" ["cb_website"]=> string(23) "www.codeomonkeysllc.com" ["cb_facebook"]=> string(0) "" ["cb_hours"]=> string(14) "Mon-Fri9am-5pm" ["cb_category"]=> string(11) "ComputerWeb" }

Please Log in to join the conversation.

9 years 11 months ago - 9 years 11 months ago #244355 by sfraise
Ok, I figured it out...

I'm using the email as both the username and email address and for some reason it's stripping out the '@' when it's passed through ajax. If I hard code the email address into the registerUser function it works.

I wasn't adding a filter to the jinputs, added ,null,html and everything worked fine.
The following user(s) said Thank You: nant

Please Log in to join the conversation.

Moderators: beatnantkrileon
Time to create page: 0.203 seconds

Facebook Twitter LinkedIn