/** * 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 . '/news.css'); $model = new NewsCategory(); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); $treeRoots = NewsCategory::model()->roots()->findAll(); if (empty($treeRoots)) { $root = new NewsCategory(); $root->name = 'Root'; $root->saveNode(); } if (isset($_POST['NewsCategory'])) { $model->attributes = $_POST['NewsCategory']; if ($model->parent_id == 'root') { $model->parent_id = null; } else { $model->parent_id = $_POST['NewsCategory']['parent_id']; } $fileUpload = CUploadedFile::getInstance($model, 'image'); if (isset($fileUpload) && $model->validate()) { $uploadPath = YiiBase::getPathOfAlias('webroot') . '/files/news/'; //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 = NewsCategory::model()->findByAttributes(array('id' => $model->parent_id)); $model->appendTo($root); $this->redirect(array('admin')); } } } $arrTrees = NewsCategory::model()->roots()->findAll(); $this->render('form', array('model' => $model, 'arrTrees' => $arrTrees)); }