| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 10 | | CiviCRM is free software; you can copy, modify, and distribute it | |
| 11 | | under the terms of the GNU Affero General Public License | |
| 12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
| 13 | | | |
| 14 | | CiviCRM is distributed in the hope that it will be useful, but | |
| 15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 17 | | See the GNU Affero General Public License for more details. | |
| 18 | | | |
| 19 | | You should have received a copy of the GNU Affero General Public | |
| 20 | | License and the CiviCRM Licensing Exception along | |
| 21 | | with this program; if not, contact CiviCRM LLC | |
| 22 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
| 23 | | GNU Affero General Public License or the licensing of CiviCRM, | |
| 24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
| 25 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | class CRM_Badge_BAO_Layout extends CRM_Core_DAO_PrintLabel { |
| 36 | |
| 37 | /** |
| 38 | * Class constructor |
| 39 | */ |
| 40 | function __construct() { |
| 41 | parent::__construct(); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Takes a bunch of params that are needed to match certain criteria and |
| 46 | * retrieves the relevant objects. It also stores all the retrieved |
| 47 | * values in the default array |
| 48 | * |
| 49 | * @param array $params (reference ) an assoc array of name/value pairs |
| 50 | * @param array $defaults (reference ) an assoc array to hold the flattened values |
| 51 | * |
| 52 | * @return CRM_Core_DAO_PrintLabel object on success, null otherwise |
| 53 | * @access public |
| 54 | * @static |
| 55 | */ |
| 56 | static function retrieve(&$params, &$defaults) { |
| 57 | $printLabel = new CRM_Core_DAO_PrintLabel(); |
| 58 | $printLabel->copyValues($params); |
| 59 | if ($printLabel->find(TRUE)) { |
| 60 | CRM_Core_DAO::storeValues($printLabel, $defaults); |
| 61 | return $printLabel; |
| 62 | } |
| 63 | return NULL; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Update the is_active flag in the db |
| 68 | * |
| 69 | * @param int $id id of the database record |
| 70 | * @param boolean $is_active value we want to set the is_active field |
| 71 | * |
| 72 | * @return Object DAO object on success, null otherwise |
| 73 | * |
| 74 | * @access public |
| 75 | * @static |
| 76 | */ |
| 77 | static function setIsActive($id, $is_active) { |
| 78 | return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_PrintLabel', $id, 'is_active', $is_active); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Add a name label |
| 83 | * |
| 84 | * @param array $params reference array contains the values submitted by the form |
| 85 | * |
| 86 | * @access public |
| 87 | * @static |
| 88 | * |
| 89 | * @return object |
| 90 | */ |
| 91 | static function create(&$params) { |
| 92 | $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE); |
| 93 | $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE); |
| 94 | $params['is_reserved'] = CRM_Utils_Array::value('is_reserved', $params, FALSE); |
| 95 | |
| 96 | $params['label_type_id'] = CRM_Core_OptionGroup::getValue('label_type', 'Event Badge', 'name'); |
| 97 | |
| 98 | // check if new layout is create, if so set the created_id (if not set) |
| 99 | if (empty($params['id'])) { |
| 100 | if (empty($params['created_id'])) { |
| 101 | $session = CRM_Core_Session::singleton(); |
| 102 | $params['created_id'] = $session->get('userID'); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | if (!isset($params['id']) && !isset($params['name'])) { |
| 107 | $params['name'] = CRM_Utils_String::munge($params['title'], '_', 64); |
| 108 | } |
| 109 | |
| 110 | // action is taken depending upon the mode |
| 111 | $printLabel = new CRM_Core_DAO_PrintLabel(); |
| 112 | $printLabel->copyValues($params); |
| 113 | |
| 114 | if ($params['is_default']) { |
| 115 | $query = "UPDATE civicrm_print_label SET is_default = 0"; |
| 116 | CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray); |
| 117 | } |
| 118 | |
| 119 | $printLabel->save(); |
| 120 | return $printLabel; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Delete name labels |
| 125 | * |
| 126 | * @param int $printLabelId ID of the name label to be deleted. |
| 127 | * |
| 128 | * @access public |
| 129 | * @static |
| 130 | */ |
| 131 | static function del($printLabelId) { |
| 132 | $printLabel = new CRM_Core_DAO_PrintLabel(); |
| 133 | $printLabel->id = $printLabelId; |
| 134 | $printLabel->delete(); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * get the list of print labels |
| 139 | * |
| 140 | * @return array list of labels |
| 141 | * @access public |
| 142 | * @static |
| 143 | */ |
| 144 | static function getList() { |
| 145 | $printLabel = new CRM_Core_DAO_PrintLabel(); |
| 146 | $printLabel->find(); |
| 147 | |
| 148 | $labels = array(); |
| 149 | while($printLabel->fetch()) { |
| 150 | $labels[$printLabel->id] = $printLabel->title; |
| 151 | } |
| 152 | return $labels; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Build layout structure |
| 157 | * |
| 158 | * @param array $params associated array of submitted values |
| 159 | * |
| 160 | * @return array $formattedLayout array formatted array |
| 161 | * @access public |
| 162 | */ |
| 163 | static function buildLayout(&$params) { |
| 164 | $layoutParams = array('id' => $params['badge_id']); |
| 165 | CRM_Badge_BAO_Layout::retrieve($layoutParams, $layoutInfo); |
| 166 | |
| 167 | $formatProperties = CRM_Core_OptionGroup::getValue('name_badge', $layoutInfo['label_format_name'], 'name'); |
| 168 | $layoutInfo['format'] = json_decode($formatProperties, true); |
| 169 | $layoutInfo['data'] = CRM_Badge_BAO_Layout::getDecodedData($layoutInfo['data']); |
| 170 | return $layoutInfo; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Decode encoded data and return as an array |
| 175 | * |
| 176 | * @param json $jsonData json object |
| 177 | * |
| 178 | * @return array associated array of decoded elements |
| 179 | * @static |
| 180 | */ |
| 181 | static public function getDecodedData($jsonData) { |
| 182 | return json_decode($jsonData, true); |
| 183 | } |
| 184 | } |
| 185 | |