| 1 |
<?php |
| 2 |
|
| 3 |
trait BirthdayAutomationTriggers |
| 4 |
{ |
| 5 |
private $dataSources = []; |
| 6 |
|
| 7 |
public function onAcymDefineUserStatusCheckTriggers(&$triggers) |
| 8 |
{ |
| 9 |
$triggers[] = 'on_birthday'; |
| 10 |
} |
| 11 |
|
| 12 |
public function onAcymDeclareTriggers(&$triggers, &$defaultValues) |
| 13 |
{ |
| 14 |
$dataSources = []; |
| 15 |
acym_trigger('onAcymDeclareDataSourcesBirthdayTrigger', [&$dataSources]); |
| 16 |
|
| 17 |
$this->dataSources = $dataSources; |
| 18 |
|
| 19 |
$sourceOptions = []; |
| 20 |
|
| 21 |
$defaultSource = empty($defaultValues['on_birthday']['source']) ? 'acymailing' : $defaultValues['on_birthday']['source']; |
| 22 |
$defaultField = empty($defaultValues['on_birthday']['field']) ? '' : $defaultValues['on_birthday']['field']; |
| 23 |
$defaultDayBefore = empty($defaultValues['on_birthday']['day_before']) ? '0' : $defaultValues['on_birthday']['day_before']; |
| 24 |
$defaultBeforeHour = empty($defaultValues['on_birthday']['hour']) ? '12' : $defaultValues['on_birthday']['hour']; |
| 25 |
$defaultBeforeMinutes = empty($defaultValues['on_birthday']['minutes']) ? '00' : $defaultValues['on_birthday']['minutes']; |
| 26 |
|
| 27 |
foreach ($this->dataSources as $key => $oneSource) { |
| 28 |
$sourceOptions[] = acym_selectOption($key, $oneSource['source_name']); |
| 29 |
} |
| 30 |
|
| 31 |
$triggers['user']['on_birthday'] = new stdClass(); |
| 32 |
$triggers['user']['on_birthday']->name = acym_translation('ACYM_ON_USER_BIRTHDAY'); |
| 33 |
|
| 34 |
$hour = []; |
| 35 |
$minutes = []; |
| 36 |
$i = 0; |
| 37 |
while ($i <= 59) { |
| 38 |
$j = $i < 10 ? '0'.$i : $i; |
| 39 |
if ($i <= 23) { |
| 40 |
$hour[$j] = $j; |
| 41 |
} |
| 42 |
$minutes[$j] = $j; |
| 43 |
$i++; |
| 44 |
} |
| 45 |
|
| 46 |
$option = '<div class="grid-x grid-margin-x grid-margin-y"> |
| 47 |
<div class="cell grid-x"> |
| 48 |
<div class="cell medium-shrink" style="display: none"> |
| 49 |
'.acym_translation('ACYM_SOURCE').' : '; |
| 50 |
|
| 51 |
$option .= acym_select( |
| 52 |
$sourceOptions, |
| 53 |
'[triggers][user][on_birthday][source]', |
| 54 |
$defaultSource, |
| 55 |
['data-class' => 'intext_select acym__select'] |
| 56 |
); |
| 57 |
$option .= ' |
| 58 |
</div> |
| 59 |
</div>'; |
| 60 |
|
| 61 |
if (empty($this->dataSources[$defaultSource]['fields']) && !empty($this->dataSources[$defaultSource]['no_fields_error_message'])) { |
| 62 |
$option .= '<div class="cell grid-x"><span class="cell small-1 vertical-align-middle"><i class="acymicon-exclamation-circle acym__color__orange"></i></span><span class="cell small-11"><b>'.acym_translation( |
| 63 |
$this->dataSources[$defaultSource]['no_fields_error_message'] |
| 64 |
).'</b></span></div>'; |
| 65 |
} else { |
| 66 |
$option .= '<div class="cell grid-x"> |
| 67 |
<div class="cell medium-shrink"> |
| 68 |
'.acym_translation('ACYM_FIELD').' : '; |
| 69 |
$fieldsOption = $this->getFieldsForTable('acymailing'); |
| 70 |
|
| 71 |
$option .= acym_select( |
| 72 |
$fieldsOption, |
| 73 |
'[triggers][user][on_birthday][field]', |
| 74 |
$defaultField, |
| 75 |
['data-class' => 'intext_select acym__select'] |
| 76 |
); |
| 77 |
|
| 78 |
$hourSelector = acym_select( |
| 79 |
$hour, |
| 80 |
'[triggers][user][on_birthday][hour]', |
| 81 |
$defaultBeforeHour, |
| 82 |
['data-class' => 'intext_select acym__select'] |
| 83 |
); |
| 84 |
$minuteSelector = acym_select( |
| 85 |
$minutes, |
| 86 |
'[triggers][user][on_birthday][minutes]', |
| 87 |
$defaultBeforeMinutes, |
| 88 |
['data-class' => 'intext_select acym__select'] |
| 89 |
); |
| 90 |
|
| 91 |
$option .= ' |
| 92 |
</div> |
| 93 |
</div> |
| 94 |
<div class="cell grid-x"> |
| 95 |
<div class="cell auto word-break acym__automation__trigger__action__birthday"> |
| 96 |
'.acym_translationSprintf( |
| 97 |
'ACYM_TRIGGER_EVENT_BEFORE_BIRTHDAY', |
| 98 |
'<input type="number" name="[triggers][user][on_birthday][day_before]" class="intext_input" min="0" value="'.esc_attr($defaultDayBefore).'">', |
| 99 |
$hourSelector, |
| 100 |
$minuteSelector |
| 101 |
).' |
| 102 |
</div> |
| 103 |
</div> |
| 104 |
<span class="cell margin-top-1 acym__color__dark-gray word-break">'.acym_translation('ACYM_BIRTHDAY_TRIGGER_INFO').'</span>'; |
| 105 |
} |
| 106 |
|
| 107 |
$option .= '</div>'; |
| 108 |
|
| 109 |
$triggers['user']['on_birthday']->option = $option; |
| 110 |
} |
| 111 |
|
| 112 |
public function onAcymDeclareTriggersScenario(&$triggers, &$defaultValues) |
| 113 |
{ |
| 114 |
$this->onAcymDeclareTriggers($triggers, $defaultValues); |
| 115 |
} |
| 116 |
|
| 117 |
public function onAcymExecuteTrigger(&$step, &$execute, &$data) |
| 118 |
{ |
| 119 |
if (!empty($step->next_execution) && $step->next_execution > $data['time']) { |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
$triggers = $step->triggers; |
| 124 |
|
| 125 |
if (empty($triggers['on_birthday'])) return; |
| 126 |
|
| 127 |
//Values from trigger |
| 128 |
$sourceName = $triggers['on_birthday']['source']; |
| 129 |
$fieldId = $triggers['on_birthday']['field']; |
| 130 |
$dayBefore = $triggers['on_birthday']['day_before']; |
| 131 |
$hour = $triggers['on_birthday']['hour']; |
| 132 |
$minutes = $triggers['on_birthday']['minutes']; |
| 133 |
|
| 134 |
$dateNowWithTimeZone = acym_date('now', 'Y-m-d h:i:s'); |
| 135 |
$now = new DateTime($dateNowWithTimeZone); |
| 136 |
$triggerDate = new DateTime($dateNowWithTimeZone); |
| 137 |
|
| 138 |
$triggerDate->setTime($hour, $minutes); |
| 139 |
|
| 140 |
if (!empty($now->date) && !empty($triggerDate->date) && $now->date < $triggerDate->date) { |
| 141 |
$step->next_execution = acym_getTime('today '.$hour.':'.$minutes); |
| 142 |
|
| 143 |
return; |
| 144 |
} else { |
| 145 |
$step->next_execution = acym_getTime('tomorrow '.$hour.':'.$minutes); |
| 146 |
} |
| 147 |
|
| 148 |
$dataSources = []; |
| 149 |
acym_trigger('onAcymDeclareDataSourcesBirthdayTrigger', [&$dataSources]); |
| 150 |
|
| 151 |
$this->dataSources = $dataSources; |
| 152 |
|
| 153 |
$format = ''; |
| 154 |
$query = ''; |
| 155 |
|
| 156 |
if (empty($this->dataSources[$sourceName]['fields'])) return; |
| 157 |
|
| 158 |
foreach ($this->dataSources[$sourceName]['fields'] as $oneField) { |
| 159 |
if ($oneField['id'] === $fieldId) { |
| 160 |
$format = $oneField['format']; |
| 161 |
$query = $oneField['query']; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
if (empty($format) || empty($query)) return; |
| 166 |
|
| 167 |
$users = acym_loadObjectList($query); |
| 168 |
|
| 169 |
if (empty($users)) return; |
| 170 |
|
| 171 |
foreach ($users as $oneUser) { |
| 172 |
if (empty($oneUser->date)) continue; |
| 173 |
|
| 174 |
$userBirthday = DateTime::createFromFormat($format, $oneUser->date); |
| 175 |
if (empty($userBirthday)) continue; |
| 176 |
|
| 177 |
$userBirthday->sub(new DateInterval('P'.$dayBefore.'D')); |
| 178 |
|
| 179 |
if ($now->format('m-d') === $userBirthday->format('m-d')) { |
| 180 |
$execute = true; |
| 181 |
$data['userIds'][] = $oneUser->user_id; |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
protected function getFieldsForTable($dataSource): array |
| 187 |
{ |
| 188 |
if (empty($this->dataSources[$dataSource]['fields'])) return []; |
| 189 |
|
| 190 |
$fieldsOption = []; |
| 191 |
foreach ($this->dataSources[$dataSource]['fields'] as $oneField) { |
| 192 |
$fieldsOption[] = acym_selectOption($oneField['id'], $oneField['name']); |
| 193 |
} |
| 194 |
|
| 195 |
return $fieldsOption; |
| 196 |
} |
| 197 |
|
| 198 |
public function onAcymDeclareSummary_triggers(object $automation): void |
| 199 |
{ |
| 200 |
if (empty($automation->triggers['on_birthday']['source'])) { |
| 201 |
return; |
| 202 |
} |
| 203 |
|
| 204 |
$dataSources = []; |
| 205 |
acym_trigger('onAcymDeclareDataSourcesBirthdayTrigger', [&$dataSources]); |
| 206 |
|
| 207 |
if ( |
| 208 |
empty($dataSources[$automation->triggers['on_birthday']['source']]['fields']) |
| 209 |
&& !empty($dataSources[$automation->triggers['on_birthday']['source']]['no_fields_error_message']) |
| 210 |
) { |
| 211 |
$automation->triggers['on_birthday'] = acym_translation($dataSources[$automation->triggers['on_birthday']['source']]['no_fields_error_message']); |
| 212 |
|
| 213 |
return; |
| 214 |
} |
| 215 |
|
| 216 |
$fieldToDisplay = []; |
| 217 |
foreach ($dataSources[$automation->triggers['on_birthday']['source']]['fields'] as $field) { |
| 218 |
if ($field['id'] != $automation->triggers['on_birthday']['field']) continue; |
| 219 |
$fieldToDisplay = $field; |
| 220 |
} |
| 221 |
|
| 222 |
$date = empty($automation->triggers['on_birthday']['day_before']) |
| 223 |
? acym_translation('ACYM_ON_USER_BIRTHDAY') |
| 224 |
: acym_translationSprintf( |
| 225 |
'ACYM_X_DAYS_BEFORE_BIRTHDAY', |
| 226 |
$automation->triggers['on_birthday']['day_before'] |
| 227 |
); |
| 228 |
$time = acym_translationSprintf('ACYM_AT_DATE_TIME', $automation->triggers['on_birthday']['hour'], $automation->triggers['on_birthday']['minutes']); |
| 229 |
$end = acym_translationSprintf('ACYM_FOR_THE_X_FIELD_X', $dataSources[$automation->triggers['on_birthday']['source']]['source_name'], $fieldToDisplay['name']); |
| 230 |
|
| 231 |
$automation->triggers['on_birthday'] = $date.' '.$time.' '.$end; |
| 232 |
} |
| 233 |
} |
| 234 |
|