save() public method

Saves the category.
Since: 2.0.0
public save ( $FormPostValues, array | false $Settings = false ) : integer
$Settings array | false Additional settings to affect saving.
return integer ID of the saved category.
Ejemplo n.º 1
0
 /**
  * saves/updates a category from a json-rpc call
  * @param  $categoryJson
  * @return a status array which contains the error message if needed
  * {category_id:'', errorCode:'', errorMessage:''}
  */
 function saveCategoryFromJsonObject($categoryJson)
 {
     // TODO make a sanity check to see if required parameters are set and check if the user has privileges to update a category
     $category = null;
     if (isset($categoryJson->id)) {
         $category = CategoryModel::find_by_id($categoryJson->id);
     }
     if ($category == null) {
         $category = new CategoryModel();
     }
     $category->loadFromObject($categoryJson);
     $category->save();
     // also save languages
     $language_values = $categoryJson->lang;
     foreach ($language_values as $lang) {
         $category_lang = CategoryLangModel::find(array('conditions' => 'category_id = ' . $category->id . ' AND lang_id = ' . $lang->lang_id));
         if ($category_lang == null) {
             $category_lang = new CategoryLangModel();
         }
         $category_lang->category_id = $category->id;
         $category_lang->lang_id = $lang->lang_id;
         $category_lang->name = $lang->name;
         $category_lang->keywords = $lang->keywords;
         $category_lang->short_desc = $lang->short_desc;
         $category_lang->description = $lang->description;
         $category_lang->save();
     }
     $this->fixCategoryOrderRecursive();
     return array('category_id' => $category->id, 'errorCode' => 0, 'errorMessage' => '');
 }
Ejemplo n.º 2
0
 protected static function actionActive()
 {
     $category = new CategoryModel($_GET['id']);
     $category->active = $category->active ? '0' : '1';
     $category->save();
     self::redirect(App::getLink('AdminCategories'));
 }
Ejemplo n.º 3
0
 /**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate($section_id)
 {
     $model = new CategoryModel();
     $model->section_id = $section_id;
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['CategoryModel'])) {
         $model->attributes = $_POST['CategoryModel'];
         if ($model->save()) {
             $this->redirect(array('view', 'id' => $model->id));
         }
     }
     $this->render('create', array('model' => $model));
 }
 /**
  *
  * @return unknown_type
  * @todo $_POST shouldn't be used, check how this can be integrated with {@link Request}
  */
 public function saveAction()
 {
     $View = new View('category/edit');
     $View->assign('_title_', _('View Category'));
     $id = isset($_POST['CatID']) ? (int) $_POST['CatID'] : 0;
     $Category = new CategoryModel($id);
     $Category->load();
     $Category->Categoria = $_POST['Categoria'];
     if (!$Category->save()) {
         $_SESSION['_MESSAGE_'] = _('Couldn\'t save Category');
         header('Location: ' . BASE_URL . '/category');
         exit;
     }
     $_SESSION['_MESSAGE_'] = _('Saved');
     if ($id == 0) {
         $DbConnection = DbConnection::getInstance();
         $id = $DbConnection->getLastId();
     }
     header('Location: ' . BASE_URL . "/category/view/?id={$id}");
 }
Ejemplo n.º 5
0
<?php

$cate_name = $_GET['CateName'];
$style_id = $_GET['StyleId'];
if (trim($cate_name) !== "") {
    $connection = Yii::app()->db;
    //搜索 category 表中是否存在此次上送的 名字。 如果没有就插入记录。
    $categoryModel = new CategoryModel();
    $ret = CategoryModel::model()->find('name=:cate_name', array(':cate_name' => $cate_name));
    if (!isset($ret)) {
        $categoryModel->name = $cate_name;
        $categoryModel->create_time = time();
        $categoryModel->save();
    }
    //找到名字对应的_id。以便做插入 rel 关系表的时候用
    $sql = "select _id from tbl_category where name = :CateName";
    $command = $connection->createCommand($sql);
    $cate_id = $command->query(array(':CateName' => $cate_name))->readAll();
    if (isset($cate_id)) {
        //插入 tbl_style_category_rel 表
        $StyleCateRelModel = new StyleCateRelModel();
        $StyleCateRelModel->style_id = $style_id;
        $StyleCateRelModel->category_id = $cate_id[0]['_id'];
        $StyleCateRelModel->create_time = time();
        $StyleCateRelModel->save();
    }
}
$sql = "select B.name as cate_name\n\t\t\tfrom tbl_style_category_rel A\n\t\t\tinner join tbl_category B on B._id =  A.category_id\n\t\t\twhere A.style_id = :style_id order by B.name asc";
$command = $connection->createCommand($sql);
$cates = $command->query(array(':style_id' => $style_id))->readAll();
foreach ($cates as $cate) {
Ejemplo n.º 6
0
 public function actionUpStyle()
 {
     $connection = Yii::app()->db;
     $Cates = array();
     $Tags = array();
     $isSuc = false;
     $userId = $_REQUEST['id'];
     $picDesc = $_REQUEST['desc'];
     $category_name = $_REQUEST['category_name'];
     $tag_name = $_REQUEST['tag_name'];
     $categories = explode(",", $category_name);
     $tags = explode(",", $tag_name);
     //本地
     //$root = YiiBase::getPathOfAlias('webroot').Yii::app()->getBaseUrl();
     //服务器代码
     $root = YiiBase::getPathOfAlias('webroot');
     $folder = $root . '/images/images/styles/' . $userId . '/';
     $desFilePath;
     $tmpFilePath;
     $relPath = Yii::app()->getBaseUrl() . '/images/images/styles/' . $userId . '/';
     //echo ($folder);
     //exit();
     $this->mkDirIfNotExist($folder);
     if ($_FILES["file"]["type"] == "image/gif" || $_FILES["file"]["type"] == "image/jpeg" || $_FILES["file"]["type"] == "image/png" || $_FILES["file"]["type"] == "image/jpg" || $_FILES["file"]["type"] == "image/pjpeg") {
         if ($_FILES["file"]["error"] > 0) {
             $isSuc = false;
         } else {
             $tmpFilePath = $_FILES["file"]["tmp_name"];
             /*
             				$array = explode("/", $tmpFilePath); 
             				var_dump($array[ count($array) -1]);
             			    $tmpPath  = $tmpPath.$array[ count($array) -1];
             */
             $name = $this->getUploadImageFileName($_FILES["file"]["name"]);
             $desFilePath = $folder . $name;
             $desThumbPath = $folder . 'thumb/' . $name;
             $relPath = $relPath . $name;
             if (file_exists($desFilePath)) {
                 unlink($desFilePath);
                 //echo $_FILES["file"]["name"] . " already exists. ";
             } else {
                 move_uploaded_file($tmpFilePath, $desFilePath);
                 //生成缩略图
                 $im = null;
                 $imtp = null;
                 if ($_FILES["file"]["type"] == "image/gif") {
                     $im = imagecreatefromgif($desFilePath);
                     $imtp = 'gif';
                 } else {
                     if ($_FILES["file"]["type"] == "image/jpg" || $_FILES["file"]["type"] == "image/jpeg") {
                         $im = imagecreatefromjpeg($desFilePath);
                         $imtp = 'jpg';
                     } else {
                         if ($_FILES["file"]["type"] == "image/png") {
                             $im = imagecreatefrompng($desFilePath);
                             $imtp = 'png';
                         }
                     }
                 }
                 $this->mkDirIfNotExist($folder . 'thumb/');
                 CThumb::resizeImage($im, 100, 100, $desThumbPath, $imtp);
                 //------------生成缩略图
                 $isSuc = true;
             }
         }
     } else {
         $isSuc = false;
     }
     if ($isSuc) {
         $styleModel = new StyleModel();
         $tagModel = new TagModel();
         $categoryModel = new CategoryModel();
         $styleModel->image = $relPath;
         $styleModel->user_id = $userId;
         $styleModel->create_time = time();
         $styleModel->like_num = 0;
         $styleModel->recommand_val = 0;
         if (!$styleModel->save()) {
             echo "style saved fail!";
             return false;
         }
         foreach ($categories as $cate_name) {
             //搜索 category 表中是否存在此次上送的 名字。 如果没有就插入记录。
             $ret = CategoryModel::model()->find('name=:cate_name', array(':cate_name' => $cate_name));
             if (!isset($ret)) {
                 $categoryModel->name = $cate_name;
                 $categoryModel->create_time = time();
                 if (!$categoryModel->save()) {
                     echo "category zd fail!";
                 }
             }
             //找到名字对应的_id。以便做插入 rel 关系表的时候用
             $sql = "select _id from tbl_category where name = :CateName";
             $command = $connection->createCommand($sql);
             $tmp = $command->query(array(':CateName' => $cate_name))->readAll();
             array_push($Cates, array('cate_id' => $tmp[0]["_id"]));
         }
         foreach ($tags as $tag_name) {
             //搜索 tag 表中是否存在此次上送的 名字。 如果没有就插入记录。
             $ret = TagModel::model()->find('name=:tag_name', array(':tag_name' => $tag_name));
             if (!isset($ret)) {
                 $tagModel->name = $tag_name;
                 $tagModel->create_time = time();
                 if (!$tagModel->save()) {
                     echo "tag saved fail!";
                 }
             }
             //找到名字对应的_id。以便做插入 rel 关系表的时候用
             $sql = "select _id from tbl_tag where name = :TagName";
             $command = $connection->createCommand($sql);
             $tmp = $command->query(array(':TagName' => $tag_name))->readAll();
             array_push($Tags, array('tag_id' => $tmp[0]["_id"]));
         }
         //插入 tbl_style_category_rel 表 ( 可优化。 改成批量插入 )
         foreach ($Cates as $Cate) {
             $styleCateRelModel = new StyleCateRelModel();
             $styleCateRelModel->style_id = $styleModel->_id;
             $styleCateRelModel->category_id = $Cate['cate_id'];
             $styleModel->create_time = time();
             if (!$styleCateRelModel->save()) {
                 echo "styleCateRel saved fail!";
             }
         }
         //插入 tbl_style_tag_rel 表 ( 可优化。 改成批量插入 )
         foreach ($Tags as $Tag) {
             $styleTagRelModel = new StyleTagRelModel();
             $styleTagRelModel->style_id = $styleModel->_id;
             $styleTagRelModel->tag_id = $Tag['tag_id'];
             $styleModel->create_time = time();
             if (!$styleTagRelModel->save()) {
                 echo "styleTagRel saved fail!";
             }
         }
         $host = 'http://' . Yii::app()->request->getServerName();
         //本地调试
         $styleModel->image = $host . $styleModel->image;
         //服务器
         //$styleModel->image = $host.Yii::app()->getBaseUrl().$styleModel->image;
         echo json_encode(array('result' => 1, 'res' => array('id' => $styleModel->_id, 'user_id' => $styleModel->user_id, 'image' => $styleModel->image)));
     } else {
         echo json_encode(array('result' => 0));
     }
 }
Ejemplo n.º 7
0
 /**
  * 编辑后更新数据库数据
  */
 function update()
 {
     $category = new CategoryModel();
     if (!!($data = $category->create())) {
         if (!empty($data['id'])) {
             if (false !== $category->save()) {
                 //同时更新menu_item里面的标题
                 $menu_item = M('MenuItem');
                 $title = $data['title'];
                 $new_data = array('title' => $title);
                 $new_where = array('type_id' => array('eq', $data['id']), 'type' => 'Category');
                 $bools = $menu_item->where($new_where)->save($new_data);
                 if ($bools) {
                     $this->assign('jumpUrl', __URL__ . '/index');
                     $this->success('更新成功');
                 }
             } else {
                 $this->error('更新失败:' . $category->getDbError());
             }
         } else {
             $this->error('请选择编辑用户');
         }
     } else {
         $this->error('更新失败:( ' . $category->getError() . ' )');
     }
 }
Ejemplo n.º 8
0
 function categoryAction(NForm $form)
 {
     $values = $form->getValues();
     $langs = Setting::getLangs();
     switch ($this->mode) {
         case 'edit':
             $id_category = $values['id_category'];
             foreach ($langs as $l) {
                 $lang_val = array();
                 foreach ($values as $k => $v) {
                     if (strpos($k, self::$_separator . $l['iso'])) {
                         list($name) = explode(self::$_separator . $l['iso'], $k);
                         $lang_val[$name] = $v;
                         //pridanie linky rewrite
                         if ($name == 'link_rewrite' and $v == '') {
                             $lang_val[$name] = NStrings::webalize($values['name' . self::$_separator . $l['iso']]);
                         } elseif ($name == 'link_rewrite') {
                             $lang_val[$name] = NStrings::webalize($lang_val[$name]);
                         }
                         //			    unset($values[$k]);
                     }
                 }
                 $lang_val += array('id_parent' => $values['id_parent'], 'active' => $values['active'], 'show_on_bottom' => $values['show_on_bottom']);
                 $c = new CategoryModel($l['id_lang']);
                 $c->save($lang_val, $id_category);
             }
             $c = new CategoryModel(1);
             $c->repairSequence();
             //	print_r($values);
             CategoryModel::repairCategoryRewriteLink();
             CategoryModel::invalidateCache();
             $this->getPresenter()->flashMessage(_('Kategória bola upravená.'));
             $this->getPresenter()->redirect("this");
             break;
         case 'add':
             $id_category = CategoryModel::add(array('id_parent' => $values['id_parent'], 'active' => $values['active']));
             unset($values['id_parent'], $values['active']);
             foreach ($langs as $l) {
                 $lang_val = array();
                 foreach ($values as $k => $v) {
                     if (strpos($k, self::$_separator . $l['iso'])) {
                         list($name) = explode(self::$_separator . $l['iso'], $k);
                         $lang_val[$name] = $v;
                         //pridanie linky rewrite
                         if ($name == 'link_rewrite' and $v == '') {
                             $lang_val[$name] = NStrings::webalize($values['name' . self::$_separator . $l['iso']]);
                         }
                     }
                 }
                 $lang_val += array('id_category' => $id_category, 'id_lang' => $l['id_lang']);
                 CategoryModel::addCategoryLang($lang_val);
             }
             $c = new CategoryModel(1);
             $c->repairSequence();
             // prepisanie file_node na novy id_category
             dibi::query("UPDATE [file_node] SET id_module = %i", $id_category, "WHERE id_module = 999999 AND type_module = 'category'");
             CategoryModel::repairCategoryRewriteLink();
             CategoryModel::invalidateCache();
             //	print_r($values);
             $this->getPresenter()->flashMessage(_('Kategória bola pridaná.'));
             $this->getPresenter()->redirect("Eshop:default");
             break;
     }
 }