public function actionProducts()
 {
     $criteria = new CDbCriteria();
     $criteria->addCondition("t.active=1");
     $criteria->addCondition("t.delete=0");
     $criteria->order = "t.order, t.id DESC";
     $category = null;
     if (isset($_GET['category'])) {
         //$criteria->addCondition("t.productCategories=".$_GET['category']);
         //$categories=ProductCategories::model()->findByPk($_GET['category']);
         $category = ProductCategories::model()->findByAttributes(array('non_utf8_name' => $_GET['category']));
         if ($category === null || $category->active == 0 || $category->delete == 1) {
             throw new CHttpException(404, 'Địa chỉ bạn yêu cầu không tồn tại.');
         }
         //$criteria->addCondition("t.productCategories=".$category->id);
         // danh sach cac danh muc con
         $sub_categories = array();
         array_push($sub_categories, $category->id);
         foreach ($category->childCategories as $key => $item) {
             array_push($sub_categories, $item->id);
         }
         //$sub_categories = "'".implode("', '", $sub_categories)."'";
         $criteria->addInCondition('t.productCategories', $sub_categories);
     }
     $count = Product::model()->count($criteria);
     $pages = new CPagination($count);
     // results per page
     $pages->pageSize = Config::model()->getValueByKey('rowsperpageproduct');
     $pages->applyLimit($criteria);
     $models = Product::model()->findAll($criteria);
     //$pageTitle = Yii::t('site', 'AllProducts');
     $pageTitle = 'Tất cả sản phẩm';
     $metaDescription = Config::model()->getValueByKey('metadescription');
     $metaKeywords = Config::model()->getValueByKey('metakeywords');
     if (isset($category)) {
         $pageTitle = $category->name;
         $metaDescription = $category->metadescription;
         $metaKeywords = $category->metakeywords;
     }
     $this->pageTitle = $pageTitle . ' - ' . Config::model()->getValueByKey('sitetitle');
     $this->metaDescription = $metaDescription;
     $this->metaKeywords = $metaKeywords;
     $this->render('products', array('products' => $models, 'categories' => $category, 'pages' => $pages));
 }
 public function getDropdown()
 {
     global $dataDropdown;
     $dataDropdown = array();
     $parents = ProductCategories::model()->with('levelTop')->findALl();
     foreach ($parents as $parent) {
         $dataDropdown[$parent->id] = $parent->name;
         $this->_subDropDown($parent->childCategories);
     }
     return $dataDropdown;
 }
        <div id="leftNav">
            <div class="h">
                <a href="<?php 
echo $this->createAbsoluteUrl('sitemap');
?>
" id="leftDmSp">
					<span class="sp_all_prod _rounded">Danh mục sản phẩm</span>
				</a>
            </div>
            <div class="c">
                <ul class="leftNavMenu">
				<?php 
foreach ($categories as $item) {
    ?>
				<?php 
    $categoriesSub = ProductCategories::model()->findAll('t.parentid = ' . $item->id . ' AND t.active=1 AND t.delete=0 ORDER BY t.order, t.id DESC');
    ?>
				<?php 
    if (count($categoriesSub)) {
        ?>
					<li class="mnu-item">
						<a href="<?php 
        echo $item->getPublicURL();
        ?>
"><span class="menu-text"><?php 
        echo $item->name;
        ?>
</span><span class="menu-icon"></span></a>
					<ul class="leftSubNavMenu">
						<?php 
        foreach ($categoriesSub as $subitem) {
 /**
  * Returns the data model based on the primary key given in the GET variable.
  * If the data model is not found, an HTTP exception will be raised.
  * @param integer $id the ID of the model to be loaded
  * @return ProductCategories the loaded model
  * @throws CHttpException
  */
 public function loadModel($id)
 {
     $model = ProductCategories::model()->findByPk($id);
     if ($model === null) {
         throw new CHttpException(404, 'The requested page does not exist.');
     }
     return $model;
 }
 public function actionImport()
 {
     if (isset($_POST['media'])) {
         $arr = preg_split('/[,]/', $_POST['media']);
         $file_path = Yii::getPathOfAlias('webroot') . '/uploads/' . $arr[0];
         $sheet_array = Yii::app()->yexcel->readActiveSheet($file_path);
         $countError = 0;
         $rowError = '';
         $index = 1;
         foreach ($sheet_array as $row) {
             if ($index > 1) {
                 try {
                     $code = trim($sheet_array[$index]['B']);
                     $model = Product::model()->findByAttributes(array('code' => $code));
                     if ($model === null) {
                         $model = new Product();
                     }
                     $model->name = trim($sheet_array[$index]['A']);
                     $model->non_utf8_name = Common::removeUnicodeURL($model->name);
                     if ($code == '') {
                         $model->code = 'SP_';
                     } else {
                         $model->code = $code;
                     }
                     $model->model = trim($sheet_array[$index]['C']);
                     $model->color = trim($sheet_array[$index]['D']);
                     $model->origin = trim($sheet_array[$index]['E']);
                     $model->unit = trim($sheet_array[$index]['F']);
                     $model->price = trim($sheet_array[$index]['G']);
                     $model->discount = trim($sheet_array[$index]['H']);
                     $model->summary = trim($sheet_array[$index]['I']);
                     $strCategory = trim($sheet_array[$index]['J']);
                     $strCategory = Common::removeUnicodeURL($strCategory);
                     $category = ProductCategories::model()->findByAttributes(array('non_utf8_name' => $strCategory));
                     if ($category != null) {
                         $model->productCategories = $category->id;
                     }
                     $model->description = trim($sheet_array[$index]['K']);
                     $model->order = trim($sheet_array[$index]['L']);
                     $media = trim($sheet_array[$index]['M']);
                     if ($media == '') {
                         $media = '-';
                     }
                     $model->createdby = Yii::app()->user->id;
                     $model->createdatetime = new CDbExpression('NOW()');
                     $model->setRelationRecords('media', $this->getRelationMedia('tbl_product', $media));
                     if (!$model->save()) {
                         $rowError = $rowError . $index . ',';
                         $countError += 1;
                     } else {
                         if ($code == '') {
                             $model->code = sprintf('SP_%1$06d', $model->id);
                             $model->save();
                         }
                     }
                 } catch (Exception $e) {
                     //throw new CHttpException(500,$e->getMessage());
                     $rowError = $rowError . $index . ',';
                     $countError += 1;
                 }
             }
             $index += 1;
         }
         if ($countError == 0) {
             Yii::app()->user->setFlash('importdata', 'Quá trình nhập liệu thành công!');
         } else {
             Yii::app()->user->setFlash('importdata', 'Có ' . $countError . ' dòng (' . substr($rowError, 0, -1) . ') trong ' . (count($sheet_array) - 1) . ' dòng dữ liệu xảy ra lỗi trong quá trình nhập liệu!');
         }
         $this->refresh();
     }
     $this->render('import');
 }