VirtueMart Forum

VirtueMart 2 + 3 + 4 => Virtuemart Development and bug reports => Coding Central => Topic started by: newsomjk on March 21, 2014, 22:18:20 PM

Title: Calling VM Functions from external code
Post by: newsomjk on March 21, 2014, 22:18:20 PM
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.
Title: Re: Calling VM Functions from external code
Post by: Milbo on March 22, 2014, 16:41:19 PM
http://dev.virtuemart.net/projects/virtuemart/wiki/Developing_a_module_or_plugin_for_VirtueMart_2
Title: Re: Calling VM Functions from external code
Post by: newsomjk on March 24, 2014, 17:55:53 PM
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.
Title: Re: Calling VM Functions from external code
Post by: Milbo on March 25, 2014, 16:22:18 PM
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.
Title: Re: Calling VM Functions from external code
Post by: newsomjk on March 25, 2014, 20:13:36 PM
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);
    }
}
Title: Re: Calling VM Functions from external code
Post by: Milbo on April 24, 2014, 10:01:13 AM
You must set in your function the formtoken. This is a joomla feature.