public function actionCreate() { $request = Yii::app()->request->getIsPostRequest(); $ajaxRequest = Yii::app()->request->getIsAjaxRequest(); if ($request) { $berita = new Berita(); $kategori = new Kategori(); $tags = new Tags(); $image = new Image(); $postBerita = !empty($_POST['Berita']) ? $_POST['Berita'] : ""; $postKategori = !empty($_POST['Kategori']) ? $_POST['Kategori'] : ""; $postTags = !empty($_POST['Tags']) ? $_POST['Tags'] : ""; if ($kategori) { $kategori->setAttributes($postKategori); } if ($tags) { $tags->setAttributes($postTags); } if ($postBerita) { $berita->setAttributes($postBerita); $berita->setCreatetime($postBerita['tgl_create']); $berita->setUpdatetime($postBerita['tgl_update']); } } $jsonData = array(); if ($ajaxRequest) { echo CJSON::encode($jsonData); Yii::app()->end(); } else { $this->render("create", array()); } }
public function testValidation() { $contact = $this->contacts('testAnyone'); $tag = new Tags(); $tag->setAttributes(array('itemId' => $contact->id, 'type' => get_class($contact), 'itemName' => $contact->name, 'tag' => 'test', 'taggedBy' => 'testuser')); $this->assertSaves($tag); // ensure that normalization was performed upon validation $this->assertEquals(Tags::normalizeTag('test'), $tag->tag); // ensure that tag must be unique $tag = new Tags(); $tag->setAttributes(array('itemId' => $contact->id, 'type' => get_class($contact), 'itemName' => $contact->name, 'tag' => 'test', 'taggedBy' => 'testuser')); $tag->validate(); $this->assertTrue($tag->hasErrors('tag')); // ensure that tag must be unique $tag = new Tags(); $tag->setAttributes(array('itemId' => $contact->id, 'type' => get_class($contact), 'itemName' => $contact->name, 'tag' => '#test', 'taggedBy' => 'testuser')); $tag->validate(); $this->assertTrue($tag->hasErrors('tag')); // ensure that tag must be unique $tag = new Tags(); $tag->setAttributes(array('itemId' => $contact->id, 'type' => get_class($contact), 'itemName' => $contact->name, 'tag' => '#test,', 'taggedBy' => 'testuser')); $tag->validate(); $this->assertTrue($tag->hasErrors('tag')); }
public function actionEdit() { $this->layout = '//layouts/admin'; $success = false; $id = $_REQUEST['id']; $model = NewsCategories::model()->findByPk($id); $this->pageTitle = is_object($model) ? $model->name : 'Новая категория'; $this->breadcrumbs = array('Новости' => array('/admin/news'), 'Категории' => array('/admin/news/categories/list'), is_object($model) ? $model->name : 'Новая категория'); if (isset($_POST['data']) && is_object($model)) { $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; $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']) && $_FILES["anons_pic"]["name"]) { if ($model->anons_pic) { $anons_pic = Yii::app()->basePath . "/.." . $model->anons_pic; if (is_file($anons_pic)) { unlink($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)); if (!$model->save()) { print_r($model->errors); die; } } } } if (isset($_FILES['content_pic']) && $_FILES["content_pic"]["name"]) { if ($model->content_pic) { $content_pic = Yii::app()->basePath . "/.." . $model->content_pic; if (is_file($content_pic)) { unlink($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)) { $trs = TagsRelations::model()->findAllByAttributes(array("object_id" => $id, "model" => "news_category")); if (is_array($trs) && count($trs)) { foreach ($trs as $tr) { $tr->delete(); } } 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(); } } } } $this->render('edit', array('model' => $model, 'success' => $success, 'errors' => $model->errors)); }
public function actionEdit() { $this->layout = '//layouts/admin'; $success = false; $id = $_REQUEST['id']; $model = CatalogObjects::model()->findByPk($id); $this->pageTitle = is_object($model) ? $model->name : 'Новый товар'; $this->breadcrumbs = array('Каталог' => array('/admin/catalog'), is_object($model) ? $model->name : 'Новый товар'); if (isset($_POST['data']) && is_object($model)) { $tags = array(); if (isset($_POST['data']['tags'])) { $tags = $_POST['data']['tags']; unset($_POST['data']['tags']); } $dataArray = $_POST['data']; $additionalDataArray = isset($dataArray['additional']) ? $dataArray['additional'] : array(); unset($dataArray['additional']); $dataArray['is_active'] = isset($_POST['data']['is_active']) && $_POST['data']['is_active'] == 1 ? 1 : 0; $dataArray['on_main'] = isset($_POST['data']['on_main']) && $_POST['data']['on_main'] == 1 ? 1 : 0; $model->setAttributes($dataArray); if ($model->save()) { $success = true; if (isset($_FILES['anons_pic']) || isset($_FILES['content_pic'])) { $uploaddir = 'images/upload/' . date("d.m.Y", time()); if (!is_dir($uploaddir)) { mkdir($uploaddir); } if (isset($_FILES['anons_pic']) && $_FILES["anons_pic"]["name"]) { if ($model->anons_pic) { $anons_pic = Yii::app()->basePath . "/.." . $model->anons_pic; if (is_file($anons_pic)) { unlink($anons_pic); } } $tmp_name = $_FILES["anons_pic"]["tmp_name"]; $name = $_FILES["anons_pic"]["name"]; if ($name) { $ext = explode(".", $name); $uploadfile = $uploaddir . '/na_' . $model->id . '_' . md5(basename($name) . time()) . "." . end($ext); if (move_uploaded_file($tmp_name, $uploadfile)) { $model->setAttributes(array("anons_pic" => "/" . $uploadfile)); $model->save(); } } } if (isset($_FILES['content_pic']) && $_FILES["content_pic"]["name"]) { if ($model->content_pic) { $content_pic = Yii::app()->basePath . "/.." . $model->content_pic; if (is_file($content_pic)) { unlink($content_pic); } } $tmp_name = $_FILES["content_pic"]["tmp_name"]; $name = $_FILES["content_pic"]["name"]; if ($name) { $ext = explode(".", $name); $uploadfile = $uploaddir . '/nc_' . $model->id . '_' . md5(basename($name) . time()) . "." . end($ext); if (move_uploaded_file($tmp_name, $uploadfile)) { $model->setAttributes(array("content_pic" => "/" . $uploadfile)); $model->save(); } } } if (isset($_FILES['extra_img']) && $_FILES["extra_img"]["name"]) { if ($model->extra_img) { $extra_img = Yii::app()->basePath . "/.." . $model->extra_img; if (is_file($extra_img)) { unlink($extra_img); } } $tmp_name = $_FILES["extra_img"]["tmp_name"]; $name = $_FILES["extra_img"]["name"]; if ($name) { $ext = explode(".", $name); $uploadfile = $uploaddir . '/nc_' . $model->id . '_' . md5(basename($name) . time()) . "." . end($ext); if (move_uploaded_file($tmp_name, $uploadfile)) { $model->setAttributes(array("extra_img" => "/" . $uploadfile)); $model->save(); } } } } if (is_array($tags) && count($tags)) { $trs = TagsRelations::model()->findAllByAttributes(array("object_id" => $id, "model" => "catalog_object")); if (is_array($trs) && count($trs)) { foreach ($trs as $tr) { $tr->delete(); } } 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" => "catalog_object")); $tagsRelations->save(); } } if (is_array($additionalDataArray) && count($additionalDataArray)) { foreach ($additionalDataArray as $fieldName => $data) { $fieldModel = CatalogFilteredFields::model()->findByAttributes(array("name" => $fieldName, "category_id" => $model->category_id)); if (is_object($fieldModel)) { switch ($fieldModel->data_type) { case 'varchar': case 'text': case 'html': if ($data) { $newVal = FieldsValues::model()->findByAttributes(array("field_id" => $fieldModel->id, "object_id" => $model->id)); if (!is_object($newVal)) { $newVal = new FieldsValues(); } $newVal->setAttributes(array("field_id" => $fieldModel->id, "object_id" => $model->id, "text_val" => $data)); $newVal->save(); } break; case 'int': case 'list': if (is_numeric($data)) { $newVal = FieldsValues::model()->findByAttributes(array("field_id" => $fieldModel->id, "object_id" => $model->id)); if (!is_object($newVal)) { $newVal = new FieldsValues(); } $newVal->setAttributes(array("field_id" => $fieldModel->id, "object_id" => $model->id, "int_val" => $data)); $newVal->save(); } break; case 'bool': $data = $data ? 1 : 0; $newVal = FieldsValues::model()->findByAttributes(array("field_id" => $fieldModel->id, "object_id" => $model->id)); if (!is_object($newVal)) { $newVal = new FieldsValues(); } $newVal->setAttributes(array("field_id" => $fieldModel->id, "object_id" => $model->id, "int_val" => $data)); $newVal->save(); break; case 'date': $newVal = FieldsValues::model()->findByAttributes(array("field_id" => $fieldModel->id, "object_id" => $model->id)); if (!is_object($newVal)) { $newVal = new FieldsValues(); } $newVal->setAttributes(array("field_id" => $fieldModel->id, "object_id" => $model->id, "int_val" => strtotime($data))); $newVal->save(); break; case 'multiple': /* 09.04.2015 * TODO: придумать что то лучше, чем удаление записей перед созданием новых... */ if (is_array($data) && count($data) && $data[0]) { $oldValues = FieldsValues::model()->findAllByAttributes(array("field_id" => $fieldModel->id, "object_id" => $model->id)); if (is_array($oldValues) && count($oldValues)) { foreach ($oldValues as $ov) { $ov->delete(); } } if (is_array($data) && count($data)) { foreach ($data as $v) { $newVal = new FieldsValues(); $newVal->setAttributes(array("field_id" => $fieldModel->id, "object_id" => $model->id, "int_val" => $v)); $newVal->save(); } } } break; } } } } } } $this->render('edit', array('model' => $model, 'success' => $success, 'errors' => $model->errors)); }