Example #1
0
 /**
  * Agent handler for repeating tasks.
  * Create new task based on given template.
  * 
  * @param integer $templateId - id of task template
  * @param integer $flipFlop - this param needs for prevent duplicate names 
  * 		of agent (when adding agent, there is still exists current agent,
  * 		which will be removed later, when our function returns empty string),
  * 		must be 1 or 0.
  * 
  * @return string empty string.
  */
 public static function RepeatTaskByTemplateId($templateId, $flipFlop = 0)
 {
     global $DB;
     $curFlipFlop = (int) $flipFlop;
     if ($curFlipFlop === 0) {
         $newFlipFlop = 1;
     } else {
         $newFlipFlop = 0;
     }
     $templateId = (int) $templateId;
     $arFilter = array('ID' => $templateId);
     $rsTemplate = CTaskTemplates::GetList(array(), $arFilter, false, false, array('*', 'UF_*'));
     $arTemplate = $rsTemplate->Fetch();
     if (!$arTemplate) {
         return '';
     }
     // nothing to do
     if ($arTemplate['REPLICATE'] !== 'Y') {
         return '';
     }
     // nothing to do
     if (intval($arTemplate['CREATED_BY'])) {
         CTasksTools::setOccurAsUserId($arTemplate['CREATED_BY']);
     }
     // not admin in logs, but template creator
     CTaskItem::addByTemplate($templateId, CTasksTools::GetCommanderInChief(), array(), array('TEMPLATE_DATA' => $arTemplate, 'SPAWNED_BY_AGENT' => true));
     $arTemplate['REPLICATE_PARAMS'] = unserialize($arTemplate['REPLICATE_PARAMS']);
     // get next time task will be created, i.e. next Thursday if today is Thursday (and task marked as repeated) and so on
     $nextTime = CTasks::GetNextTime($arTemplate['REPLICATE_PARAMS']);
     if ($nextTime) {
         CTimeZone::Disable();
         $currentAgentCallStr = 'CTasks::RepeatTaskByTemplateId(' . $templateId . ', ' . $curFlipFlop . ');';
         $newAgentCallStr = 'CTasks::RepeatTaskByTemplateId(' . $templateId . ', ' . $newFlipFlop . ');';
         CAgent::RemoveAgent($currentAgentCallStr);
         CAgent::AddAgent($newAgentCallStr, 'tasks', 'N', 86400, $nextTime, 'Y', $nextTime);
         CTimeZone::Enable();
     }
     return '';
 }