INFRA-132 - CRM/Logging - Convert single-line @param to multi-line
[civicrm-core.git] / CRM / Mailing / BAO / Spool.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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 and the CiviCRM Licensing Exception. |
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 and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Mailing_BAO_Spool extends CRM_Mailing_DAO_Spool {
36
37 /**
100fef9d 38 * Class constructor
6a488035 39 */
00be9182 40 public function __construct() {
6a488035
TO
41 parent::__construct();
42 }
43
44 /**
45 * Store Mails into Spool table.
46 *
77b97be7 47 * @param $recipient
6a488035
TO
48 * @param array $headers The string of headers to send with the mail.
49 *
50 * @param string $body The full text of the message body, including any
51 * Mime parts, etc.
52 *
100fef9d 53 * @param int $job_id
77b97be7
EM
54 *
55 * @internal param mixed $recipients Either a comma-seperated list of recipients
56 * (RFC822 compliant), or an array of recipients,
57 * each RFC822 valid. This may contain recipients not
58 * specified in the headers, for Bcc:, resending
59 * messages, etc.
60 *
6a488035
TO
61 * @return mixed Returns true on success, or a CRM_Eore_Error
62 * containing a descriptive error message on
63 * failure.
6a488035 64 */
00be9182 65 public function send($recipient, $headers, $body, $job_id = null) {
6a488035
TO
66 $headerStr = array();
67 foreach ($headers as $name => $value) {
68 $headerStr[] = "$name: $value";
69 }
70 $headerStr = implode("\n", $headerStr);
71
72 if ( is_null( $job_id ) ) {
73 // This is not a bulk mailing. Create a dummy job for it.
74
75 $session = CRM_Core_Session::singleton();
76 $params = array();
77 $params['created_id'] = $session->get('userID');
78 $params['created_date'] = date('YmdHis');
79 $params['scheduled_id'] = $params['created_id'];
80 $params['scheduled_date'] = $params['created_date'];
6a488035
TO
81 $params['is_completed'] = 1;
82 $params['is_archived'] = 1;
83 $params['body_html'] = htmlspecialchars( $headerStr ) . "\n\n" . $body;
84 $params['subject'] = $headers['Subject'];
85 $params['name'] = $headers['Subject'];
86 $ids = array();
87 $mailing = CRM_Mailing_BAO_Mailing::create($params, $ids);
88
89 if ( empty( $mailing ) || is_a( $mailing, 'CRM_Core_Error' ) ) {
90 return PEAR::raiseError( 'Unable to create spooled mailing.' );
91 }
92
9da8dc8c 93 $job = new CRM_Mailing_BAO_MailingJob();
6a488035
TO
94 $job->is_test = 0; // if set to 1 it doesn't show in the UI
95 $job->status = 'Complete';
96 $job->scheduled_date = CRM_Utils_Date::processDate(date('Y-m-d'), date('H:i:s'));
97 $job->start_date = $job->scheduled_date;
98 $job->end_date = $job->scheduled_date;
99 $job->mailing_id = $mailing->id;
100 $job->save();
101 $job_id = $job->id; // need this for parent_id below
102
9da8dc8c 103 $job = new CRM_Mailing_BAO_MailingJob();
6a488035
TO
104 $job->is_test = 0;
105 $job->status = 'Complete';
106 $job->scheduled_date = CRM_Utils_Date::processDate(date('Y-m-d'), date('H:i:s'));
107 $job->start_date = $job->scheduled_date;
108 $job->end_date = $job->scheduled_date;
109 $job->mailing_id = $mailing->id;
110 $job->parent_id = $job_id;
111 $job->job_type = 'child';
112 $job->save();
113 $job_id = $job->id; // this is the one we want for the spool
114
115 if ( is_array( $recipient ) ) {
116 $recipient = implode( ';', $recipient );
117 }
118 }
119
120 $session = CRM_Core_Session::singleton();
121
122 $params = array(
123 'job_id' => $job_id,
124 'recipient_email' => $recipient,
125 'headers' => $headerStr,
126 'body' => $body,
127 'added_at' => date("YmdHis"),
128 'removed_at' => NULL,
129 );
130
131 $spoolMail = new CRM_Mailing_DAO_Spool();
132 $spoolMail->copyValues($params);
133 $spoolMail->save();
134
135 return TRUE;
136 }
137}