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