/** * Creates a new Tag model. * For ajax request will return json object * and for non-ajax request if creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $request = Yii::$app->request; $model = new Tag(); if ($request->isAjax) { /* * Process for ajax request */ Yii::$app->response->format = Response::FORMAT_JSON; if ($request->isGet) { return ['code' => '200', 'message' => 'OK', 'data' => $this->renderPartial('create', ['model' => $model])]; } else { if ($model->load($request->post()) && $model->save()) { return ['code' => '200', 'message' => 'Create Tag success']; } else { return ['code' => '400', 'message' => 'Validate error', 'data' => $this->renderPartial('create', ['model' => $model])]; } } } else { /* * Process for non-ajax request */ if ($model->load($request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model]); } } }
/** * 将文章插入数据库 * @param $title * @param $content * @param $publish_at * @param $tag * @return bool */ public static function insert($title, $content, $publish_at, $tag = '') { //插入标签(搜索的分类) $article = new Article(); $article->title = $title; $article->content = $content; $article->author = 'yang'; $article->status = Article::STATUS_GATHER; $article->publish_at = $publish_at; $res = $article->save(false); if ($tag) { try { $tagModel = Tag::find()->where(['name' => $tag])->one(); if (!$tagModel) { $tagModel = new Tag(); $tagModel->name = $tag; $tagModel->article_count = 0; $tagModel->save(false); } $articleTag = new ArticleTag(); $articleTag->article_id = $article->id; $articleTag->tag_id = $tagModel->id; $articleTag->save(false); } catch (\Exception $e) { echo $e->getMessage() . PHP_EOL; } } return $res ? true : false; }
/** * Creates a new Tag model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Tag(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model]); } }
public static function addTags($tags) { self::updateAllCounters(['frequency' => 1], ['in', 'name', $tags]); foreach ($tags as $name) { if (!self::find()->where(['name' => $name])->exists()) { $tag = new Tag(); $tag->name = $name; $tag->frequency = 1; $tag->save(); } } }
/** * Creates a new Meta model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Tag(); if (Yii::$app->request->isPost) { if ($model->load(Yii::$app->request->post())) { if ($model->save()) { return $this->redirect(['index']); } } } return $this->render('create', ['model' => $model]); }
public function actionAdd() { $model = new Tag(); if ($model->load(Yii::$app->request->post())) { if ($model->save()) { Yii::$app->session->setFlash('success', '成功添加标签“' . $model->name . '”。'); return $this->redirect(['index']); } else { Yii::$app->session->setFlash('danger', '标签添加失败。'); } } return $this->render('form', ['model' => $model]); }
public function saveGroup() { $cleanUrl = preg_replace("/[^a-zA-Z0-9\\/_|+ -]/", '', $this->name); $cleanUrl = strtolower(trim($cleanUrl, '-')); $cleanUrl = preg_replace("/[\\/_|+ -]+/", '-', $cleanUrl); $this->url = $cleanUrl; $urlExists = Group::findOne(['url' => $cleanUrl]); if ($urlExists) { if ($this->isNewRecord) { $lastGroup = Group::find()->orderBy(['id' => SORT_DESC])->one(); $this->url = $lastGroup->id++ . '-' . $this->url; } else { if ($urlExists->id !== $this->id) { $this->url = $this->id . '-' . $this->url; } } } if (!$this->save()) { return false; } $tags = explode(',', str_replace(['[', ']', '\''], ['', '', ''], $this->tag)); foreach ($tags as $tag) { $newTag = Tag::findOne(['name' => strtolower($tag)]); if (!$newTag) { $newTag = new Tag(); $newTag->name = strtolower($tag); if ($newTag->save()) { $tagToGroup = new TagToGroup(); $tagToGroup->tag_id = $newTag->id; $tagToGroup->group_id = $this->id; $tagToGroup->save(); } } else { $tagToGroup = new TagToGroup(); $tagToGroup->tag_id = $newTag->id; $tagToGroup->group_id = $this->id; $tagToGroup->save(); } } $userToGroup = new UserToGroup(); $userToGroup->user_id = Yii::$app->user->getIdentity()->id; $userToGroup->group_id = $this->id; $userToGroup->can_edit = 1; $userToGroup->group_admin = 1; if ($userToGroup->save()) { return true; } return false; }
/** * Creates a new Tag model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Tag(); try { if ($model->load($_POST) && $model->save()) { return $this->redirect(Url::previous()); } elseif (!\Yii::$app->request->isPost) { $model->load($_GET); } } catch (\Exception $e) { $msg = isset($e->errorInfo[2]) ? $e->errorInfo[2] : $e->getMessage(); $model->addError('_exception', $msg); } return $this->render('create', ['model' => $model]); }
public static function addTags($tags) { foreach ($tags as $name) { $aTag = Tag::find()->where(['name' => $name])->one(); $aTagCount = Tag::find()->where(['name' => $name])->count(); if (!$aTagCount) { $tag = new Tag(); $tag->name = $name; $tag->frequency = 1; $tag->save(); } else { $aTag->frequency += 1; $aTag->save(); } } }
public function saveManyTags($vidmageTags) { var_dump($vidmageTags); foreach ($vidmageTags as $vidmageTagId) { if (!is_numeric($vidmageTagId)) { $tag = new Tag(); $tag->name = $vidmageTagId; $tag->save(); $vidmageTagId = $tag->id; } $vidmageTag = new VidmageTag(); $vidmageTag->vidmage_id = $this->id; $vidmageTag->tag_id = $vidmageTagId; $vidmageTag->save(); } }
/** * @param mixed $id Tag id OR new tag name * * @return boolean */ public function addTag($id) { if (is_numeric($id)) { $tag = Tag::find()->where(['id' => $id])->one(); } elseif (strpos($id, '{new}') !== false) { $name = mb_substr($id, 5, mb_strlen($id, 'UTF-8') - 5, 'UTF-8'); $tag = new Tag(); $tag->name = $name; if (!$tag->save()) { return false; } } if (!empty($tag)) { $tagging = new Tagging(); $tagging->taggable_id = $this->id; $tagging->taggable_type = Tagging::TAGGABLE_VIDEO; $tagging->tag_id = $tag->id; return $tagging->save(); } return false; }
/** * 新建文章 * @return bool * @throws Exception */ public function saveArticle() { if (!$this->validate()) { return false; } if (!$this->tag) { return $this->save(); } $res = $this->save(); try { $tags = explode(';', $this->tag); foreach ($tags as $v) { if (!$v) { continue; } $tag = Tag::findOne(['name' => $v]); if (!$tag) { $tag = new Tag(); $tag->name = $v; $tag->article_count = 0; $res = $tag->save(); $id = $tag->id; } else { $id = $tag->id; } $article_tag = new ArticleTag(); $article_tag->article_id = $this->id; $article_tag->tag_id = $id; $article_tag->save(); //更前标签的文章数量 $tag->article_count++; $tag->save(false); } } catch (Exception $e) { throw new Exception($e->getMessage()); } return $res; }