Ian province abbreviation patch - issue 724
[civicrm-core.git] / CRM / Badge / BAO / Layout.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
32 */
33 class CRM_Badge_BAO_Layout extends CRM_Core_DAO_PrintLabel {
34
35 /**
36 * Class constructor.
37 */
38 public function __construct() {
39 parent::__construct();
40 }
41
42 /**
43 * Retrieve DB object based on input parameters.
44 *
45 * It also stores all the retrieved values in the default array.
46 *
47 * @param array $params
48 * (reference ) an assoc array of name/value pairs.
49 * @param array $defaults
50 * (reference ) an assoc array to hold the flattened values.
51 *
52 * @return CRM_Core_DAO_PrintLabel|null
53 * object on success, null otherwise
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
69 * Id of the database record.
70 * @param bool $is_active
71 * Value we want to set the is_active field.
72 *
73 * @return Object
74 * DAO object on success, null otherwise
75 *
76 */
77 public 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
85 * Reference array contains the values submitted by the form.
86 *
87 *
88 * @return object
89 */
90 public static function create(&$params) {
91 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
92 $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE);
93 $params['is_reserved'] = CRM_Utils_Array::value('is_reserved', $params, FALSE);
94
95 $params['label_type_id'] = CRM_Core_OptionGroup::getValue('label_type', 'Event Badge', 'name');
96
97 // check if new layout is create, if so set the created_id (if not set)
98 if (empty($params['id'])) {
99 if (empty($params['created_id'])) {
100 $session = CRM_Core_Session::singleton();
101 $params['created_id'] = $session->get('userID');
102 }
103 }
104
105 if (!isset($params['id']) && !isset($params['name'])) {
106 $params['name'] = CRM_Utils_String::munge($params['title'], '_', 64);
107 }
108
109 // action is taken depending upon the mode
110 $printLabel = new CRM_Core_DAO_PrintLabel();
111 $printLabel->copyValues($params);
112
113 if ($params['is_default']) {
114 $query = "UPDATE civicrm_print_label SET is_default = 0";
115 CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray);
116 }
117
118 $printLabel->save();
119 return $printLabel;
120 }
121
122 /**
123 * Delete name labels.
124 *
125 * @param int $printLabelId
126 * ID of the name label to be deleted.
127 *
128 */
129 public static function del($printLabelId) {
130 $printLabel = new CRM_Core_DAO_PrintLabel();
131 $printLabel->id = $printLabelId;
132 $printLabel->delete();
133 }
134
135 /**
136 * get the list of print labels.
137 *
138 * @return array
139 * list of labels
140 */
141 public static function getList() {
142 $printLabel = new CRM_Core_DAO_PrintLabel();
143 $printLabel->find();
144
145 $labels = array();
146 while ($printLabel->fetch()) {
147 $labels[$printLabel->id] = $printLabel->title;
148 }
149 return $labels;
150 }
151
152 /**
153 * Build layout structure.
154 *
155 * @param array $params
156 * Associated array of submitted values.
157 *
158 * @return array
159 * array formatted array
160 */
161 public static function buildLayout(&$params) {
162 $layoutParams = array('id' => $params['badge_id']);
163 CRM_Badge_BAO_Layout::retrieve($layoutParams, $layoutInfo);
164
165 $formatProperties = CRM_Core_OptionGroup::getValue('name_badge', $layoutInfo['label_format_name'], 'name');
166 $layoutInfo['format'] = json_decode($formatProperties, TRUE);
167 $layoutInfo['data'] = CRM_Badge_BAO_Layout::getDecodedData($layoutInfo['data']);
168 return $layoutInfo;
169 }
170
171 /**
172 * Decode encoded data and return as an array.
173 *
174 * @param json $jsonData
175 * Json object.
176 *
177 * @return array
178 * associated array of decoded elements
179 */
180 static public function getDecodedData($jsonData) {
181 return json_decode($jsonData, TRUE);
182 }
183
184 }