PageRenderTime 26ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/CRM/Mailing/Event/BAO/Queue.php

https://github.com/ksecor/civicrm
PHP | 290 lines | 156 code | 38 blank | 96 comment | 9 complexity | f1bcc567b57982c2ffff22b0125445e9 MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 3.1 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2009 |
  7. +--------------------------------------------------------------------+
  8. | This file is a part of CiviCRM. |
  9. | |
  10. | CiviCRM is free software; you can copy, modify, and distribute it |
  11. | under the terms of the GNU Affero General Public License |
  12. | Version 3, 19 November 2007. |
  13. | |
  14. | CiviCRM is distributed in the hope that it will be useful, but |
  15. | WITHOUT ANY WARRANTY; without even the implied warranty of |
  16. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
  17. | See the GNU Affero General Public License for more details. |
  18. | |
  19. | You should have received a copy of the GNU Affero General Public |
  20. | License along with this program; if not, contact CiviCRM LLC |
  21. | at info[AT]civicrm[DOT]org. If you have questions about the |
  22. | GNU Affero General Public License or the licensing of CiviCRM, |
  23. | see the CiviCRM license FAQ at http://civicrm.org/licensing |
  24. +--------------------------------------------------------------------+
  25. */
  26. /**
  27. *
  28. * @package CRM
  29. * @copyright CiviCRM LLC (c) 2004-2009
  30. * $Id$
  31. *
  32. */
  33. require_once 'CRM/Mailing/Event/DAO/Queue.php';
  34. require_once 'CRM/Mailing/BAO/Job.php';
  35. require_once 'CRM/Mailing/BAO/Mailing.php';
  36. require_once 'CRM/Contact/BAO/Contact.php';
  37. class CRM_Mailing_Event_BAO_Queue extends CRM_Mailing_Event_DAO_Queue {
  38. /**
  39. * class constructor
  40. */
  41. function __construct( ) {
  42. parent::__construct( );
  43. }
  44. /**
  45. * Queue a new recipient
  46. *
  47. * @param array The values of the new EventQueue
  48. * @return object The new EventQueue
  49. * @access public
  50. * @static
  51. */
  52. public static function &create(&$params) {
  53. $eq =& new CRM_Mailing_Event_BAO_Queue();
  54. $eq->copyValues($params);
  55. $eq->hash = self::hash($params);
  56. $eq->save();
  57. return $eq;
  58. }
  59. /**
  60. * Create a security hash from the job, email and contact ids
  61. *
  62. * @param array The ids to be hashed
  63. * @return int The hash
  64. * @access public
  65. * @static
  66. */
  67. public static function hash($params) {
  68. $jobId = $params['job_id'];
  69. $emailId = $params['email_id'];
  70. $contactId = $params['contact_id'];
  71. return substr( sha1( "{$jobId}:{$emailId}:{$contactId}:" . time( ) ),
  72. 0, 16 );
  73. }
  74. /**
  75. * Verify that a queue event exists with the specified id/job id/hash
  76. *
  77. * @param int $job_id The job ID of the event to find
  78. * @param int $queue_id The Queue Event ID to find
  79. * @param string $hash The hash to validate against
  80. * @return object|null The queue event if verified, or null
  81. * @access public
  82. * @static
  83. */
  84. public static function &verify($job_id, $queue_id, $hash) {
  85. $q =& new CRM_Mailing_Event_BAO_Queue();
  86. if (!empty($job_id) && !empty($queue_id) && !empty($hash)) {
  87. $q->id = $queue_id;
  88. $q->job_id = $job_id;
  89. $q->hash = $hash;
  90. if ($q->find(true)) {
  91. return $q;
  92. }
  93. }
  94. return null;
  95. }
  96. /**
  97. * Given a queue event ID, find the corresponding email address.
  98. *
  99. * @param int $queue_id The queue event ID
  100. * @return string The email address
  101. * @access public
  102. * @static
  103. */
  104. public static function getEmailAddress($queue_id) {
  105. $email = CRM_Core_BAO_Email::getTableName();
  106. $eq = self::getTableName();
  107. $query = " SELECT $email.email as email
  108. FROM $email
  109. INNER JOIN $eq
  110. ON $eq.email_id = $email.id
  111. WHERE $eq.id = "
  112. . CRM_Utils_Type::rule($queue_id, 'Integer');
  113. $q =& new CRM_Mailing_Event_BAO_Queue();
  114. $q->query($query);
  115. if (! $q->fetch()) {
  116. return null;
  117. }
  118. return $q->email;
  119. }
  120. /**
  121. * Count up events given a mailing id and optional job id
  122. *
  123. * @param int $mailing_id ID of the mailing to count
  124. * @param int $job_id Optional ID of a job to limit results
  125. * @return int Number of matching events
  126. * @access public
  127. * @static
  128. */
  129. public static function getTotalCount($mailing_id, $job_id = null) {
  130. $dao =& new CRM_Core_DAO();
  131. $queue = self::getTableName();
  132. $mailing = CRM_Mailing_BAO_Mailing::getTableName();
  133. $job = CRM_Mailing_BAO_Job::getTableName();
  134. $dao->query("
  135. SELECT COUNT(*) as queued
  136. FROM $queue
  137. INNER JOIN $job
  138. ON $queue.job_id = $job.id
  139. INNER JOIN $mailing
  140. ON $job.mailing_id = $mailing.id
  141. AND $job.is_test = 0
  142. WHERE $mailing.id = "
  143. . CRM_Utils_Type::escape($mailing_id, 'Integer')
  144. . ($job_id ? " AND $job.id = " . CRM_Utils_Type::escape($job_id,
  145. 'Integer') : ''));
  146. $dao->fetch();
  147. return $dao->queued;
  148. }
  149. /**
  150. * Get rows for the event browser
  151. *
  152. * @param int $mailing_id ID of the mailing
  153. * @param int $job_id optional ID of the job
  154. * @param int $offset Offset
  155. * @param int $rowCount Number of rows
  156. * @param array $sort sort array
  157. * @return array Result set
  158. * @access public
  159. * @static
  160. */
  161. public static function &getRows($mailing_id, $job_id = null, $offset = null,
  162. $rowCount = null, $sort = null) {
  163. $dao =& new CRM_Core_Dao();
  164. $queue = self::getTableName();
  165. $mailing = CRM_Mailing_BAO_Mailing::getTableName();
  166. $job = CRM_Mailing_BAO_Job::getTableName();
  167. $contact = CRM_Contact_BAO_Contact::getTableName();
  168. $email = CRM_Core_BAO_Email::getTableName();
  169. $query = "
  170. SELECT $contact.display_name as display_name,
  171. $contact.id as contact_id,
  172. $email.email as email,
  173. $job.start_date as date
  174. FROM $contact
  175. INNER JOIN $queue
  176. ON $queue.contact_id = $contact.id
  177. INNER JOIN $email
  178. ON $queue.email_id = $email.id
  179. INNER JOIN $job
  180. ON $queue.job_id = $job.id
  181. INNER JOIN $mailing
  182. ON $job.mailing_id = $mailing.id
  183. AND $job.is_test = 0
  184. WHERE $mailing.id = "
  185. . CRM_Utils_Type::escape($mailing_id, 'Integer');
  186. if (!empty($job_id)) {
  187. $query .= " AND $job.id = "
  188. . CRM_Utils_Type::escape($job_id, 'Integer');
  189. }
  190. $query .= " ORDER BY $contact.sort_name, $job.start_date DESC ";
  191. if ($offset) {
  192. $query .= ' LIMIT '
  193. . CRM_Utils_Type::escape($offset, 'Integer') . ', '
  194. . CRM_Utils_Type::escape($rowCount, 'Integer');
  195. }
  196. $dao->query($query);
  197. $results = array();
  198. while ($dao->fetch()) {
  199. $url = CRM_Utils_System::url('civicrm/contact/view',
  200. "reset=1&cid={$dao->contact_id}");
  201. $results[] = array(
  202. 'name' => "<a href=\"$url\">{$dao->display_name}</a>",
  203. 'email' => $dao->email,
  204. 'date' => CRM_Utils_Date::customFormat($dao->date)
  205. );
  206. }
  207. return $results;
  208. }
  209. /**
  210. * Get the mailing object for this queue event instance
  211. *
  212. * @param
  213. * @return object Mailing BAO
  214. * @access public
  215. */
  216. public function &getMailing() {
  217. $mailing =& new CRM_Mailing_BAO_Mailing();
  218. $jobs = CRM_Mailing_BAO_Job::getTableName();
  219. $mailings = CRM_Mailing_BAO_Mailing::getTableName();
  220. $queue = self::getTableName();
  221. $mailing->query("
  222. SELECT $mailings.*
  223. FROM $mailings
  224. INNER JOIN $jobs
  225. ON $jobs.mailing_id = $mailings.id
  226. INNER JOIN $queue
  227. ON $queue.job_id = $jobs.id
  228. WHERE $queue.id = {$this->id}");
  229. $mailing->fetch();
  230. return $mailing;
  231. }
  232. public static function getContactInfo($queueID) {
  233. $query = "
  234. SELECT DISTINCT(civicrm_mailing_event_queue.contact_id) as contact_id,
  235. civicrm_contact.display_name as display_name,
  236. civicrm_email.email as email
  237. FROM civicrm_mailing_event_queue,
  238. civicrm_contact,
  239. civicrm_email
  240. WHERE civicrm_mailing_event_queue.contact_id = civicrm_contact.id
  241. AND civicrm_mailing_event_queue.email_id = civicrm_email.id
  242. AND civicrm_mailing_event_queue.id = " . CRM_Utils_Type::escape($queueID, 'Integer');
  243. $dao =& CRM_Core_DAO::executeQuery( $query, CRM_Core_DAO::$_nullArray );
  244. $displayName = 'Unknown';
  245. $email = 'Unknown';
  246. if ( $dao->fetch( ) ) {
  247. $displayName = $dao->display_name;
  248. $email = $dao->email;
  249. }
  250. return array( $displayName, $email );
  251. }
  252. }