config = &$CONF; $this->exercise = &$EXERCISE; $this->lng = &$LNG; $this->fields = array( '1' => 'ec.id', '2' => 'ec.id_empresa', '3' => 'ec.alias', '4' => 'ec.nombre', '5' => 'ec.telefono', '6' => 'ec.telefono_2', '7' => 'ec.puesto', '8' => 'ec.email', '9' => 'ec.observaciones', '10' => 'e.nombre'); $this->query_order = array( 'a' => 'ASC', 'd' => 'DESC' ); $this->records_view = array('order' => array('1','10','3','4','5','8','7','9'), 'width' => array('25', '100', '100', '130', '75', '100', '100', '100'), 'orderby' => array(true, true, true, true, true, true, true, true) ); } //----------------------------------------------------------------------------------- // getRecordList // Obtiene un listado de registros de la BBDD. function getRecordList(&$page, $order_field = 1, $order_type = 'd', $SQL_records_filter = '' ) { $records_list = array(); if ( !isset( $this->fields[$order_field] ) ) $order_field = 1; if ( !isset( $this->query_order[$order_type] ) ) $order_type = 'd'; $SQL_records_query = db_query('SELECT COUNT(*) AS total FROM empresa_contactos ec LEFT JOIN empresas e ON ec.id_empresa = e.id' . $SQL_records_filter); $SQL_records = db_fetch_array($SQL_records_query); if ( $SQL_records['total'] > 0 ) { $page = check_records_page($page, $SQL_records['total']); $this->num_records = $SQL_records['total']; $SQL_records_order = ' ORDER BY ' . $this->fields[$order_field] . ' ' . $this->query_order[$order_type]; $SQL_records_limit = ' LIMIT ' . ( ( $page - 1 ) * $this->config['max_num_regs_list'] ) . ', ' . $this->config['max_num_regs_list']; $SQL_query = db_query('SELECT ec.id, ec.id_empresa, ec.alias, ec.nombre, ec.telefono, ec.telefono_2, ec.puesto, ec.email, ec.observaciones, e.nombre AS nombre_empresa FROM empresa_contactos ec LEFT JOIN empresas e ON ec.id_empresa = e.id' . $SQL_records_filter . $SQL_records_order . $SQL_records_limit); while ( $SQL_record = db_fetch_array($SQL_query) ) { $records_list[$SQL_record['id']]['1'] = $SQL_record['id']; $records_list[$SQL_record['id']]['2'] = $SQL_record['id_empresa']; $records_list[$SQL_record['id']]['3'] = stripslashes($SQL_record['alias']); $records_list[$SQL_record['id']]['4'] = stripslashes($SQL_record['nombre']); $records_list[$SQL_record['id']]['5'] = $SQL_record['telefono']; $records_list[$SQL_record['id']]['6'] = $SQL_record['telefono_2']; $records_list[$SQL_record['id']]['7'] = stripslashes($SQL_record['puesto']); $records_list[$SQL_record['id']]['8'] = stripslashes($SQL_record['email']); $records_list[$SQL_record['id']]['9'] = stripslashes($SQL_record['observaciones']); $records_list[$SQL_record['id']]['10'] = stripslashes($SQL_record['nombre_empresa']); } } return $records_list; } //----------------------------------------------------------------------------------- // generateFilterQuery function generateFilterQuery( $filter_field = '', $filter_values = '', $exactSearch = false ) { $filterQuery = ' WHERE 1'; if ( is_array($filter_values) ) { if ( count($filter_values) > 0 ) { while ( list($id) = each($filter_values) ) { $filter_values[$id] = ( $exactSearch ? addslashes($filter_values[$id]) : '%' . addslashes($filter_values[$id]) . '%' ); if ( isset( $this->fields[$id]) && !empty($filter_values[$id]) ) $filterQuery .= ' AND ' . $this->fields[$id] . ' LIKE \'' . $filter_values[$id] . '\''; } } } elseif ( !empty($filter_values) && isset( $this->fields[$filter_field] ) ) { $filter_values = ( $exactSearch ? '\'' . addslashes($filter_values) . '\'' : '\'%' . addslashes($filter_values) . '%\'' ); $filterQuery .= ' AND ' . $this->fields[$filter_field] . ' LIKE ' . $filter_values; } return $filterQuery; } //----------------------------------------------------------------------------------- // deleteRecord // Elimina un registro de la BBDD. function deleteRecord($record) { if ( $this->checkRecord($record) ) { if ( db_query('DELETE FROM empresa_contactos WHERE id = \'' . $record . '\'') ) return '0'; else return '2'; } else return '1'; } //----------------------------------------------------------------------------------- // checkRecord // Comprueba si existe o no un registro function checkRecord(&$record) { settype($record, "integer"); $SQL_check_record = db_query('SELECT count(*) AS total FROM empresa_contactos WHERE id = \'' . $record . '\''); $SQL_num_records = db_fetch_array($SQL_check_record); if ( $SQL_num_records['total'] == 1 ) return true; else return false; } //----------------------------------------------------------------------------------- // getRecordValues // Obtiene los valores para el registro indicado. function getRecordValues($record) { if ( $this->checkRecord($record) ) { $SQL_check_record = db_query('SELECT ec.id, ec.id_empresa AS company_id, ec.alias AS contact_alias, ec.nombre AS contact_name, ec.telefono AS contact_phone, ec.telefono_2 AS contact_phone_2, ec.puesto AS contact_position, ec.email AS contact_email, ec.observaciones AS contact_comments, e.nombre AS company_name FROM empresa_contactos ec LEFT JOIN empresas e ON ec.id_empresa = e.id WHERE ec.id = \'' . $record . '\''); if ( db_num_rows($SQL_check_record) == 1 ) { $this->contact = db_fetch_array($SQL_check_record); strip_slashes($this->contact); return $this->contact; } else return false; } else return false; } //----------------------------------------------------------------------------------- // manageRecord // - Recibe los parametros enviados desde el formulario mediante metodo POST. // - Determina el tipo de accion a realizar (INSERT o UPDATE). // - Trata los valores recibidos. // - Genera un codigo de error correspondiente a las acciones realizadas. function manageRecord(&$params, $companyValues = '') { $action = ''; $error = ''; // Chequeo y tratamiento de los valores recibidos if ( isset($params['record_id']) ) { if ( ereg("^[0-9]+$", $params['record_id'] ) && $params['record_id'] > 0 ) { if ( $this->checkRecord($params['record_id']) ) $action = 'update'; } } else $action = 'insert'; // Generacion del codigo de error $error .= ( $action == '' ? 1 : 0 ); $error .= ( empty($params['contact_name']) ? 1 : 0); // Acciones sobre la BBDD if ( ereg("^0+$", $error) ) { format_record($params); if ( $action == 'insert' ) { if ( db_query('INSERT INTO empresa_contactos ( id_empresa, alias, nombre, telefono, telefono_2, puesto, email, observaciones ) VALUES (\'' . $params['company_id'] . '\', \'' . $params['contact_alias'] . '\', \'' . $params['contact_name'] . '\', \'' . $params['contact_phone'] . '\', \'' . $params['contact_phone_2'] . '\', \'' . $params['contact_position'] . '\', \'' . $params['contact_email'] . '\', \'' . $params['contact_comments'] . '\')') ) { $params['record_id'] = db_insert_id(); $error .= 0; } else $error .= 1; } elseif ( $action == 'update' ) { if ( db_query('UPDATE empresa_contactos SET alias = \'' . $params['contact_alias'] . '\', nombre = \'' . $params['contact_name'] . '\', telefono = \'' . $params['contact_phone'] . '\', telefono_2 = \'' . $params['contact_phone_2'] . '\', puesto = \'' . $params['contact_position'] . '\', email = \'' . $params['contact_email'] . '\', observaciones = \'' . $params['contact_comments'] . '\' WHERE id = \'' . $params['record_id'] . '\'') ) $error .= 0; else $error .= 1; } } strip_slashes($params); return $error; } } ?>