Guest User

Untitled

a guest
Sep 1st, 2014
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.11 KB | None | 0 0
  1. <?php
  2. class hikashopCategorysubType{
  3.     var $type='tax';
  4.     var $value='';
  5.     var $multiple = false;
  6.     function load($form=true){
  7.         static $data = array();
  8.         if(!isset($data[$this->type])){
  9.             $query = 'SELECT category_id FROM '.hikashop_table('category').' WHERE  category_parent_id=0 LIMIT 1';
  10.             $db = JFactory::getDBO();
  11.             $db->setQuery($query);
  12.             $parent = (int)$db->loadResult();
  13.             $select = 'SELECT a.category_name,a.category_id,a.category_namekey';
  14.             $table = ' FROM '.hikashop_table('category') . ' AS a';
  15.             $app = JFactory::getApplication();
  16.             $translationHelper = hikashop_get('helper.translation');
  17.  
  18.             if($app->isAdmin() && $translationHelper->isMulti()){
  19.                 $user = JFactory::getUser();
  20.                 $locale = $user->getParam('language');
  21.                 if(empty($locale)){
  22.                     $config = JFactory::getConfig();
  23.                     if(HIKASHOP_J30){
  24.                         $locale = $config->get('language');
  25.                     }else{
  26.                         $locale = $config->getValue('config.language');
  27.                     }
  28.                 }
  29.                 $lgid = $translationHelper->getId($locale);
  30.                 $select .= ',b.value';
  31.                 $trans_table = 'jf_content';
  32.                 if($translationHelper->falang){
  33.                     $trans_table = 'falang_content';
  34.                 }
  35.                 $table .=' LEFT JOIN '.hikashop_table($trans_table,false).' AS b ON a.category_id=b.reference_id AND b.reference_table=\'hikashop_category\' AND b.reference_field=\'category_name\' AND b.published=1 AND language_id='.$lgid;
  36.             }
  37.  
  38.             static $multiTranslation = null;
  39.             $app = JFactory::getApplication();
  40.             if($multiTranslation === null && !$app->isAdmin()) {
  41.                 $translationHelper = hikashop_get('helper.translation');
  42.                 $multiTranslation = $translationHelper->isMulti(true);
  43.             }
  44.  
  45.             $query = $select.$table;
  46.             $query .= ' WHERE  a.category_type = \''.$this->type.'\' AND a.category_parent_id!='.$parent.' ORDER BY a.category_ordering ASC';
  47.             $db->setQuery($query);
  48.             if(!$app->isAdmin() && $multiTranslation && class_exists('JFalangDatabase')){
  49.                 $this->categories = $db->loadObjectList('','stdClass',false);
  50.             }elseif(!$app->isAdmin() && $multiTranslation && (class_exists('JFDatabase')||class_exists('JDatabaseMySQLx'))){
  51.                 if(HIKASHOP_J25){
  52.                     $this->categories = $db->loadObjectList('','stdClass',false);
  53.                 }else{
  54.                     $this->categories = $db->loadObjectList('',false);
  55.                 }
  56.             }else{
  57.                 $this->categories = $db->loadObjectList();
  58.             }
  59.             $data[$this->type] =& $this->categories;
  60.         }else{
  61.             $this->categories =& $data[$this->type];
  62.         }
  63.         $this->values = array();
  64.         if($form){
  65.             if(in_array($this->type,array('status','tax'))){
  66.                 $this->values[] = JHTML::_('select.option', '', JText::_('HIKA_NONE') );
  67.             }else{
  68.                 $this->values[] = JHTML::_('select.option', 0, JText::_('HIKA_NONE') );
  69.             }
  70.         }else{
  71.             if($this->type=='status'){
  72.                 $this->values[] = JHTML::_('select.option', '', JText::_('ALL_STATUSES') );
  73.             }else{
  74.                 $this->values[] = JHTML::_('select.option', 0, JText::_('ALL_'.strtoupper($this->type)) );
  75.             }
  76.         }
  77.         if(!empty($this->categories)){
  78.             foreach($this->categories as $k => $category){
  79.                 if(empty($category->value)){
  80.                     $val = str_replace(' ','_',strtoupper($category->category_name));
  81.                     $category->value = JText::_($val);
  82.                     if($val==$category->value){
  83.                         $category->value = $category->category_name;
  84.                     }
  85.                     $this->categories[$k]->value = $category->value;
  86.                 }
  87.  
  88.                 if($this->type=='status'){
  89.                     $this->values[] = JHTML::_('select.option', $category->category_name, $category->value );
  90.                 }elseif($this->type=='tax'){
  91.                     $field = $this->field;
  92.                     $this->values[] = JHTML::_('select.option', $category->$field, $category->value );
  93.                 }else{
  94.                     $this->values[] = JHTML::_('select.option', (int)$category->category_id, $category->value );
  95.                 }
  96.             }
  97.         }
  98.  
  99.     }
  100.  
  101.     function trans($status){
  102.         foreach($this->categories as $value){
  103.             if($value->category_name == $status){
  104.                 if($this->type == 'status' && $value->value == $status)
  105.                     return hikashop_orderStatus($status);
  106.                 return $value->value;
  107.             }
  108.         }
  109.         foreach($this->categories as $value){
  110.             if($value->category_namekey == $status){
  111.                 if($this->type == 'status' && $value->value == $status)
  112.                     return hikashop_orderStatus($status);
  113.                 return $value->value;
  114.             }
  115.         }
  116.         if($this->type == 'status')
  117.             return hikashop_orderStatus($status);
  118.         return $status;
  119.     }
  120.  
  121.     function get($val){
  122.         foreach($this->values as $value){
  123.             if($value->value == $val){
  124.                 return $value->text;
  125.             }
  126.         }
  127.         return $val;
  128.     }
  129.  
  130.     function display($map,$value,$form=true,$none=true,$id=''){
  131.         $this->value = $value;
  132.         if(!is_bool($form)){
  133.             $attribute = $form;
  134.             $form = $none;
  135.  
  136.         }elseif(!$form){
  137.             $attribute = ' onchange="document.adminForm.submit();"';
  138.         }else{
  139.             $attribute = '';
  140.         }
  141.         $this->load($form);
  142.  
  143.         if(!in_array($this->type,array('status','tax'))){
  144.             $value = (int)$value;
  145.         }
  146.         if(strpos($attribute,'size="')===false){
  147.             if($this->multiple && !HIKASHOP_J30){
  148.                 $attribute.=' size="3"';
  149.             }else{
  150.                 $attribute.=' size="1"';
  151.             }
  152.         }
  153.         if($this->multiple){
  154.             $attribute .= ' multiple="multiple"';
  155.             $map .='[]';
  156.         }
  157.         if(!empty($id)){
  158.             return JHTML::_('select.genericlist', $this->values, $map, 'class="inputbox"'.$attribute, 'value', 'text', $value , $id);
  159.         }else{
  160.             return JHTML::_('select.genericlist', $this->values, $map, 'class="inputbox"'.$attribute, 'value', 'text', $value );
  161.         }
  162.     }
  163.  
  164.     /**
  165.      *
  166.      * @param string $map
  167.      * @param string $value
  168.      * @param string $type
  169.      * @param integer $root
  170.      */
  171.     public function displaySingle($map, $value, $type = '', $root = 0, $delete = false) {
  172.  
  173.         hikashop_loadJslib('otree');
  174.         $id = str_replace(array('[',']'),array('_',''),$map);
  175.  
  176.         $key = 0;
  177.         $name = 'Root';
  178.         if((int)$value > 0) {
  179.             $categoryClass = hikashop_get('class.category');
  180.             $category = $categoryClass->get((int)$value);
  181.             if($category) {
  182.                 $key = (int)$value;
  183.                 $name = $category->category_name;
  184.             }
  185.         }
  186.  
  187.         if($delete && ($value === null || $value === '')) {
  188.             $key = '';
  189.             $name = '<em>'.JText::_('HIKA_NONE').'</em>';
  190.         }
  191.  
  192.         if(empty($type))
  193.             $type = array('product','manufacturer','vendor');
  194.  
  195.         $cleanText = '<em>'.str_replace("'", "\\'", JText::_('HIKA_NONE')).'</em>';
  196.  
  197.         $ret = '
  198. <div class="nameboxes" id="'.$id.'" onclick="window.nameboxes.focus(\''.$id.'\',\''.$id.'_text\');">
  199.     <div class="namebox" id="'.$id.'_namebox">
  200.         <input type="hidden" name="'.$map.'" id="'.$id.'_valuehidden" value="'.$key.'"/><span id="'.$id.'_valuetext">'.$name.'</span>
  201.         '.(!$delete?'<a class="editbutton" href="#" onclick="return false;"><span>-</span></a>':
  202.         '<a class="closebutton" href="#" onclick="window.nameboxes.clean(\''.$id.'\',this,\''.$cleanText.'\');return false;"><span>X</span></a>').'
  203.     </div>
  204.     <div class="nametext">
  205.         <input id="'.$id.'_text" type="text" style="width:50px;min-width:60px" onfocus="window.nameboxes.focus(\''.$id.'\',this);" onkeyup="window.nameboxes.simpleSearch(\''.$id.'\',this);" onchange="window.nameboxes.simpleSearch(\''.$id.'\',this);"/>
  206.         <span style="position:absolute;top:0px;left:-2000px;visibility:hidden" id="'.$id.'_span">span</span>
  207.     </div>
  208.     <div style="clear:both;float:none;"></div>
  209. </div>
  210. <div class="namebox-popup">
  211.     <div id="'.$id.'_otree" style="display:none;" class="oTree namebox-popup-content"></div>
  212. </div>
  213. <script type="text/javascript">
  214. var options = {rootImg:"'.HIKASHOP_IMAGES.'otree/", showLoading:false};
  215. var data = '.$this->getData($type, $root, $root == 0, $root == 0).';
  216. var '.$id.' = new window.oTree("'.$id.'",options,null,data,false);
  217. '.$id.'.addIcon("world","world.png");
  218. '.$id.'.callbackSelection = function(tree,id) {
  219.     var d = document, node = tree.get(id);
  220.     if( node.value && node.name ) {
  221.         var e = d.getElementById("'.$id.'_valuehidden");
  222.         if(e) e.value = node.value;
  223.         e = d.getElementById("'.$id.'_valuetext");
  224.         if(e) e.innerHTML = node.name;
  225.     }
  226.     var c = d.getElementById("'.$id.'_otree");
  227.     if(c) c.style.display = "none";
  228.     c = d.getElementById("'.$id.'_text");
  229.     if(c) c.value = "";
  230.     tree.sel(0);
  231. };
  232. '.$id.'.render(true);
  233. </script>';
  234.  
  235.         return $ret;
  236.     }
  237.  
  238.     /**
  239.      *
  240.      * @param string $map
  241.      * @param array $value
  242.      */
  243.     public function displayMultiple($map, $values, $type = '', $root = 0) {
  244.         if(substr($map,-2) == '[]')
  245.             $map = substr($map,0,-2);
  246.         $id = str_replace(array('[',']'),array('_',''),$map);
  247.         $ret = '<div class="nameboxes" id="'.$id.'" onclick="window.nameboxes.focus(\''.$id.'\',\''.$id.'_text\');">';
  248.         if(!empty($values)) {
  249.             foreach($values as $key => $name) {
  250.                 $obj = null;
  251.                 if(is_object($name)) {
  252.                     $obj = $name;
  253.                     $name = $name->category_name;
  254.                 }
  255.                 $ret .= '<div class="namebox" id="'.$id.'_'.$key.'">'.
  256.                     '<input type="hidden" name="'.$map.'[]" value="'.$key.'"/>'.$name.
  257.                     ' <a class="closebutton" href="#" onclick="window.hikashop.deleteId(\''.$id.'_'.$key.'\');return false;"><span>X</span></a>'.
  258.                     '</div>';
  259.             }
  260.         }
  261.  
  262.         $ret .= '<div class="namebox" style="display:none;" id="'.$id.'tpl">'.
  263.                 '<input type="hidden" name="{map}" value="{key}"/>{name}'.
  264.                 ' <a class="closebutton" href="#" onclick="window.hikashop.deleteId(this.parentNode);window.Oby.cancelEvent();return false;"><span>X</span></a>'.
  265.                 '</div>';
  266.  
  267.         $ret .= '<div class="nametext">'.
  268.             '<input id="'.$id.'_text" type="text" style="width:50px;min-width:60px" onfocus="window.nameboxes.focus(\''.$id.'\',this);" '. /*onblur="'.$jsShow.'(this,false);"*/' onkeyup="window.nameboxes.tree(\''.$id.'\',this);" onchange="window.nameboxes.tree(\''.$id.'\',this);"/>'.
  269.             '<span style="position:absolute;top:0px;left:-2000px;visibility:hidden" id="'.$id.'_span">span</span>'.
  270.             '</div>';
  271.  
  272.         hikashop_loadJslib('otree');
  273.  
  274.         if(empty($type))
  275.             $type = array('product','manufacturer','vendor');
  276.  
  277.         $ret .= '<div style="clear:both;float:none;"></div></div>
  278. <div class="namebox-popup">
  279.     <div id="'.$id.'_otree" style="display:none;" class="oTree namebox-popup-content"></div>
  280. </div>
  281. <script type="text/javascript">
  282. var options = {rootImg:"'.HIKASHOP_IMAGES.'otree/", showLoading:false};
  283. var data = '.$this->getData($type, $root, true).';
  284. var '.$id.' = new window.oTree("'.$id.'",options,null,data,false);
  285. '.$id.'.addIcon("world","world.png");
  286. '.$id.'.callbackSelection = function(tree,id) {
  287.     var d = document, node = tree.get(id);
  288.     if( node.value && node.name) {
  289.         var blocks = {map: "'.$map.'[]", key: node.value, name: node.name}, cur = d.getElementById("'.$id.'_"+node.value);
  290.         if(!cur) {
  291.             window.hikashop.dup("'.$id.'tpl", blocks, "'.$id.'_"+node.value);
  292.         }
  293.     }
  294.     var c = d.getElementById("'.$id.'_otree");
  295.     if(c) c.style.display = "none";
  296.     c = d.getElementById("'.$id.'_text");
  297.     if(c) c.value = "";
  298.     tree.sel(0);
  299. };
  300. '.$id.'.render(true);
  301. </script>';
  302.  
  303.         return $ret;
  304.     }
  305.  
  306.     /**
  307.      *
  308.      */
  309.     public function displayTree($id, $root = 0, $type = null, $displayRoot = false, $selectRoot = false) {
  310.         hikashop_loadJslib('otree');
  311.         if(empty($type))
  312.             $type = array('product','manufacturer','vendor');
  313.         $ret = '';
  314.  
  315.         $ret .= '<div id="'.$id.'_otree" class="oTree"></div>
  316. <script type="text/javascript">
  317. var options = {rootImg:"'.HIKASHOP_IMAGES.'otree/", showLoading:false};
  318. var data = '.$this->getData($type, $root, $displayRoot, $selectRoot).';
  319. var '.$id.' = new window.oTree("'.$id.'",options,null,data,false);
  320. '.$id.'.addIcon("world","world.png");
  321. '.$id.'.render(true);
  322. </script>';
  323.         return $ret;
  324.     }
  325.  
  326.     /**
  327.      *
  328.      * @param string $type
  329.      * @param integer $root
  330.      * @param boolean $displayRoot
  331.      * @param boolean $selectRoot
  332.      */
  333.     private function getData($type = 'product', $root = 0, $displayRoot = false, $selectRoot = false) {
  334.         $categoryClass = hikashop_get('class.category');
  335.         if($root == 1)
  336.             $root = 0;
  337.         $elements = $categoryClass->getList($type, $root, $displayRoot);
  338.  
  339.         $ret = '[';
  340.         $cpt = count($elements)-1;
  341.         $sep = '';
  342.         $rootDepth = 0;
  343.         foreach($elements as $k => $element) {
  344.             $next = null;
  345.             if($k < $cpt)
  346.                 $next = $elements[$k+1];
  347.  
  348.             $status = 4;
  349.             if(!empty($next) && $next->category_parent_id == $element->category_id)
  350.                 $status = 2;
  351.             if($element->category_type == 'root') {
  352.                 $status = 5;
  353.                 $rootDepth = (int)$element->category_depth + 1;
  354.             }
  355.             if(($element->category_id == $root) || ($root == 0 && !$displayRoot && $rootDepth == 0))
  356.                 $rootDepth = (int)$element->category_depth;
  357.  
  358.             $ret .= $sep.'{"status":'.$status.',"name":"'.str_replace('"','&quot;',$element->category_name).'"';
  359.  
  360.             if($element->category_type == 'root') {
  361.                 $ret .= ',"icon":"world"';
  362.                 if(!$selectRoot)
  363.                     $ret .= ',"noselection":1';
  364.                 else
  365.                     $ret .= ',"value":'.$element->category_id;
  366.             } else {
  367.                 $ret .= ',"value":'.$element->category_id;
  368.             }
  369.  
  370.             $sep = '';
  371.             if(!empty($next)) {
  372.                 if($next->category_depth > $element->category_depth && $element->category_type != 'root') {
  373.                     $ret .= ',"data":[';
  374.                 } else if($next->category_depth < $element->category_depth) {
  375.                     $ret .= '}'.str_repeat(']}', $element->category_depth - $next->category_depth);
  376.                     $sep = ',';
  377.                 } else {
  378.                     $ret .= '}';
  379.                     $sep = ',';
  380.                 }
  381.             } else {
  382.                 $ret .= '}';
  383.                 if($element->category_depth >= $rootDepth)
  384.                     $ret .= str_repeat(']}', $element->category_depth - $rootDepth);
  385.             }
  386.         }
  387.         $ret .= ']';
  388.  
  389.         return $ret;
  390.     }
  391. }
Advertisement
Add Comment
Please, Sign In to add comment