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