public function run($request)
 {
     $sent = 0;
     if (WorkflowInstance::get()->count()) {
         // Don't attempt the filter if no instances -- prevents a crash
         $active = WorkflowInstance::get()->innerJoin('WorkflowDefinition', '"DefinitionID" = "WorkflowDefinition"."ID"')->filter(array('WorkflowStatus' => array('Active', 'Paused'), 'RemindDays:GreaterThan' => '0'));
         $active->filter(array('RemindDays:GreaterThan' => '0'));
         if ($active) {
             foreach ($active as $instance) {
                 $edited = strtotime($instance->LastEdited);
                 $days = $instance->Definition()->RemindDays;
                 if ($edited + $days * 3600 * 24 > time()) {
                     continue;
                 }
                 $email = new Email();
                 $bcc = '';
                 $members = $instance->getAssignedMembers();
                 $target = $instance->getTarget();
                 if (!$members || !count($members)) {
                     continue;
                 }
                 $email->setSubject("Workflow Reminder: {$instance->Title}");
                 $email->setBcc(implode(', ', $members->column('Email')));
                 $email->setTemplate('WorkflowReminderEmail');
                 $email->populateTemplate(array('Instance' => $instance, 'Link' => $target instanceof SiteTree ? "admin/show/{$target->ID}" : ''));
                 $email->send();
                 $sent++;
                 $instance->LastEdited = time();
                 $instance->write();
             }
         }
     }
     echo "Sent {$sent} workflow reminder emails.\n";
 }
 public function process()
 {
     $sent = 0;
     $filter = array('WorkflowStatus' => array('Active', 'Paused'), 'Definition.RemindDays:GreaterThan' => 0);
     $active = WorkflowInstance::get()->filter($filter);
     foreach ($active as $instance) {
         $edited = strtotime($instance->LastEdited);
         $days = $instance->Definition()->RemindDays;
         if ($edited + $days * 3600 * 24 > time()) {
             continue;
         }
         $email = new Email();
         $bcc = '';
         $members = $instance->getAssignedMembers();
         $target = $instance->getTarget();
         if (!$members || !count($members)) {
             continue;
         }
         $email->setSubject("Workflow Reminder: {$instance->Title}");
         $email->setBcc(implode(', ', $members->column('Email')));
         $email->setTemplate('WorkflowReminderEmail');
         $email->populateTemplate(array('Instance' => $instance, 'Link' => $target instanceof SiteTree ? "admin/show/{$target->ID}" : ''));
         $email->send();
         $sent++;
         // add a comment to the workflow if possible
         $action = $instance->CurrentAction();
         $currentComment = $action->Comment;
         $action->Comment = sprintf(_t('AdvancedWorkflow.JOB_REMINDER_COMMENT', '%s: Reminder email sent\\n\\n'), date('Y-m-d H:i:s')) . $currentComment;
         try {
             $action->write();
         } catch (Exception $ex) {
             SS_Log::log($ex, SS_Log::WARN);
         }
         $instance->LastEdited = time();
         try {
             $instance->write();
         } catch (Exception $ex) {
             SS_Log::log($ex, SS_Log::WARN);
         }
     }
     $this->currentStep = 2;
     $this->isComplete = true;
     $nextDate = date('Y-m-d H:i:s', time() + $this->repeatInterval);
     $this->queuedJobService->queueJob(new WorkflowReminderJob($this->repeatInterval), $nextDate);
 }
 public function WorkflowInstances()
 {
     return WorkflowInstance::get()->filter(array('TargetClass' => $this->ownerBaseClass, 'TargetID' => $this->owner->ID));
 }
 /**
  * When deleting an action from a workflow definition, make sure that workflows currently paused on that action
  * are deleted
  * Also removes all outbound transitions
  */
 public function onAfterDelete()
 {
     parent::onAfterDelete();
     $wfActionInstances = WorkflowActionInstance::get()->leftJoin("WorkflowInstance", '"WorkflowInstance"."ID" = "WorkflowActionInstance"."WorkflowID"')->where(sprintf('"BaseActionID" = %d AND ("WorkflowStatus" IN (\'Active\',\'Paused\'))', $this->ID));
     foreach ($wfActionInstances as $wfActionInstance) {
         $wfInstances = WorkflowInstance::get()->filter('CurrentActionID', $wfActionInstance->ID);
         foreach ($wfInstances as $wfInstance) {
             $wfInstance->Groups()->removeAll();
             $wfInstance->Users()->removeAll();
             $wfInstance->delete();
         }
         $wfActionInstance->delete();
     }
     // Delete outbound transitions
     $transitions = WorkflowTransition::get()->filter('ActionID', $this->ID);
     foreach ($transitions as $transition) {
         $transition->Groups()->removeAll();
         $transition->Users()->removeAll();
         $transition->delete();
     }
 }