/** * Get a list of leads. * * @param array $args * * @return array */ public function getEntities($args = []) { return $this->getEntitiesWithCustomFields('lead', $args, function ($r) { if (!empty($this->triggerModel)) { $r->setColor($this->triggerModel->getColorForLeadPoints($r->getPoints())); } $r->setAvailableSocialFields($this->availableSocialFields); }); }
/** * Get a list of leads * * @param array $args * * @return array */ public function getEntities($args = array()) { //Get the list of custom fields $fq = $this->_em->getConnection()->createQueryBuilder(); $fq->select('f.id, f.label, f.alias, f.type, f.field_group as "group"')->from(MAUTIC_TABLE_PREFIX . 'lead_fields', 'f')->where('f.is_published = :published')->setParameter('published', true, 'boolean'); $results = $fq->execute()->fetchAll(); $fields = array(); foreach ($results as $r) { $fields[$r['alias']] = $r; } unset($results); //Fix arguments if necessary $args = $this->convertOrmProperties('Mautic\\LeadBundle\\Entity\\Lead', $args); //DBAL $dq = $this->_em->getConnection()->createQueryBuilder(); $dq->select('count(l.id) as count')->from(MAUTIC_TABLE_PREFIX . 'leads', 'l'); $this->buildWhereClause($dq, $args); //get a total count $result = $dq->execute()->fetchAll(); $total = $result[0]['count']; //now get the actual paginated results $this->buildOrderByClause($dq, $args); $this->buildLimiterClauses($dq, $args); $dq->resetQueryPart('select'); $dq->select('l.*'); $results = $dq->execute()->fetchAll(); //loop over results to put fields in something that can be assigned to the entities $fieldValues = array(); $groups = array('core', 'social', 'personal', 'professional'); foreach ($results as $result) { $leadId = $result['id']; //unset all the columns that are not fields $this->removeNonFieldColumns($result); foreach ($result as $k => $r) { if (isset($fields[$k])) { $fieldValues[$leadId][$fields[$k]['group']][$fields[$k]['alias']] = $fields[$k]; $fieldValues[$leadId][$fields[$k]['group']][$fields[$k]['alias']]['value'] = $r; } } //make sure each group key is present foreach ($groups as $g) { if (!isset($fieldValues[$leadId][$g])) { $fieldValues[$leadId][$g] = array(); } } } unset($results, $fields); //get an array of IDs for ORM query $ids = array_keys($fieldValues); if (count($ids)) { //ORM //build the order by id since the order was applied above //unfortunately, doctrine does not have a way to natively support this and can't use MySQL's FIELD function //since we have to be cross-platform; it's way ugly //We should probably totally ditch orm for leads $order = '(CASE'; foreach ($ids as $count => $id) { $order .= ' WHEN l.id = ' . $id . ' THEN ' . $count; $count++; } $order .= ' ELSE ' . $count . ' END) AS HIDDEN ORD'; //ORM - generates lead entities $q = $this->_em->createQueryBuilder(); $q->select('l, u, i,' . $order)->from('MauticLeadBundle:Lead', 'l', 'l.id')->leftJoin('l.ipAddresses', 'i')->leftJoin('l.owner', 'u'); //only pull the leads as filtered via DBAL $q->where($q->expr()->in('l.id', ':leadIds'))->setParameter('leadIds', $ids); $q->orderBy('ORD', 'ASC'); $results = $q->getQuery()->useQueryCache(false)->useResultCache(false)->getResult(); //assign fields foreach ($results as $r) { if (!empty($this->triggerModel)) { $r->setColor($this->triggerModel->getColorForLeadPoints($r->getPoints())); } $leadId = $r->getId(); $r->setFields($fieldValues[$leadId]); $r->setAvailableSocialFields($this->availableSocialFields); } } else { $results = array(); } return !empty($args['withTotalCount']) ? array('count' => $total, 'results' => $results) : $results; }