Merge pull request #3112 from freeform/CRM-14568
[civicrm-core.git] / CRM / SMS / Form / Schedule.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
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 */
35
36/**
37 *
38 */
39class CRM_SMS_Form_Schedule extends CRM_Core_Form {
40
41 /**
42 * Function to set variables up before form is built
43 *
44 * @return void
45 * @access public
46 */
47 public function preProcess() {
48
49 $this->_mailingID = $this->get('mailing_id');
50
51 if (!$this->_mailingID) {
52 $this->_mailingID = CRM_Utils_Request::retrieve('mid', 'Integer', $this, TRUE);
53 }
54 }
55
56 /**
57 * This function sets the default values for the form.
58 *
59 * @access public
60 *
355ba699 61 * @return void
6a488035
TO
62 */
63 function setDefaultValues() {
64 $defaults = array();
65
66 $count = $this->get('count');
67
68 $this->assign('count', $count);
69 $defaults['now'] = 1;
70 return $defaults;
71 }
72
73 /**
74 * Build the form for the last step of the sms wizard
75 *
76 * @param
77 *
78 * @return void
79 * @access public
80 */
81 public function buildQuickform() {
82 $this->addDateTime('start_date', ts('Schedule SMS'), FALSE, array('formatType' => 'mailing'));
83
84 $this->addElement('checkbox', 'now', ts('Send Immediately'));
85
86 $this->addFormRule(array('CRM_SMS_Form_Schedule', 'formRule'), $this);
87
88 $buttons = array(
89 array('type' => 'back',
90 'name' => ts('<< Previous'),
91 ),
92 array(
93 'type' => 'next',
94 'name' => ts('Submit Mass SMS'),
95 'spacing' => '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;',
96 'isDefault' => TRUE,
97 'js' => array('onclick' => "return submitOnce(this,'" . $this->_name . "','" . ts('Processing') . "');"),
98 ),
99 array(
100 'type' => 'cancel',
101 'name' => ts('Continue Later'),
102 ),
103 );
104
105 $this->addButtons($buttons);
106
107 $preview = array();
108 $preview['type'] = CRM_Core_DAO::getFieldValue('CRM_Mailing_DAO_Mailing', $this->_mailingID, 'body_html') ? 'html' : 'text';
109 $preview['viewURL'] = CRM_Utils_System::url('civicrm/mailing/view', "reset=1&id={$this->_mailingID}");
110 $this->assign_by_ref('preview', $preview);
111 }
112
113 /**
114 * Form rule to validate the date selector and/or if we should deliver
115 * immediately.
116 *
117 * Warning: if you make changes here, be sure to also make them in
118 * Retry.php
119 *
fd31fa4c
EM
120 * @param array $params The form values
121 *
122 * @param $files
123 * @param $self
6a488035
TO
124 *
125 * @return boolean True if either we deliver immediately, or the
126 * date is properly set.
127 * @static
128 */
129 public static function formRule($params, $files, $self) {
130 if (!empty($params['_qf_Schedule_submit'])) {
131
132 CRM_Core_Session::setStatus(ts("Your Mass SMS has been saved. Click the 'Continue' action to resume working on it."), ts('Saved'), 'success');
133 $url = CRM_Utils_System::url('civicrm/mailing/browse/unscheduled', 'scheduled=false&reset=1&sms=1');
134 CRM_Utils_System::redirect($url);
135 }
136 if (isset($params['now']) || CRM_Utils_Array::value('_qf_Schedule_back', $params) == '<< Previous') {
137 return TRUE;
138 }
139
140 if (CRM_Utils_Date::format(CRM_Utils_Date::processDate($params['start_date'],
141 $params['start_date_time']
142 )) < CRM_Utils_Date::format(date('YmdHi00'))) {
143 return array(
144 'start_date' =>
145 ts('Start date cannot be earlier than the current time.'),
146 );
147 }
148 return TRUE;
149 }
150
151 /**
152 * Process the posted form values. Create and schedule a Mass SMS.
153 *
154 * @param
155 *
156 * @return void
157 * @access public
158 */
159 public function postProcess() {
160 $params = array();
161
162 $params['mailing_id'] = $ids['mailing_id'] = $this->_mailingID;
163
164 if (empty($params['mailing_id'])) {
165 CRM_Core_Error::fatal(ts('Could not find a mailing id'));
166 }
167
1e2f5f25
DS
168 foreach (array('now', 'start_date', 'start_date_time') as $parameter) {
169 $params[$parameter] = $this->controller->exportValue($this->_name, $parameter);
6a488035
TO
170 }
171
1e2f5f25
DS
172 if ($params['now']) {
173 $params['scheduled_date'] = date('YmdHis');
174 }
175 else {
176 $params['scheduled_date'] = CRM_Utils_Date::processDate($params['start_date'] . ' ' . $params['start_date_time']);
177 }
178
179 $session = CRM_Core_Session::singleton();
180 // set the scheduled_id
181 $params['scheduled_id'] = $session->get('userID');
182 $params['scheduled_date'] = date('YmdHis');
183
184 // set approval details if workflow is not enabled
185 if (!CRM_Mailing_Info::workflowEnabled()) {
186 $params['approver_id'] = $session->get('userID');
187 $params['approval_date'] = date('YmdHis');
188 $params['approval_status_id'] = 1;
6a488035
TO
189 }
190
1e2f5f25
DS
191 if ($params['now']) {
192 $params['scheduled_date'] = date('YmdHis');
193 }
194 else {
195 $params['scheduled_date'] = CRM_Utils_Date::processDate($params['start_date'] . ' ' . $params['start_date_time']);
196 }
197
198 /* Build the mailing object */
199 CRM_Mailing_BAO_Mailing::create($params, $ids);
200
6a488035
TO
201 $session = CRM_Core_Session::singleton();
202 $session->pushUserContext(CRM_Utils_System::url('civicrm/mailing/browse/scheduled',
203 'reset=1&scheduled=true&sms=1'
204 ));
205 }
206
207 /**
208 * Display Name of the form
209 *
210 * @access public
211 *
212 * @return string
213 */
214 public function getTitle() {
215 return ts('Schedule or Send');
216 }
217}
218