3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
35 class CRM_Core_BAO_Tag
extends CRM_Core_DAO_Tag
{
40 function __construct() {
41 parent
::__construct();
45 * Fetch object based on array of properties
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
50 * @return object CRM_Core_DAO_Tag object on success, otherwise null
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);
65 * @param null $usedFor
66 * @param bool $excludeHidden
70 function getTree($usedFor = NULL, $excludeHidden = FALSE) {
71 if (!isset($this->tree
)) {
72 $this->buildTree($usedFor, $excludeHidden);
78 * Build a nested array from hierarchical tags. Supports infinite levels of nesting.
79 * @param null $usedFor
80 * @param bool $excludeHidden
82 function buildTree($usedFor = NULL, $excludeHidden = FALSE) {
83 $sql = "SELECT id, parent_id, name, description, is_selectable FROM civicrm_tag";
85 $whereClause = array();
87 $whereClause[] = "used_for like '%{$usedFor}%'";
90 $whereClause[] = "is_tagset = 0";
93 if (!empty($whereClause)) {
94 $sql .= " WHERE " . implode(' AND ', $whereClause);
97 $sql .= " ORDER BY parent_id,name";
99 $dao = CRM_Core_DAO
::executeQuery($sql, CRM_Core_DAO
::$_nullArray, TRUE, NULL, FALSE, FALSE);
102 while ($dao->fetch()) {
103 $thisref = &$refs[$dao->id
];
105 $thisref['parent_id'] = $dao->parent_id
;
106 $thisref['name'] = $dao->name
;
107 $thisref['description'] = $dao->description
;
108 $thisref['is_selectable'] = $dao->is_selectable
;
110 if (!$dao->parent_id
) {
111 $this->tree
[$dao->id
] = &$thisref;
114 $refs[$dao->parent_id
]['children'][$dao->id
] = &$thisref;
121 * @param array $usedFor
122 * @param bool $buildSelect
124 * @param int $parentId
128 static function getTagsUsedFor($usedFor = array('civicrm_contact'),
135 if (empty($usedFor)) {
138 if (!is_array($usedFor)) {
139 $usedFor = array($usedFor);
142 if ($parentId === NULL) {
143 $parentClause = " parent_id IS NULL AND ";
146 $parentClause = " parent_id = {$parentId} AND ";
149 foreach ($usedFor as $entityTable) {
150 $tag = new CRM_Core_DAO_Tag();
152 $tag->orderBy('parent_id');
154 $tag->whereAdd("is_tagset = 0 AND {$parentClause} used_for LIKE '%{$entityTable}%'");
157 $tag->whereAdd("used_for LIKE '%{$entityTable}%'");
164 while ($tag->fetch()) {
166 $tags[$tag->id
] = $tag->name
;
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
;
182 * @param string $usedFor
184 * @param int $parentId
185 * @param string $separator
189 static function getTags($usedFor = 'civicrm_contact',
192 $separator = ' '
194 if (!is_array($tags)) {
197 // We need to build a list of tags ordered by hierarchy and sorted by
198 // name. The heirarchy will be communicated by an accumulation of
199 // separators in front of the name to give it a visual offset.
200 // Instead of recursively making mysql queries, we'll make one big
201 // query and build the heirarchy with the algorithm below.
202 $args = array(1 => array('%' . $usedFor . '%', 'String'));
203 $query = "SELECT id, name, parent_id, is_tagset
205 WHERE used_for LIKE %1";
207 $query .= " AND parent_id = %2";
208 $args[2] = array($parentId, 'Integer');
210 $query .= " ORDER BY name";
211 $dao = CRM_Core_DAO
::executeQuery($query, $args, TRUE, NULL, FALSE, FALSE);
213 // Sort the tags into the correct storage by the parent_id/is_tagset
214 // filter the filter was in place previously, we're just reusing it.
215 // $roots represents the current leaf nodes that need to be checked for
216 // children. $rows represents the unplaced nodes, not all of much
217 // are necessarily placed.
218 $roots = $rows = array();
219 while ($dao->fetch()) {
220 if ($dao->parent_id
== $parentId && $dao->is_tagset
== 0) {
221 $roots[] = array('id' => $dao->id
, 'prefix' => '', 'name' => $dao->name
);
224 $rows[] = array('id' => $dao->id
, 'prefix' => '', 'name' => $dao->name
, 'parent_id' => $dao->parent_id
);
228 // While we have nodes left to build, shift the first (alphabetically)
229 // node of the list, place it in our tags list and loop through the
230 // list of unplaced nodes to find its children. We make a copy to
231 // iterate through because we must modify the unplaced nodes list
233 while (count($roots)) {
234 $new_roots = array();
235 $current_rows = $rows;
236 $root = array_shift($roots);
237 $tags[$root['id']] = array($root['prefix'], $root['name']);
239 // As you find the children, append them to the end of the new set
240 // of roots (maintain alphabetical ordering). Also remove the node
241 // from the set of unplaced nodes.
242 if (is_array($current_rows)) {
243 foreach ($current_rows as $key => $row) {
244 if ($row['parent_id'] == $root['id']) {
245 $new_roots[] = array('id' => $row['id'], 'prefix' => $tags[$root['id']][0] . $separator, 'name' => $row['name']);
251 //As a group, insert the new roots into the beginning of the roots
252 //list. This maintains the hierarchical ordering of the tags.
253 $roots = array_merge($new_roots, $roots);
256 // Prefix each name with the calcuated spacing to give the visual
257 // appearance of ordering when transformed into HTML in the form layer.
258 foreach ($tags as & $tag) {
259 $tag = $tag[0] . $tag[1];
268 * @param int $id tag id
275 static function del($id) {
276 // since this is a destructive operation, lets make sure
277 // id is a postive number
278 CRM_Utils_Type
::validate($id, 'Positive');
280 // delete all crm_entity_tag records with the selected tag id
281 $entityTag = new CRM_Core_DAO_EntityTag();
282 $entityTag->tag_id
= $id;
283 $entityTag->delete();
285 // delete from tag table
286 $tag = new CRM_Core_DAO_Tag();
289 CRM_Utils_Hook
::pre('delete', 'Tag', $id, $tag);
291 if ($tag->delete()) {
292 CRM_Utils_Hook
::post('delete', 'Tag', $id, $tag);
299 * Takes an associative array and creates a contact object
301 * The function extract all the params it needs to initialize the create a
302 * contact object. the params array could contain additional unused name/value
305 * @param array $params (reference) an assoc array of name/value pairs
306 * @param array $ids (optional) the array that holds all the db ids - we are moving away from this in bao
309 * @return object CRM_Core_DAO_Tag object on success, otherwise null
313 static function add(&$params, $ids = array()) {
314 $id = CRM_Utils_Array
::value('id', $params, CRM_Utils_Array
::value('tag', $ids));
315 if (!$id && !self
::dataExists($params)) {
319 $tag = new CRM_Core_DAO_Tag();
321 // if parent id is set then inherit used for and is hidden properties
322 if (!empty($params['parent_id'])) {
323 // get parent details
324 $params['used_for'] = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Tag', $params['parent_id'], 'used_for');
327 $tag->copyValues($params);
329 $hook = !$id ?
'create' : 'edit';
330 CRM_Utils_Hook
::pre($hook, 'Tag', $tag->id
, $params);
332 // save creator id and time
334 $session = CRM_Core_Session
::singleton();
335 $tag->created_id
= $session->get('userID');
336 $tag->created_date
= date('YmdHis');
340 CRM_Utils_Hook
::post($hook, 'Tag', $tag->id
, $tag);
342 // if we modify parent tag, then we need to update all children
343 if ($tag->parent_id
=== 'null') {
344 CRM_Core_DAO
::executeQuery("UPDATE civicrm_tag SET used_for=%1 WHERE parent_id = %2",
345 array(1 => array($params['used_for'], 'String'),
346 2 => array($tag->id
, 'Integer'),
355 * Check if there is data to create the object
357 * @param array $params (reference ) an assoc array of name/value pairs
363 static function dataExists(&$params) {
364 // Disallow empty values except for the number zero.
365 // TODO: create a utility for this since it's needed in many places
366 if (!empty($params['name']) ||
(string) $params['name'] === '0') {
374 * Get the tag sets for a entity object
376 * @param string $entityTable entity_table
378 * @return array $tagSets array of tag sets
382 static function getTagSet($entityTable) {
384 $query = "SELECT name, id FROM civicrm_tag
385 WHERE is_tagset=1 AND parent_id IS NULL and used_for LIKE %1";
386 $dao = CRM_Core_DAO
::executeQuery($query, array(1 => array('%' . $entityTable . '%', 'String')), TRUE, NULL, FALSE, FALSE);
387 while ($dao->fetch()) {
388 $tagSets[$dao->id
] = $dao->name
;
395 * Get the tags that are not children of a tagset.
397 * @return array $tags associated array of tag name and id@access public
400 static function getTagsNotInTagset() {
401 $tags = $tagSets = array();
402 // first get all the tag sets
403 $query = "SELECT id FROM civicrm_tag WHERE is_tagset=1 AND parent_id IS NULL";
404 $dao = CRM_Core_DAO
::executeQuery($query, CRM_Core_DAO
::$_nullArray);
405 while ($dao->fetch()) {
406 $tagSets[] = $dao->id
;
410 if (!empty($tagSets)) {
411 $parentClause = ' WHERE ( parent_id IS NULL ) OR ( parent_id NOT IN ( ' . implode(',', $tagSets) . ' ) )';
414 // get that tags that don't have tagset as parent
415 $query = "SELECT id, name FROM civicrm_tag {$parentClause}";
416 $dao = CRM_Core_DAO
::executeQuery($query);
417 while ($dao->fetch()) {
418 $tags[$dao->id
] = $dao->name
;