Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRM-20821 & CRM-20938 - fix bugs in managing premium product images #10720

Merged
merged 9 commits into from
Jul 25, 2017
36 changes: 11 additions & 25 deletions CRM/Contribute/BAO/ManagePremiums.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,42 +86,28 @@ public static function setIsActive($id, $is_active) {
}

/**
* add the financial types.
* Add a premium product to the database, and return it.
*
* @param array $params
* Reference array contains the values submitted by the form.
* @param array $ids
* Reference array contains the id.
*
*
* @return object
* @return CRM_Contribute_DAO_Product
*/
public static function add(&$params, &$ids) {
// CRM-14283 - strip protocol and domain from image URLs
$image_type = array('image', 'thumbnail');
foreach ($image_type as $key) {
if (isset($params[$key]) && $params[$key]) {
$parsedURL = explode('/', $params[$key]);
$pathComponents = array_slice($parsedURL, 3);
$params[$key] = '/' . implode('/', $pathComponents);
}
}
$params = array_merge(array(
'id' => CRM_Utils_Array::value('premium', $ids),
'image' => CRM_Utils_String::simplifyURL(CRM_Utils_Array::value('image', $params, ''), TRUE),
'thumbnail' => CRM_Utils_String::simplifyURL(CRM_Utils_Array::value('thumbnail', $params, ''), TRUE),
'is_active' => 0,
'is_deductible' => FALSE,
'currency' => CRM_Core_Config::singleton()->defaultCurrency,
), $params);

$params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
$params['is_deductible'] = CRM_Utils_Array::value('is_deductible', $params, FALSE);

// action is taken depending upon the mode
// Save and return
$premium = new CRM_Contribute_DAO_Product();
$premium->copyValues($params);

$premium->id = CRM_Utils_Array::value('premium', $ids);

// set currency for CRM-1496
if (!isset($premium->currency)) {
$config = CRM_Core_Config::singleton();
$premium->currency = $config->defaultCurrency;
}

$premium->save();
return $premium;
}
Expand Down
216 changes: 93 additions & 123 deletions CRM/Contribute/Form/ManagePremiums.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,35 +201,34 @@ 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 (CRM_Utils_Array::value('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 (CRM_Utils_Array::value('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 Expand Up @@ -266,133 +265,104 @@ public static function formRule($params, $files) {
* Process the form submission.
*/
public function postProcess() {

// If previewing, don't do any post-processing
if ($this->_action & CRM_Core_Action::PREVIEW) {
return;
}

// If deleting, then only delete and skip the rest of the post-processing
if ($this->_action & CRM_Core_Action::DELETE) {
CRM_Contribute_BAO_ManagePremiums::del($this->_id);
CRM_Core_Session::setStatus(ts('Selected Premium Product type has been deleted.'), ts('Deleted'), 'info');
CRM_Core_Session::setStatus(
ts('Selected Premium Product type has been deleted.'),
ts('Deleted'), 'info');
return;
}
else {
$params = $this->controller->exportValues($this->_name);
$imageFile = CRM_Utils_Array::value('uploadFile', $params);
$imageFile = $imageFile['name'];

$config = CRM_Core_Config::singleton();

$ids = array();
$error = FALSE;
// store the submitted values in an array

// FIX ME
if (CRM_Utils_Array::value('imageOption', $params, FALSE)) {
$value = CRM_Utils_Array::value('imageOption', $params, FALSE);
if ($value == 'image') {

// to check wether GD is installed or not
$gdSupport = CRM_Utils_System::getModuleSetting('gd', 'GD Support');
if ($gdSupport) {
if ($imageFile) {
$error = FALSE;
$params['image'] = $this->_resizeImage($imageFile, "_full", 200, 200);
$params['thumbnail'] = $this->_resizeImage($imageFile, "_thumb", 50, 50);
}
}
else {
$error = TRUE;
$params['image'] = $config->resourceBase . 'i/contribute/default_premium.jpg';
$params['thumbnail'] = $config->resourceBase . 'i/contribute/default_premium_thumb.jpg';
}
}
elseif ($value == 'thumbnail') {
$params['image'] = $params['imageUrl'];
$params['thumbnail'] = $params['thumbnailUrl'];
}
elseif ($value == 'default_image') {
$url = parse_url($config->userFrameworkBaseURL);
$params['image'] = $config->resourceBase . 'i/contribute/default_premium.jpg';
$params['thumbnail'] = $config->resourceBase . 'i/contribute/default_premium_thumb.jpg';
}
else {
$params['image'] = "";
$params['thumbnail'] = "";
}
}

if ($this->_action & CRM_Core_Action::UPDATE) {
$ids['premium'] = $this->_id;
}
$params = $this->controller->exportValues($this->_name);

// fix the money fields
foreach (array(
'cost',
'price',
'min_contribution',
) as $f) {
$params[$f] = CRM_Utils_Rule::cleanMoney($params[$f]);
}
// Clean the the money fields
$moneyFields = array('cost', 'price', 'min_contribution');
foreach ($moneyFields as $field) {
$params[$field] = CRM_Utils_Rule::cleanMoney($params[$field]);
}

$premium = CRM_Contribute_BAO_ManagePremiums::add($params, $ids);
if ($error) {
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');
}
else {
CRM_Core_Session::setStatus(ts("The Premium '%1' has been saved.", array(1 => $premium->name)), ts('Saved'), 'success');
}
$ids = array();
if ($this->_action & CRM_Core_Action::UPDATE) {
$ids['premium'] = $this->_id;
}

$this->_processImages($params);

// Save to database
$premium = CRM_Contribute_BAO_ManagePremiums::add($params, $ids);

CRM_Core_Session::setStatus(
ts("The Premium '%1' has been saved.", array(1 => $premium->name)),
ts('Saved'), 'success');
}

/**
* Resize a premium image to a different size.
*
*
* @param string $filename
* @param string $resizedName
* @param $width
* @param $height
* Look at $params to find form info about images. Manipulate images if
* necessary. Then alter $params to point to the newly manipulated images.
*
* @return string
* Path to image
* @param array $params
*/
private function _resizeImage($filename, $resizedName, $width, $height) {
// figure out the new filename
$pathParts = pathinfo($filename);
$newFilename = $pathParts['dirname'] . "/" . $pathParts['filename'] . $resizedName . "." . $pathParts['extension'];

// get image about original image
$imageInfo = getimagesize($filename);
$widthOrig = $imageInfo[0];
$heightOrig = $imageInfo[1];
$image = imagecreatetruecolor($width, $height);
if ($imageInfo['mime'] == 'image/gif') {
$source = imagecreatefromgif($filename);
protected function _processImages(&$params) {
$defaults = array(
'imageOption' => 'noImage',
'uploadFile' => array('name' => ''),
'image' => '',
'thumbnail' => '',
'imageUrl' => '',
'thumbnailUrl' => '',
);
$params = array_merge($defaults, $params);

// User is uploading an image
if ($params['imageOption'] == 'image') {
$imageFile = $params['uploadFile']['name'];
try {
$params['image'] = CRM_Utils_File::resizeImage($imageFile, 200, 200, "_full");
$params['thumbnail'] = CRM_Utils_File::resizeImage($imageFile, 50, 50, "_thumb");
}
catch (CRM_Core_Exception $e) {
$params['image'] = self::_defaultImage();
$params['thumbnail'] = self::_defaultThumbnail();
$msg = ts('The product has been configured to use a default image.');
CRM_Core_Session::setStatus($e->getMessage() . " $msg", ts('Notice'), 'alert');
}
}
elseif ($imageInfo['mime'] == 'image/png') {
$source = imagecreatefrompng($filename);

// User is specifying existing URLs for the images
elseif ($params['imageOption'] == 'thumbnail') {
$params['image'] = $params['imageUrl'];
$params['thumbnail'] = $params['thumbnailUrl'];
}
else {
$source = imagecreatefromjpeg($filename);

// User wants a default image
elseif ($params['imageOption'] == 'default_image') {
$params['image'] = self::_defaultImage();
$params['thumbnail'] = self::_defaultThumbnail();
}
}

/**
* Returns the path to the default premium image
* @return string
*/
protected static function _defaultImage() {
$config = CRM_Core_Config::singleton();
return $config->resourceBase . 'i/contribute/default_premium.jpg';
}

// resize
imagecopyresized($image, $source, 0, 0, 0, 0, $width, $height, $widthOrig, $heightOrig);

// save the resized image
$fp = fopen($newFilename, 'w+');
ob_start();
imagejpeg($image);
$image_buffer = ob_get_contents();
ob_end_clean();
imagedestroy($image);
fwrite($fp, $image_buffer);
rewind($fp);
fclose($fp);

// return the URL to link to
/**
* Returns the path to the default premium thumbnail
* @return string
*/
protected static function _defaultThumbnail() {
$config = CRM_Core_Config::singleton();
return $config->imageUploadURL . basename($newFilename);
return $config->resourceBase . 'i/contribute/default_premium_thumb.jpg';
}

}
Loading