CRM-14747 - Profiles - Fixed error caused by profile fields with empty labels
[civicrm-core.git] / CRM / Core / BAO / UFMatch.php
index ce3284c1c865ed51d8ffcde744bf7aba99c95a68..1630ece952640de14ad3f86007c9f9274088b8a3 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.5                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2013                                |
+ | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -28,7 +28,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2013
+ * @copyright CiviCRM LLC (c) 2004-2014
  * $Id$
  *
  */
@@ -44,6 +44,11 @@ class CRM_Core_BAO_UFMatch extends CRM_Core_DAO_UFMatch {
    *
    * @param array $params input parameters
    */
+  /**
+   * @param $params
+   *
+   * @return CRM_Core_DAO_UFMatch
+   */
   static function create($params) {
     $hook = empty($params['id']) ? 'create' : 'edit';
     CRM_Utils_Hook::pre($hook, 'UFMatch', CRM_Utils_Array::value('id', $params), $params);
@@ -63,43 +68,27 @@ class CRM_Core_BAO_UFMatch extends CRM_Core_DAO_UFMatch {
    * object for this user. If the user has new values, we need
    * to update the CRM DB with the new values
    *
-   * @param Object  $user    the drupal user object
-   * @param boolean $update  has the user object been edited
-   * @param         $uf
+   * @param Object $user the drupal user object
+   * @param boolean $update has the user object been edited
+   * @param $uf
+   *
+   * @param $ctype
+   * @param bool $isLogin
    *
    * @return void
    * @access public
    * @static
    */
   static function synchronize(&$user, $update, $uf, $ctype, $isLogin = FALSE) {
-    $config = CRM_Core_Config::singleton();
+    $userSystem = CRM_Core_Config::singleton()->userSystem;
     $session = CRM_Core_Session::singleton();
     if (!is_object($session)) {
       CRM_Core_Error::fatal('wow, session is not an object?');
       return;
     }
 
-    if ($config->userSystem->is_drupal) {
-      $key   = 'uid';
-      $login = 'name';
-      $mail  = 'mail';
-    }
-    elseif ($uf == 'Joomla') {
-      $key   = 'id';
-      $login = 'username';
-      $mail  = 'email';
-      if (!isset($user->id) || !isset($user->email)) {
-        $user = JFactory::getUser();
-      }
-    }
-    elseif ($uf == 'WordPress') {
-      $key   = 'ID';
-      $login = 'user_login';
-      $mail  = 'user_email';
-    }
-    else {
-      CRM_Core_Error::statusBounce(ts('Please set the user framework variable'));
-    }
+    $userSystemID = $userSystem->getBestUFID($user);
+    $uniqId = $userSystem->getBestUFUniqueIdentifier($user);
 
     // if the id of the object is zero (true for anon users in drupal)
     // have we already processed this user, if so early
@@ -107,7 +96,7 @@ class CRM_Core_BAO_UFMatch extends CRM_Core_DAO_UFMatch {
     $userID = $session->get('userID');
     $ufID = $session->get('ufID');
 
-    if (!$update && $ufID == $user->$key) {
+    if (!$update && $ufID == $userSystemID) {
       return;
     }
 
@@ -115,7 +104,7 @@ class CRM_Core_BAO_UFMatch extends CRM_Core_DAO_UFMatch {
     $isUserLoggedIn = CRM_Utils_System::isUserLoggedIn();
 
     // reset the session if we are a different user
-    if ($ufID && $ufID != $user->$key) {
+    if ($ufID && $ufID != $userSystemID) {
       $session->reset();
 
       //get logged in user ids, and set to session.
@@ -128,15 +117,11 @@ class CRM_Core_BAO_UFMatch extends CRM_Core_DAO_UFMatch {
     }
 
     // return early
-    if ($user->$key == 0) {
+    if ($userSystemID == 0) {
       return;
     }
 
-    if (!isset($uniqId) || !$uniqId) {
-      $uniqId = $user->$mail;
-    }
-
-    $ufmatch = self::synchronizeUFMatch($user, $user->$key, $uniqId, $uf, NULL, $ctype, $isLogin);
+    $ufmatch = self::synchronizeUFMatch($user, $userSystemID, $uniqId, $uf, NULL, $ctype, $isLogin);
     if (!$ufmatch) {
       return;
     }
@@ -187,11 +172,14 @@ class CRM_Core_BAO_UFMatch extends CRM_Core_DAO_UFMatch {
    * Synchronize the object with the UF Match entry. Can be called stand-alone from
    * the drupalUsers script
    *
-   * @param Object  $user    the drupal user object
-   * @param string  $userKey the id of the user from the uf object
-   * @param string  $uniqId    the OpenID of the user
-   * @param string  $uf      the name of the user framework
-   * @param integer $status  returns the status if user created or already exits (used for CMS sync)
+   * @param Object $user the drupal user object
+   * @param string $userKey the id of the user from the uf object
+   * @param string $uniqId the OpenID of the user
+   * @param string $uf the name of the user framework
+   * @param integer $status returns the status if user created or already exits (used for CMS sync)
+   *
+   * @param null $ctype
+   * @param bool $isLogin
    *
    * @return the ufmatch object that was found or created
    * @access public
@@ -426,7 +414,9 @@ AND    domain_id    = %4
    * Update the email value for the contact and user profile
    *
    * @param  $contactId  Int     Contact ID of the user
-   * @param  $email      String  email to be modified for the user
+   * @param $emailAddress
+   *
+   * @internal param String $email email to be modified for the user
    *
    * @return void
    * @access public
@@ -543,6 +533,9 @@ AND    domain_id    = %4
     return NULL;
   }
 
+  /**
+   * @return bool
+   */
   static function isEmptyTable() {
     $sql = "SELECT count(id) FROM civicrm_uf_match";
     return CRM_Core_DAO::singleValueQuery($sql) > 0 ? FALSE : TRUE;
@@ -570,6 +563,8 @@ AND    domain_id    = %4
    * see if this user exists, and if so, if they're allowed to login
    *
    *
+   * @param $openId
+   *
    * @return bool     true if allowed to login, false otherwise
    * @access public
    * @static
@@ -606,6 +601,11 @@ AND    domain_id    = %4
     return $ufId;
   }
 
+  /**
+   * @param $email
+   *
+   * @return bool
+   */
   static function isDuplicateUser($email) {
     $session = CRM_Core_Session::singleton();
     $contactID = $session->get('userID');
@@ -622,10 +622,13 @@ AND    domain_id    = %4
   /**
    * Get uf match values for given uf id or logged in user.
    *
-   * @param int    $ufID uf id.
+   * @param int $ufID uf id.
    *
    * return array  $ufValues uf values.
-   **/
+   **
+   *
+   * @return array
+   */
   static function getUFValues($ufID = NULL) {
     if (!$ufID) {
       //get logged in user uf id.