Skip to content

Commit

Permalink
CRM-20821 - Improve ManagePremiums formRule()
Browse files Browse the repository at this point in the history
- Improve CRM_Contribute_Form_ManagePremiums::formRule()
- Require an image file, if "upload" is chosen
- Remove the commented-out code for a rule which required images to be
  within a certain size
- Minor refactoring to improve code clarity
  • Loading branch information
seancolsen committed Jul 21, 2017
1 parent 8373012 commit 8dd1d75
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions CRM/Contribute/Form/ManagePremiums.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,35 +201,36 @@ public function buildQuickForm() {
*
* @param array $params
* (ref.) an assoc array of name/value pairs.
*
* @param $files
*
* @return bool|array
* mixed true or array of errors
*/
public static function formRule($params, $files) {
if (isset($params['imageOption'])) {
if ($params['imageOption'] == 'thumbnail') {
if (!$params['imageUrl']) {
$errors['imageUrl'] = ts('Image URL is Required');
}
if (!$params['thumbnailUrl']) {
$errors['thumbnailUrl'] = ts('Thumbnail URL is Required');
}

// If choosing to upload an image, then an image must be provided
if (
isset($params['imageOption']) &&
$params['imageOption'] == 'image' &&
empty($files['uploadFile']['name'])
) {
$errors['uploadFile'] = ts('A file must be selected');
}

// If choosing to use image URLs, then both URLs must be present
if (isset($params['imageOption']) && $params['imageOption'] == 'thumbnail') {
if (!$params['imageUrl']) {
$errors['imageUrl'] = ts('Image URL is Required');
}
if (!$params['thumbnailUrl']) {
$errors['thumbnailUrl'] = ts('Thumbnail URL is Required');
}
}

// CRM-13231 financial type required if product has cost
if (!empty($params['cost']) && empty($params['financial_type_id'])) {
$errors['financial_type_id'] = ts('Financial Type is required for product having cost.');
}
$fileLocation = $files['uploadFile']['tmp_name'];
if ($fileLocation != "") {
list($width, $height) = getimagesize($fileLocation);

if (($width < 80 || $width > 500) || ($height < 80 || $height > 500)) {
//$errors ['uploadFile'] = "Please Enter files with dimensions between 80 x 80 and 500 x 500," . " Dimensions of this file is ".$width."X".$height;
}
}

if (!$params['period_type']) {
if ($params['fixed_period_start_day'] || $params['duration_unit'] || $params['duration_interval'] ||
Expand Down

0 comments on commit 8dd1d75

Please sign in to comment.