3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class generates form components for Premiums.
21 class CRM_Contribute_Form_ManagePremiums
extends CRM_Contribute_Form
{
24 * Pre process the form.
26 public function preProcess() {
31 * Set default values for the form.
33 public function setDefaultValues() {
34 $defaults = parent
::setDefaultValues();
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']);
46 $defaults['imageOption'] = 'noImage';
48 if (isset($tempDefaults['thumbnail']) && isset($tempDefaults['image'])) {
49 $this->assign('thumbURL', $tempDefaults['thumbnail']);
50 $this->assign('imageURL', $tempDefaults['image']);
52 if (isset($tempDefaults['period_type'])) {
53 $this->assign('showSubscriptions', TRUE);
61 * Build the form object.
63 public function buildQuickForm() {
64 parent
::buildQuickForm();
65 $this->setPageTitle(ts('Premium Product'));
67 if ($this->_action
& CRM_Core_Action
::PREVIEW
) {
68 CRM_Contribute_BAO_Premium
::buildPremiumPreviewBlock($this, $this->_id
);
72 if ($this->_action
& CRM_Core_Action
::DELETE
) {
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',
82 $this->add('text', 'sku', ts('SKU'), CRM_Core_DAO
::getAttribute('CRM_Contribute_DAO_Product', 'sku'));
84 $this->add('textarea', 'description', ts('Description'), 'rows=3, cols=60');
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\');');
91 $this->addGroup($image, 'imageOption', ts('Premium Image'));
92 $this->addRule('imageOption', ts('Please select an option for the premium image.'), 'required');
94 $this->addElement('text', 'imageUrl', ts('Image URL'));
95 $this->addElement('text', 'thumbnailUrl', ts('Thumbnail URL'));
97 $this->add('file', 'uploadFile', ts('Image File Name'), 'onChange="select_option();"');
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');
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');
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');
108 $this->add('textarea', 'options', ts('Options'), 'rows=3, cols=60');
110 $this->add('select', 'period_type', ts('Period Type'), [
112 'rolling' => 'Rolling',
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'));
118 $this->add('Select', 'duration_unit', ts('Duration Unit'), ['' => '- select period -'] + CRM_Core_SelectValues
::getPremiumUnits());
120 $this->add('text', 'duration_interval', ts('Duration'), CRM_Core_DAO
::getAttribute('CRM_Contribute_DAO_Product', 'duration_interval'));
122 $this->add('Select', 'frequency_unit', ts('Frequency Unit'), ['' => '- select period -'] + CRM_Core_SelectValues
::getPremiumUnits());
124 $this->add('text', 'frequency_interval', ts('Frequency'), CRM_Core_DAO
::getAttribute('CRM_Contribute_DAO_Product', 'frequency_interval'));
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',
133 $retrieve = 'entity_id',
135 'account_relationship = 8'
138 $costFinancialType = [];
139 CRM_Core_PseudoConstant
::populate(
141 'CRM_Financial_DAO_EntityFinancialAccount',
143 $retrieve = 'entity_id',
145 'account_relationship = 7'
147 $productFinancialType = array_intersect($costFinancialType, $premiumFinancialType);
148 foreach ($financialType as $key => $financialTypeName) {
149 if (!in_array($key, $productFinancialType)) {
150 unset($financialType[$key]);
153 if (count($financialType)) {
154 $this->assign('financialType', $financialType);
159 ts('Financial Type'),
160 ['' => ts('- select -')] +
$financialType
163 $this->add('checkbox', 'is_active', ts('Enabled?'));
165 $this->addFormRule(['CRM_Contribute_Form_ManagePremiums', 'formRule']);
170 'name' => ts('Save'),
175 'name' => ts('Cancel'),
178 $this->assign('productId', $this->_id
);
182 * Function for validation.
184 * @param array $params
185 * (ref.) an assoc array of name/value pairs.
189 * mixed true or array of errors
191 public static function formRule($params, $files) {
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'])
197 $errors['uploadFile'] = ts('A file must be selected');
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');
205 if (!$params['thumbnailUrl']) {
206 $errors['thumbnailUrl'] = ts('Thumbnail URL is Required');
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.');
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']
219 $errors['period_type'] = ts('Please select the Period Type for this subscription or service.');
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.');
227 if ($params['duration_unit'] && !$params['duration_interval']) {
228 $errors['duration_interval'] = ts('Please enter the Duration Interval for this subscription or service.');
231 if ($params['duration_interval'] && !$params['duration_unit']) {
232 $errors['duration_unit'] = ts('Please enter the Duration Unit for this subscription or service.');
235 if ($params['frequency_interval'] && !$params['frequency_unit']) {
236 $errors['frequency_unit'] = ts('Please enter the Frequency Unit for this subscription or service.');
239 if ($params['frequency_unit'] && !$params['frequency_interval']) {
240 $errors['frequency_interval'] = ts('Please enter the Frequency Interval for this subscription or service.');
243 return empty($errors) ?
TRUE : $errors;
247 * Process the form submission.
249 public function postProcess() {
250 // If previewing, don't do any post-processing
251 if ($this->_action
& CRM_Core_Action
::PREVIEW
) {
255 // If deleting, then only delete and skip the rest of the post-processing
256 if ($this->_action
& CRM_Core_Action
::DELETE
) {
258 CRM_Contribute_BAO_Product
::del($this->_id
);
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'));
266 CRM_Core_Session
::setStatus(
267 ts('Selected Premium Product type has been deleted.'),
268 ts('Deleted'), 'info');
272 $params = $this->controller
->exportValues($this->_name
);
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]);
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
;
285 $this->_processImages($params);
287 // Save the premium product to database
288 $premium = CRM_Contribute_BAO_Product
::create($params);
290 CRM_Core_Session
::setStatus(
291 ts("The Premium '%1' has been saved.", [1 => $premium->name
]),
292 ts('Saved'), 'success');
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.
299 * @param array $params
301 protected function _processImages(&$params) {
303 'imageOption' => 'noImage',
304 'uploadFile' => ['name' => ''],
308 'thumbnailUrl' => '',
310 $params = array_merge($defaults, $params);
312 // User is uploading an image
313 if ($params['imageOption'] == 'image') {
314 $imageFile = $params['uploadFile']['name'];
316 $params['image'] = CRM_Utils_File
::resizeImage($imageFile, 200, 200, "_full");
317 $params['thumbnail'] = CRM_Utils_File
::resizeImage($imageFile, 50, 50, "_thumb");
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');
327 // User is specifying existing URLs for the images
328 elseif ($params['imageOption'] == 'thumbnail') {
329 $params['image'] = $params['imageUrl'];
330 $params['thumbnail'] = $params['thumbnailUrl'];
333 // User wants a default image
334 elseif ($params['imageOption'] == 'default_image') {
335 $params['image'] = self
::_defaultImage();
336 $params['thumbnail'] = self
::_defaultThumbnail();
341 * Returns the path to the default premium image
344 protected static function _defaultImage() {
345 $config = CRM_Core_Config
::singleton();
346 return $config->resourceBase
. 'i/contribute/default_premium.jpg';
350 * Returns the path to the default premium thumbnail
353 protected static function _defaultThumbnail() {
354 $config = CRM_Core_Config
::singleton();
355 return $config->resourceBase
. 'i/contribute/default_premium_thumb.jpg';