db; $table = self::$table; $where = array(); $where[] = "c.`category_id` = {$parent_id}"; $sql = " SELECT * FROM `{$table}` c WHERE ".implode(" AND ", $where)." ORDER BY c.`name_am` ASC "; $command = $connection->createCommand($sql); $res = $command->queryAll(); $category_data = array(); foreach ($res as $result) { $category_data[] = array( 'id' => $result['id'], 'path' => self::getCategoryPath($result['id']) ); $category_data = array_merge($category_data, self::getAllCategories($result['id'])); } return $category_data; } public static function getCategoryPathArray($id, array &$res_id = null) { if (is_null($res_id)) { $res_id = array(); } $connection = Yii::app()->db; $table = self::$table; $sql = " SELECT c.`id`, c.`name_am`, c.`name_ru`, c.`category_id` FROM `{$table}` c LEFT JOIN `{$table}` cd ON (c.id = cd.id) WHERE c.id = {$id} "; $command = $connection->createCommand($sql); $result = $command->queryAll(); if (!$result) return false; $result = $result[0]; if ($result['category_id']) { $res_id[] = $result["id"]; self::getCategoryPathArray($result['category_id'], $res_id); return $res_id; } else { $res_id[] = $result["id"]; return $res_id; } } public static function getCategoryPathArray1($id) { $arrPath = self::getCategoryPathArray($id); if ($arrPath) { $length = count($arrPath); $arrPathSort = array(); foreach ($arrPath as $item) { $arrPathSort[] = $arrPath[--$length]; } return $arrPathSort; } return false; } public static function getCategoryPath($id, $link = false) { $table = Category::$table; $connection = Yii::app()->db; $sql = " SELECT c.`id`, c.`name_am`, c.`name_ru`, c.`name_en`, c.`category_id` FROM `{$table}` c LEFT JOIN `{$table}` cd ON (c.id = cd.id) WHERE c.id = {$id} "; $command = $connection->createCommand($sql); $result = $command->queryAll(); if (!$result) return false; $result = $result[0]; $_key = Content::TranslateMeField("name"); $_name = $result[$_key]; $link = $link === true ? 'product' : $link; if($link){ if($link == 'category'){ $url = Yii::app()->request->baseUrl . "/index.php/site/category?id=" . $result['id']; } elseif($link == 'product') { $url = Yii::app()->request->baseUrl . "/index.php/site/ProductList?id_category=" . $result['id']; } } if ($result['category_id']) { if($link == 'category' && isset($_GET['id']) && $_GET['id'] == $id){ $name = self::getCategoryPath($result['category_id'], $link) . " > " . $_name; } elseif($link){ $name = $name = self::getCategoryPath($result['category_id'], $link) . " > " . $_name . ""; } else{ $name = self::getCategoryPath($result['category_id'], $link) . " > " . $_name; } return $name; } else { $name = $_name; if($link == 'category' && isset($_GET['id']) && $_GET['id'] == $id){ } elseif($link){ $name = "" . $name . ""; } return $name; } } public static function getCategoryChildById($id = 0) { $connection = Yii::app()->db; $table = self::$table; $sql = "SELECT `id` FROM {$table} WHERE `category_id`={$id}"; $command = $connection->createCommand($sql); $result = $command->queryAll(); $children = array(); if ($result) { # It has children, let's get them. foreach ($result as $item) { # Add the child to the list of children, and get its subchildren $children[$item["id"]] = self::getCategoryChildById($item["id"]); } } return $children; } public static function getCategoryChildPretty($id = 0) { $arrCategories = self::getCategoryChildById($id); //echo "
"; print_r( ($arrCategories) ); echo "
"; $result = self::find_keys($arrCategories); $result[] = $id; return $result; } public static function find_keys(array $haystack, array &$result = null) { if (is_null($result)) { $result = array(); } foreach ($haystack as $key => $value) { $result[] = $key; if (is_array($value)) { self::find_keys($value, $result); } } return $result; } public static function getCategorySecondLevel($id = 0) { $table = self::$table; $model = MarketCategory::model(); $where = array(); $where[] = "cp.`category_id`=0"; if ($id) $where[] = "cp.`id`={$id}"; $criteria = new CDbCriteria(); $criteria->alias = "c"; $criteria->order = "`name_am` ASC"; $criteria->join = "LEFT JOIN {$table} cp ON cp.`id`=c.`category_id`"; $criteria->condition = implode(" AND ", $where); $objSecondLevel = $model->findAll($criteria); return $objSecondLevel; } public static function delCategory($id){ $table = Products::$table; $result = self::getCategoryChildById($id); $result = self::find_keys($result); $result[] = $id; $conn = Yii::app()->db; $sql = "DELETE FROM `market_product_category` WHERE `category_id` IN (".implode(", ", $result).")"; $res = $conn->createCommand($sql)->execute(); $sql = "DELETE FROM `market_category` WHERE `id` IN (".implode(", ", $result).")"; $res = $conn->createCommand($sql)->execute(); return $res; } public static function getCategoryById($id){ $conn = Yii::app()->db; $sql = " SELECT * FROM `".self::$table."` WHERE `id`={$id}"; $model = $conn->createCommand($sql)->queryAll(); return $model ? $model[0] : false; } public static function getAllCategoryLabel(){ $conn = Yii::app()->db; $field = Content::TranslateMeField("all"); $sql = " SELECT `{$field}` AS label, `active` FROM `all_category` WHERE `id` = 1 "; $command = $conn->createCommand($sql); $result = $command->queryAll(); return $result ? (object) $result[0] : false; } public static function getParent($id){ $conn = Yii::app()->db; $sql = " SELECT `category_id` FROM `market_category` WHERE `id` = {$id} LIMIT 1 "; $command = $conn->createCommand($sql); $result = $command->queryAll(); return $result ? $result[0]['category_id'] : false; } } ?>