Source for file m_index.php

Documentation is available at m_index.php

  1. <?php
  2. /**
  3. * Archivo modelo para el proceso de instalacion
  4. *
  5. @package m_index.php
  6. @copyright 2007 2008 Luis Jorge Martin Aznar
  7. @version 1.0
  8. @Review 1
  9. @author Luis Jorge Martin Aznar
  10. *  creacion= 20-noviembre-2007
  11. *  modificacion= 12-abril-2008
  12. *
  13. *  generator= gedit
  14. *
  15. *  This file is part of PreAdmin ©.
  16. *
  17. *     This program is free software: you can redistribute it and/or modify
  18. *     it under the terms of the GNU General Public License as published by
  19. *     the Free Software Foundation, either version 3 of the License.
  20. *
  21. *     This program is distributed in the hope that it will be useful,
  22. *     but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  24. *     GNU General Public License for more details.
  25. *
  26. *     You should have received a copy of the GNU General Public License
  27. *     along with this program.  If not, see <http://www.gnu.org/licenses/>.
  28. */
  29.  
  30. /**
  31. * Incluye el fichero de clases para postgres
  32. *
  33. */
  34. include_once('../../modelo/postgres.php');
  35.  
  36. /**
  37. * Funcion que crea los objetos de la base de datos
  38. *
  39. * Esta funcion crea los objetos de la base de datos. Esta funcion forma parte
  40. * de la capa modelo y como tal esta alojada en el directorio modelo local al directorio
  41. * "instalacion".
  42. @param  Es el array con los valores de los campos enviados por
  43. *  el formulario
  44. @return Si la creacion ha finalizado con exito retorna "creado"; en caso contrario retorna
  45. *  0.
  46. */
  47. function completarbd($campos){
  48.     $hoy date("Y-m-d H:i:s");
  49.     $servidor $campos['txt_servidor'];
  50.     $nom_bd $campos['txt_bd'];
  51.     $clave_postgre $campos['txt_clave_postgre'];
  52.     $usuario_postgre $campos['txt_usuario'];
  53.     $administrador $campos['txt_admin'];
  54.     $clave $campos['txt_clave'];
  55.     $nombre_admin $campos['nombre_admin'];
  56.     $apellidos_admin $campos['apellidos_admin'];
  57.     $dni_admin $campos['dni_admin'];
  58.     $direc_admin $campos['direc_admin'];
  59.     $telf_admin $campos['telf_admin'];
  60.     $mail_admin $campos['mail_admin'];
  61.  
  62.     
  63.  
  64.     $conecto=new bd();
  65.     $conexion=$conecto->conectar2($servidor,$clave_postgre,$usuario_postgre);
  66.     if(!$conexion){return("0");break}
  67.  
  68.     $conexion=$conecto->query ("DROP DATABASE $nom_bd");
  69.  
  70.     $conexion=$conecto->query ("CREATE DATABASE $nom_bd
  71.                   WITH OWNER = $usuario_postgre
  72.                        ENCODING = 'UTF8'
  73.                        TABLESPACE = pg_default");
  74.     if(!$conexion){return("0");break}
  75.     $conexion=$conecto-> cerrar();
  76.     
  77.  
  78.     $conecto=new bd();
  79.     $conexion=$conecto->conectar();
  80.     if ($conexion!="0")
  81.     {
  82.         $conecto->begin ();
  83.  
  84.         $conexion=$conecto->query ("CREATE TRUSTED LANGUAGE plpgsql
  85.                HANDLER language_handler_in");
  86.         if(!$conexion){return("0");break}
  87.  
  88.         /**
  89.         * Formato de fecha
  90.         *
  91.         */
  92.         $conecto->query ("SET TIME ZONE 'Europe/Rome';
  93.         SELECT CURRENT_TIMESTAMP AS ahora");
  94.         if(!$conecto){return("0");break}
  95.  
  96.         /**
  97.         * creo las tablas
  98.         *
  99.         */
  100.         $conecto->query ("CREATE TABLE personas
  101.         (
  102.           id_persona serial NOT NULL,
  103.           firstname varchar(60),
  104.           lastname varchar(60),
  105.           dni varchar(9) NOT NULL,
  106.           adress varchar(80),
  107.           cpostal varchar(6),
  108.           email varchar(40),
  109.           phone varchar(40),
  110.           fax varchar(40),
  111.           isactive_p char(1) NOT NULL DEFAULT 'Y',
  112.           created_p timestamp NOT NULL,
  113.           createdby_p numeric(10) NOT NULL,
  114.           updated_p timestamp NOT NULL,
  115.           updatedby_p numeric(10) NOT NULL,
  116.         CONSTRAINT personas_pkey PRIMARY KEY (id_persona),
  117.         CONSTRAINT personas_dni_key UNIQUE (dni)
  118.         )");
  119.         if(!$conecto){return("0");break}
  120.         $conecto->respaldo('personas','id_persona','updatedby_p');
  121.         $conecto->respaldo_borrar('personas','id_persona','updatedby_p');
  122.  
  123.         $conecto->query ("CREATE TABLE menus
  124.         (
  125.           id_menu serial NOT NULL,
  126.           name varchar(30),
  127.           ispadre char(1) NOT NULL DEFAULT 'Y',
  128.         id_padre integer NOT NULL DEFAULT 0,
  129.         funtion varchar(100) NOT NULL,
  130.         orden integer NOT NULL DEFAULT 0,
  131.           created_me timestamp NOT NULL,
  132.           createdby_me numeric(10) NOT NULL,
  133.           updated_me timestamp NOT NULL,
  134.           updatedby_me numeric(10) NOT NULL,
  135.         CONSTRAINT menus_pkey PRIMARY KEY (id_menu),
  136.         CONSTRAINT menus_fkey FOREIGN KEY (id_padre)
  137.               REFERENCES menus (id_menu)
  138.               ON UPDATE CASCADE ON DELETE NO ACTION
  139.         )");
  140.         if(!$conecto){return("0");break}
  141.         $conecto->respaldo('menus','id_menu','updatedby_me');
  142.         $conecto->respaldo_borrar('menus','id_menu','updatedby_me');
  143.  
  144.         $conecto->query ("CREATE TABLE perfiles
  145.         (
  146.           id_perfil serial NOT NULL,
  147.         name varchar(60),
  148.         created_per timestamp NOT NULL,
  149.           createdby_per numeric(10) NOT NULL,
  150.           updated_per timestamp NOT NULL,
  151.           updatedby_per numeric(10) NOT NULL,
  152.         CONSTRAINT perfiles_pkey PRIMARY KEY (id_perfil)
  153.         )");
  154.         if(!$conecto){return("0");break}
  155.         $conecto->respaldo('perfiles','id_perfil','updatedby_per');
  156.         $conecto->respaldo_borrar('perfiles','id_perfil','updatedby_per');
  157.         
  158.         $conecto->query ("CREATE TABLE perfil_menus
  159.         (
  160.           id_perfil_menu serial NOT NULL,
  161.         id_menu numeric(10) NOT NULL,
  162.         id_perfil numeric(10) NOT NULL,
  163.         created_peme timestamp NOT NULL,
  164.           createdby_peme numeric(10) NOT NULL,
  165.           updated_peme timestamp NOT NULL,
  166.           updatedby_peme numeric(10) NOT NULL,
  167.         CONSTRAINT perfil_menus_pkey PRIMARY KEY (id_perfil_menu),
  168.         CONSTRAINT perfil_menus_fkey FOREIGN KEY (id_menu)
  169.               REFERENCES menus (id_menu)
  170.         ON UPDATE CASCADE ON DELETE NO ACTION,
  171.         CONSTRAINT perfil_menus2_fkey FOREIGN KEY (id_perfil)
  172.               REFERENCES perfiles (id_perfil)
  173.         ON UPDATE CASCADE ON DELETE NO ACTION
  174.         )");
  175.         if(!$conecto){return("0");break}
  176.         $conecto->respaldo('perfil_menus','id_perfil_menu','updatedby_peme');
  177.         $conecto->respaldo_borrar('perfil_menus','id_perfil_menu','updatedby_peme');
  178.         
  179.  
  180.         $conecto->query ("CREATE TABLE historial
  181.         (
  182.           id_historial serial NOT NULL,
  183.           created timestamp NOT NULL DEFAULT now(),
  184.           createdby numeric(10) NOT NULL,
  185.           tabla varchar(60),
  186.           campo varchar(60),
  187.           id numeric(10) NOT NULL,
  188.           oldvalue text,
  189.           newvalue text,
  190.           CONSTRAINT historial1_pkey PRIMARY KEY (id_historial)
  191.         )");
  192.         if(!$conecto){return("0");break}
  193.  
  194.  
  195.         $conecto->query ("CREATE TABLE planes
  196.         (
  197.           id_plan serial NOT NULL,
  198.           planname varchar(60) NOT NULL,
  199.           isactive char(1) NOT NULL DEFAULT 'Y',
  200.           isblocked char(1) NOT NULL DEFAULT 'Y',
  201.         is_evaini_end char(1) NOT NULL DEFAULT 'N',
  202.           created timestamp NOT NULL DEFAULT now(),
  203.           createdby numeric(10) NOT NULL,
  204.           updated timestamp NOT NULL DEFAULT now(),
  205.           updatedby numeric(10) NOT NULL,
  206.         date_report date NOT NULL DEFAULT now(),
  207.         servicio_report varchar(60) NOT NULL,
  208.         CONSTRAINT planes_pkey PRIMARY KEY (id_plan),
  209.         CONSTRAINT nombre_ukey UNIQUE(planname)
  210.         )");
  211.         if(!$conecto){return("0");break}
  212.         $conecto->respaldo('planes','id_plan','updatedby');
  213.         $conecto->respaldo_borrar('planes','id_plan','updatedby');
  214.  
  215.         $conecto->query ("CREATE TABLE usuarios
  216.         (
  217.           id_user serial NOT NULL,
  218.           id_persona int8 NOT NULL,
  219.           username varchar(60) NOT NULL,
  220.           clave varchar(30),
  221.           rol  int2 NOT NULL,
  222.           isactive_u char(1) NOT NULL DEFAULT 'Y',
  223.           isblocked char(1) NOT NULL DEFAULT 'N',
  224.           created_u timestamp NOT NULL DEFAULT now(),
  225.           createdby_u numeric(10) NOT NULL,
  226.           updated_u timestamp NOT NULL DEFAULT now(),
  227.           updatedby_u numeric(10) NOT NULL,
  228.         cont_int int2 DEFAULT 0,
  229.         memoplan numeric(10)DEFAULT 0,
  230.         date_password date NOT NULL DEFAULT now(),
  231.         CONSTRAINT usuarios_pkey PRIMARY KEY (id_user),
  232.         CONSTRAINT usuarios_fkey FOREIGN KEY (id_persona)
  233.               REFERENCES personas (id_persona)
  234.               ON UPDATE CASCADE ON DELETE NO ACTION,
  235.         CONSTRAINT usuarios2_fkey FOREIGN KEY (rol)
  236.               REFERENCES perfiles (id_perfil )
  237.               ON UPDATE CASCADE ON DELETE NO ACTION,
  238.         CONSTRAINT usuarios_username_key UNIQUE (username)
  239.         )");
  240.         if(!$conecto){return("0");break}
  241.         $conecto->respaldo('usuarios','id_user','updatedby_u');
  242.         $conecto->respaldo_borrar('usuarios','id_user','updatedby_u');
  243.  
  244.         $conecto->query ("CREATE TABLE registro_entradas
  245.         (
  246.           id_registro serial NOT NULL,
  247.           username varchar(60) NOT NULL,
  248.           ip varchar(30),
  249.         host varchar(255),
  250.         exito char(1) NOT NULL DEFAULT 'N',
  251.         fecha_entrada timestamp NOT NULL DEFAULT now(),
  252.         CONSTRAINT registro_entradas_pkey PRIMARY KEY (id_registro),
  253.         CONSTRAINT registro_entradas_fkey FOREIGN KEY (username)
  254.               REFERENCES usuarios (username) ON UPDATE CASCADE ON DELETE NO ACTION
  255.         )");
  256.         if(!$conecto){return("0");break}
  257.  
  258.  
  259.  
  260.         $conecto->query ("CREATE TABLE agenda
  261.         (
  262.          id serial NOT NULL,
  263.           id_user numeric(10) NOT NULL,
  264.           fecha date NOT NULL,
  265.           cita text NOT NULL,
  266.           tipo char(1) NOT NULL,
  267.           franja varchar(2) NOT NULL,
  268.           dia int4 NOT NULL,
  269.         CONSTRAINT agenda_pkey PRIMARY KEY (id),
  270.         CONSTRAINT agenda_fkey FOREIGN KEY (id_user)REFERENCES usuarios (id_user)
  271.         )");
  272.         if(!$conecto){return("0");break}
  273.  
  274.         $conecto->query ("CREATE TABLE franjas
  275.         (
  276.           id_franja serial NOT NULL,
  277.           franja varchar(2) NOT NULL,
  278.           hini varchar(5) NOT NULL,
  279.           CONSTRAINT franja_pkey PRIMARY KEY (id_franja)
  280.         )");
  281.         if(!$conecto){return("0");break}
  282.  
  283.  
  284.         $horas=5;
  285.         for($ns=0;$ns<21;$ns++)    
  286.         {
  287.             $franja=$ns+1;
  288.             if($ns%2==0){
  289.                 $minutos='00';
  290.                 ++$horas;
  291.             }else{
  292.                 $minutos='30';
  293.             }
  294.         $hini=$horas.':'.$minutos;
  295.         $inserta pg_query("insert into franjas(franja,hini) values('$franja','$hini')");
  296.         }
  297.  
  298.  
  299.  
  300.         $conecto->query ("CREATE TABLE mensajes
  301.         (
  302.           id serial NOT NULL,
  303.           remitente varchar(8) NOT NULL,
  304.           destinatario varchar(8) NOT NULL,
  305.           asunto varchar(250) NOT NULL,
  306.           mensaje text NOT NULL,
  307.           lectura boolean NOT NULL,
  308.           borrador boolean NOT NULL,
  309.           fecha timestamp NOT NULL,
  310.           tipo varchar(2) NOT NULL,
  311.           ocultorec boolean NOT NULL,
  312.           ocultoenv boolean NOT NULL,
  313.         CONSTRAINT mensajes_pkey PRIMARY KEY (id),
  314.         CONSTRAINT mensajes_fkey FOREIGN KEY (destinatario)REFERENCES usuarios (id_user),
  315.         CONSTRAINT mensajes2_fkey FOREIGN KEY (remitente)REFERENCES usuarios (id_user)
  316.         )");
  317.         if(!$conecto){return("0");break}
  318.  
  319.  
  320.         $conecto->query ("CREATE TABLE todosmensajes
  321.         (
  322.           id int8 NOT NULL default '0',
  323.           destinatario varchar(6) NOT NULL,
  324.           CONSTRAINT todosmensajes_pkey PRIMARY KEY (id, destinatario)
  325.         )");
  326.         if(!$conecto){return("0");break}
  327.  
  328.  
  329.         $conecto->query ("CREATE TABLE planes_asignados
  330.         (
  331.           id_planes serial NOT NULL,
  332.           pa_created timestamp NOT NULL DEFAULT now(),
  333.           pa_createdby numeric(10) NOT NULL,
  334.           pa_updated timestamp NOT NULL DEFAULT now(),
  335.           pa_updatedby numeric(10) NOT NULL,
  336.         id_user numeric(10) NOT NULL,
  337.         id_plan numeric(10) NOT NULL,
  338.         CONSTRAINT planes_asignados_pkey PRIMARY KEY (id_planes),
  339.         CONSTRAINT planes_asignados_fkey FOREIGN KEY (id_user)REFERENCES usuarios (id_user)
  340.         ON UPDATE CASCADE ON DELETE NO ACTION,
  341.         CONSTRAINT planes2_fkey FOREIGN KEY (id_plan)REFERENCES planes (id_plan)
  342.         ON UPDATE CASCADE ON DELETE NO ACTION
  343.         )");
  344.         if(!$conecto){return("0");break}
  345.         $conecto->respaldo('planes_asignados','id_planes','pa_updatedby');
  346.         $conecto->respaldo_borrar('planes_asignados','id_planes','pa_updatedby');
  347.  
  348.         $conecto->query ("CREATE OR REPLACE VIEW usu_pers as (select * from usuarios natural join personas)");
  349.         if(!$conecto){return("0");break}
  350.         $conecto->query ("CREATE OR REPLACE VIEW plan_asigna as (select * from usu_pers natural join planes_asignados)");
  351.         if(!$conecto){return("0");break}
  352.         $conecto->query ("CREATE OR REPLACE VIEW plan_planes as (select * from planes_asignados natural join planes)");
  353.         if(!$conecto){return("0");break}
  354.         $conecto->query ("CREATE OR REPLACE VIEW perfil_menus_ver as (select * from perfil_menus natural join menus)");
  355.         if(!$conecto){return("0");break}
  356.     
  357.         $conecto->query ("insert into perfiles(name, createdby_per, created_per, updatedby_per, updated_per) values('1','0','$hoy','0','$hoy' )");
  358.         if(!$conecto){return("0");break}
  359.         $clave_crypt=crypt($clave,'preadmin');
  360.             $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' )");
  361.             if(!$conecto){return("0");break}
  362.             $result $conecto->query_c("select id_persona from personas where dni='$dni_admin'");
  363.             count($result);
  364.             $record=$result[0];
  365.             $id_poner=$record['id_persona'];
  366.             $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')");
  367.             if(!$conecto){return("0");break}
  368.         /**
  369.         * menus por defecto
  370.         *
  371.         */
  372.         $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)");
  373.         if(!$conecto){return("0");break}
  374.         /**
  375.         * usuarios
  376.         *
  377.         */
  378.         $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)");
  379.         if(!$conecto){return("0");break}
  380.             $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)");
  381.             if(!$conecto){return("0");break}
  382.             $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)");
  383.             if(!$conecto){return("0");break}
  384.             $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)");
  385.             if(!$conecto){return("0");break}
  386.             $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)");
  387.             if(!$conecto){return("0");break}
  388.             $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)");
  389.             if(!$conecto){return("0");break}
  390.         /**
  391.         * bd
  392.         *
  393.         */    
  394.         $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)");
  395.         if(!$conecto){return("0");break}
  396.             $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)");
  397.             if(!$conecto){return("0");break}
  398.             $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)");
  399.             if(!$conecto){return("0");break}
  400.             $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)");
  401.             if(!$conecto){return("0");break}
  402.  
  403.         /**
  404.         * mensajes
  405.         *
  406.         */    
  407.         $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)");
  408.         if(!$conecto){return("0");break}
  409.             $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)");
  410.             if(!$conecto){return("0");break}
  411.             $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)");
  412.             if(!$conecto){return("0");break}
  413.             $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)");
  414.             if(!$conecto){return("0");break}
  415.             $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)");
  416.             if(!$conecto){return("0");break}
  417.             $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)");
  418.             if(!$conecto){return("0");break}
  419.     
  420.         /**
  421.         * asignamos al perfil admon
  422.         *
  423.         */
  424.         for ($y=2;$y<18;$y++)
  425.         {
  426.         $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' )");
  427.         if(!$conecto){return("0");break}
  428.         }
  429.         $conecto->commit();
  430.         }else{
  431.         if ($conexion!=0){return ("creado");}else{return("0");}
  432.     }
  433. }
  434. ?>

Documentation generated on Fri, 10 Oct 2008 11:49:06 +0200 by phpDocumentor 1.4.1