Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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 | |
31 | * @copyright CiviCRM LLC (c) 2004-2013 | |
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 | ||
68 | function getTree($usedFor = NULL, $excludeHidden = FALSE) { | |
69 | if (!isset($this->tree)) { | |
70 | $this->buildTree($usedFor, $excludeHidden); | |
71 | } | |
72 | return $this->tree; | |
73 | } | |
74 | ||
75 | function buildTree($usedFor = NULL, $excludeHidden = FALSE) { | |
76 | $sql = "SELECT civicrm_tag.id, civicrm_tag.parent_id,civicrm_tag.name FROM civicrm_tag "; | |
77 | ||
78 | $whereClause = array(); | |
79 | if ($usedFor) { | |
80 | $whereClause[] = "used_for like '%{$usedFor}%'"; | |
81 | } | |
82 | if ($excludeHidden) { | |
83 | $whereClause[] = "is_tagset = 0"; | |
84 | } | |
85 | ||
86 | if (!empty($whereClause)) { | |
87 | $sql .= " WHERE " . implode(' AND ', $whereClause); | |
88 | } | |
89 | ||
90 | $sql .= " ORDER BY parent_id,name"; | |
91 | ||
92 | $dao = CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray, TRUE, NULL, FALSE, FALSE); | |
93 | ||
94 | $orphan = array(); | |
95 | while ($dao->fetch()) { | |
96 | if (!$dao->parent_id) { | |
97 | $this->tree[$dao->id]['name'] = $dao->name; | |
98 | } | |
99 | else { | |
100 | if (array_key_exists($dao->parent_id, $this->tree)) { | |
101 | $parent = &$this->tree[$dao->parent_id]; | |
102 | if (!isset($this->tree[$dao->parent_id]['children'])) { | |
103 | $this->tree[$dao->parent_id]['children'] = array(); | |
104 | } | |
105 | } | |
106 | else { | |
107 | //3rd level tag | |
108 | if (!array_key_exists($dao->parent_id, $orphan)) { | |
109 | $orphan[$dao->parent_id] = array('children' => array()); | |
110 | } | |
111 | $parent = &$orphan[$dao->parent_id]; | |
112 | } | |
113 | $parent['children'][$dao->id] = array('name' => $dao->name); | |
114 | } | |
115 | } | |
116 | if (sizeof($orphan)) { | |
117 | //hang the 3rd level lists at the right place | |
118 | foreach ($this->tree as & $level1) { | |
119 | if (!isset($level1['children'])) { | |
120 | continue; | |
121 | } | |
122 | ||
123 | foreach ($level1['children'] as $key => & $level2) { | |
124 | if (array_key_exists($key, $orphan)) { | |
125 | $level2['children'] = $orphan[$key]['children']; | |
126 | } | |
127 | } | |
128 | } | |
129 | } | |
130 | } | |
131 | ||
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 | ||
185 | static function getTags($usedFor = 'civicrm_contact', | |
186 | &$tags = array(), | |
187 | $parentId = NULL, | |
188 | $separator = ' ' | |
189 | ) { | |
dee5ccbb CW |
190 | if (!is_array($tags)) { |
191 | $tags = array(); | |
192 | } | |
6a488035 TO |
193 | // We need to build a list of tags ordered by hierarchy and sorted by |
194 | // name. The heirarchy will be communicated by an accumulation of | |
195 | // separators in front of the name to give it a visual offset. | |
196 | // Instead of recursively making mysql queries, we'll make one big | |
197 | // query and build the heirarchy with the algorithm below. | |
198 | $args = array(1 => array('%' . $usedFor . '%', 'String')); | |
199 | $query = "SELECT id, name, parent_id, is_tagset | |
200 | FROM civicrm_tag | |
201 | WHERE used_for LIKE %1"; | |
202 | if ($parentId) { | |
203 | $query .= " AND parent_id = %2"; | |
204 | $args[2] = array($parentId, 'Integer'); | |
205 | } | |
206 | $query .= " ORDER BY name"; | |
207 | $dao = CRM_Core_DAO::executeQuery($query, $args, TRUE, NULL, FALSE, FALSE); | |
208 | ||
209 | // Sort the tags into the correct storage by the parent_id/is_tagset | |
210 | // filter the filter was in place previously, we're just reusing it. | |
211 | // $roots represents the current leaf nodes that need to be checked for | |
212 | // children. $rows represents the unplaced nodes, not all of much | |
213 | // are necessarily placed. | |
214 | $roots = $rows = array(); | |
215 | while ($dao->fetch()) { | |
216 | if ($dao->parent_id == $parentId && $dao->is_tagset == 0) { | |
217 | $roots[] = array('id' => $dao->id, 'prefix' => '', 'name' => $dao->name); | |
218 | } | |
219 | else { | |
220 | $rows[] = array('id' => $dao->id, 'prefix' => '', 'name' => $dao->name, 'parent_id' => $dao->parent_id); | |
221 | } | |
222 | } | |
223 | $dao->free(); | |
224 | // While we have nodes left to build, shift the first (alphabetically) | |
225 | // node of the list, place it in our tags list and loop through the | |
226 | // list of unplaced nodes to find its children. We make a copy to | |
227 | // iterate through because we must modify the unplaced nodes list | |
228 | // during the loop. | |
229 | while (count($roots)) { | |
230 | $new_roots = array(); | |
231 | $current_rows = $rows; | |
232 | $root = array_shift($roots); | |
233 | $tags[$root['id']] = array($root['prefix'], $root['name']); | |
234 | ||
235 | // As you find the children, append them to the end of the new set | |
236 | // of roots (maintain alphabetical ordering). Also remove the node | |
237 | // from the set of unplaced nodes. | |
238 | if (is_array($current_rows)) { | |
239 | foreach ($current_rows as $key => $row) { | |
240 | if ($row['parent_id'] == $root['id']) { | |
241 | $new_roots[] = array('id' => $row['id'], 'prefix' => $tags[$root['id']][0] . $separator, 'name' => $row['name']); | |
242 | unset($rows[$key]); | |
243 | } | |
244 | } | |
245 | } | |
246 | ||
247 | //As a group, insert the new roots into the beginning of the roots | |
248 | //list. This maintains the hierarchical ordering of the tags. | |
249 | $roots = array_merge($new_roots, $roots); | |
250 | } | |
251 | ||
252 | // Prefix each name with the calcuated spacing to give the visual | |
253 | // appearance of ordering when transformed into HTML in the form layer. | |
254 | foreach ($tags as & $tag) { | |
255 | $tag = $tag[0] . $tag[1]; | |
256 | } | |
257 | ||
258 | return $tags; | |
259 | } | |
260 | ||
261 | /** | |
262 | * Function to delete the tag | |
263 | * | |
264 | * @param int $id tag id | |
265 | * | |
266 | * @return boolean | |
267 | * @access public | |
268 | * @static | |
269 | * | |
270 | */ | |
271 | static function del($id) { | |
272 | // since this is a destructive operation, lets make sure | |
273 | // id is a postive number | |
274 | CRM_Utils_Type::validate($id, 'Positive'); | |
275 | ||
276 | // delete all crm_entity_tag records with the selected tag id | |
277 | $entityTag = new CRM_Core_DAO_EntityTag(); | |
278 | $entityTag->tag_id = $id; | |
279 | $entityTag->delete(); | |
280 | ||
281 | // delete from tag table | |
282 | $tag = new CRM_Core_DAO_Tag(); | |
283 | $tag->id = $id; | |
284 | ||
285 | CRM_Utils_Hook::pre('delete', 'Tag', $id, $tag); | |
286 | ||
287 | if ($tag->delete()) { | |
288 | CRM_Utils_Hook::post('delete', 'Tag', $id, $tag); | |
6a488035 TO |
289 | return TRUE; |
290 | } | |
291 | return FALSE; | |
292 | } | |
293 | ||
294 | /** | |
295 | * takes an associative array and creates a contact object | |
296 | * | |
297 | * The function extract all the params it needs to initialize the create a | |
298 | * contact object. the params array could contain additional unused name/value | |
299 | * pairs | |
300 | * | |
301 | * @param array $params (reference) an assoc array of name/value pairs | |
d989d349 | 302 | * @param array $ids (optional) the array that holds all the db ids - we are moving away from this in bao |
303 | * signatures | |
6a488035 TO |
304 | * |
305 | * @return object CRM_Core_DAO_Tag object on success, otherwise null | |
306 | * @access public | |
307 | * @static | |
308 | */ | |
d989d349 | 309 | static function add(&$params, $ids = array()) { |
6a488035 TO |
310 | if (!self::dataExists($params)) { |
311 | return NULL; | |
312 | } | |
313 | ||
314 | $tag = new CRM_Core_DAO_Tag(); | |
315 | ||
316 | // if parent id is set then inherit used for and is hidden properties | |
317 | if (CRM_Utils_Array::value('parent_id', $params)) { | |
318 | // get parent details | |
319 | $params['used_for'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $params['parent_id'], 'used_for'); | |
320 | } | |
321 | ||
322 | $tag->copyValues($params); | |
d989d349 | 323 | $tag->id = CRM_Utils_Array::value('id', $params, CRM_Utils_Array::value('tag', $ids)); |
324 | $hook = empty($params['id']) ? 'create' : 'edit'; | |
325 | CRM_Utils_Hook::pre($hook, 'Tag', $tag->id, $params); | |
6a488035 TO |
326 | |
327 | // save creator id and time | |
328 | if (!$tag->id) { | |
329 | $session = CRM_Core_Session::singleton(); | |
330 | $tag->created_id = $session->get('userID'); | |
331 | $tag->created_date = date('YmdHis'); | |
332 | } | |
333 | ||
334 | $tag->save(); | |
d989d349 | 335 | CRM_Utils_Hook::post($hook, 'Tag', $tag->id, $tag); |
6a488035 TO |
336 | |
337 | // if we modify parent tag, then we need to update all children | |
338 | if ($tag->parent_id === 'null') { | |
339 | CRM_Core_DAO::executeQuery("UPDATE civicrm_tag SET used_for=%1 WHERE parent_id = %2", | |
340 | array(1 => array($params['used_for'], 'String'), | |
341 | 2 => array($tag->id, 'Integer'), | |
342 | ) | |
343 | ); | |
344 | } | |
345 | ||
346 | return $tag; | |
347 | } | |
348 | ||
349 | /** | |
350 | * Check if there is data to create the object | |
351 | * | |
352 | * @param array $params (reference ) an assoc array of name/value pairs | |
353 | * | |
354 | * @return boolean | |
355 | * @access public | |
356 | * @static | |
357 | */ | |
358 | static function dataExists(&$params) { | |
5ba3bfc8 CW |
359 | // Disallow empty values except for the number zero. |
360 | // TODO: create a utility for this since it's needed in many places | |
361 | if (!empty($params['name']) || (string) $params['name'] === '0') { | |
6a488035 TO |
362 | return TRUE; |
363 | } | |
364 | ||
365 | return FALSE; | |
366 | } | |
367 | ||
368 | /** | |
369 | * Function to get the tag sets for a entity object | |
370 | * | |
371 | * @param string $entityTable entity_table | |
372 | * | |
373 | * @return array $tagSets array of tag sets | |
374 | * @access public | |
375 | * @static | |
376 | */ | |
377 | static function getTagSet($entityTable) { | |
378 | $tagSets = array(); | |
379 | $query = "SELECT name, id FROM civicrm_tag | |
380 | WHERE is_tagset=1 AND parent_id IS NULL and used_for LIKE %1"; | |
381 | $dao = CRM_Core_DAO::executeQuery($query, array(1 => array('%' . $entityTable . '%', 'String')), TRUE, NULL, FALSE, FALSE); | |
382 | while ($dao->fetch()) { | |
383 | $tagSets[$dao->id] = $dao->name; | |
384 | } | |
385 | $dao->free(); | |
386 | return $tagSets; | |
387 | } | |
388 | ||
389 | /** | |
390 | * Function to get the tags that are not children of a tagset. | |
391 | * | |
392 | * @return $tags associated array of tag name and id | |
393 | * @access public | |
394 | * @static | |
395 | */ | |
396 | static function getTagsNotInTagset() { | |
397 | $tags = $tagSets = array(); | |
398 | // first get all the tag sets | |
399 | $query = "SELECT id FROM civicrm_tag WHERE is_tagset=1 AND parent_id IS NULL"; | |
400 | $dao = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray); | |
401 | while ($dao->fetch()) { | |
402 | $tagSets[] = $dao->id; | |
403 | } | |
404 | ||
405 | $parentClause = ''; | |
406 | if (!empty($tagSets)) { | |
407 | $parentClause = ' WHERE ( parent_id IS NULL ) OR ( parent_id NOT IN ( ' . implode(',', $tagSets) . ' ) )'; | |
408 | } | |
409 | ||
410 | // get that tags that don't have tagset as parent | |
411 | $query = "SELECT id, name FROM civicrm_tag {$parentClause}"; | |
412 | $dao = CRM_Core_DAO::executeQuery($query); | |
413 | while ($dao->fetch()) { | |
414 | $tags[$dao->id] = $dao->name; | |
415 | } | |
416 | ||
417 | return $tags; | |
418 | } | |
419 | } | |
420 |