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