/** * @covers FireflyIII\Models\Tag::firstOrCreateEncrypted */ public function testFirstOrCreateEncryptedNew() { $tag = FactoryMuffin::create('FireflyIII\\Models\\Tag'); $search = ['tagMode' => 'something', 'tag' => 'Something else', 'user_id' => $tag->user_id]; $result = Tag::firstOrCreateEncrypted($search); $this->assertNotEquals($tag->id, $result->id); }
/** * @return bool */ public function act() { // journal has this tag maybe? $tag = Tag::firstOrCreateEncrypted(['tag' => $this->action->action_value, 'user_id' => Auth::user()->id]); $count = $this->journal->tags()->where('id', $tag->id)->count(); if ($count == 0) { $this->journal->tags()->save($tag); } return true; }
/** * @return Collection */ public function convert() { $tags = new Collection(); $strings = explode(' ', $this->value); foreach ($strings as $string) { $tag = Tag::firstOrCreateEncrypted(['tag' => $string, 'tagMode' => 'nothing', 'user_id' => Auth::user()->id]); $tags->push($tag); } $tags = $tags->merge($this->data['tags']); return $tags; }
/** * @param TransactionJournal $journal * * @return bool */ public function act(TransactionJournal $journal) : bool { // journal has this tag maybe? $tag = Tag::firstOrCreateEncrypted(['tag' => $this->action->action_value, 'user_id' => $journal->user->id]); $count = $journal->tags()->where('tag_id', $tag->id)->count(); if ($count === 0) { $journal->tags()->save($tag); Log::debug(sprintf('RuleAction AddTag. Added tag #%d ("%s") to journal %d.', $tag->id, $tag->tag, $journal->id)); return true; } Log::debug(sprintf('RuleAction AddTag fired but tag %d ("%s") was already added to journal %d.', $tag->id, $tag->tag, $journal->id)); return true; }
/** * @param TransactionJournal $journal * @param array $array * * @return bool */ private function updateTags(TransactionJournal $journal, array $array) : bool { // create tag repository /** @var TagRepositoryInterface $tagRepository */ $tagRepository = app(TagRepositoryInterface::class); // find or create all tags: $tags = []; $ids = []; foreach ($array as $name) { if (strlen(trim($name)) > 0) { $tag = Tag::firstOrCreateEncrypted(['tag' => $name, 'user_id' => $journal->user_id]); $tags[] = $tag; $ids[] = $tag->id; } } // delete all tags connected to journal not in this array: if (count($ids) > 0) { DB::table('tag_transaction_journal')->where('transaction_journal_id', $journal->id)->whereNotIn('tag_id', $ids)->delete(); } // if count is zero, delete them all: if (count($ids) == 0) { DB::table('tag_transaction_journal')->where('transaction_journal_id', $journal->id)->delete(); } // connect each tag to journal (if not yet connected): /** @var Tag $tag */ foreach ($tags as $tag) { $tagRepository->connect($journal, $tag); } return true; }