From 8c7f8a8bc608618f67e55d6dbde4336dc96ca3eb Mon Sep 17 00:00:00 2001 From: Kurund Jalmi Date: Fri, 5 Jul 2013 01:48:09 +0530 Subject: [PATCH] CRM-12966, more work on name badge workflow, now you can print labels :) --- CRM/Badge/BAO/Layout.php | 61 +++++++++++++++++++++++++++++++++++ CRM/Badge/Form/Layout.php | 17 ++-------- CRM/Event/Form/Task/Badge.php | 61 ++++++++++++++++++++++------------- 3 files changed, 102 insertions(+), 37 deletions(-) diff --git a/CRM/Badge/BAO/Layout.php b/CRM/Badge/BAO/Layout.php index f7c5fc1d7b..a4370cb8b5 100644 --- a/CRM/Badge/BAO/Layout.php +++ b/CRM/Badge/BAO/Layout.php @@ -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; + } } diff --git a/CRM/Badge/Form/Layout.php b/CRM/Badge/Form/Layout.php index cf285186ab..3d663ec230 100644 --- a/CRM/Badge/Form/Layout.php +++ b/CRM/Badge/Form/Layout.php @@ -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']); diff --git a/CRM/Event/Form/Task/Badge.php b/CRM/Event/Form/Task/Badge.php index 3c691cc94a..2e5febd7b2 100644 --- a/CRM/Event/Form/Task/Badge.php +++ b/CRM/Event/Form/Task/Badge.php @@ -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); } } -- 2.25.1