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