3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class generates form components for processing a pcp page.
21 class CRM_PCP_Form_Campaign
extends CRM_Core_Form
{
28 public function preProcess() {
29 // we do not want to display recently viewed items, so turn off
30 $this->assign('displayRecent', FALSE);
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
);
37 $this->_context
= CRM_Utils_Request
::retrieve('context', 'Alphanumeric', $this);
38 $this->assign('context', $this->_context
);
40 $this->_pageId
= CRM_Utils_Request
::retrieve('id', 'Positive', $this, FALSE);
41 $title = ts('Setup a Personal Campaign Page - Step 2');
44 $title = ts('Edit Your Personal Campaign Page');
47 CRM_Utils_System
::setTitle($title);
52 * Set default form values.
55 * Default values for the form.
57 public function setDefaultValues() {
59 $dao = new CRM_PCP_DAO_PCP();
62 $dao->id
= $this->_pageId
;
63 if ($dao->find(TRUE)) {
64 CRM_Core_DAO
::storeValues($dao, $defaults);
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');
71 $defaults['pcp_title'] = $defaults['title'] ??
NULL;
72 $defaults['pcp_intro_text'] = $defaults['intro_text'] ??
NULL;
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;
79 $defaults['is_notify'] = 1;
82 $this->_contactID
= $defaults['contact_id'] ??
NULL;
83 $this->_contriPageId
= $defaults['page_id'] ??
NULL;
89 * Build the form object.
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');
98 if ($this->_component
== 'event') {
99 if ($this->get('action') & CRM_Core_Action
::ADD
) {
100 $attributes = ['value' => ts('Join Us'), 'onClick' => 'select();'];
102 $this->add('text', 'donate_link_text', ts('Sign up Button'), $attributes);
105 if ($this->get('action') & CRM_Core_Action
::ADD
) {
106 $attributes = ['value' => ts('Donate Now'), 'onClick' => 'select();'];
108 $this->add('text', 'donate_link_text', ts('Donation Button'), $attributes);
111 $attrib = ['rows' => 8, 'cols' => 60];
112 $this->add('textarea', 'page_text', ts('Your Message'), NULL, FALSE);
115 CRM_Core_BAO_File
::buildAttachment($this, 'civicrm_pcp', $this->_pageId
, $maxAttachments);
117 $this->addElement('checkbox', 'is_thermometer', ts('Progress Bar'));
118 $this->addElement('checkbox', 'is_honor_roll', ts('Honor Roll'), NULL);
119 if ($this->_pageId
) {
120 $params = ['id' => $this->_pageId
];
121 CRM_Core_DAO
::commonRetrieve('CRM_PCP_DAO_PCP', $params, $pcpInfo);
122 $owner_notification_option = CRM_Core_DAO
::getFieldValue('CRM_PCP_BAO_PCPBlock', $pcpInfo['pcp_block_id'], 'owner_notify_id');
125 $owner_notification_option = CRM_PCP_BAO_PCP
::getOwnerNotificationId($this->controller
->get('component_page_id'), $this->_component ?
$this->_component
: 'contribute');
127 if ($owner_notification_option == CRM_Core_PseudoConstant
::getKey('CRM_PCP_BAO_PCPBlock', 'owner_notify_id', 'owner_chooses')) {
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);
132 $this->addElement('checkbox', 'is_active', ts('Active'));
133 if ($this->_context
== 'dashboard') {
134 CRM_Core_Session
::singleton()->pushUserContext(CRM_Utils_System
::url('civicrm/admin/pcp', 'reset=1'));
141 'name' => ts('Save'),
146 'name' => ts('Cancel'),
150 $this->addFormRule(['CRM_PCP_Form_Campaign', 'formRule'], $this);
156 * @param array $fields
157 * The input form values.
158 * @param array $files
159 * The uploaded files if any.
164 * true if no errors, else array of errors
166 public static function formRule($fields, $files, $self) {
168 if ($fields['goal_amount'] <= 0) {
169 $errors['goal_amount'] = ts('Goal Amount should be a numeric value greater than zero.');
171 if (strlen($fields['donate_link_text']) >= 64) {
172 $errors['donate_link_text'] = ts('Button Text must be less than 64 characters.');
178 * Process the form submission.
180 public function postProcess() {
181 $params = $this->controller
->exportValues($this->_name
);
182 $checkBoxes = ['is_thermometer', 'is_honor_roll', 'is_active', 'is_notify'];
184 foreach ($checkBoxes as $key) {
185 if (!isset($params[$key])) {
189 $session = CRM_Core_Session
::singleton();
190 $contactID = $this->_contactID ??
$session->get('userID');
192 $contactID = $this->get('contactID');
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
;
200 // since we are allowing html input from the user
201 // we also need to purify it, so lets clean it up
202 $htmlFields = ['intro_text', 'page_text', 'title'];
203 foreach ($htmlFields as $field) {
204 if (!empty($params[$field])) {
205 $params[$field] = CRM_Utils_String
::purifyHTML($params[$field]);
209 $entity_table = CRM_PCP_BAO_PCP
::getPcpEntityTable($params['page_type']);
211 $pcpBlock = new CRM_PCP_DAO_PCPBlock();
212 $pcpBlock->entity_table
= $entity_table;
213 $pcpBlock->entity_id
= $params['page_id'];
214 $pcpBlock->find(TRUE);
216 $params['pcp_block_id'] = $pcpBlock->id
;
218 $params['goal_amount'] = CRM_Utils_Rule
::cleanMoney($params['goal_amount']);
220 $approval_needed = $pcpBlock->is_approval_needed
;
221 $approvalMessage = NULL;
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.');
228 $params['id'] = $this->_pageId
;
230 $pcp = CRM_PCP_BAO_PCP
::create($params);
232 // add attachments as needed
233 CRM_Core_BAO_File
::formatAttachment($params,
239 $pageStatus = isset($this->_pageId
) ?
ts('updated') : ts('created');
240 $statusId = CRM_Core_DAO
::getFieldValue('CRM_PCP_DAO_PCP', $pcp->id
, 'status_id');
242 //send notification of PCP create/update.
243 $pcpParams = ['entity_table' => $entity_table, 'entity_id' => $pcp->page_id
];
246 CRM_Core_DAO
::commonRetrieve('CRM_PCP_DAO_PCPBlock', $pcpParams, $notifyParams, ['notify_email']);
248 if ($emails = $pcpBlock->notify_email
) {
249 $this->assign('pcpTitle', $pcp->title
);
251 if ($this->_pageId
) {
252 $this->assign('mode', 'Update');
255 $this->assign('mode', 'Add');
257 $pcpStatus = CRM_Core_PseudoConstant
::getLabel('CRM_PCP_DAO_PCP', 'status_id', $statusId);
258 $this->assign('pcpStatus', $pcpStatus);
260 $this->assign('pcpId', $pcp->id
);
262 $supporterUrl = CRM_Utils_System
::url('civicrm/contact/view',
263 "reset=1&cid={$pcp->contact_id}",
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);
271 if ($this->_component
== 'contribute') {
272 $pageUrl = CRM_Utils_System
::url('civicrm/contribute/transact',
273 "reset=1&id={$pcpBlock->entity_id}",
277 $contribPageTitle = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_ContributionPage', $pcpBlock->entity_id
, 'title');
279 elseif ($this->_component
== 'event') {
280 $pageUrl = CRM_Utils_System
::url('civicrm/event',
281 "reset=1&id={$pcpBlock->entity_id}",
285 $contribPageTitle = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event', $pcpBlock->entity_id
, 'title');
288 $this->assign('contribPageUrl', $pageUrl);
289 $this->assign('contribPageTitle', $contribPageTitle);
291 $managePCPUrl = CRM_Utils_System
::url('civicrm/admin/pcp',
296 $this->assign('managePCPUrl', $managePCPUrl);
298 //get the default domain email address.
299 list($domainEmailName, $domainEmailAddress) = CRM_Core_BAO_Domain
::getNameAndEmail();
301 if (!$domainEmailAddress ||
$domainEmailAddress == 'info@EXAMPLE.ORG') {
302 $fixUrl = CRM_Utils_System
::url('civicrm/admin/domain', 'action=update&reset=1');
303 CRM_Core_Error
::fatal(ts('The site administrator needs to enter a valid \'FROM Email Address\' in <a href="%1">Administer CiviCRM » Communications » FROM Email Addresses</a>. The email address used may need to be a valid mail account with your email service provider.', [1 => $fixUrl]));
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
310 $emailArray = explode(',', $emails);
311 $to = $emailArray[0];
312 unset($emailArray[0]);
313 $cc = implode(',', $emailArray);
315 list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate
::sendTemplate(
317 'groupName' => 'msg_tpl_workflow_contribution',
318 'valueName' => 'pcp_notify',
319 'contactId' => $contactID,
320 'from' => "$domainEmailName <$domainEmailAddress>",
327 $notifyStatus = ts('A notification email has been sent to the site administrator.');
331 CRM_Core_BAO_File
::processAttachment($params, 'civicrm_pcp', $pcp->id
);
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.');
341 //check if pcp created by anonymous user
343 if (!$session->get('userID')) {
347 CRM_Core_Session
::setStatus(ts("Your Personal Campaign Page has been %1 %2 %3",
348 [1 => $pageStatus, 2 => $approvalMessage, 3 => $notifyStatus]
350 if (!$this->_pageId
) {
351 $session->pushUserContext(CRM_Utils_System
::url('civicrm/pcp/info', "reset=1&id={$pcp->id}&ap={$anonymousPCP}"));
353 elseif ($this->_context
== 'dashboard') {
354 $session->pushUserContext(CRM_Utils_System
::url('civicrm/admin/pcp', 'reset=1'));