_categories = $categories;
}
private function _prepareCategories(){
foreach ($this->_categories as $key=>$category){
if($category['parent_id']==self::PARENT_ID){
$array = array();
$array = $category;
$array['children'] = $this->_getChild($category);
$array['count'] = count($array['children']);
$array['totalCount'] = $this->_getChildTotalCount($array);
array_push($this->_fmtCategories, $array);
}
}
}
private function _segmentColumns(){
foreach ($this->_fmtCategories as $key=>$fmtCategory){
$totalCount = $fmtCategory['totalCount'];
if($totalCount<=self::SINGLE_COLUMN_VALUE){
$this->_fmtCategories[$key]['colNumber'] = 1;
}else if($totalCount>self::SINGLE_COLUMN_VALUE&&$totalCount<=self::THREE_COLUMN_VALUE){
$this->_fmtCategories[$key]['colNumber'] = 2;
}else if($totalCount>self::THREE_COLUMN_VALUE){
$this->_fmtCategories[$key]['colNumber'] = 3;
}
if($this->_fmtCategories[$key]['colNumber']>0){
$this->_fmtCategories[$key]['columns'] = $this->_getColumns($this->_fmtCategories[$key]);
}
}
}
private function _outputMenuHtml($categories){
$html = '';
$extra_class = '';
if($_GET['main_page']!=='index'||isset($_GET['cPath'])||isset($_GET['manufacturers_id'])){
$extra_class = ' hide not_main';
} else {
$extra_class = ' ';
}
if(is_array($categories) && !empty($categories)){
$html .='';
$m=5;
foreach ($categories as $vk=>$category){
if($vk>$m){
break;
}else{
$html .= '
'.$category['categories_name'].'/'.$this->_getChildHtml($category['columns'],$category['categories_id']).'';
}
}
$html .='';
}
return $html;
}
private function _getChildHtml($columns,$basePath){
global $db;
$html = '';
if(isset($columns) && count($columns)>0){
$html .='';
}
return $html;
}
private function _getColumnHtml($column,$basePath){
$html = '';
$productsInCategory='';
if(isset($column) && !empty($column)){
$html .='';
foreach ($column as $item){
$itemPath = array();
$itemPath[] = $basePath;
$itemPath[] = $item['categories_id'];
$itemcPath = implode('_', $itemPath);
$html .='
'.$item['categories_name'].''; //2级分类
if(is_array($item['children']) && !empty($item['children'])){
$html .='
';
foreach ($item['children'] as $child){
$chilPath = $itemPath;
$chilPath[] = $child['categories_id'];
$childcPath = implode('_', $chilPath);
$html .= '- '.$child['categories_name'].'
'; //3级分类
// if(is_array($child['children']) && !empty($child['children'])){
// foreach ($child['children'] as $ch2 ) {
// $chil2Path = $chilPath;
// $chil2Path[] = $ch2['categories_id'];
// $child2cPath = implode('_', $chil2Path);
// //$html .= '- '.$child['categories_name'].'
';
// //$html .= $ch2['categories_name'];
//// $html .= ''.$ch2['categories_name'].''; //4级分类
// }
// }
}
$html .='
';
}
$html .='
';
}
$html .='
';
}
return $html;
}
private function _getColumns($fmtCategory){
$colNumber = $fmtCategory['colNumber'];
$columns = array();
switch ($colNumber){
case 1:
if(!empty($fmtCategory['children']))
$columns[] = $fmtCategory['children'];
break;
case 2:
if($fmtCategory['count']==1){
if(!empty($fmtCategory['children']))
$columns[] = $fmtCategory['children'];
}else if($fmtCategory['count']==2){
$columns[] = array($fmtCategory['children'][0]);
$columns[] = array($fmtCategory['children'][1]);
}else if($fmtCategory['count']>2){
$colSize = ceil($fmtCategory['totalCount']/2);
$children = $fmtCategory['children'];
$subCnt = 0;
$level = 0;
$column = array();
foreach ($children as $_k=>$_v){
if($_v['count']>=$colSize){
$columns[] = array($_v);
$subCnt += $_v['count'];
$level = 1;
unset($children[$_k]);
}
}
$length = count($children);
foreach ($children as $_key=>$_value){
$column[] = $_value;
$subCnt += $_value['count'];
unset($children[$_key]);
if($subCnt>=($colSize*($level+1))){
$columns[$level++] = $column;
$column = array();
}
if($_key==$length-1&&!empty($column)){
$columns[$level] = $column;
}
}
}
break;
case 3:
if($fmtCategory['count']==1){
if(!empty($fmtCategory['children']))
$columns[] = $fmtCategory['children'];
}else if($fmtCategory['count']==2){
$columns[] = array($fmtCategory['children'][0]);
$columns[] = array($fmtCategory['children'][1]);
}else if($fmtCategory['count']==3){
$columns[] = array($fmtCategory['children'][0]);
$columns[] = array($fmtCategory['children'][1]);
$columns[] = array($fmtCategory['children'][2]);
}else if($fmtCategory['count']>3){
$colSize = ceil($fmtCategory['totalCount']/3);
$children = $fmtCategory['children'];
$subCnt = 0;
$level = 0;
$column = array();
foreach ($children as $_k=>$_v){
if($_v['count']>=$colSize){
$columns[] = array($_v);
$subCnt += $_v['count'];
$level += 1;
unset($children[$_k]);
}
}
$length = count($children);
foreach ($children as $_key=>$_value){
if($level>=0){
$column[] = $_value;
$subCnt += $_value['count'];
}
if($subCnt>=$colSize&&$subCnt<($colSize*2)&&$level==0){
$level=1;
$columns[] = $column;
$column = array();
}
if($subCnt>=($colSize*2)&&$level==1){
$level=2;
$columns[] = $column;
$column = array();
}
if($_key==$length-1&&!empty($column)){
$columns[] = $column;
}
}
}
break;
}
return $columns;
}
private function _adjustColumns($categories){
return $categories;
}
private function _getChildTotalCount($menuItem){
$count = 0;
if($menuItem['count']>0){
foreach ($menuItem['children'] as $item){
if($item['count']>0)
{
$count +=$item['count'];
}else{
$count +=1;
}
}
}
return $count;
}
public function getMenuHtml(){
$this->_prepareCategories();
$this->_segmentColumns();
return $this->_getHtml();
}
private function _getHtml(){
return $this->_outputMenuHtml($this->_adjustColumns($this->_fmtCategories));
}
private function _getChild($menuItem){
$children = array();
foreach ($this->_categories as $key=>$child){
if($menuItem['categories_id']==$child['parent_id']){
$child['children'] = $this->_getChild($child);
$child['count'] = count($child['children']);
if($child['count']==0){
$child['count']=1;
}
array_push($children, $child);
unset($this->_categories[$key]);
}
}
return $children;
}
}
require($template->get_template_dir('tpl_side_menu.php',DIR_WS_TEMPLATE, $current_page_base,'sideboxes'). '/tpl_side_menu.php');
$title = BOX_HEADING_SM_TITLE;
$left_corner = false;
$right_corner = false;
$right_arrow = false;
$title_link = false;
$sql = 'select c.categories_id,cd.categories_name,c.categories_image,c.parent_id from '.TABLE_CATEGORIES.' c ,'.TABLE_CATEGORIES_DESCRIPTION.' cd where c.categories_id=cd.categories_id and cd.language_id="'.(int)$_SESSION['languages_id'].'" and c.categories_status="1" order by c.sort_order asc, cd.categories_name asc';
$categories_tab = $db->Execute($sql);
$counter = 0;
$categories = array();
$category_image = $categories_tab->fields['categories_image'];
$menus = array();
$_max = 70;
while (!$categories_tab->EOF) {
array_push($menus, $categories_tab->fields);
if($categories_tab->fields['parent_id']==0){
if(count($main_categories)<$_max)
$main_categories[] = $categories_tab->fields;
}else{
$categories[] = $categories_tab->fields;
}
$categories_tab->MoveNext();
}
$smb = new Side_Menu_Block($menus);
$content = $smb->getMenuHtml();
require($template->get_template_dir($column_box_default, DIR_WS_TEMPLATE, $current_page_base,'common') . '/' . $column_box_default);
?>