Commit | Line | Data |
---|---|---|
d2e138ee KJ |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
d2e138ee | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
d2e138ee KJ |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
d2e138ee KJ |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
d2e138ee KJ |
32 | */ |
33 | class CRM_Badge_BAO_Layout extends CRM_Core_DAO_PrintLabel { | |
34 | ||
35 | /** | |
eceb18cc | 36 | * Class constructor. |
d2e138ee | 37 | */ |
00be9182 | 38 | public function __construct() { |
d2e138ee KJ |
39 | parent::__construct(); |
40 | } | |
41 | ||
42 | /** | |
fe482240 EM |
43 | * Retrieve DB object based on input parameters. |
44 | * | |
45 | * It also stores all the retrieved values in the default array. | |
d2e138ee | 46 | * |
691efc59 TO |
47 | * @param array $params |
48 | * (reference ) an assoc array of name/value pairs. | |
49 | * @param array $defaults | |
50 | * (reference ) an assoc array to hold the flattened values. | |
d2e138ee | 51 | * |
16b10e64 CW |
52 | * @return CRM_Core_DAO_PrintLabel|null |
53 | * object on success, null otherwise | |
d2e138ee | 54 | */ |
00be9182 | 55 | public static function retrieve(&$params, &$defaults) { |
d2e138ee KJ |
56 | $printLabel = new CRM_Core_DAO_PrintLabel(); |
57 | $printLabel->copyValues($params); | |
58 | if ($printLabel->find(TRUE)) { | |
59 | CRM_Core_DAO::storeValues($printLabel, $defaults); | |
60 | return $printLabel; | |
61 | } | |
62 | return NULL; | |
63 | } | |
64 | ||
65 | /** | |
fe482240 | 66 | * Update the is_active flag in the db. |
d2e138ee | 67 | * |
691efc59 TO |
68 | * @param int $id |
69 | * Id of the database record. | |
70 | * @param bool $is_active | |
71 | * Value we want to set the is_active field. | |
d2e138ee | 72 | * |
8a4fede3 | 73 | * @return bool |
74 | * true if we found and updated the object, else false | |
d2e138ee | 75 | */ |
00be9182 | 76 | public static function setIsActive($id, $is_active) { |
d2e138ee KJ |
77 | return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_PrintLabel', $id, 'is_active', $is_active); |
78 | } | |
79 | ||
80 | /** | |
eceb18cc | 81 | * Add a name label. |
d2e138ee | 82 | * |
691efc59 TO |
83 | * @param array $params |
84 | * Reference array contains the values submitted by the form. | |
77b97be7 | 85 | * |
d2e138ee KJ |
86 | * |
87 | * @return object | |
88 | */ | |
00be9182 | 89 | public static function create(&$params) { |
d2e138ee KJ |
90 | $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE); |
91 | $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE); | |
92 | $params['is_reserved'] = CRM_Utils_Array::value('is_reserved', $params, FALSE); | |
93 | ||
0571e734 | 94 | $params['label_type_id'] = CRM_Core_PseudoConstant::getKey('CRM_Core_DAO_PrintLabel', 'label_type_id', 'Event Badge'); |
636f1cbe KJ |
95 | |
96 | // check if new layout is create, if so set the created_id (if not set) | |
97 | if (empty($params['id'])) { | |
98 | if (empty($params['created_id'])) { | |
99 | $session = CRM_Core_Session::singleton(); | |
100 | $params['created_id'] = $session->get('userID'); | |
101 | } | |
102 | } | |
103 | ||
104 | if (!isset($params['id']) && !isset($params['name'])) { | |
105 | $params['name'] = CRM_Utils_String::munge($params['title'], '_', 64); | |
106 | } | |
107 | ||
d2e138ee KJ |
108 | // action is taken depending upon the mode |
109 | $printLabel = new CRM_Core_DAO_PrintLabel(); | |
110 | $printLabel->copyValues($params); | |
111 | ||
112 | if ($params['is_default']) { | |
113 | $query = "UPDATE civicrm_print_label SET is_default = 0"; | |
33621c4f | 114 | CRM_Core_DAO::executeQuery($query); |
d2e138ee KJ |
115 | } |
116 | ||
117 | $printLabel->save(); | |
118 | return $printLabel; | |
119 | } | |
120 | ||
121 | /** | |
eceb18cc | 122 | * Delete name labels. |
d2e138ee | 123 | * |
691efc59 TO |
124 | * @param int $printLabelId |
125 | * ID of the name label to be deleted. | |
d2e138ee | 126 | * |
d2e138ee | 127 | */ |
00be9182 | 128 | public static function del($printLabelId) { |
d2e138ee KJ |
129 | $printLabel = new CRM_Core_DAO_PrintLabel(); |
130 | $printLabel->id = $printLabelId; | |
131 | $printLabel->delete(); | |
132 | } | |
8c7f8a8b KJ |
133 | |
134 | /** | |
eceb18cc | 135 | * get the list of print labels. |
8c7f8a8b | 136 | * |
a6c01b45 CW |
137 | * @return array |
138 | * list of labels | |
8c7f8a8b | 139 | */ |
00be9182 | 140 | public static function getList() { |
8c7f8a8b KJ |
141 | $printLabel = new CRM_Core_DAO_PrintLabel(); |
142 | $printLabel->find(); | |
143 | ||
be2fb01f | 144 | $labels = []; |
22e263ad | 145 | while ($printLabel->fetch()) { |
8c7f8a8b KJ |
146 | $labels[$printLabel->id] = $printLabel->title; |
147 | } | |
148 | return $labels; | |
149 | } | |
150 | ||
151 | /** | |
eceb18cc | 152 | * Build layout structure. |
8c7f8a8b | 153 | * |
691efc59 TO |
154 | * @param array $params |
155 | * Associated array of submitted values. | |
8c7f8a8b | 156 | * |
a6c01b45 CW |
157 | * @return array |
158 | * array formatted array | |
8c7f8a8b | 159 | */ |
00be9182 | 160 | public static function buildLayout(&$params) { |
be2fb01f | 161 | $layoutParams = ['id' => $params['badge_id']]; |
8c7f8a8b KJ |
162 | CRM_Badge_BAO_Layout::retrieve($layoutParams, $layoutInfo); |
163 | ||
0571e734 | 164 | $formatProperties = CRM_Core_PseudoConstant::getKey('CRM_Core_DAO_PrintLabel', 'label_format_name', $layoutInfo['label_format_name']); |
165 | ||
3a872e59 | 166 | $layoutInfo['format'] = json_decode($formatProperties, TRUE); |
8c7f8a8b KJ |
167 | $layoutInfo['data'] = CRM_Badge_BAO_Layout::getDecodedData($layoutInfo['data']); |
168 | return $layoutInfo; | |
169 | } | |
170 | ||
171 | /** | |
eceb18cc | 172 | * Decode encoded data and return as an array. |
8c7f8a8b | 173 | * |
691efc59 TO |
174 | * @param json $jsonData |
175 | * Json object. | |
8c7f8a8b | 176 | * |
a6c01b45 CW |
177 | * @return array |
178 | * associated array of decoded elements | |
8c7f8a8b | 179 | */ |
f157740d | 180 | public static function getDecodedData($jsonData) { |
3a872e59 | 181 | return json_decode($jsonData, TRUE); |
8c7f8a8b | 182 | } |
96025800 | 183 | |
d2e138ee | 184 | } |