Merge remote-tracking branch 'upstream/4.5' into 4.5-master-2015-02-09-11-44-07
[civicrm-core.git] / CRM / Contact / BAO / ContactType.php
index 76181fa440796bff150cbb7602bf2a9d16200c38..24b44f90930eb7188b8ef45cebb5727ea02c2d83 100644 (file)
@@ -23,7 +23,7 @@
  | GNU Affero General Public License or the licensing of CiviCRM,     |
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
-*/
+ */
 
 /**
  *
 class CRM_Contact_BAO_ContactType extends CRM_Contact_DAO_ContactType {
 
   /**
-   * Fetch object based on array of properties
+   * Fetch object based on array of properties.
    *
-   * @param array $params   (reference ) an assoc array of name/value pairs
-   * @param array $defaults (reference ) an assoc array to hold the flattened values
+   * @param array $params
+   *   (reference ) an assoc array of name/value pairs.
+   * @param array $defaults
+   *   (reference ) an assoc array to hold the flattened values.
    *
-   * @return CRM_Contact_BAO_ContactType object on success, null otherwise
-   * @static
+   * @return CRM_Contact_BAO_ContactType|null
+   *   object on success, null otherwise
    */
   public static function retrieve(&$params, &$defaults) {
     $contactType = new CRM_Contact_DAO_ContactType();
@@ -69,8 +71,8 @@ class CRM_Contact_BAO_ContactType extends CRM_Contact_DAO_ContactType {
    *
    * @param bool $all
    *
-   * @return  array of basic contact types information.
-   * @static
+   * @return array
+   *   Array of basic contact types information.
    */
   public static function &basicTypeInfo($all = FALSE) {
     static $_cache = NULL;
@@ -115,8 +117,8 @@ WHERE  parent_id IS NULL
    *
    * @param bool $all
    *
-   * @return  array of basic contact types
-   * @static
+   * @return array
+   *   Array of basic contact types
    */
   public static function basicTypes($all = FALSE) {
     return array_keys(self::basicTypeInfo($all));
@@ -142,13 +144,14 @@ WHERE  parent_id IS NULL
   /**
    * Retrieve all subtypes Information.
    *
-   * @param array $contactType .
+   * @param array $contactType
+   *   ..
    * @param bool $all
    * @param bool $ignoreCache
    * @param bool $reset
    *
-   * @return  array of sub type information
-   * @static
+   * @return array
+   *   Array of sub type information
    */
   public static function &subTypeInfo($contactType = NULL, $all = FALSE, $ignoreCache = FALSE, $reset = FALSE) {
     static $_cache = NULL;
@@ -210,14 +213,15 @@ WHERE  subtype.name IS NOT NULL AND subtype.parent_id IS NOT NULL {$ctWHERE}
    *
    *   retrieve all subtypes
    *
-   * @param array $contactType .
+   * @param array $contactType
+   *   ..
    * @param bool $all
    * @param string $columnName
    * @param bool $ignoreCache
    *
-   * @return  array of all subtypes OR list of subtypes associated to
-   *a given basic contact type
-   * @static
+   * @return array
+   *   all subtypes OR list of subtypes associated to
+   *   a given basic contact type
    */
   public static function subTypes($contactType = NULL, $all = FALSE, $columnName = 'name', $ignoreCache = FALSE) {
     if ($columnName == 'name') {
@@ -232,13 +236,13 @@ WHERE  subtype.name IS NOT NULL AND subtype.parent_id IS NOT NULL {$ctWHERE}
    *
    * retrieve subtype pairs with name as 'subtype-name' and 'label' as value
    *
-   * @param array $contactType .
+   * @param array $contactType
    * @param bool $all
    * @param string $labelPrefix
    * @param bool $ignoreCache
    *
-   * @return list of subtypes with name as 'subtype-name' and 'label' as value
-   * @static
+   * @return array
+   *   list of subtypes with name as 'subtype-name' and 'label' as value
    */
   public static function subTypePairs($contactType = NULL, $all = FALSE, $labelPrefix = '- ', $ignoreCache = FALSE) {
     $subtypes = self::subTypeInfo($contactType, $all, $ignoreCache);
@@ -256,8 +260,8 @@ WHERE  subtype.name IS NOT NULL AND subtype.parent_id IS NOT NULL {$ctWHERE}
    *
    * @param bool $all
    *
-   * @return  array of basic types + all subtypes.
-   * @static
+   * @return array
+   *   Array of basic types + all subtypes.
    */
   public static function contactTypes($all = FALSE) {
     return array_keys(self::contactTypeInfo($all));
@@ -270,8 +274,8 @@ WHERE  subtype.name IS NOT NULL AND subtype.parent_id IS NOT NULL {$ctWHERE}
    * @param bool $all
    * @param bool $reset
    *
-   * @return  array of basic types + all subtypes.
-   * @static
+   * @return array
+   *   Array of basic types + all subtypes.
    */
   public static function contactTypeInfo($all = FALSE, $reset = FALSE) {
     static $_cache = NULL;
@@ -302,7 +306,7 @@ WHERE  type.name IS NOT NULL
         }
 
         $dao = CRM_Core_DAO::executeQuery($sql,
-          CRM_Core_DAO::$_nullArray,
+          array(),
           FALSE,
           'CRM_Contact_DAO_ContactType'
         );
@@ -330,8 +334,8 @@ WHERE  type.name IS NOT NULL
    * @param null $typeName
    * @param null $delimiter
    *
-   * @return array of basictypes with name as 'built-in name' and 'label' as value
-   * @static
+   * @return array
+   *   Array of basictypes with name as 'built-in name' and 'label' as value
    */
   public static function contactTypePairs($all = FALSE, $typeName = NULL, $delimiter = NULL) {
     $types = self::contactTypeInfo($all);
@@ -358,7 +362,7 @@ WHERE  type.name IS NOT NULL
   }
 
   /**
-   * Get a list of elements for select box
+   * Get a list of elements for select box.
    * Note that this used to default to using the hex(01) character - which results in an invalid character being used in form fields
    * which was not handled well be anything that loaded & resaved the html (outside core)
    * The use of this separator is now explicit in the calling functions as a step towards it's removal
@@ -369,7 +373,8 @@ WHERE  type.name IS NOT NULL
    *
    * @return mixed
    */
-  static function getSelectElements($all = FALSE,
+  public  static function getSelectElements(
+    $all = FALSE,
     $isSeparator = TRUE,
     $separator = '__'
   ) {
@@ -408,12 +413,12 @@ AND   ( p.is_active = 1 OR p.id IS NULL )
         $dao = CRM_Core_DAO::executeQuery($sql);
         while ($dao->fetch()) {
           if (!empty($dao->parent_id)) {
-            $key   = $isSeparator ? $dao->parent_name . $separator . $dao->child_name : $dao->child_name;
+            $key = $isSeparator ? $dao->parent_name . $separator . $dao->child_name : $dao->child_name;
             $label = "- {$dao->child_label}";
             $pName = $dao->parent_name;
           }
           else {
-            $key   = $dao->child_name;
+            $key = $dao->child_name;
             $label = $dao->child_label;
             $pName = $dao->child_name;
           }
@@ -439,13 +444,14 @@ AND   ( p.is_active = 1 OR p.id IS NULL )
   }
 
   /**
-   * Check if a given type is a subtype
+   * Check if a given type is a subtype.
    *
-   * @param string $subType contact subType.
+   * @param string $subType
+   *   Contact subType.
    * @param bool $ignoreCache
    *
-   * @return  boolean true if subType, false otherwise.
-   * @static
+   * @return bool
+   *   true if subType, false otherwise.
    */
   public static function isaSubType($subType, $ignoreCache = FALSE) {
     return in_array($subType, self::subTypes(NULL, TRUE, 'name', $ignoreCache));
@@ -454,10 +460,8 @@ AND   ( p.is_active = 1 OR p.id IS NULL )
   /**
    * Retrieve the basic contact type associated with given subType.
    *
-   *@param array/string $subType contact subType.
-   *@return array/string of basicTypes.
-   *@static
-   *
+   * @param array /string $subType contact subType.
+   * @return array/string of basicTypes.
    */
   public static function getBasicType($subType) {
     static $_cache = NULL;
@@ -495,11 +499,12 @@ WHERE  subtype.name IN ('" . implode("','", $subType) . "' )";
   /**
    * Suppress all subtypes present in given array.
    *
-   * @param array $subTypes contact subTypes
+   * @param array $subTypes
+   *   Contact subTypes.
    * @param bool $ignoreCache
    *
-   * @return array of suppressed subTypes.
-   * @static
+   * @return array
+   *   Array of suppressed subTypes.
    */
   public static function suppressSubTypes(&$subTypes, $ignoreCache = FALSE) {
     $subTypes = array_diff($subTypes, self::subTypes(NULL, TRUE, 'name', $ignoreCache));
@@ -509,13 +514,15 @@ WHERE  subtype.name IN ('" . implode("','", $subType) . "' )";
   /**
    * Verify if a given subtype is associated with a given basic contact type.
    *
-   * @param  string $subType contact subType
-   * @param  string $contactType contact Type
+   * @param string $subType
+   *   Contact subType.
+   * @param string $contactType
+   *   Contact Type.
    * @param bool $ignoreCache
    * @param string $columnName
    *
-   * @return boolean true if contact extends, false otherwise.
-   * @static
+   * @return bool
+   *   true if contact extends, false otherwise.
    */
   public static function isExtendsContactType($subType, $contactType, $ignoreCache = FALSE, $columnName = 'name') {
     if (!is_array($subType)) {
@@ -527,10 +534,10 @@ WHERE  subtype.name IN ('" . implode("','", $subType) . "' )";
   }
 
   /**
-   * Create shortcuts menu for contactTypes
+   * Create shortcuts menu for contactTypes.
    *
-   * @return array  of contactTypes
-   * @static
+   * @return array
+   *   of contactTypes
    */
   public static function getCreateNewList() {
     $shortCuts = array();
@@ -564,12 +571,12 @@ WHERE  subtype.name IN ('" . implode("','", $subType) . "' )";
   }
 
   /**
-   * Delete Contact SubTypes
+   * Delete Contact SubTypes.
    *
-   * @param  int $contactTypeId ID of the Contact Subtype to be deleted.
+   * @param int $contactTypeId
+   *   ID of the Contact Subtype to be deleted.
    *
    * @return bool
-   * @static
    */
   public static function del($contactTypeId) {
 
@@ -616,23 +623,23 @@ WHERE name = %1";
   }
 
   /**
-   * Add or update Contact SubTypes
+   * Add or update Contact SubTypes.
    *
-   * @param  array $params  an assoc array of name/value pairs
+   * @param array $params
+   *   An assoc array of name/value pairs.
    *
-   * @return object
-   * @static
+   * @return object|void
    */
   public static function add(&$params) {
 
     // label or name
     if (empty($params['id']) && empty($params['label'])) {
-      return;
+      return NULL;
     }
     if (!empty($params['parent_id']) &&
       !CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_ContactType', $params['parent_id'])
     ) {
-      return;
+      return NULL;
     }
 
     $contactType = new CRM_Contact_DAO_ContactType();
@@ -643,8 +650,8 @@ WHERE name = %1";
     $contactType->save();
     if ($contactType->find(TRUE)) {
       $contactName = $contactType->name;
-      $contact     = ucfirst($contactType->label);
-      $active      = $contactType->is_active;
+      $contact = ucfirst($contactType->label);
+      $active = $contactType->is_active;
     }
 
     if (!empty($params['id'])) {
@@ -681,13 +688,15 @@ WHERE name = %1";
   }
 
   /**
-   * Update the is_active flag in the db
+   * Update the is_active flag in the db.
    *
-   * @param int      $id        id of the database record
-   * @param boolean  $is_active value we want to set the is_active field
+   * @param int $id
+   *   Id of the database record.
+   * @param bool $is_active
+   *   Value we want to set the is_active field.
    *
-   * @return Object             DAO object on success, null otherwise
-   * @static
+   * @return Object
+   *   DAO object on success, null otherwise
    */
   public static function setIsActive($id, $is_active) {
     $params = array('id' => $id);
@@ -720,11 +729,12 @@ WHERE name = %1";
    * on the basis of custom data and relationship of specific subtype
    * currently used in contact/edit form amd in import validation
    *
-   * @param  int     $contactId    contact id.
-   * @param  string  $subType      subtype.
+   * @param int $contactId
+   *   Contact id.
+   * @param string $subType
+   *   Subtype.
    *
-   * @return boolean true/false.
-   * @static
+   * @return bool
    */
   public static function isAllowEdit($contactId, $subType = NULL) {
 
@@ -792,8 +802,8 @@ WHERE name = %1";
   public static function hasRelationships($contactId, $contactType) {
     $subTypeClause = NULL;
     if (self::isaSubType($contactType)) {
-      $subType       = $contactType;
-      $contactType   = self::getBasicType($subType);
+      $subType = $contactType;
+      $contactType = self::getBasicType($subType);
       $subTypeClause = " AND ( ( crt.contact_type_a = '{$contactType}' AND crt.contact_sub_type_a = '{$subType}') OR
                                      ( crt.contact_type_b = '{$contactType}' AND crt.contact_sub_type_b = '{$subType}')  ) ";
     }
@@ -832,8 +842,8 @@ LIMIT 1";
 
     $customSet = $subTypeClause = array();
     foreach ($subtypeSet as $subtype) {
-      $subtype         = CRM_Utils_Type::escape($subtype, 'String');
-      $subType         = CRM_Core_DAO::VALUE_SEPARATOR . $subtype . CRM_Core_DAO::VALUE_SEPARATOR;
+      $subtype = CRM_Utils_Type::escape($subtype, 'String');
+      $subType = CRM_Core_DAO::VALUE_SEPARATOR . $subtype . CRM_Core_DAO::VALUE_SEPARATOR;
       $subTypeClause[] = "extends_entity_column_value LIKE '%{$subtype}%' ";
     }
     $query = "SELECT table_name
@@ -847,7 +857,7 @@ WHERE extends = %1 AND " . implode(" OR ", $subTypeClause);
   }
 
   /**
-   * Function that does something
+   * Function that does something.
    * @todo what does this function do?
    *
    * @param int $contactID
@@ -857,7 +867,8 @@ WHERE extends = %1 AND " . implode(" OR ", $subTypeClause);
    *
    * @return bool
    */
-  static function deleteCustomSetForSubtypeMigration($contactID,
+  public  static function deleteCustomSetForSubtypeMigration(
+    $contactID,
     $contactType,
     $oldSubtypeSet = array(),
     $newSubtypeSet = array()
@@ -877,8 +888,10 @@ WHERE extends = %1 AND " . implode(" OR ", $subTypeClause);
    * This function currently works for contact subtypes only and could be later improved / genralized
    * to work for other subtypes as well.
    *
-   * @param   int  $gID      - custom group id.
-   * @param  array $subtypes - list of subtypes related to which entry is to be removed.
+   * @param int $gID
+   *   Custom group id.
+   * @param array $subtypes
+   *   List of subtypes related to which entry is to be removed.
    *
    * @return void
    */
@@ -913,8 +926,10 @@ WHERE ($subtypeClause)";
   /**
    * Delete content / rows of a custom table specific entity-id for a given custom-group table.
    *
-   * @param  int $customTable - custom table name.
-   * @param  int $entityID - entity id.
+   * @param int $customTable
+   *   Custom table name.
+   * @param int $entityID
+   *   Entity id.
    *
    * @return void
    */
@@ -923,4 +938,5 @@ WHERE ($subtypeClause)";
     $query = "DELETE FROM {$customTable} WHERE entity_id = %1";
     return CRM_Core_DAO::singleValueQuery($query, array(1 => array($entityID, 'Integer')));
   }
+
 }