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