config = &$CONF; $this->exercise = &$EXERCISE; $this->lng = &$LNG; $this->tables = array( 'companies' => array( 'empresa_tarifas', 'empresas' ), 'teachers' => array( 'profesor_tarifas', 'profesores' ) ); $this->fields = array( '1' => 't.id', '2' => 't.id_registro', '3' => 't.id_tarifa', '4' => 't.importe', '5' => 't.descripcion', '6' => 'r.nombre', '7' => 'rf.nombre', '8' => 't.defecto'); $this->query_order = array( 'a' => 'ASC', 'd' => 'DESC' ); $this->records_view = array('order' => array('8','1','6','4','5'), 'width' => array('15','25', '150', '75', '300'), 'orderby' => array(false, true, true, true, false) ); $this->ref_tables = array( 'cost_type' => 'ref_tarifas' ); $this->getReferenceValues(); } //----------------------------------------------------------------------------------- // getReferenceValues // Obtiene el listado de los datos de las tablas de referencia. function getReferenceValues() { if ( count($this->ref_tables) > 0 ) { while ( list($field) = each($this->ref_tables) ) { $SQL_query = db_query('SELECT id, nombre FROM ' . $this->ref_tables[$field] . ' ORDER BY nombre'); if ( db_num_rows($SQL_query) > 0 ) { while ( $SQL_values = db_fetch_array($SQL_query) ) $this->ref[$field][$SQL_values['id']] = $SQL_values['nombre']; } } reset($this->ref_tables); } } //----------------------------------------------------------------------------------- // getRecordList // Obtiene un listado de registros de la BBDD. function getRecordList(&$page, $order_field = 1, $order_type = 'd', $SQL_records_filter = '', $section ) { $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 ' . $this->tables[$section][0] . ' t LEFT JOIN ' . $this->tables[$section][1] . ' r ON t.id_registro = r.id LEFT JOIN ref_tarifas rf ON t.id_tarifa = rf.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 t.id, t.id_registro, t.id_tarifa, t.importe, t.descripcion, t.defecto, r.nombre, rf.nombre AS nombre_tarifa FROM ' . $this->tables[$section][0] . ' t LEFT JOIN ' . $this->tables[$section][1] . ' r ON t.id_registro = r.id LEFT JOIN ref_tarifas rf ON t.id_tarifa = rf.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']]['4'] = $SQL_record['importe']; $records_list[$SQL_record['id']]['5'] = $SQL_record['descripcion']; $records_list[$SQL_record['id']]['6'] = $SQL_record['nombre_tarifa']; $records_list[$SQL_record['id']]['8'] = $SQL_record['defecto']; } } 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]) && trim($filter_values[$id]) != '' ) $filterQuery .= ' AND ' . $this->fields[$id] . ' LIKE \'' . $filter_values[$id] . '\''; } } } elseif ( trim($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, $section) { if ( $this->checkRecord($record, $section) ) { if ( db_query('DELETE FROM ' . $this->tables[$section][0] . ' WHERE id = \'' . $record . '\'') ) return '0'; else return '2'; } else return '1'; } //----------------------------------------------------------------------------------- // checkRecord // Comprueba si existe o no un registro function checkRecord(&$record, $section) { settype($record, "integer"); if ( is_array($this->tables[$section]) ) { $SQL_check_record = db_query('SELECT count(*) AS total FROM ' . $this->tables[$section][0] . ' WHERE id = \'' . $record . '\''); $SQL_num_records = db_fetch_array($SQL_check_record); if ( $SQL_num_records['total'] == 1 ) return true; else return false; } else return false; } //----------------------------------------------------------------------------------- // getRecordValues // Obtiene los valores para el registro indicado. function getRecordValues($record, $section) { if ( $this->checkRecord($record, $section) ) { $SQL_check_record = db_query('SELECT t.id, t.id_registro AS reg_id, t.id_tarifa AS cost_type, t.importe AS cost_price, t.descripcion AS cost_description, r.nombre AS reg_name, rf.nombre AS cost_name FROM ' . $this->tables[$section][0] . ' t LEFT JOIN ' . $this->tables[$section][1] . ' r ON t.id_registro = r.id LEFT JOIN ref_tarifas rf ON t.id_tarifa = rf.id WHERE t.id = \'' . $record . '\''); if ( db_num_rows($SQL_check_record) == 1 ) { $this->cost = db_fetch_array($SQL_check_record); strip_slashes($this->cost); return $this->cost; } 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, $section) { $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'], $section) ) $action = 'update'; } } else $action = 'insert'; $params['cost_price'] = ereg_replace('[^0-9,\.]' , '', $params['cost_price']); $params['cost_price'] = ereg_replace(',' , '.', $params['cost_price']); // Generacion del codigo de error $error .= ( $action == '' ? 1 : 0 ); $error .= ( empty($params['reg_id']) ? 1 : 0 ); $error .= ( empty($params['cost_type']) || !is_numeric($params['cost_type']) ? 1 : 0 ); $error .= ( empty($params['cost_price']) || !is_numeric($params['cost_price']) ? 1 : 0 ); // Acciones sobre la BBDD if ( ereg("^0+$", $error) ) { format_record($params); if ( $action == 'insert' ) { if ( db_query('INSERT INTO ' . $this->tables[$section][0] . ' (id_registro, id_tarifa, importe, descripcion) VALUES (\'' . $params['reg_id'] . '\', \'' . $params['cost_type'] . '\', \'' . $params['cost_price'] . '\', \'' . addslashes($params['cost_description']) . '\')') ) { $params['record_id'] = db_insert_id(); $error .= 0; } else $error .= 1; } elseif ( $action == 'update' ) { if ( db_query('UPDATE ' . $this->tables[$section][0] . ' SET id_tarifa = \'' . $params['cost_type'] . '\', importe = \'' . $params['cost_price'] . '\', descripcion = \'' . addslashes($params['cost_description']) . '\' WHERE id = \'' . $params['record_id'] . '\'') ) $error .= 0; else $error .= 2; } } strip_slashes($params); return $error; } //----------------------------------------------------------------------------------- // setDefaultCost function setDefaultCost($record, $section) { if ( $this->checkRecord($record, $section) ) { $record_values = $this->getRecordValues($record, $section); @db_query('UPDATE ' . $this->tables[$section][0] . ' SET defecto = \'0\' WHERE id_registro = \'' . $record_values['reg_id'] . '\''); @db_query('UPDATE ' . $this->tables[$section][0] . ' SET defecto = \'1\' WHERE id = \'' . $record . '\' AND id_registro = \'' . $record_values['reg_id'] . '\''); return true; } else return false; } //----------------------------------------------------------------------------------- // getDefaultCost function getDefaultCost($record, $section) { $SQL_query = db_query('SELECT importe FROM ' . $this->tables[$section][0] . ' WHERE id_registro = \'' . $record . '\' AND defecto = \'1\''); if ( db_num_rows($SQL_query) ) { $SQL_record = db_fetch_array($SQL_query); return $SQL_record['importe']; } else { $SQL_query = @db_query('SELECT importe FROM ' . $this->tables[$section][0] . ' WHERE id_registro = \'' . $record . '\' AND id_tarifa = \'1\' ORDER BY id LIMIT 1'); if ( db_num_rows($SQL_query) ) { $SQL_record = db_fetch_array($SQL_query); return $SQL_record['importe']; } } } //----------------------------------------------------------------------------------- // getCostByType function getCostByType($section, $record, $type) { if ( $this->tables[$section] ) { $SQL_query = db_query('SELECT t.importe FROM ' . $this->tables[$section][0] . ' t LEFT JOIN ref_tarifas rt ON t.id_tarifa = rt.id LEFT JOIN ref_tipo_curso rtc ON rt.nombre = rtc.nombre WHERE id_registro = \'' . $record . '\' AND rtc.id = \'' . $type . '\' ORDER BY t.defecto, t.id LIMIT 1'); if ( db_num_rows($SQL_query) ) { echo '1'; $SQL_record = db_fetch_array($SQL_query); return $SQL_record['importe']; } else { $cost = $this->getDefaultCost($record, $section); return $cost; } } return false; } } ?>