News:

You may pay someone to create your store, or you visit our seminar and become a professional yourself with the silver certification

Main Menu

Calling VM Functions from external code

Started by newsomjk, March 21, 2014, 22:18:20 PM

Previous topic - Next topic

newsomjk

Working on a plugin for our employees to manage users/subscriptions. Part of this includes creating new users.

I have the function creating the new user in joomla and putting info in the database for VM but that doesn't seem to work for VM. What's the easiest way to call the store() function in the user controller from another component?

I'm assuming instantiate the VM user model and call the store function? But I'm not quite sure how I'll get the info into the instantiated model. I'm looking through the api now.

Milbo

Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

newsomjk

Do yall read the posts here or just look at the title and find relevant links to share?

I'm looking at that already, and the API, I'm asking for a bit of help with using it. I instantiated the user object, but I need to get my array into the user object. And I can't find in the API where it lists the required fields or what the names need to be. I'm assuming it should match the userfields in the database.

And then I would call

$user_model = VmModel::getModel('user');
$u = $user_model->getCurrentUser();
$u->store($_POST);

however that isn't storing the address information in the post data.

Milbo

I read your whole post and there is no evidence or hint that you read the manual.


$user_model = VmModel::getModel('user');
$user_model->store();

Done, imho. Just check the user controller to understand how we use it.
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

newsomjk

#4
Using that code in the following function just returns "Invalid token while saving user." and doesn't put any data in the database. So far I've only been able to get data in the database by running a couple of queries after creating the joomla user. But this method leaves me with the problem of not sending the user an email with their information.

function create_user($data = NULL){

    $user = array();
    $user['fullname'] = $data['name'];
    $user['email'] = $data['email'];
    $user['password'] = $data['password'];
    $user['username'] = $data['username'];         

    $instance = JUser::getInstance();
    jimport('joomla.application.component.helper');
    $config = JComponentHelper::getParams('com_users');

    // Default to Registered.
    $defaultUserGroup = $config->get('new_usertype', 2);
    $instance->set('id', 0);
    $instance->set('name', $user['fullname']);
    $instance->set('username', $user['username']);

    jimport('joomla.user.helper');
    $salt = JUserHelper::genRandomPassword(32);
    $password_clear = $user['password'];
    $crypted    = JUserHelper::getCryptedPassword($password_clear, $salt);
    $password    = $crypted.':'.$salt;


    $instance->set('password', $password);
    $instance->set('password_clear',$password_clear);
    $instance->set('email', $user['email']);
    $instance->set('usertype', 'deprecated');
    $instance->set('groups', array($defaultUserGroup));

    //If autoregister is set let's register the user
    $autoregister = NULL;
    if(isset($options['autoregister'])){
$autoregister = $options['autoregister'];
}else{
$autoregister = $config->get('autoregister', 1);
}

if ($autoregister) {
        if (!$instance->save()) {
            return false;
        }
        else{
        if(!class_exists('VmConfig')) require(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_virtuemart'.DS.'helpers'.DS.'config.php');
if(!class_exists('shopFunctionsF')) require(JPATH_VM_SITE.DS.'helpers'.DS.'shopfunctionsf.php');

        $config = VmConfig::loadConfig();       

          //$db = JFactory::getDBO();
//$customer_nr = uniqid(rand());

$my_id = $instance->get('id');
$data['virtuemart_user_id'] = $my_id;

$user_model = VmModel::getModel('user');
$user_model->store();
}
    }
    else {
        $instance->set('tmp_user', true);
    }
}

Milbo

You must set in your function the formtoken. This is a joomla feature.
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/