| 1 |
<?php |
| 2 |
|
| 3 |
use AcyMailing\Controllers\CampaignsController; |
| 4 |
use AcyMailing\Classes\CampaignClass; |
| 5 |
use AcyMailing\Classes\FieldClass; |
| 6 |
|
| 7 |
trait BirthdayCampaignType |
| 8 |
{ |
| 9 |
private $mailType = 'birthday'; |
| 10 |
|
| 11 |
public function getNewEmailsTypeBlock(&$extraBlocks) |
| 12 |
{ |
| 13 |
if (acym_isAdmin()) { |
| 14 |
$favoriteTemplate = $this->config->get('favorite_template', 0); |
| 15 |
if (empty($favoriteTemplate)) { |
| 16 |
$birthdayMailLink = acym_completeLink('campaigns&task=edit&step=chooseTemplate&campaign_type='.$this->mailType); |
| 17 |
} else { |
| 18 |
$birthdayMailLink = acym_completeLink('campaigns&task=edit&step=editEmail&from='.$favoriteTemplate.'&campaign_type='.$this->mailType); |
| 19 |
} |
| 20 |
} else { |
| 21 |
$birthdayMailLink = acym_frontendLink('frontcampaigns&task=edit&step=chooseTemplate&campaign_type='.$this->mailType); |
| 22 |
} |
| 23 |
|
| 24 |
$extraBlocks[] = [ |
| 25 |
'name' => acym_translation('ACYM_BIRTHDAY'), |
| 26 |
'description' => acym_translation('ACYM_BIRTHDAY_MAIL_DESC'), |
| 27 |
'icon' => 'acymicon-calendar', |
| 28 |
'link' => $birthdayMailLink, |
| 29 |
'level' => ACYM_ENTERPRISE, |
| 30 |
'email_type' => $this->mailType, |
| 31 |
]; |
| 32 |
} |
| 33 |
|
| 34 |
public function getCampaignTypes(&$types) |
| 35 |
{ |
| 36 |
$types[$this->mailType] = $this->mailType; |
| 37 |
} |
| 38 |
|
| 39 |
public function getCampaignSpecificSendSettings($type, $sendingParams, &$specificSettings) |
| 40 |
{ |
| 41 |
if ($type != $this->mailType) return; |
| 42 |
|
| 43 |
$defaultNumber = 1; |
| 44 |
if (!empty($sendingParams) && isset($sendingParams[$this->mailType.'_number'])) { |
| 45 |
$defaultNumber = $sendingParams[$this->mailType.'_number']; |
| 46 |
} |
| 47 |
$inputTime = '<input type="number" min="0" step="1" name="acym_birthday_time_number" class="intext_input" value="'.esc_attr($defaultNumber).'">'; |
| 48 |
|
| 49 |
$timeSelectOptions = [ |
| 50 |
'days' => acym_translation('ACYM_DAYS'), |
| 51 |
'weeks' => acym_translation('ACYM_WEEKS'), |
| 52 |
'months' => acym_translation('ACYM_MONTHS'), |
| 53 |
]; |
| 54 |
|
| 55 |
$selectedtType = 'days'; |
| 56 |
if (!empty($sendingParams) && isset($sendingParams[$this->mailType.'_type'])) { |
| 57 |
$selectedtType = $sendingParams[$this->mailType.'_type']; |
| 58 |
} |
| 59 |
$timeSelect = '<div class="cell medium-2 margin-left-1 margin-right-1">'; |
| 60 |
$timeSelect .= acym_select( |
| 61 |
$timeSelectOptions, |
| 62 |
'acym_birthday_time_frame', |
| 63 |
$selectedtType, |
| 64 |
['class' => 'acym__select'] |
| 65 |
); |
| 66 |
$timeSelect .= '</div>'; |
| 67 |
|
| 68 |
$timeRelativeOptions = [ |
| 69 |
'before' => acym_translation('ACYM_BEFORE'), |
| 70 |
'after' => acym_translation('ACYM_AFTER'), |
| 71 |
]; |
| 72 |
|
| 73 |
$selectedRelative = 'before'; |
| 74 |
if (!empty($sendingParams) && isset($sendingParams[$this->mailType.'_relative'])) { |
| 75 |
$selectedRelative = $sendingParams[$this->mailType.'_relative']; |
| 76 |
} |
| 77 |
$inputRelative = '<div class="cell medium-2 margin-left-1 margin-right-1">'; |
| 78 |
$inputRelative .= acym_select( |
| 79 |
$timeRelativeOptions, |
| 80 |
'acym_birthday_relative', |
| 81 |
$selectedRelative, |
| 82 |
['class' => 'acym__select'] |
| 83 |
); |
| 84 |
$inputRelative .= '</div>'; |
| 85 |
|
| 86 |
$whenSettings = '<div class="cell grid-x acym_vcenter">'; |
| 87 |
$whenSettings .= acym_translationSprintf('ACYM_SEND_IT_BEFORE_USER_BIRTHDAY', $inputTime, $timeSelect, $inputRelative); |
| 88 |
$whenSettings .= '</div>'; |
| 89 |
|
| 90 |
// Birthday field choice |
| 91 |
$fieldClass = new FieldClass(); |
| 92 |
$dateFields = $fieldClass->getFieldsByType(['date']); |
| 93 |
|
| 94 |
$pluginFields = []; |
| 95 |
foreach ($dateFields as $oneField) { |
| 96 |
$pluginFields[$oneField->id] = acym_translation($oneField->name); |
| 97 |
} |
| 98 |
|
| 99 |
$availablePlugins = [ |
| 100 |
get_class($this) => 'AcyMailing', |
| 101 |
]; |
| 102 |
acym_trigger('onAcymGetPluginField', [&$availablePlugins]); |
| 103 |
|
| 104 |
$onlyAcymailing = count($availablePlugins) === 1; |
| 105 |
$selectedPlugin = ''; |
| 106 |
$availableFields = [ |
| 107 |
'' => acym_translation('ACYM_SELECT_FIELD'), |
| 108 |
]; |
| 109 |
if (!empty($sendingParams) && !empty($sendingParams[$this->mailType.'_plugin'])) { |
| 110 |
$selectedPlugin = $sendingParams[$this->mailType.'_plugin']; |
| 111 |
} |
| 112 |
|
| 113 |
if (!empty($selectedPlugin)) { |
| 114 |
$installed = false; |
| 115 |
acym_trigger('onAcymCheckInstalled', [&$installed], $selectedPlugin); |
| 116 |
|
| 117 |
if (!$installed) { |
| 118 |
acym_trigger('getBirthdayField', [&$availableFields], get_class($this)); |
| 119 |
acym_enqueueMessage( |
| 120 |
acym_translation('ACYM_WARNING_CAMPAIGN_BASED_ON_ANOTHER_PLUGIN'), |
| 121 |
'error' |
| 122 |
); |
| 123 |
} else { |
| 124 |
acym_trigger('getBirthdayField', [&$availableFields], $selectedPlugin); |
| 125 |
} |
| 126 |
} elseif ($onlyAcymailing) { |
| 127 |
acym_trigger('getBirthdayField', [&$availableFields], get_class($this)); |
| 128 |
} |
| 129 |
|
| 130 |
if (count($availableFields) == 1) { |
| 131 |
$availableFields = ['' => acym_translation('ACYM_NO_FIELD_AVAILABLE')]; |
| 132 |
} |
| 133 |
|
| 134 |
$inputField = '<div class="cell medium-2 margin-left-1 margin-right-1" >'; |
| 135 |
$inputField .= acym_select( |
| 136 |
$availablePlugins, |
| 137 |
'acym_plugin_field', |
| 138 |
!empty($selectedPlugin) ? $selectedPlugin : '', |
| 139 |
['class' => 'acym__select'] |
| 140 |
); |
| 141 |
$inputField .= '</div><div class="cell medium-8"></div>'; |
| 142 |
|
| 143 |
$additionalSettings = '<div class="cell grid-x acym_vcenter margin-left-3 margin-bottom-1" '.($onlyAcymailing ? 'style="display:none"' : '').'>'; |
| 144 |
$additionalSettings .= acym_translationSprintf('ACYM_CHOOSE_PLUGIN_TO_SELECT_BIRTHDAY_FIELD', $inputField); |
| 145 |
$additionalSettings .= '</div>'; |
| 146 |
|
| 147 |
|
| 148 |
$selectedField = ''; |
| 149 |
if (!empty($sendingParams) && isset($sendingParams[$this->mailType.'_field'])) { |
| 150 |
$selectedField = $sendingParams[$this->mailType.'_field']; |
| 151 |
} |
| 152 |
|
| 153 |
$inputField = '<div class="cell medium-2 margin-left-1 margin-right-1">'; |
| 154 |
$inputField .= acym_select( |
| 155 |
$availableFields, |
| 156 |
'acym_birthday_field', |
| 157 |
$selectedField, |
| 158 |
['class' => 'acym__select'] |
| 159 |
); |
| 160 |
$inputField .= '</div><div class="cell medium-8"></div>'; |
| 161 |
|
| 162 |
$additionalSettings .= '<div class="cell grid-x acym_vcenter margin-left-3 margin-bottom-1" id="acym_div_date_field"'.(empty($selectedPlugin) && !$onlyAcymailing |
| 163 |
? 'style="display:none"' : '').'>'; |
| 164 |
$additionalSettings .= acym_translationSprintf('ACYM_BIRTHDAY_FIELD_PLUGIN', $inputField); |
| 165 |
$additionalSettings .= '</div>'; |
| 166 |
|
| 167 |
$specificSettings[] = [ |
| 168 |
'whenSettings' => $whenSettings, |
| 169 |
'additionnalSettings' => $additionalSettings, |
| 170 |
]; |
| 171 |
} |
| 172 |
|
| 173 |
public function saveCampaignSpecificSendSettings($type, &$specialSendings) |
| 174 |
{ |
| 175 |
if ($type != $this->mailType) return; |
| 176 |
|
| 177 |
$inputTime = acym_getVar('int', 'acym_birthday_time_number', 0); |
| 178 |
$typeTime = acym_getVar('string', 'acym_birthday_time_frame', 'day'); |
| 179 |
$relative = acym_getVar('string', 'acym_birthday_relative', 'before'); |
| 180 |
$plugin = acym_getVar('string', 'acym_plugin_field', 0); |
| 181 |
$field = acym_getVar('string', 'acym_birthday_field', 0); |
| 182 |
|
| 183 |
$specialSendings[] = [ |
| 184 |
$this->mailType.'_number' => $inputTime, |
| 185 |
$this->mailType.'_type' => $typeTime, |
| 186 |
$this->mailType.'_relative' => $relative, |
| 187 |
$this->mailType.'_plugin' => $plugin, |
| 188 |
$this->mailType.'_field' => $field, |
| 189 |
]; |
| 190 |
} |
| 191 |
|
| 192 |
public function onAcymSendCampaignSpecial($campaign, &$filters, &$pluginIsExisting) |
| 193 |
{ |
| 194 |
if ($campaign->sending_type !== $this->mailType) { |
| 195 |
return; |
| 196 |
} |
| 197 |
|
| 198 |
$sendingTime = $campaign->sending_params[$this->mailType.'_number'] ?? 1; |
| 199 |
$sendingTime = intval($sendingTime); |
| 200 |
|
| 201 |
if (empty($campaign->sending_params[$this->mailType.'_type'])) { |
| 202 |
$campaign->sending_params[$this->mailType.'_type'] = 'days'; |
| 203 |
} |
| 204 |
|
| 205 |
if ($campaign->sending_params[$this->mailType.'_type'] === 'weeks') { |
| 206 |
$sendingTime *= 7; |
| 207 |
} elseif ($campaign->sending_params[$this->mailType.'_type'] === 'months') { |
| 208 |
$sendingTime *= 30; |
| 209 |
} |
| 210 |
$filter = [ |
| 211 |
'birthday' => [ |
| 212 |
'days' => $sendingTime, |
| 213 |
'field' => $campaign->sending_params[$this->mailType.'_field'] ?? '', |
| 214 |
'relative' => $campaign->sending_params[$this->mailType.'_relative'] ?? 'before', |
| 215 |
'plugin' => $campaign->sending_params[$this->mailType.'_plugin'] ?? get_class($this), |
| 216 |
], |
| 217 |
]; |
| 218 |
|
| 219 |
$installed = false; |
| 220 |
acym_trigger('onAcymCheckInstalled', [&$installed], $filter['birthday']['plugin']); |
| 221 |
|
| 222 |
if (!$installed) { |
| 223 |
$pluginIsExisting = false; |
| 224 |
|
| 225 |
return; |
| 226 |
} |
| 227 |
$filters[] = $filter; |
| 228 |
} |
| 229 |
|
| 230 |
public function specialActionOnDelete($typeElement, $elements) |
| 231 |
{ |
| 232 |
if ($typeElement != 'field') return; |
| 233 |
$campaignClass = new CampaignClass(); |
| 234 |
$fieldClass = new FieldClass(); |
| 235 |
$birthdayMails = $campaignClass->getCampaignsByTypes([$this->mailType]); |
| 236 |
foreach ($elements as $oneElement) { |
| 237 |
if ($fieldClass->getFieldTypeById($oneElement) !== 'date') continue; |
| 238 |
foreach ($birthdayMails as $oneBirthdayMail) { |
| 239 |
if ($oneBirthdayMail->sending_params['birthday_field'] != $oneElement) continue; |
| 240 |
$oneBirthdayMail->sending_params['birthday_field'] = ''; |
| 241 |
$oneBirthdayMail->draft = 1; |
| 242 |
$campaignClass->save($oneBirthdayMail); |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
public function onAcymDisplayCampaignListingSpecificTabs(&$tabs) |
| 248 |
{ |
| 249 |
if (acym_level(ACYM_ENTERPRISE)) { |
| 250 |
$tabs['specificListing&type='.$this->mailType] = 'ACYM_BIRTHDAY_EMAIL'; |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
public function onAcymSpecificListingActive(&$exists, $task) |
| 255 |
{ |
| 256 |
if ($task == $this->mailType) { |
| 257 |
$exists = true; |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
public function onAcymCampaignDataSpecificListing(&$data, $type) |
| 262 |
{ |
| 263 |
if ($type === $this->mailType) { |
| 264 |
$data['typeWorkflowTab'] = 'specificListing&type='.$this->mailType; |
| 265 |
$data['element_to_display'] = acym_translation('ACYM_BIRTHDAY_EMAIL'); |
| 266 |
$campaignController = new CampaignsController(); |
| 267 |
$campaignController->prepareEmailsListing($data, $type); |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
public function onAcymCampaignAddFiltersSpecificListing(&$filters, $type) |
| 272 |
{ |
| 273 |
if ($type === $this->mailType) { |
| 274 |
$filters[] = 'campaign.sending_type = '.acym_escapeDB($this->mailType); |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
public function filterSpecificMailsToSend(&$specialMails, $time) |
| 279 |
{ |
| 280 |
$this->filterSpecialMailsDailySend($specialMails, $time, $this->mailType); |
| 281 |
} |
| 282 |
} |
| 283 |
|