/**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $this->__baseScriptUrl = Yii::app()->assetManager->publish(dirname(__FILE__) . '/../assets');
     Yii::app()->getClientScript()->registerCssFile($this->__baseScriptUrl . '/products.css');
     $model = new ProductCategory();
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     $treeRoots = ProductCategory::model()->roots()->findAll();
     if (empty($treeRoots)) {
         $root = new ProductCategory();
         $root->name = 'Root';
         $root->saveNode();
     }
     if (isset($_POST['ProductCategory'])) {
         $model->attributes = $_POST['ProductCategory'];
         if ($model->parent_id == 'root') {
             $model->parent_id = null;
         } else {
             $model->parent_id = $_POST['ProductCategory']['parent_id'];
         }
         $fileUpload = CUploadedFile::getInstance($model, 'image');
         if (isset($fileUpload) && $model->validate()) {
             $uploadPath = YiiBase::getPathOfAlias('webroot') . '/files/products/';
             //Yii::app()->basePath . '/../jlwebroot/upload';
             if (!is_dir($uploadPath)) {
                 @mkdir($uploadPath);
                 @chmod($uploadPath, 0777);
             }
             $filename = time() . mt_rand(0, 0xfff) . '.' . $fileUpload->getExtensionName();
             $fileUpload->saveAs($uploadPath . '/' . $filename);
             /**
              * @todo chưa có setting cho thumbnail                 
              * */
             // thumbnails image
             $thumbsPath = $uploadPath;
             Yii::import('ext.phpthumb.EasyPhpThumb');
             $thumbs = new EasyPhpThumb();
             $thumbs->init();
             $thumbs->setThumbsDirectory($thumbsPath);
             $thumbs->load($uploadPath . '/' . $filename)->resize(80, 60)->save('thumb_' . $filename);
             $model->image = $filename;
         }
         $model->created = date('Y-m-d H:i:s', time());
         $model->modified = date('Y-m-d H:i:s', time());
         if ($model->parent_id == 'root') {
             if ($model->validate()) {
                 $model->saveNode();
                 $this->redirect(array('admin'));
             }
         } else {
             if ($model->validate()) {
                 $root = ProductCategory::model()->findByAttributes(array('id' => $model->parent_id));
                 $model->appendTo($root);
                 $this->redirect(array('admin'));
             }
         }
     }
     $arrTrees = ProductCategory::model()->roots()->findAll();
     $this->render('form', array('model' => $model, 'arrTrees' => $arrTrees));
 }