conf = &$CONF; $this->lng = &$LNG; $this->fields = array( '1' => 'f.id', '2' => 'f.nombre', '3' => 'f.estado', '4' => 'f.id_seccion', '5' => 'f.fichero', '6' => 'f.orden' ); $this->query_order = array( 'a' => 'ASC', 'd' => 'DESC' ); $this->records_view = array('order' => array('1','2','3'), 'width' => array('25', '500', '80'), 'orderby' => array(true, true, true), 'search' => array(true, true, false) ); $this->tables = array( 'oa' => 'oas_ficheros', 'section' => 'secciones_ficheros' ); } //----------------------------------------------------------------------------------- // 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'; if ( empty($SQL_records_filter) ) $this->generateFilterQuery(); $SQL_records_query = db_query('SELECT COUNT(*) AS total FROM ' . $this->tables[$section] . ' f ' . $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] . ', ' . $this->fields[2]; $SQL_records_limit = ' LIMIT ' . ( ( $page - 1 ) * $this->conf['max_num_regs_list'] ) . ', ' . $this->conf['max_num_regs_list']; $SQL_query = db_query('SELECT f.id, f.id_seccion, f.nombre, f.estado FROM ' . $this->tables[$section] . ' f ' . $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'] = stripslashes($SQL_record['nombre']); $records_list[$SQL_record['id']]['3'] = $this->lng['form_status_' . $SQL_record['estado']]; $records_list[$SQL_record['id']]['4'] = $SQL_record['id_seccion']; } } return $records_list; } //----------------------------------------------------------------------------------- // getRecordList // Obtiene un listado de registros de la BBDD. function getRecordListBySectionWithoutLimit($order_field = 1, $order_type = 'd', $SQL_records_filter = '') { $section = 'section'; $records_list = array(); if ( !isset( $this->fields[$order_field] ) ) $order_field = 1; if ( !isset( $this->query_order[$order_type] ) ) $order_type = 'd'; if ( empty($SQL_records_filter) ) $this->generateFilterQuery(); $SQL_records_query = db_query('SELECT COUNT(*) AS total FROM ' . $this->tables[$section] . ' f ' . $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] . ', ' . $this->fields[2]; $SQL_query = db_query('SELECT f.id, f.id_seccion, f.nombre, f.imagen, f.estado, f.orden FROM ' . $this->tables[$section] . ' f ' . $SQL_records_filter . $SQL_records_order); while ( $SQL_record = db_fetch_array($SQL_query) ) { $records_list['file_' . $SQL_record['id']]['id'] = $SQL_record['id']; $records_list['file_' . $SQL_record['id']]['nombre'] = stripslashes($SQL_record['nombre']); $records_list['file_' . $SQL_record['id']]['estado'] = $this->lng['form_status_' . $SQL_record['estado']]; $records_list['file_' . $SQL_record['id']]['id_seccion'] = $SQL_record['id_seccion']; $records_list['file_' . $SQL_record['id']]['imagen'] = $SQL_record['imagen']; $records_list['file_' . $SQL_record['id']]['orden'] = $SQL_record['orden']; } } return $records_list; } //----------------------------------------------------------------------------------- // generateFilterQuery function generateFilterQuery( $filter_field = '', $filter_values = '', $exactSearch = false, $sections = array() ) { $filterQuery = ' WHERE 1'; $filter_values = ( $exactSearch ? '\'' . addslashes($filter_values) . '\'' : '\'%' . addslashes($filter_values) . '%\'' ); $sections[] = $filter_values; if ( !empty($filter_values) && isset( $this->fields[$filter_field] ) ) { if ( $filter_field == '4' && count($sections) > '0' ) { $filterQuery .= ' AND ' . $this->fields[$filter_field] . ' IN (' . implode(',', $sections) . ')'; } else $filterQuery .= ' AND ' . $this->fields[$filter_field] . ' LIKE ' . $filter_values; } return $filterQuery; } //----------------------------------------------------------------------------------- // deleteRecord // Elimina un registro de la BBDD. function deleteRecord($record, $section) { $error .= ''; if ( $this->checkRecord($record, $section) ) { if ( @db_query('DELETE FROM ' . $this->tables[$section] . ' WHERE id = \'' . $record . '\'') ) $error .= '0'; else $error .= 3; } else $error .= 1; return $error; } //----------------------------------------------------------------------------------- // checkRecord // Comprueba si existe o no un registro function checkRecord(&$record, $section) { settype($record, "integer"); if ( $this->tables[$section] ) { $SQL_check_record = db_query('SELECT count(*) AS total FROM ' . $this->tables[$section] . ' 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->tables[$section] && $this->checkRecord($record, $section) ) { $SQL_check_record = db_query('SELECT f.id, f.id_seccion, f.nombre, f.fichero, f.imagen, f.estado, f.visualizar, f.orden FROM ' . $this->tables[$section] . ' f WHERE f.id = \'' . $record . '\''); if ( db_num_rows($SQL_check_record) == 1 ) { $this->filename = db_fetch_array($SQL_check_record); strip_slashes($this->filename); return $this->filename; } 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, &$files, $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'; if ( $action == 'update' ) $old_record = $this->getRecordValues($params['record_id'], $section); $params['id_seccion'] = eregi_replace("[^0-9]", '', $params['id_seccion']); $params['estado'] = ereg_replace("^([^0-1])${1}", '', $params['estado']); $params['orden'] = eregi_replace("[^0-9]", '', $params['orden']); // Generacion del codigo de error $error = ( $action == '' ? 1 : 0 ); $error .= ( empty($params['nombre']) ? 1 : 0 ); $error .= ( empty($params['id_seccion']) ? 1 : 0 ); $error .= (!empty($params['orden']) && !is_numeric($params['orden']) ? 1 : 0 ); // Acciones sobre la BBDD if ( ereg("^0+$", $error) ) { format_record($params); if ( $action == 'insert' ) { if ( db_query('INSERT INTO ' . $this->tables[$section] . ' (id_seccion, nombre, estado, visualizar, imagen' . (!empty($params['orden'])?', orden':'') . ') VALUES (\'' . $params['id_seccion'] . '\', \'' . $params['nombre'] . '\', \'' . $params['estado'] . '\', \'' . implode(',', $params['visualizar']) . '\', \'' . $params['imagen'] . '\'' . (!empty($params['orden'])?', \'' . $params['orden'] . '\'':'') . ')') ) { $params['record_id'] = db_insert_id(); $error .= 0; } else $error .= 1; } elseif ( $action == 'update' ) { if ( db_query('UPDATE ' . $this->tables[$section] . ' SET id_seccion = \'' . $params['id_seccion'] . '\', nombre = \'' . $params['nombre'] . '\', visualizar = \'' . (!empty($params['visualizar'])?implode(',', $params['visualizar']):'') . '\', estado = \'' . $params['estado'] . '\', imagen = \'' . $params['imagen'] . '\', orden = ' . (!empty($params['orden'])?'\'' . $params['orden'] . '\'':'NULL') . ' WHERE id = \'' . $params['record_id'] . '\'') ) $error .= 0; else $error .= 2; } if ($this->manageFile($files['fichero'], $section, $params['record_id'], $params, $error, $old_record['fichero']) ) { $params['fichero'] = $files['fichero']['name']; @db_query('UPDATE ' . $this->tables[$section] . ' SET fichero = \'' . $files['fichero']['name'] . '\' WHERE id = \'' . $params['record_id'] . '\''); } } strip_slashes($params); return $error; } function manageFile( &$file, $section, $id, &$params, &$error, $replace = '' ) { if ( is_array($file) && $file['error'] != '4' ) { if ( is_uploaded_file($file['tmp_name']) ) { if ( $this->checkRecord($id, $section) ) { $location = $this->conf['fs_files']; // echo $location . $replace; if ( !empty($replace) && file_exists($location . $replace) ) @unlink($location . $replace); @unlink($location . $file['name']); if ( @copy($file['tmp_name'], $location . $file['name']) ) { $error .= 0; return true; } else $error .= 3; } else $error .= 2; } else $error .= 1; } else $error .= 0; return false; } function getFileImages( $dir ) { $iconList = array(); $handle=@opendir($dir); if ($handle) { while (($file = readdir($handle))!==false) { if ($file != "." && $file != ".." && exif_imagetype($dir . $file)) $iconList[] = $file; } } closedir($handle); return $iconList; } } ?>