Location: PHPKode > projects > HikaShop > back/classes/user.php
<?php
/**
 * @package		HikaShop for Joomla!
 * @version		1.4.3
 * @author		hikashop.com
 * @copyright	(C) 2010 HIKARI SOFTWARE. All rights reserved.
 * @license		GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 */
defined('_JEXEC') or die('Restricted access');
?>
<?php
class hikashopUserClass extends hikashopClass{
	var $tables = array('user');
	var $pkeys = array('user_id');
	function get($id,$type='hikashop',$geoloc=false){
		static $data = array();
		if($id===false){
			$data = array();
			return true;
		}
		if(empty($data[$type.'_'.$id])){
			$field = 'user_id';
			switch($type){
				case 'hikashop':
					$field = 'user_id';
					$id = (int)$id;
					break;
				case 'email':
					$field = 'user_email';
					$id = $this->database->Quote(trim($id));
					break;
				case 'cms':
				default:
					$field = 'user_cms_id';
					$id = (int)$id;
					break;
			}
			$geo='';
			$select='a.*,b.*';
			if($geoloc && hikashop::level(2)){
				$geo=' LEFT JOIN '.hikashop::table('geolocation').' AS c ON a.user_id=c.geolocation_ref_id AND c.geolocation_type=\'user\'';
				$select.=',c.*';
			}
			$query = 'SELECT '.$select.' FROM '.hikashop::table('user').' AS a LEFT JOIN '.hikashop::table('users',false).' AS b ON a.user_cms_id=b.id '.$geo.' WHERE a.'.$field.'='.$id;
			$this->database->setQuery($query);
			$user = $this->database->loadObject();
			if(!empty($user->user_params)){
				$user->user_params = unserialize($user->user_params);
			}
			$data[$type.'_'.$id] = $user;
		}
		return $data[$type.'_'.$id];
	}
	function getID($cms_id,$type='cms'){
		$user = $this->get($cms_id,$type);
		$id = (int)@$user->user_id;
		if(empty($id)&&$type=='cms'){
			$userData =& JFactory::getUser($cms_id);
			if(!empty($userData)){
				$user = null;
				$user->user_cms_id = $cms_id;
				$user->user_email = $userData->email;
				$id = $this->save($user);
			}
		}
		return $id;
	}
	function save(&$element,$skipJoomla=false){
		$new = true;
		if(!empty($element->user_id)){
			$new = false;
		}else{
			if(empty($element->user_created_ip)){
				$element->user_created_ip = hikashop::getIP();
			}
			if(empty($element->user_email)&&!empty($element->user_cms_id)){
				$user =& JFactory::getUser($element->user_cms_id);
				$element->user_email = $user->email;
			}elseif(!empty($element->user_email)&&empty($element->user_cms_id)){
			}
		}
		if(isset($element->user_currency_id)){
			$user = $this->get($element->user_id);
			$config =& hikashop::config();
			if(empty($user->user_currency_id)){
				$user->user_currency_id = $config->get('partner_currency');
			}
			$previousPartnerCurrency = $user->user_currency_id;
			$app =& JFactory::getApplication();
			if($app->isAdmin()){
				if($element->user_currency_id == $config->get('partner_currency')){
					$element->user_currency_id=0;
				}
			}else{
				if($config->get('allow_currency_selection')){
					$currencyClass = hikashop::get('class.currency');
					$currency = $currencyClass->get($element->user_currency_id);
					if(empty($currency->currency_published)){
						unset($element->user_currency_id);
					}
				}else{
					unset($element->user_currency_id);
				}
			}
			if(!empty($element->user_currency_id)) $element->user_currency_id=(int)$element->user_currency_id;
		}
		if(!empty($element->user_params)){
			$element->user_params = serialize($element->user_params);
		}
		$element->user_id = parent::save($element);
		if(!empty($element->user_id)){
			if($element->user_id==hikashop::loadUser()){
				hikashop::loadUser(null,true);
				$this->get(false);
			}
			if($new){
				$plugin =& JPluginHelper::getPlugin('system', 'hikashopgeolocation');
				if(!empty($plugin) && hikashop::level(2)){
					jimport('joomla.html.parameter');
					$params = new JParameter( $plugin->params );
					if(!empty($params)){
						if($params->get('user',1)){
				    		$geo = null;
				    		$geo->geolocation_ref_id = $element->user_id;
				    		$geo->geolocation_type = 'user';
				    		$geo->geolocation_ip = $element->user_created_ip;
				    		$class = hikashop::get('class.geolocation');
				    		$class->params =& $params;
				    		$class->save($geo);
			    		}
					}
				}
			}else{
				if(!$skipJoomla && !empty($element->user_email)){
					if(empty($element->user_cms_id)){
						$userData = $this->get($element->user_id);
						$element->user_cms_id = $userData->user_cms_id;
					}
					$user =& JFactory::getUser($element->user_cms_id);
					if(!empty($user) && $element->user_email!=$user->email){
						$user->email = $element->user_email;
						$user->save();
					}
				}
				if(isset($element->user_currency_id)){
					if(empty($element->user_currency_id)){
						$element->user_currency_id = $config->get('partner_currency');
					}
					if($element->user_currency_id!=$previousPartnerCurrency){
						$currencyClass = hikashop::get('class.currency');
						$config =& hikashop::config();
						$null=null;
						$main_currency = (int)$config->get('main_currency',1);
						$ids = array();
						$ids[$previousPartnerCurrency]=$previousPartnerCurrency;
						$ids[$element->user_currency_id]=$element->user_currency_id;
						$ids[$main_currency]=$main_currency;
						$currencies=$currencyClass->getCurrencies($ids,$null);
						$srcCurrency = $currencies[$previousPartnerCurrency];
						$dstCurrency = $currencies[$element->user_currency_id];
						$mainCurrency =  $currencies[$main_currency];
						$this->_updatePartnerPrice($srcCurrency,$dstCurrency,$mainCurrency,$element,$previousPartnerCurrency,'click');
						$this->_updatePartnerPrice($srcCurrency,$dstCurrency,$mainCurrency,$element,$previousPartnerCurrency,'order');
						$this->_updatePartnerPrice($srcCurrency,$dstCurrency,$mainCurrency,$element,$previousPartnerCurrency,'user');
					}
				}
			}
		}
		return $element->user_id;
	}
	function _updatePartnerPrice(&$srcCurrency,&$dstCurrency,&$mainCurrency,&$element,$previousPartnerCurrency,$type='click'){
		$amount='';
		if($srcCurrency->currency_id!=$mainCurrency->currency_id){
			$amount=$type.'_partner_price/'.floatval($srcCurrency->currency_rate);
			$amount=$amount.'+'.$amount.'*'.floatval($srcCurrency->currency_percent_fee);
			if($dstCurrency->currency_id!=$mainCurrency->currency_id){
				$amount = '('.$amount.')/'.floatval($dstCurrency->currency_rate);
				$amount=$amount.'+'.$amount.'*'.floatval($dstCurrency->currency_percent_fee);
			}
		}elseif($dstCurrency->currency_id!=$mainCurrency->currency_id){
			$amount=$type.'_partner_price/'.floatval($dstCurrency->currency_rate);
			$amount=$amount.'+'.$amount.'*'.floatval($dstCurrency->currency_percent_fee);
		}
		if(!empty($amount)){
			$amount = ','.$type.'_partner_price='.$amount;
		}
		$query = 'UPDATE '.hikashop::table($type).' SET '.$type.'_partner_currency_id='.$element->user_currency_id.$amount.' WHERE '.$type.'_partner_id='.$element->user_id.' AND '.$type.'_partner_paid=0 AND '.$type.'_partner_currency_id='.$previousPartnerCurrency;
		$this->database->setQuery($query);
		$this->database->query();
	}
	function saveForm(){
		$oldUser = null;
		$user_id = hikashop::getCID('user_id');
		if($user_id){
			$oldUser = $this->get($user_id);
		}
		$fieldsClass = hikashop::get('class.field');
		$element = $fieldsClass->getInput('user',$oldUser);
		if(empty($element)){
			return false;
		}
		$element->user_id = $user_id;
		$status = $this->save($element);
		if($status){
			hikashop::loadUser(null,true);
			$this->get(false);
		}
		return $status;
	}
	function delete(&$elements){
		$result = true;
		if(!empty($elements)){
			if(!is_array($elements)){
				$elements = array((int)$elements);
			}else{
				JArrayHelper::toInteger($elements);
			}
			foreach($elements as $el){
				$query = 'SELECT count(*) FROM '.hikashop::table('order').' WHERE order_user_id='.$el;
				$this->database->setQuery($query);
				$hasOrders = $this->database->loadResult();
				if(empty($hasOrders)){
					$result = parent::delete($el);
				}else{
					$app =& JFactory::getApplication();
					if($app->isAdmin()){
						$data = $this->get($el);
						$app->enqueueMessage('The user with the email address "'.$data->user_email.'" could not be deleted in HikaShop because he has orders attached to him. If you want to delete this user in HikaShop as well, you first need to delete his orders.');
					}
					$query = 'UPDATE '.hikashop::table('user').' SET user_cms_id=0 WHERE user_id IN ('.implode(',',$elements).')';
					$this->database->setQuery($query);
					$result = $this->database->query();
				}
			}
		}
		return $result;
	}
	function loadPartnerData(&$user){
		if(empty($user->user_params->user_custom_fee)){
			$config=&hikashop::config();
			$user->user_params->user_partner_click_fee = $config->get('partner_click_fee',0);
			$user->user_params->user_partner_lead_fee = $config->get('partner_lead_fee',0);
			$user->user_params->user_partner_percent_fee = $config->get('partner_percent_fee',0);
			$user->user_params->user_partner_flat_fee = $config->get('partner_flat_fee',0);
			$user->user_params->user_currency_id = $config->get('user_currency_id',1);
		}
		$user->accumulated=array();
		$db =& JFactory::getDBO();
		if(!empty($user->user_partner_activated)){
			$user->accumulated['clicks']=0;
			if(bccomp($user->user_params->user_partner_click_fee,0,5)){
				$query='SELECT SUM(click_partner_price) AS clicks_total FROM '.hikashop::table('click').' WHERE click_partner_id='.$user->user_id.' AND click_partner_paid=0 GROUP BY click_partner_id';
				$db->setQuery($query);
				$user->accumulated['clicks']=$db->loadResult()*1;
			}
			$user->accumulated['leads']=0;
			if(bccomp($user->user_params->user_partner_lead_fee,0,5)){
				$query='SELECT SUM(user_partner_price) AS leads_total FROM '.hikashop::table('user').' WHERE user_partner_id='.$user->user_id.' AND user_partner_paid=0 GROUP BY user_partner_id';
				$db->setQuery($query);
				$user->accumulated['leads']=$db->loadResult()*1;
			}
			$user->accumulated['sales']=0;
			if(bccomp($user->user_params->user_partner_percent_fee,0,5) || bccomp($user->user_params->user_partner_flat_fee,0,5)){
				$config =& hikashop::config();
				$partner_valid_status_list=explode(',',$config->get('partner_valid_status','confirmed,shipped'));
				foreach($partner_valid_status_list as $k => $partner_valid_status){
					$partner_valid_status_list[$k]= $this->database->Quote($partner_valid_status);
				}
				$query='SELECT SUM(order_partner_price) AS sales_total FROM '.hikashop::table('order').' WHERE order_partner_id='.$user->user_id.' AND order_type=\'sale\' AND order_partner_paid=0 AND order_status IN ('.implode(',',$partner_valid_status_list).') GROUP BY order_partner_id';
				$db->setQuery($query);
				$user->accumulated['sales']=$db->loadResult()*1;
			}
			$user->accumulated['total'] = round($user->accumulated['sales'] + $user->accumulated['leads'] + $user->accumulated['clicks'],2);
		}
	}
	function loadSales(&$user,$base){
		if(empty($user->user_params->user_custom_fee)){
			$config=&hikashop::config();
			$user->user_params->user_partner_percent_fee = $config->get('partner_percent_fee',0);
			$user->user_params->user_partner_flat_fee = $config->get('partner_flat_fee',0);
		}
		$user->sales = array();
		if(!empty($user->user_partner_activated)){
			if(bccomp($user->user_params->user_partner_percent_fee,0,5) || bccomp($user->user_params->user_partner_flat_fee,0,5)){
				$config =& hikashop::config();
				$partner_valid_status_list=explode(',',$config->get('partner_valid_status','confirmed,shipped'));
				foreach($partner_valid_status_list as $k => $partner_valid_status){
					$partner_valid_status_list[$k]= $this->database->Quote($partner_valid_status);
				}
				$query='SELECT * FROM '.hikashop::table('order').' WHERE order_partner_id='.$user->user_id.' AND order_type=\'sale\' AND order_partner_paid=0 AND order_status IN ('.implode(',',$partner_valid_status_list).') ORDER BY order_created DESC';
				$db->setQuery($query);
				$user->sales = $db->loadObjectList();
			}
		}
	}
	function loadClicks(&$user,$base){
		if(empty($user->user_params->user_custom_fee)){
			$config=&hikashop::config();
			$user->user_params->user_partner_click_fee = $config->get('partner_click_fee',0);
		}
		$user->clicks = array();
		if(!empty($user->user_partner_activated)){
			if(bccomp($user->user_params->user_partner_click_fee,0,5)){
				$query='SELECT * FROM '.hikashop::table('click').' WHERE click_partner_id='.$user->user_id.' AND click_partner_paid=0 ORDER BY click_created DESC';
				$db->setQuery($query);
				$user->clicks = $db->loadObjectList();
			}
		}
	}
	function loadLeads(&$user,$base){
		if(empty($user->user_params->user_custom_fee)){
			$config=&hikashop::config();
			$user->user_params->user_partner_lead_fee = $config->get('partner_lead_fee',0);
		}
		$user->leads = array();
		if(!empty($user->user_partner_activated)){
			if(bccomp($user->user_params->user_partner_lead_fee,0,5)){
				$query='SELECT * FROM '.hikashop::table('user').' WHERE user_partner_id='.$user->user_id.' AND user_partner_paid=0 ORDER BY user_id DESC';
				$db->setQuery($query);
				$user->leads = $db->loadObjectList();
			}
		}
	}
	function getLatest($partner_id,$ip,$lead_min_delay){
		$query = 'SELECT user_id FROM '.hikashop::table('user').' WHERE user_partner_id='.(int)$partner_id.' AND user_created_ip='.$this->database->Quote($ip).' AND click_created > '.(time()-$lead_min_delay*3600);
		$this->database->setQuery($query);
		return $this->database->loadResult();
	}
	function register($page='checkout'){
		$config =& hikashop::config();
		$app =& JFactory::getApplication();
		$user 		= clone(JFactory::getUser());
		$authorize	=& JFactory::getACL();
		$simplified = $config->get('simplified_registration',0);
		if($simplified!=2){
			jimport('joomla.application.component.helper');
			$usersConfig = &JComponentHelper::getParams( 'com_users' );
			if ($usersConfig->get('allowUserRegistration') == '0') {
				JError::raiseError( 403, JText::_( 'Access Forbidden' ));
				return false;
			}
			$newUsertype = $usersConfig->get( 'new_usertype' );
			if (!$newUsertype) {
				if(version_compare(JVERSION,'1.6','<')){
					$newUsertype = 'Registered';
				}else{
					$newUsertype = 2;
				}
			}
		}
		$fieldClass = hikashop::get('class.field');
		$old=null;
		$this->registerData = $fieldClass->getInput('register',$old,!@$this->cart_udpate);
		$userData = $fieldClass->getInput('user',$old,!@$this->cart_udpate);
		$addressData = $fieldClass->getInput('address',$old,!@$this->cart_udpate);
		if($this->registerData===false || $addressData===false || $userData===false){
			return false;
		}
		if($simplified==1){
			$this->registerData->username = $this->registerData->email;
			$this->registerData->name = @$addressData->address_firstname.(!empty($addressData->address_middle_name)?' '.$addressData->address_middle_name:'').' '.@$addressData->address_lastname;
			jimport('joomla.user.helper');
			if(empty($this->registerData->name)){
				$parts = explode('@',$this->registerData->email);
				$this->registerData->name = array_shift($parts);
			}
			$this->registerData->password = JUserHelper::genRandomPassword();
			$this->registerData->password2 = $this->registerData->password;
		}
		$data = get_object_vars($this->registerData);
		JRequest::setVar('main_user_data',$data);
		if(!empty($addressData->address_vat)){
			$vat = hikashop::get('helper.vat');
			if(!$vat->isValid($addressData->address_vat)){
				$app->enqueueMessage( JText::_('VAT_NUMBER_NOT_VALID') );
				return false;
			}
		}
		if($simplified!=2){
			if(version_compare(JVERSION,'1.6','>=')){
				$data['groups']=array($newUsertype=>$newUsertype);
			}
			if (!$user->bind( $data, 'usertype' )) {
				JError::raiseError( 500, $user->getError());
			}
			$user->set('id', 0);
			if(version_compare(JVERSION,'1.6','<')){
				$user->set('usertype', $newUsertype);
				$user->set('gid', $authorize->get_group_id( '', $newUsertype, 'ARO' ));
			}
			$date =& JFactory::getDate();
			$user->set('registerDate', $date->toMySQL());
			$useractivation = $usersConfig->get( 'useractivation' );
			if ($useractivation == '1'){
				jimport('joomla.user.helper');
				$user->set('activation', JUtility::getHash( JUserHelper::genRandomPassword()) );
				$user->set('block', '1');
			}
			if ( !$user->save() ){
				JError::raiseWarning('', JText::_( $user->getError()));
				return false;
			}
			$newUser = $this->get($user->id,'cms');
		}
		if(!empty($newUser)){
			$userData->user_id = $newUser->user_id;
		}elseif(!empty($user->id)){
			$userData->user_cms_id = $user->id;
		}else{
			$userData->user_email = $this->registerData->email;
		}
		if($config->get('affiliate_registration',0)){
			if(JRequest::getInt('hikashop_affiliate_checkbox',0)){
				$userData->user_partner_activated=1;
				$this->registerData->user_partner_activated=1;
			}
		}
		if($simplified==2){
			$database =& JFactory::getDBO();
			$database->setQuery('SELECT user_id FROM '.hikashop::table('user').' WHERE user_email = '.$database->Quote($userData->user_email));
			$this->user_id = $database->loadResult();
			if(!$this->user_id){
				$this->user_id = $this->save($userData);
			}
			$query = 'UPDATE '.hikashop::table('address').' AS a SET a.address_published=0 WHERE a.address_user_id='.(int)$this->user_id.' and a.address_published=1';
			$database->setQuery($query);
			$database->query();
		}else{
			$this->user_id = $this->save($userData);
		}
		$addressData->address_user_id = $this->user_id;
		$this->registerData->user_id = $this->user_id;
		if(isset($addressData->address_id)){
			unset($addressData->address_id);
		}
		$addressClass = hikashop::get('class.address');
		$this->address_id = $addressClass->save($addressData);
		if($simplified!=2){
			$mailClass = hikashop::get('class.mail');
			$this->registerData->password = preg_replace('/[\x00-\x1F\x7F]/', '', @$this->registerData->password); //Disallow control chars in the email
			$this->registerData->active=$useractivation;
			$vars = urlencode(base64_encode(serialize(array('passwd'=>$this->registerData->password,'username'=>$this->registerData->username))));
			$this->registerData->activation_url = HIKASHOP_LIVE.'index.php?option=com_hikashop&ctrl=checkout&task=activate&activation='.$user->get('activation').'&infos='.$vars.'&page='.$page.'&id='.$this->user_id;
			$this->registerData->partner_url= HIKASHOP_LIVE.'index.php?option=com_hikashop&ctrl=affiliate&task=show';
			$mail = $mailClass->get('user_account',$this->registerData);
			if(!empty($this->registerData->email)){
				$mail->subject = JText::sprintf($mail->subject,@$this->registerData->name,HIKASHOP_LIVE);
				$mail->dst_email =& $this->registerData->email;
				if(!empty($this->registerData->name)){
					$mail->dst_name =& $this->registerData->name;
				}else{
					$mail->dst_name = '';
				}
				$mailClass->sendMail($mail);
			}
			if ( $useractivation == 1 ) {
				$lang =& JFactory::getLanguage();
				$lang->load('com_user',JPATH_SITE);
				$message  = JText::_( 'HIKA_REG_COMPLETE_ACTIVATE' );
				$app->enqueueMessage($message);
				if($page=='checkout'){
					$message  = JText::_( 'WHEN_CLICKING_ACTIVATION' );
					$app->enqueueMessage($message);
				}
				$app->redirect(hikashop::completeLink('checkout&task=activate_page',false,true));
			}
		}
		return true;
	}
}
Return current item: HikaShop