Location: PHPKode > projects > HikaShop > back/helpers/helper.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
jimport('joomla.application.component.controller');
jimport( 'joomla.application.component.view');
class hikashop{
	function getDate($time = 0,$format = '%d %B %Y %H:%M'){
		if(empty($time)) return '';
		static $timeoffset = null;
		if($timeoffset === null){
			$config =& JFactory::getConfig();
			$timeoffset = $config->getValue('config.offset');
		}
		if(version_compare(JVERSION,'1.6.0','>=')) $format = str_replace(array('%A','%d','%B','%m','%Y','%y','%H','%M','%S'),array('l','d','F','m','Y','y','H','i','s'),$format);
		if(is_numeric($format)) $format = JText::_('DATE_FORMAT_LC'.$format);
		return JHTML::_('date',$time- date('Z'),$format,$timeoffset);
	}
	function isAllowed($allowedGroups){
		$my =& JFactory::getUser();
		if(!is_array($allowedGroups)) $allowedGroups = explode(',',$allowedGroups);
		if(version_compare(JVERSION,'1.6.0','<')){
			return in_array($my->gid,$allowedGroups);
		}else{
			jimport('joomla.access.access');
			$userGroups = JAccess::getGroupsByUser($my->id, true);//$my->authorisedLevels();
			$inter = array_intersect($userGroups,$allowedGroups);
			if(empty($inter)) return false;
			return true;
		}
	}
	function addACLFilters(&$filters,$field,$table='',$level=2){
		if(hikashop::level($level)){
			$my =& JFactory::getUser();
			if(version_compare(JVERSION,'1.6.0','<')){
				$userGroups = array($my->gid);
			}else{
				jimport('joomla.access.access');
				$userGroups = JAccess::getGroupsByUser($my->id, true);//$my->authorisedLevels();
			}
			if(!empty($userGroups)){
				if(!empty($table)){
					$table.='.';
				}
				$acl_filters = array($table.$field." = 'all'");
				foreach($userGroups as $userGroup){
					$acl_filters[]=$table.$field." LIKE '%,".(int)$userGroup.",%'";
				}
				$filters[]='('.implode(' OR ',$acl_filters).')';
			}
		}
	}
	function currentURL($checkInRequest=''){
		if(!empty($checkInRequest)){
			$url = JRequest::getVar($checkInRequest,'');
			if(!empty($url)){
				return urldecode($url);
			}
		}
		if(isset($_SERVER["REQUEST_URI"])){
			$requestUri = $_SERVER["REQUEST_URI"];
		}else{
			$requestUri = $_SERVER['PHP_SELF'];
			if (!empty($_SERVER['QUERY_STRING'])) $requestUri = rtrim($requestUri,'/').'?'.$_SERVER['QUERY_STRING'];
		}
		return ((empty($_SERVER['HTTPS']) OR strtolower($_SERVER['HTTPS']) != "on" ) ? 'http://' : 'https://').$_SERVER["HTTP_HOST"].$requestUri;
	}
	function getTime($date){
		static $timeoffset = null;
		if($timeoffset === null){
			$config =& JFactory::getConfig();
			$timeoffset = $config->getValue('config.offset');
		}
		return strtotime($date) - $timeoffset *60*60 + date('Z');
	}
	function getIP(){
		$ip = '';
		if( !empty($_SERVER['HTTP_X_FORWARDED_FOR']) AND strlen($_SERVER['HTTP_X_FORWARDED_FOR'])>6 ){
	        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
	    }elseif( !empty($_SERVER['HTTP_CLIENT_IP']) AND strlen($_SERVER['HTTP_CLIENT_IP'])>6 ){
			 $ip = $_SERVER['HTTP_CLIENT_IP'];
		}elseif(!empty($_SERVER['REMOTE_ADDR']) AND strlen($_SERVER['REMOTE_ADDR'])>6){
			 $ip = $_SERVER['REMOTE_ADDR'];
	    }//endif
		return strip_tags($ip);
	}
	function encode(&$data,$type='order') {
		if(is_object($data)){
			$id = $data->order_id;
		}else{
			$id = $data;
		}
		if(is_object($data) && $type=='order' && hikashop::level(1)){
			$config =& hikashop::config();
			$format = $config->get('order_number_format','{automatic_code}');
			if(preg_match('#\{id *(?:size="(.*)")? *\}#Ui',$format,$matches)){
				$copy = $id;
				if(!empty($matches[1])){
					$copy = sprintf('%0'.$matches[1].'d', $copy);
				}
				$format = str_replace($matches[0],$copy,$format);
			}
			$matches=null;
			if(preg_match('#\{date *format="(.*)" *\}#Ui',$format,$matches)){
				$format = str_replace($matches[0],date($matches[1],$data->order_created),$format);
			}
			if(strpos($format,'{automatic_code}')!==false){
			    $format = str_replace('{automatic_code}',hikashop::base($id),$format);
			}
			return $format;
		}
		return hikashop::base($id);	   
	}
	function base($id){
		$base=23;
		$chars='ABCDEFGHJKLMNPQRSTUWXYZ';
	    $str = '';
	    $val2=(string)$id;
	    do {
	        $i = $id % $base;
	        $str = $chars[$i].$str;
	        $id = ($id - $i) / $base;
	    } while($id > 0);
	    $str2='';
	    $size = strlen($val2);
	    for($i=0;$i<$size;$i++){
	    	if(isset($str[$i]))$str2.=$str[$i];
	    	$str2.=$val2[$i];
	    }
	    if($i<strlen($str)){
	    	$str2.=substr($str,$i);
	    }
	    return $str2;
	}
	function decode($str,$type='order') {
		$config =& hikashop::config();
		if($type=='order' && hikashop::level(1)){
			$format = $config->get('order_number_format','{automatic_code}');
			$format = str_replace(array('^','$','.','[',']','|','(',')','?','*','+'),array('\^','\$','\.','\[','\]','\|','\(','\)','\?','\*','\+'),$format);
			if(preg_match('#\{date *format="(.*)" *\}#Ui',$format,$matches)){
				$format = str_replace($matches[0],'(?:'.preg_replace('#[a-z]+#i','[0-9a-z]+',$matches[1]).')',$format);
			}
			if(preg_match('#\{id *(?:size="(.*)")? *\}#Ui',$format,$matches)){
				$format = str_replace($matches[0],'([0-9]+)',$format);
			}
			if(strpos($format,'{automatic_code}')!==false){
			    $format = str_replace('{automatic_code}','([0-9a-z]+)',$format);
			}
			$format = str_replace(array('{','}'),array('\{','\}'),$format);
			if(preg_match('#'.$format.'#i',$str,$matches)){
				foreach($matches as $i => $match){
					if($i){
						return ltrim(preg_replace('#[^0-9]#','',$match),'0');
					}
				}
			}
		}
		return preg_replace('#[^0-9]#','',$str);
	}
	function &array_path(&$array, $path) {
	   settype($path, 'array');
	   $offset =& $array;
	   foreach ($path as $index) {
	       if (!isset($offset[$index])) {
	           return false;
	       }
	       $offset =& $offset[$index];
	   }
	   return $offset;
	}
	function toFloat($val){
		if(preg_match_all('#[0-9]+#',$val,$parts) && count($parts[0])>1){
			$dec=array_pop($parts[0]);
			return (float) implode('',$parts[0]).'.'.$dec;
		}
		return (float) $val;
	}
	function loadUser($full=false,$reset=false){
		static $user= null;
		if($reset){
			$user=null;
			return true;
		}
		if(!isset($user)){
			$app =& JFactory::getApplication();
			$user_id = (int)$app->getUserState( HIKASHOP_COMPONENT.'.user_id' );
			$class = hikashop::get('class.user');
			if(empty($user_id)){
				$userCMS =& JFactory::getUser();
				if(!$userCMS->guest){
					$user_id = $class->getID($userCMS->get('id'));
				}else{
					return $user;
				}
			}
			$user = $class->get($user_id);
		}
		if($full){
			return $user;
		}else{
			return $user->user_id;
		}
	}
	function getZone($type='shipping'){
		$app =& JFactory::getApplication();
		$shipping_address=$app->getUserState( HIKASHOP_COMPONENT.'.'.$type.'_address',0);
		$zone_id =0;
		if(!empty($shipping_address)){
			$addressClass = hikashop::get('class.address');
			$address = $addressClass->get($shipping_address);
			if(!empty($address)){
				$field = 'address_country';
				if(!empty($address->address_state)){
					$field = 'address_state';
				}
				$zoneClass = hikashop::get('class.zone');
				$zone = $zoneClass->get($address->$field);
				if(!empty($zone)){
					$zone_id = $zone->zone_id;
				}
			}
		}
		if(empty($zone_id)){
			$zone_id =$app->getUserState( HIKASHOP_COMPONENT.'.zone_id', 0 );
			if(empty($zone_id)){
				$config =& hikashop::config();
				$zone_id = explode(',',$config->get('main_tax_zone',$zone_id));
				if(count($zone_id)){
					$zone_id = array_shift($zone_id);
				}else{
					$zone_id=0;
				}
				$app->setUserState( HIKASHOP_COMPONENT.'.zone_id', $zone_id );
			}
		}
		return (int)$zone_id;
	}
	function getCurrency(){
		$config =& hikashop::config();
		$main_currency = (int)$config->get('main_currency',1);
		$app =& JFactory::getApplication();
		$currency_id = (int)$app->getUserState( HIKASHOP_COMPONENT.'.currency_id', $main_currency );
		if($currency_id!=$main_currency && !$app->isAdmin()){
			static $checked = array();
			if(!isset($checked[$currency_id])){
				$checked[$currency_id]=true;
				$db =& JFactory::getDBO();
				$db->setQuery('SELECT currency_id FROM '.hikashop::table('currency').' WHERE currency_id = '.$currency_id. ' AND ( currency_published=1 OR currency_displayed=1 )');
				$currency_id = $db->loadResult();
			}
		}
		if(empty($currency_id)){
			$app->setUserState( HIKASHOP_COMPONENT.'.currency_id', $main_currency );
			$currency_id=$main_currency;
		}
		return $currency_id;
	}
	function cleanCart(){
		$config =& hikashop::config();
		$period = $config->get('cart_retaining_period');
		$check = $config->get('cart_retaining_period_check_frequency',86400);
		$checked = $config->get('cart_retaining_period_checked',0);
		$max = time()-$check;
		if(!$checked || $checked<$max){
			$query = 'SELECT cart_id FROM '.hikashop::table('cart').' WHERE cart_modified < '.(time()-$period);
			$database =& JFactory::getDBO();
			$database->setQuery($query);
			$ids = $database->loadResultArray();
			if(!empty($ids)){
				$query = 'DELETE FROM '.hikashop::table('cart_product').' WHERE cart_id IN ('.implode(',',$ids).')';
				$database->setQuery($query);
				$database->query();
				$query = 'DELETE FROM '.hikashop::table('cart').' WHERE cart_id IN ('.implode(',',$ids).')';
				$database->setQuery($query);
				$database->query();
			}
			$config->save(array('cart_retaining_period_checked'=>time()));
		}
	}
	function import( $type, $name, $dispatcher = null ){
		$type = preg_replace('#[^A-Z0-9_\.-]#i', '', $type);
		$name = preg_replace('#[^A-Z0-9_\.-]#i', '', $name);
		if(version_compare(JVERSION,'1.6','<')){
			$path = JPATH_PLUGINS.DS.$type.DS.$name.'.php';
		}else{
			$path = JPATH_PLUGINS.DS.$type.DS.$name.DS.$name.'.php';
		}
		$instance=false;
		if (file_exists( $path )){
			require_once( $path );
			$className = 'plg'.$type.$name;
			if(class_exists($className)){
				if($dispatcher==null){
					$dispatcher =& JDispatcher::getInstance();
				}
				$instance = new $className($dispatcher, array('name'=>$name,'type'=>$type));
			}
		}
		return $instance;
	}
       function createDir($dir,$report = true){
               if(is_dir($dir)) return true;
               jimport('joomla.filesystem.folder');
               jimport('joomla.filesystem.file');
               $indexhtml = '<html><body bgcolor="#FFFFFF"></body></html>';
               if(!JFolder::create($dir)){
                       if($report) hikashop::display('Could not create the directly '.$dir,'error');
                       return false;
               }
               if(!JFile::write($dir.DS.'index.html',$indexhtml)){
                       if($report) hikashop::display('Could not create the file '.$dir.DS.'index.html','error');
               }
               return true;
       }
	function initModule(){
		static $done = false;
		if(!$done){
			$fe = JRequest::getVar('hikashop_front_end_main',0);
			if(empty($fe)){
				$done = true;
				$lang =& JFactory::getLanguage();
				$lang->load(HIKASHOP_COMPONENT,JPATH_SITE);
				if(version_compare(JVERSION,'1.6','<')){
					$override_path = JLanguage::getLanguagePath(JPATH_ROOT).DS.'overrides'.DS.$lang->getTag().'.override.ini';
					if(file_exists($override_path)){
						$lang->_load($override_path,'override');
					}
				}
			}
		}
		return true;
	}




	function absoluteURL($text){
		static $mainurl = '';
		if(empty($mainurl)){
			$urls = parse_url(HIKASHOP_LIVE);
			if(!empty($urls['path'])){
				$mainurl = substr(HIKASHOP_LIVE,0,strrpos(HIKASHOP_LIVE,$urls['path'])).'/';
			}else{
				$mainurl = HIKASHOP_LIVE;
			}
		}
		$text = str_replace(array('href="../undefined/','href="../../undefined/','href="../../../undefined//','href="undefined/'),array('href="'.$mainurl,'href="'.$mainurl,'href="'.$mainurl,'href="'.HIKASHOP_LIVE),$text);
		$text = preg_replace('#(href|src|action|background)[ ]*=[ ]*\"(?!(https?://|\#|mailto:|/))(?:\.\./|\./)?#','$1="'.HIKASHOP_LIVE,$text);
		$text = preg_replace('#(href|src|action|background)[ ]*=[ ]*\"(?!(https?://|\#|mailto:))/#','$1="'.$mainurl,$text);
		return $text;
	}
	function setTitle($name,$picture,$link){
		$config =& hikashop::config();
		$menu_style = $config->get('menu_style','title_bottom');
		$html='<a href="'. hikashop::completeLink($link).'">'.$name.'</a>';
		if($menu_style!='content_top'){
			$html=hikashop::getMenu($html);
		}
		JToolBarHelper::title( $html , $picture.'.png' );
	}
	function getMenu($title=""){
		$document =& JFactory::getDocument();
		$controller = new JController(array('name'=>'menu'));
		$viewType	= $document->getType();
		$view = & $controller->getView( '', $viewType, '');
		$view->setLayout('default');
		ob_start();
		$view->display(null,$title);
		return ob_get_clean();
	}
	function getLayout($controller,$layout,$params,&$js){
		$base_path=HIKASHOP_FRONT;
		$app =& JFactory::getApplication();
		if($app->isAdmin()){
			$base_path=HIKASHOP_BACK;
		}
		$base_path=rtrim($base_path,DS);
		$document =& JFactory::getDocument();
		$controller = new JController(array('name'=>$controller,'base_path'=>$base_path));
		$viewType	= $document->getType();
		$view = & $controller->getView( '', $viewType, '',array('base_path'=>$base_path));
		$folder	= JPATH_BASE.DS.'templates'.DS.$app->getTemplate().DS.'html'.DS.HIKASHOP_COMPONENT.DS.$view->getName();
		$view->addTemplatePath($folder);
		$view->setLayout($layout);
		ob_start();
		$view->display(null,$params);
		$js = @$view->js;
		return ob_get_clean();
	}
	function setExplorer($task,$defaultId=0,$popup=false,$type=''){
		$document =& JFactory::getDocument();
		$controller = new JController(array('name'=>'explorer'));
		$viewType	= $document->getType();
		$view = & $controller->getView( '', $viewType, '');
		$view->setLayout('default');
		ob_start();
		$view->display(null,$task,$defaultId,$popup,$type);
		return ob_get_clean();
	}
	function frontendLink($link,$popup = false){
		if($popup) $link .= '&tmpl=component';
		$config =& hikashop::config();
		if($config->get('use_sef',0)){
			$link = ltrim(JRoute::_($link),'/');
		}
		static $mainurl = '';
		static $otherarguments = false;
		if(empty($mainurl)){
			$urls = parse_url(HIKASHOP_LIVE);
			if(!empty($urls['path'])){
				$mainurl = substr(HIKASHOP_LIVE,0,strrpos(HIKASHOP_LIVE,$urls['path'])).'/';
				$otherarguments = trim(str_replace($mainurl,'',HIKASHOP_LIVE),'/');
				if(!empty($otherarguments)) $otherarguments .= '/';
			}else{
				$mainurl = HIKASHOP_LIVE;
			}
		}
		if($otherarguments AND strpos($link,$otherarguments) === false){
			$link = $otherarguments.$link;
		}
		return $mainurl.$link;
	}
	function backendLink($link,$popup = false){
		static $mainurl = '';
		static $otherarguments = false;
		if(empty($mainurl)){
			$urls = parse_url(HIKASHOP_LIVE);
			if(!empty($urls['path'])){
				$mainurl = substr(HIKASHOP_LIVE,0,strrpos(HIKASHOP_LIVE,$urls['path'])).'/';
				$otherarguments = trim(str_replace($mainurl,'',HIKASHOP_LIVE),'/');
				if(!empty($otherarguments)) $otherarguments .= '/';
			}else{
				$mainurl = HIKASHOP_LIVE;
			}
		}
		if($otherarguments AND strpos($link,$otherarguments) === false){
			$link = $otherarguments.$link;
		}
		return $mainurl.$link;
	}
	function bytes($val) {
		$val = trim($val);
		if(empty($val))
		{
			return 0;
		}
		$last = strtolower($val[strlen($val)-1]);
		switch($last) {
			case 'g':
			$val *= 1024;
			case 'm':
			$val *= 1024;
			case 'k':
			$val *= 1024;
		}
		return (int)$val;
	}
	function display($messages,$type = 'success',$return = false){
		if(empty($messages)) return;
		if(!is_array($messages)) $messages = array($messages);
		$html = '<div id="hikashop_messages_'.$type.'" class="hikashop_messages hikashop_'.$type.'"><ul><li>'.implode('</li><li>',$messages).'</li></ul></div>';
		if($return){
			return $html;
		}
		echo $html;
	}
	function completeLink($link,$popup = false,$redirect = false){
		if($popup) $link .= '&tmpl=component';
		return JRoute::_('index.php?option='.HIKASHOP_COMPONENT.'&ctrl='.$link,!$redirect);
	}
	function table($name,$component = true){
		$prefix = $component ? HIKASHOP_DBPREFIX : '#__';
		return $prefix.$name;
	}
	function secureField($fieldName){
		if (!is_string($fieldName) OR preg_match('|[^a-z0-9#_.-]|i',$fieldName) !== 0 ){
			 die('field "'.$fieldName .'" not secured');
		}
		return $fieldName;
	}
 	function increasePerf(){
        @ini_set('max_execution_time',0);
        if(hikashop::bytes(@ini_get('memory_limit')) < 60000000){
            $config = hikashop::config();
            if($config->get('hikaincreasemem','1')){
                if(!empty($_SESSION['hikaincreasemem'])){
                    $newConfig = null;
                    $newConfig->hikaincreasemem = 0;
                    $config->save($newConfig);
                    unset($_SESSION['hikaincreasemem']);
                    return;
                }
                if(isset($_SESSION)) $_SESSION['hikaincreasemem'] = 1;
                @ini_set('memory_limit','64M');
                if(isset($_SESSION['hikaincreasemem'])) unset($_SESSION['hikaincreasemem']);
            }
        }
    }
	function &config($reload = false){
		static $configClass = null;
		if($configClass === null || $reload){
			$configClass = hikashop::get('class.config');
			$configClass->load();
		}
		return $configClass;
	}
	function level($level){
		$config =& hikashop::config();
		if($config->get($config->get('level'),0) >= $level) return true;
		return false;
	}
	function footer(){
		$config =& hikashop::config();
		$description = $config->get('description_'.strtolower($config->get('level')),'Joomla!<sup style="font-size:6px">TM</sup> Ecommerce System');
		$link = 'http://www.hikashop.com';
		$aff = $config->get('partner_id');
		if(!empty($aff)){
			$link.='?partner_id='.$aff;
		}
		$text = '<!--  HikaShop Component powered by '.$link.' -->
		<!-- version '.$config->get('level').' : '.$config->get('version').' -->';
		if(!$config->get('show_footer',true)) return $text;
		$text .= '<div class="hikashop_footer" style="text-align:center" align="center"><a href="'.$link.'" target="_blank" title="'.HIKASHOP_NAME.' : '.strip_tags($description).'">'.HIKASHOP_NAME.' ';
		$app=&JFactory::getApplication();
		if($app->isAdmin()){
			$text .= $config->get('level').' '.$config->get('version');
		}
		$text .= ', '.$description.'</a></div>';
		return $text;
	}
	function search($searchString,$object,$exclude=''){
		if(empty($object) OR is_numeric($object)) return $object;
		if(is_string($object) OR is_numeric($object)){
			return preg_replace('#('.str_replace('#','\#',$searchString).')#i','<span class="searchtext">$1</span>',$object);
		}
		if(is_array($object)){
			foreach($object as $key => $element){
				$object[$key] = hikashop::search($searchString,$element,$exclude);
			}
		}elseif(is_object($object)){
			foreach($object as $key => $element){
				if($key!=$exclude) $object->$key = hikashop::search($searchString,$element,$exclude);
			}
		}
		return $object;
	}
	function get($path){
		list($group,$class) = explode('.',$path);
		if($group=='controller'){
			$className = $class.ucfirst($group);;
		}else{
			$className = 'hikashop'.ucfirst($class).ucfirst($group);
		}
		if(!class_exists($className)) include_once(constant(strtoupper('HIKASHOP_'.$group)).$class.'.php');
		if(!class_exists($className)) return null;
		$args = func_get_args();
		array_shift($args);
		switch(count($args)){
			case 3:
				return new $className($args[0],$args[1],$args[2]);
			case 2:
				return new $className($args[0],$args[1]);
			case 1:
				return new $className($args[0]);
			case 0:
			default:
				return new $className();
		}
	}
	function getCID($field = '',$int=true){
		$oneResult = reset(JRequest::getVar( 'cid', array(), '', 'array' ));
		if(empty($oneResult) && !empty($field)) $oneResult=JRequest::getCmd( $field,0);
		if($int) return intval($oneResult);
		return $oneResult;
	}
	function tooltip($desc,$title='', $image='tooltip.png', $name = '',$href='', $link=1){
		return JHTML::_('tooltip', str_replace(array("'","::"),array("&#039;",":"),$desc),str_replace(array("'",'::'),array("&#039;",':'),$title), $image, str_replace(array("'",'"','::'),array("&#039;","&quot;",':'),$name),$href, $link);
	}
	function checkRobots(){
		if(preg_match('#(libwww-perl|python)#i',@$_SERVER['HTTP_USER_AGENT'])) die('Not allowed for robots. Please contact us if you are not a robot');
	}
}
class hikashopController extends JController{
	var $pkey = array();
	var $table = array();
	var $groupMap = '';
	var $groupVal = null;
	var $orderingMap ='';
	var $display = array('listing','show');
	var $modify_views = array('edit','selectlisting','childlisting','newchild');
	var $add = array('add');
	var $modify = array('apply','save','store','orderdown','orderup','saveorder','savechild','addchild','toggle');
	var $delete = array('delete','remove');
	function __construct($config = array()){
		parent::__construct($config);
		$this->registerDefaultTask('listing');
	}
	function listing(){
		JRequest::setVar( 'layout', 'listing'  );
		return $this->display();
	}
	function show(){
		JRequest::setVar( 'layout', 'show'  );
		return $this->display();
	}
	function edit(){
		JRequest::setVar('hidemainmenu',1);
		JRequest::setVar( 'layout', 'form'  );
		return $this->display();
	}
	function add(){
		JRequest::setVar('hidemainmenu',1);
		JRequest::setVar( 'layout', 'form'  );
		return $this->display();
	}
	function apply(){
		$status = $this->store();
		return $this->edit();
	}
	function save(){
		$this->store();
		return $this->listing();
	}
	function orderdown(){
		if(!empty($this->table)&&!empty($this->pkey)&&(empty($this->groupMap)||isset($this->groupVal))&&!empty($this->orderingMap)){
			$orderClass = hikashop::get('helper.order');
			$orderClass->pkey = $this->pkey;
			$orderClass->table = $this->table;
			$orderClass->groupMap = $this->groupMap;
			$orderClass->groupVal = $this->groupVal;
			$orderClass->orderingMap = $this->orderingMap;
			if(!empty($this->main_pkey)){
				$orderClass->main_pkey = $this->main_pkey;
			}
			$orderClass->order(true);
		}
		return $this->listing();
	}
	function orderup(){
		if(!empty($this->table)&&!empty($this->pkey)&&(empty($this->groupMap)||isset($this->groupVal))&&!empty($this->orderingMap)){
			$orderClass = hikashop::get('helper.order');
			$orderClass->pkey = $this->pkey;
			$orderClass->table = $this->table;
			$orderClass->groupMap = $this->groupMap;
			$orderClass->groupVal = $this->groupVal;
			$orderClass->orderingMap = $this->orderingMap;
			if(!empty($this->main_pkey)){
				$orderClass->main_pkey = $this->main_pkey;
			}
			$orderClass->order(false);
		}
		return $this->listing();
	}
	function saveorder(){
		if(!empty($this->table)&&!empty($this->pkey)&&(empty($this->groupMap)||isset($this->groupVal))&&!empty($this->orderingMap)){
			$orderClass = hikashop::get('helper.order');
			$orderClass->pkey = $this->pkey;
			$orderClass->table = $this->table;
			$orderClass->groupMap = $this->groupMap;
			$orderClass->groupVal = $this->groupVal;
			$orderClass->orderingMap = $this->orderingMap;
			if(!empty($this->main_pkey)){
				$orderClass->main_pkey = $this->main_pkey;
			}
			$orderClass->save();
		}
		return $this->listing();
	}
	function store(){
		$app =& JFactory::getApplication();
		$class = hikashop::get('class.'.$this->type);
		$status = $class->saveForm();
		if($status){
			$app->enqueueMessage(JText::_( 'HIKASHOP_SUCC_SAVED' ), 'message');
			JRequest::setVar( 'cid', $status  );
			JRequest::setVar( 'fail', null  );
		}else{
			$app->enqueueMessage(JText::_( 'ERROR_SAVING' ), 'error');
			if(!empty($class->errors)){
				foreach($class->errors as $oneError){
					$app->enqueueMessage($oneError, 'error');
				}
			}
		}
		return $status;
	}
	function remove(){
		$cids = JRequest::getVar( 'cid', array(), '', 'array' );
		$class = hikashop::get('class.'.$this->type);
		$num = $class->delete($cids);
		if($num){
			$app =& JFactory::getApplication();
			$app->enqueueMessage(JText::sprintf('SUCC_DELETE_ELEMENTS',$num), 'message');
		}
		return $this->listing();
	}
	function authorize($task){
		if($this->isIn($task,array('modify','delete')) && !JRequest::checkToken('request')){
			return false;
		}
		$app =& JFactory::getApplication();
		if($app->isAdmin()){
		}
		return true;
	}
	function isIn($task,$lists){
		foreach($lists as $list){
			if(in_array($task,$this->$list)){
				return true;
			}
		}
		return false;
	}
	function display(){
		$config =& hikashop::config();
		$menu_style = $config->get('menu_style','title_bottom');
		if($menu_style=='content_top'){
			$app =& JFactory::getApplication();
			if($app->isAdmin() && JRequest::getString('tmpl') !== 'component'){
				echo hikashop::getMenu();
			}
		}
		return parent::display();
	}
}
class hikashopClass extends JObject{
	var $tables = array();
	var $pkeys = array();
	var $namekeys = array();
	function  __construct( $config = array() ){
		$this->database =& JFactory::getDBO();
		return parent::__construct($config);
	}
	function save($element){
		$pkey = end($this->pkeys);
		if(empty($pkey)){
			$pkey = end($this->namekeys);
		}elseif(empty($element->$pkey)){
			$tmp = end($this->namekeys);
			if(!empty($tmp)){
				if(!empty($element->$tmp)){
					$pkey = $tmp;
				}else{
					$element->$tmp=$this->getNamekey($element);
					if($element->$tmp===false){
						return false;
					}
				}
			}
		}
		if(empty($element->$pkey)){
			$this->database->setQuery($this->_getInsert($this->getTable(),$element));
			$status = $this->database->query();
		}else{
			if(count((array) $element) > 1){
				$status = $this->database->updateObject($this->getTable(),$element,$pkey);
			}else{
				$status = true;
			}
		}
		if($status){
			return empty($element->$pkey) ? $this->database->insertid() : $element->$pkey;
		}
		return false;
	}
	function getTable(){
		return hikashop::table(end($this->tables));
	}
	function _getInsert( $table, &$object, $keyName = NULL )
	{
		$fmtsql = 'INSERT IGNORE INTO '.$this->database->nameQuote($table).' ( %s ) VALUES ( %s ) ';
		$fields = array();
		foreach (get_object_vars( $object ) as $k => $v) {
			if (is_array($v) or is_object($v) or $v === NULL or $k[0] == '_') {
				continue;
			}
			$fields[] = $this->database->nameQuote( $k );
			$values[] = $this->database->isQuoted( $k ) ? $this->database->Quote( $v ) : (int) $v;
		}
		return sprintf( $fmtsql, implode( ",", $fields ) ,  implode( ",", $values ) );
	}


	function delete($elements){
		if(!is_array($elements)){
			$elements = array($elements);
		}
		$isNumeric = is_numeric(reset($elements));
		foreach($elements as $key => $val){
			$elements[$key] = $this->database->Quote($val);
		}
		$columns = $isNumeric ? $this->pkeys : $this->namekeys;
		if(empty($columns) OR empty($elements)) return false;
		$otherElements=array();
		$otherColumn='';
		foreach($columns as $i => $column){
			if(empty($column)){
				$query = 'SELECT '.($isNumeric?end($this->pkeys):end($this->namekeys)).' FROM '.$this->getTable().' WHERE '.($isNumeric?end($this->pkeys):end($this->namekeys)).' IN ( '.implode(',',$elements).');';
				$this->database->setQuery($query);
				$otherElements = $this->database->loadResultArray();
				foreach($otherElements as $key => $val){
					$otherElements[$key] = $this->database->Quote($val);
				}
				break;
			}
		}
		$result = true;
		$tables=array();
		if(empty($this->tables)){
			$tables[0]=$this->getTable();
		}else{
			foreach($this->tables as $i => $oneTable){
				$tables[$i]=hikashop::table($oneTable);
			}
		}
		foreach($tables as $i => $oneTable){
			$column = $columns[$i];
			if(empty($column)){
				$whereIn = ' WHERE '.($isNumeric?$this->namekeys[$i]:$this->pkeys[$i]).' IN ('.implode(',',$otherElements).')';
			}else{
				$whereIn = ' WHERE '.$column.' IN ('.implode(',',$elements).')';
			}
			$query = 'DELETE FROM '.$oneTable.$whereIn;
			$this->database->setQuery($query);
			$result = $this->database->query() && $result;
		}
		return $result;
	}
	function get($element){
		if(empty($element)) return null;
		$pkey = end($this->pkeys);
		$namekey = end($this->namekeys);
		if(!is_numeric($element) && !empty($namekey)) {
			$pkey = $namekey;
		}
		$query = 'SELECT * FROM '.$this->getTable().' WHERE '.$pkey.'  = '.$this->database->Quote($element).' LIMIT 1';
		$this->database->setQuery($query);
		return $this->database->loadObject();
	}
}
define('HIKASHOP_COMPONENT','com_hikashop');
define('HIKASHOP_LIVE',rtrim(JURI::root(),'/').'/');
define('HIKASHOP_ROOT',rtrim(JPATH_ROOT,DS).DS);
define('HIKASHOP_FRONT',rtrim(JPATH_SITE,DS).DS.'components'.DS.HIKASHOP_COMPONENT.DS);
define('HIKASHOP_BACK',rtrim(JPATH_ADMINISTRATOR,DS).DS.'components'.DS.HIKASHOP_COMPONENT.DS);
define('HIKASHOP_HELPER',HIKASHOP_BACK.'helpers'.DS);
define('HIKASHOP_BUTTON',HIKASHOP_BACK.'buttons');
define('HIKASHOP_CLASS',HIKASHOP_BACK.'classes'.DS);
define('HIKASHOP_INC',HIKASHOP_BACK.'inc'.DS);
define('HIKASHOP_VIEW',HIKASHOP_BACK.'views'.DS);
define('HIKASHOP_TYPE',HIKASHOP_BACK.'types'.DS);
define('HIKASHOP_MEDIA',HIKASHOP_ROOT.'media'.DS.HIKASHOP_COMPONENT.DS);
define('HIKASHOP_DBPREFIX','#__hikashop_');
$app =& JFactory::getApplication();
$config =& hikashop::config();
$doc =& JFactory::getDocument();
if($app->isAdmin()){
	define('HIKASHOP_CONTROLLER',HIKASHOP_BACK.'controllers'.DS);
	define('HIKASHOP_IMAGES','../media/'.HIKASHOP_COMPONENT.'/images/');
	define('HIKASHOP_CSS','../media/'.HIKASHOP_COMPONENT.'/css/');
	define('HIKASHOP_JS','../media/'.HIKASHOP_COMPONENT.'/js/');
	$css_type = 'backend';
}else{
	define('HIKASHOP_CONTROLLER',HIKASHOP_FRONT.'controllers'.DS);
	define('HIKASHOP_IMAGES',JURI::base(true).'/media/'.HIKASHOP_COMPONENT.'/images/');
	define('HIKASHOP_CSS',JURI::base(true).'/media/'.HIKASHOP_COMPONENT.'/css/');
	define('HIKASHOP_JS',JURI::base(true).'/media/'.HIKASHOP_COMPONENT.'/js/');
	$css_type = 'frontend';
	$doc->addScript(HIKASHOP_JS.'hikashop.js');
}
$css = $config->get('css_'.$css_type,'default');
if(!empty($css)){
	$doc->addStyleSheet( HIKASHOP_CSS.$css_type.'_'.$css.'.css' );
}
$lang =& JFactory::getLanguage();
$lang->load(HIKASHOP_COMPONENT,JPATH_SITE);
if(version_compare(JVERSION,'1.6','<')){
	$override_path = JLanguage::getLanguagePath(JPATH_ROOT).DS.'overrides'.DS.$lang->getTag().'.override.ini';
	if(file_exists($override_path)){
		$lang->_load($override_path,'override');
	}
}
define('HIKASHOP_NAME','HikaShop');
define('HIKASHOP_TEMPLATE',HIKASHOP_FRONT.'templates'.DS);
define('HIKASHOP_URL','http://www.hikashop.com/');
define('HIKASHOP_UPDATEURL',HIKASHOP_URL.'index.php?option=com_doc&ctrl=update&task=');
define('HIKASHOP_HELPURL',HIKASHOP_URL.'index.php?option=com_doc&ctrl=doc&component='.HIKASHOP_NAME.'&page=');
if(!function_exists('bccomp')){
	function bccomp($Num1,$Num2,$Scale=0) {
	  if(!preg_match("/^\+?(\d+)(\.\d+)?$/",$Num1,$Tmp1)||
	     !preg_match("/^\+?(\d+)(\.\d+)?$/",$Num2,$Tmp2)) return('0');
	  $Num1=ltrim($Tmp1[1],'0');
	  $Num2=ltrim($Tmp2[1],'0');
	  if(strlen($Num1)>strlen($Num2)) return(1);
	  else {
	    if(strlen($Num1)<strlen($Num2)) return(-1);
	    else {
	      $Dec1=isset($Tmp1[2])?rtrim(substr($Tmp1[2],1),'0'):'';
	      $Dec2=isset($Tmp2[2])?rtrim(substr($Tmp2[2],1),'0'):'';
	      if($Scale!=null) {
	        $Dec1=substr($Dec1,0,$Scale);
	        $Dec2=substr($Dec2,0,$Scale);
	      }
	      $DLen=max(strlen($Dec1),strlen($Dec2));
	      $Num1.=str_pad($Dec1,$DLen,'0');
	      $Num2.=str_pad($Dec2,$DLen,'0');
	      for($i=0;$i<strlen($Num1);$i++) {
	        if((int)$Num1{$i}>(int)$Num2{$i}) return(1);
	        else
	          if((int)$Num1{$i}<(int)$Num2{$i}) return(-1);
	      }
	      return(0);
	    }
	  }
	}
}
Return current item: HikaShop