Exemple #1
0
 /**
  * Set a user variable.
  *
  * This can be
  * - a field in the users table
  * - or an attribute and in this case either a new style attribute or an old style user information.
  *
  * Examples:
  * self::setVar('pass', 'mysecretpassword'); // store a password (should be hashed of course)
  * self::setVar('avatar', 'mypicture.gif');  // stores an users avatar, new style
  * (internally both the new and the old style write the same attribute)
  *
  * If the user variable does not exist it will be created automatically. This means with
  * self::setVar('somename', 'somevalue');
  * you can easily create brand new users variables onthefly.
  *
  * This function does not allow you to set uid or uname.
  *
  * @param string  $name  The name of the variable.
  * @param mixed   $value The value of the variable.
  * @param integer $uid   The user to set the variable for.
  *
  * @return bool true if the set was successful, false otherwise
  */
 public static function setVar($name, $value, $uid = -1)
 {
     $dbtable = DBUtil::getTables();
     if (empty($name)) {
         return false;
     }
     if (!isset($value)) {
         return false;
     }
     if ($uid == -1) {
         $uid = SessionUtil::getVar('uid');
     }
     if (empty($uid)) {
         return false;
     }
     $isRegistration = self::isRegistration($uid);
     $origUserObj = self::getVars($uid, false, 'uid', $isRegistration);
     if (!$origUserObj) {
         // No such user record!
         return false;
     }
     $varIsSet = false;
     // Cannot setVar the user's uid or uname
     if ($name != 'uid' && $name != 'uname') {
         if (self::fieldAlias($name)) {
             // this value comes from the users table
             $obj = array('uid' => $uid, $name => $value);
             $oldValue = isset($origUserObj[$name]) ? $origUserObj[$name] : null;
             $varIsSet = (bool) DBUtil::updateObject($obj, 'users', '', 'uid');
         } else {
             // Not a table field alias, not 'uid', and not 'uname'. Treat it as an attribute.
             $dudAttributeName = self::convertOldDynamicUserDataAlias($name);
             if ($dudAttributeName) {
                 LogUtil::log(__f('Warning! User variable [%1$s] is deprecated. Please use [%2$s] instead.', array($name, $mappingarray[$name])), E_USER_DEPRECATED);
                 // $name is a former DUD /old style user information now stored as an attribute
                 $attributeName = $dudAttributeName;
             } else {
                 // $name not in the users table and also not found in the mapping array and also not one of the
                 // forbidden names, let's make an attribute out of it
                 $attributeName = $name;
             }
             $obj = array('uid' => $uid, '__ATTRIBUTES__' => array($attributeName => $value));
             $oldValue = isset($origUserObj['__ATTRIBUTES__'][$attributeName]) ? $origUserObj['__ATTRIBUTES__'][$attributeName] : null;
             $varIsSet = (bool) ObjectUtil::updateObjectAttributes($obj, 'users', 'uid', true);
         }
         // force loading of attributes from db
         $updatedUserObj = self::getVars($uid, true, '', $isRegistration);
         if (!$updatedUserObj) {
             // Should never get here!
             return false;
         }
         // Do not fire update event/hook unless the update happened, it was not a registration record, it was not
         // the password being updated, and the system is not currently being installed.
         if ($varIsSet && $name != 'pass' && !System::isInstalling()) {
             // Fire the event
             $eventName = $isRegistration ? 'user.registration.update' : 'user.account.update';
             $eventArgs = array('action' => 'setVar', 'field' => isset($attributeName) ? null : $name, 'attribute' => isset($attributeName) ? $attributeName : null);
             $eventData = array('old_value' => $oldValue, 'new_value' => $value);
             $updateEvent = new Zikula_Event($eventName, $updatedUserObj, $eventArgs, $eventData);
             EventUtil::notify($updateEvent);
         }
     }
     return $varIsSet;
 }