codespell: CRM/*
[civicrm-core.git] / CRM / Core / BAO / Block.php
index 034846cad79b144d546dbc7e19c40dbdec3c13ef..31317569558d2857811272d010fd224a45665f9f 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
- | 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        |
  +--------------------------------------------------------------------+
-*/
+ */
 
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2014
+ * @copyright CiviCRM LLC (c) 2004-2015
  * $Id$
  *
  * add static functions to include some common functionality
 class CRM_Core_BAO_Block {
 
   /**
-   * Fields that are required for a valid block
+   * Fields that are required for a valid block.
    */
   static $requiredBlockFields = array(
     'email' => array('email'),
     'phone' => array('phone'),
     'im' => array('name'),
-    'openid' => array('openid')
+    'openid' => array('openid'),
   );
 
   /**
    * Given the list of params in the params array, fetch the object
    * and store the values in the values array
    *
-   * @param string $blockName name of the above object
-   * @param array $params input parameters to find object
+   * @param string $blockName
+   *   Name of the above object.
+   * @param array $params
+   *   Input parameters to find object.
    *
-   * @return array of $block objects.
-   * @static
+   * @return array
+   *   Array of $block objects.
    */
   public static function &getValues($blockName, $params) {
     if (empty($params)) {
       return NULL;
     }
     $BAOString = 'CRM_Core_BAO_' . $blockName;
-    $block = new $BAOString( );
+    $block = new $BAOString();
 
     $blocks = array();
     if (!isset($params['entity_table'])) {
@@ -81,9 +83,9 @@ class CRM_Core_BAO_Block {
 
       $count = 1;
       foreach ($blockIds as $blockId) {
-        $block = new $BAOString( );
-        $block->id        = $blockId['id'];
-        $getBlocks        = self::retrieveBlock($block, $blockName);
+        $block = new $BAOString();
+        $block->id = $blockId['id'];
+        $getBlocks = self::retrieveBlock($block, $blockName);
         $blocks[$count++] = array_pop($getBlocks);
       }
     }
@@ -95,11 +97,13 @@ class CRM_Core_BAO_Block {
    * Given the list of params in the params array, fetch the object
    * and store the values in the values array
    *
-   * @param Object $block typically a Phone|Email|IM|OpenID object
-   * @param string $blockName name of the above object
+   * @param Object $block
+   *   Typically a Phone|Email|IM|OpenID object.
+   * @param string $blockName
+   *   Name of the above object.
    *
-   * @return array of $block objects.
-   * @static
+   * @return array
+   *   Array of $block objects.
    */
   public static function retrieveBlock(&$block, $blockName) {
     // we first get the primary location due to the order by clause
@@ -122,13 +126,15 @@ class CRM_Core_BAO_Block {
   }
 
   /**
-   * Check if the current block object has any valid data
+   * Check if the current block object has any valid data.
    *
-   * @param array  $blockFields   array of fields that are of interest for this object
-   * @param array  $params        associated array of submitted fields
+   * @param array $blockFields
+   *   Array of fields that are of interest for this object.
+   * @param array $params
+   *   Associated array of submitted fields.
    *
-   * @return boolean              true if the block has data, otherwise false
-   * @static
+   * @return bool
+   *   true if the block has data, otherwise false
    */
   public static function dataExists($blockFields, &$params) {
     foreach ($blockFields as $field) {
@@ -140,13 +146,15 @@ class CRM_Core_BAO_Block {
   }
 
   /**
-   * Check if the current block exits
+   * Check if the current block exits.
    *
-   * @param string  $blockName   bloack name
-   * @param array   $params      associated array of submitted fields
+   * @param string $blockName
+   *   Bloack name.
+   * @param array $params
+   *   Associated array of submitted fields.
    *
-   * @return boolean             true if the block exits, otherwise false
-   * @static
+   * @return bool
+   *   true if the block exits, otherwise false
    */
   public static function blockExists($blockName, &$params) {
     // return if no data present
@@ -158,17 +166,19 @@ class CRM_Core_BAO_Block {
   }
 
   /**
-   * Get all block ids for a contact
+   * Get all block ids for a contact.
    *
-   * @param string $blockName block name
-   * @param int $contactId contact id
+   * @param string $blockName
+   *   Block name.
+   * @param int $contactId
+   *   Contact id.
    *
    * @param null $entityElements
    * @param bool $updateBlankLocInfo
    *
-   * @return array $contactBlockIds formatted array of block ids
+   * @return array
+   *   formatted array of block ids
    *
-   * @static
    */
   public static function getBlockIds($blockName, $contactId = NULL, $entityElements = NULL, $updateBlankLocInfo = FALSE) {
     $allBlocks = array();
@@ -189,35 +199,37 @@ class CRM_Core_BAO_Block {
       // e.g $bao = new $baoString;
       // $bao->getAllBlocks()
       $baoFunction = 'all' . $name . 's';
-      $allBlocks = $baoString::$baoFunction( $contactId, $updateBlankLocInfo );
+      $allBlocks = $baoString::$baoFunction($contactId, $updateBlankLocInfo);
     }
     elseif (!empty($entityElements) && $blockName != 'openid') {
       $baoFunction = 'allEntity' . $name . 's';
-      $allBlocks = $baoString::$baoFunction( $entityElements );
+      $allBlocks = $baoString::$baoFunction($entityElements);
     }
 
     return $allBlocks;
   }
 
   /**
-   * Takes an associative array and creates a block
+   * Takes an associative array and creates a block.
    *
-   * @param string $blockName block name
-   * @param array $params (reference ) an assoc array of name/value pairs
+   * @param string $blockName
+   *   Block name.
+   * @param array $params
+   *   (reference ) an assoc array of name/value pairs.
    * @param null $entity
    * @param int $contactId
    *
-   * @return object       CRM_Core_BAO_Block object on success, null otherwise
-   * @static
+   * @return object
+   *   CRM_Core_BAO_Block object on success, null otherwise
    */
   public static function create($blockName, &$params, $entity = NULL, $contactId = NULL) {
     if (!self::blockExists($blockName, $params)) {
       return NULL;
     }
 
-    $name           = ucfirst($blockName);
-    $contactId      = NULL;
-    $isPrimary      = $isBilling = TRUE;
+    $name = ucfirst($blockName);
+    $contactId = NULL;
+    $isPrimary = $isBilling = TRUE;
     $entityElements = $blocks = array();
 
     if ($entity) {
@@ -263,7 +275,7 @@ class CRM_Core_BAO_Block {
           }
           if ($resetPrimaryId) {
             $baoString = 'CRM_Core_BAO_' . $blockName;
-            $block = new $baoString( );
+            $block = new $baoString();
             $block->selectAdd();
             $block->selectAdd("id, is_primary");
             $block->id = $resetPrimaryId;
@@ -363,7 +375,7 @@ class CRM_Core_BAO_Block {
 
       $blockFields = array_merge($value, $contactFields);
       $baoString = 'CRM_Core_BAO_' . $name;
-      $blocks[] = $baoString::add( $blockFields );
+      $blocks[] = $baoString::add($blockFields);
     }
 
     // we need to delete blocks that were deleted during update
@@ -380,13 +392,14 @@ class CRM_Core_BAO_Block {
   }
 
   /**
-   * Delete block
+   * Delete block.
    *
-   * @param  string $blockName       block name
-   * @param  int    $params          associates array
+   * @param string $blockName
+   *   Block name.
+   * @param int $params
+   *   Associates array.
    *
    * @return void
-   * @static
    */
   public static function blockDelete($blockName, $params) {
     $name = ucfirst($blockName);
@@ -398,12 +411,11 @@ class CRM_Core_BAO_Block {
     }
 
     $baoString = 'CRM_Core_DAO_' . $name;
-    $block = new $baoString( );
+    $block = new $baoString();
 
     $block->copyValues($params);
-    /*
-     * CRM-11006 add call to pre and post hook for delete action
-     */
+
+    // CRM-11006 add call to pre and post hook for delete action
     CRM_Utils_Hook::pre('delete', $name, $block->id, CRM_Core_DAO::$_nullArray);
     $block->delete();
     CRM_Utils_Hook::post('delete', $name, $block->id, $block);
@@ -427,7 +439,6 @@ class CRM_Core_BAO_Block {
    * @param $class
    *
    * @throws API_Exception
-   * @static
    */
   public static function handlePrimary(&$params, $class) {
     $table = CRM_Core_DAO_AllCoreTables::getTableForClass($class);
@@ -453,8 +464,8 @@ class CRM_Core_BAO_Block {
     if (!empty($params['is_primary'])) {
       $sql = "UPDATE $table SET is_primary = 0 WHERE contact_id = %1";
       $sqlParams = array(1 => array($contactId, 'Integer'));
-      // we don't want to create unecessary entries in the log_ tables so exclude the one we are working on
-      if(!empty($params['id'])){
+      // we don't want to create unnecessary entries in the log_ tables so exclude the one we are working on
+      if (!empty($params['id'])) {
         $sql .= " AND id <> %2";
         $sqlParams[2] = array($params['id'], 'Integer');
       }
@@ -477,7 +488,7 @@ class CRM_Core_BAO_Block {
        * is_primary to 1
        * CRM-10451
        */
-      if ( $existingEntities->N == 1 && $existingEntities->id == CRM_Utils_Array::value( 'id', $params ) ) {
+      if ($existingEntities->N == 1 && $existingEntities->id == CRM_Utils_Array::value('id', $params)) {
         $params['is_primary'] = 1;
         return;
       }
@@ -494,23 +505,22 @@ class CRM_Core_BAO_Block {
   }
 
   /**
-   * Sort location array so primary element is first
+   * Sort location array so primary element is first.
    *
    * @param array $locations
-   *
    */
-  public static function sortPrimaryFirst(&$locations){
+  public static function sortPrimaryFirst(&$locations) {
     uasort($locations, 'self::primaryComparison');
   }
 
-/**
- * compare 2 locations to see which should go first based on is_primary
- * (sort function for sortPrimaryFirst)
- * @param array $location1
- * @param array $location2
- * @return number
- */
-  public static function primaryComparison($location1, $location2){
+  /**
  * compare 2 locations to see which should go first based on is_primary
  * (sort function for sortPrimaryFirst)
  * @param array $location1
  * @param array $location2
+   * @return int
  */
+  public static function primaryComparison($location1, $location2) {
     $l1 = CRM_Utils_Array::value('is_primary', $location1);
     $l2 = CRM_Utils_Array::value('is_primary', $location2);
     if ($l1 == $l2) {
@@ -518,4 +528,5 @@ class CRM_Core_BAO_Block {
     }
     return ($l1 < $l2) ? -1 : 1;
   }
+
 }