Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This class generates form components for Premiums | |
38 | * | |
39 | */ | |
40 | class CRM_Contribute_Form_ManagePremiums extends CRM_Contribute_Form { | |
41 | ||
42 | /** | |
43 | * Function to pre process the form | |
44 | * | |
45 | * @access public | |
46 | * | |
355ba699 | 47 | * @return void |
6a488035 TO |
48 | */ |
49 | public function preProcess() { | |
50 | parent::preProcess(); | |
51 | } | |
52 | ||
53 | /** | |
54 | * This function sets the default values for the form. Manage Premiums that in edit/view mode | |
55 | * the default values are retrieved from the database | |
56 | * | |
57 | * @access public | |
58 | * | |
355ba699 | 59 | * @return void |
6a488035 TO |
60 | */ |
61 | function setDefaultValues() { | |
62 | $defaults = parent::setDefaultValues(); | |
63 | if ($this->_id) { | |
64 | $params = array('id' => $this->_id); | |
65 | CRM_Contribute_BAO_ManagePremiums::retrieve($params, $tempDefaults); | |
66 | $imageUrl = (isset($tempDefaults['image'])) ? $tempDefaults['image'] : ""; | |
67 | if (isset($tempDefaults['image']) && isset($tempDefaults['thumbnail'])) { | |
68 | $defaults['imageUrl'] = $tempDefaults['image']; | |
69 | $defaults['thumbnailUrl'] = $tempDefaults['thumbnail']; | |
70 | $defaults['imageOption'] = 'thumbnail'; | |
71 | // assign thumbnailUrl to template so we can display current image in update mode | |
72 | $this->assign('thumbnailUrl', $defaults['thumbnailUrl']); | |
73 | } | |
74 | else { | |
75 | $defaults['imageOption'] = 'noImage'; | |
76 | } | |
77 | if (isset($tempDefaults['thumbnail']) && isset($tempDefaults['image'])) { | |
78 | $this->assign('thumbURL', $tempDefaults['thumbnail']); | |
79 | $this->assign('imageURL', $tempDefaults['image']); | |
80 | } | |
81 | if (isset($tempDefaults['period_type'])) { | |
82 | $this->assign('showSubscriptions', TRUE); | |
83 | } | |
84 | } | |
85 | ||
86 | return $defaults; | |
87 | } | |
88 | ||
89 | /** | |
90 | * Function to build the form | |
91 | * | |
355ba699 | 92 | * @return void |
6a488035 TO |
93 | * @access public |
94 | */ | |
95 | public function buildQuickForm() { | |
96 | //parent::buildQuickForm( ); | |
97 | ||
98 | if ($this->_action & CRM_Core_Action::PREVIEW) { | |
99 | CRM_Contribute_BAO_Premium::buildPremiumPreviewBlock($this, $this->_id); | |
100 | ||
101 | $this->addButtons(array( | |
102 | array( | |
103 | 'type' => 'next', | |
104 | 'name' => ts('Done with Preview'), | |
105 | 'isDefault' => TRUE, | |
106 | ), | |
107 | ) | |
108 | ); | |
109 | ||
110 | return; | |
111 | } | |
112 | ||
113 | if ($this->_action & CRM_Core_Action::DELETE) { | |
114 | $this->addButtons(array( | |
115 | array( | |
116 | 'type' => 'next', | |
117 | 'name' => ts('Delete'), | |
118 | 'isDefault' => TRUE, | |
119 | ), | |
120 | array( | |
121 | 'type' => 'cancel', | |
122 | 'name' => ts('Cancel'), | |
123 | ), | |
124 | ) | |
125 | ); | |
126 | return; | |
127 | } | |
128 | ||
129 | $this->applyFilter('__ALL__', 'trim'); | |
130 | $this->add('text', 'name', ts('Name'), CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Product', 'name'), TRUE); | |
131 | $this->addRule('name', ts('A product with this name already exists. Please select another name.'), 'objectExists', array('CRM_Contribute_DAO_Product', $this->_id)); | |
132 | $this->add('text', 'sku', ts('SKU'), CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Product', 'sku')); | |
133 | ||
134 | $this->add('textarea', 'description', ts('Description'), 'rows=3, cols=60'); | |
135 | ||
136 | $image['image'] = $this->createElement('radio', NULL, NULL, ts('Upload from my computer'), 'image', 'onclick="add_upload_file_block(\'image\');'); | |
137 | $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\');'); | |
138 | $image['default_image'] = $this->createElement('radio', NULL, NULL, ts('Use default image'), 'default_image', 'onclick="add_upload_file_block(\'default\');'); | |
139 | $image['noImage'] = $this->createElement('radio', NULL, NULL, ts('Do not display an image'), 'noImage', 'onclick="add_upload_file_block(\'noImage\');'); | |
140 | ||
141 | $this->addGroup($image, 'imageOption', ts('Premium Image')); | |
142 | $this->addRule('imageOption', ts('Please select an option for the premium image.'), 'required'); | |
143 | ||
144 | $this->addElement('text', 'imageUrl', ts('Image URL')); | |
6a488035 | 145 | $this->addElement('text', 'thumbnailUrl', ts('Thumbnail URL')); |
6a488035 TO |
146 | |
147 | $this->add('file', 'uploadFile', ts('Image File Name'), 'onChange="select_option();"'); | |
148 | ||
149 | ||
150 | $this->add('text', 'price', ts('Market Value'), CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Product', 'price'), TRUE); | |
151 | $this->addRule('price', ts('Please enter the Market Value for this product.'), 'money'); | |
152 | ||
153 | $this->add('text', 'cost', ts('Actual Cost of Product'), CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Product', 'cost')); | |
154 | $this->addRule('price', ts('Please enter the Actual Cost of Product.'), 'money'); | |
155 | ||
156 | $this->add('text', 'min_contribution', ts('Minimum Contribution Amount'), CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Product', 'min_contribution'), TRUE); | |
157 | $this->addRule('min_contribution', ts('Please enter a monetary value for the Minimum Contribution Amount.'), 'money'); | |
158 | ||
159 | $this->add('textarea', 'options', ts('Options'), 'rows=3, cols=60'); | |
160 | ||
161 | $this->add('select', 'period_type', ts('Period Type'), array('' => '- select -', 'rolling' => 'Rolling', 'fixed' => 'Fixed')); | |
162 | ||
163 | $this->add('text', 'fixed_period_start_day', ts('Fixed Period Start Day'), CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Product', 'fixed_period_start_day')); | |
164 | ||
f80ce889 | 165 | $this->add('Select', 'duration_unit', ts('Duration Unit'), array('' => '- select period -') + CRM_Core_SelectValues::getPremiumUnits()); |
6a488035 TO |
166 | |
167 | $this->add('text', 'duration_interval', ts('Duration'), CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Product', 'duration_interval')); | |
168 | ||
f80ce889 | 169 | $this->add('Select', 'frequency_unit', ts('Frequency Unit'), array('' => '- select period -') + CRM_Core_SelectValues::getPremiumUnits()); |
6a488035 TO |
170 | |
171 | $this->add('text', 'frequency_interval', ts('Frequency'), CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Product', 'frequency_interval')); | |
665e5ec7 | 172 | |
6a488035 TO |
173 | //Financial Type CRM-11106 |
174 | $financialType = CRM_Contribute_PseudoConstant::financialType( ); | |
175 | $premiumFinancialType = array(); | |
176 | CRM_Core_PseudoConstant::populate( | |
177 | $premiumFinancialType, | |
178 | 'CRM_Financial_DAO_EntityFinancialAccount', | |
665e5ec7 | 179 | $all = True, |
180 | $retrieve = 'entity_id', | |
181 | $filter = null, | |
182 | 'account_relationship = 8' | |
6a488035 | 183 | ); |
665e5ec7 | 184 | |
6a488035 TO |
185 | $costFinancialType = array(); |
186 | CRM_Core_PseudoConstant::populate( | |
187 | $costFinancialType, | |
188 | 'CRM_Financial_DAO_EntityFinancialAccount', | |
665e5ec7 | 189 | $all = True, |
190 | $retrieve = 'entity_id', | |
191 | $filter = null, | |
192 | 'account_relationship = 7' | |
6a488035 TO |
193 | ); |
194 | $productFinancialType = array_intersect($costFinancialType, $premiumFinancialType); | |
195 | foreach( $financialType as $key => $financialTypeName ){ | |
196 | if(!in_array( $key, $productFinancialType)) | |
197 | unset( $financialType[$key] ); | |
198 | } | |
199 | if( count( $financialType ) ){ | |
200 | $this->assign( 'financialType', $financialType ); | |
665e5ec7 | 201 | } |
6a488035 | 202 | $this->add( |
665e5ec7 | 203 | 'select', |
204 | 'financial_type_id', | |
205 | ts( 'Financial Type' ), | |
6a488035 TO |
206 | array(''=>ts('- select -')) + $financialType |
207 | ); | |
665e5ec7 | 208 | |
6a488035 TO |
209 | $this->add('checkbox', 'is_active', ts('Enabled?')); |
210 | ||
211 | $this->addFormRule(array('CRM_Contribute_Form_ManagePremiums', 'formRule')); | |
212 | ||
213 | $this->addButtons(array( | |
214 | array( | |
215 | 'type' => 'upload', | |
216 | 'name' => ts('Save'), | |
217 | 'isDefault' => TRUE, | |
218 | ), | |
219 | array( | |
220 | 'type' => 'cancel', | |
221 | 'name' => ts('Cancel'), | |
222 | ), | |
223 | ) | |
224 | ); | |
225 | ||
226 | $this->assign('productId', $this->_id); | |
227 | } | |
228 | ||
229 | /** | |
230 | * Function for validation | |
231 | * | |
232 | * @param array $params (ref.) an assoc array of name/value pairs | |
233 | * | |
234 | * @return mixed true or array of errors | |
235 | * @access public | |
236 | * @static | |
237 | */ | |
fcc5922d | 238 | public static function formRule($params, $files) { |
6a488035 TO |
239 | if (isset($params['imageOption'])) { |
240 | if ($params['imageOption'] == 'thumbnail') { | |
241 | if (!$params['imageUrl']) { | |
3b67ab13 | 242 | $errors['imageUrl'] = ts('Image URL is Required'); |
6a488035 TO |
243 | } |
244 | if (!$params['thumbnailUrl']) { | |
3b67ab13 | 245 | $errors['thumbnailUrl'] = ts('Thumbnail URL is Required'); |
6a488035 TO |
246 | } |
247 | } | |
248 | } | |
3b67ab13 | 249 | // CRM-13231 financial type required if product has cost |
8cc574cf | 250 | if (!empty($params['cost']) && empty($params['financial_type_id'])) { |
3b67ab13 PN |
251 | $errors['financial_type_id'] = ts('Financial Type is required for product having cost.'); |
252 | } | |
6a488035 TO |
253 | $fileLocation = $files['uploadFile']['tmp_name']; |
254 | if ($fileLocation != "") { | |
255 | list($width, $height) = getimagesize($fileLocation); | |
256 | ||
257 | if (($width < 80 || $width > 500) || ($height < 80 || $height > 500)) { | |
258 | //$errors ['uploadFile'] = "Please Enter files with dimensions between 80 x 80 and 500 x 500," . " Dimensions of this file is ".$width."X".$height; | |
259 | } | |
260 | } | |
261 | ||
262 | if (!$params['period_type']) { | |
263 | if ($params['fixed_period_start_day'] || $params['duration_unit'] || $params['duration_interval'] || | |
264 | $params['frequency_unit'] || $params['frequency_interval'] | |
265 | ) { | |
266 | $errors['period_type'] = ts('Please select the Period Type for this subscription or service.'); | |
267 | } | |
268 | } | |
269 | ||
270 | if ($params['period_type'] == 'fixed' && !$params['fixed_period_start_day']) { | |
271 | $errors['fixed_period_start_day'] = ts('Please enter a Fixed Period Start Day for this subscription or service.'); | |
272 | } | |
273 | ||
274 | if ($params['duration_unit'] && !$params['duration_interval']) { | |
275 | $errors['duration_interval'] = ts('Please enter the Duration Interval for this subscription or service.'); | |
276 | } | |
277 | ||
278 | if ($params['duration_interval'] && !$params['duration_unit']) { | |
279 | $errors['duration_unit'] = ts('Please enter the Duration Unit for this subscription or service.'); | |
280 | } | |
281 | ||
282 | if ($params['frequency_interval'] && !$params['frequency_unit']) { | |
283 | $errors['frequency_unit'] = ts('Please enter the Frequency Unit for this subscription or service.'); | |
284 | } | |
285 | ||
286 | if ($params['frequency_unit'] && !$params['frequency_interval']) { | |
287 | $errors['frequency_interval'] = ts('Please enter the Frequency Interval for this subscription or service.'); | |
288 | } | |
289 | ||
290 | ||
291 | return empty($errors) ? TRUE : $errors; | |
292 | } | |
293 | ||
294 | /** | |
295 | * Function to process the form | |
296 | * | |
297 | * @access public | |
298 | * | |
355ba699 | 299 | * @return void |
6a488035 TO |
300 | */ |
301 | public function postProcess() { | |
302 | ||
303 | if ($this->_action & CRM_Core_Action::PREVIEW) { | |
304 | return; | |
305 | } | |
306 | ||
307 | if ($this->_action & CRM_Core_Action::DELETE) { | |
308 | CRM_Contribute_BAO_ManagePremiums::del($this->_id); | |
309 | CRM_Core_Session::setStatus(ts('Selected Premium Product type has been deleted.'), ts('Deleted'), 'info'); | |
310 | } | |
311 | else { | |
312 | $params = $this->controller->exportValues($this->_name); | |
313 | $imageFile = CRM_Utils_Array::value('uploadFile', $params); | |
314 | $imageFile = $imageFile['name']; | |
315 | ||
316 | $config = CRM_Core_Config::singleton(); | |
317 | ||
318 | $ids = array(); | |
319 | $error = FALSE; | |
320 | // store the submitted values in an array | |
321 | ||
322 | // FIX ME | |
323 | if (CRM_Utils_Array::value('imageOption', $params, FALSE)) { | |
324 | $value = CRM_Utils_Array::value('imageOption', $params, FALSE); | |
325 | if ($value == 'image') { | |
326 | ||
327 | // to check wether GD is installed or not | |
328 | $gdSupport = CRM_Utils_System::getModuleSetting('gd', 'GD Support'); | |
329 | if($gdSupport) { | |
330 | if($imageFile) { | |
331 | $error = false; | |
332 | $params['image'] = $this->_resizeImage($imageFile, "_full", 200, 200); | |
333 | $params['thumbnail'] = $this->_resizeImage($imageFile, "_thumb", 50, 50); | |
334 | } | |
335 | } else { | |
336 | $error = true; | |
337 | $params['image'] = $config->resourceBase . 'i/contribute/default_premium.jpg'; | |
338 | $params['thumbnail'] = $config->resourceBase . 'i/contribute/default_premium_thumb.jpg'; | |
339 | } | |
340 | } elseif ($value == 'thumbnail') { | |
341 | $params['image'] = $params['imageUrl']; | |
342 | $params['thumbnail'] = $params['thumbnailUrl']; | |
343 | } elseif ($value == 'default_image') { | |
344 | $url = parse_url($config->userFrameworkBaseURL); | |
345 | $params['image'] = $config->resourceBase . 'i/contribute/default_premium.jpg'; | |
346 | $params['thumbnail'] = $config->resourceBase . 'i/contribute/default_premium_thumb.jpg'; | |
347 | } else { | |
348 | $params['image'] = ""; | |
349 | $params['thumbnail'] = ""; | |
350 | } | |
351 | } | |
352 | ||
353 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
354 | $ids['premium'] = $this->_id; | |
355 | } | |
356 | ||
357 | // fix the money fields | |
358 | foreach (array( | |
359 | 'cost', 'price', 'min_contribution') as $f) { | |
360 | $params[$f] = CRM_Utils_Rule::cleanMoney($params[$f]); | |
361 | } | |
362 | ||
363 | $premium = CRM_Contribute_BAO_ManagePremiums::add($params, $ids); | |
364 | if ($error) { | |
365 | CRM_Core_Session::setStatus(ts('No thumbnail of your image was created because the GD image library is not currently compiled in your PHP installation. Product is currently configured to use default thumbnail image. If you have a local thumbnail image you can upload it separately and input the thumbnail URL by editing this premium.'), ts('Notice'), 'alert'); | |
366 | } | |
367 | else { | |
368 | CRM_Core_Session::setStatus(ts("The Premium '%1' has been saved.", array(1 => $premium->name)), ts('Saved'), 'success'); | |
369 | } | |
370 | } | |
371 | } | |
372 | ||
373 | /** | |
374 | * Resize a premium image to a different size | |
375 | * | |
376 | * @access private | |
377 | * | |
378 | * @return Path to image | |
379 | */ | |
380 | private function _resizeImage($filename, $resizedName, $width, $height) { | |
381 | // figure out the new filename | |
382 | $pathParts = pathinfo($filename); | |
383 | $newFilename = $pathParts['dirname']."/".$pathParts['filename'].$resizedName.".".$pathParts['extension']; | |
384 | ||
385 | // get image about original image | |
386 | $imageInfo = getimagesize($filename); | |
387 | $widthOrig = $imageInfo[0]; | |
388 | $heightOrig = $imageInfo[1]; | |
389 | $image = imagecreatetruecolor($width, $height); | |
390 | if($imageInfo['mime'] == 'image/gif') { | |
391 | $source = imagecreatefromgif($filename); | |
392 | } elseif($imageInfo['mime'] == 'image/png') { | |
393 | $source = imagecreatefrompng($filename); | |
394 | } else { | |
395 | $source = imagecreatefromjpeg($filename); | |
396 | } | |
397 | ||
398 | // resize | |
399 | imagecopyresized($image, $source, 0, 0, 0, 0, $width, $height, $widthOrig, $heightOrig); | |
400 | ||
401 | // save the resized image | |
402 | $fp = fopen($newFilename, 'w+'); | |
403 | ob_start(); | |
404 | ImageJPEG($image); | |
405 | $image_buffer = ob_get_contents(); | |
406 | ob_end_clean(); | |
407 | ImageDestroy($image); | |
408 | fwrite($fp, $image_buffer); | |
409 | rewind($fp); | |
410 | fclose($fp); | |
411 | ||
412 | // return the URL to link to | |
413 | $config = CRM_Core_Config::singleton(); | |
414 | return $config->imageUploadURL.basename($newFilename); | |
415 | } | |
416 | } | |
417 |