3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2013
37 * This class generates form components for Premiums
40 class CRM_Contribute_Form_ManagePremiums
extends CRM_Contribute_Form
{
43 * Function to pre process the form
49 public function preProcess() {
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
61 function setDefaultValues() {
62 $defaults = parent
::setDefaultValues();
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']);
75 $defaults['imageOption'] = 'noImage';
77 if (isset($tempDefaults['thumbnail']) && isset($tempDefaults['image'])) {
78 $this->assign('thumbURL', $tempDefaults['thumbnail']);
79 $this->assign('imageURL', $tempDefaults['image']);
81 if (isset($tempDefaults['period_type'])) {
82 $this->assign('showSubscriptions', TRUE);
90 * Function to build the form
95 public function buildQuickForm() {
96 //parent::buildQuickForm( );
98 if ($this->_action
& CRM_Core_Action
::PREVIEW
) {
99 CRM_Contribute_BAO_Premium
::buildPremiumPreviewBlock($this, $this->_id
);
101 $this->addButtons(array(
104 'name' => ts('Done with Preview'),
113 if ($this->_action
& CRM_Core_Action
::DELETE
) {
114 $this->addButtons(array(
117 'name' => ts('Delete'),
122 'name' => ts('Cancel'),
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'));
134 $this->add('textarea', 'description', ts('Description'), 'rows=3, cols=60');
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\');');
141 $this->addGroup($image, 'imageOption', ts('Premium Image'));
142 $this->addRule('imageOption', ts('Please select an option for the premium image.'), 'required');
144 $this->addElement('text', 'imageUrl', ts('Image URL'));
145 $this->addRule('imageUrl', 'Please enter the valid URL to display this image.', 'url');
146 $this->addElement('text', 'thumbnailUrl', ts('Thumbnail URL'));
147 $this->addRule('thumbnailUrl', 'Please enter the valid URL to display a thumbnail of this image.', 'url');
149 $this->add('file', 'uploadFile', ts('Image File Name'), 'onChange="select_option();"');
152 $this->add('text', 'price', ts('Market Value'), CRM_Core_DAO
::getAttribute('CRM_Contribute_DAO_Product', 'price'), TRUE);
153 $this->addRule('price', ts('Please enter the Market Value for this product.'), 'money');
155 $this->add('text', 'cost', ts('Actual Cost of Product'), CRM_Core_DAO
::getAttribute('CRM_Contribute_DAO_Product', 'cost'));
156 $this->addRule('price', ts('Please enter the Actual Cost of Product.'), 'money');
158 $this->add('text', 'min_contribution', ts('Minimum Contribution Amount'), CRM_Core_DAO
::getAttribute('CRM_Contribute_DAO_Product', 'min_contribution'), TRUE);
159 $this->addRule('min_contribution', ts('Please enter a monetary value for the Minimum Contribution Amount.'), 'money');
161 $this->add('textarea', 'options', ts('Options'), 'rows=3, cols=60');
163 $this->add('select', 'period_type', ts('Period Type'), array('' => '- select -', 'rolling' => 'Rolling', 'fixed' => 'Fixed'));
165 $this->add('text', 'fixed_period_start_day', ts('Fixed Period Start Day'), CRM_Core_DAO
::getAttribute('CRM_Contribute_DAO_Product', 'fixed_period_start_day'));
167 $this->add('Select', 'duration_unit', ts('Duration Unit'), array('' => '- select period -') + CRM_Core_SelectValues
::getPremiumUnits());
169 $this->add('text', 'duration_interval', ts('Duration'), CRM_Core_DAO
::getAttribute('CRM_Contribute_DAO_Product', 'duration_interval'));
171 $this->add('Select', 'frequency_unit', ts('Frequency Unit'), array('' => '- select period -') + CRM_Core_SelectValues
::getPremiumUnits());
173 $this->add('text', 'frequency_interval', ts('Frequency'), CRM_Core_DAO
::getAttribute('CRM_Contribute_DAO_Product', 'frequency_interval'));
175 //Financial Type CRM-11106
176 $financialType = CRM_Contribute_PseudoConstant
::financialType( );
177 $premiumFinancialType = array();
178 CRM_Core_PseudoConstant
::populate(
179 $premiumFinancialType,
180 'CRM_Financial_DAO_EntityFinancialAccount',
182 $retrieve = 'entity_id',
184 'account_relationship = 8'
187 $costFinancialType = array();
188 CRM_Core_PseudoConstant
::populate(
190 'CRM_Financial_DAO_EntityFinancialAccount',
192 $retrieve = 'entity_id',
194 'account_relationship = 7'
196 $productFinancialType = array_intersect($costFinancialType, $premiumFinancialType);
197 foreach( $financialType as $key => $financialTypeName ){
198 if(!in_array( $key, $productFinancialType))
199 unset( $financialType[$key] );
201 if( count( $financialType ) ){
202 $this->assign( 'financialType', $financialType );
207 ts( 'Financial Type' ),
208 array(''=>ts('- select -')) +
$financialType
211 $this->add('checkbox', 'is_active', ts('Enabled?'));
213 $this->addFormRule(array('CRM_Contribute_Form_ManagePremiums', 'formRule'));
215 $this->addButtons(array(
218 'name' => ts('Save'),
223 'name' => ts('Cancel'),
228 $this->assign('productId', $this->_id
);
232 * Function for validation
234 * @param array $params (ref.) an assoc array of name/value pairs
236 * @return mixed true or array of errors
240 public static function formRule($params, $files) {
241 if (isset($params['imageOption'])) {
242 if ($params['imageOption'] == 'thumbnail') {
243 if (!$params['imageUrl']) {
244 $errors['imageUrl'] = ts('Image URL is Required');
246 if (!$params['thumbnailUrl']) {
247 $errors['thumbnailUrl'] = ts('Thumbnail URL is Required');
251 // CRM-13231 financial type required if product has cost
252 if (!empty($params['cost']) && empty($params['financial_type_id'])) {
253 $errors['financial_type_id'] = ts('Financial Type is required for product having cost.');
255 $fileLocation = $files['uploadFile']['tmp_name'];
256 if ($fileLocation != "") {
257 list($width, $height) = getimagesize($fileLocation);
259 if (($width < 80 ||
$width > 500) ||
($height < 80 ||
$height > 500)) {
260 //$errors ['uploadFile'] = "Please Enter files with dimensions between 80 x 80 and 500 x 500," . " Dimensions of this file is ".$width."X".$height;
264 if (!$params['period_type']) {
265 if ($params['fixed_period_start_day'] ||
$params['duration_unit'] ||
$params['duration_interval'] ||
266 $params['frequency_unit'] ||
$params['frequency_interval']
268 $errors['period_type'] = ts('Please select the Period Type for this subscription or service.');
272 if ($params['period_type'] == 'fixed' && !$params['fixed_period_start_day']) {
273 $errors['fixed_period_start_day'] = ts('Please enter a Fixed Period Start Day for this subscription or service.');
276 if ($params['duration_unit'] && !$params['duration_interval']) {
277 $errors['duration_interval'] = ts('Please enter the Duration Interval for this subscription or service.');
280 if ($params['duration_interval'] && !$params['duration_unit']) {
281 $errors['duration_unit'] = ts('Please enter the Duration Unit for this subscription or service.');
284 if ($params['frequency_interval'] && !$params['frequency_unit']) {
285 $errors['frequency_unit'] = ts('Please enter the Frequency Unit for this subscription or service.');
288 if ($params['frequency_unit'] && !$params['frequency_interval']) {
289 $errors['frequency_interval'] = ts('Please enter the Frequency Interval for this subscription or service.');
293 return empty($errors) ?
TRUE : $errors;
297 * Function to process the form
303 public function postProcess() {
305 if ($this->_action
& CRM_Core_Action
::PREVIEW
) {
309 if ($this->_action
& CRM_Core_Action
::DELETE
) {
310 CRM_Contribute_BAO_ManagePremiums
::del($this->_id
);
311 CRM_Core_Session
::setStatus(ts('Selected Premium Product type has been deleted.'), ts('Deleted'), 'info');
314 $params = $this->controller
->exportValues($this->_name
);
315 $imageFile = CRM_Utils_Array
::value('uploadFile', $params);
316 $imageFile = $imageFile['name'];
318 $config = CRM_Core_Config
::singleton();
322 // store the submitted values in an array
325 if (CRM_Utils_Array
::value('imageOption', $params, FALSE)) {
326 $value = CRM_Utils_Array
::value('imageOption', $params, FALSE);
327 if ($value == 'image') {
329 // to check wether GD is installed or not
330 $gdSupport = CRM_Utils_System
::getModuleSetting('gd', 'GD Support');
334 $params['image'] = $this->_resizeImage($imageFile, "_full", 200, 200);
335 $params['thumbnail'] = $this->_resizeImage($imageFile, "_thumb", 50, 50);
339 $params['image'] = $config->resourceBase
. 'i/contribute/default_premium.jpg';
340 $params['thumbnail'] = $config->resourceBase
. 'i/contribute/default_premium_thumb.jpg';
342 } elseif ($value == 'thumbnail') {
343 $params['image'] = $params['imageUrl'];
344 $params['thumbnail'] = $params['thumbnailUrl'];
345 } elseif ($value == 'default_image') {
346 $url = parse_url($config->userFrameworkBaseURL
);
347 $params['image'] = $config->resourceBase
. 'i/contribute/default_premium.jpg';
348 $params['thumbnail'] = $config->resourceBase
. 'i/contribute/default_premium_thumb.jpg';
350 $params['image'] = "";
351 $params['thumbnail'] = "";
355 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
356 $ids['premium'] = $this->_id
;
359 // fix the money fields
361 'cost', 'price', 'min_contribution') as $f) {
362 $params[$f] = CRM_Utils_Rule
::cleanMoney($params[$f]);
365 $premium = CRM_Contribute_BAO_ManagePremiums
::add($params, $ids);
367 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');
370 CRM_Core_Session
::setStatus(ts("The Premium '%1' has been saved.", array(1 => $premium->name
)), ts('Saved'), 'success');
376 * Resize a premium image to a different size
380 * @return Path to image
382 private function _resizeImage($filename, $resizedName, $width, $height) {
383 // figure out the new filename
384 $pathParts = pathinfo($filename);
385 $newFilename = $pathParts['dirname']."/".$pathParts['filename'].$resizedName.".".$pathParts['extension'];
387 // get image about original image
388 $imageInfo = getimagesize($filename);
389 $widthOrig = $imageInfo[0];
390 $heightOrig = $imageInfo[1];
391 $image = imagecreatetruecolor($width, $height);
392 if($imageInfo['mime'] == 'image/gif') {
393 $source = imagecreatefromgif($filename);
394 } elseif($imageInfo['mime'] == 'image/png') {
395 $source = imagecreatefrompng($filename);
397 $source = imagecreatefromjpeg($filename);
401 imagecopyresized($image, $source, 0, 0, 0, 0, $width, $height, $widthOrig, $heightOrig);
403 // save the resized image
404 $fp = fopen($newFilename, 'w+');
407 $image_buffer = ob_get_contents();
409 ImageDestroy($image);
410 fwrite($fp, $image_buffer);
414 // return the URL to link to
415 $config = CRM_Core_Config
::singleton();
416 return $config->imageUploadURL
.basename($newFilename);