3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
37 * This class generates form components for Premiums
40 class CRM_Contribute_Form_ManagePremiums
extends CRM_Contribute_Form
{
43 * Pre process the form
48 public function preProcess() {
53 * Set default values for the form. Manage Premiums that in edit/view mode
54 * the default values are retrieved from the database
59 public function setDefaultValues() {
60 $defaults = parent
::setDefaultValues();
62 $params = array('id' => $this->_id
);
63 CRM_Contribute_BAO_ManagePremiums
::retrieve($params, $tempDefaults);
64 $imageUrl = (isset($tempDefaults['image'])) ?
$tempDefaults['image'] : "";
65 if (isset($tempDefaults['image']) && isset($tempDefaults['thumbnail'])) {
66 $defaults['imageUrl'] = $tempDefaults['image'];
67 $defaults['thumbnailUrl'] = $tempDefaults['thumbnail'];
68 $defaults['imageOption'] = 'thumbnail';
69 // assign thumbnailUrl to template so we can display current image in update mode
70 $this->assign('thumbnailUrl', $defaults['thumbnailUrl']);
73 $defaults['imageOption'] = 'noImage';
75 if (isset($tempDefaults['thumbnail']) && isset($tempDefaults['image'])) {
76 $this->assign('thumbURL', $tempDefaults['thumbnail']);
77 $this->assign('imageURL', $tempDefaults['image']);
79 if (isset($tempDefaults['period_type'])) {
80 $this->assign('showSubscriptions', TRUE);
88 * Build the form object
92 public function buildQuickForm() {
93 parent
::buildQuickForm();
94 $this->setPageTitle(ts('Premium Product'));
96 if ($this->_action
& CRM_Core_Action
::PREVIEW
) {
97 CRM_Contribute_BAO_Premium
::buildPremiumPreviewBlock($this, $this->_id
);
101 if ($this->_action
& CRM_Core_Action
::DELETE
) {
105 $this->applyFilter('__ALL__', 'trim');
106 $this->add('text', 'name', ts('Name'), CRM_Core_DAO
::getAttribute('CRM_Contribute_DAO_Product', 'name'), TRUE);
107 $this->addRule('name', ts('A product with this name already exists. Please select another name.'), 'objectExists', array(
108 'CRM_Contribute_DAO_Product',
111 $this->add('text', 'sku', ts('SKU'), CRM_Core_DAO
::getAttribute('CRM_Contribute_DAO_Product', 'sku'));
113 $this->add('textarea', 'description', ts('Description'), 'rows=3, cols=60');
115 $image['image'] = $this->createElement('radio', NULL, NULL, ts('Upload from my computer'), 'image', 'onclick="add_upload_file_block(\'image\');');
116 $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\');');
117 $image['default_image'] = $this->createElement('radio', NULL, NULL, ts('Use default image'), 'default_image', 'onclick="add_upload_file_block(\'default\');');
118 $image['noImage'] = $this->createElement('radio', NULL, NULL, ts('Do not display an image'), 'noImage', 'onclick="add_upload_file_block(\'noImage\');');
120 $this->addGroup($image, 'imageOption', ts('Premium Image'));
121 $this->addRule('imageOption', ts('Please select an option for the premium image.'), 'required');
123 $this->addElement('text', 'imageUrl', ts('Image URL'));
124 $this->addElement('text', 'thumbnailUrl', ts('Thumbnail URL'));
126 $this->add('file', 'uploadFile', ts('Image File Name'), 'onChange="select_option();"');
128 $this->add('text', 'price', ts('Market Value'), CRM_Core_DAO
::getAttribute('CRM_Contribute_DAO_Product', 'price'), TRUE);
129 $this->addRule('price', ts('Please enter the Market Value for this product.'), 'money');
131 $this->add('text', 'cost', ts('Actual Cost of Product'), CRM_Core_DAO
::getAttribute('CRM_Contribute_DAO_Product', 'cost'));
132 $this->addRule('price', ts('Please enter the Actual Cost of Product.'), 'money');
134 $this->add('text', 'min_contribution', ts('Minimum Contribution Amount'), CRM_Core_DAO
::getAttribute('CRM_Contribute_DAO_Product', 'min_contribution'), TRUE);
135 $this->addRule('min_contribution', ts('Please enter a monetary value for the Minimum Contribution Amount.'), 'money');
137 $this->add('textarea', 'options', ts('Options'), 'rows=3, cols=60');
139 $this->add('select', 'period_type', ts('Period Type'), array(
141 'rolling' => 'Rolling',
145 $this->add('text', 'fixed_period_start_day', ts('Fixed Period Start Day'), CRM_Core_DAO
::getAttribute('CRM_Contribute_DAO_Product', 'fixed_period_start_day'));
147 $this->add('Select', 'duration_unit', ts('Duration Unit'), array('' => '- select period -') + CRM_Core_SelectValues
::getPremiumUnits());
149 $this->add('text', 'duration_interval', ts('Duration'), CRM_Core_DAO
::getAttribute('CRM_Contribute_DAO_Product', 'duration_interval'));
151 $this->add('Select', 'frequency_unit', ts('Frequency Unit'), array('' => '- select period -') + CRM_Core_SelectValues
::getPremiumUnits());
153 $this->add('text', 'frequency_interval', ts('Frequency'), CRM_Core_DAO
::getAttribute('CRM_Contribute_DAO_Product', 'frequency_interval'));
155 //Financial Type CRM-11106
156 $financialType = CRM_Contribute_PseudoConstant
::financialType();
157 $premiumFinancialType = array();
158 CRM_Core_PseudoConstant
::populate(
159 $premiumFinancialType,
160 'CRM_Financial_DAO_EntityFinancialAccount',
162 $retrieve = 'entity_id',
164 'account_relationship = 8'
167 $costFinancialType = array();
168 CRM_Core_PseudoConstant
::populate(
170 'CRM_Financial_DAO_EntityFinancialAccount',
172 $retrieve = 'entity_id',
174 'account_relationship = 7'
176 $productFinancialType = array_intersect($costFinancialType, $premiumFinancialType);
177 foreach ($financialType as $key => $financialTypeName) {
178 if (!in_array($key, $productFinancialType)) {
179 unset($financialType[$key]);
182 if (count($financialType)) {
183 $this->assign('financialType', $financialType);
188 ts('Financial Type'),
189 array('' => ts('- select -')) +
$financialType
192 $this->add('checkbox', 'is_active', ts('Enabled?'));
194 $this->addFormRule(array('CRM_Contribute_Form_ManagePremiums', 'formRule'));
196 $this->addButtons(array(
199 'name' => ts('Save'),
204 'name' => ts('Cancel'),
208 $this->assign('productId', $this->_id
);
212 * Function for validation
214 * @param array $params
215 * (ref.) an assoc array of name/value pairs.
220 * mixed true or array of errors
223 public static function formRule($params, $files) {
224 if (isset($params['imageOption'])) {
225 if ($params['imageOption'] == 'thumbnail') {
226 if (!$params['imageUrl']) {
227 $errors['imageUrl'] = ts('Image URL is Required');
229 if (!$params['thumbnailUrl']) {
230 $errors['thumbnailUrl'] = ts('Thumbnail URL is Required');
234 // CRM-13231 financial type required if product has cost
235 if (!empty($params['cost']) && empty($params['financial_type_id'])) {
236 $errors['financial_type_id'] = ts('Financial Type is required for product having cost.');
238 $fileLocation = $files['uploadFile']['tmp_name'];
239 if ($fileLocation != "") {
240 list($width, $height) = getimagesize($fileLocation);
242 if (($width < 80 ||
$width > 500) ||
($height < 80 ||
$height > 500)) {
243 //$errors ['uploadFile'] = "Please Enter files with dimensions between 80 x 80 and 500 x 500," . " Dimensions of this file is ".$width."X".$height;
247 if (!$params['period_type']) {
248 if ($params['fixed_period_start_day'] ||
$params['duration_unit'] ||
$params['duration_interval'] ||
249 $params['frequency_unit'] ||
$params['frequency_interval']
251 $errors['period_type'] = ts('Please select the Period Type for this subscription or service.');
255 if ($params['period_type'] == 'fixed' && !$params['fixed_period_start_day']) {
256 $errors['fixed_period_start_day'] = ts('Please enter a Fixed Period Start Day for this subscription or service.');
259 if ($params['duration_unit'] && !$params['duration_interval']) {
260 $errors['duration_interval'] = ts('Please enter the Duration Interval for this subscription or service.');
263 if ($params['duration_interval'] && !$params['duration_unit']) {
264 $errors['duration_unit'] = ts('Please enter the Duration Unit for this subscription or service.');
267 if ($params['frequency_interval'] && !$params['frequency_unit']) {
268 $errors['frequency_unit'] = ts('Please enter the Frequency Unit for this subscription or service.');
271 if ($params['frequency_unit'] && !$params['frequency_interval']) {
272 $errors['frequency_interval'] = ts('Please enter the Frequency Interval for this subscription or service.');
275 return empty($errors) ?
TRUE : $errors;
279 * Process the form submission
284 public function postProcess() {
286 if ($this->_action
& CRM_Core_Action
::PREVIEW
) {
290 if ($this->_action
& CRM_Core_Action
::DELETE
) {
291 CRM_Contribute_BAO_ManagePremiums
::del($this->_id
);
292 CRM_Core_Session
::setStatus(ts('Selected Premium Product type has been deleted.'), ts('Deleted'), 'info');
295 $params = $this->controller
->exportValues($this->_name
);
296 $imageFile = CRM_Utils_Array
::value('uploadFile', $params);
297 $imageFile = $imageFile['name'];
299 $config = CRM_Core_Config
::singleton();
303 // store the submitted values in an array
306 if (CRM_Utils_Array
::value('imageOption', $params, FALSE)) {
307 $value = CRM_Utils_Array
::value('imageOption', $params, FALSE);
308 if ($value == 'image') {
310 // to check wether GD is installed or not
311 $gdSupport = CRM_Utils_System
::getModuleSetting('gd', 'GD Support');
315 $params['image'] = $this->_resizeImage($imageFile, "_full", 200, 200);
316 $params['thumbnail'] = $this->_resizeImage($imageFile, "_thumb", 50, 50);
321 $params['image'] = $config->resourceBase
. 'i/contribute/default_premium.jpg';
322 $params['thumbnail'] = $config->resourceBase
. 'i/contribute/default_premium_thumb.jpg';
325 elseif ($value == 'thumbnail') {
326 $params['image'] = $params['imageUrl'];
327 $params['thumbnail'] = $params['thumbnailUrl'];
329 elseif ($value == 'default_image') {
330 $url = parse_url($config->userFrameworkBaseURL
);
331 $params['image'] = $config->resourceBase
. 'i/contribute/default_premium.jpg';
332 $params['thumbnail'] = $config->resourceBase
. 'i/contribute/default_premium_thumb.jpg';
335 $params['image'] = "";
336 $params['thumbnail'] = "";
340 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
341 $ids['premium'] = $this->_id
;
344 // fix the money fields
350 $params[$f] = CRM_Utils_Rule
::cleanMoney($params[$f]);
353 $premium = CRM_Contribute_BAO_ManagePremiums
::add($params, $ids);
355 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');
358 CRM_Core_Session
::setStatus(ts("The Premium '%1' has been saved.", array(1 => $premium->name
)), ts('Saved'), 'success');
364 * Resize a premium image to a different size
367 * @param string $filename
368 * @param string $resizedName
375 private function _resizeImage($filename, $resizedName, $width, $height) {
376 // figure out the new filename
377 $pathParts = pathinfo($filename);
378 $newFilename = $pathParts['dirname'] . "/" . $pathParts['filename'] . $resizedName . "." . $pathParts['extension'];
380 // get image about original image
381 $imageInfo = getimagesize($filename);
382 $widthOrig = $imageInfo[0];
383 $heightOrig = $imageInfo[1];
384 $image = imagecreatetruecolor($width, $height);
385 if ($imageInfo['mime'] == 'image/gif') {
386 $source = imagecreatefromgif($filename);
388 elseif ($imageInfo['mime'] == 'image/png') {
389 $source = imagecreatefrompng($filename);
392 $source = imagecreatefromjpeg($filename);
396 imagecopyresized($image, $source, 0, 0, 0, 0, $width, $height, $widthOrig, $heightOrig);
398 // save the resized image
399 $fp = fopen($newFilename, 'w+');
402 $image_buffer = ob_get_contents();
404 imagedestroy($image);
405 fwrite($fp, $image_buffer);
409 // return the URL to link to
410 $config = CRM_Core_Config
::singleton();
411 return $config->imageUploadURL
. basename($newFilename);