CRM-12966, more work on name badge workflow, now you can print labels :)
authorKurund Jalmi <kurund@civicrm.org>
Thu, 4 Jul 2013 20:18:09 +0000 (01:48 +0530)
committerKurund Jalmi <kurund@civicrm.org>
Thu, 4 Jul 2013 20:18:09 +0000 (01:48 +0530)
CRM/Badge/BAO/Layout.php
CRM/Badge/Form/Layout.php
CRM/Event/Form/Task/Badge.php

index f7c5fc1d7b2cdceb577b9d81a594aab780b82393..a4370cb8b54e10ce049c8e7195380f3fd4b2505a 100644 (file)
@@ -134,5 +134,66 @@ class CRM_Badge_BAO_Layout extends CRM_Core_DAO_PrintLabel {
     $printLabel->id = $printLabelId;
     $printLabel->delete();
   }
+
+  /**
+   *  get the list of print labels
+   *
+   * @return array list of labels
+   * @access public
+   * @static
+   */
+  static function getList() {
+    $printLabel = new CRM_Core_DAO_PrintLabel();
+    $printLabel->find();
+
+    $labels = array();
+    while($printLabel->fetch()) {
+      $labels[$printLabel->id] = $printLabel->title;
+    }
+    return $labels;
+  }
+
+  /**
+   * build layout structure
+   *
+   * @param array $params associated array of submitted values
+   *
+   * @return array $formattedLayout array formatted array
+   * @access public
+   */
+  static function buildLayout(&$params) {
+    $layoutParams = array('id', $params['badge_id']);
+    CRM_Badge_BAO_Layout::retrieve($layoutParams, $layoutInfo);
+
+    $formatProperties = CRM_Core_OptionGroup::getValue('name_badge', $layoutInfo['label_format_name'], 'name');
+    $layoutInfo['format'] = get_object_vars(json_decode($formatProperties));
+    $layoutInfo['data'] = CRM_Badge_BAO_Layout::getDecodedData($layoutInfo['data']);
+    return $layoutInfo;
+  }
+
+  /**
+   * Function to decode encoded data and return as an array
+   *
+   * @param json $jsonData json object
+   *
+   * @return array $dataValues associated array of decoded elements
+   * @static
+   */
+  static public function getDecodedData($jsonData) {
+    $data = get_object_vars(json_decode($jsonData));
+
+    $specialFields = array('token', 'font_name', 'font_size', 'text_alignment');
+    foreach($specialFields as $field) {
+      $dataValues[$field] = get_object_vars($data[$field]);
+    }
+
+    $dataValues['add_barcode'] = CRM_Utils_Array::value('add_barcode', $data);
+    $dataValues['barcode_alignment'] = CRM_Utils_Array::value('barcode_alignment', $data);
+
+    $dataValues['image_1'] = CRM_Utils_Array::value('image_1', $data);
+    $dataValues['image_2'] = CRM_Utils_Array::value('image_2', $data);
+
+    return $dataValues;
+  }
 }
 
index cf285186abfb83f00cebcc96fb60f44d53eeb066..3d663ec2307cc8367a1097db83e0e4d5bfbd962f 100644 (file)
@@ -46,7 +46,6 @@ class CRM_Badge_Form_Layout extends CRM_Admin_Form {
    * @access public
    */
   public function buildQuickForm() {
-
     parent::buildQuickForm();
 
     if ($this->_action & CRM_Core_Action::DELETE) {
@@ -123,20 +122,8 @@ class CRM_Badge_Form_Layout extends CRM_Admin_Form {
       CRM_Badge_BAO_Layout::retrieve($params, $this->_values );
     }
 
-    $defaults = $this->_values;
-
-    $data = get_object_vars(json_decode($this->_values['data']));
-
-    $specialFields = array('token', 'font_name', 'font_size', 'text_alignment');
-    foreach($specialFields as $field) {
-      $defaults[$field] = get_object_vars($data[$field]);
-    }
-
-    $defaults['add_barcode'] = CRM_Utils_Array::value('add_barcode', $data);
-    $defaults['barcode_alignment'] = CRM_Utils_Array::value('barcode_alignment', $data);
-
-    $defaults['image_1'] = CRM_Utils_Array::value('image_1', $data);
-    $defaults['image_2'] = CRM_Utils_Array::value('image_2', $data);
+    $defaults = array_merge($this->_values,
+      CRM_Badge_BAO_Layout::getDecodedData($this->_values['data']));
 
     if ($this->_action == CRM_Core_Action::DELETE && isset($defaults['title'])) {
       $this->assign('delName', $defaults['title']);
index 3c691cc94a8028cc68c4baffbbdc7a0696f2085d..2e5febd7b2c91a51d7a8068919a4ad4a2a0a666a 100644 (file)
@@ -87,7 +87,7 @@ class CRM_Event_Form_Task_Badge extends CRM_Event_Form_Task {
     CRM_Utils_System::setTitle(ts('Make Name Badges'));
 
     //add select for label
-    $label = CRM_Core_OptionGroup::values('event_badge');
+    $label = CRM_Badge_BAO_Layout::getList();
 
     $this->add('select',
       'badge_id',
@@ -110,11 +110,43 @@ class CRM_Event_Form_Task_Badge extends CRM_Event_Form_Task {
    */
   public function postProcess() {
     $params = $this->controller->exportValues($this->_name);
-    $config = CRM_Core_Config::singleton();
 
+    // get name badge layout info
+    $layoutInfo = CRM_Badge_BAO_Layout::buildLayout($params);
+
+    // spit / get actual field names from token
+    $returnProperties = array();
+    if (!empty($layoutInfo['data']['token'])) {
+      foreach ($layoutInfo['data']['token'] as $index => $value) {
+        $token = CRM_Utils_Token::getTokens($value);
+        if (key($token) == 'contact') {
+          $element = $token['contact'][0];
+        }
+        else {
+          $element = $token['event'][0];
+          //FIX ME - we need to standardize event token names
+          if (!strpos($element, 'event_')) {
+            $element = 'event_' .$element;
+          }
+        }
+
+        // build returnproperties
+        $returnProperties[$element] = 1;
+
+        // add actual field name to row element
+        $layoutInfo['data']['rowElements'][$index] = $element;
+      }
+    }
+
+    // check if name label format class exists
+    $classFile = 'CRM/Badge/Format/' . $layoutInfo['label_format_name'] . '.php';
+    $error = include_once($classFile);
+    if ($error == FALSE) {
+      CRM_Core_Error::fatal('Event Badge code file: ' . $classFile . ' does not exist. Please verify your custom event badge settings in CiviCRM administrative panel.');
+    }
 
-    $returnProperties = CRM_Event_BAO_Query::defaultReturnProperties(CRM_Contact_BAO_Query::MODE_EVENT);
-    $additionalFields = array('first_name', 'last_name', 'middle_name', 'current_employer');
+    // add additional required fields for query execution
+    $additionalFields = array('participant_register_date', 'participant_id');
     foreach ($additionalFields as $field) {
       $returnProperties[$field] = 1;
     }
@@ -156,25 +188,10 @@ class CRM_Event_Form_Task_Badge extends CRM_Event_Form_Task {
       }
     }
 
-    // get the class name from the participantListingID
-    $className = CRM_Core_OptionGroup::getValue('event_badge',
-      $params['badge_id'],
-      'value',
-      'Integer',
-      'name'
-    );
-
-    $classFile = str_replace('_',
-      DIRECTORY_SEPARATOR,
-      $className
-    ) . '.php';
-    $error = include_once ($classFile);
-    if ($error == FALSE) {
-      CRM_Core_Error::fatal('Event Badge code file: ' . $classFile . ' does not exist. Please verify your custom event badge settings in CiviCRM administrative panel.');
-    }
-
+    // create badge class
+    $className = 'CRM_Badge_Format_' . $layoutInfo['label_format_name'];
     $eventBadgeClass = new $className();
-    $eventBadgeClass->run($rows);
+    $eventBadgeClass->createLabels($rows, $layoutInfo);
   }
 }