/** * Modify tags with support to remove via a prefixed minus sign * * @param Lead $lead * @param $tags * @param $removeTags * @param $persist */ public function modifyTags(Lead $lead, $tags, array $removeTags = null, $persist = true) { $leadTags = $lead->getTags(); if (!is_array($tags)) { $tags = explode(',', $tags); } array_walk($tags, create_function('&$val', '$val = trim($val); \\Mautic\\CoreBundle\\Helper\\InputHelper::clean($val);')); // See which tags already exist $foundTags = $this->getTagRepository()->getTagsByName($tags); foreach ($tags as $tag) { if (strpos($tag, '-') === 0) { // Tag to be removed $tag = substr($tag, 1); if (array_key_exists($tag, $foundTags) && $leadTags->contains($foundTags[$tag])) { $lead->removeTag($foundTags[$tag]); } } else { // Tag to be added if (!array_key_exists($tag, $foundTags)) { // New tag $newTag = new Tag(); $newTag->setTag($tag); $lead->addTag($newTag); } elseif (!$leadTags->contains($foundTags[$tag])) { $lead->addTag($foundTags[$tag]); } } } if ($removeTags !== null) { foreach ($removeTags as $tag) { // Tag to be removed if (array_key_exists($tag, $foundTags) && $leadTags->contains($foundTags[$tag])) { $lead->removeTag($foundTags[$tag]); } } } if ($persist) { $this->saveEntity($lead); } }
/** * Modify tags with support to remove via a prefixed minus sign * * @param Lead $lead * @param $tags * @param $removeTags * @param $persist */ public function modifyTags(Lead $lead, $tags, array $removeTags = null, $persist = true) { $logger = $this->factory->getLogger(); $leadTags = $lead->getTags(); if ($leadTags) { $logger->debug('LEAD: Lead currently has tags ' . implode(', ', $leadTags->getKeys())); } if (!is_array($tags)) { $tags = explode(',', $tags); } $logger->debug('CONTACT: Adding ' . implode(', ', $tags) . ' to contact ID# ' . $lead->getId()); array_walk($tags, create_function('&$val', '$val = trim($val); \\Mautic\\CoreBundle\\Helper\\InputHelper::clean($val);')); // See which tags already exist $foundTags = $this->getTagRepository()->getTagsByName($tags); foreach ($tags as $tag) { if (strpos($tag, '-') === 0) { // Tag to be removed $tag = substr($tag, 1); if (array_key_exists($tag, $foundTags) && $leadTags->contains($foundTags[$tag])) { $lead->removeTag($foundTags[$tag]); $logger->debug('LEAD: Removed ' . $tag); } } else { // Tag to be added if (!array_key_exists($tag, $foundTags)) { // New tag $newTag = new Tag(); $newTag->setTag($tag); $lead->addTag($newTag); $logger->debug('LEAD: Added ' . $tag); } elseif (!$leadTags->contains($foundTags[$tag])) { $lead->addTag($foundTags[$tag]); $logger->debug('LEAD: Added ' . $tag); } } } if (!empty($removeTags)) { $logger->debug('CONTACT: Removing ' . implode(', ', $removeTags) . ' for contact ID# ' . $lead->getId()); array_walk($removeTags, create_function('&$val', '$val = trim($val); \\Mautic\\CoreBundle\\Helper\\InputHelper::clean($val);')); // See which tags really exist $foundRemoveTags = $this->getTagRepository()->getTagsByName($removeTags); foreach ($removeTags as $tag) { // Tag to be removed if (array_key_exists($tag, $foundRemoveTags) && $leadTags->contains($foundRemoveTags[$tag])) { $lead->removeTag($foundRemoveTags[$tag]); $logger->debug('LEAD: Removed ' . $tag); } } } if ($persist) { $this->saveEntity($lead); } }
/** * {@inheritDoc} */ public function getTags() { $this->__initializer__ && $this->__initializer__->__invoke($this, 'getTags', array()); return parent::getTags(); }