/**
  * Retrieve the criteria for index addressbook implementation.
  *
  * Could be used in others parts but thinks that it was created
  * with that propouse. This looks for the categories and for the acl perms.
  *
  * @param integer $owner This is the actual user of phpgroupware
  *
  * @param mixed $access This is limit the search for private only
  * (with PHPGW_CONTACTS_PRIVATE), all my contacts(PHPGW_CONTACTS_MINE)
  * or all the records I have access (PHPGW_CONTACTS_ALL).
  *
  * @param integer $categories if have any value then limit the result
  * to contacts that belongs to this categories (and childs)
  * PHPGW_CONTACTS_CATEGORIES_ALL if want not use categories criterias.
  *
  * @param array $search_fields This is used in search, is the<br />
  * list of fields to search. Also this list is used to know if we<br />
  * need to set the criteria for search the prefered addresses for contacts.
  *
  * @param string $pattern Is the string that will be used to search in
  * all fields of $search_fields, set value to this param without
  * setting $search_field is useless.
  *
  * @param array $show_fields Database fields to show
  * @return string SQL string
  */
 function criteria_for_index($owner, $access = PHPGW_CONTACTS_ALL, $categories = PHPGW_CONTACTS_CATEGORIES_ALL, $search_fields = array(), $pattern = '', $show_fields = array())
 {
     if (!is_numeric($owner) || intval($owner) == 0) {
         return;
     }
     switch ($access) {
         case PHPGW_CONTACTS_MINE:
             $criteria = phpgwapi_sql_criteria::_equal('owner', $owner);
             break;
         case PHPGW_CONTACTS_PRIVATE:
             $criteria = phpgwapi_sql_criteria::token_and(phpgwapi_sql_criteria::_equal('access', 'private'), phpgwapi_sql_criteria::_equal('owner', $owner));
             break;
         case PHPGW_CONTACTS_ALL:
         default:
             $criteria = phpgwapi_sql_criteria::token_or(phpgwapi_sql_criteria::token_and(phpgwapi_sql_criteria::_equal('access', 'public'), phpgwapi_sql_criteria::_in('owner', $this->get_contacts_shared($owner, PHPGW_ACL_READ))), phpgwapi_sql_criteria::_equal('owner', $owner));
     }
     if ($categories != PHPGW_CONTACTS_CATEGORIES_ALL) {
         if (!is_array($categories)) {
             $categories = array($categories);
         }
         $categories_array = array_merge($categories, (array) $this->get_sub_cats($categories));
         if (count($categories_array) > 0) {
             foreach ($categories_array as $cat_id) {
                 $search_categories[] = phpgwapi_sql_criteria::token_or(phpgwapi_sql_criteria::_equal('sel_cat_id', $cat_id), phpgwapi_sql_criteria::token_has('sel_cat_id', ',' . $cat_id . ','));
             }
             $categories_criteria = phpgwapi_sql_criteria::_append_or($search_categories);
             $criteria = phpgwapi_sql_criteria::token_and($criteria, $categories_criteria);
         }
     }
     $location = createObject('phpgwapi.contact_addr');
     $search_fields = empty($search_fields) || !is_array($search_fields) ? array() : $search_fields;
     $show_fields = empty($show_fields) || !is_array($show_fields) ? array() : $show_fields;
     $search_count = count(array_intersect($location->get_false_fields(), $search_fields));
     $show_count = count(array_intersect($location->get_false_fields(), $show_fields));
     if ($search_count <= 0 && $show_count > 0) {
         $addr_preferred_criteria = phpgwapi_sql_criteria::token_or(phpgwapi_sql_criteria::_equal('addr_pref_val', 'Y'), phpgwapi_sql_criteria::_is_null('key_addr_id'));
         $criteria = phpgwapi_sql_criteria::token_and($criteria, $addr_preferred_criteria);
     }
     if (isset($search_fields['comm_media']) && is_array($search_fields['comm_media']) && count($search_fields['comm_media']) > 0) {
         $search_fields_comms = $search_fields['comm_media'];
     }
     unset($search_fields['comm_media']);
     $index = array_search('per_full_name', $search_fields);
     if ($index !== False && $index !== Null) {
         unset($search_fields[$index]);
         $search_fields[] = 'per_first_name';
         $search_fields[] = 'per_last_name';
         $search_fields[] = 'per_middle_name';
     }
     if (count($search_fields) > 0 && $pattern) {
         foreach ($search_fields as $field) {
             $search_array[] = phpgwapi_sql_criteria::token_has($field, $pattern);
         }
         $criteria = phpgwapi_sql_criteria::token_and($criteria, phpgwapi_sql_criteria::_append_or($search_array));
     }
     if ($pattern && isset($search_fields_comms) && is_array($search_fields_comms) && count($search_fields_comms) > 0) {
         foreach ($search_fields_comms as $field) {
             $search_array_comm[] = phpgwapi_sql_criteria::token_and(phpgwapi_sql_criteria::token_has('comm_data', $pattern), phpgwapi_sql_criteria::_equal('comm_descr', $this->search_comm_descr($field)));
         }
         $criteria = phpgwapi_sql_criteria::token_and($criteria, phpgwapi_sql_criteria::_append_or($search_array_comm));
     }
     return $criteria;
 }
 function get_persons_by_list($list)
 {
     if (intval($list)) {
         $criteria = $this->contactsobject->criteria_for_index($GLOBALS['phpgw_info']['user']['account_id'], PHPGW_CONTACTS_ALL, $list);
         $new = phpgwapi_sql_criteria::token_and($criteria, phpgwapi_sql_criteria::_equal('comm_descr', $this->contactsobject->search_comm_descr('work email')));
         $persons = $this->contactsobject->get_persons(array('per_full_name', 'comm_data'), '', '', '', '', '', $new);
         if (!is_array($persons)) {
             $persons = array();
         }
         foreach ($persons as $data) {
             $persons_list[] = array('name' => $data['per_full_name'], 'email' => $data['comm_data']);
         }
     }
     return $persons_list;
 }