Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
95cdcc0f | 19 | * This class generates form components for Premiums. |
6a488035 TO |
20 | */ |
21 | class CRM_Contribute_Form_ManagePremiums extends CRM_Contribute_Form { | |
22 | ||
23 | /** | |
07f8d162 | 24 | * Pre process the form. |
6a488035 TO |
25 | */ |
26 | public function preProcess() { | |
27 | parent::preProcess(); | |
28 | } | |
29 | ||
30 | /** | |
95cdcc0f | 31 | * Set default values for the form. |
6a488035 | 32 | */ |
00be9182 | 33 | public function setDefaultValues() { |
6a488035 TO |
34 | $defaults = parent::setDefaultValues(); |
35 | if ($this->_id) { | |
be2fb01f | 36 | $params = ['id' => $this->_id]; |
37828d4f | 37 | CRM_Contribute_BAO_Product::retrieve($params, $tempDefaults); |
6a488035 TO |
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 | /** | |
fe482240 | 61 | * Build the form object. |
6a488035 TO |
62 | */ |
63 | public function buildQuickForm() { | |
481a74f4 | 64 | parent::buildQuickForm(); |
e2046b33 | 65 | $this->setPageTitle(ts('Premium Product')); |
6a488035 TO |
66 | |
67 | if ($this->_action & CRM_Core_Action::PREVIEW) { | |
68 | CRM_Contribute_BAO_Premium::buildPremiumPreviewBlock($this, $this->_id); | |
6a488035 TO |
69 | return; |
70 | } | |
71 | ||
72 | if ($this->_action & CRM_Core_Action::DELETE) { | |
6a488035 TO |
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); | |
be2fb01f | 78 | $this->addRule('name', ts('A product with this name already exists. Please select another name.'), 'objectExists', [ |
1330f57a SL |
79 | 'CRM_Contribute_DAO_Product', |
80 | $this->_id, | |
81 | ]); | |
6a488035 TO |
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')); | |
6a488035 | 95 | $this->addElement('text', 'thumbnailUrl', ts('Thumbnail URL')); |
6a488035 TO |
96 | |
97 | $this->add('file', 'uploadFile', ts('Image File Name'), 'onChange="select_option();"'); | |
98 | ||
6a488035 TO |
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 | ||
be2fb01f | 110 | $this->add('select', 'period_type', ts('Period Type'), [ |
1330f57a SL |
111 | '' => '- select -', |
112 | 'rolling' => 'Rolling', | |
113 | 'fixed' => 'Fixed', | |
114 | ]); | |
6a488035 TO |
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 | ||
be2fb01f | 118 | $this->add('Select', 'duration_unit', ts('Duration Unit'), ['' => '- select period -'] + CRM_Core_SelectValues::getPremiumUnits()); |
6a488035 TO |
119 | |
120 | $this->add('text', 'duration_interval', ts('Duration'), CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Product', 'duration_interval')); | |
121 | ||
be2fb01f | 122 | $this->add('Select', 'frequency_unit', ts('Frequency Unit'), ['' => '- select period -'] + CRM_Core_SelectValues::getPremiumUnits()); |
6a488035 TO |
123 | |
124 | $this->add('text', 'frequency_interval', ts('Frequency'), CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Product', 'frequency_interval')); | |
665e5ec7 | 125 | |
6a488035 | 126 | //Financial Type CRM-11106 |
481a74f4 | 127 | $financialType = CRM_Contribute_PseudoConstant::financialType(); |
be2fb01f | 128 | $premiumFinancialType = []; |
6a488035 TO |
129 | CRM_Core_PseudoConstant::populate( |
130 | $premiumFinancialType, | |
131 | 'CRM_Financial_DAO_EntityFinancialAccount', | |
874c9be7 | 132 | $all = TRUE, |
665e5ec7 | 133 | $retrieve = 'entity_id', |
874c9be7 | 134 | $filter = NULL, |
665e5ec7 | 135 | 'account_relationship = 8' |
6a488035 | 136 | ); |
665e5ec7 | 137 | |
be2fb01f | 138 | $costFinancialType = []; |
6a488035 TO |
139 | CRM_Core_PseudoConstant::populate( |
140 | $costFinancialType, | |
141 | 'CRM_Financial_DAO_EntityFinancialAccount', | |
874c9be7 | 142 | $all = TRUE, |
665e5ec7 | 143 | $retrieve = 'entity_id', |
874c9be7 | 144 | $filter = NULL, |
665e5ec7 | 145 | 'account_relationship = 7' |
6a488035 TO |
146 | ); |
147 | $productFinancialType = array_intersect($costFinancialType, $premiumFinancialType); | |
481a74f4 TO |
148 | foreach ($financialType as $key => $financialTypeName) { |
149 | if (!in_array($key, $productFinancialType)) { | |
150 | unset($financialType[$key]); | |
874c9be7 | 151 | } |
6a488035 | 152 | } |
481a74f4 TO |
153 | if (count($financialType)) { |
154 | $this->assign('financialType', $financialType); | |
665e5ec7 | 155 | } |
6a488035 | 156 | $this->add( |
665e5ec7 | 157 | 'select', |
158 | 'financial_type_id', | |
481a74f4 | 159 | ts('Financial Type'), |
be2fb01f | 160 | ['' => ts('- select -')] + $financialType |
6a488035 | 161 | ); |
665e5ec7 | 162 | |
6a488035 TO |
163 | $this->add('checkbox', 'is_active', ts('Enabled?')); |
164 | ||
be2fb01f | 165 | $this->addFormRule(['CRM_Contribute_Form_ManagePremiums', 'formRule']); |
6a488035 | 166 | |
be2fb01f | 167 | $this->addButtons([ |
1330f57a SL |
168 | [ |
169 | 'type' => 'upload', | |
170 | 'name' => ts('Save'), | |
171 | 'isDefault' => TRUE, | |
172 | ], | |
173 | [ | |
174 | 'type' => 'cancel', | |
175 | 'name' => ts('Cancel'), | |
176 | ], | |
177 | ]); | |
6a488035 TO |
178 | $this->assign('productId', $this->_id); |
179 | } | |
180 | ||
181 | /** | |
fe482240 | 182 | * Function for validation. |
6a488035 | 183 | * |
014c4014 TO |
184 | * @param array $params |
185 | * (ref.) an assoc array of name/value pairs. | |
dd244018 EM |
186 | * @param $files |
187 | * | |
72b3a70c CW |
188 | * @return bool|array |
189 | * mixed true or array of errors | |
6a488035 | 190 | */ |
fcc5922d | 191 | public static function formRule($params, $files) { |
aa4ad33a SM |
192 | |
193 | // If choosing to upload an image, then an image must be provided | |
0d176017 | 194 | if (CRM_Utils_Array::value('imageOption', $params) == 'image' |
bb80d2f9 | 195 | && empty($files['uploadFile']['name']) |
aa4ad33a SM |
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 | |
0d176017 | 201 | if (CRM_Utils_Array::value('imageOption', $params) == 'thumbnail') { |
aa4ad33a SM |
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'); | |
6a488035 TO |
207 | } |
208 | } | |
aa4ad33a | 209 | |
3b67ab13 | 210 | // CRM-13231 financial type required if product has cost |
8cc574cf | 211 | if (!empty($params['cost']) && empty($params['financial_type_id'])) { |
3b67ab13 PN |
212 | $errors['financial_type_id'] = ts('Financial Type is required for product having cost.'); |
213 | } | |
6a488035 TO |
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 | ||
6a488035 TO |
243 | return empty($errors) ? TRUE : $errors; |
244 | } | |
245 | ||
246 | /** | |
fe482240 | 247 | * Process the form submission. |
6a488035 TO |
248 | */ |
249 | public function postProcess() { | |
5459a6da | 250 | // If previewing, don't do any post-processing |
6a488035 TO |
251 | if ($this->_action & CRM_Core_Action::PREVIEW) { |
252 | return; | |
253 | } | |
254 | ||
5459a6da | 255 | // If deleting, then only delete and skip the rest of the post-processing |
6a488035 | 256 | if ($this->_action & CRM_Core_Action::DELETE) { |
a275d4b6 MW |
257 | try { |
258 | CRM_Contribute_BAO_Product::del($this->_id); | |
259 | } | |
260 | catch (CRM_Core_Exception $e) { | |
be2fb01f | 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')]); |
a275d4b6 MW |
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 | } | |
5459a6da SM |
266 | CRM_Core_Session::setStatus( |
267 | ts('Selected Premium Product type has been deleted.'), | |
268 | ts('Deleted'), 'info'); | |
269 | return; | |
6a488035 | 270 | } |
6a488035 | 271 | |
5459a6da | 272 | $params = $this->controller->exportValues($this->_name); |
6a488035 | 273 | |
5459a6da | 274 | // Clean the the money fields |
be2fb01f | 275 | $moneyFields = ['cost', 'price', 'min_contribution']; |
5459a6da SM |
276 | foreach ($moneyFields as $field) { |
277 | $params[$field] = CRM_Utils_Rule::cleanMoney($params[$field]); | |
278 | } | |
279 | ||
a275d4b6 | 280 | // If we're updating, we need to pass in the premium product Id |
5459a6da | 281 | if ($this->_action & CRM_Core_Action::UPDATE) { |
ff93a82e | 282 | $params['id'] = $this->_id; |
5459a6da SM |
283 | } |
284 | ||
285 | $this->_processImages($params); | |
6a488035 | 286 | |
a275d4b6 | 287 | // Save the premium product to database |
32f27499 | 288 | $premium = CRM_Contribute_BAO_Product::create($params); |
5459a6da SM |
289 | |
290 | CRM_Core_Session::setStatus( | |
be2fb01f | 291 | ts("The Premium '%1' has been saved.", [1 => $premium->name]), |
5459a6da SM |
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) { | |
be2fb01f | 302 | $defaults = [ |
5459a6da | 303 | 'imageOption' => 'noImage', |
be2fb01f | 304 | 'uploadFile' => ['name' => ''], |
5459a6da SM |
305 | 'image' => '', |
306 | 'thumbnail' => '', | |
307 | 'imageUrl' => '', | |
308 | 'thumbnailUrl' => '', | |
be2fb01f | 309 | ]; |
5459a6da SM |
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"); | |
6a488035 | 318 | } |
5459a6da SM |
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'); | |
6a488035 TO |
324 | } |
325 | } | |
5459a6da SM |
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'; | |
6a488035 TO |
356 | } |
357 | ||
6a488035 | 358 | } |