Merge pull request #14667 from hoegrammer/master
[civicrm-core.git] / CRM / Contribute / Form / ManagePremiums.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 Premiums.
20 */
21 class CRM_Contribute_Form_ManagePremiums extends CRM_Contribute_Form {
22
23 /**
24 * Pre process the form.
25 */
26 public function preProcess() {
27 parent::preProcess();
28 }
29
30 /**
31 * Set default values for the form.
32 */
33 public function setDefaultValues() {
34 $defaults = parent::setDefaultValues();
35 if ($this->_id) {
36 $params = ['id' => $this->_id];
37 CRM_Contribute_BAO_Product::retrieve($params, $tempDefaults);
38 if (isset($tempDefaults['image']) && isset($tempDefaults['thumbnail'])) {
39 $defaults['imageUrl'] = $tempDefaults['image'];
40 $defaults['thumbnailUrl'] = $tempDefaults['thumbnail'];
41 $defaults['imageOption'] = 'thumbnail';
42 // assign thumbnailUrl to template so we can display current image in update mode
43 $this->assign('thumbnailUrl', $defaults['thumbnailUrl']);
44 }
45 else {
46 $defaults['imageOption'] = 'noImage';
47 }
48 if (isset($tempDefaults['thumbnail']) && isset($tempDefaults['image'])) {
49 $this->assign('thumbURL', $tempDefaults['thumbnail']);
50 $this->assign('imageURL', $tempDefaults['image']);
51 }
52 if (isset($tempDefaults['period_type'])) {
53 $this->assign('showSubscriptions', TRUE);
54 }
55 }
56
57 return $defaults;
58 }
59
60 /**
61 * Build the form object.
62 */
63 public function buildQuickForm() {
64 parent::buildQuickForm();
65 $this->setPageTitle(ts('Premium Product'));
66
67 if ($this->_action & CRM_Core_Action::PREVIEW) {
68 CRM_Contribute_BAO_Premium::buildPremiumPreviewBlock($this, $this->_id);
69 return;
70 }
71
72 if ($this->_action & CRM_Core_Action::DELETE) {
73 return;
74 }
75
76 $this->applyFilter('__ALL__', 'trim');
77 $this->add('text', 'name', ts('Name'), CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Product', 'name'), TRUE);
78 $this->addRule('name', ts('A product with this name already exists. Please select another name.'), 'objectExists', [
79 'CRM_Contribute_DAO_Product',
80 $this->_id,
81 ]);
82 $this->add('text', 'sku', ts('SKU'), CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Product', 'sku'));
83
84 $this->add('textarea', 'description', ts('Description'), 'rows=3, cols=60');
85
86 $image['image'] = $this->createElement('radio', NULL, NULL, ts('Upload from my computer'), 'image', 'onclick="add_upload_file_block(\'image\');');
87 $image['thumbnail'] = $this->createElement('radio', NULL, NULL, ts('Display image and thumbnail from these locations on the web:'), 'thumbnail', 'onclick="add_upload_file_block(\'thumbnail\');');
88 $image['default_image'] = $this->createElement('radio', NULL, NULL, ts('Use default image'), 'default_image', 'onclick="add_upload_file_block(\'default\');');
89 $image['noImage'] = $this->createElement('radio', NULL, NULL, ts('Do not display an image'), 'noImage', 'onclick="add_upload_file_block(\'noImage\');');
90
91 $this->addGroup($image, 'imageOption', ts('Premium Image'));
92 $this->addRule('imageOption', ts('Please select an option for the premium image.'), 'required');
93
94 $this->addElement('text', 'imageUrl', ts('Image URL'));
95 $this->addElement('text', 'thumbnailUrl', ts('Thumbnail URL'));
96
97 $this->add('file', 'uploadFile', ts('Image File Name'), 'onChange="select_option();"');
98
99 $this->add('text', 'price', ts('Market Value'), CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Product', 'price'), TRUE);
100 $this->addRule('price', ts('Please enter the Market Value for this product.'), 'money');
101
102 $this->add('text', 'cost', ts('Actual Cost of Product'), CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Product', 'cost'));
103 $this->addRule('price', ts('Please enter the Actual Cost of Product.'), 'money');
104
105 $this->add('text', 'min_contribution', ts('Minimum Contribution Amount'), CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Product', 'min_contribution'), TRUE);
106 $this->addRule('min_contribution', ts('Please enter a monetary value for the Minimum Contribution Amount.'), 'money');
107
108 $this->add('textarea', 'options', ts('Options'), 'rows=3, cols=60');
109
110 $this->add('select', 'period_type', ts('Period Type'), [
111 '' => '- select -',
112 'rolling' => 'Rolling',
113 'fixed' => 'Fixed',
114 ]);
115
116 $this->add('text', 'fixed_period_start_day', ts('Fixed Period Start Day'), CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Product', 'fixed_period_start_day'));
117
118 $this->add('Select', 'duration_unit', ts('Duration Unit'), ['' => '- select period -'] + CRM_Core_SelectValues::getPremiumUnits());
119
120 $this->add('text', 'duration_interval', ts('Duration'), CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Product', 'duration_interval'));
121
122 $this->add('Select', 'frequency_unit', ts('Frequency Unit'), ['' => '- select period -'] + CRM_Core_SelectValues::getPremiumUnits());
123
124 $this->add('text', 'frequency_interval', ts('Frequency'), CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Product', 'frequency_interval'));
125
126 //Financial Type CRM-11106
127 $financialType = CRM_Contribute_PseudoConstant::financialType();
128 $premiumFinancialType = [];
129 CRM_Core_PseudoConstant::populate(
130 $premiumFinancialType,
131 'CRM_Financial_DAO_EntityFinancialAccount',
132 $all = TRUE,
133 $retrieve = 'entity_id',
134 $filter = NULL,
135 'account_relationship = 8'
136 );
137
138 $costFinancialType = [];
139 CRM_Core_PseudoConstant::populate(
140 $costFinancialType,
141 'CRM_Financial_DAO_EntityFinancialAccount',
142 $all = TRUE,
143 $retrieve = 'entity_id',
144 $filter = NULL,
145 'account_relationship = 7'
146 );
147 $productFinancialType = array_intersect($costFinancialType, $premiumFinancialType);
148 foreach ($financialType as $key => $financialTypeName) {
149 if (!in_array($key, $productFinancialType)) {
150 unset($financialType[$key]);
151 }
152 }
153 if (count($financialType)) {
154 $this->assign('financialType', $financialType);
155 }
156 $this->add(
157 'select',
158 'financial_type_id',
159 ts('Financial Type'),
160 ['' => ts('- select -')] + $financialType
161 );
162
163 $this->add('checkbox', 'is_active', ts('Enabled?'));
164
165 $this->addFormRule(['CRM_Contribute_Form_ManagePremiums', 'formRule']);
166
167 $this->addButtons([
168 [
169 'type' => 'upload',
170 'name' => ts('Save'),
171 'isDefault' => TRUE,
172 ],
173 [
174 'type' => 'cancel',
175 'name' => ts('Cancel'),
176 ],
177 ]);
178 $this->assign('productId', $this->_id);
179 }
180
181 /**
182 * Function for validation.
183 *
184 * @param array $params
185 * (ref.) an assoc array of name/value pairs.
186 * @param $files
187 *
188 * @return bool|array
189 * mixed true or array of errors
190 */
191 public static function formRule($params, $files) {
192
193 // If choosing to upload an image, then an image must be provided
194 if (CRM_Utils_Array::value('imageOption', $params) == 'image'
195 && empty($files['uploadFile']['name'])
196 ) {
197 $errors['uploadFile'] = ts('A file must be selected');
198 }
199
200 // If choosing to use image URLs, then both URLs must be present
201 if (CRM_Utils_Array::value('imageOption', $params) == 'thumbnail') {
202 if (!$params['imageUrl']) {
203 $errors['imageUrl'] = ts('Image URL is Required');
204 }
205 if (!$params['thumbnailUrl']) {
206 $errors['thumbnailUrl'] = ts('Thumbnail URL is Required');
207 }
208 }
209
210 // CRM-13231 financial type required if product has cost
211 if (!empty($params['cost']) && empty($params['financial_type_id'])) {
212 $errors['financial_type_id'] = ts('Financial Type is required for product having cost.');
213 }
214
215 if (!$params['period_type']) {
216 if ($params['fixed_period_start_day'] || $params['duration_unit'] || $params['duration_interval'] ||
217 $params['frequency_unit'] || $params['frequency_interval']
218 ) {
219 $errors['period_type'] = ts('Please select the Period Type for this subscription or service.');
220 }
221 }
222
223 if ($params['period_type'] == 'fixed' && !$params['fixed_period_start_day']) {
224 $errors['fixed_period_start_day'] = ts('Please enter a Fixed Period Start Day for this subscription or service.');
225 }
226
227 if ($params['duration_unit'] && !$params['duration_interval']) {
228 $errors['duration_interval'] = ts('Please enter the Duration Interval for this subscription or service.');
229 }
230
231 if ($params['duration_interval'] && !$params['duration_unit']) {
232 $errors['duration_unit'] = ts('Please enter the Duration Unit for this subscription or service.');
233 }
234
235 if ($params['frequency_interval'] && !$params['frequency_unit']) {
236 $errors['frequency_unit'] = ts('Please enter the Frequency Unit for this subscription or service.');
237 }
238
239 if ($params['frequency_unit'] && !$params['frequency_interval']) {
240 $errors['frequency_interval'] = ts('Please enter the Frequency Interval for this subscription or service.');
241 }
242
243 return empty($errors) ? TRUE : $errors;
244 }
245
246 /**
247 * Process the form submission.
248 */
249 public function postProcess() {
250 // If previewing, don't do any post-processing
251 if ($this->_action & CRM_Core_Action::PREVIEW) {
252 return;
253 }
254
255 // If deleting, then only delete and skip the rest of the post-processing
256 if ($this->_action & CRM_Core_Action::DELETE) {
257 try {
258 CRM_Contribute_BAO_Product::del($this->_id);
259 }
260 catch (CRM_Core_Exception $e) {
261 $message = ts("This Premium is linked to an <a href='%1'>Online Contribution page</a>. Please remove it before deleting this Premium.", [1 => CRM_Utils_System::url('civicrm/admin/contribute', 'reset=1')]);
262 CRM_Core_Session::setStatus($message, ts('Cannot delete Premium'), 'error');
263 CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/admin/contribute/managePremiums', 'reset=1&action=browse'));
264 return;
265 }
266 CRM_Core_Session::setStatus(
267 ts('Selected Premium Product type has been deleted.'),
268 ts('Deleted'), 'info');
269 return;
270 }
271
272 $params = $this->controller->exportValues($this->_name);
273
274 // Clean the the money fields
275 $moneyFields = ['cost', 'price', 'min_contribution'];
276 foreach ($moneyFields as $field) {
277 $params[$field] = CRM_Utils_Rule::cleanMoney($params[$field]);
278 }
279
280 // If we're updating, we need to pass in the premium product Id
281 if ($this->_action & CRM_Core_Action::UPDATE) {
282 $params['id'] = $this->_id;
283 }
284
285 $this->_processImages($params);
286
287 // Save the premium product to database
288 $premium = CRM_Contribute_BAO_Product::create($params);
289
290 CRM_Core_Session::setStatus(
291 ts("The Premium '%1' has been saved.", [1 => $premium->name]),
292 ts('Saved'), 'success');
293 }
294
295 /**
296 * Look at $params to find form info about images. Manipulate images if
297 * necessary. Then alter $params to point to the newly manipulated images.
298 *
299 * @param array $params
300 */
301 protected function _processImages(&$params) {
302 $defaults = [
303 'imageOption' => 'noImage',
304 'uploadFile' => ['name' => ''],
305 'image' => '',
306 'thumbnail' => '',
307 'imageUrl' => '',
308 'thumbnailUrl' => '',
309 ];
310 $params = array_merge($defaults, $params);
311
312 // User is uploading an image
313 if ($params['imageOption'] == 'image') {
314 $imageFile = $params['uploadFile']['name'];
315 try {
316 $params['image'] = CRM_Utils_File::resizeImage($imageFile, 200, 200, "_full");
317 $params['thumbnail'] = CRM_Utils_File::resizeImage($imageFile, 50, 50, "_thumb");
318 }
319 catch (CRM_Core_Exception $e) {
320 $params['image'] = self::_defaultImage();
321 $params['thumbnail'] = self::_defaultThumbnail();
322 $msg = ts('The product has been configured to use a default image.');
323 CRM_Core_Session::setStatus($e->getMessage() . " $msg", ts('Notice'), 'alert');
324 }
325 }
326
327 // User is specifying existing URLs for the images
328 elseif ($params['imageOption'] == 'thumbnail') {
329 $params['image'] = $params['imageUrl'];
330 $params['thumbnail'] = $params['thumbnailUrl'];
331 }
332
333 // User wants a default image
334 elseif ($params['imageOption'] == 'default_image') {
335 $params['image'] = self::_defaultImage();
336 $params['thumbnail'] = self::_defaultThumbnail();
337 }
338 }
339
340 /**
341 * Returns the path to the default premium image
342 * @return string
343 */
344 protected static function _defaultImage() {
345 $config = CRM_Core_Config::singleton();
346 return $config->resourceBase . 'i/contribute/default_premium.jpg';
347 }
348
349 /**
350 * Returns the path to the default premium thumbnail
351 * @return string
352 */
353 protected static function _defaultThumbnail() {
354 $config = CRM_Core_Config::singleton();
355 return $config->resourceBase . 'i/contribute/default_premium_thumb.jpg';
356 }
357
358 }