PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/drupal/sites/all/modules/civicrm/CRM/Core/BAO/Tag.php

https://github.com/michaelmcandrew/citycomm
PHP | 359 lines | 206 code | 46 blank | 107 comment | 36 complexity | 2c189b5dfc2f9ced1bd14e85e530d36e MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 3.2 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2010 |
  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. * @package CRM
  30. * @copyright CiviCRM LLC (c) 2004-2010
  31. * $Id$
  32. *
  33. */
  34. require_once 'CRM/Core/DAO/Tag.php';
  35. class CRM_Core_BAO_Tag extends CRM_Core_DAO_Tag {
  36. /**
  37. * class constructor
  38. */
  39. function __construct( ) {
  40. parent::__construct( );
  41. }
  42. /**
  43. * Takes a bunch of params that are needed to match certain criteria and
  44. * retrieves the relevant objects. Typically the valid params are only
  45. * contact_id. We'll tweak this function to be more full featured over a period
  46. * of time. This is the inverse function of create. It also stores all the retrieved
  47. * values in the default array
  48. *
  49. * @param array $params (reference ) an assoc array of name/value pairs
  50. * @param array $defaults (reference ) an assoc array to hold the flattened values
  51. *
  52. * @return object CRM_Core_DAO_Tag object on success, otherwise null
  53. * @access public
  54. * @static
  55. */
  56. static function retrieve( &$params, &$defaults ) {
  57. $tag = new CRM_Core_DAO_Tag( );
  58. $tag->copyValues( $params );
  59. if ( $tag->find( true ) ) {
  60. CRM_Core_DAO::storeValues( $tag, $defaults );
  61. return $tag;
  62. }
  63. return null;
  64. }
  65. function getTree ( $usedFor = null, $excludeHidden = false ) {
  66. if (!isset ($this->tree)) {
  67. $this->buildTree($usedFor, $excludeHidden);
  68. }
  69. return $this->tree;
  70. }
  71. function buildTree( $usedFor = null, $excludeHidden = false ) {
  72. $sql = "SELECT civicrm_tag.id, civicrm_tag.parent_id,civicrm_tag.name FROM civicrm_tag ";
  73. $whereClause = array( );
  74. if ( $usedFor ) {
  75. $whereClause[] = "used_for like '%{$usedFor}%'";
  76. }
  77. if ( $excludeHidden ) {
  78. $whereClause[] = "is_tagset = 0";
  79. }
  80. if ( !empty( $whereClause ) ) {
  81. $sql .= " WHERE ". implode( ' AND ', $whereClause );
  82. }
  83. $sql .= " ORDER BY parent_id,name";
  84. $dao =& CRM_Core_DAO::executeQuery( $sql, CRM_Core_DAO::$_nullArray, true, null, false, false );
  85. $orphan = array();
  86. while ( $dao->fetch( ) ) {
  87. if (!$dao->parent_id) {
  88. $this->tree[$dao->id]['name'] = $dao->name;
  89. } else {
  90. if (array_key_exists($dao->parent_id,$this->tree)) {
  91. $parent =& $this->tree[$dao->parent_id];
  92. if (!isset ($this->tree[$dao->parent_id]['children']) ) {
  93. $this->tree[$dao->parent_id]['children'] = array();
  94. }
  95. }
  96. else {
  97. //3rd level tag
  98. if (!array_key_exists($dao->parent_id,$orphan)) {
  99. $orphan[$dao->parent_id]=array('children'=> array());
  100. }
  101. $parent=& $orphan[$dao->parent_id];
  102. }
  103. $parent['children'][$dao->id] = array ('name'=>$dao->name);
  104. }
  105. }
  106. if (sizeof($orphan)) {
  107. //hang the 3rd level lists at the right place
  108. foreach ($this->tree as &$level1) {
  109. if ( ! isset ( $level1['children'] ) ) {
  110. continue;
  111. }
  112. foreach ( $level1['children'] as $key => &$level2 ) {
  113. if ( array_key_exists( $key,$orphan ) ) {
  114. $level2['children']= $orphan[$key]['children'];
  115. }
  116. }
  117. }
  118. }
  119. }
  120. function getTagsUsedFor( $usedFor = array( 'civicrm_contact' ), $buildSelect = true, $all = false ) {
  121. $tags = array( );
  122. if ( empty($usedFor) ) {
  123. return $tags;
  124. }
  125. if ( !is_array($usedFor) ) {
  126. $usedFor = array( $usedFor );
  127. }
  128. foreach( $usedFor as $entityTable ) {
  129. $tag = new CRM_Core_DAO_Tag( );
  130. $tag->fields( );
  131. $tag->orderBy( 'parent_id' );
  132. if ( $buildSelect ) {
  133. $tag->whereAdd( "is_tagset = 0 AND parent_id IS NULL AND used_for LIKE '%{$entityTable}%'");
  134. } else {
  135. $tag->whereAdd( "used_for LIKE '%{$entityTable}%'");
  136. }
  137. if ( !$all ) {
  138. $tag->is_tagset = 0;
  139. }
  140. $tag->find( );
  141. while( $tag->fetch( ) ) {
  142. if ( $buildSelect ) {
  143. $tags[$tag->id] = $tag->name;
  144. } else {
  145. $tags[$tag->id]['name'] = $tag->name;
  146. $tags[$tag->id]['parent_id'] = $tag->parent_id;
  147. $tags[$tag->id]['is_tagset'] = $tag->is_tagset;
  148. $tags[$tag->id]['used_for'] = $tag->used_for;
  149. }
  150. }
  151. $tag->free( );
  152. }
  153. return $tags;
  154. }
  155. static function getTags( $usedFor = 'civicrm_contact', &$tags = array( ), $parentId = null, $separator = '&nbsp;&nbsp;', $flatlist = true ) {
  156. $parentClause = '';
  157. if ( $parentId ) {
  158. $separator .= '&nbsp;&nbsp;';
  159. $parentClause = " parent_id = {$parentId}";
  160. } else {
  161. $separator = '';
  162. $parentClause = ' is_tagset = 0 AND parent_id IS NULL';
  163. }
  164. $query = "SELECT id, name, parent_id
  165. FROM civicrm_tag
  166. WHERE {$parentClause} AND used_for LIKE '%{$usedFor}%' ORDER BY name";
  167. $dao = CRM_Core_DAO::executeQuery( $query, CRM_Core_DAO::$_nullArray, true, null, false, false );
  168. while( $dao->fetch( ) ) {
  169. $tags[$dao->id] = $separator . $dao->name;
  170. self::getTags( $usedFor, $tags, $dao->id, $separator );
  171. }
  172. return $tags;
  173. }
  174. /**
  175. * Function to delete the tag
  176. *
  177. * @param int $id tag id
  178. *
  179. * @return boolean
  180. * @access public
  181. * @static
  182. *
  183. */
  184. static function del ( $id ) {
  185. // delete all crm_entity_tag records with the selected tag id
  186. require_once 'CRM/Core/DAO/EntityTag.php';
  187. $entityTag = new CRM_Core_DAO_EntityTag( );
  188. $entityTag->tag_id = $id;
  189. if ( $entityTag->find( ) ) {
  190. while ( $entityTag->fetch() ) {
  191. $entityTag->delete();
  192. }
  193. }
  194. // delete from tag table
  195. $tag = new CRM_Core_DAO_Tag( );
  196. $tag->id = $id;
  197. require_once 'CRM/Utils/Hook.php';
  198. CRM_Utils_Hook::pre( 'delete', 'Tag', $id, $tag);
  199. if ( $tag->delete( ) ) {
  200. CRM_Utils_Hook::post( 'delete', 'Tag', $id, $tag);
  201. CRM_Core_Session::setStatus( ts('Selected Tag has been Deleted Successfuly.') );
  202. return true;
  203. }
  204. return false;
  205. }
  206. /**
  207. * takes an associative array and creates a contact object
  208. *
  209. * The function extract all the params it needs to initialize the create a
  210. * contact object. the params array could contain additional unused name/value
  211. * pairs
  212. *
  213. * @param array $params (reference) an assoc array of name/value pairs
  214. * @param array $ids (reference) the array that holds all the db ids
  215. *
  216. * @return object CRM_Core_DAO_Tag object on success, otherwise null
  217. * @access public
  218. * @static
  219. */
  220. static function add( &$params, &$ids ) {
  221. if ( ! self::dataExists( $params ) ) {
  222. return null;
  223. }
  224. $tag = new CRM_Core_DAO_Tag( );
  225. // if parent id is set then inherit used for and is hidden properties
  226. if ( CRM_Utils_Array::value( 'parent_id', $params ) ) {
  227. // get parent details
  228. $params['used_for' ] = CRM_Core_DAO::getFieldValue( 'CRM_Core_DAO_Tag', $params['parent_id'] , 'used_for' );
  229. }
  230. $tag->copyValues( $params );
  231. $tag->id = CRM_Utils_Array::value( 'tag', $ids );
  232. require_once 'CRM/Utils/Hook.php';
  233. $edit = ($tag->id) ? true : false;
  234. if ($edit) {
  235. CRM_Utils_Hook::pre( 'edit', 'Tag', $tag->id, $tag );
  236. } else {
  237. CRM_Utils_Hook::pre( 'create', 'Tag', null, $tag );
  238. }
  239. $tag->save( );
  240. if ($edit) {
  241. CRM_Utils_Hook::post( 'edit', 'Tag', $tag->id, $tag );
  242. } else {
  243. CRM_Utils_Hook::post( 'create', 'Tag', null, $tag );
  244. }
  245. // if we modify parent tag, then we need to update all children
  246. if ( $tag->parent_id === 'null' ) {
  247. CRM_Core_DAO::executeQuery( "UPDATE civicrm_tag SET used_for=%1 WHERE parent_id = %2",
  248. array( 1 => array( $params['used_for'], 'String' ),
  249. 2 => array( $tag->id , 'Integer' ) ) );
  250. }
  251. return $tag;
  252. }
  253. /**
  254. * Check if there is data to create the object
  255. *
  256. * @param array $params (reference ) an assoc array of name/value pairs
  257. *
  258. * @return boolean
  259. * @access public
  260. * @static
  261. */
  262. static function dataExists( &$params ) {
  263. if ( !empty( $params['name'] ) ) {
  264. return true;
  265. }
  266. return false;
  267. }
  268. /**
  269. * Function to get the tag sets for a entity object
  270. *
  271. * @param string $entityTable entity_table
  272. *
  273. * @return array $tagSets array of tag sets
  274. * @access public
  275. * @static
  276. */
  277. static function getTagSet( $entityTable ) {
  278. $tagSets = array( );
  279. $query = "SELECT name FROM civicrm_tag WHERE is_tagset=1 AND parent_id IS NULL and used_for LIKE '%{$entityTable}%'";
  280. $dao = CRM_Core_DAO::executeQuery( $query, CRM_Core_DAO::$_nullArray, true, null, false, false );
  281. while( $dao->fetch( ) ) {
  282. $tagSets[] = $dao->name;
  283. }
  284. return $tagSets;
  285. }
  286. /**
  287. * Function to get the tags that are not children of a tagset.
  288. *
  289. * @return $tags associated array of tag name and id
  290. * @access public
  291. * @static
  292. */
  293. static function getTagsNotInTagset( ) {
  294. $tags = $tagSets = array( );
  295. // first get all the tag sets
  296. $query = "SELECT id FROM civicrm_tag WHERE is_tagset=1 AND parent_id IS NULL";
  297. $dao = CRM_Core_DAO::executeQuery( $query, CRM_Core_DAO::$_nullArray );
  298. while( $dao->fetch( ) ) {
  299. $tagSets[] = $dao->id;
  300. }
  301. $parentClause = '';
  302. if ( !empty( $tagSets ) ) {
  303. $parentClause = ' WHERE ( parent_id IS NULL ) OR ( parent_id NOT IN ( ' .implode( ',', $tagSets ) .' ) )';
  304. }
  305. // get that tags that don't have tagset as parent
  306. $query = "SELECT id, name FROM civicrm_tag {$parentClause}";
  307. $dao = CRM_Core_DAO::executeQuery( $query );
  308. while( $dao->fetch( ) ) {
  309. $tags[$dao->id] = $dao->name;
  310. }
  311. return $tags;
  312. }
  313. }