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