comment fixes
[civicrm-core.git] / CRM / Core / BAO / EntityTag.php
index a43d0978d7bd75984eb43634a15e7962ab3f9ebe..9745cb3778179578f53a1f1d190c0b4a99f3277d 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.6                                                |
+ | CiviCRM version 4.7                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2014                                |
+ | Copyright CiviCRM LLC (c) 2004-2015                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
  | GNU Affero General Public License or the licensing of CiviCRM,     |
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
-*/
+ */
 
 /**
  * This class contains functions for managing Tag(tag) for a contact
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2014
- * $Id$
- *
+ * @copyright CiviCRM LLC (c) 2004-2015
  */
 class CRM_Core_BAO_EntityTag extends CRM_Core_DAO_EntityTag {
 
   /**
-   *
-   * Given a contact id, it returns an array of tag id's the
-   * contact belongs to.
+   * Given a contact id, it returns an array of tag id's the contact belongs to.
    *
    * @param int $entityID
    *   Id of the entity usually the contactID.
    * @param string $entityTable
    *   Name of the entity table usually 'civicrm_contact'.
    *
-   * @return array(
-   * ) reference $tag array of category id's the contact belongs to.
-   *
-   * @static
+   * @return array
+   *   reference $tag array of category id's the contact belongs to.
    */
   public static function &getTag($entityID, $entityTable = 'civicrm_contact') {
     $tags = array();
@@ -65,7 +59,7 @@ class CRM_Core_BAO_EntityTag extends CRM_Core_DAO_EntityTag {
   }
 
   /**
-   * Takes an associative array and creates a entityTag object
+   * Takes an associative array and creates a entityTag object.
    *
    * the function extract all the params it needs to initialize the create a
    * group object. the params array could contain additional unused name/value
@@ -74,8 +68,7 @@ class CRM_Core_BAO_EntityTag extends CRM_Core_DAO_EntityTag {
    * @param array $params
    *   (reference ) an assoc array of name/value pairs.
    *
-   * @return CRM_Core_BAO_EntityTag object
-   * @static
+   * @return CRM_Core_BAO_EntityTag
    */
   public static function add(&$params) {
     $dataExists = self::dataExists($params);
@@ -100,26 +93,22 @@ class CRM_Core_BAO_EntityTag extends CRM_Core_DAO_EntityTag {
   }
 
   /**
-   * Check if there is data to create the object
+   * Check if there is data to create the object.
    *
    * @param array $params
    *   An assoc array of name/value pairs.
    *
-   * @return boolean
-   * @static
+   * @return bool
    */
   public static function dataExists($params) {
     return !($params['tag_id'] == 0);
   }
 
   /**
-   * Delete the tag for a contact
+   * Delete the tag for a contact.
    *
    * @param array $params
    *   (reference ) an assoc array of name/value pairs.
-   *
-   * @return CRM_Core_BAO_EntityTag object
-   * @static
    */
   public static function del(&$params) {
     $entityTag = new CRM_Core_BAO_EntityTag();
@@ -143,7 +132,6 @@ class CRM_Core_BAO_EntityTag extends CRM_Core_DAO_EntityTag {
    *
    * @return array
    *   (total, added, notAdded) count of enities added to tag
-   * @static
    */
   public static function addEntitiesToTag(&$entityIds, $tagId, $entityTable = 'civicrm_contact') {
     $numEntitiesAdded = 0;
@@ -189,7 +177,6 @@ class CRM_Core_BAO_EntityTag extends CRM_Core_DAO_EntityTag {
    *
    * @return array
    *   (total, removed, notRemoved) count of entities removed from tags
-   * @static
    */
   public static function removeEntitiesFromTag(&$entityIds, $tagId, $entityTable = 'civicrm_contact') {
     $numEntitiesRemoved = 0;
@@ -224,15 +211,12 @@ class CRM_Core_BAO_EntityTag extends CRM_Core_DAO_EntityTag {
   }
 
   /**
-   * Takes an associative array and creates tag entity record for all tag entities
+   * Takes an associative array and creates tag entity record for all tag entities.
    *
    * @param array $params
    *   (reference) an assoc array of name/value pairs.
-   * @param $entityTable
+   * @param string $entityTable
    * @param int $entityID
-   *
-   * @return void
-   * @static
    */
   public static function create(&$params, $entityTable, $entityID) {
     // get categories for the entity id
@@ -269,7 +253,7 @@ class CRM_Core_BAO_EntityTag extends CRM_Core_DAO_EntityTag {
   }
 
   /**
-   * This function returns all entities assigned to a specific tag
+   * This function returns all entities assigned to a specific tag.
    *
    * @param object $tag
    *   An object of a tag.
@@ -289,7 +273,7 @@ class CRM_Core_BAO_EntityTag extends CRM_Core_DAO_EntityTag {
   }
 
   /**
-   * Get contact tags
+   * Get contact tags.
    */
   public static function getContactTags($contactID, $count = FALSE) {
     $contactTags = array();
@@ -322,7 +306,7 @@ class CRM_Core_BAO_EntityTag extends CRM_Core_DAO_EntityTag {
   }
 
   /**
-   * Get child contact tags given parentId
+   * Get child contact tags given parentId.
    */
   public static function getChildEntityTags($parentId, $entityId, $entityTable = 'civicrm_contact') {
     $entityTags = array();
@@ -397,27 +381,37 @@ class CRM_Core_BAO_EntityTag extends CRM_Core_DAO_EntityTag {
 
   /**
    * Get options for a given field.
+   *
    * @see CRM_Core_DAO::buildOptions
+   * @see CRM_Core_DAO::buildOptionsContext
    *
    * @param string $fieldName
    * @param string $context
-   *   : @see CRM_Core_DAO::buildOptionsContext.
+   *   As per CRM_Core_DAO::buildOptionsContext.
    * @param array $props
-   *   whatever is known about this dao object.
+   *   whatever is known about this dao object.
    *
-   * @return Array|bool
+   * @return array|bool
    */
   public static function buildOptions($fieldName, $context = NULL, $props = array()) {
     $params = array();
 
-    $options = CRM_Core_PseudoConstant::get(__CLASS__, $fieldName, $params, $context);
+    if ($fieldName == 'tag' || $fieldName == 'tag_id') {
+      if (!empty($props['entity_table'])) {
+        $entity = CRM_Utils_Type::escape($props['entity_table'], 'String');
+        $params[] = "used_for LIKE '%$entity%'";
+      }
 
-    // Output tag list as nested hierarchy
-    // TODO: This will only work when api.entity is "entity_tag". What about others?
-    if (($fieldName == 'tag' || $fieldName == 'tag_id') && ($context == 'search' || $context == 'create')) {
-      $options = CRM_Core_BAO_Tag::getTags('civicrm_contact', CRM_Core_DAO::$_nullArray, CRM_Utils_Array::value('parent_id', $params), '- ');
+      // Output tag list as nested hierarchy
+      // TODO: This will only work when api.entity is "entity_tag". What about others?
+      if ($context == 'search' || $context == 'create') {
+        return CRM_Core_BAO_Tag::getTags(CRM_Utils_Array::value('entity_table', $props, 'civicrm_contact'), CRM_Core_DAO::$_nullArray, CRM_Utils_Array::value('parent_id', $params), '- ');
+      }
     }
 
+    $options = CRM_Core_PseudoConstant::get(__CLASS__, $fieldName, $params, $context);
+
     return $options;
   }
+
 }