Merge pull request #22426 from colemanw/writeRecordsCustom
[civicrm-core.git] / CRM / Badge / BAO / Layout.php
CommitLineData
d2e138ee
KJ
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
d2e138ee 5 | |
bc77d7c0
TO
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 |
d2e138ee 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
d2e138ee
KJ
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
d2e138ee
KJ
16 */
17class CRM_Badge_BAO_Layout extends CRM_Core_DAO_PrintLabel {
18
d2e138ee 19 /**
fe482240
EM
20 * Retrieve DB object based on input parameters.
21 *
22 * It also stores all the retrieved values in the default array.
d2e138ee 23 *
691efc59
TO
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.
d2e138ee 28 *
16b10e64
CW
29 * @return CRM_Core_DAO_PrintLabel|null
30 * object on success, null otherwise
d2e138ee 31 */
00be9182 32 public static function retrieve(&$params, &$defaults) {
d2e138ee
KJ
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 /**
fe482240 43 * Update the is_active flag in the db.
d2e138ee 44 *
691efc59
TO
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.
d2e138ee 49 *
8a4fede3 50 * @return bool
51 * true if we found and updated the object, else false
d2e138ee 52 */
00be9182 53 public static function setIsActive($id, $is_active) {
d2e138ee
KJ
54 return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_PrintLabel', $id, 'is_active', $is_active);
55 }
56
57 /**
eceb18cc 58 * Add a name label.
d2e138ee 59 *
691efc59
TO
60 * @param array $params
61 * Reference array contains the values submitted by the form.
77b97be7 62 *
d2e138ee
KJ
63 *
64 * @return object
65 */
00be9182 66 public static function create(&$params) {
d2e138ee
KJ
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
0571e734 71 $params['label_type_id'] = CRM_Core_PseudoConstant::getKey('CRM_Core_DAO_PrintLabel', 'label_type_id', 'Event Badge');
636f1cbe
KJ
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
d2e138ee
KJ
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";
33621c4f 91 CRM_Core_DAO::executeQuery($query);
d2e138ee
KJ
92 }
93
94 $printLabel->save();
95 return $printLabel;
96 }
97
98 /**
eceb18cc 99 * Delete name labels.
d2e138ee 100 *
691efc59 101 * @param int $printLabelId
67d870c5 102 * @deprecated
d2e138ee 103 */
00be9182 104 public static function del($printLabelId) {
67d870c5 105 self::deleteRecord(['id' => $printLabelId]);
d2e138ee 106 }
8c7f8a8b
KJ
107
108 /**
eceb18cc 109 * get the list of print labels.
8c7f8a8b 110 *
a6c01b45
CW
111 * @return array
112 * list of labels
8c7f8a8b 113 */
00be9182 114 public static function getList() {
8c7f8a8b
KJ
115 $printLabel = new CRM_Core_DAO_PrintLabel();
116 $printLabel->find();
117
be2fb01f 118 $labels = [];
22e263ad 119 while ($printLabel->fetch()) {
8c7f8a8b
KJ
120 $labels[$printLabel->id] = $printLabel->title;
121 }
122 return $labels;
123 }
124
125 /**
eceb18cc 126 * Build layout structure.
8c7f8a8b 127 *
691efc59
TO
128 * @param array $params
129 * Associated array of submitted values.
8c7f8a8b 130 *
a6c01b45
CW
131 * @return array
132 * array formatted array
8c7f8a8b 133 */
00be9182 134 public static function buildLayout(&$params) {
be2fb01f 135 $layoutParams = ['id' => $params['badge_id']];
8c7f8a8b
KJ
136 CRM_Badge_BAO_Layout::retrieve($layoutParams, $layoutInfo);
137
0571e734 138 $formatProperties = CRM_Core_PseudoConstant::getKey('CRM_Core_DAO_PrintLabel', 'label_format_name', $layoutInfo['label_format_name']);
139
3a872e59 140 $layoutInfo['format'] = json_decode($formatProperties, TRUE);
8c7f8a8b
KJ
141 $layoutInfo['data'] = CRM_Badge_BAO_Layout::getDecodedData($layoutInfo['data']);
142 return $layoutInfo;
143 }
144
145 /**
eceb18cc 146 * Decode encoded data and return as an array.
8c7f8a8b 147 *
691efc59
TO
148 * @param json $jsonData
149 * Json object.
8c7f8a8b 150 *
a6c01b45
CW
151 * @return array
152 * associated array of decoded elements
8c7f8a8b 153 */
f157740d 154 public static function getDecodedData($jsonData) {
3a872e59 155 return json_decode($jsonData, TRUE);
8c7f8a8b 156 }
96025800 157
d2e138ee 158}