phpDocumentor VFront
Function-Libraries
[ class tree: VFront ] [ index: VFront ] [ all elements ]

Source for file func.frontend.php

Documentation is available at func.frontend.php

  1. <?php
  2. /**
  3. * Libreria di funzioni di gestione del database di regole di VFront.
  4. * Sono qui scritte le funzioni di gestione dei registri
  5. * e di utilità per la lettura e scrittura delle tabelle di sistema
  6. @package VFront
  7. @subpackage Function-Libraries
  8. @author Mario Marcello Verona <marcelloverona@gmail.com>
  9. @copyright 2007 Mario Marcello Verona
  10. @version 0.90
  11. @license http://www.gnu.org/licenses/gpl.html GNU Public License
  12. */
  13.  
  14. /**
  15.  * Da l'oid di tabella restituisce il nome della stessa
  16.  * 
  17.  * @param int $oid 
  18.  * @param bool $solo_visibili 
  19.  * @return string 
  20.  */
  21. function oid2name($oid=0,$solo_visibili=false){
  22.     
  23.     global $link,$db1;
  24.     
  25.     $oid= (int) $oid;
  26.     
  27.     if($oid>0){
  28.         
  29.         $sql_vis ($solo_visibili"AND visibile=1" "";
  30.         
  31.         $q=vmsql_query("SELECT table_name 
  32.                         FROM {$db1['frontend']}.registro_tab
  33.                         WHERE id_table=$oid
  34.                         $sql_vis
  35.                         LIMIT 1",$link);
  36.         
  37.         if(vmsql_num_rows($q)==1){
  38.         
  39.             list($nome_tabellavmsql_fetch_row($q);
  40.         
  41.             return $nome_tabella;
  42.         }
  43.         else{
  44.             
  45.             return 0;
  46.         }
  47.     }
  48.     else{
  49.     
  50.         return 0;    
  51.     }
  52. }
  53.  
  54.  
  55.  
  56. /**
  57.  * Da nome tabella e gruppo restituisce l'id tabella (oid)
  58.  *
  59.  * @param string $name 
  60.  * @param int $gid 
  61.  * @return int 
  62.  */
  63. function name2oid($name,$gid=0){
  64.     
  65.     global $link,$db1;
  66.     
  67.     $gid= (int) $gid;
  68.     
  69.         
  70.         $q=vmsql_query("SELECT id_table 
  71.                         FROM {$db1['frontend']}.registro_tab
  72.                         WHERE table_name='$name'
  73.                         AND gid=$gid
  74.                         LIMIT 1",$link);
  75.         
  76.         if(vmsql_num_rows($q)==1){
  77.         
  78.             list($oidvmsql_fetch_row($q);
  79.         
  80.             return $oid;
  81.         }
  82.         else{
  83.             
  84.             return 0;
  85.         }
  86. }
  87.  
  88. /**
  89.  * Prendi le tabelle presenti nel registro
  90.  *
  91.  * @param bool $solo_visibili 
  92.  * @return array 
  93.  */
  94. function prendi_tabelle($gid=0,$solo_visibili=false){
  95.     
  96.     global $link,$db1;
  97.     
  98.         
  99.         $sql_vis ($solo_visibili"AND visibile=1" "";
  100.         
  101.         $gid= (int) $gid;
  102.         
  103.         $q=vmsql_query("SELECT id_tabletable_name 
  104.                         FROM {$db1['frontend']}.registro_tab
  105.                         WHERE gid=$gid
  106.                         $sql_vis
  107.                         ORDER BY table_name",$link);
  108.         
  109.         if(vmsql_num_rows($q)>0){
  110.         
  111.             return vmsql_fetch_assoc_all($q);
  112.         }
  113.         else{
  114.             return array();
  115.         }
  116. }
  117.  
  118.  
  119.  
  120. /**
  121.  * Prendi le informazioni sui campi di una data tabella dal nome oppure dall'oid per un dato gruppo
  122.  *
  123.  * @param mixed $table_name Nome della tabella
  124.  * @param string $campi Campi della tabella che si vogliono recuperare, separati da virgola (se più di uno)
  125.  * @param bool $solo_visibili Prende solo le tabelle visibili (diritto SELECT) dal gruppo specificato
  126.  * @param int $gid Gruppo secondo il quale recuperare le informazioni sulle tabelle. Se non specificato è il gruppo in sessione
  127.  * @return matrix Matrice di risultati SQL
  128.  */
  129. function prendi_colonne_frontend($table_name,$campi="*",$solo_visibili=true,$gid="session"){
  130.     
  131.     global $link,$db1;
  132.     
  133.     $sql_vis ($solo_visibili"AND in_visibile=1" "";
  134.     
  135.     // e' oid?
  136.     if(is_numeric($table_name)){
  137.         
  138.         $oid=intval($table_name);
  139.         
  140.         $clausola_ID "AND t.id_table=$oid";
  141.         
  142.     }
  143.     // allora è nome tabella.. c'è bisogno pure del GID
  144.     else{
  145.     
  146.         if($gid=="session"){
  147.             $gid= (int) $_SESSION['gid'];
  148.         }
  149.         else{
  150.             $gid= (int) $gid;
  151.         }
  152.         
  153.         $clausola_ID "AND t.table_name='$table_name'
  154.                         AND t.gid=$gid    ";
  155.     }
  156.     
  157.     
  158.     
  159.     $q_info vmsql_query("SELECT $campi
  160.                         FROM {$db1['frontend']}.registro_col c, {$db1['frontend']}.registro_tab t
  161.                         WHERE c.id_table=t.id_table
  162.                         $clausola_ID
  163.                         $sql_vis
  164.                         ORDER BY c.in_ordinec.ordinal_position",$link);
  165.  
  166.  
  167.     $matrice vmsql_fetch_row_all($q_info,true);
  168.     
  169.     return $matrice;
  170. }
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177. /**
  178.  * Funzione di test per verificare l'esistenza di una tabella
  179.  *
  180.  * @param string $tabella 
  181.  * @return bool 
  182.  */
  183. function is_tabella($tabella){
  184.     
  185.     global $link,$db1;
  186.     
  187.     $q=vmsql_query("SELECT id_table FROM {$db1['frontend']}.registro_tab WHERE table_name='$tabella",$link);
  188.     
  189.     return (vmsql_num_rows($q)>0true:false;
  190.     
  191. }
  192.  
  193.  
  194.  
  195. /**
  196.  * Funzione di test per verificare l'esistenza di una tabella attraverso l'oid
  197.  *
  198.  * @param int $oid 
  199.  * @return bool 
  200.  */
  201. function is_tabella_by_oid($oid){
  202.     
  203.     global $link,$db1;
  204.     
  205.     $q=vmsql_query("SELECT id_table FROM {$db1['frontend']}.registro_tab WHERE id_table=".intval($oid),$link);
  206.     
  207.     return (vmsql_num_rows($q)>0true:false;
  208.     
  209. }
  210.  
  211. /**
  212.  * Funzione di test per verificare l'esistenza di un campo di tabella
  213.  *
  214.  * @param string $campo 
  215.  * @return bool 
  216.  */
  217. function is_campo($campo){
  218.     
  219.     global $link,$db1;
  220.     
  221.     $q=vmsql_query("SELECT column_name FROM {$db1['frontend']}.registro_col WHERE column_name='$campo",$link);
  222.     
  223.     return (vmsql_num_rows($q)>0true:false;
  224.     
  225. }
  226.  
  227.  
  228. /**
  229.  * Data una tabella ed un opzionale campo, restituisce le tabelle che sono ad essa collegate e dipendenti
  230.  * tramite un'analisi dei campi chiavi esterne.
  231.  *
  232.  * @param string $tabella 
  233.  * @param string $campo 
  234.  * @return matrix 
  235.  */
  236. function ref_campo($tabella,$campo=null){
  237.     
  238.     global $link,$db1;
  239.     
  240.     if($campo!=null){
  241.         
  242.         $sql_campo ($db1['dbtype']=='postgres'"AND column_name='$campo"AND REFERENCED_COLUMN_NAME='$campo";
  243.     }
  244.     else $sql_campo='';
  245.     
  246.     
  247.     if($db1['dbtype']=='postgres'{
  248.         
  249.         $sql ="SELECT table_name, column_name 
  250.                 FROM information_schema.key_column_usage
  251.                 WHERE table_schema='".$db1['dbname']."'
  252.                 AND table_name='$tabella'
  253.                 $sql_campo
  254.                 ORDER BY table_namecolumn_name";
  255.     
  256.     }else{
  257.         
  258.         $sql ="SELECT TABLE_NAME, COLUMN_NAME 
  259.                 FROM information_schema.key_column_usage
  260.                 WHERE REFERENCED_TABLE_SCHEMA='".$db1['dbname']."'
  261.                 AND REFERENCED_TABLE_NAME='$tabella'
  262.                 $sql_campo
  263.                 ORDER BY TABLE_NAMECOLUMN_NAME";
  264.     }
  265.     
  266.     
  267.     $q=vmsql_query($sql,$link);
  268.     
  269.     $matrice_ref vmsql_fetch_assoc_all($q);
  270.     
  271.     return $matrice_ref;
  272.     
  273.     
  274. }
  275.  
  276.  
  277.  
  278. /**
  279.  * Prende le FK
  280.  * Individua i campi coinvolti in una relazione esterna e tabella.campo a cui è legato
  281.  * Funziona sia passando l'oid numerico che una stringa che rappresenta il nome di tabella + il gruppo
  282.  *
  283.  * @param mixed $tab 
  284.  * @param int $gid 
  285.  * @return array 
  286.  */
  287. function prendi_FK($tab,$gid=0){
  288.     
  289.     global $link,$db1;    
  290.     
  291.     
  292.     // se $tab è un numero è l'oid
  293.     // altrimenti il nome tabella
  294.     if(is_numeric($tab)){
  295.         $clausola "AND rb.id_table=$tab";
  296.     }
  297.     else{
  298.         
  299.         $clausola "AND rb.table_name='$tab'
  300.                     AND rb.gid=$gid
  301.                     GROUP BY column_name,colref
  302.                     ";
  303.     }
  304.  
  305.     if($db1['dbtype']=='postgres'){
  306.         
  307.         $sql_fk " SELECT column_name, ".concat_sintax("k.table_name ,'.', k.column_name"'colref')."
  308.             FROM information_schema.key_column_usage k
  309.                 information_schema.table_constraints c
  310.                 {$db1['frontend']}.registro_tab rb
  311.             WHERE c.constraint_type='FOREIGN KEY'
  312.             AND c.constraint_schema='{$db1['dbname']}'
  313.             AND k.constraint_schema='{$db1['dbname']}'
  314.             AND c.constraint_name=k.constraint_name
  315.             AND rb.table_name=c.table_name
  316.             $clausola
  317.             
  318.                 ";
  319.         
  320.     }
  321.     else{
  322.         
  323.         $sql_fk " SELECT column_name, ".concat_sintax("k.REFERENCED_TABLE_NAME ,'.', k.REFERENCED_COLUMN_NAME"'colref')."
  324.             FROM information_schema.key_column_usage k
  325.                 information_schema.table_constraints c
  326.                 {$db1['frontend']}.registro_tab rb
  327.             WHERE c.constraint_type='FOREIGN KEY'
  328.             AND c.constraint_schema='{$db1['dbname']}'
  329.             AND k.constraint_schema='{$db1['dbname']}'
  330.             AND c.constraint_name=k.constraint_name
  331.             AND rb.table_name=c.table_name
  332.             $clausola
  333.             
  334.                 ";
  335.     }
  336.     
  337.     
  338.     $q_fk=vmsql_query($sql_fk,$link);
  339.     
  340.     if(vmsql_num_rows($q_fk)==0){
  341.  
  342.         return array(array(),array());
  343.     }
  344.     else{
  345.          return vmsql_fetch_row_all($q_fk,true);
  346.     }
  347.     
  348. }
  349.  
  350.  
  351.  
  352. /**
  353.  * Sostituisce lato server alcune variabili specificate nell'impostazione dei campi da parte dell'amministratore
  354.  * e che possono essere inserite ad esempio nei campi hidden e restituisce il valore
  355.  *
  356.  * @param string $variabile 
  357.  * @return string 
  358.  */
  359. function variabili_campi($variabile){
  360.     
  361.     $variabile=trim($variabile);
  362.     
  363.     switch ($variabile){
  364.         
  365.         case '%nick' $out(isset($_SESSION['user']['nick'])) $_SESSION['user']['nick'falsebreak;
  366.         case '%email' $out(isset($_SESSION['user']['email'])) $_SESSION['user']['email'falsebreak;
  367.         case '%gid' $out(isset($_SESSION['gid'])) $_SESSION['gid'falsebreak;
  368.         case '%gruppo' $out(isset($_SESSION['gid'])) gid2nome_gruppo($_SESSION['gid']falsebreak;
  369.         case '%nome' $out(isset($_SESSION['user']['nome'])) $_SESSION['user']['nome'falsebreak;
  370.         case '%cognome' $out(isset($_SESSION['user']['cognome'])) $_SESSION['user']['cognome'falsebreak;
  371.         case '%nomecognome' $out(isset($_SESSION['user']['nome']&& isset($_SESSION['user']['cognome'])) 
  372.              ? $_SESSION['user']['nome']." ".$_SESSION['user']['cognome'falsebreak;
  373.         
  374.         case '%cognomenome' $out(isset($_SESSION['user']['cognome']&& isset($_SESSION['user']['nome'])) 
  375.              ? $_SESSION['user']['cognome']." ".$_SESSION['user']['nome'falsebreak;
  376.              
  377.              
  378.         case '%istitutocomp' $out(isset($_SESSION['user']['id_istituto']&& isset($_SESSION['user']['id_istituto'])) 
  379.              ? $_SESSION['user']['id_istituto'falsebreak;
  380.         
  381.         case '%now' $outdate('Y-m-d')break;
  382.         case '%timestamp' $outdate('Y-m-d H:i:s')break;
  383.         
  384.         default$out=false;    
  385.     }
  386.     
  387.     return ($out!=false && $out!=''$out false;
  388.     
  389. }
  390.  
  391.  
  392.  
  393. /**
  394.  * Funzione che, interrogando l'information_schema, recupera la chiava primaria di una tabella
  395.  *
  396.  * @param string $tabella 
  397.  * @param int $gid 
  398.  * @return string 
  399.  */
  400. function prendi_PK($tabella,$gid='session'){
  401.     
  402.     global $link,$db1;
  403.     
  404.     if($gid=='session'){
  405.         
  406.         $gid = (int) $_SESSION['gid'];
  407.     }
  408.     else{
  409.         $gid= (int) $gid;
  410.     }
  411.     
  412.     // E' tabella o vista?
  413.     $sql_tipo="SELECT table_type FROM {$db1['frontend']}.registro_tab 
  414.                          WHERE table_name='$tabellaAND gid=$gid";
  415.     $q_tipo=vmsql_query($sql_tipo,$link);
  416.     list($tipo_tab)=vmsql_fetch_row($q_tipo);
  417.     
  418.     if($tipo_tab=='VIEW'){
  419.         
  420.         // Chiave primaria esplicita delle viste
  421.         $sql_pk "SELECT view_pk AS campo_pk 
  422.                 FROM {$db1['frontend']}.registro_tab rb 
  423.                 WHERE rb.table_name='$tabella'
  424.                 AND rb.gid=$gid
  425.                 ";
  426.     }
  427.     else{
  428.     
  429.     // Chiave/i primarie
  430.         if($db1['dbtype']=='postgres'){
  431.             
  432.             $sql_pk "SELECT i.column_name as campo_pk 
  433.                 FROM {$db1['frontend']}.registro_tab rb 
  434.                 INNER JOIN information_schema.key_column_usage i ON (i.table_name=rb.table_name AND i.table_schema='{$db1['dbname']}')
  435.                 INNER JOIN information_schema.table_constraints c ON (c.table_name=rb.table_name AND c.table_schema='publicAND c.constraint_name=i.constraint_name)
  436.                 WHERE rb.table_name='$tabella'
  437.                 AND rb.gid=$gid
  438.                 AND c.constraint_type='PRIMARY KEY'
  439.                 ";
  440.             
  441.         }
  442.         
  443.         else if($db1['dbtype']=='mysql'){
  444.             $sql_pk "SELECT i.column_name as campo_pk 
  445.                 FROM {$db1['frontend']}.registro_tab rb 
  446.                 LEFT JOIN information_schema.key_column_usage i ON (i.table_name=rb.table_name AND i.table_schema='{$db1['dbname']}')
  447.                 WHERE rb.table_name='$tabella'
  448.                 AND rb.gid=$gid
  449.                 AND CONSTRAINT_NAME='PRIMARY'
  450.                 ";
  451.         }
  452.     }
  453.     
  454.     $q_pk=vmsql_query($sql_pk,$link);
  455.     
  456.     if(vmsql_num_rows($q_pk)==0return null;
  457.     
  458.     list($campoPK)=vmsql_fetch_row($q_pk);
  459.     
  460.     return $campoPK;
  461. }
  462.  
  463.  
  464.  
  465. /**
  466.  * Funzione che recupera tutte le chiavi primarie di una tabella.
  467.  * Funziona come prendi_PK() ma restituisce una o più chiavi sotto forma di array
  468.  * Da utilizzare quando si presume che le PK possano essere più di una
  469.  *
  470.  * @param string $tabella 
  471.  * @param int $gid 
  472.  * @return array 
  473.  */
  474. function prendi_all_PK($tabella,$gid='session'){
  475.     
  476.     global $link,$db1;
  477.     
  478.     if($gid=='session'){
  479.         
  480.         $gid = (int) $_SESSION['gid'];
  481.     }
  482.     else{
  483.         $gid= (int) $gid;
  484.     }
  485.     
  486.     // E' tabella o vista?
  487.     $q_tipo=vmsql_query("SELECT table_type FROM {$db1['frontend']}.registro_tab 
  488.                          WHERE table_name='$tabellaAND gid=$gid",$link);
  489.     list($tipo_tab)=vmsql_fetch_row($q_tipo);
  490.     
  491.     
  492.     
  493.     if($tipo_tab=='VIEW'){
  494.         
  495.         // Chiave primaria esplicita delle viste
  496.         $sql_pk "SELECT view_pk AS campo_pk 
  497.                 FROM {$db1['frontend']}.registro_tab rb 
  498.                 WHERE rb.table_name='$tabella'
  499.                 AND rb.gid=$gid
  500.                 ";
  501.     }
  502.     else{
  503.     
  504.         if($db1['dbtype']=='postgres'){
  505.             
  506.             $sql_pk "SELECT i.column_name as campo_pk 
  507.                 FROM {$db1['frontend']}.registro_tab rb 
  508.                 INNER JOIN information_schema.key_column_usage i ON (i.table_name=rb.table_name AND i.table_schema='{$db1['dbname']}')
  509.                 INNER JOIN information_schema.table_constraints c ON (c.table_name=rb.table_name AND c.table_schema='publicAND c.constraint_name=i.constraint_name)
  510.                 WHERE rb.table_name='$tabella'
  511.                 AND rb.gid=$gid
  512.                 AND c.constraint_type='PRIMARY KEY'
  513.                 ";
  514.             rpc_debug($sql_pk);
  515.             
  516.         }
  517.         
  518.         else if($db1['dbtype']=='mysql'){
  519.             $sql_pk "SELECT i.column_name as campo_pk 
  520.                 FROM {$db1['frontend']}.registro_tab rb 
  521.                 LEFT JOIN information_schema.key_column_usage i ON (i.table_name=rb.table_name AND i.table_schema='{$db1['dbname']}')
  522.                 WHERE rb.table_name='$tabella'
  523.                 AND rb.gid=$gid
  524.                 AND CONSTRAINT_NAME='PRIMARY'
  525.                 ";
  526.         }
  527.     }
  528.     
  529.     
  530.     $q_pk=vmsql_query($sql_pk,$link);
  531.     
  532.     list($campoPK)=vmsql_fetch_row_all($q_pk,true);
  533.     
  534.     return $campoPK;
  535. }
  536.  
  537.  
  538.  
  539. function prendi_all_PK_submask_oid($id_submask,$gid='session'){
  540.     
  541.     global $link,$db1;
  542.     
  543.     if($gid=='session'){
  544.         
  545.         $gid = (int) $_SESSION['gid'];
  546.     }
  547.     else{
  548.         $gid= (int) $gid;
  549.     }
  550.     
  551.     // Chiave/i primarie
  552.     $sql_pk "SELECT i.column_name as campo_pk 
  553.                 FROM {$db1['frontend']}.registro_submask rb 
  554.                 LEFT JOIN information_schema.key_column_usage i ON (i.table_name=rb.nome_tabella AND i.table_schema='{$db1['dbname']}')
  555.                 WHERE rb.id_submask='$id_submask'
  556.                 AND CONSTRAINT_NAME='PRIMARY'
  557.                 ";
  558.     
  559.     $q_pk=vmsql_query($sql_pk,$link);
  560.     
  561.     list($campoPK)=vmsql_fetch_row_all($q_pk,true);
  562.     
  563.     return $campoPK;
  564. }
  565.  
  566.  
  567. /**
  568.  * Prende la chiave primaria di una tabella in base all'OID (Id della tabella) passato
  569.  *
  570.  * @param int $oid 
  571.  * @param string $table_type "BASE TABLE" | "VIEW"
  572.  * @return array 
  573.  */
  574. function prendi_PK_oid($oid,$table_type='BASE TABLE'){
  575.     
  576.     global $link,$db1;
  577.     
  578.     $oid=(int) $oid;
  579.     
  580.     if($table_type=='VIEW'){
  581.     
  582.     // Chiave primaria esplicita delle viste
  583.         $sql_pk "SELECT view_pk AS campo_pk 
  584.                 FROM {$db1['frontend']}.registro_tab rb 
  585.                 WHERE rb.id_table=$oid
  586.                 ";
  587.     
  588.     }
  589.     else{
  590.         
  591.         // Chiave/i primarie
  592.         if($db1['dbtype']=='postgres'){
  593.                 
  594.                 $sql_pk "SELECT i.column_name as campo_pk 
  595.                     FROM {$db1['frontend']}.registro_tab rb 
  596.                     INNER JOIN information_schema.key_column_usage i ON (i.table_name=rb.table_name AND i.table_schema='{$db1['dbname']}')
  597.                     INNER JOIN information_schema.table_constraints c ON (c.table_name=rb.table_name AND c.table_schema='publicAND c.constraint_name=i.constraint_name)
  598.                     WHERE rb.id_table=$oid
  599.                     AND c.constraint_type='PRIMARY KEY'
  600.                     ";
  601.                 
  602.             }
  603.             
  604.         else if($db1['dbtype']=='mysql'){
  605.                 $sql_pk "SELECT i.column_name as campo_pk 
  606.                     FROM {$db1['frontend']}.registro_tab rb 
  607.                     LEFT JOIN information_schema.key_column_usage i ON (i.table_name=rb.table_name AND i.table_schema='{$db1['dbname']}')
  608.                     WHERE rb.id_table=$oid
  609.                     AND CONSTRAINT_NAME='PRIMARY'
  610.                     ";
  611.         }
  612.     }
  613.     
  614.     $q_pk=vmsql_query($sql_pk,$link);
  615.     
  616.     list($info_pk)=vmsql_fetch_row_all($q_pk,true);
  617.  
  618.     return $info_pk;
  619. }
  620.  
  621.  
  622. /**
  623.  * Prende le sottomaschere impostate per una data maschera data l'ID (oid) del record
  624.  *
  625.  * @param int $oid 
  626.  * @param bool $reverse 
  627.  * @param bool $solo_visibili 
  628.  * @return array 
  629.  */
  630. function prendi_sottomaschere($oid,$reverse=false,$solo_visibili=false){
  631.     
  632.     global $db1$link;
  633.     
  634.     $clausola_solo_visibili ($solo_visibili"AND sub_select='1' " "";
  635.     
  636.     $sql "SELECT * FROM ".$db1['frontend'].".registro_submask
  637.             WHERE id_table=$oid
  638.             $clausola_solo_visibili
  639.             ORDER BY nome_tabella
  640.             ";
  641.     
  642.     $q=vmsql_query($sql,$link);
  643.     
  644.     $matrice (vmsql_num_rows($q)>0vmsql_fetch_assoc_all($q,$reversearray();
  645.     
  646.     return $matrice;
  647.     
  648. }
  649.  
  650.  
  651. function prendi_info_view($view_name,$cols="*"){
  652.     
  653.     global $link,$db1;
  654.     
  655.     $q=vmsql_query("SELECT $cols FROM information_schema.views 
  656.                     WHERE TABLE_SCHEMA='{$db1['dbname']}'
  657.                     AND TABLE_NAME='$view_name'",$link);
  658.     
  659.     return (vmsql_num_rows($q)==1vmsql_fetch_assoc($qarray();
  660. }
  661.  
  662.  
  663.  
  664. /**
  665.  * Restituisce unamatrice bidimensionale (gid=>nome_gruppo) dei gruppi presenti in DB VFront
  666.  * Utile per (ad esempio) mostrare una tendina
  667.  * Si possono escludere dei gruppi mediante il parametro $escludi_gid
  668.  *
  669.  * @param mixed $escludi_gid 
  670.  * @return array 
  671.  */
  672. function prendi_gruppi($escludi_gid=''){
  673.     
  674.     global $link$db1;
  675.     
  676.     
  677.     $mat_gruppi=array();
  678.     
  679.     if($escludi_gid!=''){
  680.         
  681.         $clausola_esclusione=" WHERE gid!=".intval($escludi_gid);
  682.     }
  683.     
  684.     $q_g=vmsql_query("SELECT gid, nome_gruppo FROM ".$db1['frontend'].".gruppo $clausola_esclusione ORDER BY gid",$link);
  685.     
  686.     if(vmsql_num_rows($q_g)>0)    $mat_gruppi=vmsql_fetch_assoc_all($q_g);
  687.     
  688.     return $mat_gruppi;
  689. }
  690.  
  691.  
  692. function prendi_K_relazione_sub($id_table){
  693.     
  694.     global $link$db1;
  695.     
  696.     $sql="SELECT campo_pk_parentcampo_fk_sub FROM {$db1['frontend']}.registro_submask WHERE id_table=".intval($id_table);
  697.     rpc_debug($sql);
  698.     $q=vmsql_query($sql,$link);
  699.     
  700.     return (vmsql_num_rows($q)>0vmsql_fetch_row_all($qarray();
  701. }
  702.  
  703.  
  704.  
  705.  
  706. /**
  707.  * Recupera i file allegati associati ad un dato record di una data tabella
  708.  * presenti nella tabella "allegato" sotto forma di matrice
  709.  *
  710.  * @param string $tabella 
  711.  * @param mixed $valore_id 
  712.  * @return array 
  713.  */
  714. function prendi_allegati($tabella,$valore_id){
  715.     
  716.     global $link,$db1;
  717.     
  718.     $sql="SELECT * FROM "._TABELLA_ALLEGATO." WHERE tipoentita='$tabellaAND codiceentita='$valore_id'";
  719.     $q=vmsql_query($sql,$link);
  720.     
  721.     if(vmsql_num_rows($q)>0){
  722.         
  723.         return vmsql_fetch_assoc_all($q);
  724.     }
  725.     else{
  726.         
  727.         return array();
  728.     }
  729.     
  730. }
  731.  
  732.  
  733.  
  734. /**
  735.  * Recupera i link associati al record di una data tabella
  736.  * dalla tabella "Link" sotto forma di matrice
  737.  *
  738.  * @param string $tabella 
  739.  * @param mixed $valore_id 
  740.  * @return array 
  741.  */
  742. function prendi_link($tabella,$valore_id){
  743.     
  744.     global $link,$db1;
  745.     
  746.     $sql="SELECT * FROM "._TABELLA_LINK." WHERE tipoentita='$tabellaAND codiceentita='$valore_id'";
  747.     $q=vmsql_query($sql,$link);
  748.     
  749.     if(vmsql_num_rows($q)>0){
  750.         
  751.         return vmsql_fetch_assoc_all($q);
  752.     }
  753.     else{
  754.         
  755.         return array();
  756.     }
  757.     
  758. }
  759.  
  760.  
  761. /**
  762.  * Cancella dal DB e dal filesystem un allegato
  763.  *
  764.  * @param int $id_allegato 
  765.  * @return bool 
  766.  */
  767. function elimina_allegato($id_allegato=null){
  768.     
  769.     global $link,$db1;
  770.     
  771.     // elimino dal file system
  772.     $test@unlink(_PATH_ATTACHMENT."/".$id_allegato.".dat");
  773.     
  774.     if($test){
  775.         
  776.         $q=vmsql_query("DELETE FROM "._TABELLA_ALLEGATO." WHERE codiceallegato='$id_allegato",$link);
  777.         
  778.         return true;
  779.         
  780.     }else return false;
  781.     
  782. }
  783.  
  784.  
  785. /**
  786.  * Imposta la sintassi SQL di LIMIT e OFFSET in base al tipo di database utilizzato
  787.  * Questa funzione si rende necessaria a causa della diversa sintassi di mysql e postgresql
  788.  *
  789.  * @param int $limit 
  790.  * @param int $offset 
  791.  * @return string 
  792.  */
  793. function limit_sintax($limit,$offset=''){
  794.     
  795.     global $db1;
  796.     
  797.     $str='';
  798.     
  799.     if($db1['dbtype']=='postgres'){
  800.         
  801.         $str.= "LIMIT $limit";
  802.         
  803.         if($offset!=''$str.=" OFFSET $offset";
  804.     }
  805.     elseif($db1['dbtype']=='mysql'){
  806.         
  807.         if($offset!=''$str.="LIMIT $offset,$limit";
  808.         else $str.="LIMIT $limit";
  809.     }
  810.     
  811.     return $str;
  812. }
  813.  
  814.  
  815. /**
  816.  * Imposta la sintassi SQL di concatenazione stringhe
  817.  * Questa funzione si rende necessaria a causa della diversa sintassi di mysql e postgresql
  818.  *
  819.  * @param string $arguments 
  820.  * @param string $as 
  821.  * @return string 
  822.  */
  823. function concat_sintax($arguments='',$as=''){
  824.     
  825.     global $db1;
  826.     
  827.     if($db1['dbtype']=='postgres'){
  828.         
  829.         $str=str_replace(","," || ",$arguments);
  830.         
  831.         
  832.     }
  833.     elseif($db1['dbtype']=='mysql'){
  834.         
  835.         $str="CONCAT($arguments)";
  836.         
  837.     }
  838.     
  839.     if($as!=''$str.=" AS $as";
  840.     
  841.     return $str;
  842. }
  843.  
  844. /**
  845.  * Funzione per la determinazione del tipo di campo
  846.  * Utile soprattutto per PostgreSQL
  847.  *
  848.  * @param mixed $tabella_o_gid 
  849.  * @param string $campo 
  850.  * @return bool 
  851.  */
  852. function campo_is_numeric($tabella_o_gid,$campo){
  853.     
  854.     global $link,$db1;
  855.     
  856.     if(is_numeric($tabella_o_gid)){
  857.         
  858.         $id_table=$tabella_o_gid;
  859.     }
  860.     else{
  861.         
  862.         $id_table=name2oid($tabella_o_gid,$_SESSION['gid']);
  863.     }
  864.     
  865.     
  866.     $sql="SELECT column_type FROM {$db1['frontend']}.registro_col 
  867.                     WHERE id_table=$id_table AND column_name='$campo'";
  868.     rpc_debug($sql);
  869.     $q=vmsql_query($sql,$link);
  870.     
  871.     if(vmsql_num_rows($q)>0){
  872.         list($tipo)=vmsql_fetch_row($q);
  873.         
  874.         if(ereg("int",$tipo|| ereg("double",$tipo|| ereg("float",$tipo)) return true;
  875.         else return false;
  876.     }
  877.     else return false;
  878. }
  879.  
  880.  
  881. ?>

Documentation generated on Sat, 22 Sep 2007 11:50:15 +0200 by phpDocumentor 1.4.0a2