Source for file m_index.php
Documentation is available at
m_index.php
<?php
/**
* Archivo modelo para el proceso de instalacion
*
*
@package
m_index.php
*
@copyright
2007 2008 Luis Jorge Martin Aznar
*
@version
1.0
*
@Review
1
*
@author
Luis Jorge Martin Aznar
* creacion= 20-noviembre-2007
* modificacion= 12-abril-2008
*
* generator= gedit
*
* This file is part of PreAdmin ©.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Incluye el fichero de clases para postgres
*
*/
include_once
(
'../../modelo/postgres.php'
)
;
/**
* Funcion que crea los objetos de la base de datos
*
*
* Esta funcion crea los objetos de la base de datos. Esta funcion forma parte
* de la capa modelo y como tal esta alojada en el directorio modelo local al directorio
* "instalacion".
*
@param
Es
el array con los valores de los campos enviados por
* el formulario
*
@return
Si
la creacion ha finalizado con exito retorna "creado"; en caso contrario retorna
* 0.
*/
function
completarbd
(
$campos
)
{
$hoy
=
date
(
"Y-m-d H:i:s"
)
;
$servidor
=
$campos
[
'txt_servidor'
]
;
$nom_bd
=
$campos
[
'txt_bd'
]
;
$clave_postgre
=
$campos
[
'txt_clave_postgre'
]
;
$usuario_postgre
=
$campos
[
'txt_usuario'
]
;
$administrador
=
$campos
[
'txt_admin'
]
;
$clave
=
$campos
[
'txt_clave'
]
;
$nombre_admin
=
$campos
[
'nombre_admin'
]
;
$apellidos_admin
=
$campos
[
'apellidos_admin'
]
;
$dni_admin
=
$campos
[
'dni_admin'
]
;
$direc_admin
=
$campos
[
'direc_admin'
]
;
$telf_admin
=
$campos
[
'telf_admin'
]
;
$mail_admin
=
$campos
[
'mail_admin'
]
;
$conecto
=new bd
(
)
;
$conexion
=
$conecto
->
conectar2
(
$servidor
,
$clave_postgre
,
$usuario_postgre
)
;
if
(
!
$conexion
)
{
return
(
"0"
)
;
break
;
}
$conexion
=
$conecto
->
query
(
"
DROP DATABASE
$nom_bd
"
)
;
$conexion
=
$conecto
->
query
(
"
CREATE DATABASE
$nom_bd
WITH OWNER =
$usuario_postgre
ENCODING = 'UTF8'
TABLESPACE = pg_default
"
)
;
if
(
!
$conexion
)
{
return
(
"0"
)
;
break
;
}
$conexion
=
$conecto
->
cerrar
(
)
;
$conecto
=new bd
(
)
;
$conexion
=
$conecto
->
conectar
(
)
;
if
(
$conexion
!=
"0"
)
{
$conecto
->
begin
(
)
;
$conexion
=
$conecto
->
query
(
"CREATE TRUSTED LANGUAGE plpgsql
HANDLER language_handler_in"
)
;
if
(
!
$conexion
)
{
return
(
"0"
)
;
break
;
}
/**
* Formato de fecha
*
*/
$conecto
->
query
(
"SET TIME ZONE 'Europe/Rome';
SELECT CURRENT_TIMESTAMP AS ahora"
)
;
if
(
!
$conecto
)
{
return
(
"0"
)
;
break
;
}
/**
* creo las tablas
*
*/
$conecto
->
query
(
"CREATE TABLE personas
(
id_persona serial NOT NULL,
firstname varchar(60),
lastname varchar(60),
dni varchar(9) NOT NULL,
adress varchar(80),
cpostal varchar(6),
email varchar(40),
phone varchar(40),
fax varchar(40),
isactive_p char(1) NOT NULL DEFAULT 'Y',
created_p timestamp NOT NULL,
createdby_p numeric(10) NOT NULL,
updated_p timestamp NOT NULL,
updatedby_p numeric(10) NOT NULL,
CONSTRAINT personas_pkey PRIMARY KEY (id_persona),
CONSTRAINT personas_dni_key UNIQUE (dni)
)"
)
;
if
(
!
$conecto
)
{
return
(
"0"
)
;
break
;
}
$conecto
->
respaldo
(
'personas'
,
'id_persona'
,
'updatedby_p'
)
;
$conecto
->
respaldo_borrar
(
'personas'
,
'id_persona'
,
'updatedby_p'
)
;
$conecto
->
query
(
"CREATE TABLE menus
(
id_menu serial NOT NULL,
name varchar(30),
ispadre char(1) NOT NULL DEFAULT 'Y',
id_padre integer NOT NULL DEFAULT 0,
funtion varchar(100) NOT NULL,
orden integer NOT NULL DEFAULT 0,
created_me timestamp NOT NULL,
createdby_me numeric(10) NOT NULL,
updated_me timestamp NOT NULL,
updatedby_me numeric(10) NOT NULL,
CONSTRAINT menus_pkey PRIMARY KEY (id_menu),
CONSTRAINT menus_fkey FOREIGN KEY (id_padre)
REFERENCES menus (id_menu)
ON UPDATE CASCADE ON DELETE NO ACTION
)"
)
;
if
(
!
$conecto
)
{
return
(
"0"
)
;
break
;
}
$conecto
->
respaldo
(
'menus'
,
'id_menu'
,
'updatedby_me'
)
;
$conecto
->
respaldo_borrar
(
'menus'
,
'id_menu'
,
'updatedby_me'
)
;
$conecto
->
query
(
"CREATE TABLE perfiles
(
id_perfil serial NOT NULL,
name varchar(60),
created_per timestamp NOT NULL,
createdby_per numeric(10) NOT NULL,
updated_per timestamp NOT NULL,
updatedby_per numeric(10) NOT NULL,
CONSTRAINT perfiles_pkey PRIMARY KEY (id_perfil)
)"
)
;
if
(
!
$conecto
)
{
return
(
"0"
)
;
break
;
}
$conecto
->
respaldo
(
'perfiles'
,
'id_perfil'
,
'updatedby_per'
)
;
$conecto
->
respaldo_borrar
(
'perfiles'
,
'id_perfil'
,
'updatedby_per'
)
;
$conecto
->
query
(
"CREATE TABLE perfil_menus
(
id_perfil_menu serial NOT NULL,
id_menu numeric(10) NOT NULL,
id_perfil numeric(10) NOT NULL,
created_peme timestamp NOT NULL,
createdby_peme numeric(10) NOT NULL,
updated_peme timestamp NOT NULL,
updatedby_peme numeric(10) NOT NULL,
CONSTRAINT perfil_menus_pkey PRIMARY KEY (id_perfil_menu),
CONSTRAINT perfil_menus_fkey FOREIGN KEY (id_menu)
REFERENCES menus (id_menu)
ON UPDATE CASCADE ON DELETE NO ACTION,
CONSTRAINT perfil_menus2_fkey FOREIGN KEY (id_perfil)
REFERENCES perfiles (id_perfil)
ON UPDATE CASCADE ON DELETE NO ACTION
)"
)
;
if
(
!
$conecto
)
{
return
(
"0"
)
;
break
;
}
$conecto
->
respaldo
(
'perfil_menus'
,
'id_perfil_menu'
,
'updatedby_peme'
)
;
$conecto
->
respaldo_borrar
(
'perfil_menus'
,
'id_perfil_menu'
,
'updatedby_peme'
)
;
$conecto
->
query
(
"CREATE TABLE historial
(
id_historial serial NOT NULL,
created timestamp NOT NULL DEFAULT now(),
createdby numeric(10) NOT NULL,
tabla varchar(60),
campo varchar(60),
id numeric(10) NOT NULL,
oldvalue text,
newvalue text,
CONSTRAINT historial1_pkey PRIMARY KEY (id_historial)
)"
)
;
if
(
!
$conecto
)
{
return
(
"0"
)
;
break
;
}
$conecto
->
query
(
"CREATE TABLE planes
(
id_plan serial NOT NULL,
planname varchar(60) NOT NULL,
isactive char(1) NOT NULL DEFAULT 'Y',
isblocked char(1) NOT NULL DEFAULT 'Y',
is_evaini_end char(1) NOT NULL DEFAULT 'N',
created timestamp NOT NULL DEFAULT now(),
createdby numeric(10) NOT NULL,
updated timestamp NOT NULL DEFAULT now(),
updatedby numeric(10) NOT NULL,
date_report date NOT NULL DEFAULT now(),
servicio_report varchar(60) NOT NULL,
CONSTRAINT planes_pkey PRIMARY KEY (id_plan),
CONSTRAINT nombre_ukey UNIQUE(planname)
)"
)
;
if
(
!
$conecto
)
{
return
(
"0"
)
;
break
;
}
$conecto
->
respaldo
(
'planes'
,
'id_plan'
,
'updatedby'
)
;
$conecto
->
respaldo_borrar
(
'planes'
,
'id_plan'
,
'updatedby'
)
;
$conecto
->
query
(
"CREATE TABLE usuarios
(
id_user serial NOT NULL,
id_persona int8 NOT NULL,
username varchar(60) NOT NULL,
clave varchar(30),
rol int2 NOT NULL,
isactive_u char(1) NOT NULL DEFAULT 'Y',
isblocked char(1) NOT NULL DEFAULT 'N',
created_u timestamp NOT NULL DEFAULT now(),
createdby_u numeric(10) NOT NULL,
updated_u timestamp NOT NULL DEFAULT now(),
updatedby_u numeric(10) NOT NULL,
cont_int int2 DEFAULT 0,
memoplan numeric(10)DEFAULT 0,
date_password date NOT NULL DEFAULT now(),
CONSTRAINT usuarios_pkey PRIMARY KEY (id_user),
CONSTRAINT usuarios_fkey FOREIGN KEY (id_persona)
REFERENCES personas (id_persona)
ON UPDATE CASCADE ON DELETE NO ACTION,
CONSTRAINT usuarios2_fkey FOREIGN KEY (rol)
REFERENCES perfiles (id_perfil )
ON UPDATE CASCADE ON DELETE NO ACTION,
CONSTRAINT usuarios_username_key UNIQUE (username)
)"
)
;
if
(
!
$conecto
)
{
return
(
"0"
)
;
break
;
}
$conecto
->
respaldo
(
'usuarios'
,
'id_user'
,
'updatedby_u'
)
;
$conecto
->
respaldo_borrar
(
'usuarios'
,
'id_user'
,
'updatedby_u'
)
;
$conecto
->
query
(
"CREATE TABLE registro_entradas
(
id_registro serial NOT NULL,
username varchar(60) NOT NULL,
ip varchar(30),
host varchar(255),
exito char(1) NOT NULL DEFAULT 'N',
fecha_entrada timestamp NOT NULL DEFAULT now(),
CONSTRAINT registro_entradas_pkey PRIMARY KEY (id_registro),
CONSTRAINT registro_entradas_fkey FOREIGN KEY (username)
REFERENCES usuarios (username) ON UPDATE CASCADE ON DELETE NO ACTION
)"
)
;
if
(
!
$conecto
)
{
return
(
"0"
)
;
break
;
}
$conecto
->
query
(
"CREATE TABLE agenda
(
id serial NOT NULL,
id_user numeric(10) NOT NULL,
fecha date NOT NULL,
cita text NOT NULL,
tipo char(1) NOT NULL,
franja varchar(2) NOT NULL,
dia int4 NOT NULL,
CONSTRAINT agenda_pkey PRIMARY KEY (id),
CONSTRAINT agenda_fkey FOREIGN KEY (id_user)REFERENCES usuarios (id_user)
)"
)
;
if
(
!
$conecto
)
{
return
(
"0"
)
;
break
;
}
$conecto
->
query
(
"CREATE TABLE franjas
(
id_franja serial NOT NULL,
franja varchar(2) NOT NULL,
hini varchar(5) NOT NULL,
CONSTRAINT franja_pkey PRIMARY KEY (id_franja)
)"
)
;
if
(
!
$conecto
)
{
return
(
"0"
)
;
break
;
}
$horas
=5
;
for
(
$ns
=0
;
$ns
<21
;
$ns
++
)
{
$franja
=
$ns
+1
;
if
(
$ns
%2==0
)
{
$minutos
=
'00'
;
++
$horas
;
}
else
{
$minutos
=
'30'
;
}
$hini
=
$horas
.
':'
.
$minutos
;
$inserta
=
pg_query
(
"
insert into franjas(franja,hini) values('
$franja
','
$hini
')
"
)
;
}
$conecto
->
query
(
"CREATE TABLE mensajes
(
id serial NOT NULL,
remitente varchar(8) NOT NULL,
destinatario varchar(8) NOT NULL,
asunto varchar(250) NOT NULL,
mensaje text NOT NULL,
lectura boolean NOT NULL,
borrador boolean NOT NULL,
fecha timestamp NOT NULL,
tipo varchar(2) NOT NULL,
ocultorec boolean NOT NULL,
ocultoenv boolean NOT NULL,
CONSTRAINT mensajes_pkey PRIMARY KEY (id),
CONSTRAINT mensajes_fkey FOREIGN KEY (destinatario)REFERENCES usuarios (id_user),
CONSTRAINT mensajes2_fkey FOREIGN KEY (remitente)REFERENCES usuarios (id_user)
)"
)
;
if
(
!
$conecto
)
{
return
(
"0"
)
;
break
;
}
$conecto
->
query
(
"CREATE TABLE todosmensajes
(
id int8 NOT NULL default '0',
destinatario varchar(6) NOT NULL,
CONSTRAINT todosmensajes_pkey PRIMARY KEY (id, destinatario)
)"
)
;
if
(
!
$conecto
)
{
return
(
"0"
)
;
break
;
}
$conecto
->
query
(
"CREATE TABLE planes_asignados
(
id_planes serial NOT NULL,
pa_created timestamp NOT NULL DEFAULT now(),
pa_createdby numeric(10) NOT NULL,
pa_updated timestamp NOT NULL DEFAULT now(),
pa_updatedby numeric(10) NOT NULL,
id_user numeric(10) NOT NULL,
id_plan numeric(10) NOT NULL,
CONSTRAINT planes_asignados_pkey PRIMARY KEY (id_planes),
CONSTRAINT planes_asignados_fkey FOREIGN KEY (id_user)REFERENCES usuarios (id_user)
ON UPDATE CASCADE ON DELETE NO ACTION,
CONSTRAINT planes2_fkey FOREIGN KEY (id_plan)REFERENCES planes (id_plan)
ON UPDATE CASCADE ON DELETE NO ACTION
)"
)
;
if
(
!
$conecto
)
{
return
(
"0"
)
;
break
;
}
$conecto
->
respaldo
(
'planes_asignados'
,
'id_planes'
,
'pa_updatedby'
)
;
$conecto
->
respaldo_borrar
(
'planes_asignados'
,
'id_planes'
,
'pa_updatedby'
)
;
$conecto
->
query
(
"CREATE OR REPLACE VIEW usu_pers as (select * from usuarios natural join personas)"
)
;
if
(
!
$conecto
)
{
return
(
"0"
)
;
break
;
}
$conecto
->
query
(
"CREATE OR REPLACE VIEW plan_asigna as (select * from usu_pers natural join planes_asignados)"
)
;
if
(
!
$conecto
)
{
return
(
"0"
)
;
break
;
}
$conecto
->
query
(
"CREATE OR REPLACE VIEW plan_planes as (select * from planes_asignados natural join planes)"
)
;
if
(
!
$conecto
)
{
return
(
"0"
)
;
break
;
}
$conecto
->
query
(
"CREATE OR REPLACE VIEW perfil_menus_ver as (select * from perfil_menus natural join menus)"
)
;
if
(
!
$conecto
)
{
return
(
"0"
)
;
break
;
}
$conecto
->
query
(
"
insert into perfiles(name, createdby_per, created_per, updatedby_per, updated_per) values('1','0','
$hoy
','0','
$hoy
' )
"
)
;
if
(
!
$conecto
)
{
return
(
"0"
)
;
break
;
}
$clave_crypt
=
crypt
(
$clave
,
'preadmin'
)
;
$conecto
->
query
(
"
insert into personas(dni, firstname, lastname, adress, phone, email, createdby_p, created_p, updatedby_p, updated_p) values('
$dni_admin
','
$nombre_admin
','
$apellidos_admin
','
$direc_admin
','
$telf_admin
','
$mail_admin
','0','
$hoy
','0','
$hoy
' )
"
)
;
if
(
!
$conecto
)
{
return
(
"0"
)
;
break
;
}
$result
=
$conecto
->
query_c
(
"
select id_persona from personas where dni='
$dni_admin
'
"
)
;
count
(
$result
)
;
$record
=
$result
[
0
]
;
$id_poner
=
$record
[
'id_persona'
]
;
$conecto
->
query
(
"
insert into usuarios(id_persona, username , clave, rol, created_u, createdby_u, updated_u, updatedby_u) values('
$id_poner
','
$administrador
','
$clave_crypt
' ,'1', '
$hoy
','0', '
$hoy
','0')
"
)
;
if
(
!
$conecto
)
{
return
(
"0"
)
;
break
;
}
/**
* menus por defecto
*
*/
$conecto
->
query
(
"
insert into menus (name, ispadre, id_padre, funtion, orden, created_me, createdby_me, updated_me, updatedby_me) values ('raiz', 'Y', 1, 'no','0','
$hoy
', 0,'
$hoy
', 0)
"
)
;
if
(
!
$conecto
)
{
return
(
"0"
)
;
break
;
}
/**
* usuarios
*
*/
$conecto
->
query
(
"
insert into menus (name, ispadre, id_padre, funtion, orden, created_me, createdby_me, updated_me, updatedby_me) values ('usuarios', 'Y', 1, 'MostrarFilas(\'X\')','1','
$hoy
', 0,'
$hoy
', 0)
"
)
;
if
(
!
$conecto
)
{
return
(
"0"
)
;
break
;
}
$conecto
->
query
(
"
insert into menus (name, ispadre, id_padre, funtion, orden, created_me, createdby_me, updated_me, updatedby_me) values ('agrega', 'N', 2, 'docAgrega()','1','
$hoy
', 0,'
$hoy
', 0)
"
)
;
if
(
!
$conecto
)
{
return
(
"0"
)
;
break
;
}
$conecto
->
query
(
"
insert into menus (name, ispadre, id_padre, funtion, orden, created_me, createdby_me, updated_me, updatedby_me) values ('edita', 'N', 2, 'edita()','2','
$hoy
', 0,'
$hoy
', 0)
"
)
;
if
(
!
$conecto
)
{
return
(
"0"
)
;
break
;
}
$conecto
->
query
(
"
insert into menus (name, ispadre, id_padre, funtion, orden, created_me, createdby_me, updated_me, updatedby_me) values ('baja', 'N', 2, 'eliminasuarios()','3','
$hoy
', 0,'
$hoy
', 0)
"
)
;
if
(
!
$conecto
)
{
return
(
"0"
)
;
break
;
}
$conecto
->
query
(
"
insert into menus (name, ispadre, id_padre, funtion, orden, created_me, createdby_me, updated_me, updatedby_me) values ('listar', 'N', 2, 'abrePdf(\'usuarios_pdf.php\')','4','
$hoy
', 0,'
$hoy
', 0)
"
)
;
if
(
!
$conecto
)
{
return
(
"0"
)
;
break
;
}
$conecto
->
query
(
"
insert into menus (name, ispadre, id_padre, funtion, orden, created_me, createdby_me, updated_me, updatedby_me) values ('password', 'N', 2, 'cambiarpw()','5','
$hoy
', 0,'
$hoy
', 0)
"
)
;
if
(
!
$conecto
)
{
return
(
"0"
)
;
break
;
}
/**
* bd
*
*/
$conecto
->
query
(
"
insert into menus (name, ispadre, id_padre, funtion, orden, created_me, createdby_me, updated_me, updatedby_me) values ('bd', 'Y', 1, 'MostrarFilas(\'X\')','2','
$hoy
', 0,'
$hoy
', 0)
"
)
;
if
(
!
$conecto
)
{
return
(
"0"
)
;
break
;
}
$conecto
->
query
(
"
insert into menus (name, ispadre, id_padre, funtion, orden, created_me, createdby_me, updated_me, updatedby_me) values ('vacuum', 'N', 8, 'vacuum()','1','
$hoy
', 0,'
$hoy
', 0)
"
)
;
if
(
!
$conecto
)
{
return
(
"0"
)
;
break
;
}
$conecto
->
query
(
"
insert into menus (name, ispadre, id_padre, funtion, orden, created_me, createdby_me, updated_me, updatedby_me) values ('reindexa', 'N', 8, 'reindex()','2','
$hoy
', 0,'
$hoy
', 0)
"
)
;
if
(
!
$conecto
)
{
return
(
"0"
)
;
break
;
}
$conecto
->
query
(
"
insert into menus (name, ispadre, id_padre, funtion, orden, created_me, createdby_me, updated_me, updatedby_me) values ('copia', 'N', 8, 'copiaseg()','3','
$hoy
', 0,'
$hoy
', 0)
"
)
;
if
(
!
$conecto
)
{
return
(
"0"
)
;
break
;
}
/**
* mensajes
*
*/
$conecto
->
query
(
"
insert into menus (name, ispadre, id_padre, funtion, orden, created_me, createdby_me, updated_me, updatedby_me) values ('mios', 'Y', 1, 'MostrarFilas(\'X\')','3','
$hoy
', 0,'
$hoy
', 0)
"
)
;
if
(
!
$conecto
)
{
return
(
"0"
)
;
break
;
}
$conecto
->
query
(
"
insert into menus (name, ispadre, id_padre, funtion, orden, created_me, createdby_me, updated_me, updatedby_me) values ('redacta', 'N', 12, 'redactaMensaje()','1','
$hoy
', 0,'
$hoy
', 0)
"
)
;
if
(
!
$conecto
)
{
return
(
"0"
)
;
break
;
}
$conecto
->
query
(
"
insert into menus (name, ispadre, id_padre, funtion, orden, created_me, createdby_me, updated_me, updatedby_me) values ('recibidos', 'N', 12, 'recibidosMensaje()','2','
$hoy
', 0,'
$hoy
', 0)
"
)
;
if
(
!
$conecto
)
{
return
(
"0"
)
;
break
;
}
$conecto
->
query
(
"
insert into menus (name, ispadre, id_padre, funtion, orden, created_me, createdby_me, updated_me, updatedby_me) values ('enviados', 'N', 12, 'enviadosMensaje()','3','
$hoy
', 0,'
$hoy
', 0)
"
)
;
if
(
!
$conecto
)
{
return
(
"0"
)
;
break
;
}
$conecto
->
query
(
"
insert into menus (name, ispadre, id_padre, funtion, orden, created_me, createdby_me, updated_me, updatedby_me) values ('borrador', 'N', 12, 'borradorMensaje()','4','
$hoy
', 0,'
$hoy
', 0)
"
)
;
if
(
!
$conecto
)
{
return
(
"0"
)
;
break
;
}
$conecto
->
query
(
"
insert into menus (name, ispadre, id_padre, funtion, orden, created_me, createdby_me, updated_me, updatedby_me) values ('bandeja', 'N', 12, 'miraMensajes()','5','
$hoy
', 0,'
$hoy
', 0)
"
)
;
if
(
!
$conecto
)
{
return
(
"0"
)
;
break
;
}
/**
* asignamos al perfil admon
*
*/
for
(
$y
=2
;
$y
<18
;
$y
++
)
{
$conecto
->
query
(
"
insert into perfil_menus(id_menu, id_perfil, createdby_peme, created_peme, updatedby_peme, updated_peme) values('
$y
','1','0','
$hoy
','0','
$hoy
' )
"
)
;
if
(
!
$conecto
)
{
return
(
"0"
)
;
break
;
}
}
$conecto
->
commit
(
)
;
}
else
{
if
(
$conexion
!=0
)
{
return
(
"creado"
)
;
}
else
{
return
(
"0"
)
;
}
}
}
?>
Documentation generated on Fri, 10 Oct 2008 11:49:06 +0200 by
phpDocumentor 1.4.1