Skip to content

Instantly share code, notes, and snippets.

@nielsnuebel
Created October 12, 2015 15:52
Show Gist options
  • Save nielsnuebel/6b3fc6091352af63ca5e to your computer and use it in GitHub Desktop.
Save nielsnuebel/6b3fc6091352af63ca5e to your computer and use it in GitHub Desktop.
<?php
/**
* HQSUser
*
* @package HQSUser-cli
* @author Niels Nübel <[email protected]>
*
* @copyright 2015 HQS
* @license GNU General Public License version 2 or later
*/
error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', 1);
define('_JEXEC', 1);
define('JPATH_BASE', dirname(__DIR__));
// Load system defines
if (file_exists(JPATH_BASE . '/defines.php'))
{
require_once JPATH_BASE . '/defines.php';
}
if (!defined('_JDEFINES'))
{
require_once JPATH_BASE . '/includes/defines.php';
}
// Get the framework.
require_once JPATH_LIBRARIES . '/import.legacy.php';
require_once JPATH_LIBRARIES . '/cms.php';
// Load the configuration
require_once JPATH_CONFIGURATION . '/configuration.php';
// Load the JApplicationCli class
JLoader::import('joomla.application.cli');
JLoader::import('joomla.application.component.helper');
JLoader::import('cms.component.helper');
JLoader::import('cms.plugin.helper');
class HQSUser extends JApplicationWeb
{
/**
* Entry point for the script
*
* @return void
*/
public function doExecute()
{
$params = JComponentHelper::getParams('com_hqs');
$db = JFactory::getDBO();
$date = JFactory::getDate();
$date->modify($params->get('cronhours','+2') . ' hours');
$query = $db->getQuery(true);
$query->select('u.userid,u.id,u.title,u.email,u.block,u.kdnr,u.created_by')->from('#__hqs_customers AS u')->where($db->quoteName('u.update') . ' = 1');
$db->setQuery($query);
$customers = $db->loadObjectList();
foreach ($customers as $customer)
{
$query->select('a.branche,a.subbranche');
$query->from($db->quoteName('#__hqs_companies') . ' AS a');
$query->where('a.kdnr = ' . (int) $customer->kdnr);
$db->setQuery($query,0,1);
$result = $db->loadObject();
$query->clear();
$cats = array();
$cats[] = $result->branche;
if($result->subbranche != "")
$cats = array_merge($cats,explode(',',$result->subbranche));
$query->select('a.usergroups')
->from('#__hqs_categories AS a')
->join('LEFT', $db->quoteName('#__hqs_categories') . ' AS b ON a.lft > b.lft AND a.rgt < b.rgt');
$query->where('b.id = ' . (int) $params->get('customer_categories'));
$cats = $db->quote($cats);
$titles = implode(',', $cats);
$query->where('a.title IN (' . $titles . ')');
$db->setQuery($query);
$result = $db->loadObjectList();
$query->clear();
$usergroups = array();
foreach($result as $usergroup)
{
$usergroups = array_unique(array_merge($usergroups,explode(',',$usergroup->usergroups)));
}
if(!$customer->created_by)
{
$createdby = $this->createJoomlaUser($customer, $usergroups);
}
else
{
}
if(!$createdby) continue;
$query->update('#__hqs_customers')
->set($db->quoteName('update') .' = ' . (int) 0)
->set('created_by = ' . (int) $createdby)
->set('modified = "' . $date->toSql().'"')
->set('modified_by = ' . (int) $params->get('cronuser',185))
->where('id = '.$customer->id);
if($customer->block)
{
$query->set($db->quoteName('state') .' = ' . (int) 0);
}
$db->setQuery($query)->execute();
$query->clear();
}
}
private function createJoomlaUser($customer, $usergroups)
{
// Initialise the table with JUser.
$user = new JUser;
// Prepare the data for the user object.
$data['username'] = $customer->userid;
$data['name'] = $customer->title;
$data['email'] = $customer->email ;
$data['block'] = $customer->block;
$data['password'] = JUserHelper::genRandomPassword();
$data['registerDate'] = JFactory::getDate()->toSql();
// Bind the data.
if (!$user->bind($data))
{
$this->setError('Bind data to User failed');
return false;
}
// User Groups
$comUserParams = JComponentHelper::getParams('com_users');
$systemUserGroup = (array) $comUserParams->get('new_usertype', 2);
if(!is_null($usergroups))
$groups = array_unique(array_merge($systemUserGroup,$usergroups));
else
$groups = $systemUserGroup;
$user->groups = $groups;
if (!$user->save())
{
$this->setError($user->getError());
return false;
}
$userid = $user->get('id');
return $userid;
}
}
HQSUser::getInstance('HQSUser')->execute();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment