Merge pull request #4822 from kurund/indentation-fixes
[civicrm-core.git] / CRM / Badge / BAO / Layout.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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 public 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 * @static
54 */
55 public static function retrieve(&$params, &$defaults) {
56 $printLabel = new CRM_Core_DAO_PrintLabel();
57 $printLabel->copyValues($params);
58 if ($printLabel->find(TRUE)) {
59 CRM_Core_DAO::storeValues($printLabel, $defaults);
60 return $printLabel;
61 }
62 return NULL;
63 }
64
65 /**
66 * Update the is_active flag in the db
67 *
68 * @param int $id id of the database record
69 * @param boolean $is_active value we want to set the is_active field
70 *
71 * @return Object DAO object on success, null otherwise
72 *
73 * @static
74 */
75 public static function setIsActive($id, $is_active) {
76 return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_PrintLabel', $id, 'is_active', $is_active);
77 }
78
79 /**
80 * Add a name label
81 *
82 * @param array $params reference array contains the values submitted by the form
83 *
84 * @static
85 *
86 * @return object
87 */
88 public static function create(&$params) {
89 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
90 $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE);
91 $params['is_reserved'] = CRM_Utils_Array::value('is_reserved', $params, FALSE);
92
93 $params['label_type_id'] = CRM_Core_OptionGroup::getValue('label_type', 'Event Badge', 'name');
94
95 // check if new layout is create, if so set the created_id (if not set)
96 if (empty($params['id'])) {
97 if (empty($params['created_id'])) {
98 $session = CRM_Core_Session::singleton();
99 $params['created_id'] = $session->get('userID');
100 }
101 }
102
103 if (!isset($params['id']) && !isset($params['name'])) {
104 $params['name'] = CRM_Utils_String::munge($params['title'], '_', 64);
105 }
106
107 // action is taken depending upon the mode
108 $printLabel = new CRM_Core_DAO_PrintLabel();
109 $printLabel->copyValues($params);
110
111 if ($params['is_default']) {
112 $query = "UPDATE civicrm_print_label SET is_default = 0";
113 CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray);
114 }
115
116 $printLabel->save();
117 return $printLabel;
118 }
119
120 /**
121 * Delete name labels
122 *
123 * @param int $printLabelId ID of the name label to be deleted.
124 *
125 * @static
126 */
127 public static function del($printLabelId) {
128 $printLabel = new CRM_Core_DAO_PrintLabel();
129 $printLabel->id = $printLabelId;
130 $printLabel->delete();
131 }
132
133 /**
134 * get the list of print labels
135 *
136 * @return array list of labels
137 * @static
138 */
139 public static function getList() {
140 $printLabel = new CRM_Core_DAO_PrintLabel();
141 $printLabel->find();
142
143 $labels = array();
144 while($printLabel->fetch()) {
145 $labels[$printLabel->id] = $printLabel->title;
146 }
147 return $labels;
148 }
149
150 /**
151 * Build layout structure
152 *
153 * @param array $params associated array of submitted values
154 *
155 * @return array $formattedLayout array formatted array
156 */
157 public static function buildLayout(&$params) {
158 $layoutParams = array('id' => $params['badge_id']);
159 CRM_Badge_BAO_Layout::retrieve($layoutParams, $layoutInfo);
160
161 $formatProperties = CRM_Core_OptionGroup::getValue('name_badge', $layoutInfo['label_format_name'], 'name');
162 $layoutInfo['format'] = json_decode($formatProperties, true);
163 $layoutInfo['data'] = CRM_Badge_BAO_Layout::getDecodedData($layoutInfo['data']);
164 return $layoutInfo;
165 }
166
167 /**
168 * Decode encoded data and return as an array
169 *
170 * @param json $jsonData json object
171 *
172 * @return array associated array of decoded elements
173 * @static
174 */
175 static public function getDecodedData($jsonData) {
176 return json_decode($jsonData, true);
177 }
178 }