Merge pull request #22420 from eileenmcnaughton/case
[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 based on input parameters.
21 *
22 * It also stores all the retrieved values in the default array.
23 *
24 * @param array $params
25 * (reference ) an assoc array of name/value pairs.
26 * @param array $defaults
27 * (reference ) an assoc array to hold the flattened values.
28 *
29 * @return CRM_Core_DAO_PrintLabel|null
30 * object on success, null otherwise
31 */
32 public static function retrieve(&$params, &$defaults) {
33 $printLabel = new CRM_Core_DAO_PrintLabel();
34 $printLabel->copyValues($params);
35 if ($printLabel->find(TRUE)) {
36 CRM_Core_DAO::storeValues($printLabel, $defaults);
37 return $printLabel;
38 }
39 return NULL;
40 }
41
42 /**
43 * Update the is_active flag in the db.
44 *
45 * @param int $id
46 * Id of the database record.
47 * @param bool $is_active
48 * Value we want to set the is_active field.
49 *
50 * @return bool
51 * true if we found and updated the object, else false
52 */
53 public static function setIsActive($id, $is_active) {
54 return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_PrintLabel', $id, 'is_active', $is_active);
55 }
56
57 /**
58 * Add a name label.
59 *
60 * @param array $params
61 * Reference array contains the values submitted by the form.
62 *
63 *
64 * @return object
65 */
66 public static function create(&$params) {
67 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
68 $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE);
69 $params['is_reserved'] = CRM_Utils_Array::value('is_reserved', $params, FALSE);
70
71 $params['label_type_id'] = CRM_Core_PseudoConstant::getKey('CRM_Core_DAO_PrintLabel', 'label_type_id', 'Event Badge');
72
73 // check if new layout is create, if so set the created_id (if not set)
74 if (empty($params['id'])) {
75 if (empty($params['created_id'])) {
76 $session = CRM_Core_Session::singleton();
77 $params['created_id'] = $session->get('userID');
78 }
79 }
80
81 if (!isset($params['id']) && !isset($params['name'])) {
82 $params['name'] = CRM_Utils_String::munge($params['title'], '_', 64);
83 }
84
85 // action is taken depending upon the mode
86 $printLabel = new CRM_Core_DAO_PrintLabel();
87 $printLabel->copyValues($params);
88
89 if ($params['is_default']) {
90 $query = "UPDATE civicrm_print_label SET is_default = 0";
91 CRM_Core_DAO::executeQuery($query);
92 }
93
94 $printLabel->save();
95 return $printLabel;
96 }
97
98 /**
99 * Delete name labels.
100 *
101 * @param int $printLabelId
102 * @deprecated
103 */
104 public static function del($printLabelId) {
105 self::deleteRecord(['id' => $printLabelId]);
106 }
107
108 /**
109 * get the list of print labels.
110 *
111 * @return array
112 * list of labels
113 */
114 public static function getList() {
115 $printLabel = new CRM_Core_DAO_PrintLabel();
116 $printLabel->find();
117
118 $labels = [];
119 while ($printLabel->fetch()) {
120 $labels[$printLabel->id] = $printLabel->title;
121 }
122 return $labels;
123 }
124
125 /**
126 * Build layout structure.
127 *
128 * @param array $params
129 * Associated array of submitted values.
130 *
131 * @return array
132 * array formatted array
133 */
134 public static function buildLayout(&$params) {
135 $layoutParams = ['id' => $params['badge_id']];
136 CRM_Badge_BAO_Layout::retrieve($layoutParams, $layoutInfo);
137
138 $formatProperties = CRM_Core_PseudoConstant::getKey('CRM_Core_DAO_PrintLabel', 'label_format_name', $layoutInfo['label_format_name']);
139
140 $layoutInfo['format'] = json_decode($formatProperties, TRUE);
141 $layoutInfo['data'] = CRM_Badge_BAO_Layout::getDecodedData($layoutInfo['data']);
142 return $layoutInfo;
143 }
144
145 /**
146 * Decode encoded data and return as an array.
147 *
148 * @param json $jsonData
149 * Json object.
150 *
151 * @return array
152 * associated array of decoded elements
153 */
154 public static function getDecodedData($jsonData) {
155 return json_decode($jsonData, TRUE);
156 }
157
158 }