Merge pull request #4629 from systopia/CRM-15665
[civicrm-core.git] / CRM / Badge / BAO / Badge.php
index ad2fcacd991beb8b7a095b58c972ce61f8e16ced..cf0f1f97abd511ce2d461549dc14ca609d077737 100644 (file)
@@ -83,6 +83,9 @@ class CRM_Badge_BAO_Badge {
    */
   static function formatLabel(&$row, &$layout) {
     $formattedRow = array('labelFormat' => $layout['label_format_name']);
+    $formattedRow['labelTitle'] = $layout['title'];
+    $formattedRow['labelId'] = $layout['id'];
+
 
     if (!empty($layout['data']['rowElements'])) {
       foreach ($layout['data']['rowElements'] as $key => $element) {
@@ -125,6 +128,18 @@ class CRM_Badge_BAO_Badge {
     if (!empty($layout['data']['height_image_2'])) {
       $formattedRow['height_image_2'] = $layout['data']['height_image_2'];
     }
+    if (!empty($row['image_URL']) && !empty($layout['data']['show_participant_image'])) {
+      $formattedRow['participant_image'] = $row['image_URL'];
+    }
+    if (!empty($layout['data']['width_participant_image'])) {
+      $formattedRow['width_participant_image'] = $layout['data']['width_participant_image'];
+    }
+    if (!empty($layout['data']['height_participant_image'])) {
+      $formattedRow['height_participant_image'] = $layout['data']['height_participant_image'];
+    }
+    if (!empty($layout['data']['alignment_participant_image'])) {
+      $formattedRow['alignment_participant_image'] = $layout['data']['alignment_participant_image'];
+    }
 
     if (!empty($layout['data']['add_barcode'])) {
       $formattedRow['barcode'] = array(
@@ -139,6 +154,9 @@ class CRM_Badge_BAO_Badge {
     return $formattedRow;
   }
 
+  /**
+   * @param $formattedRow
+   */
   public function generateLabel($formattedRow) {
     switch ($formattedRow['labelFormat']) {
       case 'A6 Badge Portrait 150x106':
@@ -152,6 +170,10 @@ class CRM_Badge_BAO_Badge {
     }
   }
 
+  /**
+   * @param $formattedRow
+   * @param int $cellspacing
+   */
   public function labelCreator(&$formattedRow, $cellspacing = 0) {
     $this->lMarginLogo = 18;
     $this->tMarginName = 20;
@@ -159,6 +181,9 @@ class CRM_Badge_BAO_Badge {
     $x = $this->pdf->GetAbsX();
     $y = $this->pdf->getY();
 
+    //call hook alterBadge
+    CRM_Utils_Hook::alterBadge($formattedRow['labelTitle'], $this, $formattedRow, $formattedRow['values']);
+
     $startOffset = 0;
     if (!empty($formattedRow['image_1'])) {
       $this->printImage($formattedRow['image_1'], NULL, NULL, CRM_Utils_Array::value('width_image_1', $formattedRow),
@@ -178,6 +203,24 @@ class CRM_Badge_BAO_Badge {
       $startOffset = CRM_Utils_Array::value('height_image_2', $formattedRow);
     }
 
+    if (CRM_Utils_Array::value('participant_image', $formattedRow)) {
+      $imageAlign = 0;
+      switch (CRM_Utils_Array::value('alignment_participant_image', $formattedRow)) {
+        case 'R':
+          $imageAlign = 68;
+          break;
+        case 'L':
+          $imageAlign = 0;
+          break;
+        default:
+          break;
+      }
+      $this->pdf->Image($formattedRow['participant_image'], $x + $imageAlign, $y + $startOffset, CRM_Utils_Array::value('width_participant_image', $formattedRow), CRM_Utils_Array::value('height_participant_image', $formattedRow));
+      if ($startOffset == NULL && CRM_Utils_Array::value('height_participant_image', $formattedRow)) {
+        $startOffset = CRM_Utils_Array::value('height_participant_image', $formattedRow);
+      }
+    }
+
     $this->pdf->SetLineStyle(array(
       'width' => 0.1,
       'cap' => 'round',
@@ -194,12 +237,20 @@ class CRM_Badge_BAO_Badge {
           $value = $formattedRow['token'][$i]['value'];
         }
 
+        $xAlign = $x;
+        $rowWidth = $this->pdf->width;
+        if (!empty($formattedRow['participant_image']) && !empty($formattedRow['width_participant_image'])) {
+          $rowWidth = $this->pdf->width - $formattedRow['width_participant_image'];
+          if ($formattedRow['alignment_participant_image'] == 'L') {
+            $xAlign = $x + $formattedRow['width_participant_image'] + $imageAlign;
+          }
+        }
         $offset = $this->pdf->getY() + $startOffset + $cellspacing;
 
         $this->pdf->SetFont($formattedRow['token'][$i]['font_name'], $formattedRow['token'][$i]['font_style'],
           $formattedRow['token'][$i]['font_size']);
-        $this->pdf->MultiCell($this->pdf->width, 0, $value,
-          $this->border, $formattedRow['token'][$i]['text_alignment'], 0, 1, $x, $offset);
+        $this->pdf->MultiCell($rowWidth, 0, $value,
+          $this->border, $formattedRow['token'][$i]['text_alignment'], 0, 1, $xAlign, $offset);
 
         // set this to zero so that it is added only for first element
         $startOffset = 0;
@@ -296,8 +347,14 @@ class CRM_Badge_BAO_Badge {
 
   /**
    * Helper function to print images
+   *
    * @param string $img image url
    *
+   * @param string $x
+   * @param string $y
+   * @param null $w
+   * @param null $h
+   *
    * @return void
    * @access public
    */
@@ -320,6 +377,14 @@ class CRM_Badge_BAO_Badge {
     $this->pdf->SetXY($x, $y);
   }
 
+  /**
+   * @param $img
+   * @param int $imgRes
+   * @param null $w
+   * @param null $h
+   *
+   * @return array
+   */
   static function getImageProperties($img, $imgRes = 300, $w = NULL, $h = NULL) {
     $imgsize = getimagesize($img);
     $f = $imgRes / 25.4;
@@ -329,10 +394,10 @@ class CRM_Badge_BAO_Badge {
   }
 
   /**
-   * function to build badges parameters before actually creating badges.
+   * Build badges parameters before actually creating badges.
    *
-   * @param  array  $params associated array of submitted values
-   * @params object $form form/controller object
+   * @param  array $params associated array of submitted values
+   * @param CRM_Core_Form $form
    *
    * @return void
    * @access public
@@ -342,7 +407,7 @@ class CRM_Badge_BAO_Badge {
     // get name badge layout info
     $layoutInfo = CRM_Badge_BAO_Layout::buildLayout($params);
 
-    // spit / get actual field names from token
+    // split/get actual field names from token and individual contact image URLs
     $returnProperties = array();
     if (!empty($layoutInfo['data']['token'])) {
       foreach ($layoutInfo['data']['token'] as $index => $value) {
@@ -373,7 +438,7 @@ class CRM_Badge_BAO_Badge {
     }
 
     // add additional required fields for query execution
-    $additionalFields = array('participant_register_date', 'participant_id', 'event_id', 'contact_id');
+    $additionalFields = array('participant_register_date', 'participant_id', 'event_id', 'contact_id', 'image_URL');
     foreach ($additionalFields as $field) {
       $returnProperties[$field] = 1;
     }
@@ -409,6 +474,7 @@ class CRM_Badge_BAO_Badge {
     $dao = CRM_Core_DAO::executeQuery($queryString);
     $rows = array();
     while ($dao->fetch()) {
+      $query->convertToPseudoNames($dao);
       $rows[$dao->participant_id] = array();
       foreach ($returnProperties as $key => $dontCare) {
         $rows[$dao->participant_id][$key] = isset($dao->$key) ? $dao->$key : NULL;