[REF] - Deprecate & delegate BAO::retrieve
[civicrm-core.git] / CRM / Badge / BAO / Layout.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Badge_BAO_Layout extends CRM_Core_DAO_PrintLabel {
18
19 /**
20 * Retrieve DB object and copy to defaults array.
21 *
22 * @deprecated
23 * @param array $params
24 * @param array $defaults
25 *
26 * @return CRM_Core_DAO_PrintLabel|NULL
27 */
28 public static function retrieve($params, &$defaults) {
29 return self::commonRetrieve('CRM_Core_DAO_PrintLabel', $params, $defaults);
30 }
31
32 /**
33 * Update the is_active flag in the db.
34 *
35 * @param int $id
36 * Id of the database record.
37 * @param bool $is_active
38 * Value we want to set the is_active field.
39 *
40 * @return bool
41 * true if we found and updated the object, else false
42 */
43 public static function setIsActive($id, $is_active) {
44 return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_PrintLabel', $id, 'is_active', $is_active);
45 }
46
47 /**
48 * Add a name label.
49 *
50 * @param array $params
51 * Reference array contains the values submitted by the form.
52 *
53 *
54 * @return object
55 */
56 public static function create(&$params) {
57 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
58 $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE);
59 $params['is_reserved'] = CRM_Utils_Array::value('is_reserved', $params, FALSE);
60
61 $params['label_type_id'] = CRM_Core_PseudoConstant::getKey('CRM_Core_DAO_PrintLabel', 'label_type_id', 'Event Badge');
62
63 // check if new layout is create, if so set the created_id (if not set)
64 if (empty($params['id'])) {
65 if (empty($params['created_id'])) {
66 $session = CRM_Core_Session::singleton();
67 $params['created_id'] = $session->get('userID');
68 }
69 }
70
71 if (!isset($params['id']) && !isset($params['name'])) {
72 $params['name'] = CRM_Utils_String::munge($params['title'], '_', 64);
73 }
74
75 // action is taken depending upon the mode
76 $printLabel = new CRM_Core_DAO_PrintLabel();
77 $printLabel->copyValues($params);
78
79 if ($params['is_default']) {
80 $query = "UPDATE civicrm_print_label SET is_default = 0";
81 CRM_Core_DAO::executeQuery($query);
82 }
83
84 $printLabel->save();
85 return $printLabel;
86 }
87
88 /**
89 * Delete name labels.
90 *
91 * @param int $printLabelId
92 * @deprecated
93 */
94 public static function del($printLabelId) {
95 self::deleteRecord(['id' => $printLabelId]);
96 }
97
98 /**
99 * get the list of print labels.
100 *
101 * @return array
102 * list of labels
103 */
104 public static function getList() {
105 $printLabel = new CRM_Core_DAO_PrintLabel();
106 $printLabel->find();
107
108 $labels = [];
109 while ($printLabel->fetch()) {
110 $labels[$printLabel->id] = $printLabel->title;
111 }
112 return $labels;
113 }
114
115 /**
116 * Build layout structure.
117 *
118 * @param array $params
119 * Associated array of submitted values.
120 *
121 * @return array
122 * array formatted array
123 */
124 public static function buildLayout(&$params) {
125 $layoutParams = ['id' => $params['badge_id']];
126 CRM_Badge_BAO_Layout::retrieve($layoutParams, $layoutInfo);
127
128 $formatProperties = CRM_Core_PseudoConstant::getKey('CRM_Core_DAO_PrintLabel', 'label_format_name', $layoutInfo['label_format_name']);
129
130 $layoutInfo['format'] = json_decode($formatProperties, TRUE);
131 $layoutInfo['data'] = CRM_Badge_BAO_Layout::getDecodedData($layoutInfo['data']);
132 return $layoutInfo;
133 }
134
135 /**
136 * Decode encoded data and return as an array.
137 *
138 * @param string $jsonData
139 * Json object.
140 *
141 * @return array
142 * associated array of decoded elements
143 */
144 public static function getDecodedData($jsonData) {
145 return json_decode($jsonData, TRUE);
146 }
147
148 }