| @@ -1,111 +1,0 @@ | ||
| 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(int $segmentId, array $listsIds, bool $ajax = true, bool $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(): void | |
| 76 | - { | |
| 77 | - $segmentId = acym_getVar('int', 'segmentId', 0); | |
| 78 | - $isExclude = acym_getVar('boolean', 'exclude', false); | |
| 79 | - $listsIds = json_decode(acym_getVar('string', 'lists', '[]'), true); | |
| 80 | - if (empty($listsIds)) { | |
| 81 | - $listsIds = []; | |
| 82 | - } | |
| 83 | - | |
| 84 | - if (empty($segmentId)) { | |
| 85 | - acym_sendAjaxResponse('', ['count' => $this->countSegmentByParams([], $listsIds, $isExclude)]); | |
| 86 | - } | |
| 87 | - acym_sendAjaxResponse('', ['count' => $this->countSegmentById($segmentId, $listsIds, true, $isExclude)]); | |
| 88 | - } | |
| 89 | - | |
| 90 | - public function saveFromCampaign(): void | |
| 91 | - { | |
| 92 | - $name = acym_getVar('string', 'segment_name', ''); | |
| 93 | - $filters = acym_getVar('array', 'acym_action', []); | |
| 94 | - | |
| 95 | - $segmentClass = new SegmentClass(); | |
| 96 | - | |
| 97 | - $segment = new \stdClass(); | |
| 98 | - | |
| 99 | - $segment->name = $name; | |
| 100 | - $segment->creation_date = acym_date('now', 'Y-m-d H:i:s'); | |
| 101 | - $segment->active = 1; | |
| 102 | - $segment->filters = json_encode($filters['filters']); | |
| 103 | - | |
| 104 | - $segmentId = $segmentClass->save($segment); | |
| 105 | - if (!empty($segmentId)) { | |
| 106 | - acym_sendAjaxResponse(acym_translation('ACYM_SEGMENT_WELL_SAVE'), ['segment_id' => $segmentId]); | |
| 107 | - } else { | |
| 108 | - acym_sendAjaxResponse(acym_translation('ACYM_COULD_NOT_SAVE_SEGMENT'), [], false); | |
| 109 | - } | |
| 110 | - } | |
| 111 | -} | |