diff --git a/README.md b/README.md index 2e74c0e..ec88d78 100644 --- a/README.md +++ b/README.md @@ -50,10 +50,12 @@ by running the dev/build via a CLI. composer require silverstripe/contentreview dev-feature_improvements -## Setup +## Configuration -If you wish to have emails sent when a page comes up for review, you -new to have the DailyTask cron job set up. See ScheduledTask.php +In order for the contentreview module to send emails, you need to *either*: + + * Setup the DailyTask script to run daily via cron. See framework/tasks/ScheduledTask.php for more information on setup. + * Install the queuedjobs module, and follow the configuration steps to create a cron job for that module. Once installed, you can just run dev/build to have a job created, which will run at 9am every day by default. ## Usage diff --git a/code/extensions/SiteTreeContentReview.php b/code/extensions/SiteTreeContentReview.php index 2bd4c53..6aefb9f 100644 --- a/code/extensions/SiteTreeContentReview.php +++ b/code/extensions/SiteTreeContentReview.php @@ -511,4 +511,25 @@ class SiteTreeContentReview extends DataExtension implements PermissionProvider ) ); } + + /** + * If the queuedjobs module is installed, queue up the first job for 9am tomorrow morning (by default) + */ + public function requireDefaultRecords() { + if(class_exists('ContentReviewNotificationJob')) { + // Ensure there is not already a job queued + if(QueuedJobDescriptor::get()->filter('Implementation', 'ContentReviewNotificationJob')->first()) return; + + $nextRun = new ContentReviewNotificationJob(); + $runHour = Config::inst()->get('ContentReviewNotificationJob', 'first_run_hour'); + $firstRunTime = date('Y-m-d H:i:s', mktime($runHour, 0, 0, date("m"), date("d")+1, date("y"))); + + singleton('QueuedJobService')->queueJob( + $nextRun, + $firstRunTime + ); + + DB::alteration_message(sprintf('Added ContentReviewNotificationJob to run at %s', $firstRunTime)); + } + } } diff --git a/code/jobs/ContentReviewNotificationJob.php b/code/jobs/ContentReviewNotificationJob.php new file mode 100644 index 0000000..654a71f --- /dev/null +++ b/code/jobs/ContentReviewNotificationJob.php @@ -0,0 +1,82 @@ +totalSteps = 1; + return QueuedJob::QUEUED; + } + + public function process() { + $this->queueNextRun(); + + $task = new ContentReviewEmails(); + $task->run(new SS_HTTPRequest('GET', '/dev/tasks/ContentReviewEmails')); + + $this->currentStep = 1; + $this->isComplete = true; + } + + /** + * Queue up the next job to run. + */ + protected function queueNextRun() { + $nextRun = new ContentReviewNotificationJob(); + $nextRunTime = mktime( + self::$next_run_hour, + self::$next_run_minute, + 0, + date('m'), + date('d') + self::$next_run_in_days, + date('Y') + ); + + singleton('QueuedJobService')->queueJob( + $nextRun, + date('Y-m-d H:i:s', $nextRunTime) + ); + } +} \ No newline at end of file diff --git a/composer.json b/composer.json index 84a8bcc..0c9f25c 100644 --- a/composer.json +++ b/composer.json @@ -19,5 +19,8 @@ "composer/installers": "*", "silverstripe/framework": "~3.1", "silverstripe/cms": "~3.1" - } + }, + "suggests": { + "silverstripe/queuedjobs": "Automatically schedules content review emails to be sent, only requiring one crontask to be created" + } }