public function newTweet(Request $request) { $this->validate($request, ['content' => 'required|max:140']); $newTweet = new Tweet(); $newTweet->content = $request->content; $newTweet->user_id = \Auth::user()->id; $newTweet->save(); //Process Tags $tags = explode('#', $request->tags); $tagsFormatted = []; //clean up the tags foreach ($tags as $tag) { if (trim($tag)) { $tagsFormatted[] = strtolower(trim($tag)); } } //Loop over each tag foreach ($tagsFormatted as $tag) { //Grab the first matching result OR insert this new unique tag $theTag = Tag::firstOrCreate(['name' => $tag]); $allTagIds[] = $theTag->id; } //Attacth the Tag IDs to the tweet $newTweet->tags()->attach($allTagIds); return redirect('profile'); }
public function newTweet(Request $request) { $this->validate($request, ['content' => 'required|min:2|max:140']); $newTweet = new Tweet(); // point to columns in db $newTweet->content = $request->content; // user who is logged in take their id $newTweet->user_id = \Auth::user()->id; // Save into database $newTweet->save(); // Process the tags $tags = explode('#', $request->tags); $tagsFormatted = []; // clean up tags, remove white spaces foreach ($tags as $tag) { // if after trimming something remains if (trim($tag)) { $tagsFormatted[] = strtolower(trim($tag)); } } $allTagIds = []; // Loop over eah tag foreach ($tagsFormatted as $tag) { // Grab the first matching result or insert this new unique tag // if tag is present will not insert into db. If not present, inserts into db. $theTag = Tag::firstOrCreate(['name' => $tag]); $allTagIds[] = $theTag->id; } // Attach the tag ids to the tweet $newTweet->tags()->attach($allTagIds); return redirect('profile'); }
public function updatePost(Request $request, $post_id) { $new_post = $request->only('title', 'body', 'visible'); $new_tags = $request->only('tags'); // base64 encode to avoid dealing with HTML entities $new_post['body'] = base64_encode($new_post['body']); // set visible $new_post['visible'] = is_null($new_post['visible']) ? true : false; \DB::transaction(function () use($post_id, $new_post, $new_tags) { $post = Post::with('tags')->where('id', '=', $post_id)->first(); if (!$post->tags->isEmpty()) { // remove all previous tags foreach ($post->tags as $tag) { $post->tags()->detach($tag); } } // update the old post $post->update($new_post); // add the tags if (!empty($new_tags['tags'])) { $new_tags = explode(',', $new_tags['tags']); foreach ($new_tags as $key => $tag) { $new_tags[$key] = $tag = trim($tag); if (!empty($tag)) { $post->tags()->save(Tag::firstOrCreate(['name' => $tag])); } } } $post->save(); }); return redirect(route('admin')); }
public static function boot() { parent::creating(function ($m) { $m->id = sha1(str_random(128)); }); parent::created(function ($m) { preg_match_all("/(^|[^a-zA-Z0-9])@([a-zA-Z0-9_])+/i", $m->conteudo, $matches); $locais = array(); if (!empty($matches[0])) { foreach ($matches[0] as $match) { array_push($locais, preg_replace('/[^a-zA-Z0-9_]/i', "", $match)); } } preg_match_all("/(^|[^a-zA-Z0-9])#([a-zA-Z0-9_])+/i", $m->conteudo, $matches); $tags = array(); if (!empty($matches[0])) { foreach ($matches[0] as $match) { array_push($tags, preg_replace('/[^a-zA-Z0-9_]/i', "", $match)); } } foreach ($tags as $tag) { $t = \App\Tag::firstOrCreate(['tag' => $tag]); $m->tags()->save($t); } foreach ($locais as $local) { $l = \App\Local::firstOrCreate(['local' => $local]); $m->locais()->save($l); } }); }
private function tagIDs($listaTags) { $tags = array_filter(array_map('trim', explode(",", $listaTags))); $tags_ids = []; foreach ($tags as $tag) { $tags_ids[] = Tag::firstOrCreate(['name' => $tag])->id; } return $tags_ids; }
private function getTagsIds($tags) { $tagList = array_filter(array_map('trim', explode(',', $tags))); $tagsIDs = []; foreach ($tagList as $tagName) { $tagsIDs[] = Tag::firstOrCreate(['name' => $tagName])->id; } return $tagsIDs; }
private function getTagsIds($tagsRequest) { $tags = array_filter(array_map('trim', explode(',', $tagsRequest))); $tagsIds = []; foreach ($tags as $eachTagName) { $tagsIds[] = Tag::firstOrCreate(['name' => $eachTagName])->id; } return $tagsIds; }
/** * Sync up the list of tags in the database. * * @param Article $article * @param array $tags */ private function syncTags(Question $question, array $tags) { // Create new tags if needed and get ids $tagIds = []; foreach ($tags as $tag) { $tagId = Tag::firstOrCreate(['name' => mb_strtolower($tag, 'UTF-8')])->id; $tagIds[] = $tagId; } // Sync tags based on ids $question->tags()->sync($tagIds); }
public static function createAndReturnArrayOfTagIds($string, $delimiter = ',') { $tagsArray = explode($delimiter, $string); $ids = []; foreach ($tagsArray as $tag) { $tag = trim($tag); $theTag = \App\Tag::firstOrCreate(['title' => $tag]); array_push($ids, $theTag->id); } return $ids; }
private function getTagsIds($tags) { // cria um array com as tags sem espacos no inicio ou final e elimina tags vazias $tagList = array_filter(array_map('trim', explode(',', $tags))); $tagsIDs = []; // para cada tag retorna o id dela e se não existir cria uma nova foreach ($tagList as $tagName) { $tagsIDs[] = Tag::firstOrCreate(['name' => $tagName])->id; } return $tagsIDs; }
private function getTagsIds($tags) { $tagList = array_filter(array_map("trim", explode(",", $tags))); $tagsIDs = []; foreach ($tagList as $tagName) { if ($tagName != "") { $tagsIDs[] = Tag::firstOrCreate(["name" => $tagName])->id; } } return $tagsIDs; }
/** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { $post = Post::find($id); $post->fill($request->input())->save(); $tags = tags_to_array($request->get('tags')); foreach ($tags as $tag) { $tag = Tag::firstOrCreate(['name' => $tag]); $post->tags()->detach($tag); $post->tags()->attach($tag); } return redirect(route('admin.posts')); }
/** * Verifica los tag en el arreglo ( si no existe lo crea) no esta funcionado actualmente * @param $tagsArray */ private function verifyTags($tagsArray) { $tagIds = []; foreach ($tagsArray as $tag) { $tag = trim($tag); if ($tag == '') { continue; } $fTag = Tag::firstOrCreate(['id' => $tag]); $tagIds[] = $fTag->id; } }
private function getTagsIds($tags) { // array_filter -> Limpar os espações em branco. // array_map -> Criar um array com os dados separados pela virgula. //dd($tags); $tagList = array_filter(array_map('trim', explode(',', $tags))); $tagsIDs = []; foreach ($tagList as $tagName) { $tagsIDs[] = Tag::firstOrCreate(['name' => $tagName])->id; } //dd($tagsIDs); return $tagsIDs; }
private function getTagsIds($tags) { $tagsList = array_filter(array_map('trim', explode(',', $tags))); $tagsIDs = []; /* explode = funcao em php para colocar os dados em um array ex: oi, mundo = array[0 => "oi", 1 => " mundo"] array_map + trim = funcao em php para ignorar os espacos de cada dado ex: array[0 => "oi", 1 => "mundo"] array_filter = funcao em php para igonar um dado vazio ex: oi, ,mundo = array[0 => "oi", 1 => " ", 2 => " mundo"] usando a funcao: array[0 => "oi", 1 => " mundo"] */ foreach ($tagsList as $tagName) { $tagsIDs[] = Tag::firstOrCreate(['name' => $tagName])->id; } return $tagsIDs; }
public function update($data, $slug) { $tag_ids = array(); if (isset($data['tags'])) { !is_array($data['tags']) ? $data['tags'] = explode(',', str_replace(" ", "", $data['tags'])) : null; foreach ($data['tags'] as $key => $tagname) { $tag_ids[$key] = Tag::firstOrCreate(['name' => $tagname])['id']; } } $post = Post::where('slug', $slug)->first(); $post->fill(array('title' => $data['title'], 'body' => $data['body'], 'video_id' => $data['video_id']))->save(); $post->tags()->detach(); $post->tags()->attach($tag_ids); return $this->getBySlug($post->slug); // Publish event 'post_updated': Redis will tell NodeJS, who will tell the users }
public function attach(Request $request) { if ($request->has('video')) { $video = Video::firstOrCreate(['title' => $request->input('video')]); } if ($request->has('video_id')) { $video = Video::findOrFail($request->input('video_id')); } if ($request->has('tags')) { foreach ($request->input('tags') as $tag) { $tag = Tag::firstOrCreate(['tag' => $tag]); $video->tags()->sync([$tag->id], false); } } if ($request->has('tag')) { $tag = Tag::firstOrCreate(['tag' => $request->input('tag')]); $video->tags()->sync([$tag->id], false); } }
public function newTweet(Request $request) { $this->validate($request, ['content' => 'required|max:140']); $newTweet = new Tweet(); $newTweet->content = $request->content; $newTweet->user_id = \Auth::user()->id; $newTweet->save(); $tags = explode(' ', $request->tags); $tagsFormatted = []; foreach ($tags as $tag) { //clean up newly created tags array. if (trim($tag)) { $tagsFormatted[] = strtolower(trim($tag)); } } //loop over each tag foreach ($tagsFormatted as $tag) { //loop over each formatted tag and grab the first matching result or add new tag to db $theTag = Tag::firstOrCreate(['name' => $tag]); $allTagIds[] = $theTag->id; } $newTweet->tags()->attach($allTagIds); return redirect('profile'); }
/** * Update the specified resource in storage. * * @param int $id * @return Response */ public function update(ArticleRequest $request, Guard $auth, $slug) { // find or create category $category = Category::firstOrCreate(['name' => e($request->input('category')), 'slug' => e(str_slug(pinyin($request->input('category')))), 'user_id' => $auth->user()->id]); // find or create tags foreach ($request->input('tags') as $tag) { $tagids[] = Tag::firstOrCreate(['name' => e($tag), 'slug' => e(str_slug(pinyin($tag))), 'user_id' => $auth->user()->id])->id; } // create article $article = Article::where('slug', $slug)->firstOrFail(); $article->fill($request->all()); $article->slug = empty($article->slug) ? str_slug(pinyin($article->title)) : $article->slug; $article->cover = $this->saveCover($request, $article); $article->user_id = $auth->user()->id; $article->category_id = $category->id; $article->public = $request->has('public') ? 1 : 0; $article->published_at = Carbon::now(); $article->update(); $article->tags()->sync($tagids); flash()->success(trans('flash_messages.article_update_success')); return redirect('admin/articles/hub'); }
/** * Save the tags for the Post. * * @param array $data * @param $post */ protected function saveTags(array $data, $post) { $tags = explode(',', $data['tags']); foreach ($tags as $tag) { Tag::firstOrCreate(['name' => $tag, 'slug' => str_slug($tag)]); } $post->tags()->sync($this->tags->findWhereIn('name', $tags)->pluck('id')->toArray()); }
/** * Update the event's details * * @param EventEditRequest $request The User's Request * @param [type] $id id of the event */ public function update(EventEditRequest $request, $id) { $event = Event::find($id); // Parse date and time into a carbon instance $dateTime = $request['event_date'] . " " . $request['event_time']; $carbon = Carbon::createFromFormat('d F, Y H:i', $dateTime); $request['event_time'] = $carbon; // remove all old tags before attaching new ones $event->tags()->detach($event->tags()->lists('id')->toArray()); // trim custom tags for whitespace and make array $trimmedTags = preg_replace('/\\s+/', '', $request['customTags']); $tags = explode(',', $trimmedTags); if ($tags) { foreach ($tags as $tag) { $event->tags()->attach(Tag::firstOrCreate(array('name' => $tag))); } } if ($request['tags']) { foreach ($request['tags'] as $tag) { $event->tags()->attach(Tag::find($tag)); } } if ($request->file('image')) { $imageFile = $request->file('image'); //make timestamp and append event name for filename $timestamp = str_replace([' ', ':'], '-', Carbon::now()->toDateTimeString()); $filename = $timestamp . '-' . $event->name; //remove 'image/' from MIME type $mime = "." . substr($imageFile->getMimeType(), 6); //move file to /public/images/ $filename = $timestamp . '-' . $event->name; $photoData = array('fileName' => $filename, 'mime' => $mime); $photo = Photo::create($photoData); //move uploaded file to public dir $imageFile->move(public_path() . '/images/uploads/', $filename . $mime); $event->photo()->dissociate($event->photo_id); //associate the image with the user $event->photo_id = $photo->id; $event->photo()->associate($photo); $event->save(); } $attributes = $request->only('name', 'event_time', 'event_type', 'gmaps_id', 'description'); // Update the fields shown in the form Event::find($id)->update($attributes); // Show updated event $event = Event::find($id); return view('events.show', compact('event')); }
/** * Inserts imported and parsed notes into the database * * @param Request $request * * @return Response */ public function insertImport(Request $request) { if ($request->createOutline) { $outline = new Outline(); $outline->name = $request->outlineName; $outline->description = $request->outlineDescription; $outline->save(); // $index = 0; } // Just copy the code from NoteController@postCreate foreach ($request->title as $index => $title) { $note = new Note(); $note->title = $title; $note->content = $request->content[$index]; $note->save(); if ($request->createOutline) { $outline->notes()->attach($note, ["index" => $index + 1]); } // Now fill the join table // Fix: Sometimes there are just no tags attached to a note // and in these situations, Zettlr broke. Now the existence of // the variable is previously tested before the length is checked if (isset($request->tags[$index]) && count($request->tags[$index]) > 0) { if (array_key_exists($index, $request->tags) && count($request->tags[$index]) > 0) { foreach ($request->tags[$index] as $tagname) { $tag = Tag::firstOrCreate(["name" => $tagname]); $note->tags()->attach($tag->id); } } } // $index++; } if ($request->createOutline) { return redirect('/outlines/show/' . $outline->id); } // Redirect to the main page for now return redirect('/'); }
/** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { // validate input $this->validate($request, ['name' => 'required|max:255|min:5', 'priority' => 'required']); $task = Task::findOrFail($id); $task->owner_id = Auth::user()->id; // TODO: Why doesn't $request->user() work? $task->creator_id = Auth::user()->id; $task->name = $request->get('name'); $task->priority = $request->get('priority'); // TODO: Fix/re-think these? Is checking $saved correct? what about try/catch $task->description = $request->get('description'); $task->status = 'open'; // make constant? if ($request->get('due_date')) { $task->due_date = date('Y-m-d H:i:s', strtotime($request->get('due_date'))); } $saved = $task->save(); if ($saved) { $tagsForThisTask = []; if ($request->get('tags')) { foreach ($request->get('tags') as $tag) { // TODO: change variable names? $firstOrCreate = Tag::firstOrCreate(['tag' => $tag]); $tagsForThisTask[] = $firstOrCreate->id; } } $task->tags()->sync($tagsForThisTask); // if this.. $assigneesForThisTask = []; if ($request->get('assignees')) { foreach ($request->get('assignees') as $user) { $assigneesForThisTask[] = $user; } } $task->assignees()->sync($assigneesForThisTask); // if this.. } // TODO: If saved... // TODO: http://laravel.com/docs/master/responses#redirecting-named-routes // TODO: better way to flash messages? return redirect()->action('TaskController@edit', $task)->with('message-type', 'success')->with('message-message', 'Saved!'); // TODO: Shares // TODO: Approvers // TODO: Attachments // TODO: Email notifications }
private function addPoints($userLogin, $userType, $tagName, $points, $originUserLogin, $avatarUrl = '') { if (!$userLogin) { throw new Exception('Undefined user'); } if (!$tagName) { throw new Exception('Undefined tag'); } if (!$points) { throw new Exception('Undefined points'); } $this->user = User::where('login', $userLogin)->where('type', $userType)->first(); if (!$this->user) { $this->user = User::create(array('type' => $userType, 'login' => $userLogin, 'avatar_url' => $avatarUrl)); } $this->originUser = $originUserLogin ? User::firstOrCreate(array('type' => $userType, 'login' => $originUserLogin)) : null; $this->tag = Tag::firstOrCreate(array('name' => $tagName)); $this->userTag = UserTag::firstOrCreate(array('user_id' => $this->user->id, 'tag_id' => $this->tag->id)); $this->userTag->points += $points; $this->userTag->save(); $this->userTagHistory = UserTagHistory::create(array('user_id' => $this->user->id, 'tag_id' => $this->tag->id, 'points' => $points, 'origin_user_id' => $this->originUser ? $this->originUser->id : null)); $this->getBadges(); $this->checkLevelBadge(); $this->checkRocketBadge(); $this->checkKingBadge(); $this->checkPromoterMisanthropBadges(); $this->checkSparkBadge(); $this->checkFamousBadge(); }
/** * Updates an outline record in the database * * @param Request $request * @param integer $id Outline id * * @return Response */ public function postEdit(Request $request, $id) { if (!$id || $id <= 0) { return redirect('outlines/create')->withInput(); } $validator = Validator::make($request->all(), ['name' => 'required|min:3|max:255', 'description' => 'min:3']); if ($validator->fails()) { return redirect('/outlines/edit/' . $id)->withErrors($validator)->withInput(); } // If everything passed let's edit! $outline = Outline::find($id); if (count($request->tags) > 0) { $tagIDs = []; foreach ($request->tags as $tagname) { $tag = Tag::firstOrCreate(["name" => $tagname]); $tagIDs[] = $tag->id; } // Sync tag list $outline->tags()->sync($tagIDs); } else { // Sync with empty array to remove all $outline->tags()->sync([]); } if (count($request->references) > 0) { // Same for references $referenceIDs = []; foreach ($request->references as $referenceId) { try { $ref = Reference::findOrFail($referenceId); // If this line is executed the model exists $referenceIDs[] = $ref->id; } catch (ModelNotFoundException $e) { // Do nothing } } $outline->references()->sync($referenceIDs); } else { // Sync with empty array to remove all $outline->references()->sync([]); } $outline->name = $request->name; $outline->description = $request->description; $outline->save(); if ($request->noteAction == 3) { foreach ($outline->notes as $note) { foreach ($outline->references as $reference) { if (!$note->references->contains($reference->id)) { $note->references()->attach($reference->id); } } foreach ($outline->tags as $tag) { if (!$note->tags->contains($tag->id)) { $note->tags()->attach($tag->id); } } } } elseif ($request->noteAction == 2) { $ref = []; $t = []; foreach ($outline->references as $reference) { $ref[] = $reference->id; } foreach ($outline->tags as $tag) { $t[] = $tag->id; } foreach ($outline->notes as $note) { $note->references()->sync($ref); $note->tags()->sync($t); } } return redirect(url('/outlines/show/' . $id)); }
/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $tag = Tag::firstOrCreate(['tag' => $request->input('tag')]); return redirect()->route('tags.show', [$tag->id]); }
public function addTag(Request $request) { // Validate request $this->validate($request, ['tag' => 'required|max:255|alpha']); $tag = Tag::firstOrCreate(array('name' => strtolower($request->tag))); $status = 'done'; // Add user intrest in tag if (!DB::table('tag_user')->where(['user_id' => Auth::id(), 'tag_id' => $tag->id])->count() > 0) { DB::table('tag_user')->insert(['user_id' => Auth::id(), 'tag_id' => $tag->id]); } $response = array('status' => $status, 'tag' => strtolower($tag->name)); return Response::json($response); }
/** * Updates a note * * @param Request $request * @param integer $id note id * * @return Response */ public function postEdit(Request $request, $id) { // Update a note $validator = Validator::make($request->all(), ['title' => 'required|max:255', 'content' => 'required|min:3']); if ($validator->fails()) { return redirect('/notes/edit/' . $id)->withErrors($validator)->withInput(); } // Get the note $note = Note::find($id); // First add any potential new tags to the database. // And also attach them if not done yet if (count($request->tags) > 0) { $tagIDs = []; foreach ($request->tags as $tagname) { $tag = Tag::firstOrCreate(["name" => $tagname]); $tagIDs[] = $tag->id; } // Sync tag list $note->tags()->sync($tagIDs); } else { // Sync with empty array to remove all $note->tags()->sync([]); } if (count($request->references) > 0) { // Same for references $referenceIDs = []; foreach ($request->references as $referenceId) { try { $ref = Reference::findOrFail($referenceId); // If this line is executed the model exists $referenceIDs[] = $ref->id; } catch (ModelNotFoundException $e) { // Do nothing } } $note->references()->sync($referenceIDs); } else { // Sync with empty array to remove all $note->references()->sync([]); } // Update the remaining fields $note->title = $request->title; $note->content = $request->content; $note->save(); // Now redirect to note create as the user // definitely wants to add another note. return redirect(url('/notes/show/' . $id)); }
public function insertVideosAndTags($title, $tags) { $video = Video::firstOrCreate(['title' => $title]); foreach ($tags as $tag) { $tag = Tag::firstOrCreate(['tag' => $tag]); $video->tags()->sync([$tag->id], false); } }
public function update(Request $request) { $rules = ['title' => 'required|max:100', 'summernote' => 'required', 'tags' => 'required']; $validator = Validator::make($request->input(), $rules); if ($validator->passes()) { //$resolved_content = Parsedown::instance()->text($request->input('content')); $article = Article::find($request->input('article-id')); Article::where('id', $request->input('article-id'))->update(['title' => $request->input('title'), 'article_uid' => Auth::id(), 'content' => $request->input('summernote'), 'comment_permition' => $request->has('comment_permition') ? 0 : 1, 'is_public' => $request->has('is_public') ? 0 : 1, 'reproduct_permition' => $request->has('reproduct_permition') ? 0 : 1, 'type' => $request->has('article-type') ? $request->input('article-type') : 0]); $tag = Tag::where('tag_uid', Auth::id())->where('name', $request->input('tags'))->first(); if (!isset($tag)) { $tag = Tag::firstOrCreate(['tag_uid' => Auth::id(), 'name' => $request->input('tags')]); $article->tags()->attach($tag->id); } return redirect()->action('SelfMainpageController@index'); } else { return redirect('/edit')->withInput()->withErrors($validator); } }