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 PCP
22 class CRM_PCP_Form_Event
extends CRM_Event_Form_ManageEvent
{
25 * The type of pcp component.
29 public $_component = 'event';
31 public function preProcess() {
33 $this->setSelectedChild('pcp');
37 * Set default values for the form.
41 public function setDefaultValues() {
43 if (isset($this->_id
)) {
44 $title = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event', $this->_id
, 'title');
45 CRM_Utils_System
::setTitle(ts('Personal Campaign Page Settings (%1)', [1 => $title]));
47 $params = ['entity_id' => $this->_id
, 'entity_table' => 'civicrm_event'];
48 CRM_Core_DAO
::commonRetrieve('CRM_PCP_DAO_PCPBlock', $params, $defaults);
49 $defaults['pcp_active'] = $defaults['is_active'] ??
NULL;
50 // Assign contribution page ID to pageId for referencing in PCP.hlp - since $id is overwritten there. dgg
51 $this->assign('pageId', $this->_id
);
54 if (empty($defaults['id'])) {
55 $defaults['target_entity_type'] = 'event';
56 $defaults['is_approval_needed'] = 1;
57 $defaults['is_tellfriend_enabled'] = 1;
58 $defaults['tellfriend_limit'] = 5;
59 $defaults['link_text'] = ts('Promote this event with a personal campaign page');
60 $defaults['owner_notify_id'] = CRM_Core_OptionGroup
::getDefaultValue('pcp_owner_notify');
63 $ccReceipt = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_ContributionPage', $this->_id
, 'cc_receipt')
65 $defaults['notify_email'] = $ccReceipt;
72 * Build the form object.
76 public function buildQuickForm() {
77 CRM_PCP_BAO_PCP
::buildPCPForm($this);
79 $this->addElement('checkbox', 'pcp_active', ts('Enable Personal Campaign Pages? (for this event)'), NULL, ['onclick' => "return showHideByValue('pcp_active',true,'pcpFields','table-row','radio',false);"]);
81 $this->add('select', 'target_entity_type', ts('Campaign Type'),
82 ['' => ts('- select -'), 'event' => ts('Event'), 'contribute' => ts('Contribution')],
83 NULL, ['onchange' => "return showHideByValue('target_entity_type','contribute','pcpDetailFields','block','select',false);"]
86 $this->add('select', 'target_entity_id',
87 ts('Online Contribution Page'),
89 '' => ts('- select -'),
91 CRM_Contribute_PseudoConstant
::contributionPage()
94 parent
::buildQuickForm();
96 // If at least one PCP has been created, don't allow changing the target
97 $pcpBlock = new CRM_PCP_DAO_PCPBlock();
98 $pcpBlock->entity_table
= 'civicrm_event';
99 $pcpBlock->entity_id
= $this->_id
;
100 $pcpBlock->find(TRUE);
102 if (!empty($pcpBlock->id
) && CRM_PCP_BAO_PCP
::getPcpBlockInUse($pcpBlock->id
)) {
104 'target_entity_type',
106 ] as $element_name) {
107 $element = $this->getElement($element_name);
111 $this->addFormRule(['CRM_PCP_Form_Event', 'formRule'], $this);
117 * @param array $params
118 * (ref.) an assoc array of name/value pairs.
124 * mixed true or array of errors
126 public static function formRule($params, $files, $self) {
128 if (!empty($params['is_active'])) {
130 if (!empty($params['is_tellfriend_enabled']) &&
131 (CRM_Utils_Array
::value('tellfriend_limit', $params) <= 0)
133 $errors['tellfriend_limit'] = ts('if Tell Friend is enable, Maximum recipients limit should be greater than zero.');
135 if (empty($params['supporter_profile_id'])) {
136 $errors['supporter_profile_id'] = ts('Supporter profile is a required field.');
139 if (CRM_PCP_BAO_PCP
::checkEmailProfile($params['supporter_profile_id'])) {
140 $errors['supporter_profile_id'] = ts('Profile is not configured with Email address.');
144 if ($emails = CRM_Utils_Array
::value('notify_email', $params)) {
145 $emailArray = explode(',', $emails);
146 foreach ($emailArray as $email) {
147 if ($email && !CRM_Utils_Rule
::email(trim($email))) {
148 $errors['notify_email'] = ts('A valid Notify Email address must be specified');
153 return empty($errors) ?
TRUE : $errors;
157 * Process the form submission.
161 public function postProcess() {
162 // get the submitted form values.
163 $params = $this->controller
->exportValues($this->_name
);
166 $params['entity_table'] = 'civicrm_event';
167 $params['entity_id'] = $this->_id
;
170 $params['target_entity_type'] = CRM_Utils_Array
::value('target_entity_type', $params, 'event');
171 if ($params['target_entity_type'] == 'event') {
172 $params['target_entity_id'] = $this->_id
;
175 $params['target_entity_id'] = CRM_Utils_Array
::value('target_entity_id', $params, $this->_id
);
178 $dao = new CRM_PCP_DAO_PCPBlock();
179 $dao->entity_table
= $params['entity_table'];
180 $dao->entity_id
= $this->_id
;
182 $params['id'] = $dao->id
;
183 $params['is_active'] = CRM_Utils_Array
::value('pcp_active', $params, FALSE);
184 $params['is_approval_needed'] = CRM_Utils_Array
::value('is_approval_needed', $params, FALSE);
185 $params['is_tellfriend_enabled'] = CRM_Utils_Array
::value('is_tellfriend_enabled', $params, FALSE);
187 CRM_PCP_BAO_PCPBlock
::create($params);
189 // Update tab "disabled" css class
190 $this->ajaxResponse
['tabValid'] = !empty($params['is_active']);
192 parent
::endPostProcess();
196 * Return a descriptive name for the page, used in wizard header
200 public function getTitle() {
201 return ts('Enable Personal Campaign Pages');