From 07d113b7dca35d37a2d0a8034f1222532050da44 Mon Sep 17 00:00:00 2001 From: Pratik Joshi Date: Wed, 4 Sep 2013 19:30:50 +0530 Subject: [PATCH] CRM-13328-fix ---------------------------------------- * CRM-13328-fix: http://issues.civicrm.org/jira/browse/CRM-13328-fix --- CRM/Badge/BAO/Badge.php | 34 +++++++++++++++++------ CRM/Badge/Form/Layout.php | 9 +++++++ CRM/Badge/Page/AJAX.php | 42 +++++++++++++++++++++++++++++ templates/CRM/Badge/Form/Layout.js | 15 +++++++++++ templates/CRM/Badge/Form/Layout.tpl | 42 ++++++++++++++++++++++------- 5 files changed, 124 insertions(+), 18 deletions(-) create mode 100644 CRM/Badge/Page/AJAX.php diff --git a/CRM/Badge/BAO/Badge.php b/CRM/Badge/BAO/Badge.php index d1f2def6ec..34d2bee27f 100644 --- a/CRM/Badge/BAO/Badge.php +++ b/CRM/Badge/BAO/Badge.php @@ -108,10 +108,22 @@ class CRM_Badge_BAO_Badge { if (CRM_Utils_Array::value('image_1', $layout['data'])) { $formattedRow['image_1'] = $layout['data']['image_1']; } + if (CRM_Utils_Array::value('width_image_1', $layout['data'])) { + $formattedRow['width_image_1'] = $layout['data']['width_image_1']; + } + if (CRM_Utils_Array::value('height_image_1', $layout['data'])) { + $formattedRow['height_image_1'] = $layout['data']['height_image_1']; + } if (CRM_Utils_Array::value('image_2', $layout['data'])) { $formattedRow['image_2'] = $layout['data']['image_2']; } + if (CRM_Utils_Array::value('width_image_2', $layout['data'])) { + $formattedRow['width_image_2'] = $layout['data']['width_image_2']; + } + if (CRM_Utils_Array::value('height_image_2', $layout['data'])) { + $formattedRow['height_image_2'] = $layout['data']['height_image_2']; + } if (CRM_Utils_Array::value('add_barcode', $layout['data'])) { $formattedRow['barcode'] = array( @@ -144,13 +156,15 @@ class CRM_Badge_BAO_Badge { $titleWidth = $titleLeftMargin = 0; if (CRM_Utils_Array::value('image_1', $formattedRow)) { - $this->printImage($formattedRow['image_1']); + $this->printImage($formattedRow['image_1'], NULL, NULL, CRM_Utils_Array::value('width_image_1', $formattedRow), + CRM_Utils_Array::value('height_image_1', $formattedRow)); $titleWidth = $titleLeftMargin = $this->lMarginLogo; } $titleRightMargin = 0; if (CRM_Utils_Array::value('image_2', $formattedRow)) { - $this->printImage($formattedRow['image_2'], $x + 68); + $this->printImage($formattedRow['image_2'], $x + 68, NULL, CRM_Utils_Array::value('width_image_2', $formattedRow), + CRM_Utils_Array::value('height_image_2', $formattedRow)); $titleRightMargin = 36; $titleWidth = $this->lMarginLogo; } @@ -282,7 +296,7 @@ class CRM_Badge_BAO_Badge { * @return void * @access public */ - function printImage($img, $x = '', $y = '') { + function printImage($img, $x = '', $y = '', $w = NULL, $h = NULL) { if (!$x) { $x = $this->pdf->GetAbsX(); } @@ -294,17 +308,21 @@ class CRM_Badge_BAO_Badge { $this->imgRes = 300; if ($img) { - $imgsize = getimagesize($img); - // mm - $f = $this->imgRes / 25.4; - $w = $imgsize[0] / $f; - $h = $imgsize[1] / $f; + list($w, $h) = self::getImageProperties($img, $this->imgRes, $w, $h); $this->pdf->Image($img, $x, $y, $w, $h, '', '', '', FALSE, 72, '', FALSE, FALSE, $this->debug, FALSE, FALSE, FALSE); } $this->pdf->SetXY($x, $y); } + static function getImageProperties($img, $imgRes = 300, $w = NULL, $h = NULL) { + $imgsize = getimagesize($img); + $f = $imgRes / 25.4; + $w = !empty($w) ? $w : $imgsize[0] / $f; + $h = !empty($h) ? $h : $imgsize[1] / $f; + return array($w, $h); + } + /** * function to build badges parameters before actually creating badges. * diff --git a/CRM/Badge/Form/Layout.php b/CRM/Badge/Form/Layout.php index 7b034ae547..43247d27df 100644 --- a/CRM/Badge/Form/Layout.php +++ b/CRM/Badge/Form/Layout.php @@ -114,13 +114,22 @@ class CRM_Badge_Form_Layout extends CRM_Admin_Form { $attributes = array('readonly'=> true); $this->add('text', 'image_1', ts('Image (top right)'), $attributes + CRM_Core_DAO::getAttribute('CRM_Core_DAO_PrintLabel', 'title')); + $this->add('text', 'width_image_1', ts('Width'), array('size' => 6)); + $this->add('text', 'height_image_1', ts('Height'), array('size' => 6)); + $this->add('text', 'image_2', ts('Image (top left)'), $attributes + CRM_Core_DAO::getAttribute('CRM_Core_DAO_PrintLabel', 'title')); + $this->add('text', 'width_image_2', ts('Width'), array('size' => 6)); + $this->add('text', 'height_image_2', ts('Height'), array('size' => 6)); $this->add('checkbox', 'is_default', ts('Default?')); $this->add('checkbox', 'is_active', ts('Enabled?')); $this->add('checkbox', 'is_reserved', ts('Reserved?')); + $this->addRule('width_image_1', ts('Width not valid'), 'positiveInteger'); + $this->addRule('width_image_2', ts('Width not valid'), 'positiveInteger'); + $this->addRule('height_image_1', ts('Height not valid'), 'positiveInteger'); + $this->addRule('height_image_2', ts('Height not valid'), 'positiveInteger'); $this->addFormRule(array('CRM_Badge_Form_Layout', 'formRule')); $this->addButtons(array( diff --git a/CRM/Badge/Page/AJAX.php b/CRM/Badge/Page/AJAX.php new file mode 100644 index 0000000000..28a62bab11 --- /dev/null +++ b/CRM/Badge/Page/AJAX.php @@ -0,0 +1,42 @@ + $w, 'height' => $h)); + CRM_Utils_System::civiExit(); + } +} \ No newline at end of file diff --git a/templates/CRM/Badge/Form/Layout.js b/templates/CRM/Badge/Form/Layout.js index 5cc15821d6..044b4f99d3 100644 --- a/templates/CRM/Badge/Form/Layout.js +++ b/templates/CRM/Badge/Form/Layout.js @@ -4,6 +4,21 @@ cj(function ($) { window.KCFinder = { callBack: function(url) { field.val(url); + // calculate the image default width, height + // and assign to respective fields + var ajaxUrl = CRM.url('civicrm/ajax/rest', 'className=CRM_Badge_Page_AJAX&fnName=getImageProp&json=1&img=' + url); + $.ajax({ + url: ajaxUrl, + async: false, + global: false, + dataType: "json", + success: function ( response ) { + var widthId = 'width_' + field.attr('id'); + var heightId = 'height_' + field.attr('id'); + $('#' + widthId).val(response.width.toFixed(0)); + $('#' + heightId).val(response.height.toFixed(0)); + } + }); window.KCFinder = null; } }; diff --git a/templates/CRM/Badge/Form/Layout.tpl b/templates/CRM/Badge/Form/Layout.tpl index eaa07091a3..cd3269b247 100644 --- a/templates/CRM/Badge/Form/Layout.tpl +++ b/templates/CRM/Badge/Form/Layout.tpl @@ -49,21 +49,43 @@ {$form.image_1.label} - {$form.image_1.html} - - ({ts}clear{/ts}) - -
{ts}Click above and select a file by double clicking on it.{/ts} + + + + + + +
{$form.image_1.html} + + ({ts}clear{/ts}) +
+ {ts}Click above and select a file by double clicking on it.{/ts} +
+ {$form.width_image_1.html}
{$form.width_image_1.label} +
+ {$form.height_image_1.html}
{$form.height_image_1.label} +
{$form.image_2.label} - {$form.image_2.html} - - ({ts}clear{/ts}) - -
{ts}Click above and select a file by double clicking on it.{/ts} + + + + + + +
{$form.image_2.html} + + ({ts}clear{/ts}) +
+ {ts}Click above and select a file by double clicking on it.{/ts} +
+ {$form.width_image_2.html}
{$form.width_image_2.label} +
+ {$form.height_image_2.html}
{$form.height_image_2.label} +
-- 2.25.1