Source for file gestione_registro.inc.php
Documentation is available at gestione_registro.inc.php
* Sono qui presenti numerose funzioni per la gestione del registro di VFront.
* Il file viene incluso da /admin/gestione_tabelle_gruppi.php
* @desc Libreria di funzioni per la gestione del registro di VFront
* @subpackage Administration
* @author M.Marcello Verona
* @copyright 2007 M.Marcello Verona
* @see gestione_tabelle_gruppi.php
* @license http://www.gnu.org/licenses/gpl.html GNU Public License
require_once("../inc/conn.php");
require_once("../inc/func.comuni.php");
require_once("../inc/func.frontend.php");
* Funzione di inserimento di una tabella nel registro.
* Questa funzione scrive nelle tabelle registro_tab e registro_col
* informazioni su una tabella del database, recuperate a sua volta dall'information_schema
* @param string $val Nome della tabella
* @param int $gid Identificativo del gruppo
// Verifica che non ci siano già record per questo gruppo
$test_gid= vmsql_test_id($link,"gid",$gid,"{ $db1['frontend']}.registro_tab", "AND table_name='{$val['table_name']}'");
$sql_tab_dett= "INSERT INTO {$db1['frontend']}.registro_tab (
"'". $val['table_name']. "',".
"'". $val['table_type']. "',".
"'". $val['table_comment']. "')";
$ID_TABLE = vmsql_insert_id($link,"{ $db1['frontend']}. registro_tab",'id_table');
#########################
if($db1['dbtype']== 'postgres'){
character_maximum_length,
CASE WHEN column_default LIKE 'nextval(%' THEN 'auto_increment' ELSE '' END AS extra,
col_description(a.attrelid, a.attnum::integer) AS column_comment
FROM information_schema.columns i, pg_attribute a, pg_class c, pg_namespace ns
WHERE i.table_schema='{$db1['dbname']}'
AND i.table_name='". $val['table_name']. "'
AND c.relname=i.table_name
AND ns.oid=c.relnamespace
AND a.attname = i.column_name
ORDER BY table_name ASC, ordinal_position ASC ";
character_maximum_length,
SUBSTRING_INDEX(column_comment,'InnoDB',1) as column_comment
FROM information_schema.columns
WHERE table_schema='{$db1['dbname']}'
AND table_name='". $val['table_name']. "'
ORDER BY table_name ASC, ordinal_position ASC ";
foreach ($matrice1 as $k=> $valori){
$sql2= "INSERT INTO {$db1['frontend']}.registro_col (
character_maximum_length,
"'". $valori['column_name']. "',".
intval($valori['ordinal_position']). ",".
"'". $valori['column_default']. "',".
"'". $valori['is_nullable']. "',".
"'". $valori['column_type']. "',".
intval($valori['character_maximum_length']). ",".
"'". $valori['data_type']. "',".
"'". $valori['extra']. "',".
"'". $valori['column_comment']. "')";
* Funzione di clonazione registro.
* Prende le impostazioni del gruppo $gid_old (il gruppo default è 0)
* e le applica al nuovo gruppo $gid_new
* @param int $gid_new ID del nuovo gruppo
* @param int $gid_old ID del gruppo origine
* @return bool Esito dell'operazione
// Pulisco le tabelle temporanee
// Se è una sola tabella da copiare...
$vincolo_sola_tabella= ($solo_tabella> 0)? " AND id_table=". intval($solo_tabella). " " : "";
$sql_tab1 = "SELECT id_table,
FROM {$db1['frontend']}.registro_tab
WHERE gid=". intval($gid_old). "
// Preparo l'inserimento del master
$sql_ins1= sprintf("INSERT INTO {$db1['frontend']}.registro_tab (gid,visibile,in_insert,in_duplica,in_update,in_delete,in_export,
data_modifica,orderby,table_name,table_type,commento,orderby_sort,
permetti_allegati,permetti_allegati_ins,permetti_allegati_del,permetti_link,permetti_link_ins,permetti_link_del,
VALUES (%d,%d,%d,%d,%d,%d,%d,
%d,'%s','%s','%s','%s','%s',
%d,%d,%d,%d,%d,%d,'%s','%s')",
$RS1['permetti_allegati'],
$RS1['permetti_allegati_ins'],
$RS1['permetti_allegati_del'],
$RS1['permetti_link_ins'],
$RS1['permetti_link_del'],
$ID_NEW_TABLE = vmsql_insert_id($link,"{ $db1['frontend']}. registro_tab",'id_table');
// Prendi i valori del vecchio gid
$sql_col1 = "SELECT column_name,
character_maximum_length,
FROM {$db1['frontend']}.registro_col
WHERE gid=". intval($gid_old). "
AND id_table=". intval($RS1['id_table']). "
ORDER BY column_name, ordinal_position
// Inserisci i valori duplicati
// Preparo l'inserimento del master
$sql_ins1= sprintf("INSERT INTO {$db1['frontend']}.registro_col
(gid, id_table, column_name,
ordinal_position, column_default, is_nullable,
column_type, character_maximum_length, data_type,
extra, in_tipo, in_default,
in_visibile, in_richiesto, in_search,
in_suggest, in_table, in_ordine, jstest, commento)
$RS2['ordinal_position'],
$RS2['character_maximum_length'],
if(isset ($GLOBALS['VMSQL_ERROR'])){
* Dato un gid crea un registro tabelle per quel gruppo partendo da zero.
* Come inizializza registro ma non crea il record nel gruppo.
* @param int $gid Id del gruppo
* @return bool Esito dell'operazione
/*// Verifico l'esistenza del gruppo $gid
$test_gid_0 = vmsql_test_id($link,'gid',$gid,'{$db1['frontend']}.gruppo');
openErrorGenerico("Attenzione! Esiste già un gid con questo valore");
if($db1['dbtype']== 'postgres'){
$sql_tab= "SELECT table_name, table_type, obj_description(c.oid, 'pg_class'::name) AS comment
FROM information_schema.tables , pg_catalog.pg_class AS c
WHERE TABLE_SCHEMA='{$db1['dbname']}'
AND (TABLE_TYPE='BASE TABLE' OR TABLE_TYPE='VIEW')
$sql_tab= "SELECT table_name, table_type, SUBSTRING_INDEX(table_comment,'InnoDB',1) as comment
FROM information_schema.tables
WHERE TABLE_SCHEMA='{$db1['dbname']}'
AND (TABLE_TYPE='BASE TABLE' OR TABLE_TYPE='VIEW')
foreach ($matrice0 as $k=> $val){
if(isset ($GLOBALS['VMSQL_ERROR'])){
* Inizializzazione del registro.
* Viene creato il gid 0 e la prima copia del registro e viene eliminato il gruppo -1
* creato nell'installazione.
* Restituisce vero se l'operazione va a buon fine, falso se fallisce.
* @return bool Esito dell'operazione
// Verifico l'esistenza del gruppo 0
$test_gid_0 = vmsql_test_id($link,'gid','0',"{ $db1['frontend']}. gruppo");
$q_gruppo_0 = vmsql_query("INSERT INTO {$db1['frontend']}.gruppo (gid,nome_gruppo,descrizione_gruppo)
VALUES(0,'default','gruppo di default')",$link);
if($db1['dbtype']== 'postgres'){
$sql_tab= "SELECT table_name, table_type , obj_description(c.oid, 'pg_class'::name) AS comment
FROM information_schema.tables, pg_catalog.pg_class AS c
WHERE table_schema='{$db1['dbname']}'
AND (table_type='BASE TABLE' OR table_type='VIEW')
$sql_tab= "SELECT table_name, SUBSTRING_INDEX(table_comment,'InnoDB',1) as comment, table_type
FROM information_schema.tables
WHERE TABLE_SCHEMA='{$db1['dbname']}'
AND (TABLE_TYPE='BASE TABLE' OR TABLE_TYPE='VIEW')
foreach ($matrice0 as $k=> $val){
if(isset ($GLOBALS['VMSQL_ERROR'])){
// metti l'utente 1 (admin) nel gruppo 0
$q_ut= vmsql_query("UPDATE {$db1['frontend']}.utente SET gid=0 WHERE id_utente=1",$link);
$_SESSION['user']['gid']= 0;
// Elimina il gruppo temporaneo -1
$q_del_g= vmsql_query("DELETE FROM {$db1['frontend']}.gruppo WHERE gid=-1 ",$link);
* Funzione di aggiornamento dei registri di VFront.
* Vengono confrontate le tabelle attualmente presenti in registro e quelle presenti in information_schema,
* inserite nel registro quelle nuove e cancellate quelle obsolete
if($db1['dbtype']== "postgres"){
$sql_nuove= "SELECT t.table_name, t.table_type , obj_description(c.oid, 'pg_class'::name) AS comment
FROM information_schema.tables t
INNER JOIN pg_catalog.pg_class AS c ON t.table_name=c.relname
LEFT OUTER JOIN {$db1['frontend']}.registro_tab r ON t.table_name=r.table_name
WHERE t.table_schema='{$db1['dbname']}'
AND (t.table_type='BASE TABLE' OR t.table_type='VIEW')
AND t.table_name NOT IN (SELECT r.table_name FROM {$db1['frontend']}.registro_tab r)
$sql_nuove= "SELECT t.table_name , SUBSTRING_INDEX(t.table_comment,'InnoDB',1) as comment
FROM information_schema.tables t
LEFT OUTER JOIN {$db1['frontend']}.registro_tab r ON t.table_name=r.table_name
WHERE t.table_schema='{$db1['dbname']}'
AND (t.table_type='BASE TABLE' OR t.table_type='VIEW')
AND t.table_name NOT IN (SELECT r.table_name FROM {$db1['frontend']}.registro_tab r)";
// Prendi tabelle attualmente esistenti non presenti nel registro:
foreach($mat_nuove_tabelle as $k=> $val){
// Elimina le tabelle presenti nel registro e non più nel database
"SELECT r.id_table, r.table_name
FROM {$db1['frontend']}.registro_tab r
WHERE r.table_name NOT IN (
SELECT t.TABLE_NAME FROM information_schema.tables t
WHERE t.TABLE_SCHEMA='{$db1['dbname']}' AND (t.TABLE_TYPE='BASE TABLE' OR t.TABLE_TYPE='VIEW')",
// Elimina da registro_col
$q_del_col = vmsql_query("DELETE FROM {$db1['frontend']}.registro_col WHERE id_table=". intval($RS_del[0]),$link);
// Elimina da registro_tab
$q_del_col = vmsql_query("DELETE FROM {$db1['frontend']}.registro_tab WHERE id_table=". intval($RS_del[0]),$link);
if(isset ($GLOBALS['VMSQL_ERROR']) && $GLOBALS['VMSQL_ERROR']=== true){
* Inizializzazione (creazione) della sottomaschera.
* Viene inserita in registro la nuova sottomaschera e le sue caratteristiche di dettaglio.
* @param int $oid_parent ID della tabella padre (la maschera di cui questa sarà sottomaschera)
* @param string $tabella_sub Tabella che svolgerà il ruolo di sottomaschera
* @return bool Esito dell'operazione
// Inserimento dati minimi del master
$sql_ins_sub = "INSERT INTO ". $db1['frontend']. ".registro_submask (id_table,nome_tabella)
VALUES ('$oid_parent','$tabella_sub')";
// inserimento del dettaglio
$id_submask = vmsql_insert_id($link, $db1['frontend']. ".registro_submask" ,'id_submask');
// prendi info della tabella
#########################
if($db1['dbtype']== 'postgres'){
character_maximum_length,
col_description(a.attrelid, a.attnum::integer) AS column_comment
FROM information_schema.columns i, pg_attribute a, pg_class c, pg_namespace ns
WHERE table_schema='{$db1['dbname']}'
AND table_name='". $tabella_sub. "'
AND c.relname=i.table_name
AND ns.oid=c.relnamespace
AND a.attname = i.column_name
ORDER BY table_name ASC, ordinal_position ASC ";
character_maximum_length,
SUBSTRING_INDEX(column_comment,'InnoDB',1) as column_comment
FROM information_schema.columns
WHERE table_schema='{$db1['dbname']}'
AND table_name='". $tabella_sub. "'
ORDER BY table_name ASC, ordinal_position ASC ";
foreach ($matrice1 as $k=> $valori){
$sql2= "INSERT INTO {$db1['frontend']}.registro_submask_col (
character_maximum_length,
"'". $valori['column_name']. "',".
"'". $valori['ordinal_position']. "',".
"'". $valori['column_default']. "',".
"'". $valori['is_nullable']. "',".
"'". $valori['column_type']. "',".
intval($valori['character_maximum_length']). ",".
"'". $valori['data_type']. "',".
"'". $valori['extra']. "',".
"'". $valori['column_comment']. "')";
* Funzione di clonazione delle sottomaschere.
* Questa è un'utiliti per copiare una sottomaschera di un gruppo per un altro gruppo
* @param int $gid_new ID del gruppo destinazione
* @param int $gid_old ID del gruppo origine
* @param bool $solo_id_table
* @return bool Esito dell'operazione
$clausola_id_table = ($solo_id_table> 0) ? "AND t.id_table=". intval($solo_id_table) : "";
// prendi le tabelle che hanno sottomaschere da clonare
$q0 = vmsql_query("SELECT s.id_submask, s.id_table ,t.table_name
FROM ". $db1['frontend']. ".registro_submask s, ". $db1['frontend']. ".registro_tab t
AND s.id_table=t.id_table
ORDER BY t.table_name",$link);
return 0; // si ferma qui.
// ora prende l'array delle tabelle del nuovo gid
for($j= 0;$j< count($old_table_name);$j++ ){
$new_id_table[] = name2oid($old_table_name[$j],$gid_new);
# CICLO CLONAZIONE SUBMASK MASTER
for($i= 0;$i< count($old_id_submask);$i++ ){
// PRENDI UNA SOTTOMASCHERA
FROM ". $db1['frontend']. ".registro_submask
WHERE id_submask=". $old_id_submask[$i]. "
// ---> PREPARA LA QUERY DI INSERIMENTO
INSERT INTO ". $db1['frontend']. ".registro_submask
(id_table, sub_select, sub_insert, sub_update, sub_delete, nome_tabella,nome_frontend,
campo_pk_parent,campo_fk_sub, orderby_sub, orderby_sub_sort, data_modifica, max_records,tipo_vista)
VALUES (%d,%d,%d,%d,%d,'%s','%s',
'%s','%s','%s','%s',%d,%d,'%s')",
$RS1['orderby_sub_sort'],
// recupera l'id inserito.
$new_id_submask = vmsql_insert_id($link, $db1['frontend']. ".registro_submask",'id_submask');
// REcupera i dati del dettaglio
FROM ". $db1['frontend']. ".registro_submask_col
WHERE id_submask=". $old_id_submask[$i]. "
// Prepara la query di inserimento
$sql_ins2= sprintf("INSERT INTO ". $db1['frontend']. ".registro_submask_col
(id_submask, column_name, ordinal_position, column_default, is_nullable,
column_type, character_maximum_length, data_type, extra, in_tipo,
in_default, in_visibile, in_richiesto, commento)
'%s','%s','%s','%s','%s',
$RS2['ordinal_position'],
$RS2['character_maximum_length'],
* Funzione di manutenzione dei registri.
* Copia le sottomaschere per le viste
* @param int $id_vista_new
* @param int $id_tabella_old
# CICLO CLONAZIONE SUBMASK MASTER
// PRENDI UNA SOTTOMASCHERA
FROM ". $db1['frontend']. ".registro_submask
WHERE id_table=$id_tabella_old
// ---> PREPARA LA QUERY DI INSERIMENTO
INSERT INTO ". $db1['frontend']. ".registro_submask
(id_table, sub_select, sub_insert, sub_update, sub_delete, nome_tabella,nome_frontend,
campo_pk_parent,campo_fk_sub, orderby_sub, orderby_sub_sort, data_modifica, max_records,tipo_vista)
VALUES (%d,%d,%d,%d,%d,'%s','%s',
'%s','%s','%s','%s',%d,%d,'%s')",
$RS1['orderby_sub_sort'],
// recupera l'id inserito.
$new_id_submask = vmsql_insert_id($link,$db1['frontend']. ".registro_submask", "id_submask");
// REcupera i dati del dettaglio
FROM ". $db1['frontend']. ".registro_submask_col
WHERE id_submask=". $RS1['id_submask'],$link);
// Prepara la query di inserimento
$sql_ins2= sprintf("INSERT INTO ". $db1['frontend']. ".registro_submask_col
(id_submask, column_name, ordinal_position, column_default, is_nullable,
column_type, character_maximum_length, data_type, extra, in_tipo,
in_default, in_visibile, in_richiesto, commento)
'%s','%s','%s','%s','%s',
$RS2['ordinal_position'],
$RS2['character_maximum_length'],
* Funzionedi utility per le operazioni interne
* Copia le impostazioni dei campi di una tabella per un gruppo
* e le applica alla tabella per un altro gruppo
* @param int $id_table_fonte id_table della tabella fonte
* @param int $id_table_destinazione id_table della tabella destinazione
* @return bool Esito dell'operazione
// Prendi i valori del vecchio gid
$sql_col1 = "SELECT column_name,
FROM {$db1['frontend']}.registro_col
WHERE id_table=". intval($id_table_fonte). "
ORDER BY column_name, ordinal_position
// prende l'id_reg corrispettivo
$q_reg= vmsql_query("SELECT id_reg FROM {$db1['frontend']}.registro_col
WHERE id_table=". intval($id_table_destinazione). "
AND column_name='". $RS2['column_name']. "'",$link);
// Prepara la query di aggiornamento
$sql_up= sprintf("UPDATE {$db1['frontend']}.registro_col
* @param int $id_table_fonte
* @param int $id_table_dest
// Elimina eventuali vecchie sottomaschere
$q_del_sub = vmsql_query("DELETE FROM {$db1['frontend']}.registro_submask WHERE id_table=". intval($id_table_dest),$link);
* Funzione di sincronizzazione dei campi del registro frontend.
* Si associa a aggiorna registri, ma opera a livello di confronto di campo.
* @see function aggiorna_registri
* @param string $tipo_aggiornamento (UPDATE | INSERT | DELETE)
function aggiorna_campo($tabella,$campo,$tipo_aggiornamento= "UPDATE"){
if($tipo_aggiornamento== "UPDATE"){
// prendi gli id dal frontend
$sql_c= "SELECT c.id_reg, c.id_table FROM {$db1['frontend']}.registro_col c, {$db1['frontend']}.registro_tab t
WHERE c.id_table=t.id_table
AND t.table_name='$tabella'
AND c.column_name='$campo'";
// prendi le info del campo dal information_schema
$sql_i= "SELECT * FROM information_schema.columns
WHERE TABLE_SCHEMA='". $db1['dbname']. "'
AND TABLE_NAME='$tabella'
AND COLUMN_NAME='$campo'";
for($i= 0;$i< count($RSi);$i++ ){
$max_length= (is_numeric($RSi['character_maximum_length'])) ? $RSi['character_maximum_length'] : 'NULL';
if($db1['dbtype']== 'postgres'){
$sql_up= "UPDATE {$db1['frontend']}.registro_col
SET column_default='". addslashes($RSi['column_default']). "',
is_nullable='". $RSi['is_nullable']. "',
ordinal_position=". intval($RSi['ordinal_position']). ",
column_type='". $RSi['udt_name']. "',
character_maximum_length=". $max_length. ",
data_type='". $RSi['data_type']. "',
WHERE id_table=". intval($mat_c[$i]['id_table']). "
AND id_reg=". intval($mat_c[$i]['id_reg']). "
$sql_up= "UPDATE {$db1['frontend']}.registro_col
SET column_default='". $RSi['COLUMN_DEFAULT']. "',
is_nullable='". $RSi['IS_NULLABLE']. "',
ordinal_position='". $RSi['ORDINAL_POSITION']. "',
column_type='". $RSi['COLUMN_TYPE']. "',
character_maximum_length='". $RSi['CHARACTER_MAXIMUM_LENGTH']. "',
data_type='". $RSi['DATA_TYPE']. "',
WHERE id_table=". intval($mat_c[$i]['id_table']). "
AND id_reg=". intval($mat_c[$i]['id_reg']). "
// rpc_debug($sql_up,"../rpc.debug.txt");
else if($tipo_aggiornamento== "INSERT"){
// prendi le info del campo dal information_schema
$sql_i= "SELECT * FROM information_schema.columns
WHERE TABLE_SCHEMA='". $db1['dbname']. "'
AND TABLE_NAME='$tabella'
AND COLUMN_NAME='$campo'";
// prendi gli ID table da coinvolgere
$q_idt= vmsql_query("SELECT id_table,gid FROM {$db1['frontend']}.registro_tab
WHERE table_name='$tabella'",$link);
for($i= 0;$i< count($idtables);$i++ ){
if($db1['dbtype']== 'postgres'){
$sql_in= "INSERT INTO {$db1['frontend']}.registro_col
(id_table,gid,column_name,
column_default,is_nullable,ordinal_position,
column_type,character_maximum_length,data_type,in_visibile,
(". $idtables[$i]. ", ". $gids[$i]. " ,'$campo',
'". $RSi['column_default']. "','". $RSi['is_nullable']. "',". intval($RSi['ordinal_position']). ",
'". $RSi['column_type']. "',". intval($RSi['character_maximum_length']). ",'". $RSi['data_type']. "',0,
$sql_in= "INSERT INTO {$db1['frontend']}.registro_col
(id_table,gid,column_name,
column_default,is_nullable,ordinal_position,
column_type,character_maximum_length,data_type,in_visibile,
(". $idtables[$i]. ", ". $gids[$i]. " ,'$campo',
'". $RSi['COLUMN_DEFAULT']. "','". $RSi['IS_NULLABLE']. "','". $RSi['ORDINAL_POSITION']. "',
'". $RSi['COLUMN_TYPE']. "',". intval($RSi['CHARACTER_MAXIMUM_LENGTH']). ",'". $RSi['DATA_TYPE']. "',0,
else if($tipo_aggiornamento== "DELETE"){
// prendi le colonne coinvolte
$sql_del= "SELECT c.id_reg FROM {$db1['frontend']}.registro_col c, {$db1['frontend']}.registro_tab t
WHERE c.id_table=t.id_table
AND t.table_name='$tabella'
AND c.column_name='$campo'";
$sql_del2= "DELETE FROM {$db1['frontend']}.registro_col WHERE id_reg IN (". implode(",",$idregs). ")";
// rpc_debug($sql_del2,"../rpc.debug.txt");
|