public function testNormalizeTags() { $tags = array('test', '#test', '#test,', '##test,'); $normalizedTags = array('#test', '#test', '#test', '##test'); $normalizedTagsSuppressedHash = array('test', 'test', 'test', '#test'); $this->assertEquals($normalizedTags, Tags::normalizeTags($tags, false)); $this->assertEquals($normalizedTagsSuppressedHash, Tags::normalizeTags($tags, true)); }
public function testHasTag() { $contact = $this->contacts('testAnyone'); $contact->clearTags(); $tags = array('test', 'test2', 'test3'); foreach ($tags as $tag) { $this->assertFalse($contact->hasTag($tag, null, true)); } $contact->addTags($tags); $contactTags = $contact->getTags(true); $this->assertEquals(Tags::normalizeTags($tags), ArrayUtil::sort($contactTags)); foreach ($tags as $tag) { $this->assertTrue($contact->hasTag($tag, null, true)); } }
/** * Removes the specified tag(s) from the owner model * @param mixed $tags a string or array of strings containing tags * @return boolean whether or not at least one tag was deleted successfully */ public function removeTags($tags) { $result = false; $removedTags = array(); $tags = Tags::normalizeTags((array) $tags); foreach ((array) $tags as $tag) { if (empty($tag)) { continue; } $attributes = array('type' => get_class($this->getOwner()), 'itemId' => $this->getOwner()->id, 'tag' => $tag); if ($this->hasTag($tag) && CActiveRecord::model('Tags')->deleteAllByAttributes($attributes) > 0) { if (false !== ($offset = array_search($tag, $this->_tags))) { unset($this->_tags[$offset]); } // update tag cache $removedTags[] = $tag; $result = true; } } if ($this->flowTriggersEnabled) { X2Flow::trigger('RecordTagRemoveTrigger', array('model' => $this->getOwner(), 'tags' => $removedTags)); } return $result; }