CRM-13328-fix
authorPratik Joshi <pratik.joshi@webaccess.co.in>
Wed, 4 Sep 2013 14:00:50 +0000 (19:30 +0530)
committerPratik Joshi <pratik.joshi@webaccess.co.in>
Wed, 4 Sep 2013 14:00:50 +0000 (19:30 +0530)
----------------------------------------
* CRM-13328-fix:
  http://issues.civicrm.org/jira/browse/CRM-13328-fix

CRM/Badge/BAO/Badge.php
CRM/Badge/Form/Layout.php
CRM/Badge/Page/AJAX.php [new file with mode: 0644]
templates/CRM/Badge/Form/Layout.js
templates/CRM/Badge/Form/Layout.tpl

index d1f2def6eceb503fd9f4fc0198e7a60751a94632..34d2bee27f6a47b61efec014508ceac06bfc3543 100644 (file)
@@ -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.
    *
index 7b034ae547bd0efed05b84abe988eab1d1e4d6d6..43247d27df37698976f8cfd28633f381bff8465a 100644 (file)
@@ -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 (file)
index 0000000..28a62ba
--- /dev/null
@@ -0,0 +1,42 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | CiviCRM version 4.4                                                |
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC (c) 2004-2013                                |
+ +--------------------------------------------------------------------+
+ | This file is a part of CiviCRM.                                    |
+ |                                                                    |
+ | CiviCRM is free software; you can copy, modify, and distribute it  |
+ | under the terms of the GNU Affero General Public License           |
+ | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
+ |                                                                    |
+ | CiviCRM is distributed in the hope that it will be useful, but     |
+ | WITHOUT ANY WARRANTY; without even the implied warranty of         |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
+ | See the GNU Affero General Public License for more details.        |
+ |                                                                    |
+ | You should have received a copy of the GNU Affero General Public   |
+ | License and the CiviCRM Licensing Exception along                  |
+ | with this program; if not, contact CiviCRM LLC                     |
+ | at info[AT]civicrm[DOT]org. If you have questions about the        |
+ | GNU Affero General Public License or the licensing of CiviCRM,     |
+ | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
+ +--------------------------------------------------------------------+
+*/
+
+/**
+ *
+ * @package CRM
+ * @copyright CiviCRM LLC (c) 2004-2013
+ *
+ */
+
+class CRM_Badge_Page_AJAX {
+  static function getImageProp() {
+    $img = $_GET['img'];
+   list($w, $h) = CRM_Badge_BAO_Badge::getImageProperties($img);
+    echo json_encode(array('width' => $w, 'height' => $h));
+    CRM_Utils_System::civiExit();
+  }
+}
\ No newline at end of file
index 5cc15821d6b58e4e5bc307f5224e0761f763c36a..044b4f99d321c4e69712f7eba354a430550ed2ad 100644 (file)
@@ -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;
       }
     };
index eaa07091a3ca98bd2a65e30fbd0d9e220e9c7eb1..cd3269b247fbb141fa2583f95602a32f64cc6f44 100644 (file)
       <tr class="crm-badge-layout-form-block-image_1">
         <td class="label">{$form.image_1.label}</td>
         <td>
-          {$form.image_1.html}
-          <span class="crm-clear-link">
-            (<a href="#" class="clear-image" imgname='image_1'>{ts}clear{/ts}</a>)
-          </span>
-          <br/><span class="description">{ts}Click above and select a file by double clicking on it.{/ts}</span>
+         <table>
+           <tr>
+            <td>{$form.image_1.html}
+             <span class="crm-clear-link">
+               (<a href="#" class="clear-image" imgname='image_1'>{ts}clear{/ts}</a>)
+             </span><br/>
+             <span class="description">{ts}Click above and select a file by double clicking on it.{/ts}</span>
+            </td>
+            <td>
+             {$form.width_image_1.html}<br/>{$form.width_image_1.label}
+            </td>
+           <td>
+              {$form.height_image_1.html}</br>{$form.height_image_1.label}
+            </td>
+           </tr>
+         </table> 
         </td>
       </tr>
       <tr class="crm-badge-layout-form-block-image_2">
         <td class="label">{$form.image_2.label}</td>
         <td>
-          {$form.image_2.html}
-          <span class="crm-clear-link">
-            (<a href="#" class="clear-image" imgname='image_2'>{ts}clear{/ts}</a>)
-          </span>
-          <br/><span class="description">{ts}Click above and select a file by double clicking on it.{/ts}</span>
+         <table>
+          <tr>
+           <td>{$form.image_2.html}
+            <span class="crm-clear-link">
+              (<a href="#" class="clear-image" imgname='image_2'>{ts}clear{/ts}</a>)
+            </span><br/>
+            <span class="description">{ts}Click above and select a file by double clicking on it.{/ts}</span>
+           </td>
+           <td>
+            {$form.width_image_2.html}<br/>{$form.width_image_2.label}
+           </td>
+           <td>
+            {$form.height_image_2.html}<br/>{$form.height_image_2.label}
+           </td>
+          </tr>
+         </table> 
         </td>
       </tr>
       <tr class="crm-badge-layout-form-block-elements">