/**
  * @param Request $request
  *
  * @return \Symfony\Component\HttpFoundation\JsonResponse
  */
 protected function addLeadTagsAction(Request $request)
 {
     $tags = $request->request->get('tags');
     $tags = json_decode($tags, true);
     if (is_array($tags)) {
         $newTags = array();
         foreach ($tags as $tag) {
             if (!is_numeric($tag)) {
                 // New tag
                 $tagEntity = new Tag();
                 $tagEntity->setTag(InputHelper::clean($tag));
                 $newTags[] = $tagEntity;
             }
         }
         $leadModel = $this->factory->getModel('lead');
         if (!empty($newTags)) {
             $leadModel->getTagRepository()->saveEntities($newTags);
         }
         // Get an updated list of tags
         $allTags = $leadModel->getTagRepository()->getSimpleList(null, array(), 'tag');
         $tagOptions = '';
         foreach ($allTags as $tag) {
             $selected = in_array($tag['value'], $tags) || in_array($tag['label'], $tags) ? ' selected="selected"' : '';
             $tagOptions .= '<option' . $selected . ' value="' . $tag['value'] . '">' . $tag['label'] . '</option>';
         }
         $data = array('success' => 1, 'tags' => $tagOptions);
     } else {
         $data = array('success' => 0);
     }
     return $this->sendJsonResponse($data);
 }
Exemple #2
0
 /**
  * @param Page    $page
  * @param Request $request
  * @param string  $code
  *
  * @return void
  */
 public function hitPage($page, $request, $code = '200')
 {
     //don't skew results with in-house hits
     if (!$this->factory->getSecurity()->isAnonymous()) {
         return;
     }
     $hitRepo = $this->getHitRepository();
     $hit = new Hit();
     $hit->setDateHit(new \Datetime());
     //check for existing IP
     $ipAddress = $this->factory->getIpAddress();
     $hit->setIpAddress($ipAddress);
     /** @var \Mautic\LeadBundle\Model\LeadModel $leadModel */
     $leadModel = $this->factory->getModel('lead');
     //check for any clickthrough info
     $clickthrough = $request->get('ct', array());
     if (!empty($clickthrough)) {
         $clickthrough = $this->decodeArrayFromUrl($clickthrough);
         if (!empty($clickthrough['lead'])) {
             $lead = $leadModel->getEntity($clickthrough['lead']);
             if ($lead !== null) {
                 $leadModel->setLeadCookie($clickthrough['lead']);
                 $leadClickthrough = true;
                 $leadModel->setCurrentLead($lead);
             }
         }
         if (!empty($clickthrough['source'])) {
             $hit->setSource($clickthrough['source'][0]);
             $hit->setSourceId($clickthrough['source'][1]);
         }
         if (!empty($clickthrough['email'])) {
             $hit->setEmail($this->em->getReference('MauticEmailBundle:Email', $clickthrough['email']));
         }
     }
     if (empty($leadClickthrough)) {
         $lead = $leadModel->getCurrentLead();
     }
     if ($page instanceof Redirect) {
         //use the configured redirect URL
         $pageURL = $page->getUrl();
     } else {
         //use current URL
         // Tracking pixel is used
         if (strpos($request->server->get('REQUEST_URI'), '/mtracking.gif') !== false) {
             $pageURL = $request->server->get('HTTP_REFERER');
             // if additional data were sent with the tracking pixel
             if ($request->server->get('QUERY_STRING')) {
                 parse_str($request->server->get('QUERY_STRING'), $query);
                 // URL attr 'd' is encoded so let's decode it first.
                 $decoded = false;
                 if (isset($query['d'])) {
                     // parse_str auto urldecodes
                     $query = unserialize(base64_decode($query['d']));
                     $decoded = true;
                 }
                 if (!empty($query)) {
                     if (isset($query['page_url'])) {
                         $pageURL = $query['page_url'];
                         if (!$decoded) {
                             $pageURL = urldecode($pageURL);
                         }
                     } elseif (isset($query['url'])) {
                         $pageURL = $query['url'];
                         if (!$decoded) {
                             $pageURL = urldecode($pageURL);
                         }
                     }
                     if (isset($query['page_referrer'])) {
                         if (!$decoded) {
                             $query['page_referrer'] = urldecode($query['page_referrer']);
                         }
                         $hit->setReferer($query['page_referrer']);
                     } elseif (isset($query['referrer'])) {
                         if (!$decoded) {
                             $query['referrer'] = urldecode($query['referrer']);
                         }
                         $hit->setReferer($query['referrer']);
                     }
                     if (isset($query['page_language'])) {
                         if (!$decoded) {
                             $query['page_language'] = urldecode($query['page_language']);
                         }
                         $hit->setPageLanguage($query['page_language']);
                     } elseif (isset($query['language'])) {
                         if (!$decoded) {
                             $query['language'] = urldecode($query['language']);
                         }
                         $hit->setPageLanguage($query['language']);
                     }
                     if (isset($query['page_title'])) {
                         if (!$decoded) {
                             $query['page_title'] = urldecode($query['page_title']);
                         }
                         $hit->setUrlTitle($query['page_title']);
                     } elseif (isset($query['title'])) {
                         if (!$decoded) {
                             $query['title'] = urldecode($query['title']);
                         }
                         $hit->setUrlTitle($query['title']);
                     }
                     // Update lead fields if some data were sent in the URL query
                     /** @var \Mautic\LeadBundle\Model\FieldModel $leadFieldModel */
                     $leadFieldModel = $this->factory->getModel('lead.field');
                     $availableLeadFields = $leadFieldModel->getFieldList(false, false, array('isPublished' => true, 'isPubliclyUpdatable' => true));
                     $uniqueLeadFields = $this->factory->getModel('lead.field')->getUniqueIdentiferFields();
                     $uniqueLeadFieldData = array();
                     $inQuery = array_intersect_key($query, $availableLeadFields);
                     foreach ($inQuery as $k => $v) {
                         if (empty($query[$k])) {
                             unset($inQuery[$k]);
                         }
                         if (array_key_exists($k, $uniqueLeadFields)) {
                             $uniqueLeadFieldData[$k] = $v;
                         }
                     }
                     $persistLead = false;
                     if (count($inQuery)) {
                         if (count($uniqueLeadFieldData)) {
                             $existingLeads = $this->em->getRepository('MauticLeadBundle:Lead')->getLeadsByUniqueFields($uniqueLeadFieldData, $lead->getId());
                             if (!empty($existingLeads)) {
                                 $lead = $leadModel->mergeLeads($lead, $existingLeads[0]);
                             }
                             $leadIpAddresses = $lead->getIpAddresses();
                             if (!$leadIpAddresses->contains($ipAddress)) {
                                 $lead->addIpAddress($ipAddress);
                             }
                             $leadModel->setCurrentLead($lead);
                         }
                         $leadModel->setFieldValues($lead, $inQuery);
                         $persistLead = true;
                     }
                     if (isset($query['tags'])) {
                         if (!$decoded) {
                             $query['tags'] = urldecode($query['tags']);
                         }
                         $leadTags = $lead->getTags();
                         $tags = explode(',', $query['tags']);
                         array_walk($tags, create_function('&$val', '$val = trim($val); \\Mautic\\CoreBundle\\Helper\\InputHelper::clean($val);'));
                         // See which tags already exist
                         $foundTags = $leadModel->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]);
                                     $persistLead = true;
                                 }
                             } else {
                                 // Tag to be added
                                 if (!array_key_exists($tag, $foundTags)) {
                                     // New tag
                                     $newTag = new Tag();
                                     $newTag->setTag($tag);
                                     $lead->addTag($newTag);
                                     $persistLead = true;
                                 } elseif (!$leadTags->contains($foundTags[$tag])) {
                                     $lead->addTag($foundTags[$tag]);
                                     $persistLead = true;
                                 }
                             }
                         }
                     }
                     if ($persistLead) {
                         $leadModel->saveEntity($lead);
                     }
                 }
             }
         } else {
             $pageURL = 'http';
             if ($request->server->get('HTTPS') == 'on') {
                 $pageURL .= 's';
             }
             $pageURL .= '://';
             if ($request->server->get('SERVER_PORT') != '80') {
                 $pageURL .= $request->server->get('SERVER_NAME') . ':' . $request->server->get('SERVER_PORT') . $request->server->get('REQUEST_URI');
             } else {
                 $pageURL .= $request->server->get('SERVER_NAME') . $request->server->get('REQUEST_URI');
             }
         }
     }
     $hit->setUrl($pageURL);
     // Store query array
     $query = $request->query->all();
     unset($query['d']);
     $hit->setQuery($query);
     list($trackingId, $generated) = $leadModel->getTrackingCookie();
     $hit->setTrackingId($trackingId);
     $hit->setLead($lead);
     if (!$generated) {
         $lastHit = $request->cookies->get('mautic_referer_id');
         if (!empty($lastHit)) {
             //this is not a new session so update the last hit if applicable with the date/time the user left
             $this->getHitRepository()->updateHitDateLeft($lastHit);
         }
     }
     if (!empty($page)) {
         //check for a hit from tracking id
         $countById = $hitRepo->getHitCountForTrackingId($page, $trackingId);
         $isUnique = empty($countById);
         if ($page instanceof Page) {
             $hit->setPage($page);
             $hit->setPageLanguage($page->getLanguage());
             $isVariant = $isUnique ? $page->getVariantStartDate() : false;
             try {
                 $this->getRepository()->upHitCount($page->getId(), 1, $isUnique, !empty($isVariant));
             } catch (\Exception $exception) {
                 error_log($exception);
             }
         } elseif ($page instanceof Redirect) {
             $hit->setRedirect($page);
             /** @var \Mautic\PageBundle\Model\RedirectModel $redirectModel */
             $redirectModel = $this->factory->getModel('page.redirect');
             try {
                 $redirectModel->getRepository()->upHitCount($page->getId(), 1, $isUnique);
             } catch (\Exception $exception) {
                 error_log($exception);
             }
         }
     }
     //glean info from the IP address
     if ($details = $ipAddress->getIpDetails()) {
         $hit->setCountry($details['country']);
         $hit->setRegion($details['region']);
         $hit->setCity($details['city']);
         $hit->setIsp($details['isp']);
         $hit->setOrganization($details['organization']);
     }
     $hit->setCode($code);
     if (!$hit->getReferer()) {
         $hit->setReferer($request->server->get('HTTP_REFERER'));
     }
     $hit->setUserAgent($request->server->get('HTTP_USER_AGENT'));
     $hit->setRemoteHost($request->server->get('REMOTE_HOST'));
     //get a list of the languages the user prefers
     $browserLanguages = $request->server->get('HTTP_ACCEPT_LANGUAGE');
     if (!empty($browserLanguages)) {
         $languages = explode(',', $browserLanguages);
         foreach ($languages as $k => $l) {
             if ($pos = strpos(';q=', $l) !== false) {
                 //remove weights
                 $languages[$k] = substr($l, 0, $pos);
             }
         }
         $hit->setBrowserLanguages($languages);
     }
     $this->em->persist($hit);
     // Wrap in a try/catch to prevent deadlock errors on busy servers
     try {
         $this->em->flush();
     } catch (\Exception $e) {
         error_log($e);
     }
     if ($this->dispatcher->hasListeners(PageEvents::PAGE_ON_HIT)) {
         $event = new PageHitEvent($hit, $request, $code, $clickthrough);
         $this->dispatcher->dispatch(PageEvents::PAGE_ON_HIT, $event);
     }
     //save hit to the cookie to use to update the exit time
     $this->factory->getHelper('cookie')->setCookie('mautic_referer_id', $hit->getId());
 }
Exemple #3
0
 /**
  * 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);
     }
 }
Exemple #4
0
 /**
  * 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);
     }
 }
Exemple #5
0
 /**
  * Remove tag.
  *
  * @param Tag $tag
  */
 public function removeTag(Tag $tag)
 {
     $this->isChanged('tags', $tag->getTag());
     $this->tags->removeElement($tag);
 }
 /**
  * {@inheritDoc}
  */
 public function setTag($tag)
 {
     $this->__initializer__ && $this->__initializer__->__invoke($this, 'setTag', array($tag));
     return parent::setTag($tag);
 }