Merge remote-tracking branch 'upstream/4.5' into 4.5-master-2015-01-19-15-14-40
[civicrm-core.git] / CRM / PCP / Form / Campaign.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * This class generates form components for processing a pcp page creati
38 *
39 */
40 class CRM_PCP_Form_Campaign extends CRM_Core_Form {
41 public $_context;
42 public $_component;
43
44 public function preProcess() {
45 // we do not want to display recently viewed items, so turn off
46 $this->assign('displayRecent', FALSE);
47
48 // component null in controller object - fix? dgg
49 // $this->_component = $this->controller->get('component');
50 $this->_component = CRM_Utils_Request::retrieve('component', 'String', $this);
51 $this->assign('component', $this->_component);
52
53 $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this);
54 $this->assign('context', $this->_context);
55
56 $this->_pageId = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE);
57 $title = ts('Setup a Personal Campaign Page - Step 2');
58
59 if ($this->_pageId) {
60 $title = ts('Edit Your Personal Campaign Page');
61 }
62
63 CRM_Utils_System::setTitle($title);
64 parent::preProcess();
65 }
66
67 public function setDefaultValues() {
68 $dafaults = array();
69 $dao = new CRM_PCP_DAO_PCP();
70
71 if ($this->_pageId) {
72 $dao->id = $this->_pageId;
73 if ($dao->find(TRUE)) {
74 CRM_Core_DAO::storeValues($dao, $defaults);
75 }
76 // fix the display of the monetary value, CRM-4038
77 if (isset($defaults['goal_amount'])) {
78 $defaults['goal_amount'] = CRM_Utils_Money::format($defaults['goal_amount'], NULL, '%a');
79 }
80
81 $defaults['pcp_title'] = CRM_Utils_Array::value('title', $defaults);
82 $defaults['pcp_intro_text'] = CRM_Utils_Array::value('intro_text', $defaults);
83 }
84
85 if ($this->get('action') & CRM_Core_Action::ADD) {
86 $defaults['is_active'] = 1;
87 $defaults['is_honor_roll'] = 1;
88 $defaults['is_thermometer'] = 1;
89 }
90
91 $this->_contactID = CRM_Utils_Array::value('contact_id', $defaults);
92 $this->_contriPageId = CRM_Utils_Array::value('page_id', $defaults);
93
94 return $defaults;
95 }
96
97 /**
98 * Build the form object
99 *
100 * @return void
101 */
102 public function buildQuickForm() {
103 $this->add('text', 'pcp_title', ts('Title'), NULL, TRUE);
104 $this->add('textarea', 'pcp_intro_text', ts('Welcome'), NULL, TRUE);
105 $this->add('text', 'goal_amount', ts('Your Goal'), NULL, TRUE);
106 $this->addRule('goal_amount', ts('Goal Amount should be a numeric value'), 'money');
107
108 $attributes = array();
109 if ($this->_component == 'event') {
110 if ($this->get('action') & CRM_Core_Action::ADD) {
111 $attributes = array('value' => ts('Join Us'), 'onClick' => 'select();');
112 }
113 $this->add('text', 'donate_link_text', ts('Sign up Button'), $attributes);
114 }
115 else {
116 if ($this->get('action') & CRM_Core_Action::ADD) {
117 $attributes = array('value' => ts('Donate Now'), 'onClick' => 'select();');
118 }
119 $this->add('text', 'donate_link_text', ts('Donation Button'), $attributes);
120 }
121
122 $attrib = array('rows' => 8, 'cols' => 60);
123 $this->add('textarea', 'page_text', ts('Your Message'), NULL, FALSE);
124
125 $maxAttachments = 1;
126 CRM_Core_BAO_File::buildAttachment($this, 'civicrm_pcp', $this->_pageId, $maxAttachments);
127
128 $this->addElement('checkbox', 'is_thermometer', ts('Progress Bar'));
129 $this->addElement('checkbox', 'is_honor_roll', ts('Honor Roll'), NULL);
130 $this->addElement('checkbox', 'is_active', ts('Active'));
131
132 $this->addButtons(
133 array(
134 array(
135 'type' => 'upload',
136 'name' => ts('Save'),
137 'isDefault' => TRUE,
138 ),
139 array(
140 'type' => 'cancel',
141 'name' => ts('Cancel'),
142 ),
143 )
144 );
145 $this->addFormRule(array('CRM_PCP_Form_Campaign', 'formRule'), $this);
146 }
147
148 /**
149 * Global form rule
150 *
151 * @param array $fields
152 * The input form values.
153 * @param array $files
154 * The uploaded files if any.
155 * @param $self
156 *
157 *
158 * @return bool|array
159 * true if no errors, else array of errors
160 */
161 public static function formRule($fields, $files, $self) {
162 $errors = array();
163 if ($fields['goal_amount'] <= 0) {
164 $errors['goal_amount'] = ts('Goal Amount should be a numeric value greater than zero.');
165 }
166 if (strlen($fields['donate_link_text']) >= 64) {
167 $errors['donate_link_text'] = ts('Button Text must be less than 64 characters.');
168 }
169 return $errors;
170 }
171
172 /**
173 * Process the form submission
174 *
175 *
176 * @return void
177 */
178 public function postProcess() {
179 $params = $this->controller->exportValues($this->_name);
180 $checkBoxes = array('is_thermometer', 'is_honor_roll', 'is_active');
181
182 foreach ($checkBoxes as $key) {
183 if (!isset($params[$key])) {
184 $params[$key] = 0;
185 }
186 }
187 $session = CRM_Core_Session::singleton();
188 $contactID = isset($this->_contactID) ? $this->_contactID : $session->get('userID');
189 if (!$contactID) {
190 $contactID = $this->get('contactID');
191 }
192 $params['title'] = $params['pcp_title'];
193 $params['intro_text'] = $params['pcp_intro_text'];
194 $params['contact_id'] = $contactID;
195 $params['page_id'] = $this->get('component_page_id') ? $this->get('component_page_id') : $this->_contriPageId;
196 $params['page_type'] = $this->_component;
197
198 // since we are allowing html input from the user
199 // we also need to purify it, so lets clean it up
200 $htmlFields = array('intro_text', 'page_text', 'title');
201 foreach ($htmlFields as $field) {
202 if (!empty($params[$field])) {
203 $params[$field] = CRM_Utils_String::purifyHTML($params[$field]);
204 }
205 }
206
207 $entity_table = CRM_PCP_BAO_PCP::getPcpEntityTable($params['page_type']);
208
209 $pcpBlock = new CRM_PCP_DAO_PCPBlock();
210 $pcpBlock->entity_table = $entity_table;
211 $pcpBlock->entity_id = $params['page_id'];
212 $pcpBlock->find(TRUE);
213
214 $params['pcp_block_id'] = $pcpBlock->id;
215
216 $params['goal_amount'] = CRM_Utils_Rule::cleanMoney($params['goal_amount']);
217
218 $approval_needed = $pcpBlock->is_approval_needed;
219 $approvalMessage = NULL;
220
221 if ($this->get('action') & CRM_Core_Action::ADD) {
222 $params['status_id'] = $approval_needed ? 1 : 2;
223 $approvalMessage = $approval_needed ? ts('but requires administrator review before you can begin promoting your campaign. You will receive an email confirmation shortly which includes a link to return to this page.') : ts('and is ready to use.');
224 }
225
226 $params['id'] = $this->_pageId;
227
228 $pcp = CRM_PCP_BAO_PCP::add($params, FALSE);
229
230 // add attachments as needed
231 CRM_Core_BAO_File::formatAttachment($params,
232 $params,
233 'civicrm_pcp',
234 $pcp->id
235 );
236
237 $pageStatus = isset($this->_pageId) ? ts('updated') : ts('created');
238 $statusId = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $pcp->id, 'status_id');
239
240 //send notification of PCP create/update.
241 $pcpParams = array('entity_table' => $entity_table, 'entity_id' => $pcp->page_id);
242 $notifyParams = array();
243 $notifyStatus = "";
244 CRM_Core_DAO::commonRetrieve('CRM_PCP_DAO_PCPBlock', $pcpParams, $notifyParams, array('notify_email'));
245
246 if ($emails = $pcpBlock->notify_email) {
247 $this->assign('pcpTitle', $pcp->title);
248
249 if ($this->_pageId) {
250 $this->assign('mode', 'Update');
251 }
252 else {
253 $this->assign('mode', 'Add');
254 }
255 $pcpStatus = CRM_Core_OptionGroup::getLabel('pcp_status', $statusId);
256 $this->assign('pcpStatus', $pcpStatus);
257
258 $this->assign('pcpId', $pcp->id);
259
260 $supporterUrl = CRM_Utils_System::url('civicrm/contact/view',
261 "reset=1&cid={$pcp->contact_id}",
262 TRUE, NULL, FALSE,
263 FALSE
264 );
265 $this->assign('supporterUrl', $supporterUrl);
266 $supporterName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $pcp->contact_id, 'display_name');
267 $this->assign('supporterName', $supporterName);
268
269 if ($this->_component == 'contribute') {
270 $pageUrl = CRM_Utils_System::url('civicrm/contribute/transact',
271 "reset=1&id={$pcpBlock->entity_id}",
272 TRUE, NULL, FALSE,
273 TRUE
274 );
275 $contribPageTitle = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $pcpBlock->entity_id, 'title');
276 }
277 elseif ($this->_component == 'event') {
278 $pageUrl = CRM_Utils_System::url('civicrm/event',
279 "reset=1&id={$pcpBlock->entity_id}",
280 TRUE, NULL, FALSE,
281 TRUE
282 );
283 $contribPageTitle = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $pcpBlock->entity_id, 'title');
284 }
285
286 $this->assign('contribPageUrl', $pageUrl);
287 $this->assign('contribPageTitle', $contribPageTitle);
288
289 $managePCPUrl = CRM_Utils_System::url('civicrm/admin/pcp',
290 "reset=1",
291 TRUE, NULL, FALSE,
292 FALSE
293 );
294 $this->assign('managePCPUrl', $managePCPUrl);
295
296 //get the default domain email address.
297 list($domainEmailName, $domainEmailAddress) = CRM_Core_BAO_Domain::getNameAndEmail();
298
299 if (!$domainEmailAddress || $domainEmailAddress == 'info@EXAMPLE.ORG') {
300 $fixUrl = CRM_Utils_System::url('civicrm/admin/domain', 'action=update&reset=1');
301 CRM_Core_Error::fatal(ts('The site administrator needs to enter a valid \'FROM Email Address\' in <a href="%1">Administer CiviCRM &raquo; Communications &raquo; FROM Email Addresses</a>. The email address used may need to be a valid mail account with your email service provider.', array(1 => $fixUrl)));
302 }
303
304 //if more than one email present for PCP notification ,
305 //first email take it as To and other as CC and First email
306 //address should be sent in users email receipt for
307 //support purpose.
308 $emailArray = explode(',', $emails);
309 $to = $emailArray[0];
310 unset($emailArray[0]);
311 $cc = implode(',', $emailArray);
312
313 list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate(
314 array(
315 'groupName' => 'msg_tpl_workflow_contribution',
316 'valueName' => 'pcp_notify',
317 'contactId' => $contactID,
318 'from' => "$domainEmailName <$domainEmailAddress>",
319 'toEmail' => $to,
320 'cc' => $cc,
321 )
322 );
323
324 if ($sent) {
325 $notifyStatus = ts('A notification email has been sent to the site administrator.');
326 }
327 }
328
329 CRM_Core_BAO_File::processAttachment($params, 'civicrm_pcp', $pcp->id);
330
331 // send email notification to supporter, if initial setup / add mode.
332 if (!$this->_pageId) {
333 CRM_PCP_BAO_PCP::sendStatusUpdate($pcp->id, $statusId, TRUE, $this->_component);
334 if ($approvalMessage && CRM_Utils_Array::value('status_id', $params) == 1) {
335 $notifyStatus .= ts(' You will receive a second email as soon as the review process is complete.');
336 }
337 }
338
339 //check if pcp created by anonymous user
340 $anonymousPCP = 0;
341 if (!$session->get('userID')) {
342 $anonymousPCP = 1;
343 }
344
345 CRM_Core_Session::setStatus(ts("Your Personal Campaign Page has been %1 %2 %3",
346 array(1 => $pageStatus, 2 => $approvalMessage, 3 => $notifyStatus)
347 ), '', 'info');
348 if (!$this->_pageId) {
349 $session->pushUserContext(CRM_Utils_System::url('civicrm/pcp/info', "reset=1&id={$pcp->id}&ap={$anonymousPCP}"));
350 }
351 elseif ($this->_context == 'dashboard') {
352 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/pcp', 'reset=1'));
353 }
354 }
355 }