Merge pull request #19602 from JMAConsulting/money_defaults
[civicrm-core.git] / CRM / PCP / Form / Event.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class generates form components for PCP
20 *
21 */
22 class CRM_PCP_Form_Event extends CRM_Event_Form_ManageEvent {
23
24 /**
25 * The type of pcp component.
26 *
27 * @var int
28 */
29 public $_component = 'event';
30
31 public function preProcess() {
32 parent::preProcess();
33 $this->setSelectedChild('pcp');
34 }
35
36 /**
37 * Set default values for the form.
38 *
39 * @return array
40 */
41 public function setDefaultValues() {
42 $defaults = [];
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]));
46
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);
52 }
53
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');
61
62 if ($this->_id &&
63 $ccReceipt = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $this->_id, 'cc_receipt')
64 ) {
65 $defaults['notify_email'] = $ccReceipt;
66 }
67 }
68 return $defaults;
69 }
70
71 /**
72 * Build the form object.
73 *
74 * @return void
75 */
76 public function buildQuickForm() {
77 CRM_PCP_BAO_PCP::buildPCPForm($this);
78
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);"]);
80
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);"]
84 );
85
86 $this->add('select', 'target_entity_id',
87 ts('Online Contribution Page'),
88 [
89 '' => ts('- select -'),
90 ] +
91 CRM_Contribute_PseudoConstant::contributionPage()
92 );
93
94 parent::buildQuickForm();
95
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);
101
102 if (!empty($pcpBlock->id) && CRM_PCP_BAO_PCP::getPcpBlockInUse($pcpBlock->id)) {
103 foreach ([
104 'target_entity_type',
105 'target_entity_id',
106 ] as $element_name) {
107 $element = $this->getElement($element_name);
108 $element->freeze();
109 }
110 }
111 $this->addFormRule(['CRM_PCP_Form_Event', 'formRule'], $this);
112 }
113
114 /**
115 * Validation.
116 *
117 * @param array $params
118 * (ref.) an assoc array of name/value pairs.
119 *
120 * @param $files
121 * @param $self
122 *
123 * @return bool|array
124 * mixed true or array of errors
125 */
126 public static function formRule($params, $files, $self) {
127 $errors = [];
128 if (!empty($params['is_active'])) {
129
130 if (!empty($params['is_tellfriend_enabled']) &&
131 (CRM_Utils_Array::value('tellfriend_limit', $params) <= 0)
132 ) {
133 $errors['tellfriend_limit'] = ts('if Tell Friend is enable, Maximum recipients limit should be greater than zero.');
134 }
135 if (empty($params['supporter_profile_id'])) {
136 $errors['supporter_profile_id'] = ts('Supporter profile is a required field.');
137 }
138 else {
139 if (CRM_PCP_BAO_PCP::checkEmailProfile($params['supporter_profile_id'])) {
140 $errors['supporter_profile_id'] = ts('Profile is not configured with Email address.');
141 }
142 }
143
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');
149 }
150 }
151 }
152 }
153 return empty($errors) ? TRUE : $errors;
154 }
155
156 /**
157 * Process the form submission.
158 *
159 * @return void
160 */
161 public function postProcess() {
162 // get the submitted form values.
163 $params = $this->controller->exportValues($this->_name);
164
165 // Source
166 $params['entity_table'] = 'civicrm_event';
167 $params['entity_id'] = $this->_id;
168
169 // Target
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;
173 }
174 else {
175 $params['target_entity_id'] = CRM_Utils_Array::value('target_entity_id', $params, $this->_id);
176 }
177
178 $dao = new CRM_PCP_DAO_PCPBlock();
179 $dao->entity_table = $params['entity_table'];
180 $dao->entity_id = $this->_id;
181 $dao->find(TRUE);
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);
186
187 CRM_PCP_BAO_PCPBlock::create($params);
188
189 // Update tab "disabled" css class
190 $this->ajaxResponse['tabValid'] = !empty($params['is_active']);
191
192 parent::endPostProcess();
193 }
194
195 /**
196 * Return a descriptive name for the page, used in wizard header
197 *
198 * @return string
199 */
200 public function getTitle() {
201 return ts('Enable Personal Campaign Pages');
202 }
203
204 }