Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add company fields to campaign condition #5628

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion app/bundles/LeadBundle/Config/config.php
Expand Up @@ -684,7 +684,9 @@
],
'mautic.form.type.lead_fields' => [
'class' => 'Mautic\LeadBundle\Form\Type\LeadFieldsType',
'arguments' => ['mautic.factory'],
'arguments' => [
'mautic.lead.model.field',
],
'alias' => 'leadfields_choices',
],
'mautic.form.type.lead_dashboard_leads_in_time_widget' => [
Expand Down
45 changes: 35 additions & 10 deletions app/bundles/LeadBundle/Entity/LeadFieldRepository.php
Expand Up @@ -116,11 +116,43 @@ public function getFieldAliases($object = 'lead')
return $qb->select('f.alias, f.is_unique_identifer as is_unique, f.type, f.object')
->from(MAUTIC_TABLE_PREFIX.'lead_fields', 'f')
->where($qb->expr()->eq('object', ':object'))
->setParameter('f.object', $object)
->setParameter('object', $object)
->orderBy('f.field_order', 'ASC')
->execute()->fetchAll();
}

/**
* Add company left join.
*
* @param \Doctrine\ORM\QueryBuilder|\Doctrine\DBAL\Query\QueryBuilder $q
*/
private function addCompanyLeftJoin($q)
{
$q->leftJoin('l', MAUTIC_TABLE_PREFIX.'companies_leads', 'companies_lead', 'l.id = companies_lead.lead_id');
$q->leftJoin('companies_lead', MAUTIC_TABLE_PREFIX.'companies', 'company', 'companies_lead.company_id = company.id');
}

/**
* Return property by field alias and join tables.
*
* @param string $field
* @param \Doctrine\ORM\QueryBuilder|\Doctrine\DBAL\Query\QueryBuilder $q
*/
public function getPropertyByField($field, $q)
{
$columnAlias = 'l.';
// Join company tables If we're trying search by company fields
if (in_array($field, array_column($this->getFieldAliases('company'), 'alias'))) {
$this->addCompanyLeftJoin($q);
$columnAlias = 'company.';
} elseif (in_array($field, ['utm_campaign', 'utm_content', 'utm_medium', 'utm_source', 'utm_term'])) {
$q->join('l', MAUTIC_TABLE_PREFIX.'lead_utmtags', 'u', 'l.id = u.lead_id');
$columnAlias = 'u.';
}

return $columnAlias.$field;
}

/**
* Compare a form result value with defined value for defined lead.
*
Expand Down Expand Up @@ -160,14 +192,7 @@ public function compareValue($lead, $field, $value, $operatorExpr)
return false;
}
} else {
// Standard field / UTM field
$utmField = in_array($field, ['utm_campaign', 'utm_content', 'utm_medium', 'utm_source', 'utm_term']);
if ($utmField) {
$q->join('l', MAUTIC_TABLE_PREFIX.'lead_utmtags', 'u', 'l.id = u.lead_id');
$property = 'u.'.$field;
} else {
$property = 'l.'.$field;
}
$property = $this->getPropertyByField($field, $q);
if ($operatorExpr === 'empty' || $operatorExpr === 'notEmpty') {
$q->where(
$q->expr()->andX(
Expand Down Expand Up @@ -260,7 +285,7 @@ public function compareValue($lead, $field, $value, $operatorExpr)
->setParameter('lead', (int) $lead)
->setParameter('value', $value);
}
if ($utmField) {
if (strpos($property, 'u.') === 0) {
// Match only against the latest UTM properties.
$q->orderBy('u.date_added', 'DESC');
$q->setMaxResults(1);
Expand Down
Expand Up @@ -64,13 +64,14 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'field',
'leadfields_choices',
[
'label' => 'mautic.lead.campaign.event.field',
'label_attr' => ['class' => 'control-label'],
'multiple' => false,
'with_tags' => true,
'with_utm' => true,
'empty_value' => 'mautic.core.select',
'attr' => [
'label' => 'mautic.lead.campaign.event.field',
'label_attr' => ['class' => 'control-label'],
'multiple' => false,
'with_company_fields' => true,
'with_tags' => true,
'with_utm' => true,
'empty_value' => 'mautic.core.select',
'attr' => [
'class' => 'form-control',
'tooltip' => 'mautic.lead.campaign.event.field_descr',
'onchange' => 'Mautic.updateLeadFieldValues(this)',
Expand Down
27 changes: 17 additions & 10 deletions app/bundles/LeadBundle/Form/Type/LeadFieldsType.php
Expand Up @@ -11,7 +11,7 @@

namespace Mautic\LeadBundle\Form\Type;

use Mautic\CoreBundle\Factory\MauticFactory;
use Mautic\LeadBundle\Model\FieldModel;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
Expand All @@ -21,14 +21,17 @@
*/
class LeadFieldsType extends AbstractType
{
private $model;
/**
* @var FieldModel
*/
protected $fieldModel;

/**
* @param MauticFactory $factory
* @param FieldModel $fieldModel
*/
public function __construct(MauticFactory $factory)
public function __construct(FieldModel $fieldModel)
{
$this->model = $factory->getModel('lead.field');
$this->fieldModel = $fieldModel;
}

/**
Expand All @@ -37,13 +40,16 @@ public function __construct(MauticFactory $factory)
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
/** @var \Mautic\LeadBundle\Model\FieldModel $model */
$model = $this->model;
$model = $this->fieldModel;
$resolver->setDefaults([
'choices' => function (Options $options) use ($model) {
$fieldList = $model->getFieldList();
if ($options['with_tags']) {
$fieldList['Core']['tags'] = 'mautic.lead.field.tags';
}
if ($options['with_company_fields']) {
$fieldList['Company'] = $model->getFieldList(false, true, ['isPublished' => true, 'object' => 'company']);
}
if ($options['with_utm']) {
$fieldList['UTM']['utm_campaign'] = 'mautic.lead.field.utmcampaign';
$fieldList['UTM']['utm_content'] = 'mautic.lead.field.utmcontent';
Expand All @@ -54,10 +60,11 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)

return $fieldList;
},
'global_only' => false,
'required' => false,
'with_tags' => false,
'with_utm' => false,
'global_only' => false,
'required' => false,
'with_company_fields' => false,
'with_tags' => false,
'with_utm' => false,
]);
}

Expand Down
2 changes: 2 additions & 0 deletions app/bundles/LeadBundle/Model/FieldModel.php
Expand Up @@ -17,6 +17,7 @@
use Mautic\CoreBundle\Doctrine\Helper\IndexSchemaHelper;
use Mautic\CoreBundle\Helper\InputHelper;
use Mautic\CoreBundle\Model\FormModel;
use Mautic\FormBundle\Entity\Field;
use Mautic\LeadBundle\Entity\LeadField;
use Mautic\LeadBundle\Entity\LeadFieldRepository;
use Mautic\LeadBundle\Event\LeadFieldEvent;
Expand Down Expand Up @@ -808,6 +809,7 @@ public function getFieldList($byGroup = true, $alphabetical = true, $filters = [

$leadFields = [];

/** @var LeadField $f * */
foreach ($fields as $f) {
if ($byGroup) {
$fieldName = $this->translator->trans('mautic.lead.field.group.'.$f->getGroup());
Expand Down
1 change: 1 addition & 0 deletions app/bundles/LeadBundle/Translations/en_US/messages.ini
Expand Up @@ -92,6 +92,7 @@ mautic.lead.lead.update.action.help="Update the contact fields with values from
mautic.lead.company.update.action.help="Update the company fields with values from this event."
mautic.lead.field.group="Group"
mautic.lead.field.group.core="Core"
mautic.lead.field.group.company="Company"
mautic.lead.field.group.extra="Extra"
mautic.lead.field.group.personal="Personal"
mautic.lead.field.group.professional="Professional"
Expand Down