don't try to convert blanks
[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
19 /**
eceb18cc 20 * Class constructor.
d2e138ee 21 */
00be9182 22 public function __construct() {
d2e138ee
KJ
23 parent::__construct();
24 }
25
26 /**
fe482240
EM
27 * Retrieve DB object based on input parameters.
28 *
29 * It also stores all the retrieved values in the default array.
d2e138ee 30 *
691efc59
TO
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.
d2e138ee 35 *
16b10e64
CW
36 * @return CRM_Core_DAO_PrintLabel|null
37 * object on success, null otherwise
d2e138ee 38 */
00be9182 39 public static function retrieve(&$params, &$defaults) {
d2e138ee
KJ
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 /**
fe482240 50 * Update the is_active flag in the db.
d2e138ee 51 *
691efc59
TO
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.
d2e138ee 56 *
8a4fede3 57 * @return bool
58 * true if we found and updated the object, else false
d2e138ee 59 */
00be9182 60 public static function setIsActive($id, $is_active) {
d2e138ee
KJ
61 return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_PrintLabel', $id, 'is_active', $is_active);
62 }
63
64 /**
eceb18cc 65 * Add a name label.
d2e138ee 66 *
691efc59
TO
67 * @param array $params
68 * Reference array contains the values submitted by the form.
77b97be7 69 *
d2e138ee
KJ
70 *
71 * @return object
72 */
00be9182 73 public static function create(&$params) {
d2e138ee
KJ
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
0571e734 78 $params['label_type_id'] = CRM_Core_PseudoConstant::getKey('CRM_Core_DAO_PrintLabel', 'label_type_id', 'Event Badge');
636f1cbe
KJ
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
d2e138ee
KJ
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";
33621c4f 98 CRM_Core_DAO::executeQuery($query);
d2e138ee
KJ
99 }
100
101 $printLabel->save();
102 return $printLabel;
103 }
104
105 /**
eceb18cc 106 * Delete name labels.
d2e138ee 107 *
691efc59
TO
108 * @param int $printLabelId
109 * ID of the name label to be deleted.
d2e138ee 110 *
d2e138ee 111 */
00be9182 112 public static function del($printLabelId) {
d2e138ee
KJ
113 $printLabel = new CRM_Core_DAO_PrintLabel();
114 $printLabel->id = $printLabelId;
115 $printLabel->delete();
116 }
8c7f8a8b
KJ
117
118 /**
eceb18cc 119 * get the list of print labels.
8c7f8a8b 120 *
a6c01b45
CW
121 * @return array
122 * list of labels
8c7f8a8b 123 */
00be9182 124 public static function getList() {
8c7f8a8b
KJ
125 $printLabel = new CRM_Core_DAO_PrintLabel();
126 $printLabel->find();
127
be2fb01f 128 $labels = [];
22e263ad 129 while ($printLabel->fetch()) {
8c7f8a8b
KJ
130 $labels[$printLabel->id] = $printLabel->title;
131 }
132 return $labels;
133 }
134
135 /**
eceb18cc 136 * Build layout structure.
8c7f8a8b 137 *
691efc59
TO
138 * @param array $params
139 * Associated array of submitted values.
8c7f8a8b 140 *
a6c01b45
CW
141 * @return array
142 * array formatted array
8c7f8a8b 143 */
00be9182 144 public static function buildLayout(&$params) {
be2fb01f 145 $layoutParams = ['id' => $params['badge_id']];
8c7f8a8b
KJ
146 CRM_Badge_BAO_Layout::retrieve($layoutParams, $layoutInfo);
147
0571e734 148 $formatProperties = CRM_Core_PseudoConstant::getKey('CRM_Core_DAO_PrintLabel', 'label_format_name', $layoutInfo['label_format_name']);
149
3a872e59 150 $layoutInfo['format'] = json_decode($formatProperties, TRUE);
8c7f8a8b
KJ
151 $layoutInfo['data'] = CRM_Badge_BAO_Layout::getDecodedData($layoutInfo['data']);
152 return $layoutInfo;
153 }
154
155 /**
eceb18cc 156 * Decode encoded data and return as an array.
8c7f8a8b 157 *
691efc59
TO
158 * @param json $jsonData
159 * Json object.
8c7f8a8b 160 *
a6c01b45
CW
161 * @return array
162 * associated array of decoded elements
8c7f8a8b 163 */
f157740d 164 public static function getDecodedData($jsonData) {
3a872e59 165 return json_decode($jsonData, TRUE);
8c7f8a8b 166 }
96025800 167
d2e138ee 168}