| 1 |
<?php |
| 2 |
|
| 3 |
namespace AcyMailing\Controllers\Segments; |
| 4 |
|
| 5 |
use AcyMailing\Classes\SegmentClass; |
| 6 |
use AcyMailing\Helpers\AutomationHelper; |
| 7 |
|
| 8 |
trait Campaign |
| 9 |
{ |
| 10 |
public function countSegmentById($segmentId, $listsIds, $ajax = true, $exclude = false): int |
| 11 |
{ |
| 12 |
acym_arrayToInteger($listsIds); |
| 13 |
|
| 14 |
if (empty($listsIds)) { |
| 15 |
return 0; |
| 16 |
} |
| 17 |
|
| 18 |
if (empty($segmentId)) { |
| 19 |
if ($ajax) { |
| 20 |
acym_sendAjaxResponse(acym_translation('ACYM_COULD_NOT_COUNT_USER'), [], false); |
| 21 |
} else { |
| 22 |
return 0; |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
$segmentClass = new SegmentClass(); |
| 27 |
$segment = $segmentClass->getOneById($segmentId); |
| 28 |
if (empty($segment)) { |
| 29 |
if ($ajax) { |
| 30 |
acym_sendAjaxResponse(acym_translation('ACYM_COULD_NOT_COUNT_USER'), [], false); |
| 31 |
} else { |
| 32 |
return 0; |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
$automationHelperBase = new AutomationHelper(); |
| 37 |
$automationHelperBase->removeFlag(self::FLAG_COUNT); |
| 38 |
|
| 39 |
$automationHelpers = []; |
| 40 |
foreach ($segment->filters as $or => $orValues) { |
| 41 |
if (empty($orValues)) continue; |
| 42 |
$automationHelpers[$or] = new AutomationHelper(); |
| 43 |
foreach ($orValues as $and => $andValues) { |
| 44 |
$and = intval($and); |
| 45 |
foreach ($andValues as $filterName => $options) { |
| 46 |
acym_trigger('onAcymProcessFilter_'.$filterName, [&$automationHelpers[$or], &$options, $and.'_'.$or]); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
$automationHelpers[$or]->excludeSelected = $exclude; |
| 51 |
$automationHelpers[$or]->addFlag(self::FLAG_COUNT); |
| 52 |
} |
| 53 |
|
| 54 |
$join = ' #__acym_user_has_list AS user_list ON user_list.user_id = user.id '; |
| 55 |
$join .= 'AND user_list.list_id IN ('.implode(',', $listsIds).') '; |
| 56 |
$join .= 'AND user_list.status = 1 '; |
| 57 |
if ($this->config->get('require_confirmation', 1) == 1) { |
| 58 |
$join .= 'AND user.confirmed = 1 '; |
| 59 |
} |
| 60 |
|
| 61 |
if (empty($automationHelpers)) { |
| 62 |
$automationHelperBase->join['user_list'] = $join; |
| 63 |
|
| 64 |
return acym_loadResult($automationHelperBase->getQuery(['COUNT(DISTINCT user.id)'])); |
| 65 |
} else { |
| 66 |
$automationHelperBase = array_pop($automationHelpers); |
| 67 |
$automationHelperBase->join['user_list'] = $join; |
| 68 |
$numberOfRecipients = acym_loadResult($automationHelperBase->getQuery(['COUNT(DISTINCT user.id)'])); |
| 69 |
$automationHelperBase->removeFlag(self::FLAG_COUNT); |
| 70 |
|
| 71 |
return $numberOfRecipients; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
public function countGlobalBySegmentId() |
| 76 |
{ |
| 77 |
$segmentId = acym_getVar('int', 'segmentId', 0); |
| 78 |
$isExclude = acym_getVar('boolean', 'exclude', false); |
| 79 |
$listsIds = json_decode(acym_getVar('string', 'lists', '[]')); |
| 80 |
|
| 81 |
if (empty($segmentId)) { |
| 82 |
acym_sendAjaxResponse('', ['count' => $this->countSegmentByParams([], $listsIds, $isExclude)]); |
| 83 |
} |
| 84 |
acym_sendAjaxResponse('', ['count' => $this->countSegmentById($segmentId, $listsIds, true, $isExclude)]); |
| 85 |
} |
| 86 |
|
| 87 |
public function saveFromCampaign() |
| 88 |
{ |
| 89 |
$name = acym_getVar('string', 'segment_name', ''); |
| 90 |
$filters = acym_getVar('array', 'acym_action', []); |
| 91 |
|
| 92 |
$segmentClass = new SegmentClass(); |
| 93 |
|
| 94 |
$segment = new \stdClass(); |
| 95 |
|
| 96 |
$segment->name = $name; |
| 97 |
$segment->creation_date = acym_date('now', 'Y-m-d H:i:s'); |
| 98 |
$segment->active = 1; |
| 99 |
$segment->filters = json_encode($filters['filters']); |
| 100 |
|
| 101 |
$segmentId = $segmentClass->save($segment); |
| 102 |
if ($segmentId) { |
| 103 |
acym_sendAjaxResponse(acym_translation('ACYM_SEGMENT_WELL_SAVE'), ['segment_id' => $segmentId]); |
| 104 |
} else { |
| 105 |
acym_sendAjaxResponse(acym_translation('ACYM_COULD_NOT_SAVE_SEGMENT'), [], false); |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|