/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionCreate() { $model = new StoreCategory('create'); $action = 'category'; // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['StoreCategory'])) { $model->attributes = $_POST['StoreCategory']; $parent_node = $_POST['StoreCategory']['node']; if ($parent_node != 0) { $node = StoreCategory::model()->findByPk($parent_node); $model->appendTo($node); } // file handling $imageUploadFile = CUploadedFile::getInstance($model, 'pic'); if ($imageUploadFile !== null) { // only do if file is really uploaded $imageFileExt = $imageUploadFile->extensionName; $save_path = dirname(Yii::app()->basePath) . '/upload/' . $action . '/'; if (!file_exists($save_path)) { mkdir($save_path, 0777, true); } $ymd = date("Ymd"); $save_path .= $ymd . '/'; if (!file_exists($save_path)) { mkdir($save_path, 0777, true); } $img_prefix = date("YmdHis") . '_' . rand(10000, 99999); $imageFileName = $img_prefix . '.' . $imageFileExt; $model->pic = $ymd . '/' . $imageFileName; $save_path .= $imageFileName; } if ($model->saveNode()) { if ($imageUploadFile !== null) { // validate to save file $imageUploadFile->saveAs($save_path); } $this->redirect(array('admin')); } } $this->render('create', array('model' => $model)); }
/** * Get category id by path. If category not exits it will new one. * @param $path string Main/Music/Rock * @return integer category id */ protected function getCategoryByPath($path) { if (isset($this->categoriesPathCache[$path])) { return $this->categoriesPathCache[$path]; } if ($this->rootCategory === null) { $this->rootCategory = StoreCategory::model()->findByPk(1); } $result = preg_split($this->subCategoryPattern, $path, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); $result = array_map('stripcslashes', $result); $parent = $this->rootCategory; $level = 2; // Level 1 is only root foreach ($result as $name) { $cr = new CDbCriteria(); $cr->with = array('cat_translate'); $cr->compare('cat_translate.name', $name); $model = StoreCategory::model()->find($cr); if (!$model) { $model = new StoreCategory(); $model->name = $name; $model->appendTo($parent); } $parent = $model; $level++; } // Cache category id $this->categoriesPathCache[$path] = $model->id; if (isset($model)) { return $model->id; } return 1; // root category }
/** * @param $data * @param null|StoreCategory $parent */ public function importCategories($data, $parent = null) { foreach ($data->{"Группа"} as $category) { // Find category by external id $model = C1ExternalFinder::getObject(C1ExternalFinder::OBJECT_TYPE_CATEGORY, $category->{"Ид"}); if (!$model) { $model = new StoreCategory(); $model->name = $category->{"Наименование"}; $model->appendTo($this->getRootCategory()); $this->createExternalId(C1ExternalFinder::OBJECT_TYPE_CATEGORY, $model->id, $category->{"Ид"}); } if ($parent === null) { $model->moveAsLast($this->getRootCategory()); } else { $model->moveAsLast($parent); } $model->saveNode(); // Process subcategories if (isset($category->{"Группы"})) { $this->importCategories($category->{"Группы"}, $model); } } }