/** * Get tag by id. * * @param $id * @return mixed */ public function getById($id) { $tag = $this->model->find($id); if (!is_null($tag)) { $tag->hot++; $tag->update(); } return $tag; }
public function actionInit() { $imageList = Image::find()->asArray()->all(); foreach ($imageList as $image) { $rawFilePath = $image['rawFilePath']; $match = []; if (preg_match('/\\/homeNASDownloads\\/photo_[0-9\\-]+\\/?(.*)/', dirname($rawFilePath), $match)) { $tagName = $match[1]; if (empty($tagName)) { continue; } $tag = Tag::find()->where(['tagName' => $tagName])->one(); if ($tag === null) { $tag = new Tag(); $tag->tagName = $tagName; $tag->isDelete = false; $now = date('Y-m-d H:i:s'); $tag->createTime = $now; $tag->updateTime = $now; $tag->save(); } $imageTag = ImageTag::find()->where(['imageId' => $image['id'], 'tagId' => $tag->id])->one(); if ($imageTag == null) { $imageTag = new ImageTag(); $imageTag->imageId = $image['id']; $imageTag->tagId = $tag->id; $imageTag->isDelete = false; $imageTag->createTime = $now; $imageTag->updateTime = $now; $imageTag->save(); } } } }
public static function addTags($tags_str, $id) { $tags = self::string2array($tags_str); foreach ($tags as $one) { $tag_exists = Tag::find()->where("name = '" . $one . "'")->one(); //var_dump($tag_exists); exit; if ($tag_exists == null) { $tag = new Tag(); $tag->name = $one; $tag->frequency = 1; $tag->items .= $id; $tag->save(); } else { if (array_search($id, self::string2array($tag_exists->items)) === false || array_search($id, self::string2array($tag_exists->items)) < 0) { //var_dump($id); //var_dump(self::string2array($tag_exists->items)); //var_dump(array_search($id, self::string2array($tag_exists->items)) === false); //var_dump(array_search($id, self::string2array($tag_exists->items)) < 0); exit; $tag_exists->frequency++; $tag_exists->items .= "," . $id; $tag_exists->update(); } } } }
public function delete() { $user = $this->auth(); $tag = Tag::find(Input::get('tagId')); $tag->delete(); return $this->output(new \stdClass()); }
/** * Lists all Partner models. * @return mixed */ public function actionIndex() { $searchModel = new PartnerSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); // $this->ajaxAssign('tags', Tag::find()->asArray()->publicTags()->all()); return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'tags' => [__('Public tags') => Tag::find()->publicTags()->all(), __('Personal tags') => Tag::find()->personalTags()->all()]]); }
public static function getTagWeights($limit = 20) { $models = Tag::find()->with('posts')->orderBy('name')->all(); $minFrequency = 0; $maxFrequency = 0; foreach ($models as $model) { $weight = count($model->posts); $minFrequency = $minFrequency > $weight ? $weight : $minFrequency; $maxFrequency = $maxFrequency < $weight ? $weight : $maxFrequency; } $sizeRange = self::MAX_FONT_SIZE - self::MIN_FONT_SIZE; $minCount = log($minFrequency + 1); $maxCount = log($maxFrequency + 1); if ($maxCount != $minCount) { $countRange = $maxCount - $minCount; } else { $countRange = 1; } $tags = []; foreach ($models as $model) { $tags[$model->name] = round(self::MIN_FONT_SIZE + (log(count($model->posts) + 1) - $minCount) * ($sizeRange / $countRange)); } arsort($tags); return $tags; }
/** * Creates a new Artist model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Artist(); $photos = []; if (Yii::$app->request->isPost) { for ($i = 0; $i < count(Yii::$app->request->post('Photo', [])); $i++) { $photos[] = new Photo(); } $data = Yii::$app->request->post(); $data['Artist']['user_id'] = Yii::$app->user->id; } else { $data = null; } if ($model->load($data) && $model->save()) { Photo::loadMultiple($photos, Yii::$app->request->post()); foreach ($photos as $photo) { $photo->artist_id = $model->id; } if (Photo::validateMultiple($photos)) { foreach ($photos as $photo) { $model->link('photos', $photo); } } $tags = Yii::$app->request->post('Artist', ''); $tags = Tag::find()->where(['in', 'id', explode(',', $tags['tags'])])->all(); foreach ($tags as $tag) { $model->link('tags', $tag); } return $this->redirect(['release/index', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model, 'photos' => $photos]); } }
/** * Get a tag JSON by a given tag_id * method: get * route: api/tags/<tag_id> * @param int $tag_id * @return json {"success": true or false, "data": {"tag": tag}}; */ public function show($tag_id) { $tag = Tag::find($tag_id); if ($tag == NULL) { return \Response::json(array("success" => false)); } return \Response::json(["success" => true, "data" => array("tag" => $tag)]); }
/** * Display the specified resource. * * @param int $id * @return Response */ public function show($id) { $tag = Tag::find($id); if (empty($tag)) { Flash::error('Tag not found'); return redirect(route('admin.tags.index')); } return view('admin.tags.show')->with('tag', $tag); }
/** * Display the specified resource. * * @param int $id * @return Response */ public function show($id) { // $tag = Tag::find($id); if (!$tag) { return $this->respondNotFound(); } return $this->respond(['data' => [$this->tagTransformer->transform($tag->toArray())]]); }
public function deleteChildRe($id) { $tags = $this->where('parent_id', $id)->get(); if (count($tags) > 0) { foreach ($tags as $tag) { Tag::find($tag->id)->delete(); } } }
public function delete($id) { $tag = Tag::find($id); if (!$tag) { return false; } $tag->posts()->detach(); return $tag->delete(); }
/** * Update the specified resource in storage. * * @param Request $request * @param int $id * @return Response */ public function update(Request $request, $id) { $tag = Tag::find($id); $tag->tagname = $request->get('tagname'); $updateStatus = $tag->save(); if (!$updateStatus) { abort(500, 'Some error occurred while saving user data'); } }
public function run() { $tags = Tag::find()->joinWith(['postLinks'])->groupBy('tag.id')->limit($this->count)->orderBy('tag.frequency desc')->all(); $output = ''; foreach ($tags as $tag) { /* @var $tag Tag */ $output .= Html::a('#' . $tag->tag, ['/frontend/post/search', 'tag' => $tag->tag], ['class' => 'tag']); } return $output; }
/** * Update status of the specified resource in storage. * * @param int $id * @return \Illuminate\Http\Response */ public function publish($id, $status = 'n') { if (!is_admin()) { return abort(404); } $tag = Tag::find($id); $status = $status == 'y' ? Tag::openCode : Tag::hiddenCode; TagService::updateTag($tag->id, $status); return redirect()->back()->with('success', '[' . $tag->name . ']을 공개했습니다.'); }
public function actionIndex() { $searchModel = new DescriptionSearch(); //Problemi!!! It didn't work :( // $model = Description::findBySql('SELECT * FROM `description` AS a INNER JOIN `tag` AS b ON a.`tag`=b.`tag_id` ')->all(); // $model = Description::find()->join('INNER JOIN','tag','description.tag=tag.tag_id')->all(); $model = Description::find()->all(); $tag = Tag::find()->all(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); return $this->render('index', ['model' => $model, 'tag' => $tag]); }
public function search($params) { $query = Tag::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'frequency' => $this->frequency]); $query->andFilterWhere(['like', 'name', $this->name]); return $dataProvider; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Tag::find(); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSizeLimit' => [10, 100]]]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'user_id' => $this->user_id]); $query->andFilterWhere(['like', 'name', $this->name]); return $dataProvider; }
protected function findModel($name, $with = null) { $model = Tag::find()->select(['id', 'name', 'topic_count'])->where(['name' => $name]); if (!empty($with)) { $model = $model->with($with); } $model = $model->asArray()->one(); if ($model !== null) { return $model; } else { throw new NotFoundHttpException('未找到[' . $name . ']的标签'); } }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Tag::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['tag_id' => $this->tag_id, 'flag_active' => 1, 'date_added' => $this->date_added, 'date_update' => $this->date_update]); $query->andFilterWhere(['like', 'tag_name', $this->tag_name]); return $dataProvider; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Tag::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'createTime' => $this->createTime, 'attentionNum' => $this->attentionNum]); $query->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'describe', $this->describe]); return $dataProvider; }
/** * 按照name获取tag * @param string $name * @return string * @throws NotFoundHttpException */ public function actionShow($name) { $tags = Tag::find()->select('pid')->where(['name' => $name])->asArray()->all(); $pid = array(); if (empty($tags)) { throw new NotFoundHttpException('The requested page does not exist.'); } foreach ($tags as $tag) { $pid[] = $tag['pid']; } $pid = array_filter(array_filter($pid)); $dataProvider = new ActiveDataProvider(['query' => Post::find()->where(['in', 'id', $pid])->andWhere(['in', 'status', [Post::STATUS_HIDDEN, Post::STATUS_PUBLISHED]])->orderBy(['post_time' => SORT_DESC]), 'pagination' => ['pageSize' => 10]]); /* @var Post[] $posts */ return $this->render('show', ['tag' => $name, 'dataProvider' => $dataProvider]); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Tag::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails $query->where('0=1'); return $dataProvider; } $query->joinWith(['images']); $query->groupBy(['tag.id']); $query->andFilterWhere(['tag.id' => $this->id, 'image.id' => $this->image_id, 'image.owner_id' => $this->owner_id]); $query->andFilterWhere(['like', 'tag.name', $this->name]); return $dataProvider; }
public function init() { if ($this->placeholder_from_label) { $placeholder = $this->model->getAttributeLabel($this->attribute); } else { $placeholder = $this->placeholder ?: __('Select a tags...'); } $this->options = ['placeholder' => $placeholder]; $attribute = $this->attribute; $this->attribute = $attribute . 'Str'; $models = Tag::find()->{$attribute}()->all(); $tags = []; foreach ($models as $model) { $tags[] = $model->name; } $this->pluginOptions = ['tags' => $tags, 'tokenSeparators' => [',', ';'], 'maximumInputLength' => 64, 'allowClear' => true]; parent::init(); }
public static function findTagWeights($limit = 20) { $models = Tag::find()->orderBy(['frequency' => SORT_DESC])->limit($limit)->all(); $total = 0; foreach ($models as $model) { $total += $model->frequency; } $tags = array(); if ($total > 0) { foreach ($models as $model) { if ($model->frequency > 0) { $tags[$model->name] = 8 + (int) (16 * $model->frequency / ($total + 10)); } else { $tags[$model->name] = 0; } } ksort($tags); } return $tags; }
public function beforeSave($event) { if (isset($this->owner->{$this->textFieldName})) { $tags = self::string2array($this->owner->{$this->textFieldName}); if ($tags) { foreach ($tags as $name) { /* @var $tag Tag */ $tag = Tag::find()->where(['name' => $name])->one(); if (!$tag) { $tag = new Tag(); $tag->name = $name; $tag->count = 1; $tag->save(); array_push($this->_relatedTags, $tag); } unset($tag); } } //$this->owner->{$this->_relationName} = $this->_relatedTags; } }
public function eventUpdateTags($event) { $partner = $this->owner; if ($data = Yii::$app->request->post($partner->formName())) { // FIXME foreach ($this->tagTypes as $type) { $key = $type . 'Str'; if (isset($data[$key])) { $tags = $this->parseTagsStr($data[$key]); $partner_tags = $partner->{$type}; // Remove foreach ($partner_tags as $partner_tag) { if (!in_array($partner_tag->name, $tags)) { $partner->unlink('tags', $partner_tag, true); $partner_tag->gc(); } } // Add existing $mtags = Tag::find()->where(['name' => $tags])->{$type}()->all(); foreach ($mtags as $mtag) { if (!in_array($mtag, $partner_tags)) { $partner->link('tags', $mtag); } unset($tags[array_search($mtag->name, $tags)]); } // Add new foreach ($tags as $tag) { $mtag = new Tag(); $mtag->name = $tag; if ($type == 'personalTags') { $mtag->setToPersonal(); } $mtag->save(); $partner->link('tags', $mtag); } } } } }
<?php $params = (require __DIR__ . '/params.php'); $config = ['id' => 'basic', 'basePath' => dirname(__DIR__), 'bootstrap' => ['log'], 'components' => ['request' => ['cookieValidationKey' => '9kIR8n-GpOXuU8f8aDF1pmeVvkBo7x_N'], 'cache' => ['class' => 'yii\\caching\\FileCache'], 'user' => ['identityClass' => 'app\\models\\User', 'enableAutoLogin' => true], 'errorHandler' => ['errorAction' => 'site/error'], 'mailer' => ['class' => 'yii\\swiftmailer\\Mailer', 'useFileTransport' => true], 'log' => ['flushInterval' => 1, 'targets' => [['class' => 'yii\\log\\FileTarget', 'exportInterval' => 1, 'levels' => ['error', 'warning', 'profile', 'trace', 'info'], 'logVars' => []]]], 'db' => require __DIR__ . '/db.php'], 'params' => $params, 'on beforeRequest' => function ($event) { //处理请求之前,将每个请求都会用的数据在这里处理 //比如导航中需要的数据 $tagList = \app\models\Tag::find()->all(); $tagItems = []; $count = 0; $tagListShowMax = 10; foreach ($tagList as $tag) { if ($count == $tagListShowMax) { break; } $count++; $item = ['label' => $tag->tagName, 'url' => '?r=site&tagId=' . $tag->id]; $tagItems[] = $item; } if ($count >= $tagListShowMax) { $tagItems[] = ['label' => '', 'url' => '', 'options' => ['class' => 'divider']]; $tagItems[] = ['label' => '全部标签', 'url' => '?r=tag/index']; } Yii::$app->params['tagItems'] = $tagItems; Yii::trace('on beforeRequest'); }]; if (YII_ENV_DEV) { // configuration adjustments for 'dev' environment $config['bootstrap'][] = 'debug'; $config['modules']['debug'] = ['class' => 'yii\\debug\\Module']; $config['bootstrap'][] = 'gii'; $config['modules']['gii'] = ['class' => 'yii\\gii\\Module'];
/*Html::encode()*/ // plain text, not json } ?> <?php if (!empty($model->tags)) { ?> <br>-- <br> <?php foreach ($model->tags as $tag) { ?> <?php echo Html::a("#" . $tag->name, Url::toRoute(['/', 'query' => "#" . $tag->name])); ?> <?php } ?> <?php } ?> <hr> <?php $form = ActiveForm::begin(['action' => '', 'method' => 'post']); echo Html::tag("div", $form->field($model, 'id')->hiddenInput() . $form->field($model, 'text')->hiddenInput() . $form->field($model, 'hash')->hiddenInput() . $form->field($model, 'created')->hiddenInput() . $form->field($model, 'ip')->hiddenInput() . $form->field($model, 'user_agent')->hiddenInput(), ['style' => "display: none;"]); echo $form->field($model, 'tags')->widget(Select2::className(), ['data' => ArrayHelper::map(Tag::find()->all(), 'id', 'name'), 'options' => ['multiple' => true, 'placeholder' => 'Теги', 'id' => 'moderation-tags-' . $model->id], 'pluginOptions' => ['tags' => true, 'maximumInputLength' => 255]]); echo Html::tag("div", Html::a('Редактировать', ['edit', 'id' => $model->id], ['class' => 'btn btn-warning']) . " " . Html::submitButton('Опубликовать', ['class' => 'btn btn-success']), ['style' => "text-align: center;"]); $form->end(); ?> </div> </div>
public function removeTags($tags) { if (empty($tags)) { return; } foreach ($tags as $name) { $aTag = Tag::find()->where(['name' => $name])->one(); $aTagCount = Tag::find()->where(['name' => $name])->count(); if ($aTagCount) { if ($aTagCount && $aTag->frequency <= 1) { $aTag->delete(); } else { $aTag->frequency -= 1; $aTag->save(); } } } }