| 1 |
<?php |
| 2 |
|
| 3 |
use AcyMailing\Classes\SegmentClass; |
| 4 |
use AcyMailing\Helpers\AutomationHelper; |
| 5 |
|
| 6 |
trait SegmentAutomationFilters |
| 7 |
{ |
| 8 |
public function onAcymDeclareFilters(array &$filters): void |
| 9 |
{ |
| 10 |
$segmentClass = new SegmentClass(); |
| 11 |
$segments = $segmentClass->getAll(); |
| 12 |
$selectOptionSegment = []; |
| 13 |
foreach ($segments as $oneSegment) { |
| 14 |
$selectOptionSegment[] = acym_selectOption($oneSegment->id, $oneSegment->name); |
| 15 |
} |
| 16 |
|
| 17 |
$filters['acy_segment'] = new stdClass(); |
| 18 |
$filters['acy_segment']->name = acym_translation('ACYM_ACYMAILING_SEGMENT'); |
| 19 |
$filters['acy_segment']->option = '<div class="intext_select_automation cell">'; |
| 20 |
$filters['acy_segment']->option .= acym_select( |
| 21 |
$selectOptionSegment, |
| 22 |
'acym_action[filters][__numor__][__numand__][acy_segment][id]', |
| 23 |
null, |
| 24 |
[ |
| 25 |
'class' => 'intext_select_automation acym__select', |
| 26 |
] |
| 27 |
); |
| 28 |
$filters['acy_segment']->option .= '</div>'; |
| 29 |
} |
| 30 |
|
| 31 |
public function onAcymProcessFilterCount_acy_segment(&$query, &$options, &$num) |
| 32 |
{ |
| 33 |
$this->onAcymProcessFilter_acy_segment($query, $options, $num); |
| 34 |
|
| 35 |
return acym_translationSprintf('ACYM_SELECTED_USERS', $query->count()); |
| 36 |
} |
| 37 |
|
| 38 |
public function onAcymProcessFilter_acy_segment(&$query, &$options, $num) |
| 39 |
{ |
| 40 |
$segmentClass = new SegmentClass(); |
| 41 |
$oneSegment = $segmentClass->getOneById($options['id']); |
| 42 |
|
| 43 |
$automationHelpers = []; |
| 44 |
if (!empty($oneSegment->filters)) { |
| 45 |
foreach ($oneSegment->filters as $or => $orValues) { |
| 46 |
if (empty($orValues)) continue; |
| 47 |
|
| 48 |
$automationHelpers[$or] = new AutomationHelper(); |
| 49 |
foreach ($orValues as $and => $andValues) { |
| 50 |
$and = intval($and); |
| 51 |
foreach ($andValues as $filterName => $filterOptions) { |
| 52 |
acym_trigger('onAcymProcessFilter_'.$filterName, [&$automationHelpers[$or], &$filterOptions, $and.'_'.$or]); |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
$whereClauses = []; |
| 59 |
foreach ($automationHelpers as $automationHelper) { |
| 60 |
if (!empty($automationHelper->where)) { |
| 61 |
$whereClauses[] = ' ('.implode(') AND (', $automationHelper->where).')'; |
| 62 |
} |
| 63 |
if (!empty($automationHelper->join)) { |
| 64 |
$query->join = array_merge($query->join, $automationHelper->join); |
| 65 |
} |
| 66 |
if (!empty($automationHelper->leftjoin)) { |
| 67 |
$query->leftjoin = array_merge($query->leftjoin, $automationHelper->leftjoin); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
if (!empty($whereClauses)) { |
| 72 |
$query->where = array_merge($query->where, [implode(' OR ', $whereClauses)]); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
public function onAcymDeclareSummary_filters(&$automation) |
| 77 |
{ |
| 78 |
if (empty($automation['acy_segment'])) return; |
| 79 |
$oneSegment = (new SegmentClass())->getOneById($automation['acy_segment']['id']); |
| 80 |
$automation = acym_translationSprintf('ACYM_FILTER_ACY_SEGMENT_SUMMARY', $oneSegment->name); |
| 81 |
} |
| 82 |
} |
| 83 |
|