Exemplo n.º 1
0
 /**
  * Fetch an array of labels. By default, returns all the labels.
  * @method fetch
  * @param {string} [$userId=null] The id of the user whose contact labels should be fetched
  * @param {string|Db_Expression} [$filter=''] Pass a string prefix such as "Users/", or some db expression, to get only a particular subset of labels.
  * @param {boolean} [$checkContacts=false] Whether to also look in the Users_Contact table and only return labels that have at least one contact.
  * @return {array} An array of array(label => title) pairs
  */
 static function fetch($userId = null, $filter = '', $checkContacts = false)
 {
     if (!isset($userId)) {
         $user = Users::loggedInUser(true);
         $userId = $user->id;
     }
     $criteria = array('userId' => $userId);
     if ($filter) {
         $criteria['label'] = is_string($filter) ? new Db_Range($filter, true, false, null) : $filter;
     }
     if ($checkContacts) {
         $contact_array = Users_Contact::select('*')->where($criteria)->groupBy('userId, label')->fetchDbRows();
     }
     $labels = Users_Label::select('*')->where($criteria)->fetchDbRows(null, null, 'label');
     $icons = array();
     if (!$checkContacts) {
         return $labels;
     }
     $contacts = array();
     foreach ($contact_array as $contact) {
         $contacts[$contact->label] = $contact->label;
     }
     foreach ($labels as $label) {
         if (!isset($contacts[$label->label])) {
             unset($labels[$label->label]);
         }
     }
     return $labels;
 }