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
21 * This class generates form components for PCP
24 class CRM_PCP_Form_Event
extends CRM_Event_Form_ManageEvent
{
27 * The type of pcp component.
31 public $_component = 'event';
33 public function preProcess() {
35 $this->setSelectedChild('pcp');
39 * Set default values for the form.
43 public function setDefaultValues() {
45 if (isset($this->_id
)) {
46 $title = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event', $this->_id
, 'title');
47 CRM_Utils_System
::setTitle(ts('Personal Campaign Page Settings (%1)', [1 => $title]));
49 $params = ['entity_id' => $this->_id
, 'entity_table' => 'civicrm_event'];
50 CRM_Core_DAO
::commonRetrieve('CRM_PCP_DAO_PCPBlock', $params, $defaults);
51 $defaults['pcp_active'] = $defaults['is_active'] ??
NULL;
52 // Assign contribution page ID to pageId for referencing in PCP.hlp - since $id is overwritten there. dgg
53 $this->assign('pageId', $this->_id
);
56 if (empty($defaults['id'])) {
57 $defaults['target_entity_type'] = 'event';
58 $defaults['is_approval_needed'] = 1;
59 $defaults['is_tellfriend_enabled'] = 1;
60 $defaults['tellfriend_limit'] = 5;
61 $defaults['link_text'] = ts('Promote this event with a personal campaign page');
62 $defaults['owner_notify_id'] = CRM_Core_OptionGroup
::getDefaultValue('pcp_owner_notify');
65 $ccReceipt = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_ContributionPage', $this->_id
, 'cc_receipt')
67 $defaults['notify_email'] = $ccReceipt;
74 * Build the form object.
78 public function buildQuickForm() {
79 CRM_PCP_BAO_PCP
::buildPCPForm($this);
81 $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);"]);
83 $this->add('select', 'target_entity_type', ts('Campaign Type'),
84 ['' => ts('- select -'), 'event' => ts('Event'), 'contribute' => ts('Contribution')],
85 NULL, ['onchange' => "return showHideByValue('target_entity_type','contribute','pcpDetailFields','block','select',false);"]
88 $this->add('select', 'target_entity_id',
89 ts('Online Contribution Page'),
91 '' => ts('- select -'),
93 CRM_Contribute_PseudoConstant
::contributionPage()
96 parent
::buildQuickForm();
98 // If at least one PCP has been created, don't allow changing the target
99 $pcpBlock = new CRM_PCP_DAO_PCPBlock();
100 $pcpBlock->entity_table
= 'civicrm_event';
101 $pcpBlock->entity_id
= $this->_id
;
102 $pcpBlock->find(TRUE);
104 if (!empty($pcpBlock->id
) && CRM_PCP_BAO_PCP
::getPcpBlockInUse($pcpBlock->id
)) {
106 'target_entity_type',
108 ] as $element_name) {
109 $element = $this->getElement($element_name);
113 $this->addFormRule(['CRM_PCP_Form_Event', 'formRule'], $this);
119 * @param array $params
120 * (ref.) an assoc array of name/value pairs.
126 * mixed true or array of errors
128 public static function formRule($params, $files, $self) {
130 if (!empty($params['is_active'])) {
132 if (!empty($params['is_tellfriend_enabled']) &&
133 (CRM_Utils_Array
::value('tellfriend_limit', $params) <= 0)
135 $errors['tellfriend_limit'] = ts('if Tell Friend is enable, Maximum recipients limit should be greater than zero.');
137 if (empty($params['supporter_profile_id'])) {
138 $errors['supporter_profile_id'] = ts('Supporter profile is a required field.');
141 if (CRM_PCP_BAO_PCP
::checkEmailProfile($params['supporter_profile_id'])) {
142 $errors['supporter_profile_id'] = ts('Profile is not configured with Email address.');
146 if ($emails = CRM_Utils_Array
::value('notify_email', $params)) {
147 $emailArray = explode(',', $emails);
148 foreach ($emailArray as $email) {
149 if ($email && !CRM_Utils_Rule
::email(trim($email))) {
150 $errors['notify_email'] = ts('A valid Notify Email address must be specified');
155 return empty($errors) ?
TRUE : $errors;
159 * Process the form submission.
163 public function postProcess() {
164 // get the submitted form values.
165 $params = $this->controller
->exportValues($this->_name
);
168 $params['entity_table'] = 'civicrm_event';
169 $params['entity_id'] = $this->_id
;
172 $params['target_entity_type'] = CRM_Utils_Array
::value('target_entity_type', $params, 'event');
173 if ($params['target_entity_type'] == 'event') {
174 $params['target_entity_id'] = $this->_id
;
177 $params['target_entity_id'] = CRM_Utils_Array
::value('target_entity_id', $params, $this->_id
);
180 $dao = new CRM_PCP_DAO_PCPBlock();
181 $dao->entity_table
= $params['entity_table'];
182 $dao->entity_id
= $this->_id
;
184 $params['id'] = $dao->id
;
185 $params['is_active'] = CRM_Utils_Array
::value('pcp_active', $params, FALSE);
186 $params['is_approval_needed'] = CRM_Utils_Array
::value('is_approval_needed', $params, FALSE);
187 $params['is_tellfriend_enabled'] = CRM_Utils_Array
::value('is_tellfriend_enabled', $params, FALSE);
189 CRM_PCP_BAO_PCPBlock
::create($params);
191 // Update tab "disabled" css class
192 $this->ajaxResponse
['tabValid'] = !empty($params['is_active']);
194 parent
::endPostProcess();
198 * Return a descriptive name for the page, used in wizard header
202 public function getTitle() {
203 return ts('Enable Personal Campaign Pages');