function read($data)
 {
     //_debug_array($data);die();
     $start = isset($data['start']) && $data['start'] ? (int) $data['start'] : 0;
     $filter = isset($data['filter']) && $data['filter'] ? $data['filter'] : 'none';
     $query = isset($data['query']) ? $data['query'] : '';
     $sort = isset($data['sort']) && $data['sort'] ? $data['sort'] : 'DESC';
     $order = isset($data['order']) ? $data['order'] : '';
     $cat_id = isset($data['cat_id']) ? (int) $data['cat_id'] : '';
     $vendor_id = isset($data['vendor_id']) ? (int) $data['vendor_id'] : '';
     $allrows = isset($data['allrows']) ? $data['allrows'] : '';
     $member_id = isset($data['member_id']) ? (int) $data['member_id'] : 0;
     $agreement_id = isset($data['agreement_id']) ? (int) $data['agreement_id'] : '';
     $status_id = isset($data['status_id']) ? $data['status_id'] : '';
     $filtermethod = '';
     $querymethod = '';
     $choice_table = 'phpgw_cust_choice';
     $attribute_table = 'phpgw_cust_attribute';
     $entity_table = 'fm_agreement';
     $category_table = 'fm_branch';
     $location_id = $GLOBALS['phpgw']->locations->get_id('property', '.agreement');
     $attribute_filter = " location_id = {$location_id}";
     $paranthesis = '(';
     $joinmethod = " {$this->join} {$category_table} ON ( {$entity_table}.category = {$category_table}.id)";
     $joinmethod .= " {$this->join}  fm_vendor ON ( {$entity_table}.vendor_id =fm_vendor.id )";
     $joinmethod .= " {$this->join} fm_agreement_status ON ( {$entity_table}.status = fm_agreement_status.id))";
     $cols = "{$entity_table}.*,{$category_table}.descr as category, org_name, fm_agreement_status.descr as status";
     $cols_return[] = 'id';
     $uicols['input_type'][] = 'text';
     $uicols['name'][] = 'id';
     $uicols['descr'][] = lang('ID');
     $uicols['statustext'][] = lang('ID');
     $uicols['datatype'][] = 'I';
     $cols_return[] = 'name';
     $uicols['input_type'][] = 'text';
     $uicols['name'][] = 'name';
     $uicols['descr'][] = lang('name');
     $uicols['statustext'][] = lang('name');
     $uicols['datatype'][] = 'V';
     $cols_return[] = 'org_name';
     $uicols['input_type'][] = 'text';
     $uicols['name'][] = 'org_name';
     $uicols['descr'][] = lang('Vendor');
     $uicols['statustext'][] = lang('Vendor');
     $uicols['datatype'][] = 'V';
     $cols_return[] = 'category';
     $uicols['input_type'][] = 'text';
     $uicols['name'][] = 'category';
     $uicols['descr'][] = lang('category');
     $uicols['statustext'][] = lang('category');
     $uicols['datatype'][] = 'V';
     $cols_return[] = 'start_date';
     $uicols['input_type'][] = 'text';
     $uicols['name'][] = 'start_date';
     $uicols['descr'][] = lang('start');
     $uicols['statustext'][] = lang('start date');
     $uicols['datatype'][] = 'D';
     $cols_return[] = 'termination_date';
     $uicols['input_type'][] = 'text';
     $uicols['name'][] = 'termination_date';
     $uicols['descr'][] = lang('termination date');
     $uicols['statustext'][] = lang('termination date');
     $uicols['datatype'][] = 'D';
     $cols_return[] = 'end_date';
     $uicols['input_type'][] = 'text';
     $uicols['name'][] = 'end_date';
     $uicols['descr'][] = lang('end');
     $uicols['statustext'][] = lang('end date');
     $uicols['datatype'][] = 'D';
     $cols_return[] = 'status';
     $uicols['input_type'][] = 'text';
     $uicols['name'][] = 'status';
     $uicols['descr'][] = lang('status');
     $uicols['statustext'][] = lang('status');
     $uicols['datatype'][] = 'V';
     if ($order) {
         switch ($order) {
             case 'id':
             case 'status':
                 $ordermethod = " ORDER BY {$entity_table}.{$order} {$sort}";
                 break;
             case 'category':
                 $ordermethod = " ORDER BY {$category_table}.descr {$sort}";
                 break;
             default:
                 $ordermethod = " ORDER BY {$order} {$sort}";
         }
     } else {
         $ordermethod = " ORDER BY {$entity_table}.id DESC";
     }
     //_debug_array($ordermethod);
     $sql = "SELECT {$cols} FROM {$paranthesis} {$entity_table} {$joinmethod}";
     $i = count($uicols['name']);
     $user_columns = isset($GLOBALS['phpgw_info']['user']['preferences']['property']['agreement_columns' . !!$agreement_id]) ? $GLOBALS['phpgw_info']['user']['preferences']['property']['agreement_columns' . !!$agreement_id] : '';
     $user_column_filter = '';
     if (isset($user_columns) and is_array($user_columns) and $user_columns[0]) {
         $user_column_filter = " OR ({$attribute_filter} AND id IN (" . implode(',', $user_columns) . '))';
     }
     $this->db->query("SELECT * FROM {$attribute_table} WHERE list=1 AND {$attribute_filter} {$user_column_filter} ");
     while ($this->db->next_record()) {
         $uicols['input_type'][] = 'text';
         $uicols['name'][] = $this->db->f('column_name');
         $uicols['descr'][] = $this->db->f('input_text');
         $uicols['statustext'][] = $this->db->f('statustext');
         $uicols['datatype'][$i] = $this->db->f('datatype');
         $cols_return_extra[] = array('name' => $this->db->f('column_name'), 'datatype' => $this->db->f('datatype'), 'attrib_id' => $this->db->f('id'));
         $i++;
     }
     $this->uicols = $uicols;
     $where = 'WHERE';
     /*			if ($filter=='all')
     			{
     				if (is_array($grants))
     				{
     					while (list($user) = each($grants))
     					{
     						$public_user_list[] = $user;
     					}
     					reset($public_user_list);
     					$filtermethod .= " $where ( $entity_table.user_id IN(" . implode(',',$public_user_list) . "))";
     
     					$where= 'AND';
     				}
     
     			}
     			else
     			{
     				$filtermethod = " $where $entity_table.user_id=$filter ";
     				$where= 'AND';
     			}
      */
     if ($agreement_id) {
         $filtermethod .= " {$where} {$entity_table}.agreement_id={$agreement_id}";
         $filtermethod .= " AND current_index = 1";
         $where = 'AND';
     }
     if ($cat_id) {
         $filtermethod .= " {$where} {$entity_table}.category='{$cat_id}' ";
         $where = 'AND';
     }
     if ($vendor_id) {
         $filtermethod .= " {$where} {$entity_table}.vendor_id='{$vendor_id}' ";
         $where = 'AND';
     }
     if ($member_id > 0) {
         $filtermethod .= " {$where} fm_vendor.member_of {$this->like} '%,{$member_id},%' ";
         $where = 'AND';
     }
     if ($status_id) {
         $filtermethod .= " {$where} {$entity_table}.status='{$status_id}' ";
         $where = 'AND';
     }
     if ($query) {
         $query = $this->db->db_addslashes($query);
         $querymethod[] = "fm_branch.descr {$this->like} '%{$query}%'";
         $querymethod[] = "{$entity_table}.name {$this->like} '%{$query}%'";
         $this->db->query("SELECT * FROM {$attribute_table} WHERE search='1' AND {$attribute_filter} ");
         while ($this->db->next_record()) {
             if ($this->db->f('datatype') == 'V' || $this->db->f('datatype') == 'email' || $this->db->f('datatype') == 'CH') {
                 $querymethod[] = "{$entity_table}." . $this->db->f('column_name') . " {$this->like} '%{$query}%'";
             } else {
                 $querymethod[] = "{$entity_table}." . $this->db->f('column_name') . " = '{$query}'";
             }
         }
         if (isset($querymethod) and is_array($querymethod)) {
             $querymethod = " {$where} (" . implode(' OR ', $querymethod) . ')';
             $where = 'AND';
         }
     }
     $sql .= " {$filtermethod} {$querymethod}";
     $this->db2->query($sql, __LINE__, __FILE__);
     $this->total_records = $this->db2->num_rows();
     if (!$allrows) {
         $this->db->limit_query($sql . $ordermethod, $start, __LINE__, __FILE__);
     } else {
         $this->db->query($sql . $ordermethod, __LINE__, __FILE__);
     }
     $j = 0;
     $n = count($cols_return);
     //_debug_array($cols_return);
     $contacts = CreateObject('phpgwapi.contacts');
     $agreement_list = array();
     while ($this->db->next_record()) {
         for ($i = 0; $i < $n; $i++) {
             $agreement_list[$j][$cols_return[$i]] = $this->db->f($cols_return[$i]);
             $agreement_list[$j]['grants'] = (int) isset($grants[$this->db->f('user_id')]) ? $grants[$this->db->f('user_id')] : '';
         }
         if (isset($cols_return_extra) && is_array($cols_return_extra)) {
             for ($i = 0; $i < count($cols_return_extra); $i++) {
                 $value = '';
                 $value = $this->db->f($cols_return_extra[$i]['name']);
                 if (($cols_return_extra[$i]['datatype'] == 'R' || $cols_return_extra[$i]['datatype'] == 'LB') && $value) {
                     $sql = "SELECT value FROM {$choice_table} WHERE {$attribute_filter} AND attrib_id=" . $cols_return_extra[$i]['attrib_id'] . "  AND id=" . $value;
                     $this->db2->query($sql);
                     $this->db2->next_record();
                     $agreement_list[$j][$cols_return_extra[$i]['name']] = $this->db2->f('value');
                 } else {
                     if ($cols_return_extra[$i]['datatype'] == 'AB' && $value) {
                         $contact_data = $contacts->read_single_entry($value, array('n_given' => 'n_given', 'n_family' => 'n_family', 'email' => 'email'));
                         $agreement_list[$j][$cols_return_extra[$i]['name']] = $contact_data[0]['n_family'] . ', ' . $contact_data[0]['n_given'];
                     } else {
                         if ($cols_return_extra[$i]['datatype'] == 'VENDOR' && $value) {
                             $sql = "SELECT org_name FROM fm_vendor where id={$value}";
                             $this->db2->query($sql);
                             $this->db2->next_record();
                             $agreement_list[$j][$cols_return_extra[$i]['name']] = $this->db2->f('org_name');
                         } else {
                             if ($cols_return_extra[$i]['datatype'] == 'CH' && $value) {
                                 //							$ch= unserialize($value);
                                 $ch = explode(',', trim($data['value'], ','));
                                 if (isset($ch) and is_array($ch)) {
                                     for ($k = 0; $k < count($ch); $k++) {
                                         $sql = "SELECT value FROM {$choice_table} WHERE {$attribute_filter} AND attrib_id=" . $cols_return_extra[$i]['attrib_id'] . "  AND id=" . $ch[$k];
                                         $this->db2->query($sql);
                                         while ($this->db2->next_record()) {
                                             $ch_value[] = $this->db2->f('value');
                                         }
                                     }
                                     $agreement_list[$j][$cols_return_extra[$i]['name']] = @implode(",", $ch_value);
                                     unset($ch_value);
                                 }
                             } else {
                                 if ($cols_return_extra[$i]['datatype'] == 'D' && $value) {
                                     $agreement_list[$j][$cols_return_extra[$i]['name']] = date($GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'], strtotime($value));
                                 } else {
                                     if ($cols_return_extra[$i]['datatype'] == 'timestamp' && $value) {
                                         $agreement_list[$j][$cols_return_extra[$i]['name']] = date($GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'], $value);
                                     } else {
                                         if ($cols_return_extra[$i]['datatype'] == 'link' && $value) {
                                             $agreement_list[$j][$cols_return_extra[$i]['name']] = phpgw::safe_redirect($value);
                                         } else {
                                             $agreement_list[$j][$cols_return_extra[$i]['name']] = $value;
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
         $j++;
     }
     //_debug_array($agreement_list);
     return $agreement_list;
 }
 public function get_translated_value($data, $location_id)
 {
     static $cache_lb = array();
     static $cache_contact = array();
     static $cache_vendor = array();
     static $cache_ch = array();
     static $cache_account = array();
     static $cache_custom_lookup = array();
     if (!$data['value']) {
         return $data['value'];
     }
     $ret = '';
     $choice_table = 'phpgw_cust_choice';
     $attribute_table = 'phpgw_cust_attribute';
     $attribute_filter = " location_id = {$location_id}";
     switch ($data['datatype']) {
         case 'R':
         case 'LB':
             if ($data['attrib_id']) {
                 if (!isset($cache_lb[$location_id][$data['attrib_id']][$data['value']]) || $ret != $cache_lb[$location_id][$data['attrib_id']][$data['value']]) {
                     $sql = "SELECT value FROM {$choice_table} WHERE {$attribute_filter} AND attrib_id=" . (int) $data['attrib_id'] . "  AND id=" . (int) $data['value'];
                     $this->_db2->query($sql);
                     $this->_db2->next_record();
                     $ret = $this->_db2->f('value');
                     $cache_lb[$location_id][$data['attrib_id']][$data['value']] = $ret;
                 }
             }
             break;
         case 'AB':
             if (!isset($cache_contact[$data['value']]) || $ret != $cache_contact[$data['value']]) {
                 $contact_data = $this->contacts->read_single_entry($data['value'], array('fn'));
                 $ret = $contact_data[0]['fn'];
                 $cache_contact[$data['value']] = $ret;
             }
             break;
         case 'ABO':
             if (!isset($cache_contact[$data['value']]) || $ret != $cache_contact[$data['value']]) {
                 $contact_data = $this->contacts->get_principal_organizations_data($data['value']);
                 $ret = $contact_data[0]['org_name'];
                 $cache_contact[$data['value']] = $ret;
             }
             break;
         case 'VENDOR':
             if (!isset($cache_vendor[$data['value']]) || $ret != $cache_vendor[$data['value']]) {
                 $sql = "SELECT org_name FROM fm_vendor where id=" . (int) $data['value'];
                 $this->_db2->query($sql);
                 $this->_db2->next_record();
                 $ret = $this->_db2->f('org_name', true);
                 $cache_vendor[$data['value']] = $ret;
             }
             break;
         case 'CH':
             if ($data['attrib_id']) {
                 $ret = '';
                 if (!isset($cache_ch[$location_id][$data['attrib_id']][$data['value']]) || $ret != $cache_ch[$location_id][$data['attrib_id']][$data['value']]) {
                     $ch = explode(',', trim($data['value'], ','));
                     if (isset($ch) and is_array($ch)) {
                         for ($k = 0; $k < count($ch); $k++) {
                             $sql = "SELECT value FROM {$choice_table} WHERE {$attribute_filter} AND attrib_id= " . (int) $data['attrib_id'] . ' AND id = ' . (int) $ch[$k];
                             $this->_db2->query($sql);
                             while ($this->_db2->next_record()) {
                                 $ch_value[] = $this->_db2->f('value');
                             }
                         }
                         $ret = @implode(",", $ch_value);
                         unset($ch_value);
                     }
                     $cache_ch[$location_id][$data['attrib_id']][$data['value']] = $ret;
                 }
             }
             break;
         case 'D':
             $ret = $GLOBALS['phpgw']->common->show_date(strtotime($data['value']), $GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat']);
             break;
         case 'DT':
             $ret = $GLOBALS['phpgw']->common->show_date(strtotime($data['value']));
             break;
         case 'timestamp':
             //			$ret =  date($GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'],$data['value']);
             $ret = $GLOBALS['phpgw']->common->show_date($data['value']);
             break;
         case 'link':
             $ret = phpgw::safe_redirect($data['value']);
             break;
         case 'user':
             if (!isset($cache_vendor[$data['value']]) || $ret != $cache_vendor[$data['value']]) {
                 $ret = $GLOBALS['phpgw']->accounts->get($data['value'])->__toString();
                 $cache_vendor[$data['value']] = $ret;
             }
             break;
         case 'pwd':
             $ret = lang('yes');
             break;
         case 'custom1':
             $ret = '';
             if ($data['value'] && $data['get_single_function']) {
                 if (!$data['get_single_function_input']) {
                     $data['get_single_function_input'] = $data['value'];
                 }
                 $_compare_key = $this->_get_compare_key($data['get_single_function'], $data['get_single_function_input']);
                 if (!isset($cache_custom_lookup[$_compare_key]) || $ret != $cache_custom_lookup[$_compare_key]) {
                     $ret = execMethod($data['get_single_function'], $data['get_single_function_input']);
                     $cache_custom_lookup[$_compare_key] = $ret;
                 }
             } else {
                 if ($data['value'] && $data['get_list_function']) {
                     $_compare_key = $this->_get_compare_key($data['get_list_function'], $data['get_list_function_input']);
                     if (!isset($cache_custom_lookup[$_compare_key]) || $_list != $cache_custom_lookup[$_compare_key]) {
                         $_list = execMethod($data['get_list_function'], $data['get_list_function_input']);
                         $cache_custom_lookup[$_compare_key] = $_list;
                     }
                     if (isset($_list) && is_array($_list)) {
                         foreach ($_list as $_key => $_entry) {
                             if ($_entry['id'] == $data['value']) {
                                 $ret = $_entry['name'];
                                 break;
                             }
                         }
                     }
                 }
             }
             break;
         case 'custom2':
         case 'custom3':
             if ($data['value'] && $data['get_single_function']) {
                 if (!$data['get_single_function_input']) {
                     $data['get_single_function_input'] = $data['value'];
                 }
                 $_compare_key = $this->_get_compare_key($data['get_single_function'], $data['get_single_function_input']);
                 if (!isset($cache_custom_lookup[$_compare_key]) || $ret != $cache_custom_lookup[$_compare_key]) {
                     $ret = execMethod($data['get_single_function'], $data['get_single_function_input']);
                     $cache_custom_lookup[$_compare_key] = $ret;
                 }
             }
             break;
         default:
             if (is_array($data['value'])) {
                 $ret = $data['value'];
             } else {
                 $ret = stripslashes($data['value']);
             }
     }
     return $ret;
 }
 function index()
 {
     if (!$this->acl_read) {
         $GLOBALS['phpgw']->redirect_link('/index.php', array('menuaction' => 'property.uilocation.stop', 'perm' => 1, 'acl_location' => $this->acl_location));
     }
     $address = phpgw::get_var('address');
     $check_payments = phpgw::get_var('check_payments', 'bool');
     $location_code = phpgw::get_var('location_code');
     $gaards_nr = phpgw::get_var('gaards_nr', 'int');
     $bruksnr = phpgw::get_var('bruksnr', 'int');
     $feste_nr = phpgw::get_var('feste_nr', 'int');
     $seksjons_nr = phpgw::get_var('seksjons_nr', 'int');
     $datatable = array();
     $values_combo_box = array();
     if (phpgw::get_var('phpgw_return_as') != 'json') {
         $datatable['menu'] = $this->bocommon->get_menu();
         $datatable['config']['base_url'] = $GLOBALS['phpgw']->link('/index.php', array('menuaction' => 'property.uigab.index', 'location_code' => $location_code, 'gaards_nr' => $gaards_nr, 'bruksnr' => $bruksnr, 'feste_nr' => $feste_nr, 'seksjons_nr' => $seksjons_nr, 'address' => $address, 'check_payments' => $check_payments));
         $datatable['config']['allow_allrows'] = true;
         $datatable['config']['base_java_url'] = "menuaction:'property.uigab.index'," . "location_code:'{$location_code}'," . "gaards_nr:'{$gaards_nr}'," . "bruksnr: '{$bruksnr}'," . "feste_nr:'{$feste_nr}'," . "seksjons_nr:'{$seksjons_nr}'," . "address:'{$address}'," . "check_payments:'{$check_payments}'";
         $datatable['actions']['form'] = array(array('action' => $GLOBALS['phpgw']->link('/index.php', array('menuaction' => 'property.uigab.index', 'location_code' => $location_code, 'gaards_nr' => $gaards_nr, 'bruksnr' => $bruksnr, 'feste_nr' => $feste_nr, 'seksjons_nr' => $seksjons_nr, 'address' => $address, 'check_payments' => $check_payments)), 'fields' => array('field' => array(array('type' => 'label', 'id' => 'lbl_address', 'value' => lang('Address'), 'style' => 'filter'), array('name' => 'address', 'id' => 'txt_address', 'value' => '', 'type' => 'text', 'size' => 28, 'tab_index' => 1, 'onkeypress' => 'return pulsar(event)', 'style' => 'filter'), array('type' => 'label', 'id' => 'lbl_check', 'value' => lang('check payments'), 'style' => 'filter'), array('name' => 'check', 'id' => 'txt_check', 'value' => 0, 'type' => 'checkbox', 'tab_index' => 2, 'style' => 'filter'), array('type' => 'hidden', 'name' => 'check_payments', 'id' => 'txt_check_payments', 'value' => 0, 'style' => 'filter'), array('type' => 'label', 'id' => 'lbl_property_id', 'value' => lang('property id'), 'style' => 'filter'), array('name' => 'location_code', 'id' => 'txt_location_code', 'value' => $location_code, 'type' => 'text', 'size' => 6, 'tab_index' => 3, 'onkeypress' => 'return pulsar(event)', 'style' => 'filter'), array('type' => 'label', 'id' => 'lbl_gaards_nr', 'value' => lang('gaards nr'), 'style' => 'filter'), array('name' => 'gaards_nr', 'id' => 'txt_gaards_nr', 'value' => '', 'type' => 'text', 'size' => 6, 'tab_index' => 4, 'onkeypress' => 'return pulsar(event)', 'style' => 'filter'), array('type' => 'label', 'id' => 'lbl_bruksnr', 'value' => lang('bruks nr'), 'style' => 'filter'), array('name' => 'bruksnr', 'id' => 'txt_bruksnr', 'value' => '', 'type' => 'text', 'size' => 6, 'tab_index' => 5, 'onkeypress' => 'return pulsar(event)', 'style' => 'filter'), array('type' => 'label', 'id' => 'lbl_feste_nr', 'value' => lang('Feste nr'), 'style' => 'filter'), array('name' => 'feste_nr', 'id' => 'txt_feste_nr', 'value' => '', 'type' => 'text', 'size' => 6, 'tab_index' => 6, 'onkeypress' => 'return pulsar(event)', 'style' => 'filter'), array('type' => 'label', 'id' => 'lbl_seksjons_nr', 'value' => lang('Seksjons nr'), 'style' => 'filter'), array('name' => 'seksjons_nr', 'id' => 'txt_seksjons_nr', 'value' => '', 'type' => 'text', 'size' => 6, 'tab_index' => 7, 'onkeypress' => 'return pulsar(event)', 'style' => 'filter'), array('id' => 'btn_search', 'name' => 'search', 'value' => lang('search'), 'type' => 'button', 'tab_index' => 8, 'style' => 'filter'), array('id' => 'btn_reset', 'name' => 'reset', 'value' => lang('reset'), 'type' => 'reset', 'tab_index' => 9, 'style' => 'filter'), array('type' => 'button', 'id' => 'btn_new', 'tab_index' => 10, 'value' => lang('add'), 'style' => 'filter'), array('type' => 'button', 'id' => 'btn_export', 'tab_index' => 11, 'value' => lang('download'), 'style' => 'filter')), 'hidden_value' => array())));
     }
     $gab_list = $this->bo->read($location_code, $gaards_nr, $bruksnr, $feste_nr, $seksjons_nr, $address, $check_payments);
     $config = CreateObject('phpgwapi.config', 'property');
     $config->read_repository();
     $link_to_map = isset($config->config_data['map_url']) ? $config->config_data['map_url'] : '';
     if ($link_to_map) {
         $text_map = lang('Map');
     }
     $link_to_gab = isset($config->config_data['gab_url']) ? $config->config_data['gab_url'] : '';
     $gab_url_paramtres = isset($config->config_data['gab_url_paramtres']) ? $config->config_data['gab_url_paramtres'] : 'type=eiendom&Gnr=__gaards_nr__&Bnr=__bruks_nr__&Fnr=__feste_nr__&Snr=__seksjons_nr__';
     if ($link_to_gab) {
         $text_gab = lang('GAB');
     }
     $payment_date = $this->bo->payment_date;
     $uicols = array('input_type' => array('hidden', 'text', 'text', 'text', 'text', 'hidden', 'text', 'text', 'text', 'link', 'link'), 'name' => array('gab_id', 'gaards_nr', 'bruksnr', 'feste_nr', 'seksjons_nr', 'hits', 'owner', 'location_code', 'address', 'map', 'gab'), 'formatter' => array('', '', '', '', '', '', '', '', '', '', ''), 'descr' => array('dummy', lang('Gaards nr'), lang('Bruks nr'), lang('Feste nr'), lang('Seksjons nr'), lang('hits'), lang('Owner'), lang('Location'), lang('Address'), lang('Map'), lang('Gab')), 'className' => array('', '', '', '', '', '', '', '', '', '', ''));
     while (is_array($payment_date) && (list(, $date) = each($payment_date))) {
         $uicols['input_type'][] = 'date';
         $uicols['name'][] = str_replace('/', '_', $date);
         $uicols['formatter'][] = '';
         $uicols['descr'][] = $date;
         $uicols['className'][] = 'rightClasss';
         $uicols_add['input_type'][] = 'date';
         $uicols_add['name'][] = str_replace('/', '_', $date);
         $uicols_add['formatter'][] = '';
         $uicols_add['descr'][] = $date;
         $uicols_add['className'][] = 'rightClasss';
     }
     $content = array();
     $j = 0;
     if (isset($gab_list) && is_array($gab_list)) {
         foreach ($gab_list as $gab) {
             for ($i = 0; $i < count($uicols['name']); $i++) {
                 if ($uicols['input_type'][$i] != 'hidden') {
                     if ($uicols['name'][$i] == 'gaards_nr') {
                         $value_gaards_nr = substr($gab['gab_id'], 4, 5);
                         $value = $value_gaards_nr;
                     } else {
                         if ($uicols['name'][$i] == 'bruksnr') {
                             $value_bruks_nr = substr($gab['gab_id'], 9, 4);
                             $value = $value_bruks_nr;
                         } else {
                             if ($uicols['name'][$i] == 'feste_nr') {
                                 $value_feste_nr = substr($gab['gab_id'], 13, 4);
                                 $value = $value_feste_nr;
                             } else {
                                 if ($uicols['name'][$i] == 'seksjons_nr') {
                                     $value_seksjons_nr = substr($gab['gab_id'], 17, 3);
                                     $value = $value_seksjons_nr;
                                 } else {
                                     $value = isset($gab[$uicols['name'][$i]]) ? $gab[$uicols['name'][$i]] : '';
                                 }
                             }
                         }
                     }
                     $datatable['rows']['row'][$j]['column'][$i]['value'] = $value;
                     $datatable['rows']['row'][$j]['column'][$i]['name'] = $uicols['name'][$i];
                     $datatable['rows']['row'][$j]['column'][$i]['align'] = 'center';
                     if (isset($uicols['input_type']) && isset($uicols['input_type'][$i]) && $uicols['input_type'][$i] == 'link' && $uicols['name'][$i] == 'map') {
                         $value_gaards_nr = substr($gab['gab_id'], 4, 5);
                         $value_bruks_nr = substr($gab['gab_id'], 9, 4);
                         $value_feste_nr = substr($gab['gab_id'], 13, 4);
                         $link = phpgw::safe_redirect($link_to_map . '?maptype=Eiendomskart&gnr=' . (int) $value_gaards_nr . '&bnr=' . (int) $value_bruks_nr . '&fnr=' . (int) $value_feste_nr);
                         $datatable['rows']['row'][$j]['column'][$i]['format'] = 'link';
                         $datatable['rows']['row'][$j]['column'][$i]['value'] = $text_map;
                         $datatable['rows']['row'][$j]['column'][$i]['link'] = $link;
                         $datatable['rows']['row'][$j]['column'][$i]['target'] = '_blank';
                     }
                     if (isset($uicols['input_type']) && isset($uicols['input_type'][$i]) && $uicols['input_type'][$i] == 'link' && $uicols['name'][$i] == 'gab') {
                         $value_kommune_nr = substr($gab['gab_id'], 0, 4);
                         $value_gaards_nr = substr($gab['gab_id'], 4, 5);
                         $value_bruks_nr = substr($gab['gab_id'], 9, 4);
                         $value_feste_nr = substr($gab['gab_id'], 13, 4);
                         $value_seksjons_nr = substr($gab['gab_id'], 17, 3);
                         $_param = str_replace(array('__kommune_nr__', '__gaards_nr__', '__bruks_nr__', '__feste_nr__', '__seksjons_nr__'), array($value_kommune_nr, (int) $value_gaards_nr, (int) $value_bruks_nr, (int) $value_feste_nr, (int) $value_seksjons_nr), $gab_url_paramtres);
                         $link = phpgw::safe_redirect("{$link_to_gab}?{$_param}");
                         $datatable['rows']['row'][$j]['column'][$i]['format'] = 'link';
                         $datatable['rows']['row'][$j]['column'][$i]['value'] = $text_gab;
                         $datatable['rows']['row'][$j]['column'][$i]['link'] = $link;
                         $datatable['rows']['row'][$j]['column'][$i]['target'] = '_blank';
                     }
                     if (isset($uicols['input_type'][$i]) && $uicols['input_type'][$i] == 'date') {
                         $datatable['rows']['row'][$j]['column'][$i]['value'] = $gab['payment'][str_replace('_', '/', $uicols['name'][$i])];
                         $datatable['rows']['row'][$j]['column'][$i]['name'] = $uicols['name'][$i];
                         $datatable['rows']['row'][$j]['column'][$i]['align'] = 'right';
                     }
                 } else {
                     $datatable['rows']['row'][$j]['column'][$i]['name'] = $uicols['name'][$i];
                     $datatable['rows']['row'][$j]['column'][$i]['value'] = $gab[$uicols['name'][$i]];
                 }
                 $datatable['rows']['row'][$j]['hidden'][$i]['value'] = isset($gab[$uicols['name'][$i]]) ? $gab[$uicols['name'][$i]] : '';
                 $datatable['rows']['row'][$j]['hidden'][$i]['name'] = $uicols['name'][$i];
             }
             $j++;
         }
     }
     // NO pop-up
     $datatable['rowactions']['action'] = array();
     $parameters = array('parameter' => array(array('name' => 'gab_id', 'source' => 'gab_id')));
     if ($this->acl_read) {
         $datatable['rowactions']['action'][] = array('my_name' => 'view', 'text' => lang('view'), 'action' => $GLOBALS['phpgw']->link('/index.php', array('menuaction' => 'property.uigab.list_detail')), 'parameters' => $parameters);
     }
     if ($this->acl_add) {
         $datatable['rowactions']['action'][] = array('my_name' => 'add', 'text' => lang('add'), 'action' => $GLOBALS['phpgw']->link('/index.php', array('menuaction' => 'property.uigab.edit', 'from' => 'index')));
     }
     unset($parameters);
     //$uicols_count indicates the number of columns to display in actuall option-menu. this variable was set in $this->bo->read()
     $uicols_count = count($uicols['descr']);
     for ($i = 0; $i < $uicols_count; $i++) {
         //all colums should be have formatter
         $datatable['headers']['header'][$i]['formatter'] = $uicols['formatter'][$i] == '' ? '""' : $uicols['formatter'][$i];
         if ($uicols['input_type'][$i] != 'hidden') {
             $datatable['headers']['header'][$i]['name'] = $uicols['name'][$i];
             $datatable['headers']['header'][$i]['text'] = $uicols['descr'][$i];
             $datatable['headers']['header'][$i]['visible'] = true;
             $datatable['headers']['header'][$i]['sortable'] = false;
             if ($uicols['name'][$i] == 'gaards_nr') {
                 $datatable['headers']['header'][$i]['sortable'] = true;
                 $datatable['headers']['header'][$i]['sort_field'] = 'gab_id';
             } else {
                 if ($uicols['name'][$i] == 'location_code') {
                     $datatable['headers']['header'][$i]['sortable'] = true;
                     $datatable['headers']['header'][$i]['sort_field'] = 'location_code';
                 }
             }
         } else {
             $datatable['headers']['header'][$i]['name'] = $uicols['name'][$i];
             $datatable['headers']['header'][$i]['text'] = $uicols['descr'][$i];
             $datatable['headers']['header'][$i]['visible'] = false;
             $datatable['headers']['header'][$i]['sortable'] = false;
             $datatable['headers']['header'][$i]['format'] = 'hidden';
         }
     }
     // path for property.js
     $property_js = "/property/js/yahoo/property.js";
     if (!isset($GLOBALS['phpgw_info']['server']['no_jscombine']) || !$GLOBALS['phpgw_info']['server']['no_jscombine']) {
         $cachedir = urlencode($GLOBALS['phpgw_info']['server']['temp_dir']);
         $property_js = "/phpgwapi/inc/combine.php?cachedir={$cachedir}&type=javascript&files=" . str_replace('/', '--', ltrim($property_js, '/'));
     }
     $datatable['property_js'] = $GLOBALS['phpgw_info']['server']['webserver_url'] . $property_js;
     // Pagination and sort values
     $datatable['pagination']['records_start'] = (int) $this->bo->start;
     $datatable['pagination']['records_limit'] = $GLOBALS['phpgw_info']['user']['preferences']['common']['maxmatchs'];
     $datatable['pagination']['records_returned'] = count($gab_list);
     $datatable['pagination']['records_total'] = $this->bo->total_records;
     $datatable['sorting']['order'] = phpgw::get_var('order', 'string');
     // Column
     $appname = lang('gab');
     $function_msg = lang('list gab');
     if (phpgw::get_var("start") == "" && phpgw::get_var("order", 'string') == "") {
         $datatable['sorting']['order'] = 'gab_id';
         // name key Column in myColumnDef
         $datatable['sorting']['sort'] = 'asc';
         // ASC / DESC
     } else {
         $datatable['sorting']['order'] = phpgw::get_var('order', 'string');
         // name of column of Database
         $datatable['sorting']['sort'] = phpgw::get_var('sort', 'string');
         // ASC / DESC
     }
     phpgwapi_yui::load_widget('dragdrop');
     phpgwapi_yui::load_widget('datatable');
     phpgwapi_yui::load_widget('menu');
     phpgwapi_yui::load_widget('connection');
     phpgwapi_yui::load_widget('loader');
     phpgwapi_yui::load_widget('paginator');
     phpgwapi_yui::load_widget('tabview');
     //-- BEGIN----------------------------- JSON CODE ------------------------------
     //values for Pagination
     $json = array('recordsReturned' => $datatable['pagination']['records_returned'], 'totalRecords' => (int) $datatable['pagination']['records_total'], 'startIndex' => $datatable['pagination']['records_start'], 'sort' => $datatable['sorting']['order'], 'dir' => $datatable['sorting']['sort'], 'records' => array(), 'headers' => $uicols_add['name'], 'headers_all' => $uicols['name']);
     // values for datatable
     if (isset($datatable['rows']['row']) && is_array($datatable['rows']['row'])) {
         foreach ($datatable['rows']['row'] as $row) {
             $json_row = array();
             foreach ($row['column'] as $column) {
                 if (isset($column['format']) && $column['format'] == "link" && isset($column['java_link']) && $column['java_link'] == true) {
                     $json_row[$column['name']] = "<a href='#' id='" . $column['link'] . "' onclick='javascript:filter_data(this.id);'>" . $column['value'] . "</a>";
                 } elseif (isset($column['format']) && $column['format'] == "link") {
                     $json_row[$column['name']] = "<a href='" . $column['link'] . "' target='_blank'>" . $column['value'] . "</a>";
                 } else {
                     $json_row[$column['name']] = $column['value'];
                 }
             }
             $json['records'][] = $json_row;
         }
     }
     // right in datatable
     if (isset($datatable['rowactions']['action']) && is_array($datatable['rowactions']['action'])) {
         $json['rights'] = $datatable['rowactions']['action'];
     }
     if (phpgw::get_var('phpgw_return_as') == 'json') {
         return $json;
     }
     $datatable['json_data'] = json_encode($json);
     //-------------------- JSON CODE ----------------------
     // Prepare template variables and process XSLT
     $template_vars = array();
     $template_vars['datatable'] = $datatable;
     $GLOBALS['phpgw']->xslttpl->add_file(array('datatable'));
     $GLOBALS['phpgw']->xslttpl->set_var('phpgw', $template_vars);
     if (!isset($GLOBALS['phpgw']->css) || !is_object($GLOBALS['phpgw']->css)) {
         $GLOBALS['phpgw']->css = createObject('phpgwapi.css');
     }
     // Prepare CSS Style
     $GLOBALS['phpgw']->css->validate_file('datatable');
     $GLOBALS['phpgw']->css->validate_file('property');
     $GLOBALS['phpgw']->css->add_external_file('property/templates/base/css/property.css');
     $GLOBALS['phpgw']->css->add_external_file('phpgwapi/js/yahoo/datatable/assets/skins/sam/datatable.css');
     $GLOBALS['phpgw']->css->add_external_file('phpgwapi/js/yahoo/paginator/assets/skins/sam/paginator.css');
     $GLOBALS['phpgw']->css->add_external_file('phpgwapi/js/yahoo/container/assets/skins/sam/container.css');
     //Title of Page
     $GLOBALS['phpgw_info']['flags']['app_header'] = lang('property') . ' - ' . $appname . ': ' . $function_msg;
     // Prepare YUI Library
     $GLOBALS['phpgw']->js->validate_file('yahoo', 'gab.index', 'property');
     $this->save_sessiondata();
 }
 function read($data)
 {
     if (is_array($data)) {
         $start = isset($data['start']) && $data['start'] ? $data['start'] : 0;
         $filter = isset($data['filter']) ? $data['filter'] : 'none';
         $query = isset($data['query']) ? $data['query'] : '';
         $sort = isset($data['sort']) && $data['sort'] ? $data['sort'] : 'DESC';
         $order = isset($data['order']) ? $data['order'] : '';
         $cat_id = isset($data['cat_id']) ? $data['cat_id'] : '';
         $vendor_id = isset($data['vendor_id']) ? $data['vendor_id'] : '';
         $allrows = isset($data['allrows']) ? $data['allrows'] : '';
         $member_id = isset($data['member_id']) && $data['member_id'] ? $data['member_id'] : 0;
         $s_agreement_id = isset($data['s_agreement_id']) ? $data['s_agreement_id'] : '';
         $detail = isset($data['detail']) ? $data['detail'] : '';
         $p_num = isset($data['p_num']) ? $data['p_num'] : '';
         $status_id = isset($data['status_id']) && $data['status_id'] ? (int) $data['status_id'] : 0;
         $location_code = isset($data['location_code']) ? $data['location_code'] : '';
     }
     $choice_table = 'phpgw_cust_choice';
     $attribute_table = 'phpgw_cust_attribute';
     if (!$detail) {
         $entity_table = 'fm_s_agreement';
         $category_table = 'fm_s_agreement_category';
         $location_id = $GLOBALS['phpgw']->locations->get_id('property', '.s_agreement');
         $attribute_filter = " location_id = {$location_id}";
         $paranthesis = '(';
         $joinmethod = " {$this->join} {$category_table} ON ( {$entity_table}.category ={$category_table}.id))";
         $paranthesis .= '(';
         $joinmethod .= " {$this->left_join} fm_vendor ON ( {$entity_table}.vendor_id =fm_vendor.id))";
         $paranthesis .= '(';
         $joinmethod .= " {$this->left_join} fm_s_agreement_detail ON ( fm_s_agreement.id = fm_s_agreement_detail.agreement_id))";
         $cols = $entity_table . ".*,{$category_table}.descr as category, org_name";
         $cols_return[] = 'id';
         $uicols['input_type'][] = 'text';
         $uicols['name'][] = 'id';
         $uicols['descr'][] = lang('ID');
         $uicols['statustext'][] = lang('ID');
         $cols_return[] = 'name';
         $uicols['input_type'][] = 'text';
         $uicols['name'][] = 'name';
         $uicols['descr'][] = lang('name');
         $uicols['statustext'][] = lang('name');
         $cols_return[] = 'org_name';
         $uicols['input_type'][] = 'text';
         $uicols['name'][] = 'org_name';
         $uicols['descr'][] = lang('vendor');
         $uicols['statustext'][] = lang('vendor');
         $cols_return[] = 'category';
         $uicols['input_type'][] = 'text';
         $uicols['name'][] = 'category';
         $uicols['descr'][] = lang('category');
         $uicols['statustext'][] = lang('category');
         $cols_return[] = 'start_date';
         $uicols['input_type'][] = 'text';
         $uicols['name'][] = 'start_date';
         $uicols['descr'][] = lang('start');
         $uicols['statustext'][] = lang('start date');
         $cols_return[] = 'termination_date';
         $uicols['input_type'][] = 'text';
         $uicols['name'][] = 'termination_date';
         $uicols['descr'][] = lang('termination date');
         $uicols['statustext'][] = lang('termination date');
         //			$uicols['datatype'][]		= 'D';
         $cols_return[] = 'end_date';
         $uicols['input_type'][] = 'text';
         $uicols['name'][] = 'end_date';
         $uicols['descr'][] = lang('end');
         $uicols['statustext'][] = lang('end date');
     } else {
         $query = '';
         $allrows = true;
         $entity_table = 'fm_s_agreement_detail';
         $location_id = $GLOBALS['phpgw']->locations->get_id('property', '.s_agreement.detail');
         $attribute_filter = " location_id = {$location_id}";
         $paranthesis .= '(';
         $joinmethod .= " {$this->join}  fm_s_agreement_pricing ON ( {$entity_table}.agreement_id =fm_s_agreement_pricing.agreement_id AND {$entity_table}.id =fm_s_agreement_pricing.item_id))";
         $cols = "{$entity_table}.*, fm_s_agreement_pricing.cost,fm_s_agreement_pricing.id as index_count,fm_s_agreement_pricing.index_date,fm_s_agreement_pricing.item_id,fm_s_agreement_pricing.this_index";
         $cols_return[] = 'agreement_id';
         $uicols['input_type'][] = 'hidden';
         $uicols['name'][] = 'agreement_id';
         $uicols['descr'][] = lang('agreement_id');
         $uicols['statustext'][] = lang('agreement_id');
         $uicols['import'][] = false;
         $cols_return[] = 'item_id';
         $uicols['input_type'][] = 'text';
         $uicols['name'][] = 'item_id';
         $uicols['descr'][] = lang('ID');
         $uicols['statustext'][] = lang('ID');
         $uicols['import'][] = false;
         $cols_return[] = 'id';
         $uicols['input_type'][] = 'hidden';
         $uicols['name'][] = 'id';
         $uicols['descr'][] = false;
         $uicols['statustext'][] = false;
         $uicols['import'][] = false;
         $cols_return[] = 'location_code';
         $uicols['input_type'][] = 'text';
         $uicols['name'][] = 'location_code';
         $uicols['descr'][] = lang('location');
         $uicols['statustext'][] = lang('location');
         $uicols['import'][] = true;
         $cols_return[] = 'address';
         $uicols['input_type'][] = 'text';
         $uicols['name'][] = 'address';
         $uicols['descr'][] = lang('address');
         $uicols['statustext'][] = lang('address');
         $uicols['import'][] = true;
         $cols_return[] = 'p_entity_id';
         $uicols['input_type'][] = 'text';
         $uicols['name'][] = 'p_entity_id';
         $uicols['descr'][] = 'entity_id';
         $uicols['statustext'][] = false;
         $uicols['import'][] = true;
         $cols_return[] = 'p_cat_id';
         $uicols['input_type'][] = 'text';
         $uicols['name'][] = 'p_cat_id';
         $uicols['descr'][] = 'cat_id';
         $uicols['statustext'][] = false;
         $uicols['import'][] = true;
         $cols_return[] = 'p_num';
         $uicols['input_type'][] = 'text';
         $uicols['name'][] = 'p_num';
         $uicols['descr'][] = lang('entity num');
         $uicols['statustext'][] = lang('entity num');
         $uicols['import'][] = true;
         $cols_return[] = 'cost';
         $uicols['input_type'][] = 'text';
         $uicols['name'][] = 'cost';
         $uicols['descr'][] = lang('cost');
         $uicols['statustext'][] = lang('cost');
         $uicols['import'][] = true;
         $cols_return[] = 'this_index';
         $uicols['input_type'][] = 'text';
         $uicols['name'][] = 'this_index';
         $uicols['descr'][] = lang('index');
         $uicols['statustext'][] = lang('index');
         $uicols['import'][] = false;
         $cols_return[] = 'index_count';
         $uicols['input_type'][] = 'text';
         $uicols['name'][] = 'index_count';
         $uicols['descr'][] = lang('index_count');
         $uicols['statustext'][] = lang('index_count');
         $uicols['import'][] = false;
         $cols_return[] = 'index_date';
         $uicols['input_type'][] = 'text';
         $uicols['name'][] = 'index_date';
         $uicols['descr'][] = lang('date');
         $uicols['statustext'][] = lang('date');
         $uicols['import'][] = false;
     }
     $sql = "SELECT DISTINCT {$cols} FROM {$paranthesis} {$entity_table} {$joinmethod}";
     $i = count($uicols['name']);
     $user_columns = isset($GLOBALS['phpgw_info']['user']['preferences']['property']['s_agreement_columns' . !!$s_agreement_id]) ? $GLOBALS['phpgw_info']['user']['preferences']['property']['s_agreement_columns' . !!$s_agreement_id] : '';
     $user_column_filter = '';
     if (is_array($user_columns) && $user_columns[0]) {
         $user_column_filter = " OR ({$attribute_filter} AND id IN (" . implode(',', $user_columns) . '))';
     }
     $this->db->query("SELECT * FROM {$attribute_table} WHERE list=1 AND {$attribute_filter} {$user_column_filter} ");
     while ($this->db->next_record()) {
         $uicols['input_type'][] = 'text';
         $uicols['name'][] = $this->db->f('column_name');
         $uicols['descr'][] = $this->db->f('input_text');
         $uicols['statustext'][] = $this->db->f('statustext');
         $uicols['datatype'][$i] = $this->db->f('datatype');
         $uicols['import'][] = true;
         $cols_return_extra[] = array('name' => $this->db->f('column_name'), 'datatype' => $this->db->f('datatype'), 'attrib_id' => $this->db->f('id'));
         $i++;
     }
     $this->uicols = $uicols;
     if (!$s_agreement_id > 0 && $detail) {
         return;
     }
     if ($order) {
         if (!$detail) {
             switch ($order) {
                 case 'id':
                 case 'status':
                     $ordermethod = " ORDER BY {$entity_table}.{$order} {$sort}";
                     break;
                 case 'category':
                     $ordermethod = " ORDER BY {$category_table}.descr {$sort}";
                     break;
                 default:
                     $ordermethod = " ORDER BY {$order} {$sort}";
             }
         } else {
             switch ($order) {
                 case 'id':
                     $ordermethod = " ORDER BY {$entity_table}.{$order} {$sort}";
                     break;
             }
         }
     } else {
         $ordermethod = " ORDER BY {$entity_table}.id DESC";
     }
     $filtermethod = '';
     $where = 'WHERE';
     /*			if ($filter=='all')
     			{
     				if (is_array($grants))
     				{
     					while (list($user) = each($grants))
     					{
     						$public_user_list[] = $user;
     					}
     					reset($public_user_list);
     					$filtermethod .= " $where ( $entity_table.user_id IN(" . implode(',',$public_user_list) . "))";
     
     					$where= 'AND';
     				}
     
     			}
     			else
     			{
     				$filtermethod = " $where $entity_table.user_id=$filter ";
     				$where= 'AND';
     			}
      */
     if ($s_agreement_id) {
         $filtermethod .= " {$where} {$entity_table}.agreement_id={$s_agreement_id} AND current_index = 1";
         $where = 'AND';
     }
     if ($location_code) {
         $filtermethod .= " {$where} location_code {$this->like} '{$location_code}%'";
         $where = 'AND';
     }
     if ($cat_id && !$detail) {
         $filtermethod .= " {$where} {$entity_table}.category='{$cat_id}' ";
         $where = 'AND';
     }
     if ($vendor_id && !$detail) {
         $filtermethod .= " {$where} {$entity_table}.vendor_id='{$vendor_id}' ";
         $where = 'AND';
     }
     if ($member_id > 0 && !$detail) {
         $filtermethod .= " {$where} fm_vendor.member_of {$this->like} '%,{$member_id},%' ";
         $where = 'AND';
     }
     if (!$detail && $status_id) {
         $filtermethod .= " {$where} {$entity_table}.status='{$status_id}' ";
         $where = 'AND';
     }
     $_querymethod = array();
     $__querymethod = array();
     $_joinmethod_datatype = array();
     $_joinmethod_datatype_custom = array();
     $querymethod = '';
     if ($query) {
         $query = $this->db->db_addslashes($query);
         if ($p_num) {
             $query = explode(".", $query);
             $querymethod = " {$where} (fm_s_agreement_detail.p_entity_id='" . (int) $query[1] . "' AND fm_s_agreement_detail.p_cat_id='" . (int) $query[2] . "' AND fm_s_agreement_detail.p_num='{$query[3]}')";
             $where = 'AND';
         } else {
             $query_arr = array();
             $this->db->query("SELECT * FROM {$attribute_table} WHERE search='1' AND {$attribute_filter}");
             while ($this->db->next_record()) {
                 switch ($this->db->f('datatype')) {
                     case 'V':
                     case 'email':
                     case 'T':
                         if (!$criteria_id) {
                             $_querymethod[] = "{$entity_table}." . $this->db->f('column_name') . " {$this->like} '%{$query}%'";
                             $__querymethod = array();
                             // remove block
                         }
                         break;
                     case 'CH':
                         if (!$criteria_id) {
                             // from filter
                             $_querymethod[] = "{$entity_table}." . $this->db->f('column_name') . " {$this->like} '%,{$query},%'";
                             $__querymethod = array();
                             // remove block
                             // from text-search
                             $_filter_choise = "WHERE (phpgw_cust_choice.location_id =" . (int) $this->db->f('location_id') . " AND phpgw_cust_choice.attrib_id =" . (int) $this->db->f('id') . " AND phpgw_cust_choice.value {$this->like} '%{$query}%')";
                             $this->db2->query("SELECT phpgw_cust_choice.id FROM phpgw_cust_choice {$_filter_choise}", __LINE__, __FILE__);
                             while ($this->db2->next_record()) {
                                 $_querymethod[] = "{$entity_table}." . $this->db->f('column_name') . " {$this->like} '%," . $this->db2->f('id') . ",%'";
                             }
                         }
                         break;
                     case 'R':
                     case 'LB':
                         if (!$criteria_id) {
                             $_filter_choise = "WHERE (phpgw_cust_choice.location_id =" . (int) $this->db->f('location_id') . " AND phpgw_cust_choice.attrib_id =" . (int) $this->db->f('id') . " AND phpgw_cust_choice.value {$this->like} '%{$query}%')";
                             $this->db2->query("SELECT phpgw_cust_choice.id FROM phpgw_cust_choice {$_filter_choise}", __LINE__, __FILE__);
                             $__filter_choise = array();
                             while ($this->db2->next_record()) {
                                 $__filter_choise[] = $this->db2->f('id');
                             }
                             if ($__filter_choise) {
                                 $_querymethod[] = "{$entity_table}." . $this->db->f('column_name') . ' IN (' . implode(',', $__filter_choise) . ')';
                             }
                             $__querymethod = array();
                             // remove block
                         }
                         break;
                     case 'I':
                         if (ctype_digit($query) && !$criteria_id) {
                             $_querymethod[] = "{$entity_table}." . $this->db->f('column_name') . " = " . (int) $query;
                             $__querymethod = array();
                             // remove block
                         }
                         break;
                     case 'VENDOR':
                         if ($criteria_id == 'vendor') {
                             $_joinmethod_datatype[] = "{$this->join} fm_vendor ON ({$entity_table}." . $this->db->f('column_name') . " = fm_vendor.id AND fm_vendor.org_name {$this->like} '%{$query}%') ";
                             $__querymethod = array();
                             // remove block
                         }
                         break;
                     case 'AB':
                         if ($criteria_id == 'ab') {
                             $_joinmethod_datatype[] = "{$this->join} phpgw_contact_person ON ({$entity_table}." . $this->db->f('column_name') . " = pphpgw_contact_person.person_id AND (phpgw_contact_person.first_name {$this->like} '%{$query}%' OR phpgw_contact_person.last_name {$this->like} '%{$query}%'))";
                             $__querymethod = array();
                             // remove block
                         }
                         break;
                     case 'ABO':
                         if ($criteria_id == 'abo') {
                             $_joinmethod_datatype[] = "{$this->join} phpgw_contact_org ON ({$entity_table}." . $this->db->f('column_name') . " = phpgw_contact_org.org_id AND phpgw_contact_org.name {$this->like} '%{$query}%')";
                             $__querymethod = array();
                             // remove block
                         }
                         break;
                     default:
                         if (!$criteria_id) {
                             $_querymethod[] = "{$entity_table}." . $this->db->f('column_name') . " = '{$query}'";
                             $__querymethod = array();
                             // remove block
                         }
                 }
             }
         }
     }
     $_joinmethod_datatype = array_merge($_joinmethod_datatype, $_joinmethod_datatype_custom);
     foreach ($_joinmethod_datatype as $_joinmethod) {
         $sql .= $_joinmethod;
     }
     $_querymethod = array_merge($__querymethod, $_querymethod);
     if ($_querymethod) {
         $querymethod = " {$where} (" . implode(' OR ', $_querymethod) . ')';
         unset($_querymethod);
     }
     $sql .= " {$filtermethod} {$querymethod}";
     //			echo $sql;
     $this->db2->query($sql, __LINE__, __FILE__);
     $this->total_records = $this->db2->num_rows();
     if (!$allrows) {
         $this->db->limit_query($sql . $ordermethod, $start, __LINE__, __FILE__);
     } else {
         $this->db->query($sql . $ordermethod, __LINE__, __FILE__);
     }
     $j = 0;
     $n = count($cols_return);
     //_debug_array($cols_return);
     $contacts = CreateObject('phpgwapi.contacts');
     $s_agreement_list = array();
     while ($this->db->next_record()) {
         for ($i = 0; $i < $n; $i++) {
             $s_agreement_list[$j][$cols_return[$i]] = stripslashes($this->db->f($cols_return[$i]));
             //	$s_agreement_list[$j]['grants'] = (int)$grants[$this->db->f('user_id')];
         }
         if (isset($cols_return_extra) && is_array($cols_return_extra)) {
             foreach ($cols_return_extra as $return_extra) {
                 $value = '';
                 $value = $this->db->f($return_extra['name']);
                 if (($return_extra['datatype'] == 'R' || $return_extra['datatype'] == 'LB') && $value) {
                     $sql = "SELECT value FROM {$choice_table} WHERE {$attribute_filter} AND attrib_id=" . $return_extra['attrib_id'] . "  AND id=" . $value;
                     $this->db2->query($sql);
                     $this->db2->next_record();
                     $s_agreement_list[$j][$return_extra['name']] = $this->db2->f('value');
                 } else {
                     if ($return_extra['datatype'] == 'AB' && $value) {
                         $contact_data = $contacts->read_single_entry($value, array('n_given' => 'n_given', 'n_family' => 'n_family', 'email' => 'email'));
                         $s_agreement_list[$j][$return_extra['name']] = $contact_data[0]['n_family'] . ', ' . $contact_data[0]['n_given'];
                     } else {
                         if ($return_extra['datatype'] == 'VENDOR' && $value) {
                             $sql = "SELECT org_name FROM fm_vendor where id={$value}";
                             $this->db2->query($sql);
                             $this->db2->next_record();
                             $s_agreement_list[$j][$return_extra['name']] = $this->db2->f('org_name');
                         } else {
                             if ($return_extra['datatype'] == 'CH' && $value) {
                                 $ch = explode(',', trim($value, ','));
                                 if (isset($ch) and is_array($ch)) {
                                     for ($k = 0; $k < count($ch); $k++) {
                                         $sql = "SELECT value FROM {$choice_table} WHERE  {$attribute_filter} AND attrib_id=" . $return_extra['attrib_id'] . "  AND id=" . $ch[$k];
                                         $this->db2->query($sql);
                                         while ($this->db2->next_record()) {
                                             $ch_value[] = $this->db2->f('value');
                                         }
                                     }
                                     $s_agreement_list[$j][$return_extra['name']] = @implode(",", $ch_value);
                                     unset($ch_value);
                                 }
                             } else {
                                 if ($return_extra['datatype'] == 'D' && $value) {
                                     $s_agreement_list[$j][$return_extra['name']] = date($GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'], strtotime($value));
                                 } else {
                                     if ($cols_return_extra[$i]['datatype'] == 'timestamp' && $value) {
                                         $s_agreement_list[$j][$return_extra['name']] = date($GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'], $value);
                                     } else {
                                         if ($cols_return_extra[$i]['datatype'] == 'link' && $value) {
                                             $s_agreement_list[$j][$return_extra['name']] = phpgw::safe_redirect($value);
                                         } else {
                                             $s_agreement_list[$j][$return_extra['name']] = $value;
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
         $j++;
     }
     //_debug_array($s_agreement_list);
     return $s_agreement_list;
 }