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