Example #1
0
 /**
  * Add contact with one or more labels
  * @method addContact
  * @static
  * @param {string} $userId
  *  The id of the user whose contact will be added
  * @param {string} $contactUserId
  *  The id of the user who is the contact
  * @param {string|array} $label
  *  The label of the contact. This can be a string or an array of strings, in which case
  *  multiple contact rows are saved.
  * @param {string} [$nickname='']
  *  Optional nickname to assign to the contact
  *  @optional
  * @throws {Q_Exception_RequiredField}
  *	if $label is missing
  * @return {array} Array of contacts that are saved
  */
 static function addContact($userId, $label, $contactUserId, $nickname = '')
 {
     foreach (array('userId', 'label', 'contactUserId') as $field) {
         if (empty(${$field})) {
             throw new Q_Exception_RequiredField(compact('field'));
         }
     }
     $labels = is_array($label) ? $label : array($label);
     $contacts = array();
     foreach ($labels as $l) {
         // Insert the contacts one by one
         $contact = new Users_Contact();
         $contact->userId = $userId;
         $contact->contactUserId = $contactUserId;
         $contact->label = $l;
         if ($nickname) {
             $contact->nickname = $nickname;
         }
         $contact->save(true);
         $contacts[] = $contact;
     }
     /**
      * @event Users/Contact/addContact {after}
      * @param {string} contactUserId
      * @param {string} label
      * @param {array} contacts
      */
     Q::event('Users/Contact/addContact', compact('contactUserId', 'label', 'contacts'), 'after');
     return $contacts;
 }
Example #2
0
/**
 * Edits a contact in the system. Fills the "contact" slot.
 * @param {array} $_REQUEST
 * @param {string} $_REQUEST.label The label of the contact
 * @param {string} $_REQUEST.contactUserId The contactUserId of the contact
 * @param {string} [$_REQUEST.nickname] The nickname of the contact
 * @param {string} [$_REQUEST.userId=Users::loggedInUser(true)->id] You can override the user id, if another plugin adds a hook that allows you to do this
 */
function Users_contact_put($params = array())
{
    $req = array_merge($_REQUEST, $params);
    Q_Request::requireFields(array('label', 'contactUserId'), $req, true);
    $loggedInUserId = Users::loggedInUser(true)->id;
    $userId = Q::ifset($req, 'userId', $loggedInUserId);
    $label = $req['label'];
    $contactUserId = $req['contactUserId'];
    $nickname = Q::ifset($req, 'nickname', null);
    Users::canManageContacts($loggedInUserId, $userId, $label, true);
    $contact = new Users_Contact();
    $contact->userId = $userId;
    $contact->label = $label;
    $contact->contactUserId = $contactUserId;
    if (!$contact->retrieve()) {
        throw new Q_Exception_MissingRow(array('table' => 'Users_Contact', 'criteria' => json_encode($contact->fields)));
    }
    if ($nickname) {
        $contact->nickname = $nickname;
    }
    $contact->save();
    Q_Response::setSlot('contact', $contact->exportArray());
}
Example #3
0
 /**
  * Inserts some Users_Contact rows for the locally registered users
  * who have added links to this particular contact information.
  * Removes the links after successfully adding the Users_Contact rows.
  * @method saveContactsFromLinks
  * @static
  * @param {array} $contact_info An array of key => value pairs, where keys can be:
  *
  * * "email" => the user's email address
  * * "mobile" => the user's mobile number
  * * "email_hashed" => the standard hash of the user's email address
  * * "mobile_hashed" => the standard hash of the user's mobile number
  * * "facebook" => the user's facebook uid
  * * "twitter" => the user's twitter uid
  *
  * @param {string} $userId The id of the user who has verified these identifiers
  */
 static function saveContactsFromLinks()
 {
     /**
      * @event Users/saveContactsFromLinks {before}
      */
     Q::event('Users/saveContactsFromLinks', array(), 'before');
     $user = self::loggedInUser();
     $contact_info = array();
     foreach (self::$types as $type => $field) {
         if (!empty($user->{$field})) {
             $contact_info[$type] = $user->{$field};
         }
     }
     $links = $contact_info ? Users::links($contact_info) : array();
     $contacts = array();
     foreach ($links as $link) {
         $extraInfo = json_decode($link->extraInfo, true);
         $firstName = Q::ifset($extraInfo, 'firstName', '');
         $lastName = Q::ifset($extraInfo, 'lastName', '');
         $fullName = $firstName ? $lastName ? "{$firstName} {$lastName}" : $firstName : ($lastName ? $lastName : "");
         if (!empty($extraInfo['labels']) and is_array($extraInfo['labels'])) {
             foreach ($extraInfo['labels'] as $label) {
                 // Insert the contacts one by one, so if an error occurs
                 // we can continue right on inserting the rest.
                 $contact = new Users_Contact();
                 $contact->userId = $link->userId;
                 $contact->contactUserId = $user->id;
                 $contact->label = $label;
                 $contact->nickname = $fullName;
                 $contact->save(true);
                 $link->remove();
                 // we don't need this link anymore
                 // TODO: Think about porting this to Node
                 // and setting a flag when done.
                 // Perhaps we should send a custom message through socket.io
                 // which would cause Users.js to add a notice to the interface
             }
         }
     }
     /**
      * @event Users/saveContactsFromLinks {after}
      * @param {array} contacts
      */
     Q::event('Users/saveContactsFromLinks', compact('contacts'), 'after');
     // TODO: Add a handler to this event in the Streams plugin, so that
     // we post this information to a stream on the hub, which will
     // update all its subscribers, who will also run saveContactsFromLinks
     // for their local users.
 }
Example #4
0
 /**
  * Update a particular contact with a given userId, label, contactId.
  * @method updateContact
  * @static
  * @param {string} $userId
  * @param {string} $label
  * @param {string} $contactUserId
  * @param {array} $updates should be an array with only one key: "nickname"
  * @param {string} [$asUserId=null] The user to do this operation as.
  *   Defaults to the logged-in user. Pass false to skip access checks.
  * @throws {Users_Exception_NotAuthorized}
  * @return {Db_Query_Mysql}
  */
 static function updateContact($userId, $label, $contactUserId, $updates, $asUserId = null)
 {
     foreach (array('userId', 'label', 'contactUserId', 'updates') as $field) {
         if (empty(${$field})) {
             throw new Q_Exception_RequiredField(compact($field));
         }
     }
     Users::canManageContacts($asUserId, $userId, $label, true);
     $contact = new Users_Contact();
     $contact->userId = $userId;
     $contact->label = $label;
     $contact->contactUserId = $contactUserId;
     if (!$contact->retrieve()) {
         throw new Q_Exception_MissingRow(array('table' => 'Users_Contact', 'criteria' => Q::json_encode($contact->fields)));
     }
     if (isset($updates['nickname'])) {
         $contact->nickname = $updates['nickname'];
     }
     $contact->save();
     return $contact;
 }