| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace SilverStripe\QueuedJobs\Extensions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use SilverStripe\Forms\DatetimeField; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use SilverStripe\Forms\DropdownField; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use SilverStripe\Forms\FieldGroup; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use SilverStripe\Forms\FieldList; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use SilverStripe\Forms\NumericField; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use SilverStripe\Forms\ReadonlyField; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use SilverStripe\Forms\TextField; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use SilverStripe\ORM\DataExtension; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use SilverStripe\QueuedJobs\Jobs\ScheduledExecutionJob; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * An extension that can be added to objects that automatically | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * adds scheduled execution capabilities to data objects. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * Developers who want to use these capabilities can set up | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @author [email protected] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @license BSD License http://silverstripe.org/bsd-license/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | class ScheduledExecutionExtension extends DataExtension | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     private static $db = array( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         'FirstExecution' => 'DBDatetime', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         'ExecuteInterval' => 'Int', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         'ExecuteEvery' => "Enum(',Minute,Hour,Day,Week,Fortnight,Month,Year')", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         'ExecuteFree' => 'Varchar', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     private static $defaults = array( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         'ExecuteInterval' => 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     private static $has_one = array( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         'ScheduledJob' => 'SilverStripe\\QueuedJobs\\DataObjects\\QueuedJobDescriptor', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @param FieldSet $fields | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 52 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     public function updateCMSFields(FieldList $fields) | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         $fields->findOrMakeTab( | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |             'Root.Schedule', | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |             _t('ScheduledExecution.ScheduleTabTitle', 'Schedule') | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         $fields->addFieldsToTab('Root.Schedule', array( | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |             $dt = DatetimeField::create('FirstExecution', _t('ScheduledExecution.FIRST_EXECUTION', 'First Execution')), | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |             FieldGroup::create( | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |                 NumericField::create('ExecuteInterval', ''), | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |                 DropdownField::create( | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |                     'ExecuteEvery', | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |                     '', | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |                     array( | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |                         '' => '', | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |                         'Minute' => _t('ScheduledExecution.ExecuteEveryMinute', 'Minute'), | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |                         'Hour' => _t('ScheduledExecution.ExecuteEveryHour', 'Hour'), | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |                         'Day' => _t('ScheduledExecution.ExecuteEveryDay', 'Day'), | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |                         'Week' => _t('ScheduledExecution.ExecuteEveryWeek', 'Week'), | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |                         'Fortnight' => _t('ScheduledExecution.ExecuteEveryFortnight', 'Fortnight'), | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |                         'Month' => _t('ScheduledExecution.ExecuteEveryMonth', 'Month'), | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |                         'Year' => _t('ScheduledExecution.ExecuteEveryYear', 'Year'), | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |                     ) | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |                 ) | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |             )->setTitle(_t('ScheduledExecution.EXECUTE_EVERY', 'Execute every')), | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |             TextField::create( | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |                 'ExecuteFree', | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |                 _t('ScheduledExecution.EXECUTE_FREE', 'Scheduled (in strtotime format from first execution)') | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |             ) | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |         )); | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |         if ($this->owner->ScheduledJobID) { | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |             $jobTime = $this->owner->ScheduledJob()->StartAfter; | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |             $fields->addFieldsToTab('Root.Schedule', array( | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |                 ReadonlyField::create('NextRunDate', _t('ScheduledExecution.NEXT_RUN_DATE', 'Next run date'), $jobTime) | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |             )); | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |         $dt->getDateField()->setConfig('showcalendar', true); | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |         $dt->getTimeField()->setConfig('showdropdown', true); | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     public function onBeforeWrite() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         parent::onBeforeWrite(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         if ($this->owner->FirstExecution) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             $changed = $this->owner->getChangedFields(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             $changed = ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |                 isset($changed['FirstExecution']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                 || isset($changed['ExecuteInterval']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                 || isset($changed['ExecuteEvery']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                 || isset($changed['ExecuteFree']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             if ($changed && $this->owner->ScheduledJobID) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                 if ($this->owner->ScheduledJob()->exists()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                     $this->owner->ScheduledJob()->delete(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |                 $this->owner->ScheduledJobID = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             if (!$this->owner->ScheduledJobID) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                 $job = new ScheduledExecutionJob($this->owner); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                 $time = date('Y-m-d H:i:s'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                 if ($this->owner->FirstExecution) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                     $time = date('Y-m-d H:i:s', strtotime($this->owner->FirstExecution)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |                 $this->owner->ScheduledJobID = singleton('SilverStripe\\QueuedJobs\\Services\\QueuedJobService') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |                     ->queueJob($job, $time); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * Define your own version of this method in your data objects to be executed EVERY time | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * the scheduled job triggers. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     public function onScheduledExecution() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 136 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 137 |  |  |  | 
            
                        
This check marks private properties in classes that are never used. Those properties can be removed.