<?php
class Catalog
{
function Catalog($config,$db,$dbtable_categs,$dbtable_items,$admin=false,$htpf=array())
{
//echo " ".basename(__FILE__).":".__LINE__;
$this->items = new CatalogItemController($config,$db,$dbtable_items,$admin,$htpf);
$this->categs = new CatalogCategController($config,$db,$dbtable_categs,$admin,$htpf);
$this->admin = $admin;
$this->postget = $_POST?$_POST:$_GET;
$this->config = $config;
$this->db = $db;
}
function think()
{
switch (@$this->postget["action"])
{
case $this->categs->dbtable."_clean_submit":$this->categ_clean_submit($this->postget["id"]);break;
case $this->categs->dbtable."_delete_submit":$this->categ_delete_submit($this->postget["id"]);break;
case $this->categs->dbtable."_update_submit":$this->categs->update($this->postget);break;
case "category_thumb_delete":$this->categs->category_thumb_delete($this->postget["id"]);break;
}
$this->items->think();
//$this->categs->think();
}
function categs_gets($id=0)
{
//echo " ".basename(__FILE__).":".__LINE__;
$result = $this->categs->gets($id);
//var_dump($result);
return $this->categs_gets_recursive($result);
}
//count categories items
function categs_gets_recursive($categs=array())
{
//echo " ".basename(__FILE__).":".__LINE__;
for($i=0;$i<count($categs);$i++)
{
$categs[$i]['childs'] = $this->categs_gets_recursive($categs[$i]['childs']);
if($this->admin)
{
$categs[$i]['total'] = $this->items->count_("WHERE categ_id=".$categs[$i]['id']);
foreach($categs[$i]['childs'] as $child_cat)
{
@$categs[$i]['subtotal'] += $child_cat['subtotal'];
}
@$categs[$i]['subtotal'] += $categs[$i]['total'];
}
}
// var_dump($categs);
return $categs;
}
function items_gets_by_categ($categ_id=0)
{
//echo " ".basename(__FILE__).":".__LINE__;
//return $this->items->gets("WHERE categ_id=".(int)$categ_id);
$conditions = "";
$conditions = " WHERE categ_id IN (".(int)$categ_id;
if(!$this->admin)
{
$childs = $this->categs->get_subcategs((int)$categ_id);
if(count($childs))
{
foreach($childs as $child_categ)
{
$conditions .= ", ".$child_categ;
}
}
}
$conditions .= " ) ";
return $this->items->gets($conditions);
}
function get_default_categ_id()
{
echo " ".basename(__FILE__).":".__LINE__;
$categ_id = 0;
if(@$this->postget["id"]>0)
{
$item = $this->items->get(" WHERE id=".$this->postget["id"]);
return $item["categ_id"];
}
else if($this->admin)
{
$categ = $this->categs->get();
return $categ["id"];
}
else
return null;
}
function categ_clean_submit($categ_id)
{
// $this->items->config["pagination"] = 0;
$items = $this->items->model->gets(" WHERE categ_id=".$categ_id);
foreach($items as $item)
{
$this->items->delete($item["id"]);
}
}
function categ_delete_submit($categ_id)
{
$subcategs = $this->categs->get_subcategs($categ_id);
$subcategs[] = $categ_id;
foreach($subcategs as $subcateg_id)
{
$this->categ_clean_submit($subcateg_id);
$this->categs->delete($subcateg_id);
}
}
function _getCount_search($keywords)
{
$sql = "SELECT COUNT(*) FROM ".$this->items->model->dbtable." AS i LEFT JOIN ".$this->categs->model->dbtable." AS c ON i.categ_id=c.id WHERE i.is_enabled='1' AND (i.title REGEXP '$keywords' OR i.comment REGEXP '$keywords') AND (c.is_enabled='1' OR i.categ_id=0) ORDER BY i.rank,i.title,i.id ";
$result = $this->db->query($sql);
//var_dump($result);
if($result)
return $result[0]["COUNT(*)"];
return null;
}
function _get_items_search($offset,$limit,$keywords)
{
$sql = "SELECT i.* FROM ".$this->items->model->dbtable." AS i LEFT JOIN ".$this->categs->model->dbtable." AS c ON i.categ_id=c.id WHERE i.is_enabled='1' AND (i.title REGEXP '$keywords' OR i.comment REGEXP '$keywords') AND (c.is_enabled='1' OR i.categ_id=0) ORDER BY i.rank,i.title,i.id LIMIT $offset,$limit";
$html["items"] = $this->db->query($sql);
return($html["items"]);
}
function search($keywords_get)
{
//echo $keywords_safe = htmlspecialchars($keywords_get, ENT_QUOTES);
$keywords_split = preg_split("/[\s,]+/", $keywords_get);
$keywords = "";
foreach($keywords_split as $keyword)
$keywords .= trim($keyword)."|";
$keywords = substr($keywords,0,strlen($keywords)-1);
$html = array();
//if($keywords)
{
$html["pagination"] = Misc::pagination($this->_getCount_search($keywords),$this->config["pagination"],@$this->postget["page"]);
$html["items"] = $this->_get_items_search($html["pagination"]["offset"], $this->config["pagination"],$keywords);
}
return $html;
}
function categs_get()
{
$categ_id = 0;
if(isset($this->postget["categ_id"]))
$categ_id = (int)$this->postget["categ_id"];
if(@(int)$this->postget["id"])
{
$item = $this->items->get("WHERE id=".(int)$this->postget["id"] );
$categ_id = $item["categ_id"];
}
$result = $this->categs->get("WHERE id=".$categ_id);
if($result)
return $result;
return null;
}
}
?>