Location: PHPKode > projects > HikaShop > back/classes/field.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 hikashopFieldClass extends hikashopClass{
	var $tables = array('field');
	var $pkeys = array('field_id');
	var $namekeys = array();
	var $errors = array();
	var $prefix = '';
	var $suffix = '';
	var $excludeValue = array();
	var $toggle = array('field_required'=>'field_id','field_published'=>'field_id','field_backend'=>'field_id','field_backend_listing'=>'field_id','field_frontcomp'=>'field_id','field_core'=>'field_id');
	var $where = array();
	var $skipAddressName=false;
	var $report = true;
	function & getData($area,$type,$notcoreonly=false){
		static $data = array();
		$key = $area.'_'.$type.'_'.$notcoreonly;
		if(!isset($data[$key])){
			$this->where = array();
			$this->where[] = 'a.`field_published` = 1';
			if($area == 'backend'){
				$this->where[] = 'a.`field_backend` = 1';
			}elseif($area == 'frontcomp'){
				$this->where[] = 'a.`field_frontcomp` = 1';
			}elseif($area=='backend_listing'){
				$this->where[] = 'a.`field_backend_listing` = 1';
			}
			if($notcoreonly){
				$this->where[] = 'a.`field_core` = 0';
			}
			if($this->skipAddressName){
				$this->where[]='a.field_namekey!=\'address_name\'';
			}
			$this->where[]='a.field_table='.$this->database->Quote($type);
			$this->database->setQuery('SELECT * FROM '.hikashop::table('field').' as a WHERE '.implode(' AND ',$this->where).' ORDER BY a.`field_ordering` ASC');
			$data[$key] = $this->database->loadObjectList('field_namekey');
		}
		return $data[$key];
	}
	function getField($fieldid,$type=''){
		if(is_numeric($fieldid)){
			$element = parent::get($fieldid);
		}else{
			$this->database->setQuery('SELECT * FROM '.hikashop::table('field').' WHERE field_table='.$this->database->Quote($type).' AND field_namekey='.$this->database->Quote($fieldid));
			$element = $this->database->loadObject();
		}
		$fields = array($element);
		$data = null;
		$this->prepareFields($fields,$data,$fields[0]->field_type,'',true);
		return $fields[0];
	}
	function getFields($area,&$data,$type='user',$url='checkout&task=state'){
		$fields = $this->getData($area,$type);
		$this->prepareFields($fields,$data,$type,$url);
		return $fields;
	}
	function prepareFields(&$fields,&$data,$type='user',$url='checkout&task=state',$test=false){
		$id = $type.'_id';
		foreach($fields as $namekey => $field){
			if(!empty($fields[$namekey]->field_options) && is_string($fields[$namekey]->field_options)){
				$fields[$namekey]->field_options = unserialize($fields[$namekey]->field_options);
			}
			if(!empty($field->field_value) && is_string($fields[$namekey]->field_value)){
				$fields[$namekey]->field_value = $this->explodeValues($fields[$namekey]->field_value);
			}
			if(empty($data->$id) && empty($data->$namekey)){
				$data->$namekey = $field->field_default;
			}
			if(!empty($fields[$namekey]->field_options['zone_type']) && $fields[$namekey]->field_options['zone_type']=='country'){
				$fields[$namekey]->field_url=JURI::base(true).'/index.php?option='.HIKASHOP_COMPONENT.'&tmpl=component&ctrl='.$url;
			}
		}
		$this->handleZone($fields,$test);
	}
	function handleZone(&$fields,$test=false){
		$types = array();
		foreach($fields as $k => $field){
			if($field->field_type=='zone' && !empty($field->field_options['zone_type'])){
				if($field->field_options['zone_type']!='state'){
					$types[$field->field_options['zone_type']]=$field->field_options['zone_type'];
				}elseif($test){
					$allFields = $this->getData('',$field->field_table,false);
					foreach($allFields as $oneField){
						if(!empty($oneField->field_options)){
							$oneField->field_options = unserialize($oneField->field_options);
						}
						if($oneField->field_type=='zone' && !empty($oneField->field_options['zone_type']) && $oneField->field_options['zone_type']=='country'){
							$zoneType = hikashop::get('type.country');
							$zoneType->type = 'state';
							$zoneType->published = true;
							$zoneType->country_name = $oneField->field_default;
							$zones = $zoneType->load();
							$this->setValues($zones,$fields,$k,$field);
							break;
						}
					}
				}
			}
		}
		if(!empty($types)){
			$zoneType = hikashop::get('type.country');
			$zoneType->type = $types;
			$zoneType->published = true;
			$zones = $zoneType->load();
			if(!empty($zones)){
				foreach($fields as $k => $field){
					$this->setValues($zones,$fields,$k,$field);
				}
			}
		}
	}
	function handleZoneListing(&$fields,&$rows){
		$values = array();
		foreach($fields as $k => $field){
			if($field->field_type=='zone'){
				$field_namekey = $field->field_namekey;
				foreach($rows as $row){
					if(!empty($row->$field_namekey)){
						$values[$row->$field_namekey]=$this->database->Quote($row->$field_namekey);
					}
				}
			}
		}
		if(!empty($values)){
			$query = 'SELECT * FROM '.hikashop::table('zone').' WHERE zone_namekey IN ('.implode(',',$values).')';
			$this->database->setQuery($query);
			$zones = $this->loadObjectList('zone_namekey');
			foreach($fields as $k => $field){
				if($field->field_type=='zone'){
					$field_namekey = $field->field_namekey;
					foreach($rows as $k => $row){
						if(!empty($row->$field_namekey)){
							foreach($zones as $zone){
								if($zone->zone_namekey==$row->$field_namekey){
									$title = $zone->zone_name_english;
									if($zone->zone_name_english != $zone->zone_name){
										$title.=' ('.$zone->zone_name.')';
									}
									$rows[$k]->$field_namekey=$title;
									break;
								}
							}
						}
					}
				}
			}
		}
	}
	function setValues(&$zones,&$fields,$k,&$field){
		foreach($zones as $zone){
			if($field->field_type=='zone' && !empty($field->field_options['zone_type']) && $field->field_options['zone_type']==$zone->zone_type){
				$title = $zone->zone_name_english;
				if($zone->zone_name_english != $zone->zone_name){
					$title.=' ('.$zone->zone_name.')';
				}
				$obj = null;
				$obj->value = $title;
				$obj->disabled = '0';
				$fields[$k]->field_value[$zone->zone_namekey]=$obj;
			}
		}
	}
	function getInput($type,&$oldData,$report=true){
		$this->report = $report;
		$data = null;
		static $formData = null;
		if(!isset($formData)){
			$formData = JRequest::getVar( 'data', array(), '', 'array' );
		}
		if(empty($formData[$type])){
			$formData[$type]=array();
		}
		$app =& JFactory::getApplication();
		if($app->isAdmin()){
			$area = 'backend';
		}else{
			$area = 'frontcomp';
		}
		$fields =& $this->getData($area,$type);
		foreach($fields as $namekey => $field){
			if(!empty($fields[$namekey]->field_options)){
				$fields[$namekey]->field_options = unserialize($fields[$namekey]->field_options);
			}
		}
		if($type=='entry' && $area=='frontcomp'){
			$ok = true;
			$data=array();
			foreach($formData[$type] as $key => $form){
				$obj=null;
				$data[$key]=$obj;
				if(!$this->_checkOneInput($fields,@$formData[$type][$key],$data[$key],$type,$oldData)){
					$ok = false;
				}
			}
		}else{
			$ok = $this->_checkOneInput($fields,@$formData[$type],$data,$type,$oldData);
		}
		JRequest::setVar($type.'_data',$data);
		if(!$ok){
			return $ok;
		}
		return $data;
	}
	function _checkOneInput(&$fields,&$formData,&$data,$type,&$oldData){
		$ok = true;
		foreach($fields as $k => $field){
			if($field->field_required){
				$namekey = $field->field_namekey;
				if(!empty($field->field_options['limit_to_parent'])){
					$parent = $field->field_options['limit_to_parent'];
					if(!isset($field->field_options['parent_value'])){
						$field->field_options['parent_value']='';
					}
					$skip = false;
					foreach($fields as $otherField){
						if($otherField->field_namekey==$parent){
							if(!isset($formData[$parent]) || $field->field_options['parent_value']!=$formData[$parent]){
								if(isset($formData[$namekey])){
									unset($formData[$namekey]);
								}
								$skip=true;
							}
							break;
						}
					}
					if($skip){
						continue;
					}
				}
				$classType = 'hikashop'.ucfirst($field->field_type);
				$class = new $classType($this);
				$val = @$formData[$namekey];
				if(!$class->check($fields[$k],$val,@$oldData->$namekey)){
					$ok = false;
				}
				$formData[$namekey] = $val;
			}
		}
		$this->checkFields($formData,$data,$type);
		return $ok;
	}
	function checkFields(&$data,&$object,$type){
		$app =& JFactory::getApplication();
		static $safeHtmlFilter= null;
		if(!$app->isAdmin()){
			if (is_null($safeHtmlFilter)) {
				jimport('joomla.filter.filterinput');
				$safeHtmlFilter = & JFilterInput::getInstance(null, null, 1, 1);
			}
		}
		foreach($data as $column => $value){
			$column = trim(strtolower($column));
			if($this->allowed($column,$type)){
				hikashop::secureField($column);
				if(is_array($value)){
					if($type='user' && $column=='user_params'){
						$object->$column = null;
						foreach($value as $c => $v){
							$c = trim(strtolower($c));
							if($this->allowed($c,$type)){
								hikashop::secureField($c);
								$object->$column->$c = strip_tags($v);
							}
						}
					}else{
						$value = implode(',',$value);
						$object->$column = strip_tags($value);
					}
				}elseif(is_null($safeHtmlFilter)){
					$object->$column = strip_tags($value);
				}else{
					$object->$column = $safeHtmlFilter->clean($value, 'string');
				}
			}
		}
	}
	function checkFieldsForJS(&$extraFields,&$requiredFields,&$validMessages,&$values){
		foreach($extraFields as $type => $oneType){
			foreach($oneType as $k => $oneField){
				if(empty($oneField->field_js_added)){
					$classType = 'hikashop'.ucfirst($oneField->field_type);
					$class = new $classType($this);
					$class->JSCheck($oneField,$requiredFields[$type],$validMessages[$type],$values[$type]);
				}
				$extraFields[$type][$k]->field_js_added = true;
			}
		}
	}
	function addJS(&$requiredFields,&$validMessages,$types=array()){
		static $done = false;
		$doc =& JFactory::getDocument();
		if(!$done){
			$js="var hikashop=Array();
			hikashop['reqFieldsComp']=Array();
			hikashop['validFieldsComp']=Array();";
			$doc->addScriptDeclaration( "<!--\n".$js."\n//-->" );
			$done = true;
		}
		$js='';
		foreach($types as $type){
			if(!empty($requiredFields[$type])){
				$js .= "
				hikashop['reqFieldsComp']['".$type."'] = Array('".implode("','",$requiredFields[$type])."');
				hikashop['validFieldsComp']['".$type."'] = Array('".implode("','",$validMessages[$type])."');";
			}
			if($type=='register'){
				$js.="
				hikashop['password_different'] = '".JText::_('PASSWORDS_DO_NOT_MATCH')."';
				hikashop['valid_email'] = '".JText::_('VALID_EMAIL')."';";
			}
		}
		if(!empty($js)){
			$doc->addScriptDeclaration( "<!--\n".$js."\n//-->" );
		}
	}
	function jsToggle(&$fields,$data){
		$doc =& JFactory::getDocument();
		$parents = $this->getParents($fields);
		if(empty($parents)){
			return false;
		}
		$first = reset($parents);
		$type = $first->type;
		$js ="hikashop['".$type."']=Array();";
		foreach($parents as $namekey => $parent){
			$js.="
			hikashop['".$type."']['".$namekey."']=Array();";
			foreach($parent->childs as $value => $childs){
				$js.="
			hikashop['".$type."']['".$namekey."']['".$value."']=Array();";
				foreach($childs as $field){
					$js.="
			hikashop['".$type."']['".$namekey."']['".$value."']['".$field->field_namekey."']='".$field->field_namekey."';";
				}
			}
		}
		static $done = false;
		if(!$done){
			$js.="
			function hikashopToggleFields(new_value,namekey,field_type,id){
				if(typeof hikashop != 'undefined' && typeof hikashop[field_type] != 'undefined'){
					for(var j in hikashop[field_type][namekey]) {
						if(typeof hikashop[field_type][namekey][j] == 'object'){
							for(var i in hikashop[field_type][namekey][j]){
								if(typeof hikashop[field_type][namekey][j][i] == 'string'){
									var elementName = 'hikashop_'+field_type+'_'+hikashop[field_type][namekey][j][i];
									if(id){
										elementName = elementName + '_' + id;
									}
									el = document.getElementById(elementName);
									if(el){
										if(j==new_value){
											el.style.display='';
										}else{
											el.style.display='none';
										}
									}
								}
							}
						}
					}
				}
			}";
			$done = true;
		}
		$js .= $this->getLoadJSForToggle($parents,$data);
		$doc->addScriptDeclaration( "<!--\n".$js."\n//-->" );
	}
	function getLoadJSForToggle(&$parents,&$data,$id=1){
		$js="
		window.addEvent('domready', function(){";
		$js.=$this->initJSToggle($parents,$data,$id);
		$js.="});";
		return $js;
	}
	function initJSToggle(&$parents,&$data,$id=1){
		$first = reset($parents);
		$type = $first->type;
		$js = '';
		foreach($parents as $namekey => $parent){
			$js.="
			hikashopToggleFields('".@$data->$namekey."','".$namekey ."','".$type."',".$id.");";
		}
		return $js;
	}
	function getParents(&$fields){
		$parents = array();
		if(empty($fields)){
			return false;
		}
		foreach($fields as $k => $field){
			if(!empty($field->field_options['limit_to_parent'])){
				$parent = $field->field_options['limit_to_parent'];
				$parent_value = @$field->field_options['parent_value'];
				if(!isset($parents[$parent])){
					$obj=null;
					$obj->type = $field->field_table;
					$obj->childs = array();
					$parents[$parent]=$obj;
				}
				if(!isset($parents[$parent]->childs[$parent_value])){
					$parents[$parent]->childs[$parent_value]=array();
				}
				$parents[$parent]->childs[$parent_value][$field->field_namekey]=$field;
			}
		}
		return $parents;
	}
	function allowed($column,$type='user'){
		$restricted=array('user'=>array('user_partner_price'=>1,'user_partner_paid'=>1,'user_created_ip'=>1,'user_partner_id'=>1,'user_partner_lead_fee'=>1,'user_partner_click_fee'=>1,'user_partner_percent_fee'=>1,'user_partner_flat_fee'=>1),
						  'order'=>array('order_id'=>1,'order_billing_address_id'=>1,'order_shipping_address_id'=>1,'order_user_id'=>1,'order_status'=>1,'order_discount_code'=>1,'order_created'=>1,'order_ip'=>1,'order_currency_id'=>1,'order_status'=>1,'order_shipping_price'=>1,'order_discount_price'=>1,'order_shipping_id'=>1,'order_shipping_method'=>1,'order_payment_id'=>1,'order_payment_method'=>1,'order_full_price'=>1,'order_modified'=>1,'order_partner_id'=>1,'order_partner_price'=>1,'order_partner_paid'=>1,'order_type'=>1,'order_partner_currency_id'=>1));
		if(isset($restricted[$type][$column])){
			$app =& JFactory::getApplication();
			if(!$app->isAdmin()){
				return false;
			}
		}
		return true;
	}
	function explodeValues($values){
		$allValues = explode("\n",$values);
		$returnedValues = array();
		foreach($allValues as $id => $oneVal){
			$line = explode('::',trim($oneVal));
			$var = $line[0];
			$val = $line[1];
			if(count($line)==2){
				$disable = '0';
			}else{
				$disable = $line[2];
			}
			if(strlen($val)>0){
				$obj = null;
				$obj->value = $val;
				$obj->disabled = $disable;
				$returnedValues[$var] = $obj;
			}
		}
		return $returnedValues;
	}
	function getFieldName($field){
		return '<label for="'.$this->prefix.$field->field_namekey.$this->suffix.'">'.$this->trans($field->field_realname).'</label>';
	}
	function trans($name){
		$val = preg_replace('#[^a-z0-9]#i','_',strtoupper($name));
		$trans = JText::_($val);
		if($val==$trans){
			$trans = $name;
		}
		return $trans;

	}
	function get($field_id){
		$query = 'SELECT a.* FROM '.hikashop::table('field').' as a WHERE a.`field_id` = '.intval($field_id).' LIMIT 1';
		$this->database->setQuery($query);
		$field = $this->database->loadObject();
		if(!empty($field->field_options)){
			$field->field_options = unserialize($field->field_options);
		}
		if(!empty($field->field_value)){
			$field->field_value = $this->explodeValues($field->field_value);
		}
		return $field;
	}
function saveForm(){
    $field = null;
    $field->field_id = hikashop::getCID('field_id');
    $formData = JRequest::getVar( 'data', array(), '', 'array' );
    foreach($formData['field'] as $column => $value){
      hikashop::secureField($column);
      if(is_array($value)) $value = implode(',',$value);
      $field->$column = strip_tags($value);
    }
	$fieldOptions = JRequest::getVar( 'field_options', array(), '', 'array' );
    foreach($fieldOptions as $column => $value){
      $fieldOptions[$column] = strip_tags($value);
    }
    $field->field_options = serialize($fieldOptions);
    $fieldValues = JRequest::getVar('field_values', array(), '', 'array' );
    if(!empty($fieldValues)){
    	$field->field_value = array();
    	foreach($fieldValues['title'] as $i => $title){
    		if(strlen($title)<1 AND strlen($fieldValues['value'][$i])<1) continue;
    		$value = strlen($fieldValues['value'][$i])<1 ? $title : $fieldValues['value'][$i];
    		$disabled = strlen($fieldValues['disabled'][$i])<1 ? '0' : $fieldValues['disabled'][$i];
    		$field->field_value[] = strip_tags($title).'::'.strip_tags($value).'::'.strip_tags($disabled);
    	}
    	$field->field_value = implode("\n",$field->field_value);
    }
	if(empty($field->field_id)){
		if(empty($field->field_namekey)) $field->field_namekey = $field->field_realname;
		$field->field_namekey = preg_replace('#[^a-z0-9_\-]#i', '',strtolower($field->field_namekey));
		if(empty($field->field_namekey)){
			$this->errors[] = 'Please specify a namekey';
			return false;
		}
		$columnsTable = $this->database->getTableFields(hikashop::table($field->field_table));
		$columns = reset($columnsTable);
		if(isset($columns[$field->field_namekey])){
			$this->errors[] = 'The field "'.$field->field_namekey.'" already exists in the table "'.$field->field_table.'"';
			return false;
		}
		$tables = array($field->field_table);
		if($field->field_table=='item'){
			$tables = array('cart_product','order_product');
		}
		foreach($tables as $table_name){
			$query = 'ALTER TABLE '.hikashop::table($table_name).' ADD `'.$field->field_namekey.'` VARCHAR ( 255 ) NULL';
			$this->database->setQuery($query);
			$this->database->query();
		}
	}
    $field_id = $this->save($field);
    if(!$field_id) return false;
    if(empty($field->field_id)){
		$orderClass = hikashop::get('helper.order');
		$orderClass->pkey = 'field_id';
		$orderClass->table = 'field';
		$orderClass->groupMap = 'field_table';
		$orderClass->groupVal = $field->field_table;
		$orderClass->orderingMap = 'field_ordering';
		$orderClass->reOrder();
    }
    JRequest::setVar( 'field_id', $field_id);
    return true;
  }
	function delete($elements){
		if(!is_array($elements)){
			$elements = array($elements);
		}
		foreach($elements as $key => $val){
			$elements[$key] = $this->database->getEscaped($val);
		}
		if(empty($elements)) return false;
		$this->database->setQuery('SELECT `field_namekey`,`field_id`,`field_table` FROM '.hikashop::table('field').'  WHERE `field_core` = 0 AND `field_id` IN ('.implode(',',$elements).')');
		$fieldsToDelete = $this->database->loadObjectList('field_id');
		if(empty($fieldsToDelete)){
			$app =& JFactory::getApplication();
			$app->enqueueMessage(JText::_('CORE_FIELD_DELETE_ERROR'));
			return false;
		}
		$namekeys = array();
		foreach($fieldsToDelete as $oneField){
			$namekeys[$oneField->field_table][] = $oneField->field_namekey;
		}
		foreach($namekeys as $table => $fields){
			$this->database->setQuery('ALTER TABLE '.hikashop::table($table).' DROP `'.implode('`, DROP `',$fields)).'`';
			$this->database->query();
		}
		$this->database->setQuery('DELETE FROM '.hikashop::table('field').' WHERE `field_id` IN ('.implode(',',array_keys($fieldsToDelete)).')');
		$result = $this->database->query();
		if(!$result) return false;
		$affectedRows = $this->database->getAffectedRows();
		foreach($namekeys as $table => $fields){
			$orderClass = hikashop::get('helper.order');
			$orderClass->pkey = 'field_id';
			$orderClass->table = 'field';
			$orderClass->groupMap = 'field_table';
			$orderClass->groupVal = $table;
			$orderClass->orderingMap = 'field_ordering';
			$orderClass->reOrder();
		}
		return $affectedRows;
	}
	function display(&$field,$value,$map,$inside = false,$options='',$test=false){
		$classType = 'hikashop'.ucfirst($field->field_type);
		$class = new $classType($this);
		$html = $class->display($field,$value,$map,$inside,$options,$test);
		if(!empty($field->field_required)){
			$html .=' *';
		}
		return $html;
	}
	function show(&$field,$value){
		$classType = 'hikashop'.ucfirst($field->field_type);
		$class = new $classType($this);
		$html = $class->show($field,$value);
		return $html;
	}
}
class hikashopItem{
	function hikashopItem(&$obj){
		$this->prefix = $obj->prefix;
		$this->suffix = $obj->suffix;
		$this->excludeValue =& $obj->excludeValue;
		$this->report = @$obj->report;
	}
	function getFieldName($field){
		return '<label for="'.$this->prefix.$field->field_namekey.$this->suffix.'">'.$this->trans($field->field_realname).'</label>';
	}
	function trans($name){
		$val = preg_replace('#[^a-z0-9]#i','_',strtoupper($name));
		$trans = JText::_($val);
		if($val==$trans){
			$trans = $name;
		}
		return $trans;
	}
	function show(&$field,$value){
		return $this->trans($value);
	}
	function JSCheck(&$oneField,&$requiredFields,&$validMessages,&$values){
		if(!empty($oneField->field_required)){
			$requiredFields[] = $oneField->field_namekey;
			if(!empty($oneField->field_options['errormessage'])){
				$validMessages[] = addslashes($this->trans($oneField->field_options['errormessage']));
			}else{
				$validMessages[] = addslashes(JText::sprintf('FIELD_VALID',$this->trans($oneField->field_realname)));
			}
		}
	}
	function check(&$field,$value,$oldvalue){
		if(strlen($value) || strlen($oldvalue)){
			return true;
		}
		if($this->report){
			$app =& JFactory::getApplication();
			$app->enqueueMessage(JText::sprintf('PLEASE_FILL_THE_FIELD',$this->trans($field->field_realname)));
		}
		return false;
	}
}
class hikashopText extends hikashopItem{
	function display($field,$value,$map,$inside,$options='',$test=false,$type='text',$class='inputbox'){
		$size = empty($field->field_options['size']) ? '' : 'size="'.intval($field->field_options['size']).'"';
		$js = '';
		if($inside AND strlen($value) < 1){
			$value = addslashes($this->trans($field->field_realname));
			$this->excludeValue[$field->field_namekey] = $value;
			$js = 'onfocus="if(this.value == \''.$value.'\') this.value = \'\';" onblur="if(this.value==\'\') this.value=\''.$value.'\';"';
		}
		return '<input class="'.$class.'" id="'.$this->prefix.@$field->field_namekey.$this->suffix.'" '.$size.' '.$js.' '.$options.' type="'.$type.'" name="'.$map.'" value="'.$value.'" />';
	}
}
class hikashopFile extends hikashopText{
	function display($field,$value,$map,$inside,$options='',$test=false){
		$html='';
		if(!empty($value)){
			$html.=$this->show($field,$value,'hikashop_custom_file_upload_link');
		}
		$map = $field->field_table.'_'.$field->field_namekey;
		$html.= parent::display($field,$value,$map,$inside,$options,$test,'file','inputbox hikashop_custom_file_upload_field');
		$html.= '<span class="hikashop_custom_file_upload_message">'.JText::sprintf('MAX_UPLOAD',(hikashop::bytes(ini_get('upload_max_filesize')) > hikashop::bytes(ini_get('post_max_size'))) ? ini_get('post_max_size') : ini_get('upload_max_filesize')).'</span>';
		return $html;
	}
	function JSCheck(&$oneField,&$requiredFields,&$validMessages,&$values){
		$namekey = $oneField->field_namekey;
		if(empty($values->$namekey)){
			return parent::JSCheck($oneField,$requiredFields,$validMessages,$values);
		}
		return true;
	}
	function show(&$field,$value,$class='hikashop_custom_file_link'){
		return '<a target="_blank" class="'.$class.'" href="'.hikashop::completeLink('order&task=download&field_table='.$field->field_table.'&field_namekey='.$field->field_namekey.'&name='.$value).'">'.$value.'</a>';
	}
	function check(&$field,&$value,$oldvalue){
		$class = hikashop::get('class.file');
		$map = $field->field_table.'_'.$field->field_namekey;
		if(empty($field->field_options['file_type'])){
			$field->field_options['file_type']='file';
		}
		$file = $class->saveFile($map,$field->field_options['file_type']);
		if(!empty($file)){
			$value = $file;
		}else{
			if(!empty($oldvalue)){
				$value = $oldvalue;
			}else{
				$value = '';
			}
		}
		return parent::check($field,$value,$oldvalue);
	}
}
class hikashopCoupon extends hikashopText{
	function check(&$field,$value){
		$status = parent::check($field,$value);
		if($status){
			$zone_id = hikashop::getZone('shipping');
			$discount=hikashop::get('class.discount');
			$zoneClass = hikashop::get('class.zone');
			$zones = $zoneClass->getZoneParents($zone_id);
			$total = null;
			$price = null;
			$price->price_value_with_tax = 0;
			$price->price_value = 0;
			$price->price_currency_id = hikashop::getCurrency();
			$total->prices = array($price);
			if(empty($field->coupon)){
				$field->coupon=array();
			}
			$products = array();
			$field->coupon[$value] = $discount->loadAndCheck($value,$total,$zones,$products,true);
			if(empty($field->coupon[$value])){
				$app =& JFactory::getApplication();
				$app->enqueueMessage(JRequest::getVar('coupon_error_message'));
				$status = false;
			}
			static $validCoupons = array();
			if(!isset($validCoupons[$value])){
				$validCoupons[$value] = 1;
			}else{
				$validCoupons[$value]++;
			}
			$left = ($field->coupon[$value]->discount_quota - $field->coupon[$value]->discount_used_times);
			if($left<$validCoupons[$value]){
				$app =& JFactory::getApplication();
				$app->enqueueMessage('You cannot use the coupon '.$value.' more than '.$left.' times !');
				$status = false;
			}
		}
		return $status;
	}
}
class hikashopTextarea extends hikashopItem{
	function display($field,$value,$map,$inside,$options='',$test=false){
		$js = '';
		if($inside AND strlen($value) < 1){
			$value = addslashes($this->trans($field->field_realname));
			$this->excludeValue[$field->field_namekey] = $value;
			$js = 'onfocus="if(this.value == \''.$value.'\') this.value = \'\';" onblur="if(this.value==\'\') this.value=\''.$value.'\';"';
		}
		$cols = empty($field->field_options['cols']) ? '' : 'cols="'.intval($field->field_options['cols']).'"';
		$rows = empty($field->field_options['rows']) ? '' : 'rows="'.intval($field->field_options['rows']).'"';
		return '<textarea class="inputbox" id="'.$this->prefix.@$field->field_namekey.$this->suffix.'" name="'.$map.'" '.$cols.' '.$rows.' '.$js.' '.$options.'>'.$value.'</textarea>';
	}
}
class hikashopDropdown extends hikashopItem{
	function display($field,$value,$map,$type,$inside,$options='',$test=false){
		$string = '';
		if($type == "multiple"){
			$string.= '<input type="hidden" name="'.$map.'" value=" " />';
			$map.='[]';
			$arg = 'multiple="multiple"';
			if(!empty($field->field_options['size'])) $arg .= ' size="'.intval($field->field_options['size']).'"';
		}else{
			$arg = 'size="1"';
		}
		$string .= '<select id="'.$this->prefix.$field->field_namekey.$this->suffix.'" name="'.$map.'" '.$arg.$options.'>';
		if(empty($field->field_value)) return $string;
		$app =& JFactory::getApplication();
		$admin = $app->isAdmin();
		foreach($field->field_value as $oneValue => $title){
			$selected = ((int)$title->disabled && !$admin) ? 'disabled="disabled" ' : '';
			$selected .= ((is_numeric($value) AND is_numeric($oneValue) AND $oneValue == $value) OR (is_string($value) AND $oneValue === $value) OR is_array($value) AND in_array($oneValue,$value)) ? 'selected="selected" ' : '';
			$id = $this->prefix.$field->field_namekey.$this->suffix.'_'.$oneValue;
			$string .= '<option value="'.$oneValue.'" id="'.$id.'" '.$selected.'>'.$this->trans($title->value).'</option>';
		}
		$string .= '</select>';
		return $string;
	}
}
class hikashopSingledropdown extends hikashopDropdown{
	function display($field,$value,$map,$inside,$options='',$test=false){
		return parent::display($field,$value,$map,'single',$inside,$options,$test);
	}
}
class hikashopZone extends hikashopSingledropdown{
	function display($field,$value,$map,$inside,$options='',$test=false){
		static $namekey = null;
		if($field->field_options['zone_type']=='country'){
			if(!empty($options)){
				if(stripos($options,'onchange="')!==false){
					$options=preg_replace('#onchange="#i','onchange="changeState(this.value);',$options);
				}else{
					$options .= ' onchange="changeState(this.value);"';
				}
			}else{
				$options = ' onchange="changeState(this.value);"';
			}
			if(!empty($namekey)){
				$namekey='&field_namekey='.$namekey;
			}
			$js = '
			function changeState(newvalue){
				var defaultValInput = document.getElementById(\'state_default_value\');
				var defaultVal = \'\';
				var namekey = false;
				if(defaultValInput){
					defaultVal = defaultValInput.value;
					namekey = document.getElementById(\'state_namekey\').value;
				}
				if(namekey){
					try{
						new Ajax(\''.$field->field_url.'&field_type='.$field->field_table.$namekey.'&namekey=\'+newvalue, { method: \'get\', onComplete: function(result) { old = window.document.getElementById(\'state_dropdown\'); if(old){ old.innerHTML = result;if(namekey) {var stateSelect = document.getElementById(namekey); if(stateSelect) stateSelect.value=defaultVal;}}}}).request();
					}catch(err){
						new Request({url:\''.$field->field_url.'&field_type='.$field->field_table.$namekey.'&namekey=\'+newvalue, method: \'get\', onComplete: function(result) { old = window.document.getElementById(\'state_dropdown\'); if(old){ old.innerHTML = result;if(namekey) {var stateSelect = document.getElementById(namekey); if(stateSelect) stateSelect.value=defaultVal;}}}}).send();
					}
				}
			}
			window.addEvent(\'domready\', function(){ changeState(document.getElementById(\''.$this->prefix.$field->field_namekey.$this->suffix.'\').value); });
			';
			$doc =& JFactory::getDocument();
			$doc->addScriptDeclaration( $js );
		}elseif($field->field_options['zone_type']=='state' && !$test){
			$namekey=$field->field_namekey;
			return '<span id="state_dropdown"></span><input type="hidden" id="state_namekey" name="state_namekey" value="'.$this->prefix.$field->field_namekey.$this->suffix.'"/><input type="hidden" id="state_default_value" name="state_default_value" value="'.$value.'"/>';
		}
		return parent::display($field,$value,$map,$inside,$options,$test);
	}
	function check(&$field,$value,$oldvalue){
		return true;
	}
	function JSCheck(&$oneField,&$requiredFields,&$validMessages,&$values){
	}
}
class hikashopMultipledropdown extends hikashopDropdown{
	function display($field,$value,$map,$inside,$options='',$test=false){
		$value = explode(',',$value);
		return parent::display($field,$value,$map,'multiple',$inside,$options,$test=false);
	}
}
class hikashopRadioCheck extends hikashopItem{
	function display($field,$value,$map,$type,$inside,$options='',$test=false){
		$string = '';
		if($inside) $string = $this->trans($field->field_realname).' ';
		if($type == 'checkbox'){
			$string.= '<input type="hidden" name="'.$map.'" value=" "/>';
			$map.='[]';
		}
		if(empty($field->field_value)) return $string;
		$app =& JFactory::getApplication();
		$admin = $app->isAdmin();
		foreach($field->field_value as $oneValue => $title){
			$checked = ((int)$title->disabled && !$admin) ? 'disabled="disabled" ' : '';
			$checked .= ((is_string($value) AND $oneValue == $value) OR is_array($value) AND in_array($oneValue,$value)) ? 'checked="checked" ' : '';
			$id = $this->prefix.$field->field_namekey.$this->suffix.'_'.$oneValue;
			$string .= '<input type="'.$type.'" name="'.$map.'" value="'.$oneValue.'" id="'.$id.'" '.$checked.' '.$options.' /><label for="'.$id.'">'.$this->trans($title->value).'</label>';
		}
		return $string;
	}
}
class hikashopRadio extends hikashopRadioCheck{
	function display($field,$value,$map,$inside,$options='',$test=false){
		return parent::display($field,$value,$map,'radio',$inside,$options,$test);
	}
}
class hikashopDate extends hikashopItem{
	function display($field,$value,$map,$inside,$options='',$test=false){
		if(empty($field->field_options['format'])) $field->field_options['format'] = "%Y-%m-%d";
		$size = empty($field->field_options['size']) ? '' : 'size="'.$field->field_options['size'].'"';
		return JHTML::_('calendar', $value, $map,$this->prefix.$field->field_namekey.$this->suffix,$field->field_options['format'],$size);
	}
}
class hikashopCheckbox extends hikashopRadioCheck{
	function display($field,$value,$map,$inside,$options='',$test=false){
		$value = explode(',',$value);
		return parent::display($field,$value,$map,'checkbox',$inside,$options,$test);
	}
}
Return current item: HikaShop