Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Core_BAO_Tag extends CRM_Core_DAO_Tag { | |
36 | ||
37 | /** | |
38 | * class constructor | |
39 | */ | |
40 | function __construct() { | |
41 | parent::__construct(); | |
42 | } | |
43 | ||
44 | /** | |
45 | * Takes a bunch of params that are needed to match certain criteria and | |
46 | * retrieves the relevant objects. Typically the valid params are only | |
47 | * contact_id. We'll tweak this function to be more full featured over a period | |
48 | * of time. This is the inverse function of create. It also stores all the retrieved | |
49 | * values in the default array | |
50 | * | |
51 | * @param array $params (reference ) an assoc array of name/value pairs | |
52 | * @param array $defaults (reference ) an assoc array to hold the flattened values | |
53 | * | |
54 | * @return object CRM_Core_DAO_Tag object on success, otherwise null | |
55 | * @access public | |
56 | * @static | |
57 | */ | |
58 | static function retrieve(&$params, &$defaults) { | |
59 | $tag = new CRM_Core_DAO_Tag(); | |
60 | $tag->copyValues($params); | |
61 | if ($tag->find(TRUE)) { | |
62 | CRM_Core_DAO::storeValues($tag, $defaults); | |
63 | return $tag; | |
64 | } | |
65 | return NULL; | |
66 | } | |
67 | ||
b5c2afd0 EM |
68 | /** |
69 | * @param null $usedFor | |
70 | * @param bool $excludeHidden | |
71 | * | |
72 | * @return mixed | |
73 | */ | |
6a488035 TO |
74 | function getTree($usedFor = NULL, $excludeHidden = FALSE) { |
75 | if (!isset($this->tree)) { | |
76 | $this->buildTree($usedFor, $excludeHidden); | |
77 | } | |
78 | return $this->tree; | |
79 | } | |
80 | ||
b5c2afd0 | 81 | /** |
80257742 | 82 | * Build a nested array from hierarchical tags. Supports infinite levels of nesting. |
b5c2afd0 EM |
83 | * @param null $usedFor |
84 | * @param bool $excludeHidden | |
85 | */ | |
6a488035 | 86 | function buildTree($usedFor = NULL, $excludeHidden = FALSE) { |
6dac2504 | 87 | $sql = "SELECT id, parent_id, name, description, is_selectable FROM civicrm_tag"; |
6a488035 TO |
88 | |
89 | $whereClause = array(); | |
90 | if ($usedFor) { | |
91 | $whereClause[] = "used_for like '%{$usedFor}%'"; | |
92 | } | |
93 | if ($excludeHidden) { | |
94 | $whereClause[] = "is_tagset = 0"; | |
95 | } | |
96 | ||
97 | if (!empty($whereClause)) { | |
98 | $sql .= " WHERE " . implode(' AND ', $whereClause); | |
99 | } | |
100 | ||
101 | $sql .= " ORDER BY parent_id,name"; | |
102 | ||
103 | $dao = CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray, TRUE, NULL, FALSE, FALSE); | |
104 | ||
80257742 | 105 | $refs = array(); |
6a488035 | 106 | while ($dao->fetch()) { |
80257742 CW |
107 | $thisref = &$refs[$dao->id]; |
108 | ||
109 | $thisref['parent_id'] = $dao->parent_id; | |
110 | $thisref['name'] = $dao->name; | |
111 | $thisref['description'] = $dao->description; | |
6dac2504 | 112 | $thisref['is_selectable'] = $dao->is_selectable; |
80257742 | 113 | |
6a488035 | 114 | if (!$dao->parent_id) { |
80257742 | 115 | $this->tree[$dao->id] = &$thisref; |
6a488035 TO |
116 | } |
117 | else { | |
80257742 | 118 | $refs[$dao->parent_id]['children'][$dao->id] = &$thisref; |
6a488035 TO |
119 | } |
120 | } | |
6a488035 | 121 | |
6a488035 TO |
122 | } |
123 | ||
b5c2afd0 EM |
124 | /** |
125 | * @param array $usedFor | |
126 | * @param bool $buildSelect | |
127 | * @param bool $all | |
128 | * @param null $parentId | |
129 | * | |
130 | * @return array | |
131 | */ | |
6a488035 TO |
132 | static function getTagsUsedFor($usedFor = array('civicrm_contact'), |
133 | $buildSelect = TRUE, | |
134 | $all = FALSE, | |
135 | $parentId = NULL | |
136 | ) { | |
137 | $tags = array(); | |
138 | ||
139 | if (empty($usedFor)) { | |
140 | return $tags; | |
141 | } | |
142 | if (!is_array($usedFor)) { | |
143 | $usedFor = array($usedFor); | |
144 | } | |
145 | ||
146 | if ($parentId === NULL) { | |
147 | $parentClause = " parent_id IS NULL AND "; | |
148 | } | |
149 | else { | |
150 | $parentClause = " parent_id = {$parentId} AND "; | |
151 | } | |
152 | ||
153 | foreach ($usedFor as $entityTable) { | |
154 | $tag = new CRM_Core_DAO_Tag(); | |
155 | $tag->fields(); | |
156 | $tag->orderBy('parent_id'); | |
157 | if ($buildSelect) { | |
158 | $tag->whereAdd("is_tagset = 0 AND {$parentClause} used_for LIKE '%{$entityTable}%'"); | |
159 | } | |
160 | else { | |
161 | $tag->whereAdd("used_for LIKE '%{$entityTable}%'"); | |
162 | } | |
163 | if (!$all) { | |
164 | $tag->is_tagset = 0; | |
165 | } | |
166 | $tag->find(); | |
167 | ||
168 | while ($tag->fetch()) { | |
169 | if ($buildSelect) { | |
170 | $tags[$tag->id] = $tag->name; | |
171 | } | |
172 | else { | |
173 | $tags[$tag->id]['name'] = $tag->name; | |
174 | $tags[$tag->id]['parent_id'] = $tag->parent_id; | |
175 | $tags[$tag->id]['is_tagset'] = $tag->is_tagset; | |
176 | $tags[$tag->id]['used_for'] = $tag->used_for; | |
177 | } | |
178 | } | |
179 | $tag->free(); | |
180 | } | |
181 | ||
182 | return $tags; | |
183 | } | |
184 | ||
b5c2afd0 EM |
185 | /** |
186 | * @param string $usedFor | |
187 | * @param array $tags | |
188 | * @param null $parentId | |
189 | * @param string $separator | |
190 | * | |
191 | * @return array | |
192 | */ | |
6a488035 TO |
193 | static function getTags($usedFor = 'civicrm_contact', |
194 | &$tags = array(), | |
195 | $parentId = NULL, | |
196 | $separator = ' ' | |
197 | ) { | |
dee5ccbb CW |
198 | if (!is_array($tags)) { |
199 | $tags = array(); | |
200 | } | |
6a488035 TO |
201 | // We need to build a list of tags ordered by hierarchy and sorted by |
202 | // name. The heirarchy will be communicated by an accumulation of | |
203 | // separators in front of the name to give it a visual offset. | |
204 | // Instead of recursively making mysql queries, we'll make one big | |
205 | // query and build the heirarchy with the algorithm below. | |
206 | $args = array(1 => array('%' . $usedFor . '%', 'String')); | |
207 | $query = "SELECT id, name, parent_id, is_tagset | |
208 | FROM civicrm_tag | |
209 | WHERE used_for LIKE %1"; | |
210 | if ($parentId) { | |
211 | $query .= " AND parent_id = %2"; | |
212 | $args[2] = array($parentId, 'Integer'); | |
213 | } | |
214 | $query .= " ORDER BY name"; | |
215 | $dao = CRM_Core_DAO::executeQuery($query, $args, TRUE, NULL, FALSE, FALSE); | |
216 | ||
217 | // Sort the tags into the correct storage by the parent_id/is_tagset | |
218 | // filter the filter was in place previously, we're just reusing it. | |
219 | // $roots represents the current leaf nodes that need to be checked for | |
220 | // children. $rows represents the unplaced nodes, not all of much | |
221 | // are necessarily placed. | |
222 | $roots = $rows = array(); | |
223 | while ($dao->fetch()) { | |
224 | if ($dao->parent_id == $parentId && $dao->is_tagset == 0) { | |
225 | $roots[] = array('id' => $dao->id, 'prefix' => '', 'name' => $dao->name); | |
226 | } | |
227 | else { | |
228 | $rows[] = array('id' => $dao->id, 'prefix' => '', 'name' => $dao->name, 'parent_id' => $dao->parent_id); | |
229 | } | |
230 | } | |
231 | $dao->free(); | |
232 | // While we have nodes left to build, shift the first (alphabetically) | |
233 | // node of the list, place it in our tags list and loop through the | |
234 | // list of unplaced nodes to find its children. We make a copy to | |
235 | // iterate through because we must modify the unplaced nodes list | |
236 | // during the loop. | |
237 | while (count($roots)) { | |
238 | $new_roots = array(); | |
239 | $current_rows = $rows; | |
240 | $root = array_shift($roots); | |
241 | $tags[$root['id']] = array($root['prefix'], $root['name']); | |
242 | ||
243 | // As you find the children, append them to the end of the new set | |
244 | // of roots (maintain alphabetical ordering). Also remove the node | |
245 | // from the set of unplaced nodes. | |
246 | if (is_array($current_rows)) { | |
247 | foreach ($current_rows as $key => $row) { | |
248 | if ($row['parent_id'] == $root['id']) { | |
249 | $new_roots[] = array('id' => $row['id'], 'prefix' => $tags[$root['id']][0] . $separator, 'name' => $row['name']); | |
250 | unset($rows[$key]); | |
251 | } | |
252 | } | |
253 | } | |
254 | ||
255 | //As a group, insert the new roots into the beginning of the roots | |
256 | //list. This maintains the hierarchical ordering of the tags. | |
257 | $roots = array_merge($new_roots, $roots); | |
258 | } | |
259 | ||
260 | // Prefix each name with the calcuated spacing to give the visual | |
261 | // appearance of ordering when transformed into HTML in the form layer. | |
262 | foreach ($tags as & $tag) { | |
263 | $tag = $tag[0] . $tag[1]; | |
264 | } | |
265 | ||
266 | return $tags; | |
267 | } | |
268 | ||
269 | /** | |
270 | * Function to delete the tag | |
271 | * | |
272 | * @param int $id tag id | |
273 | * | |
274 | * @return boolean | |
275 | * @access public | |
276 | * @static | |
277 | * | |
278 | */ | |
279 | static function del($id) { | |
280 | // since this is a destructive operation, lets make sure | |
281 | // id is a postive number | |
282 | CRM_Utils_Type::validate($id, 'Positive'); | |
283 | ||
284 | // delete all crm_entity_tag records with the selected tag id | |
285 | $entityTag = new CRM_Core_DAO_EntityTag(); | |
286 | $entityTag->tag_id = $id; | |
287 | $entityTag->delete(); | |
288 | ||
289 | // delete from tag table | |
290 | $tag = new CRM_Core_DAO_Tag(); | |
291 | $tag->id = $id; | |
292 | ||
293 | CRM_Utils_Hook::pre('delete', 'Tag', $id, $tag); | |
294 | ||
295 | if ($tag->delete()) { | |
296 | CRM_Utils_Hook::post('delete', 'Tag', $id, $tag); | |
6a488035 TO |
297 | return TRUE; |
298 | } | |
299 | return FALSE; | |
300 | } | |
301 | ||
302 | /** | |
303 | * takes an associative array and creates a contact object | |
304 | * | |
305 | * The function extract all the params it needs to initialize the create a | |
306 | * contact object. the params array could contain additional unused name/value | |
307 | * pairs | |
308 | * | |
309 | * @param array $params (reference) an assoc array of name/value pairs | |
d989d349 | 310 | * @param array $ids (optional) the array that holds all the db ids - we are moving away from this in bao |
311 | * signatures | |
6a488035 TO |
312 | * |
313 | * @return object CRM_Core_DAO_Tag object on success, otherwise null | |
314 | * @access public | |
315 | * @static | |
316 | */ | |
d989d349 | 317 | static function add(&$params, $ids = array()) { |
9b576df4 EM |
318 | $id = CRM_Utils_Array::value('id', $params, CRM_Utils_Array::value('tag', $ids)); |
319 | if (!$id && !self::dataExists($params)) { | |
6a488035 TO |
320 | return NULL; |
321 | } | |
322 | ||
323 | $tag = new CRM_Core_DAO_Tag(); | |
324 | ||
325 | // if parent id is set then inherit used for and is hidden properties | |
a7488080 | 326 | if (!empty($params['parent_id'])) { |
6a488035 TO |
327 | // get parent details |
328 | $params['used_for'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $params['parent_id'], 'used_for'); | |
329 | } | |
330 | ||
331 | $tag->copyValues($params); | |
9b576df4 EM |
332 | $tag->id = $id; |
333 | $hook = !$id ? 'create' : 'edit'; | |
d989d349 | 334 | CRM_Utils_Hook::pre($hook, 'Tag', $tag->id, $params); |
6a488035 TO |
335 | |
336 | // save creator id and time | |
337 | if (!$tag->id) { | |
338 | $session = CRM_Core_Session::singleton(); | |
339 | $tag->created_id = $session->get('userID'); | |
340 | $tag->created_date = date('YmdHis'); | |
341 | } | |
342 | ||
343 | $tag->save(); | |
d989d349 | 344 | CRM_Utils_Hook::post($hook, 'Tag', $tag->id, $tag); |
6a488035 TO |
345 | |
346 | // if we modify parent tag, then we need to update all children | |
347 | if ($tag->parent_id === 'null') { | |
348 | CRM_Core_DAO::executeQuery("UPDATE civicrm_tag SET used_for=%1 WHERE parent_id = %2", | |
349 | array(1 => array($params['used_for'], 'String'), | |
350 | 2 => array($tag->id, 'Integer'), | |
351 | ) | |
352 | ); | |
353 | } | |
354 | ||
355 | return $tag; | |
356 | } | |
357 | ||
358 | /** | |
359 | * Check if there is data to create the object | |
360 | * | |
361 | * @param array $params (reference ) an assoc array of name/value pairs | |
362 | * | |
363 | * @return boolean | |
364 | * @access public | |
365 | * @static | |
366 | */ | |
367 | static function dataExists(&$params) { | |
5ba3bfc8 CW |
368 | // Disallow empty values except for the number zero. |
369 | // TODO: create a utility for this since it's needed in many places | |
370 | if (!empty($params['name']) || (string) $params['name'] === '0') { | |
6a488035 TO |
371 | return TRUE; |
372 | } | |
373 | ||
374 | return FALSE; | |
375 | } | |
376 | ||
377 | /** | |
378 | * Function to get the tag sets for a entity object | |
379 | * | |
380 | * @param string $entityTable entity_table | |
381 | * | |
382 | * @return array $tagSets array of tag sets | |
383 | * @access public | |
384 | * @static | |
385 | */ | |
386 | static function getTagSet($entityTable) { | |
387 | $tagSets = array(); | |
388 | $query = "SELECT name, id FROM civicrm_tag | |
389 | WHERE is_tagset=1 AND parent_id IS NULL and used_for LIKE %1"; | |
390 | $dao = CRM_Core_DAO::executeQuery($query, array(1 => array('%' . $entityTable . '%', 'String')), TRUE, NULL, FALSE, FALSE); | |
391 | while ($dao->fetch()) { | |
392 | $tagSets[$dao->id] = $dao->name; | |
393 | } | |
394 | $dao->free(); | |
395 | return $tagSets; | |
396 | } | |
397 | ||
398 | /** | |
399 | * Function to get the tags that are not children of a tagset. | |
400 | * | |
2a6da8d7 | 401 | * @return array $tags associated array of tag name and id@access public |
6a488035 TO |
402 | * @static |
403 | */ | |
404 | static function getTagsNotInTagset() { | |
405 | $tags = $tagSets = array(); | |
406 | // first get all the tag sets | |
407 | $query = "SELECT id FROM civicrm_tag WHERE is_tagset=1 AND parent_id IS NULL"; | |
408 | $dao = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray); | |
409 | while ($dao->fetch()) { | |
410 | $tagSets[] = $dao->id; | |
411 | } | |
412 | ||
413 | $parentClause = ''; | |
414 | if (!empty($tagSets)) { | |
415 | $parentClause = ' WHERE ( parent_id IS NULL ) OR ( parent_id NOT IN ( ' . implode(',', $tagSets) . ' ) )'; | |
416 | } | |
417 | ||
418 | // get that tags that don't have tagset as parent | |
419 | $query = "SELECT id, name FROM civicrm_tag {$parentClause}"; | |
420 | $dao = CRM_Core_DAO::executeQuery($query); | |
421 | while ($dao->fetch()) { | |
422 | $tags[$dao->id] = $dao->name; | |
423 | } | |
424 | ||
425 | return $tags; | |
426 | } | |
427 | } | |
428 |