Commit | Line | Data |
---|---|---|
d2e138ee KJ |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
d2e138ee | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
d2e138ee KJ |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Badge_BAO_Layout extends CRM_Core_DAO_PrintLabel { | |
36 | ||
37 | /** | |
eceb18cc | 38 | * Class constructor. |
d2e138ee | 39 | */ |
00be9182 | 40 | public function __construct() { |
d2e138ee KJ |
41 | parent::__construct(); |
42 | } | |
43 | ||
44 | /** | |
eceb18cc | 45 | * Takes a bunch of params that are needed to match certain criteria and. |
d2e138ee KJ |
46 | * retrieves the relevant objects. It also stores all the retrieved |
47 | * values in the default array | |
48 | * | |
691efc59 TO |
49 | * @param array $params |
50 | * (reference ) an assoc array of name/value pairs. | |
51 | * @param array $defaults | |
52 | * (reference ) an assoc array to hold the flattened values. | |
d2e138ee | 53 | * |
16b10e64 CW |
54 | * @return CRM_Core_DAO_PrintLabel|null |
55 | * object on success, null otherwise | |
d2e138ee | 56 | */ |
00be9182 | 57 | public static function retrieve(&$params, &$defaults) { |
d2e138ee KJ |
58 | $printLabel = new CRM_Core_DAO_PrintLabel(); |
59 | $printLabel->copyValues($params); | |
60 | if ($printLabel->find(TRUE)) { | |
61 | CRM_Core_DAO::storeValues($printLabel, $defaults); | |
62 | return $printLabel; | |
63 | } | |
64 | return NULL; | |
65 | } | |
66 | ||
67 | /** | |
100fef9d | 68 | * Update the is_active flag in the db |
d2e138ee | 69 | * |
691efc59 TO |
70 | * @param int $id |
71 | * Id of the database record. | |
72 | * @param bool $is_active | |
73 | * Value we want to set the is_active field. | |
d2e138ee | 74 | * |
a6c01b45 CW |
75 | * @return Object |
76 | * DAO object on success, null otherwise | |
d2e138ee | 77 | * |
d2e138ee | 78 | */ |
00be9182 | 79 | public static function setIsActive($id, $is_active) { |
d2e138ee KJ |
80 | return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_PrintLabel', $id, 'is_active', $is_active); |
81 | } | |
82 | ||
83 | /** | |
eceb18cc | 84 | * Add a name label. |
d2e138ee | 85 | * |
691efc59 TO |
86 | * @param array $params |
87 | * Reference array contains the values submitted by the form. | |
77b97be7 | 88 | * |
d2e138ee KJ |
89 | * |
90 | * @return object | |
91 | */ | |
00be9182 | 92 | public static function create(&$params) { |
d2e138ee KJ |
93 | $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE); |
94 | $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE); | |
95 | $params['is_reserved'] = CRM_Utils_Array::value('is_reserved', $params, FALSE); | |
96 | ||
636f1cbe KJ |
97 | $params['label_type_id'] = CRM_Core_OptionGroup::getValue('label_type', 'Event Badge', 'name'); |
98 | ||
99 | // check if new layout is create, if so set the created_id (if not set) | |
100 | if (empty($params['id'])) { | |
101 | if (empty($params['created_id'])) { | |
102 | $session = CRM_Core_Session::singleton(); | |
103 | $params['created_id'] = $session->get('userID'); | |
104 | } | |
105 | } | |
106 | ||
107 | if (!isset($params['id']) && !isset($params['name'])) { | |
108 | $params['name'] = CRM_Utils_String::munge($params['title'], '_', 64); | |
109 | } | |
110 | ||
d2e138ee KJ |
111 | // action is taken depending upon the mode |
112 | $printLabel = new CRM_Core_DAO_PrintLabel(); | |
113 | $printLabel->copyValues($params); | |
114 | ||
115 | if ($params['is_default']) { | |
116 | $query = "UPDATE civicrm_print_label SET is_default = 0"; | |
117 | CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray); | |
118 | } | |
119 | ||
120 | $printLabel->save(); | |
121 | return $printLabel; | |
122 | } | |
123 | ||
124 | /** | |
eceb18cc | 125 | * Delete name labels. |
d2e138ee | 126 | * |
691efc59 TO |
127 | * @param int $printLabelId |
128 | * ID of the name label to be deleted. | |
d2e138ee | 129 | * |
d2e138ee | 130 | */ |
00be9182 | 131 | public static function del($printLabelId) { |
d2e138ee KJ |
132 | $printLabel = new CRM_Core_DAO_PrintLabel(); |
133 | $printLabel->id = $printLabelId; | |
134 | $printLabel->delete(); | |
135 | } | |
8c7f8a8b KJ |
136 | |
137 | /** | |
eceb18cc | 138 | * get the list of print labels. |
8c7f8a8b | 139 | * |
a6c01b45 CW |
140 | * @return array |
141 | * list of labels | |
8c7f8a8b | 142 | */ |
00be9182 | 143 | public static function getList() { |
8c7f8a8b KJ |
144 | $printLabel = new CRM_Core_DAO_PrintLabel(); |
145 | $printLabel->find(); | |
146 | ||
147 | $labels = array(); | |
22e263ad | 148 | while ($printLabel->fetch()) { |
8c7f8a8b KJ |
149 | $labels[$printLabel->id] = $printLabel->title; |
150 | } | |
151 | return $labels; | |
152 | } | |
153 | ||
154 | /** | |
eceb18cc | 155 | * Build layout structure. |
8c7f8a8b | 156 | * |
691efc59 TO |
157 | * @param array $params |
158 | * Associated array of submitted values. | |
8c7f8a8b | 159 | * |
a6c01b45 CW |
160 | * @return array |
161 | * array formatted array | |
8c7f8a8b | 162 | */ |
00be9182 | 163 | public static function buildLayout(&$params) { |
d35b4c07 | 164 | $layoutParams = array('id' => $params['badge_id']); |
8c7f8a8b KJ |
165 | CRM_Badge_BAO_Layout::retrieve($layoutParams, $layoutInfo); |
166 | ||
167 | $formatProperties = CRM_Core_OptionGroup::getValue('name_badge', $layoutInfo['label_format_name'], 'name'); | |
3a872e59 | 168 | $layoutInfo['format'] = json_decode($formatProperties, TRUE); |
8c7f8a8b KJ |
169 | $layoutInfo['data'] = CRM_Badge_BAO_Layout::getDecodedData($layoutInfo['data']); |
170 | return $layoutInfo; | |
171 | } | |
172 | ||
173 | /** | |
eceb18cc | 174 | * Decode encoded data and return as an array. |
8c7f8a8b | 175 | * |
691efc59 TO |
176 | * @param json $jsonData |
177 | * Json object. | |
8c7f8a8b | 178 | * |
a6c01b45 CW |
179 | * @return array |
180 | * associated array of decoded elements | |
8c7f8a8b KJ |
181 | */ |
182 | static public function getDecodedData($jsonData) { | |
3a872e59 | 183 | return json_decode($jsonData, TRUE); |
8c7f8a8b | 184 | } |
96025800 | 185 | |
d2e138ee | 186 | } |