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 setTags($entryId, $tags) { if (!$tags) { return; } // Write new tags to Tag table try { foreach ($tags as $k => $t) { // Add new tag if id is not exist if (!isset($t['id'])) { $tag = new Tag(); $tag->text = $t['text']; $tag->save(); $tags[$k]['id'] = $tag->id; } } } catch (Exception $e) { return $e->getMessage(); } // Update tags for current entry //TODO: rewrite with junction table relations try { EntryTag::deleteAll(['entry_id' => (int) $entryId]); foreach ($tags as $t) { $e = new EntryTag(); $e->tag_id = $t['id']; $e->entry_id = $entryId; $e->save(); } } catch (Exception $e) { return $e->getMessage(); } }
public function store(Request $request) { $tag = new Tag(); $tag->fill($request->all()); $tag->save(); return $tag; }
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(); } } } }
/** * Страница добавления метки. * * @return string|\yii\web\Response */ public function actionCreate() { $modelTag = new Tag(); if ($modelTag->load(Yii::$app->request->post()) && $modelTag->save()) { return $this->redirect(['back-tag/view', 'id' => $modelTag->id]); } return $this->render('/back/tag/create', ['modelTag' => $modelTag]); }
/** * Run the database seeds. * * @return void */ public function run() { // Seeds the tag table foreach (range(1, 1000) as $t) { $tag = new Tag(); $tag->tag_name = str_shuffle('abcdefghijklmnopqrstuvwxyz'); $tag->save(); } }
/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $tag = new Tag(); foreach (array_keys($this->fields) as $field) { $tag->{$field} = $request->get($field); } $tag->save(); return redirect('/admin/tag')->withSuccess("The tag '{$tag->tag}' was created."); }
/** * Store a newly created resource in storage. * * @param Request $request * @return Response */ public function store(Request $request) { $tag = new Tag(); $tag->tagname = $request->get('tagname'); $saveStatus = $tag->save(); if (!$saveStatus) { abort(500, 'Some error occurred while saving tag data'); } }
/** * 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->tag_id]); } else { return $this->render('create', ['model' => $model]); } }
public function createItem($item) { $tag = new Tag(); $tag->name = $item['name']; $tag->parent_id = $item['parent_id']; $tag->created = date('Y-m-d H:i:s'); $tag->updated = date('Y-m-d H:i:s'); $tag->save(); }
public function saved($model) { foreach ($model->tags as $tag) { $new_tag = Tag::where('type', '=', $tag->type)->where('tag', '=', $tag->tag)->first(); if (!$new_tag) { $new_tag = new Tag(['type' => $tag->type, 'tag' => $tag->tag]); $new_tag->save(); } } }
/** * Create tag * * @param Request $request * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View */ public function create(Request $request) { if ($request->isMethod('post')) { $rules = ['name' => 'required|unique:tags,name']; Validator::make($request->all(), $rules)->validate(); $tag = new Tag(); $tag->name = $request->input('name'); $tag->save(); return redirect()->route('tags'); } else { return view('admin.tag.create'); } }
public function addTags($tags = []) { $rowTagsName = Tag::all(['name'])->toArray(); $rowTagsName = array_flatten($rowTagsName); foreach ($tags as $key => $tag) { if (in_array($tag, $rowTagsName)) { } else { $tag = new Tag(); $tag->name = $tag; $tag->save(); } } }
/** * 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())) { if ($result = $model->save()) { $model->setActive(); return $this->message('标签创建成功', 'success', ['view', 'id' => $model->id]); } return $this->message(array_values($model->getFirstErrors())[0], 'error'); } else { // return $this->render('create', [ // 'model' => $model, // ]); } }
public 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(); } } }
/** * Store a newly created resource in storage. * * @return Response */ public function store(Request $request) { $model = new Tag(); $data = $request->all(); $rules = $this->rules(); $v = Validator::make($data, $rules); if ($v->fails()) { $output = ['error' => true, 'message' => 'Input invalidate!', 'errors' => $v->errors()]; } else { $model->fill($data); if ($model->save()) { $output = ['error' => false, 'message' => 'Created Success!', 'data' => $model->toArray()]; } else { $output = ['error' => true, 'message' => 'Save Failed!']; } } return response()->json($output, 200); }
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); } } } } }
public static function addTags($tags, $classify_type) { if (!empty($tags)) { foreach ($tags as $name) { if (!empty($name)) { $count = Tag::find()->where(['name' => $name, 'classify_type' => $classify_type])->one(); $tag = new Tag(); if (isset($count) && !empty($count)) { $count->frequency += 1; $count->save(); } else { $tag->name = $tags; $tag->frequency = 1; $tag->classify_type = $classify_type; $tag->save(); } } } } }
/** * @param int $antrag_id * * @return string */ public function actionSaveantrag($antrag_id) { \yii::$app->response->format = \yii\web\Response::FORMAT_RAW; \yii::$app->response->headers->add('Content-Type', 'application/json'); /** @var Antrag $antrag */ $antrag = Antrag::findOne($antrag_id); if (!$antrag) { return json_encode(['error' => 'Antrag nicht gefunden.']); } $antrag->notiz = $_POST['antrag']['notiz']; if ($_POST['antrag']['abgeschlossen'] == 1) { $antrag->status_override = $antrag->status == 'erledigt' ? '' : 'erledigt'; } else { $antrag->status_override = $antrag->status == 'erledigt' ? 'In Bearbeitung' : ''; } if (!$antrag->save()) { return json_encode(['error' => 'Es ist ein (seltsamer) Fehler beim Speichern aufgetreten.']); } foreach ($antrag->tags as $tag) { $antrag->unlink('tags', $tag, true); } $tags = explode(',', $_POST['antrag']['tags']); foreach ($tags as $tagName) { $tag = Tag::findOne(['name' => $tagName]); if (!$tag) { $tag = new Tag(); $tag->name = $tagName; $tag->save(); } $antrag->link('tags', $tag); } $row = $this->renderPartial('index_antrag_row', ['antrag' => $antrag]); return json_encode(['success' => 1, 'content' => $row]); }
/** * Updates an existing Post model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @return mixed */ public function actionUpdate($id) { $model = $this->findModel($id); $model->updated_at = date("Y-m-d h:i:s"); $tag = new Tag(); $tmp = array(); $tags = array(); if (isset($_POST['Tag'])) { $pieces = explode(",", Yii::$app->request->post()['Tag']['name']); $dataProvider = Post_tags::getTags($id); $count = $dataProvider->getModels(); for ($i = 0; $i < count($count); $i++) { for ($j = 0; $j < count($pieces); $j++) { if ($dataProvider->getModels()[$i]['name'] == $pieces[$j]) { array_push($tmp, $pieces[$j]); $pieces[$j] = ""; } } } $dataProvider = Tag::getAllTags(); $count = $dataProvider->getModels(); for ($i = 0; $i < count($count); $i++) { for ($j = 0; $j < count($pieces); $j++) { if ($dataProvider->getModels()[$i]['name'] == $pieces[$j]) { array_push($tmp, $pieces[$j]); $pieces[$j] = ""; } } } $ids = "'" . implode("','", $tmp) . "'"; $dataProvider2 = Tag::getTagsIn($ids); for ($i = 0; $i < count($pieces); $i++) { $tag = new Tag(); $tag->name = $pieces[$i]; $tag->save(); array_push($tags, $tag->tag_id); } for ($i = 0; $i < count($dataProvider2->getModels()); $i++) { array_push($tags, $dataProvider2->getModels()[$i]['tag_id']); } } $postTags = Post_tags::getPostTags($id); for ($i = 0; $i < count($postTags->getModels()); $i++) { for ($j = 0; $j < count($tags); $j++) { if ($postTags->getModels()[$i]['tag_id'] == $tags[$j]) { $tags[$j] = ""; } } } if ($model->load(Yii::$app->request->post()) && $model->save()) { for ($i = 0; $i < count($tags); $i++) { $postTags = new Post_tags(); $postTags->tag_id = $tags[$i]; $postTags->post_id = $model->post_id; $postTags->save(); } return $this->redirect(['view', 'id' => $model->post_id]); } else { return $this->render('update', ['model' => $model, 'model' => $model, 'tag' => $tag]); } }
/** * Handle the form for adding/editing a picture */ public function postEdit(Request $request, $id = null) { // Are we editing an existing image or creating one ? $newPicture = $id === null; $picture = $newPicture ? new Picture() : Picture::findOrFail($id); // Validate the request, if invalid this will return to the form wih errors $this->validate($request, ['title' => 'required', 'description' => 'required', 'picture' => $newPicture ? 'required|image' : 'image']); // If the file has been uploaded in the form if ($request->hasFile('picture')) { // The uploaded picture file $pictureFile = $request->file('picture'); // The filename of the picture $pictureFilename = $pictureFile->getClientOriginalName(); // If the file already exists, prepend a random string in the filename if (file_exists(public_path('uploads/' . $pictureFilename))) { $pictureFilename = str_random(5) . $pictureFilename; } // Move the temp file to the uploads directory $pictureFile->move(public_path('uploads'), $pictureFilename); // Set the filename in the model $picture->filename = $pictureFilename; } // Setting the title and description $picture->title = $request->input('title'); $picture->description = $request->input('description'); // Save the model in the db $picture->save(); // Handles tags, this must be done after creating the picture in the db, // because we need the picture's id // Remove whitespace and make the string lowercase $tagsRaw = strtolower(trim($request->input('tags'))); // If the tags are empty, that means the user is trying to remove all tags if (empty($tagsRaw)) { // Remove all tags $picture->tags()->sync([]); } else { // Explode the string on spaces $tags = explode(' ', $tagsRaw); // Will be filled with tags id $tagIds = []; foreach ($tags as $tagName) { // Try to get the tag in the db $tag = Tag::where('name', $tagName)->first(); // If the tag doesn't exist, create it if ($tag == null) { // Create a new model $tag = new Tag(); $tag->name = $tagName; // Insert the model in the db $tag->save(); } // Keep the tag id $tagIds[] = $tag->id; } // Update the relationship with the tags' ids if (count($tagIds) > 0) { $picture->tags()->sync($tagIds); } } // Return to the image return redirect('images/show/' . $picture->id); }
private function updateTags($ticket) { TagTicket::where('ticket_id', $ticket->id)->forceDelete(); if (Input::get('tagit')) { $tags = explode(",", Input::get('tagit')); foreach ($tags as $new_tag) { $tag = Tag::where('name', $new_tag)->first(); if (!isset($tag->id)) { $tag = new Tag(); $tag->name = $new_tag; $tag->save(); } $tag_ticket = new TagTicket(); $tag_ticket->ticket_id = $ticket->id; $tag_ticket->tag_id = $tag->id; $tag_ticket->save(); } } }
/** * Return a fresh instance of the model (called on `create()`). * * @return \Illuminate\Database\Eloquent\Model */ protected function make() { $model = new Tag(); $model->save(); return $model; }
public function addTags($tags) { self::updateAllCounters(['frequency' => 1], ['in', 'name', $tags]); foreach ($tags as $name) { if (!self::findOne(['name' => $name])) { $tag = new Tag(); $tag->name = $name; $tag->frequency = 1; $tag->save(); } } }