config = &$CONF; $this->fields = array( '1' => 'id', '2' => 'nombre', '3' => 'email', '4' => 'telefono', '5' => 'fax', '6' => 'tipo_facturacion' ); $this->query_order = array( 'a' => 'ASC', 'd' => 'DESC' ); $this->records_view = array('order' => array('1','2','3','4','5','6'), 'width' => array('25','160', '125', '90', '90', '90'), 'orderby' => array(true, true, true, true, true, true), 'search' => array(true, true, false, true, false, false) ); $this->ref_tables = array( 'company_bill_type' => 'ref_facturacion', 'company_state' => 'ref_provincias' ); $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); } } //----------------------------------------------------------------------------------- // getNewCompanyList function getNewCompanyList() { $SQL_query = db_query('SELECT id, nombre FROM empresas WHERE estado = \'1\' ORDER BY fecha_alta DESC LIMIT 0, 20'); if ( db_num_rows($SQL_query) > 0 ) { $companyList = array(); while ( $SQL_values = db_fetch_array($SQL_query) ) $companyList[$SQL_values['id']] = $SQL_values['nombre']; } return $companyList; } //----------------------------------------------------------------------------------- // 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 empresas' . $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 id, nombre, email, telefono, fax, tipo_facturacion FROM empresas' . $SQL_records_filter . $SQL_records_order . $SQL_records_limit); while ( $SQL_record = db_fetch_array($SQL_query) ) { strip_slashes($SQL_record); $records_list[$SQL_record['id']]['1'] = $SQL_record['id']; $records_list[$SQL_record['id']]['2'] = stripslashes($SQL_record['nombre']); $records_list[$SQL_record['id']]['3'] = stripslashes($SQL_record['email']); $records_list[$SQL_record['id']]['4'] = stripslashes($SQL_record['telefono']); $records_list[$SQL_record['id']]['5'] = stripslashes($SQL_record['fax']); $records_list[$SQL_record['id']]['6'] = $this->ref['company_bill_type'][$SQL_record['tipo_facturacion']]; } } return $records_list; } //----------------------------------------------------------------------------------- // generateFilterQuery function generateFilterQuery( $filter_field = '', $filter_values = '' ) { if ( !empty($filter_values) && isset( $this->fields[$filter_field] ) ) return ' WHERE ' . $this->fields[$filter_field] . ' LIKE \'%' . addslashes($filter_values) . '%\''; else return false; } //----------------------------------------------------------------------------------- // deleteRecord // Elimina un registro de la BBDD. function deleteRecord($record) { $error .= ''; if ( $this->checkRecord($record) ) { $error .= 0; $SQL_query = @db_query('SELECT count(*) AS total FROM grupos WHERE id_empresa = \'' . $record . '\''); $SQL_values = db_fetch_array($SQL_query); $error .= ( $SQL_values['total'] > 0 ? 1 : 0 ); $SQL_query = @db_query('SELECT count(*) AS total FROM alumnos WHERE id_empresa = \'' . $record . '\' AND estado != \'2\''); $SQL_values = db_fetch_array($SQL_query); $error .= ( $SQL_values['total'] > 0 ? 1 : 0 ); if ( ereg("^0+$", $error) ) { if ( db_query('DELETE FROM empresas WHERE id = \'' . $record . '\'') ) $error .= '0'; else $error .= 1; } } else $error .= 1; return $error; } //----------------------------------------------------------------------------------- // checkRecord // Comprueba si existe o no un registro function checkRecord(&$record) { settype($record, "integer"); $SQL_check_record = db_query('SELECT count(*) AS total FROM empresas 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, $setcompany = false) { if ( $this->checkRecord($record) ) { $SQL_check_record = db_query('SELECT id, nombre AS company_name, estado as company_status, cif AS company_cif, tipo_facturacion AS company_bill_type, email AS company_email, telefono AS company_telephone, fax AS company_fax, situacion AS company_how_arrive, observaciones AS company_comments FROM empresas WHERE id = \'' . $record . '\''); if ( db_num_rows($SQL_check_record) == 1 ) { $SQL_values = db_fetch_array($SQL_check_record); strip_slashes($SQL_values); if ( $setcompany ) $this->company = $SQL_values; else return $SQL_values; } 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. // // [1] > Tipo de accion // [2] > Campo nombre // [3] > Campo CIF // [4] > Resultado de la accion sobre la BBDD. function manageRecord(&$params) { $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'; $params['company_cif'] = eregi_replace("[^0-9a-z]", '', $params['company_cif']); $this->validateParams($params); // Generacion del codigo de error $error = ( $action == '' ? 1 : 0 ); $error .= ( empty($params['company_name']) ? 1 : 0 ); $error .= ( !empty($params['company_cif']) && !$this->checkCif($params['company_cif'], $params['record_id']) ? 1 : 0 ); // Acciones sobre la BBDD if ( ereg("^0+$", $error) ) { format_record($params); if ( $action == 'insert' ) { if ( db_query('INSERT INTO empresas (nombre, estado, cif, tipo_facturacion, email, telefono, fax, situacion, observaciones) VALUES (\'' . $params['company_name'] . '\', \'' . $params['company_status'] . '\', \'' . $params['company_cif'] . '\', \'' . $params['company_bill_type'] . '\', \'' . $params['company_email'] . '\', \'' . $params['company_telephone'] . '\', \'' . $params['company_fax'] . '\', \'' . $params['company_how_arrive'] . '\', \'' . $params['company_comments'] . '\', \'' . $params['contact_name'] . '\', \'' . $params['contact_phone'] . '\', \'' . $params['contact_phone_2'] . '\', \'' . $params['contact_position'] . '\', \'' . $params['business_name'] . '\', \'' . $params['business_phone'] . '\', \'' . $params['business_phone_2'] . '\', \'' . $params['business_position'] . '\')') ) { $params['record_id'] = db_insert_id(); $error .= 0; } else $error .= 1; } elseif ( $action == 'update' ) { if ( db_query('UPDATE empresas SET nombre = \'' . $params['company_name'] . '\', estado = \'' . $params['company_status'] . '\', cif = \'' . $params['company_cif'] . '\', tipo_facturacion = \'' . $params['company_bill_type'] . '\', email = \'' . $params['company_email'] . '\', telefono = \'' . $params['company_telephone'] . '\', fax = \'' . $params['company_fax'] . '\', situacion = \'' . $params['company_how_arrive'] . '\', observaciones = \'' . $params['company_comments'] . '\' WHERE id = \'' . $params['record_id'] . '\'') ) $error .= 0; else $error .= 2; } } strip_slashes($params); return $error; } //----------------------------------------------------------------------------------- // validateParams // Comprueba los datos recibidos de los campos de tipo select con los valores de // las tablas de referencia. function validateParams( &$params ) { while ( list($field) = each($this->ref_tables) ) { if ( !empty($params[$field]) ) { if ( is_array($params[$field]) ) { while ( list($id) = each($params[$field]) ) { if ( !$this->ref[$field][$params[$field][$id]] ) unset($params[$field][$id]); } } else { if ( !$this->ref[$field][$params[$field]] ) unset($params[$field]); } } } reset($this->ref_tables); } //----------------------------------------------------------------------------------- // checkCif function checkCif( $cif, $record = '' ) { $SQL_query = db_query('SELECT count(*) AS total FROM empresas WHERE cif = \'' . $cif . '\'' . ( !empty($record) ? ' AND id != \'' . $record . '\'' : '' ) ); $SQL_values = db_fetch_array($SQL_query); if ( $SQL_values['total'] > 0 ) return false; else return true; } //----------------------------------------------------------------------------------- // getCompanyGroupsByDate function getCompanyGroupsByDate( $company, $start_time, $end_time ) { $companyGroups = array(); $SQL_teacher_date_start = date('Y-m-d', $start_time); $SQL_teacher_date_end = date('Y-m-d', $end_time); $SQL_query = db_query('SELECT g.id, g.nombre, g.fecha_inicio, IF( g.fecha_fin, g.fecha_fin, el.fecha_fin ) AS fecha_fin FROM grupos g LEFT JOIN ejercicios_lectivos el ON g.id_periodo = el.id WHERE g.id_empresa = \'' . $company . '\' HAVING ( ( fecha_inicio <= "' . $SQL_teacher_date_end . '" AND fecha_fin >= "' . $SQL_teacher_date_end . '" ) OR ( fecha_inicio <= "' . $SQL_teacher_date_start . '" AND fecha_fin >= "' . $SQL_teacher_date_start . '" ) OR ( fecha_inicio = "' . $SQL_teacher_date_start . '" AND fecha_fin = "' . $SQL_teacher_date_end . '" ) )'); if ( db_num_rows($SQL_query) ) { while ( $SQL_record = db_fetch_array($SQL_query) ) { $companyGroups[$SQL_record['id']] = array( 'id' => $SQL_record['id'], 'name' => $SQL_record['nombre'], 'startdate' => mktime(0, 0, 0, substr($SQL_record['fecha_inicio'], 5, 2), substr($SQL_record['fecha_inicio'], 8), substr($SQL_record['fecha_inicio'], 0, 4) ), 'enddate' => mktime(0, 0, 0, substr($SQL_record['fecha_fin'], 5, 2), substr($SQL_record['fecha_fin'], 8), substr($SQL_record['fecha_fin'], 0, 4) ), 'timetable' => $this->getGroupTimeTableById($SQL_record['id']) ); } } return $companyGroups; } //----------------------------------------------------------------------------------- // getGroupTimeTableById function getGroupTimeTableById( $record ) { $timeTable = array(); $SQL_query = db_query('SELECT dia_inicio, dia_fin, hora_inicio, hora_fin FROM grupo_horario WHERE id = \'' . $record . '\' ORDER BY dia_inicio'); if ( db_num_rows($SQL_query) ) { while ( $SQL_record = db_fetch_array($SQL_query) ) { if ( $SQL_record['dia_inicio'] == $SQL_record['dia_fin'] ) $timeTable[$SQL_record['dia_inicio']][] = array( 'start' => $SQL_record['hora_inicio'], 'end' => $SQL_record['hora_fin'] ); else { for ( $i=$SQL_record['dia_inicio']; $i<=$SQL_record['dia_fin']; $i++ ) $timeTable[$i][] = array( 'start' => $SQL_record['hora_inicio'], 'end' => $SQL_record['hora_fin'] ); } } } return $timeTable; } } ?>