Skip to content

Commit

Permalink
dev/mail#11 Add pre/post hook for CRM_Mailing_BAO_MailingJob
Browse files Browse the repository at this point in the history
  • Loading branch information
monishdeb committed May 30, 2018
1 parent 7f1a99f commit 088eb27
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 40 deletions.
31 changes: 17 additions & 14 deletions CRM/Mailing/BAO/Mailing.php
Original file line number Diff line number Diff line change
Expand Up @@ -1638,21 +1638,24 @@ public static function create(&$params, $ids = array()) {
// Create parent job if not yet created.
// Condition on the existence of a scheduled date.
if (!empty($params['scheduled_date']) && $params['scheduled_date'] != 'null' && empty($params['_skip_evil_bao_auto_schedule_'])) {
$jobParams = [
'mailing_id' => $mailing->id,
// If we are creating a new Completed mailing (e.g. import from another system) set the job to completed.
// Keeping former behaviour when an id is present is precautionary and may warrant reconsideration later.
'status' => ((empty($params['is_completed']) || !empty($params['id'])) ? 'Scheduled' : 'Complete'),
'is_test' => 0,
];
$job = new CRM_Mailing_BAO_MailingJob();
$job->mailing_id = $mailing->id;
// If we are creating a new Completed mailing (e.g. import from another system) set the job to completed.
// Keeping former behaviour when an id is present is precautionary and may warrant reconsideration later.
$job->status = ((empty($params['is_completed']) || !empty($params['id'])) ? 'Scheduled' : 'Complete');
$job->is_test = 0;
$job->copyValues($jobParams);

if (!$job->find(TRUE)) {
// Don't schedule job until we populate the recipients.
$job->scheduled_date = NULL;
$job->save();
$jobParams['scheduled_date'] = NULL;
$jobParams['id'] = CRM_Mailing_BAO_MailingJob::create($jobParams)->id;
}
// Schedule the job now that it has recipients.
$job->scheduled_date = $params['scheduled_date'];
$job->save();
$jobParams['scheduled_date'] = $params['scheduled_date'];
CRM_Mailing_BAO_MailingJob::create($jobParams);
}

// Populate the recipients.
Expand Down Expand Up @@ -2519,8 +2522,8 @@ public static function del($id) {
}

/**
* Delete Jobss and all its associated records
* related to test Mailings
* @deprecated
* Use CRM_Mailing_BAO_MailingJob::deleteMailingJob($id)
*
* @param int $id
* Id of the Job to delete.
Expand All @@ -2532,9 +2535,9 @@ public static function delJob($id) {
CRM_Core_Error::fatal();
}

$dao = new CRM_Mailing_BAO_MailingJob();
$dao->id = $id;
$dao->delete();
\Civi::log('This function is deprecated, use CRM_Mailing_BAO_MailingJob::deleteMailingJob instead', ['civi.tag' => 'deprecated']);

CRM_Mailing_BAO_MailingJob::deleteMailingJob($id);
}

/**
Expand Down
31 changes: 25 additions & 6 deletions CRM/Mailing/BAO/MailingJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ public static function runJobs($testParams = NULL, $mode = NULL) {
}

/* Queue up recipients for the child job being launched */

if ($job->status != 'Running') {
$transaction = new CRM_Core_Transaction();

Expand Down Expand Up @@ -283,11 +282,11 @@ public static function runJobs_post($mode = NULL) {

$transaction = new CRM_Core_Transaction();

$saveJob = new CRM_Mailing_DAO_MailingJob();
$saveJob->id = $job->id;
$saveJob->end_date = date('YmdHis');
$saveJob->status = 'Complete';
$saveJob->save();
self::create([
'id' => $job->id,
'end_date' => date('YmdHis'),
'status' => 'Complete',
]);

$mailing->reset();
$mailing->id = $job->mailing_id;
Expand Down Expand Up @@ -1042,4 +1041,24 @@ public static function findPendingTasks($jobId, $medium) {
return $eq;
}

/**
* Delete the mailing job.
*
* @param int $id
* Mailing Job id.
*
* @return mixed
*/
public static function deleteMailingJob($id) {
CRM_Utils_Hook::pre('delete', 'MailingJob', $id, CRM_Core_DAO::$_nullArray);

$jobDAO = new CRM_Mailing_BAO_MailingJob();
$jobDAO->id = $id;
$results = $jobDAO->delete();

CRM_Utils_Hook::post('delete', 'MailingJob', $jobDAO->id, $jobDAO);

return $result;
}

}
35 changes: 16 additions & 19 deletions CRM/Mailing/BAO/Spool.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,26 +87,23 @@ public function send($recipient, $headers, $body, $job_id = NULL) {
return PEAR::raiseError('Unable to create spooled mailing.');
}

$job = new CRM_Mailing_BAO_MailingJob();
$job->is_test = 0; // if set to 1 it doesn't show in the UI
$job->status = 'Complete';
$job->scheduled_date = CRM_Utils_Date::processDate(date('Y-m-d'), date('H:i:s'));
$job->start_date = $job->scheduled_date;
$job->end_date = $job->scheduled_date;
$job->mailing_id = $mailing->id;
$job->save();
$job_id = $job->id; // need this for parent_id below
$saveJob = new CRM_Mailing_DAO_MailingJob();

$job = new CRM_Mailing_BAO_MailingJob();
$job->is_test = 0;
$job->status = 'Complete';
$job->scheduled_date = CRM_Utils_Date::processDate(date('Y-m-d'), date('H:i:s'));
$job->start_date = $job->scheduled_date;
$job->end_date = $job->scheduled_date;
$job->mailing_id = $mailing->id;
$job->parent_id = $job_id;
$job->job_type = 'child';
$job->save();
$jobParams = array(
'is_test' => 0,
'scheduled_date' => date('YmdHis'),
'start_date' => date('YmdHis'),
'end_date' => date('YmdHis'),
'mailing_id' => $mailing->id,
'status' => 'Complete',
);
$job = CRM_Mailing_BAO_MailingJob::create($jobParams);

$jobParams = array_merge($jobParams, array(
'parent_id' => $job->id,
'job_type' => 'child',
));
$job = CRM_Mailing_BAO_MailingJob::create($jobParams);
$job_id = $job->id; // this is the one we want for the spool

if (is_array($recipient)) {
Expand Down
4 changes: 3 additions & 1 deletion CRM/Mailing/Form/Approve.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,9 @@ public function postProcess() {
// also delete any jobs associated with this mailing
$job = new CRM_Mailing_BAO_MailingJob();
$job->mailing_id = $ids['mailing_id'];
$job->delete();
while ($job->fetch()) {
CRM_Mailing_BAO_MailingJob::deleteMailingJob($job->id);
}
}
else {
$mailing = new CRM_Mailing_BAO_Mailing();
Expand Down

0 comments on commit 088eb27

Please sign in to comment.