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