Ejegg has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/151573

Change subject: WIP email bounce tracking
......................................................................

WIP email bounce tracking

Change-Id: I27ee5804027fb3280f47cb97b9d265ee4f21f9f9
---
A sites/all/modules/wmf_communication/CiviMail/CiviMailQueueRecord.php
A sites/all/modules/wmf_communication/CiviMail/CiviMailStore.php
A sites/all/modules/wmf_communication/CiviMail/CiviMailingRecord.php
M sites/all/modules/wmf_communication/wmf_communication.info
4 files changed, 272 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/crm 
refs/changes/73/151573/1

diff --git 
a/sites/all/modules/wmf_communication/CiviMail/CiviMailQueueRecord.php 
b/sites/all/modules/wmf_communication/CiviMail/CiviMailQueueRecord.php
new file mode 100644
index 0000000..1062980
--- /dev/null
+++ b/sites/all/modules/wmf_communication/CiviMail/CiviMailQueueRecord.php
@@ -0,0 +1,55 @@
+<?php
+
+interface ICiviMailQueueRecord {
+       /**
+        * Adds a bounce mail record and calls the CiviCRM bounce processing 
hooks
+        */
+       function markBounced( $bounceType );
+
+       /**
+        * Get the Variable Email Return Path header to use
+        *
+        * @returns string the VERP header
+        */
+       function getVerp();
+
+       /**
+        * Get the CiviMail database ID of this queue record
+        *
+        * @returns int
+        */
+       function getQueueID();
+}
+
+class CiviMailQueueRecord implements ICiviMailQueueRecord {
+
+       protected $queue;
+       protected $emailAddress;
+
+       /**
+        * @param CRM_Mailing_Event_DAO_Queue $queue
+        * @param string $emailAddress
+        */
+       public function __construct( $queue, $emailAddress ) {
+               $this->$queue = $queue;
+               $this->$emailAddress = $emailAddress;
+       }
+
+       public function getVerp() {
+               $verpAndUrls = CRM_Mailing_BAO_Mailing::getVerpAndUrls(
+                       $this->queue->job_id,
+                       $this->queue->id,
+                       $this->queue->hash,
+                       $this->emailAddress );
+
+               return $verpAndUrls[0];
+       }
+
+       public function markBounced( $bounceType ) {
+               //TODO
+       }
+
+       public function getQueueID() {
+               return $this->queue->id;
+       }
+}
diff --git a/sites/all/modules/wmf_communication/CiviMail/CiviMailStore.php 
b/sites/all/modules/wmf_communication/CiviMail/CiviMailStore.php
new file mode 100644
index 0000000..47d30d6
--- /dev/null
+++ b/sites/all/modules/wmf_communication/CiviMail/CiviMailStore.php
@@ -0,0 +1,165 @@
+<?php //namespace wmf_communication;
+require_once 'CiviMailingRecord.php';
+require_once 'CiviMailQueueRecord.php';
+/**
+ * Handle inserting sent CiviMail records for emails
+ * not actually sent by CiviCRM
+ */
+interface ICiviMailStore {
+
+       /**
+        * Adds a mailing template, a mailing, and a 'sent' parent job to 
CiviMail
+        *
+        * @param string $source the content's system of origination (eg 
Silverpop, WMF)
+        * @param string $templateName the source system's template name
+        * @param string $bodyTemplate the body of the mailing
+        * @param string $subjectTemplate the subject of the mailing
+        * @param int $revision the revision the mailing
+        *
+        * We use the source, templateName and revision to create a unique name
+        */
+       function addMailing( $source, $templateName, $bodyTemplate, 
$subjectTemplate, $revision = 0 );
+
+       /**
+        * Gets a mailing record matching the input parameters
+        *
+        * @param string $source
+        * @param string $templateName
+        * @param int $revision
+        *
+        * @returns ICiviMailingRecord
+        */
+       function getMailing( $source, $templateName, $revision = 0 );
+
+       /**
+        * Adds a child job with completed date $date, a queue entry,
+        * and an Activity record.
+        *
+        * @param ICiviMailingRecord $mailingRecord Mailing that is being sent
+        * @param string $email Email address of recipient
+        * @param int $date Completion date to use for child job
+        *
+        * @returns ICiviMailQueueRecord
+        *
+        * @throws CiviContactMissingException if the contact can't be found
+        */
+       function addQueueRecord( $mailingRecord, $email, $date );
+
+       /**
+        * Retrieves the queue record matching the parameters.
+        *
+        * @param ICiviMailingRecord $mailingRecord
+        * @param string $email
+        *
+        * @returns ICiviMailQueueRecord
+        */
+       function getQueueRecord( $mailingRecord, $email, $date = null );
+}
+
+class CiviMailStore implements ICiviMailStore {
+       public function addMailing( $source, $templateName, $bodyTemplate, 
$subjectTemplate, $revision = 0 ) {
+               civicrm_initialize();
+               $name = $this::makeUniqueName( $source, $templateName, 
$revision );
+               $mailing = $this->getMailingInternal( $name );
+               //TODO: transaction around adding mailing and job
+               if ( !$mailing ) {
+                       $params = array(
+                               'subject' => $subjectTemplate,
+                               'body_html' => $bodyTemplate,
+                               'name' => $name,
+                               'is_completed' => FALSE
+                       );
+                       $mailing = CRM_Mailing_BAO_Mailing::add( $params, 
CRM_Core_DAO::$_nullArray );
+               }
+
+               $job = new CRM_Mailing_BAO_Job();
+               $job->start_date = $job->end_date = date('YmdHis');
+               $job->status = 'Complete';
+               $job->job_type = 'external';
+               $job->mailing_id = $mailing->id;
+               $job->save();
+
+               return new CiviMailingRecord( $name, $mailing->id, $job->id );
+       }
+
+       public function addQueueRecord( $mailingRecord, $emailAddress, $date ) {
+               $email = new CRM_Core_DAO_Email();
+               $email->email = $emailAddress;
+               //TODO: handle multiple contacts with same email
+               if ( !$email->find() || !$email->fetch() ) {
+                       throw new CiviContactMissingException();
+               }
+               $childJob = $this->addChildJob( $mailingRecord, $date );
+               $queue = $this->addQueueInternal( $childJob, $email );
+               //TODO: Add activity record
+               return new CiviMailQueueRecord( $queue, $emailAddress );
+       }
+
+       public function getMailing( $source, $templateName, $revision = 0) {
+               $name = $this::makeUniqueName( $source, $templateName, 
$revision );
+               $mailing = $this->getMailingInternal( $name );
+               if ( !$mailing ) {
+                       return false;
+               }
+               $job = $this->getJobInternal( $mailing->id );
+               // We need both.  If somehow the job wasn't created, return 
false
+               // so the caller tries to add the mailing again.
+               if ( !$job ) {
+                       return false;
+               }
+               return new CiviMailingRecord($name, $mailing->id, $job->id);
+       }
+
+       protected function getMailingInternal( $name ) {
+               $mailing = new CRM_Mailing_DAO_Mailing;
+               $mailing->name = $name;
+
+               if ( !$mailing->find() || !$mailing->fetch() ) {
+                       return false;
+               }
+               return $mailing;
+       }
+
+       protected function getJobInternal( $mailingId ) {
+               $job = new CRM_Mailing_DAO_Job();
+               $job->mailing_id = $mailingId;
+               if ( !$job->find() || !$job->fetch() ) {
+                       return false;
+               }
+               return $job;
+       }
+
+       //TODO: Move to CiviMailingRecord?
+       protected function addChildJob( $mailingRecord, $date ) {
+               $job = new CRM_Mailing_DAO_Job();
+               $job->mailing_id = $mailingRecord->mailingId;
+               $job->parent_id = $mailingRecord->jobId;
+               $job->status = 'Complete';
+               $job->jobType = 'child';
+               $job->job_limit = 1;
+               $job->start_date = $job->end_date = $date;
+               $job->save();
+               return $job;
+       }
+
+       protected function addQueueInternal( $job, $email ) {
+               $params = array(
+                       'mailing_id' => $job->mailing_id,
+                       'job_id' => $job->id,
+                       'email_id' => $email->id,
+                       'contact_id' => $email->contact_id,
+               );
+               $queue = CRM_Mailing_Event_BAO_Queue::create( $params );
+               return $queue;
+       }
+
+       public function getQueueRecord( $mailingRecord, $email, $date = null ) {
+               return new CiviMailQueueRecord( '1.2.6d8a8e7f6b87c8af4e', 123 );
+       }
+
+       public static function makeUniqueName( $source, $templateName, 
$revision ) {
+               return "$source|$templateName|$revision";
+       }
+}
+
+class CiviContactMissingException extends Exception {}
diff --git a/sites/all/modules/wmf_communication/CiviMail/CiviMailingRecord.php 
b/sites/all/modules/wmf_communication/CiviMail/CiviMailingRecord.php
new file mode 100644
index 0000000..313eed0
--- /dev/null
+++ b/sites/all/modules/wmf_communication/CiviMail/CiviMailingRecord.php
@@ -0,0 +1,49 @@
+<?php
+
+interface ICiviMailingRecord {
+       /**
+        * Gets the unique name for this mailing in CiviCRM
+        *
+        * @returns string mailing name
+        */
+       function getMailingName();
+
+       /**
+        * Gets the CiviCRM db ID for the mailing
+        *
+        * @returns int mailing id
+        */
+       function getMailingID();
+
+       /**
+        * Gets the id of the completed parent job created along with this 
mailing
+        *
+        * @returns int parent job id
+        */
+       function getJobID();
+}
+
+class CiviMailingRecord implements ICiviMailingRecord {
+
+       protected $mailingName;
+       protected $mailingID;
+       protected $jobID;
+
+       public function __construct( $mailingName, $mailingID, $jobID ) {
+               $this->mailingName = $mailingName;
+               $this->mailingID = $mailingID;
+               $this->jobID = $jobID;
+       }
+
+       public function getJobID() {
+               return $this->$jobID;
+       }
+
+       public function getMailingID() {
+               return $this->mailingID;
+       }
+
+       public function getMailingName() {
+               return $this->mailingName;
+       }
+}
diff --git a/sites/all/modules/wmf_communication/wmf_communication.info 
b/sites/all/modules/wmf_communication/wmf_communication.info
index c9e34d2..00f1659 100644
--- a/sites/all/modules/wmf_communication/wmf_communication.info
+++ b/sites/all/modules/wmf_communication/wmf_communication.info
@@ -11,3 +11,6 @@
 files[] = Recipient.php
 files[] = Templating.php
 files[] = Translation.php
+files[] = CiviMail/CiviMailStore.php
+files[] = CiviMail/CiviMailingRecord.php
+files[] = CiviMail/CiviMailQueueRecord.php

-- 
To view, visit https://gerrit.wikimedia.org/r/151573
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I27ee5804027fb3280f47cb97b9d265ee4f21f9f9
Gerrit-PatchSet: 1
Gerrit-Project: wikimedia/fundraising/crm
Gerrit-Branch: master
Gerrit-Owner: Ejegg <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to