Example #1
0
 /**
  * Starts the process of adding a mobile to a saved user object.
  * Also modifies and saves this user object back to the database.
  * @param string $mobile_number
  *  The mobile number to add.
  * @param string $activation_mobile_view
  *  The view to use for the body of the activation mobile to send.
  * @param boolean $html
  *  Defaults to true. Whether to send as HTML mobile.
  * @param array $fields
  *  An array of additional fields to pass to the mobile view.
  * @return boolean
  *  Returns true on success.
  *  Returns false if this mobile number is already verified for this user.
  * @throws Pie_Exception_WrongType
  *  If the mobile number is in an invalid format, this is thrown.
  * @throws Users_Exception_AlreadyVerified
  *  If the mobile number already exists and has been verified for
  *  another user, then this exception is thrown.
  */
 function addMobile($mobile_number, $activation_mobile_subject = null, $activation_mobile_view = null, $html = true, $fields = array())
 {
     // TODO: Implement Users_Mobile::sendMessage
     if (!Pie_Valid::mobile($mobile_number)) {
         throw new Pie_Exception_WrongValue(array('field' => 'Mobile phone', 'range' => 'a valid number'), 'mobile_number');
     }
     Pie::event('users/validate/mobile_number', array('mobile_number' => &$mobile_number));
     $m = new Users_Mobile();
     $m->number = $mobile_number;
     if ($m->retrieve() and $m->state !== 'unverified') {
         if ($m->user_id === $this->id) {
             return false;
         }
         // Otherwise, say it's verified for another user,
         // even if it unsubscribed or was suspended.
         throw new Users_Exception_AlreadyVerified(array('key' => $m->number, 'user_id' => $m->user_id), 'mobile_number');
     }
     // If we are here, then the mobile record either
     // doesn't exist, or hasn't been verified yet.
     // In either event, update the record in the database,
     // and re-send the mobile.
     $minutes = Pie_Config::get('users', 'activationCodeExpires', 60 * 24 * 7);
     $m->state = 'unverified';
     $m->user_id = $this->id;
     $m->activation_code = Pie_Utils::unique(5);
     $m->activation_code_expires = new Db_Expression("CURRENT_TIMESTAMP + INTERVAL {$minutes} MINUTE");
     $m->auth_code = md5(microtime() + mt_rand());
     $m->save();
     if (!isset($activation_message_view)) {
         $activation_message_view = Pie_Config::get('users', 'activationMessageView', 'users/message/activation.php');
     }
     $fields2 = array_merge($fields, array('user' => $this, 'message' => $m));
     $m->sendMessage($activation_mobile_view, $fields2, array('html' => $html));
     Pie::event('users/addMobile', compact('mobile_number'), 'after');
 }