function criteria_my_preferred($element)
 {
     $field = $this->put_alias($element['real_field']);
     $criteria = phpgwapi_sql_criteria::or_(phpgwapi_sql_criteria::equal($field, phpgwapi_sql::string($element['value'])), phpgwapi_sql_criteria::is_null($field));
     $this->_add_criteria($criteria);
     return $criteria;
 }
 function criteria_org_id($element)
 {
     $field = $this->put_alias($element['real_field']);
     if (is_array($element['value'])) {
         $this->_add_criteria(phpgwapi_sql_criteria::in($field, $element['value']));
     } else {
         $this->_add_criteria(phpgwapi_sql_criteria::equal($field, $element['value']));
     }
 }
 function criteria_comm_find_descr($element)
 {
     $field = $this->put_alias($element['real_field']);
     foreach ($element['value'] as $value) {
         $data[] = phpgwapi_sql_criteria::equal($field, phpgwapi_sql::string($value));
     }
     $criteria = phpgwapi_sql_criteria::append_or($data);
     $this->_add_criteria($criteria);
 }
 function criteria_person_only($element)
 {
     $this->_add_criteria($this->put_alias(phpgwapi_sql_criteria::not_null($this->real_field('account_person_id'))));
 }
 function searchAddress($_searchString)
 {
     if (method_exists($GLOBALS['phpgw']->contacts, 'search')) {
         // 1.3+
         $contacts = $GLOBALS['phpgw']->contacts->search(array('n_fn' => $_searchString, 'email' => $_searchString, 'email_home' => $_searchString), array('n_fn', 'email', 'email_home'), 'n_fn', '', '%', false, 'OR', array(0, 20));
         // additionally search the accounts, if the contact storage is not the account storage
         if ($GLOBALS['phpgw_info']['server']['account_repository'] == 'ldap' && $GLOBALS['phpgw_info']['server']['contact_repository'] == 'sql') {
             $accounts = $GLOBALS['phpgw']->contacts->search(array('n_fn' => $_searchString, 'email' => $_searchString, 'email_home' => $_searchString), array('n_fn', 'email', 'email_home'), 'n_fn', '', '%', false, 'OR', array(0, 20), array('owner' => 0));
             if ($contacts && $accounts) {
                 $contacts = array_merge($contacts, $accounts);
                 usort($contacts, create_function('$a,$b', 'return strcasecmp($a["n_fn"],$b["n_fn"]);'));
             } elseif ($accounts) {
                 $contacts =& $accounts;
             }
             unset($accounts);
         }
     } else {
         // < 1.3
         $d = CreateObject('phpgwapi.contacts');
         $fields = array('per_first_name', 'per_last_name', 'email', 'email_home');
         $criteria_search[] = phpgwapi_sql_criteria::token_begin('per_first_name', $_searchString);
         $criteria_search[] = phpgwapi_sql_criteria::token_begin('per_last_name', $_searchString);
         $criteria_search[] = phpgwapi_sql_criteria::token_has('email', $_searchString);
         $criteria[] = phpgwapi_sql_criteria::_append_or($criteria_search);
         $criteria[] = $d->criteria_for_index((int) $GLOBALS['phpgw_info']['user']['account_id']);
         $criteria_token = phpgwapi_sql_criteria::_append_and($criteria);
         //FIXME	: the sql_builder/sql_criteria has issues.
         $contacts = $d->get_persons($fields, 0, 0, 'per_last_name', 'ASC', '', $criteria_token);
     }
     $response =& new xajaxResponse();
     if (is_array($contacts)) {
         $innerHTML = '';
         $jsArray = array();
         $i = 0;
         foreach ($contacts as $contact) {
             foreach (array($contact['email'], $contact['email_home']) as $email) {
                 // avoid wrong addresses, if an rfc822 encoded address is in addressbook
                 $email = preg_replace("/(^.*<)([a-zA-Z0-9_\\-]+@[a-zA-Z0-9_\\-\\.]+)(.*)/", '$2', $email);
                 if (!empty($email) && !isset($jsArray[$email])) {
                     $i++;
                     $str = $this->translation->convert(trim($contact['n_fn'] ? $contact['n_fn'] : $contact['fn']) . ' <' . trim($email) . '>', $this->charset, 'utf-8');
                     #$innerHTML .= '<div class="inactiveResultRow" onclick="selectSuggestion('. $i .')">'.
                     $innerHTML .= '<div class="inactiveResultRow" onmousedown="keypressed(13,1)" onmouseover="selectSuggestion(' . ($i - 1) . ')">' . htmlentities($str, ENT_QUOTES, 'utf-8') . '</div>';
                     $jsArray[$email] = addslashes(trim($contact['n_fn'] ? $contact['n_fn'] : $contact['fn']) . ' <' . trim($email) . '>');
                 }
                 if ($i > 10) {
                     break;
                 }
                 // we check for # of results here, as we might have empty email addresses
             }
         }
         if ($jsArray) {
             $response->addAssign('resultBox', 'innerHTML', $innerHTML);
             $response->addScript('results = new Array("' . implode('","', $jsArray) . '");');
             $response->addScript('displayResultBox();');
         }
         //$response->addScript("getResults();");
         //$response->addScript("selectSuggestion(-1);");
     } else {
         $response->addAssign('resultBox', 'className', 'resultBoxHidden');
     }
     return $response->getXML();
 }
 /**
  * Generate a powerfull criteria based.
  *
  * Recieve $data that is an array (operand_left, operand_right, operator)
  * If operant_left is array I call... myself :), else try to get the field name.
  * If right operand is array can myself to, else, nothing.
  * Third operator IS_ a operator name that send to sql::operate();
  * @param array $data Genertaded by calls of sql class.
  * @param string $operation Is one of: insert, select, delete, update, if is diferent to select, then the result will not include the alias for table.
  * @return Criteria for any sql query.
  */
 function builder_criteria($token_criteria, $operation = 'insert')
 {
     $num_elements = count($token_criteria);
     $this->_criteria_built = True;
     switch ($num_elements) {
         case 0:
         case 1:
             $local_criteria = $token_criteria;
             break;
         case 2:
         case 3:
             $operator = array_pop($token_criteria);
             $left = array_shift($token_criteria);
             $right = array_shift($token_criteria);
             if (is_array($left) && $operator != 'in') {
                 $left = $this->builder_criteria($left, $operation);
             } else {
                 $entity = $this->get_entity($left);
                 $this->entities[$entity]->set_operation($operation);
                 $field = $left;
                 $left = $this->entities[$entity]->put_real_alias($this->real_field($entity, $left));
             }
             if (is_array($right)) {
                 if ($operator != 'in') {
                     $right = $this->builder_criteria($right, $operation);
                 }
             } else {
                 if (isset($field) && $field && !($operator == 'has' || $operator == 'begin_with' || $operator == 'end_with')) {
                     $right = $this->entities[$entity]->cast($right, $field);
                 }
             }
             if ($operator == 'append_and' || $operator == 'append_or') {
                 $param = array($left, $right);
                 $local_criteria = phpgwapi_sql_criteria::operate($operator, $param);
             } else {
                 $local_criteria = phpgwapi_sql_criteria::operate($operator, $left, $right);
             }
             break;
         default:
             $operator = array_pop($token_criteria);
             foreach ($token_criteria as $criteria) {
                 $criterias[] = $this->builder_criteria($criteria, $operation);
             }
             $local_criteria = phpgwapi_sql_criteria::operate($operator, $criterias);
     }
     return $local_criteria;
 }
 function search($search_fields, $pattern, $data = 'contact_id')
 {
     $criteria = array();
     if (count($search_fields) > 0 && $pattern) {
         $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';
         }
         foreach ($search_fields as $field) {
             $search_array[] = phpgwapi_sql_criteria::token_has($field, $pattern);
         }
         $criteria = phpgwapi_sql_criteria::_append_or($search_array);
     }
     $this->request('contact_id');
     $this->criteria_token($criteria);
     $records = $this->get_records(__LINE__, __FILE__);
     if (is_array($records)) {
         foreach ($records as $value) {
             $info[] = $value['contact_id'];
         }
     }
     return $info;
 }
 /**
  * Generate a critieria to search in the end of a field for sql.
  *
  * @param string $field For search in.
  * @param string $value That will search.
  *
  * @return string that use LIKE to search in field.
  */
 public static function end_with($field, $value)
 {
     return phpgwapi_sql_criteria::upper($field) . ' LIKE ' . "'%{$value}'";
 }
 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;
 }
 /**
  * Create an update query for this entity
  *
  * @param Array $data Fields that want change value and their values
  * @param Array $criteria With criterias that set the rows to edit
  * @param integer action
  * @return string SQL update string
  */
 function update($data, $criteria, $action = PHPGW_SQL_RETURN_SQL)
 {
     if (is_array($data) && count($data) > 0) {
         array_walk($data, array(&$this, 'add_update'));
     } else {
         list($field, $value) = explode('=', $data);
         $this->add_update($field, $value);
     }
     if (is_string($criteria)) {
         $this->set_criteria($criteria);
     } else {
         $this->set_criteria(phpgwapi_sql_criteria::criteria($criteria));
     }
     if (!empty($this->values)) {
         switch ($action) {
             case PHPGW_SQL_RETURN_RECORDS:
             case PHPGW_SQL_RUN_SQL:
                 $sql = $this->return_update();
                 $GLOBALS['phpgw']->db->query($sql, __LINE__, __FILE__);
                 $this->ldebug('update', $sql, 'msg');
                 return;
             case PHPGW_SQL_RETURN_SQL:
                 return $this->return_update();
         }
     }
 }
 function criteria_sel_cat_id($element)
 {
     $field = $this->put_alias($element['real_field']);
     if (is_array($element['value'])) {
         foreach ($element['value'] as $value) {
             $data[] = phpgwapi_sql_criteria::or_(phpgwapi_sql_criteria::equal($field, sql::string($value)), phpgwapi_sql_criteria::has($field, ',' . $value . ','));
         }
         $criteria = phpgwapi_sql_criteria::append_or($data);
         $this->_add_criteria($criteria);
     } else {
         $this->_add_criteria(phpgwapi_sql_criteria::equal($field, phpgwapi_sql::string($element['value'])));
     }
 }