Style - Remove @public, @private, @protected
[civicrm-core.git] / CRM / Contact / BAO / Relationship.php
index 19f2a9d3a6fcb24a3b2122425561819d81208117..2a142ea00345e3c3c88d854da9bb36d0122311d9 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.5                                                |
+ | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
 class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship {
 
   /**
-   * various constants to indicate different type of relationships
+   * Various constants to indicate different type of relationships
    *
    * @var int
    */
-  CONST PAST = 1, DISABLED = 2, CURRENT = 4, INACTIVE = 8;
+  const PAST = 1, DISABLED = 2, CURRENT = 4, INACTIVE = 8;
 
   /**
-   * takes an associative array and creates a relationship object
+   * Takes an associative array and creates a relationship object
    *
    *
    * @param array $params (reference ) an assoc array of name/value pairs
@@ -50,11 +50,11 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship {
    * per http://wiki.civicrm.org/confluence/display/CRM/Database+layer
    *  "we are moving away from the $ids param "
    *
-   * @return object CRM_Contact_BAO_Relationship object
+   * @return CRM_Contact_BAO_Relationship object
    * @access public
    * @static
    */
-  static function create(&$params, $ids = array()) {
+  public static function create(&$params, $ids = array()) {
     $valid = $invalid = $duplicate = $saved = 0;
     $relationships = $relationshipIds = array();
     $relationshipId = CRM_Utils_Array::value('relationship', $ids, CRM_Utils_Array::value('id', $params));
@@ -194,11 +194,11 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship {
    * @param integer $contactId  this is contact id for adding relationship
    * @param array $ids          the array that holds all the db ids
    *
-   * @return object CRM_Contact_BAO_Relationship
+   * @return CRM_Contact_BAO_Relationship
    * @access public
    * @static
    */
-  static function add(&$params, $ids = array(), $contactId = NULL) {
+  public static function add(&$params, $ids = array(), $contactId = NULL) {
     $relationshipId =
       CRM_Utils_Array::value('relationship', $ids, CRM_Utils_Array::value('id', $params));
 
@@ -261,7 +261,7 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship {
 
     $relationship->free();
 
-    CRM_Utils_Hook::post($hook, 'Relationship', $relationshipId, $relationship);
+    CRM_Utils_Hook::post($hook, 'Relationship', $relationship->id, $relationship);
 
     return $relationship;
   }
@@ -272,7 +272,7 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship {
    * @access public
    * @static
    */
-  static function getdefaults() {
+  public static function getdefaults() {
     return array(
       'is_active' => 0,
       'is_permission_a_b' => 0,
@@ -294,7 +294,7 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship {
    * @access public
    * @static
    */
-  static function dataExists(&$params) {
+  public static function dataExists(&$params) {
     // return if no data present
     if (!is_array(CRM_Utils_Array::value('contact_check', $params))) {
       return FALSE;
@@ -303,7 +303,7 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship {
   }
 
   /**
-   * Function to get get list of relationship type based on the contact type.
+   * Get get list of relationship type based on the contact type.
    *
    * @param int $contactId this is the contact id of the current contact.
    * @param null $contactSuffix
@@ -311,15 +311,11 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship {
    * @param string $contactType contact type
    * @param boolean $all if true returns relationship types in both the direction
    * @param string $column name/label that going to retrieve from db.
-   *
-   *
    * @param bool $biDirectional
    * @param string $contactSubType includes relationshiptypes between this subtype
-   *
    * @param boolean $onlySubTypeRelationTypes if set only subtype which is passed by $contactSubType
    *                                          related relationshiptypes get return
    *
-   * @internal param string $strContact this value is currently ignored, keeping it there for legacy reasons
    * @access public
    * @static
    *
@@ -373,15 +369,9 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship {
           (!$otherContactType) ||
           $value['contact_type_b'] == $otherContactType
         ) &&
-        (!$contactSubType ||
-          (in_array($value['contact_sub_type_a'], $contactSubType) ||
-            in_array($value['contact_sub_type_b'], $contactSubType) ||
-            ((!$value['contact_sub_type_b'] &&
-                !$value['contact_sub_type_a']
-              ) &&
-              !$onlySubTypeRelationTypes
-            )
-          )
+        (in_array($value['contact_sub_type_a'], $contactSubType) ||
+          in_array($value['contact_sub_type_b'], $contactSubType) ||
+          (!$value['contact_sub_type_a'] && !$onlySubTypeRelationTypes)
         )
       ) {
         $relationshipType[$key . '_a_b'] = $value["{$column}_a_b"];
@@ -394,15 +384,9 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship {
           (!$otherContactType) ||
           $value['contact_type_a'] == $otherContactType
         ) &&
-        (!$contactSubType ||
-          (in_array($value['contact_sub_type_b'], $contactSubType) ||
-            in_array($value['contact_sub_type_a'], $contactSubType) ||
-            ((!$value['contact_sub_type_a'] &&
-                !$value['contact_sub_type_b']
-              ) &&
-              !$onlySubTypeRelationTypes
-            )
-          )
+        (in_array($value['contact_sub_type_b'], $contactSubType) ||
+          in_array($value['contact_sub_type_a'], $contactSubType) ||
+          (!$value['contact_sub_type_b'] && !$onlySubTypeRelationTypes)
         )
       ) {
         $relationshipType[$key . '_b_a'] = $value["{$column}_b_a"];
@@ -426,12 +410,12 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship {
   }
 
   /**
-   * @param $id
+   * @param int $id
    * @param $action
    *
    * @return CRM_Contact_DAO_Relationship
    */
-  static function clearCurrentEmployer($id, $action) {
+  public static function clearCurrentEmployer($id, $action) {
     $relationship = new CRM_Contact_DAO_Relationship();
     $relationship->id = $id;
     $relationship->find(TRUE);
@@ -455,7 +439,7 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship {
   }
 
   /**
-   * Function to delete the relationship
+   * Delete the relationship
    *
    * @param int $id relationship id
    *
@@ -464,7 +448,7 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship {
    *
    * @static
    */
-  static function del($id) {
+  public static function del($id) {
     // delete from relationship table
     CRM_Utils_Hook::pre('delete', 'Relationship', $id, CRM_Core_DAO::$_nullArray);
 
@@ -491,7 +475,7 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship {
     $relationship->delete();
     CRM_Core_Session::setStatus(ts('Selected relationship has been deleted successfully.'), ts('Record Deleted'), 'success');
 
-    CRM_Utils_Hook::post('delete', 'Relationship', $relationship->id, $relationship);
+    CRM_Utils_Hook::post('delete', 'Relationship', $id, $relationship);
 
     // delete the recently created Relationship
     $relationshipRecent = array(
@@ -504,7 +488,7 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship {
   }
 
   /**
-   * Function to disable/enable the relationship
+   * Disable/enable the relationship
    *
    * @param int $id relationship id
    *
@@ -514,7 +498,7 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship {
    * @access public
    * @static
    */
-  static function disableEnableRelationship($id, $action) {
+  public static function disableEnableRelationship($id, $action) {
     $relationship = self::clearCurrentEmployer($id, $action);
     if (CRM_Core_Permission::access('CiviMember')) {
       // create $params array which isrequired to delete memberships
@@ -556,7 +540,7 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship {
    * @access public
    * @static
    */
-  static function deleteContact($contactId) {
+  public static function deleteContact($contactId) {
     $relationship = new CRM_Contact_DAO_Relationship();
     $relationship->contact_id_a = $contactId;
     $relationship->delete();
@@ -569,7 +553,7 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship {
   }
 
   /**
-   * Function to get the other contact in a relationship
+   * Get the other contact in a relationship
    *
    * @param int $id relationship id
    *
@@ -579,7 +563,7 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship {
    * @access public
    * @static
    */
-  static function getContactIds($id) {
+  public static function getContactIds($id) {
     $relationship = new CRM_Contact_DAO_Relationship();
 
     $relationship->id = $id;
@@ -591,7 +575,7 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship {
   }
 
   /**
-   * Function to check if the relationship type selected between two contacts is correct
+   * Check if the relationship type selected between two contacts is correct
    *
    * @param int $contact_a 1st contact id
    * @param int $contact_b 2nd contact id
@@ -601,7 +585,7 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship {
    * @access public
    * @static
    */
-  static function checkRelationshipType($contact_a, $contact_b, $relationshipTypeId) {
+  public static function checkRelationshipType($contact_a, $contact_b, $relationshipTypeId) {
     $relationshipType = new CRM_Contact_DAO_RelationshipType();
     $relationshipType->id = $relationshipTypeId;
     $relationshipType->selectAdd();
@@ -632,7 +616,7 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship {
   }
 
   /**
-   * this function does the validtion for valid relationship
+   * This function does the validtion for valid relationship
    *
    * @param array $params this array contains the values there are subitted by the form
    * @param array $ids the array that holds all the db ids
@@ -642,7 +626,7 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship {
   @access public
    * @static
    */
-  static function checkValidRelationship(&$params, &$ids, $contactId) {
+  public static function checkValidRelationship(&$params, &$ids, $contactId) {
     $errors = '';
 
     // get the string of relationship type
@@ -659,7 +643,7 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship {
   }
 
   /**
-   * this function checks for duplicate relationship
+   * This function checks for duplicate relationship
    *
    * @param array $params (reference ) an assoc array of name/value pairs
    * @param integer $id this the id of the contact whom we are adding relationship
@@ -670,7 +654,7 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship {
    * @access public
    * @static
    */
-  static function checkDuplicateRelationship(&$params, $id, $contactId = 0, $relationshipId = 0) {
+  public static function checkDuplicateRelationship(&$params, $id, $contactId = 0, $relationshipId = 0) {
     $relationshipTypeId = CRM_Utils_Array::value('relationship_type_id', $params);
     list($type, $first, $second) = explode('_', $relationshipTypeId);
 
@@ -736,7 +720,7 @@ WHERE  relationship_type_id = " . CRM_Utils_Type::escape($type, 'Integer');
   }
 
   /**
-   * 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
@@ -745,7 +729,7 @@ WHERE  relationship_type_id = " . CRM_Utils_Type::escape($type, 'Integer');
    * @return Object             DAO object on success, null otherwise
    * @static
    */
-  static function setIsActive($id, $is_active) {
+  public static function setIsActive($id, $is_active) {
     // as both the create & add functions have a bunch of logic in them that
     // doesn't seem to cope with a normal update we will call the api which
     // has tested handling for this
@@ -774,13 +758,12 @@ WHERE  relationship_type_id = " . CRM_Utils_Type::escape($type, 'Integer');
    *
    * @param array $params input parameters to find object
    * @param array $values output values of the object
-   * @internal param array $ids the array that holds all the db ids
    *
    * @return array (reference)   the values that could be potentially assigned to smarty
    * @access public
    * @static
    */
-  static function &getValues(&$params, &$values) {
+  public static function &getValues(&$params, &$values) {
     if (empty($params)) {
       return NULL;
     }
@@ -804,14 +787,14 @@ WHERE  relationship_type_id = " . CRM_Utils_Type::escape($type, 'Integer');
   }
 
   /**
-   * helper function to form the sql for relationship retrieval
+   * Helper function to form the sql for relationship retrieval
    *
    * @param int $contactId contact id
    * @param int $status (check const at top of file)
    * @param int $numRelationship no of relationships to display (limit)
    * @param int $count get the no of relationships
    * $param int $relationshipId relationship id
-   * @param $relationshipId
+   * @param int $relationshipId
    * @param string $direction the direction we are interested in a_b or b_a
    * @param array $params array of extra values including relationship_type_id per api spec
    *
@@ -821,7 +804,7 @@ WHERE  relationship_type_id = " . CRM_Utils_Type::escape($type, 'Integer');
    * @static
    * @access public
    */
-  static function makeURLClause($contactId, $status, $numRelationship, $count, $relationshipId, $direction, $params = array()) {
+  public static function makeURLClause($contactId, $status, $numRelationship, $count, $relationshipId, $direction, $params = array()) {
     $select = $from = $where = '';
 
     $select = '( ';
@@ -846,6 +829,7 @@ WHERE  relationship_type_id = " . CRM_Utils_Type::escape($type, 'Integer');
                               civicrm_state_province.abbreviation as state,
                               civicrm_country.name as country,
                               civicrm_email.email as email,
+                              civicrm_contact.contact_type as contact_type,
                               civicrm_phone.phone as phone,
                               civicrm_contact.id as civicrm_contact_id,
                               civicrm_relationship.contact_id_b as contact_id_b,
@@ -861,13 +845,11 @@ WHERE  relationship_type_id = " . CRM_Utils_Type::escape($type, 'Integer');
 
       if ($direction == 'a_b') {
         $select .= ', civicrm_relationship_type.label_a_b as label_a_b,
-                              civicrm_relationship_type.label_b_a as relation,
-                              civicrm_contact.contact_type as contact_type_a_b ';
+                              civicrm_relationship_type.label_b_a as relation ';
       }
       else {
         $select .= ', civicrm_relationship_type.label_a_b as label_a_b,
-                              civicrm_relationship_type.label_a_b as relation,
-                              civicrm_contact.contact_type as contact_type_b_a ';
+                              civicrm_relationship_type.label_a_b as relation ';
       }
     }
 
@@ -1055,8 +1037,7 @@ LEFT JOIN  civicrm_country ON (civicrm_address.country_id = civicrm_country.id)
         $values[$rid]['cid'] = $cid;
         $values[$rid]['contact_id_a'] = $relationship->contact_id_a;
         $values[$rid]['contact_id_b'] = $relationship->contact_id_b;
-        $values[$rid]['contact_type_a_b'] = $relationship->contact_type_a_b;
-        $values[$rid]['contact_type_b_a'] = $relationship->contact_type_b_a;
+        $values[$rid]['contact_type'] = $relationship->contact_type;
         $values[$rid]['relationship_type_id'] = $relationship->civicrm_relationship_type_id;
         $values[$rid]['relation'] = $relationship->relation;
         $values[$rid]['name'] = $relationship->sort_name;
@@ -1158,14 +1139,14 @@ LEFT JOIN  civicrm_country ON (civicrm_address.country_id = civicrm_country.id)
   }
 
   /**
-   * Function to get get list of relationship type based on the target contact type.
+   * Get get list of relationship type based on the target contact type.
    *
    * @param string $targetContactType it's valid contact tpye(may be Individual , Organization , Household)
    *
    * @return array - array reference of all relationship types with context to current contact type .
    *
    */
-  function getRelationType($targetContactType) {
+  public function getRelationType($targetContactType) {
     $relationshipType = array();
     $allRelationshipType = CRM_Core_PseudoConstant::relationshipType();
 
@@ -1179,7 +1160,7 @@ LEFT JOIN  civicrm_country ON (civicrm_address.country_id = civicrm_country.id)
   }
 
   /**
-   * Function to create / update / delete membership for related contacts.
+   * Create / update / delete membership for related contacts.
    *
    * This function will create/update/delete membership for related
    * contact based on 1) contact have active membership 2) that
@@ -1195,7 +1176,7 @@ LEFT JOIN  civicrm_country ON (civicrm_address.country_id = civicrm_country.id)
    *
    * @static
    */
-  static function relatedMemberships($contactId, &$params, $ids, $action = CRM_Core_Action::ADD, $active = TRUE) {
+  public static function relatedMemberships($contactId, &$params, $ids, $action = CRM_Core_Action::ADD, $active = TRUE) {
     // Check the end date and set the status of the relationship
     // accrodingly.
     $status = self::CURRENT;
@@ -1410,7 +1391,7 @@ SELECT count(*)
    * @static
    *
    */
-  static function isDeleteRelatedMembership($relTypeIds, $contactId, $mainRelatedContactId, $relTypeId, $relIds) {
+  public static function isDeleteRelatedMembership($relTypeIds, $contactId, $mainRelatedContactId, $relTypeId, $relIds) {
     if (in_array($relTypeId, $relTypeIds)) {
       return TRUE;
     }
@@ -1441,13 +1422,13 @@ SELECT count(*)
   }
 
   /**
-   * Function to get Current Employer for Contact
+   * Get Current Employer for Contact
    *
    * @param $contactIds       Contact Ids
    *
    * @return array $currentEmployer array of the current employer@static
    */
-  static function getCurrentEmployer($contactIds) {
+  public static function getCurrentEmployer($contactIds) {
     $contacts = implode(',', $contactIds);
 
     $query = "
@@ -1467,7 +1448,7 @@ WHERE id IN ( {$contacts} )
   }
 
   /**
-   * Function to return list of permissioned employer for a given contact.
+   * Return list of permissioned employer for a given contact.
    *
    * @param $contactID   int     contact id whose employers
    * are to be found.
@@ -1478,7 +1459,7 @@ WHERE id IN ( {$contacts} )
    * @return array array of employers.
    *
    */
-  static function getPermissionedEmployer($contactID, $name = NULL) {
+  public static function getPermissionedEmployer($contactID, $name = NULL) {
     //get the relationship id
     $relTypeId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType',
       'Employee of', 'id', 'name_a_b'
@@ -1499,7 +1480,7 @@ WHERE id IN ( {$contacts} )
   *
   * @return array of contacts
   */
-  static function getPermissionedContacts($contactID, $relTypeId, $name = NULL) {
+  public static function getPermissionedContacts($contactID, $relTypeId, $name = NULL) {
     $contacts = array();
 
     if ($relTypeId) {
@@ -1512,7 +1493,8 @@ cr.relationship_type_id IN (%2) AND
 cr.is_permission_a_b    = 1 AND
 IF(cr.end_date IS NULL, 1, (DATEDIFF( CURDATE( ), cr.end_date ) <= 0)) AND
 cr.is_active = 1 AND
-cc.id = cr.contact_id_b";
+cc.id = cr.contact_id_b AND 
+cc.is_deleted = 0";
 
       if (!empty($name)) {
         $name   = CRM_Utils_Type::escape($name, 'String');
@@ -1545,7 +1527,7 @@ AND cc.sort_name LIKE '%$name%'";
    *
    * @static
    */
-  static function mergeRelationships($mainId, $otherId, &$sqls) {
+  public static function mergeRelationships($mainId, $otherId, &$sqls) {
     // Delete circular relationships
     $sqls[] = "DELETE FROM civicrm_relationship
       WHERE (contact_id_a = $mainId AND contact_id_b = $otherId)
@@ -1579,7 +1561,7 @@ AND cc.sort_name LIKE '%$name%'";
    *
    * @return True on success, false if error is encountered.
    */
-  static function disableExpiredRelationships() {
+  public static function disableExpiredRelationships() {
     $query = "SELECT id FROM civicrm_relationship WHERE is_active = 1 AND end_date < CURDATE()";
 
     $dao = CRM_Core_DAO::executeQuery($query);
@@ -1604,7 +1586,7 @@ AND cc.sort_name LIKE '%$name%'";
    *
    * @return array
    */
-  static function membershipTypeToRelationshipTypes(&$params, $direction = NULL) {
+  public static function membershipTypeToRelationshipTypes(&$params, $direction = NULL) {
     $membershipType = civicrm_api3('membership_type', 'getsingle', array('id' => $params['membership_type_id'], 'return' => 'relationship_type_id, relationship_direction'));
     $relationshipTypes = $membershipType['relationship_type_id'];
     if(empty($relationshipTypes)) {
@@ -1709,7 +1691,7 @@ AND cc.sort_name LIKE '%$name%'";
       // format params
       foreach ($relationships as $relationshipId => $values) {
         //Add image icon for related contacts: CRM-14919
-        $icon = CRM_Contact_BAO_Contact_Utils::getImage($values['contact_type_a_b'],
+        $icon = CRM_Contact_BAO_Contact_Utils::getImage($values['contact_type'],
           FALSE,
           $values['cid']
         );