| 1 |
<?php |
| 2 |
|
| 3 |
trait TimeAutomationTriggers |
| 4 |
{ |
| 5 |
public function onAcymDeclareTriggers(&$triggers, &$defaultValues) |
| 6 |
{ |
| 7 |
$dailyHour = $this->config->get('daily_hour', '12'); |
| 8 |
$dailyMinute = $this->config->get('daily_minute', '00'); |
| 9 |
|
| 10 |
$triggers['classic']['asap'] = new stdClass(); |
| 11 |
$triggers['classic']['asap']->name = acym_translation('ACYM_EACH_TIME'); |
| 12 |
$triggers['classic']['asap']->option = '<input type="hidden" name="[triggers][classic][asap]" value="y">'; |
| 13 |
|
| 14 |
$hour = []; |
| 15 |
$minutes = []; |
| 16 |
$i = 0; |
| 17 |
while ($i <= 59) { |
| 18 |
if ($i <= 23) { |
| 19 |
$hour[$i] = $i < 10 ? '0'.$i : $i; |
| 20 |
} |
| 21 |
$minutes[$i] = $i < 10 ? '0'.$i : $i; |
| 22 |
$i++; |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
$triggers['classic']['day'] = new stdClass(); |
| 27 |
$triggers['classic']['day']->name = acym_translation('ACYM_EVERY_DAY_AT'); |
| 28 |
$triggers['classic']['day']->option = '<div class="grid-x grid-margin-x" style="height: 40px;">'; |
| 29 |
$triggers['classic']['day']->option .= '<div class="cell medium-shrink">'.acym_select( |
| 30 |
$hour, |
| 31 |
'[triggers][classic][day][hour]', |
| 32 |
empty($defaultValues['day']) ? date('H') : $defaultValues['day']['hour'], |
| 33 |
['data-class' => 'intext_select acym__select'] |
| 34 |
).'</div>'; |
| 35 |
$triggers['classic']['day']->option .= '<div class="cell medium-shrink acym_vcenter">:</div>'; |
| 36 |
$triggers['classic']['day']->option .= '<div class="cell medium-auto">'.acym_select( |
| 37 |
$minutes, |
| 38 |
'[triggers][classic][day][minutes]', |
| 39 |
empty($defaultValues['day']) ? date('i') : $defaultValues['day']['minutes'], |
| 40 |
['data-class' => 'intext_select acym__select'] |
| 41 |
).'</div>'; |
| 42 |
$triggers['classic']['day']->option .= '</div>'; |
| 43 |
|
| 44 |
$days = [ |
| 45 |
'monday' => acym_translation('ACYM_MONDAY'), |
| 46 |
'tuesday' => acym_translation('ACYM_TUESDAY'), |
| 47 |
'wednesday' => acym_translation('ACYM_WEDNESDAY'), |
| 48 |
'thursday' => acym_translation('ACYM_THURSDAY'), |
| 49 |
'friday' => acym_translation('ACYM_FRIDAY'), |
| 50 |
'saturday' => acym_translation('ACYM_SATURDAY'), |
| 51 |
'sunday' => acym_translation('ACYM_SUNDAY'), |
| 52 |
]; |
| 53 |
|
| 54 |
$triggers['classic']['weeks_on'] = new stdClass(); |
| 55 |
$triggers['classic']['weeks_on']->name = acym_translation('ACYM_EVERY_WEEK_ON'); |
| 56 |
$triggers['classic']['weeks_on']->option = '<div class="grid-x">'; |
| 57 |
$triggers['classic']['weeks_on']->option .= '<div class="cell">'.acym_selectMultiple( |
| 58 |
$days, |
| 59 |
'[triggers][classic][weeks_on][day]', |
| 60 |
empty($defaultValues['weeks_on']) ? ['monday'] : $defaultValues['weeks_on']['day'], |
| 61 |
['data-class' => 'acym__select'] |
| 62 |
).'</div>'; |
| 63 |
$triggers['classic']['weeks_on']->option .= '<div class="cell margin-top-1 acym_vcenter">'; |
| 64 |
$triggers['classic']['weeks_on']->option .= acym_translationSprintf( |
| 65 |
'ACYM_AT_DATE_TIME', |
| 66 |
'<div class="margin-left-1 margin-right-1">'.acym_select( |
| 67 |
$hour, |
| 68 |
'[triggers][classic][weeks_on][hour]', |
| 69 |
!isset($defaultValues['weeks_on']['hour']) ? $dailyHour : $defaultValues['weeks_on']['hour'], |
| 70 |
['data-class' => 'intext_select acym__select'] |
| 71 |
).'</div>', |
| 72 |
'<div class="margin-left-1 margin-right-1">'.acym_select( |
| 73 |
$minutes, |
| 74 |
'[triggers][classic][weeks_on][minutes]', |
| 75 |
!isset($defaultValues['weeks_on']['minutes']) ? $dailyMinute : $defaultValues['weeks_on']['minutes'], |
| 76 |
['data-class' => 'intext_select acym__select'] |
| 77 |
).'</div>' |
| 78 |
); |
| 79 |
$triggers['classic']['weeks_on']->option .= '</div>'; |
| 80 |
$triggers['classic']['weeks_on']->option .= '</div>'; |
| 81 |
|
| 82 |
|
| 83 |
$triggers['classic']['on_day_month'] = new stdClass(); |
| 84 |
$triggers['classic']['on_day_month']->name = acym_translation('ACYM_ONTHE'); |
| 85 |
$triggers['classic']['on_day_month']->option = '<div class="grid-x grid-margin-x margin-y">'; |
| 86 |
$triggers['classic']['on_day_month']->option .= '<div class="cell medium-4">'.acym_select( |
| 87 |
[ |
| 88 |
'first' => acym_translation('ACYM_FIRST'), |
| 89 |
'second' => acym_translation('ACYM_SECOND'), |
| 90 |
'third' => acym_translation('ACYM_THIRD'), |
| 91 |
'fourth' => acym_translation('ACYM_FOURTH'), |
| 92 |
'last' => acym_translation('ACYM_LAST'), |
| 93 |
], |
| 94 |
'[triggers][classic][on_day_month][number]', |
| 95 |
empty($defaultValues['on_day_month']) ? null : $defaultValues['on_day_month']['number'], |
| 96 |
['data-class' => 'acym__select'] |
| 97 |
).'</div>'; |
| 98 |
$triggers['classic']['on_day_month']->option .= '<div class="cell medium-4">'.acym_select( |
| 99 |
$days, |
| 100 |
'[triggers][classic][on_day_month][day]', |
| 101 |
empty($defaultValues['on_day_month']) ? null : $defaultValues['on_day_month']['day'], |
| 102 |
[ |
| 103 |
'data-class' => 'acym__select', |
| 104 |
'style' => 'margin: 0 10px;', |
| 105 |
] |
| 106 |
).'</div>'; |
| 107 |
$triggers['classic']['on_day_month']->option .= '<div class="cell medium-4 acym_vcenter">'.acym_translation('ACYM_DAYOFMONTH').'</div>'; |
| 108 |
$triggers['classic']['on_day_month']->option .= '<div class="cell acym_vcenter">'; |
| 109 |
$triggers['classic']['on_day_month']->option .= acym_translationSprintf( |
| 110 |
'ACYM_AT_DATE_TIME', |
| 111 |
'<div class="margin-left-1 margin-right-1">'.acym_select( |
| 112 |
$hour, |
| 113 |
'[triggers][classic][on_day_month][hour]', |
| 114 |
!isset($defaultValues['on_day_month']['hour']) ? $dailyHour : $defaultValues['on_day_month']['hour'], |
| 115 |
['data-class' => 'intext_select acym__select'] |
| 116 |
).'</div>', |
| 117 |
'<div class="margin-left-1 margin-right-1">'.acym_select( |
| 118 |
$minutes, |
| 119 |
'[triggers][classic][on_day_month][minutes]', |
| 120 |
!isset($defaultValues['on_day_month']['minutes']) ? $dailyMinute : $defaultValues['on_day_month']['minutes'], |
| 121 |
['data-class' => 'intext_select acym__select'] |
| 122 |
).'</div>' |
| 123 |
); |
| 124 |
$triggers['classic']['on_day_month']->option .= '</div>'; |
| 125 |
$triggers['classic']['on_day_month']->option .= '</div>'; |
| 126 |
|
| 127 |
|
| 128 |
$every = [ |
| 129 |
'3600' => acym_translation('ACYM_HOURS'), |
| 130 |
'86400' => acym_translation('ACYM_DAYS'), |
| 131 |
'604800' => acym_translation('ACYM_WEEKS'), |
| 132 |
'2628000' => acym_translation('ACYM_MONTHS'), |
| 133 |
]; |
| 134 |
|
| 135 |
$defaultEvery = empty($defaultValues['every']['number']) ? '1' : $defaultValues['every']['number']; |
| 136 |
$triggers['classic']['every'] = new stdClass(); |
| 137 |
$triggers['classic']['every']->name = acym_translation('ACYM_EVERY'); |
| 138 |
$triggers['classic']['every']->option = '<div class="grid-x grid-margin-x">'; |
| 139 |
$triggers['classic']['every']->option .= '<div class="cell medium-shrink">'; |
| 140 |
$triggers['classic']['every']->option .= '<input type="number" min="1" name="[triggers][classic][every][number]" class="intext_input" value="'.intval($defaultEvery).'">'; |
| 141 |
$triggers['classic']['every']->option .= '</div>'; |
| 142 |
$triggers['classic']['every']->option .= '<div class="cell medium-auto">'.acym_select( |
| 143 |
$every, |
| 144 |
'[triggers][classic][every][type]', |
| 145 |
empty($defaultValues['every']) ? '604800' : $defaultValues['every']['type'], |
| 146 |
['data-class' => 'intext_select acym__select'] |
| 147 |
).'</div>'; |
| 148 |
$triggers['classic']['every']->option .= '</div>'; |
| 149 |
} |
| 150 |
|
| 151 |
public function onAcymExecuteTrigger(&$step, &$execute, &$data) |
| 152 |
{ |
| 153 |
if (!empty($step->is_scenario)) { |
| 154 |
return; |
| 155 |
} |
| 156 |
|
| 157 |
$time = $data['time']; |
| 158 |
$triggers = $step->triggers; |
| 159 |
|
| 160 |
$nextExecutionDate = []; |
| 161 |
|
| 162 |
$dailyHour = $this->config->get('daily_hour', '12'); |
| 163 |
$dailyMinute = $this->config->get('daily_minute', '00'); |
| 164 |
|
| 165 |
|
| 166 |
if (!empty($triggers['asap'])) { |
| 167 |
$execute = true; |
| 168 |
$nextExecutionDate[] = $time; |
| 169 |
} |
| 170 |
|
| 171 |
if (!empty($triggers['day'])) { |
| 172 |
$dayBasedOnCMSTimezone = acym_date('now', 'Y-m-d'); |
| 173 |
|
| 174 |
$hour = $triggers['day']['hour']; |
| 175 |
$minutes = $triggers['day']['minutes']; |
| 176 |
|
| 177 |
if (strlen($hour) < 2) $hour = '0'.$hour; |
| 178 |
if (strlen($minutes) < 2) $minutes = '0'.$minutes; |
| 179 |
|
| 180 |
$dayBasedOnCMSTimezoneAtSpecifiedHour = acym_getTimeFromCMSDate($dayBasedOnCMSTimezone.' '.$hour.':'.$minutes); |
| 181 |
|
| 182 |
if ($time < $dayBasedOnCMSTimezoneAtSpecifiedHour) { |
| 183 |
$nextExecutionDate[] = $dayBasedOnCMSTimezoneAtSpecifiedHour; |
| 184 |
} else { |
| 185 |
$nextExecutionDate[] = $dayBasedOnCMSTimezoneAtSpecifiedHour + 86400; |
| 186 |
|
| 187 |
if (empty($step->last_execution)) $execute = true; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
if (!empty($triggers['weeks_on'])) { |
| 192 |
if (isset($triggers['weeks_on']['hour'])) { |
| 193 |
$hour = $triggers['weeks_on']['hour']; |
| 194 |
$minutes = $triggers['weeks_on']['minutes']; |
| 195 |
} else { |
| 196 |
$hour = $dailyHour; |
| 197 |
$minutes = $dailyMinute; |
| 198 |
} |
| 199 |
|
| 200 |
if (strlen($hour) < 2) $hour = '0'.$hour; |
| 201 |
if (strlen($minutes) < 2) $minutes = '0'.$minutes; |
| 202 |
|
| 203 |
foreach ($triggers['weeks_on']['day'] as $day) { |
| 204 |
$dayBasedOnCMSTimezone = acym_date('now', 'Y-m-d'); |
| 205 |
|
| 206 |
$dayBasedOnCMSTimezoneAtSpecifiedHour = acym_getTimeFromCMSDate($dayBasedOnCMSTimezone.' '.$hour.':'.$minutes); |
| 207 |
|
| 208 |
if ($day == strtolower(acym_date('now', 'l', true, false))) { |
| 209 |
if ($time < $dayBasedOnCMSTimezoneAtSpecifiedHour) { |
| 210 |
$nextExecutionDate[] = $dayBasedOnCMSTimezoneAtSpecifiedHour; |
| 211 |
} else { |
| 212 |
$nextExecutionDate[] = $dayBasedOnCMSTimezoneAtSpecifiedHour + 604800; |
| 213 |
|
| 214 |
$lastExecutionIsNotToday = acym_date($step->last_execution, 'Y-m-d') !== $dayBasedOnCMSTimezone; |
| 215 |
$nextExecutionIsToday = acym_date($step->next_execution, 'Y-m-d') === $dayBasedOnCMSTimezone; |
| 216 |
if (empty($step->last_execution) || ($lastExecutionIsNotToday && $nextExecutionIsToday)) $execute = true; |
| 217 |
} |
| 218 |
} else { |
| 219 |
$days = [ |
| 220 |
'monday', |
| 221 |
'tuesday', |
| 222 |
'wednesday', |
| 223 |
'thursday', |
| 224 |
'friday', |
| 225 |
'saturday', |
| 226 |
'sunday', |
| 227 |
]; |
| 228 |
$currentDayOfWeek = acym_date('now', 'N') - 1; |
| 229 |
$wantedDayOfWeek = array_search($day, $days); |
| 230 |
|
| 231 |
$shift = $wantedDayOfWeek - $currentDayOfWeek; |
| 232 |
if ($shift < 0) $shift += 7; |
| 233 |
|
| 234 |
$nextExecutionDate[] = $dayBasedOnCMSTimezoneAtSpecifiedHour + 86400 * $shift; |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
if (!empty($triggers['on_day_month'])) { |
| 240 |
if (isset($triggers['on_day_month']['hour'])) { |
| 241 |
$hour = $triggers['on_day_month']['hour']; |
| 242 |
$minutes = $triggers['on_day_month']['minutes']; |
| 243 |
} else { |
| 244 |
$hour = $dailyHour; |
| 245 |
$minutes = $dailyMinute; |
| 246 |
} |
| 247 |
|
| 248 |
if (strlen($hour) < 2) $hour = '0'.$hour; |
| 249 |
if (strlen($minutes) < 2) $minutes = '0'.$minutes; |
| 250 |
|
| 251 |
$today = acym_getTime('today '.$hour.':'.$minutes); |
| 252 |
|
| 253 |
$execution = acym_getTime($triggers['on_day_month']['number'].' '.$triggers['on_day_month']['day'].' of this month '.$hour.':'.$minutes); |
| 254 |
|
| 255 |
if ($execution < $today) { |
| 256 |
$execution = acym_getTime($triggers['on_day_month']['number'].' '.$triggers['on_day_month']['day'].' of next month '.$hour.':'.$minutes); |
| 257 |
} |
| 258 |
|
| 259 |
if ($execution > $time) { |
| 260 |
$nextExecutionDate[] = $execution; |
| 261 |
} else { |
| 262 |
|
| 263 |
|
| 264 |
if (empty($step->last_execution)) { |
| 265 |
$execute = true; |
| 266 |
} |
| 267 |
|
| 268 |
$nextExecutionDate[] = $execution + 2628000; |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
if (!empty($triggers['every'])) { |
| 273 |
if (empty($step->last_execution)) { |
| 274 |
$execute = true; |
| 275 |
} else { |
| 276 |
if ($triggers['every']['type'] == 2628000) { |
| 277 |
$nextDate = new \DateTime(acym_date($step->last_execution, 'Y-m-d H:m:i'), new \DateTimeZone('UTC')); |
| 278 |
$nextDate = $nextDate->add(new \DateInterval('P'.$triggers['every']['number'].'M')); |
| 279 |
$nextDate = $nextDate->getTimestamp(); |
| 280 |
} else { |
| 281 |
$nextDate = $step->last_execution + ($triggers['every']['number'] * $triggers['every']['type']); |
| 282 |
} |
| 283 |
|
| 284 |
if ($nextDate > $time) { |
| 285 |
$nextExecutionDate[] = $nextDate; |
| 286 |
} else { |
| 287 |
$execute = true; |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
if ($execute) { |
| 292 |
$nextExecutionDate[] = $time + ($triggers['every']['number'] * $triggers['every']['type']); |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
if (!empty($nextExecutionDate)) { |
| 297 |
$step->next_execution = min($nextExecutionDate); |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
public function onAcymDeclareSummary_triggers(&$automation) |
| 302 |
{ |
| 303 |
if (!empty($automation->triggers['type_trigger'])) unset($automation->triggers['type_trigger']); |
| 304 |
if (!empty($automation->triggers['asap'])) $automation->triggers['asap'] = acym_translation('ACYM_EACH_TIME'); |
| 305 |
if (!empty($automation->triggers['day'])) { |
| 306 |
|
| 307 |
$hour = $automation->triggers['day']['hour']; |
| 308 |
$minutes = $automation->triggers['day']['minutes']; |
| 309 |
|
| 310 |
if (strlen($hour) < 2) $hour = '0'.$hour; |
| 311 |
if (strlen($minutes) < 2) $minutes = '0'.$minutes; |
| 312 |
|
| 313 |
$automation->triggers['day'] = acym_translationSprintf('ACYM_TRIGGER_DAY_SUMMARY', $hour, $minutes); |
| 314 |
} |
| 315 |
|
| 316 |
$days = [ |
| 317 |
'monday' => acym_translation('ACYM_MONDAY'), |
| 318 |
'tuesday' => acym_translation('ACYM_TUESDAY'), |
| 319 |
'wednesday' => acym_translation('ACYM_WEDNESDAY'), |
| 320 |
'thursday' => acym_translation('ACYM_THURSDAY'), |
| 321 |
'friday' => acym_translation('ACYM_FRIDAY'), |
| 322 |
'saturday' => acym_translation('ACYM_SATURDAY'), |
| 323 |
'sunday' => acym_translation('ACYM_SUNDAY'), |
| 324 |
]; |
| 325 |
if (!empty($automation->triggers['weeks_on'])) { |
| 326 |
foreach ($automation->triggers['weeks_on']['day'] as $i => $oneDay) { |
| 327 |
$automation->triggers['weeks_on']['day'][$i] = $days[$oneDay]; |
| 328 |
} |
| 329 |
|
| 330 |
if (isset($automation->triggers['weeks_on']['hour'])) { |
| 331 |
$hour = $automation->triggers['weeks_on']['hour']; |
| 332 |
$minutes = $automation->triggers['weeks_on']['minutes']; |
| 333 |
} else { |
| 334 |
$hour = $this->config->get('daily_hour', '12'); |
| 335 |
$minutes = $this->config->get('daily_minute', '00'); |
| 336 |
} |
| 337 |
|
| 338 |
if (strlen($hour) < 2) $hour = '0'.$hour; |
| 339 |
if (strlen($minutes) < 2) $minutes = '0'.$minutes; |
| 340 |
|
| 341 |
$automation->triggers['weeks_on'] = acym_translationSprintf( |
| 342 |
'ACYM_TRIGGER_WEEKS_ON_SUMMARY', |
| 343 |
implode(', ', $automation->triggers['weeks_on']['day']) |
| 344 |
).' '.acym_translationSprintf('ACYM_AT_DATE_TIME', $hour, $minutes); |
| 345 |
} |
| 346 |
if (!empty($automation->triggers['on_day_month'])) { |
| 347 |
$numbers = [ |
| 348 |
'first' => acym_translation('ACYM_FIRST'), |
| 349 |
'second' => acym_translation('ACYM_SECOND'), |
| 350 |
'third' => acym_translation('ACYM_THIRD'), |
| 351 |
'fourth' => acym_translation('ACYM_FOURTH'), |
| 352 |
'last' => acym_translation('ACYM_LAST'), |
| 353 |
]; |
| 354 |
|
| 355 |
if (isset($automation->triggers['on_day_month']['hour'])) { |
| 356 |
$hour = $automation->triggers['on_day_month']['hour']; |
| 357 |
$minutes = $automation->triggers['on_day_month']['minutes']; |
| 358 |
} else { |
| 359 |
$hour = $this->config->get('daily_hour', '12'); |
| 360 |
$minutes = $this->config->get('daily_minute', '00'); |
| 361 |
} |
| 362 |
|
| 363 |
if (strlen($hour) < 2) $hour = '0'.$hour; |
| 364 |
if (strlen($minutes) < 2) $minutes = '0'.$minutes; |
| 365 |
|
| 366 |
$automation->triggers['on_day_month'] = acym_translationSprintf( |
| 367 |
'ACYM_TRIGGER_ON_DAY_MONTH_SUMMARY', |
| 368 |
$numbers[$automation->triggers['on_day_month']['number']], |
| 369 |
$days[$automation->triggers['on_day_month']['day']] |
| 370 |
).' '.acym_translationSprintf('ACYM_AT_DATE_TIME', $hour, $minutes); |
| 371 |
} |
| 372 |
if (!empty($automation->triggers['every'])) { |
| 373 |
if ($automation->triggers['every']['type'] == 3600) $automation->triggers['every']['type'] = acym_translation('ACYM_HOURS'); |
| 374 |
if ($automation->triggers['every']['type'] == 86400) $automation->triggers['every']['type'] = acym_translation('ACYM_DAYS'); |
| 375 |
if ($automation->triggers['every']['type'] == 604800) $automation->triggers['every']['type'] = acym_translation('ACYM_WEEKS'); |
| 376 |
if ($automation->triggers['every']['type'] == 2628000) $automation->triggers['every']['type'] = acym_translation('ACYM_MONTHS'); |
| 377 |
$automation->triggers['every'] = acym_translationSprintf( |
| 378 |
'ACYM_TRIGGER_EVERY_SUMMARY', |
| 379 |
$automation->triggers['every']['number'], |
| 380 |
$automation->triggers['every']['type'] |
| 381 |
); |
| 382 |
} |
| 383 |
} |
| 384 |
} |
| 385 |
|