if (stripos($new->title, 'testing') !== false) { $categories['testing'] = true; } if (stripos($new->title, 'security') !== false) { $categories['security'] = true; } if (preg_match('/ [0-9]{4}/', $new->title)) { $categories['conf'] = true; } if (preg_match('/RC[0-9]{1}/', $new->title)) { $categories['testing'] = true; } if (stripos($new->title, 'alpha') !== false) { $categories['testing'] = true; } if (stripos($new->title, 'beta') !== false) { $categories['testing'] = true; } if (stripos($new->title, 'documentation') !== false) { $categories['documentation'] = true; } if (count($categories) == 0) { $categories['other'] = true; } foreach ($categories as $category => $one) { $newCategory = new NewsCategories(); $newCategory->news_id = $new->id; $newCategory->categories_id = $categoriesCode[$category]; $newCategory->save(); } }
public function actionAdd() { $this->layout = '//layouts/admin'; $this->pageTitle = 'Новая категория'; $this->breadcrumbs = array('Новости' => array('/admin/news'), 'Категории' => array('/admin/news/categories/list'), 'Новая категория'); $success = false; if (isset($_POST['data'])) { $model = new NewsCategories(); $tags = array(); if (isset($_POST['data']['tags'])) { $tags = $_POST['data']['tags']; unset($_POST['data']['tags']); } $dataArray = $_POST['data']; $dataArray['is_active'] = isset($_POST['data']['is_active']) && $_POST['data']['is_active'] == 1 ? 1 : 0; $dataArray['is_deleted'] = 0; $model->setAttributes($dataArray); if ($model->save()) { $success = true; if (isset($_FILES['anons_pic']) && $_FILES['anons_pic']['name'] || isset($_FILES['content_pic']) && $_FILES['content_pic']['name']) { $uploaddir = 'images/upload/' . date("d.m.Y", time()); if (!is_dir($uploaddir)) { mkdir($uploaddir); } if (isset($_FILES['anons_pic'])) { $tmp_name = $_FILES["anons_pic"]["tmp_name"]; $name = $_FILES["anons_pic"]["name"]; if ($name) { $uploadfile = $uploaddir . 'arca_' . $model->id . '_' . md5(basename($name) . time()) . "." . end(explode(".", $name)); if (move_uploaded_file($tmp_name, $uploadfile)) { $model->setAttributes(array("anons_pic" => "/" . $uploadfile)); $model->save(); } } } if (isset($_FILES['content_pic'])) { $tmp_name = $_FILES["content_pic"]["tmp_name"]; $name = $_FILES["content_pic"]["name"]; if ($name) { $uploadfile = $uploaddir . 'arcc_' . $model->id . '_' . md5(basename($name) . time()) . "." . end(explode(".", $name)); if (move_uploaded_file($tmp_name, $uploadfile)) { $model->setAttributes(array("content_pic" => "/" . $uploadfile)); $model->save(); } } } } if (is_array($tags) && count($tags)) { foreach ($tags as $tag) { $tagModel = Tags::model()->findByAttributes(array("name" => $tag)); if (!is_object($tagModel)) { $tagModel = new Tags(); $tagModel->setAttributes(array("name" => $tag, "count" => 0)); } $tagModel->count += 1; $tagModel->save(); $tagsRelations = new TagsRelations(); $tagsRelations->setAttributes(array("tag_id" => $tagModel->id, "object_id" => $model->id, "model" => "news_category")); $tagsRelations->save(); } } } } if ($success) { $this->redirect("/admin/news/categories/list"); } if (!isset($model) || !is_object($model)) { $model = new NewsCategories(); } $this->render('add', array("errors" => $model->errors)); }