Source for file db.class.php

Documentation is available at db.class.php

  1. <?php
  2. /**
  3.  * TPLN Database Plugin
  4.  * @package Template Engine
  5.  */
  6. class Db extends Form
  7. {
  8.     // DataBase
  9.     public  $db_index = -1;
  10.     public  $req_index = -1// index the results in progress
  11.     public  $db = array()// object which contains the connection informations
  12.     protected  $req = array()// storage of results
  13.     protected  $DBLastError = ''// the last SQL error
  14.     protected  $query_count = 0// number of executed requests
  15.     // Xtra
  16.     protected  $cons_query = array()// the dislocated request is stored here
  17.     protected  $NavColorFirst = ''// color for the navigation
  18.     protected  $NavColorSecond = '';
  19.     protected  $url = ''// contient les variables
  20.     protected  $url_var = ''// contains the parameters of the url
  21.     protected  $UrlRgxPatterns = ''// contains the regex for formatting the urls
  22.     protected  $UrlRgxReplace = '';
  23.     protected  $user_resulset = array()// contains the user results
  24.  
  25.     // owner
  26.     protected  $NbRecordPerPage = 0// number of wished results per page
  27.     protected  $NbResults = 0// the results
  28.     protected  $Count = 0;
  29.     protected  $T_first = 0// which used to determinate the id
  30.     protected  $First = 0;
  31.     protected  $Last = 0;
  32.     protected  $PageNumber = 0;
  33.     protected  $PageCount = 0;
  34.     protected  $NavColor = '';
  35.     protected  $OrderByFields = array();
  36.     protected  $OrderByImgsPath = ''// path per defect des icone asc-desc
  37.  
  38.     protected $dbProtectionMode = true// enable or disable protection mode
  39.  
  40.  
  41. // DataBase ******************************************************************************************************************* @author H2LSOFT */
  42.     /**
  43.      * this method allows you to connect to your MySQL database.
  44.      *
  45.      * @param string $db_type 
  46.      * @param string $host 
  47.      * @param string $login 
  48.      * @param string $password 
  49.      * @param string $base 
  50.      * @param string $port 
  51.      * @param boolean $new_connection 
  52.      *
  53.      * @author H2LSOFT
  54.      */
  55.     public function dbConnect($db_type ''$host ''$login ''$password ''$base ''$port ''$new_connection '')
  56.     {
  57.         // initialisation of variable
  58.         if(empty($db_type))$db_type TPLN_DB_TYPE_DEFAULT;
  59.         if(empty($host))$host TPLN_DB_HOST_DEFAULT;
  60.         if(empty($login))$login TPLN_DB_LOGIN_DEFAULT;
  61.         if(empty($password))$password TPLN_DB_PASSWORD_DEFAULT;
  62.         if(empty($base))$base TPLN_DB_BASE_DEFAULT;
  63.         if(empty($port))$port TPLN_DB_PORT;
  64.         if(empty($new_connection))$new_connection TPLN_DB_NEW_CONNECTION;
  65.  
  66.         $this->db_index++;
  67.         if(!empty($port))$this->db[$this->db_index]->port $port;
  68.  
  69.         try {
  70.             $persistant ($new_connectionfalse true;
  71.             $this->db[$this->db_index@new PDO("$db_type:host=$host;dbname=$base;port=$port"$login$passwordarray(PDO::ATTR_PERSISTENT => $new_connection));
  72.         }
  73.         catch(PDOException $e){
  74.             $this->dbError(0$e->getMessage());
  75.         }
  76.         
  77.         // init queries
  78.         if(TPLN_DB_INIT_QUERIES != '')
  79.         {
  80.             $t explode(';'TPLN_DB_INIT_QUERIES);
  81.             foreach($t as $sql_n)
  82.             {
  83.                 $sql_n trim($sql_n);
  84.                 if(!empty($sql_n))
  85.                     $this->doQuery($sql_n);
  86.             }
  87.         }
  88.     }
  89.  
  90.     /**
  91.      * this method allows to change connection.
  92.      *
  93.      * @param int $db_index 
  94.      *
  95.      * @return boolean 
  96.      *
  97.      * @deprecated
  98.      * @since 2.9
  99.      * @see setConnectionID()
  100.      * @author H2LSOFT */
  101.     public function changeConnection($db_index)
  102.     {
  103.         if($db_index || $db_index >= count($this->db))
  104.         {
  105.             $this->dbError('2.1'$db_index);
  106.             return;
  107.         }
  108.  
  109.         $this->db_index = $db_index;
  110.     }
  111.  
  112.     /**
  113.      * return db connection object to share with framework
  114.      * @param int dbIndex
  115.      */
  116.     public function dbGetConnectionObject($db_index='')
  117.     {
  118.         if($db_index == '')$db_index $this->db_index;
  119.         return $this->db[$db_index];
  120.     }
  121.  
  122.  
  123.  
  124.     /**
  125.      * @deprecated
  126.      * @since 2.9
  127.      * @see DbSetConnectionId().
  128.      * @author H2LSOFT */
  129.     public function setConnectionID($db_index)
  130.     {
  131.         $this->changeConnection($db_index);
  132.     }
  133.  
  134.     /**
  135.      * method allows you to change connection.
  136.      *
  137.      * @param int $db_index 
  138.      * @since 2.9
  139.      * @see dbConnect()
  140.      * @author H2LSOFT */
  141.     public function dbSetConnectionID($db_index)
  142.     {
  143.         $this->setConnectionId($db_index);
  144.     }
  145.  
  146.     /**
  147.      * @deprecated
  148.      * @since 2.9
  149.      * @see dbGetConnectionId
  150.      * @author H2LSOFT */
  151.     public function getConnectionID()
  152.     {
  153.         return $this->db_index;
  154.     }
  155.  
  156.     /**
  157.      * this method allows to change connection.
  158.      *
  159.      * @return int 
  160.      * @since 2.9
  161.      * @see setConnectionId
  162.      * @author H2LSOFT */
  163.     public function dbGetConnectionID()
  164.     {
  165.         return $this->getConnectionID();
  166.     }
  167.  
  168.     /**
  169.      * @deprecated
  170.      * @since 2.2.5
  171.      * @see dbsetQueryId
  172.      * @author H2LSOFT */
  173.     public function changeQuery($req_index)
  174.     {
  175.         if($req_index || $req_index >= count($this->req))
  176.         {
  177.             $this->dbError('2.11'$req_index);
  178.             return;
  179.         }
  180.  
  181.         $this->req_index = $req_index;
  182.     }
  183.  
  184.     /**
  185.      * @deprecated
  186.      * @since 2.9
  187.      * @see dbSetQueryId
  188.      * @author H2LSOFT */
  189.     public function setQueryID($req_index)
  190.     {
  191.         $this->changeQuery($req_index);
  192.     }
  193.  
  194.     /**
  195.      * This method allows to change queries resultset.
  196.      *
  197.      * @param int $req_index 
  198.      * @since 2.2.5
  199.      * @see getQueryId
  200.      * @author H2LSOFT */
  201.     public function dbSetQueryID($req_index)
  202.     {
  203.         $this->setQueryID($req_index);
  204.     }
  205.  
  206.     /**
  207.      * @deprecated
  208.      * @since 2.2.5
  209.      * @see dbGetQueryNb
  210.      * @author H2LSOFT */
  211.     public function getQueryNb()
  212.     {
  213.         return $this->req_index;
  214.     }
  215.  
  216.     /**
  217.      * This method allows to retriece the index of the current query.
  218.      *
  219.      * @return int 
  220.      *
  221.      * @since 2.2.5
  222.      * @see setQueryID
  223.      * @author H2LSOFT */
  224.     public function dbGetQueryNb()
  225.     {
  226.         return $this->getQueryNb();
  227.     }
  228.  
  229.     /**
  230.      * @deprecated
  231.      * @since 2.9
  232.      * @see dbGetQueryId
  233.      * @author H2LSOFT */
  234.     public function getQueryID()
  235.     {
  236.         return $this->getQueryNb();
  237.     }
  238.  
  239.     /**
  240.      * This method allows you to retriece the index of the current query.
  241.      *
  242.      * @return int 
  243.      *
  244.      * @since 2.9
  245.      * @see setQueryId
  246.      * @author H2LSOFT */
  247.     public function dbGetQueryID()
  248.     {
  249.         return $this->getQueryId();
  250.     }
  251.  
  252.     /**
  253.      * method allows you to close database connection.
  254.      *
  255.      * @see DbConnect()
  256.      * @author H2LSOFT */
  257.     public function dbClose()
  258.     {
  259.         $this->db[$this->db_indexnull;
  260.     }
  261.  
  262.     /**
  263.      * verify quotes
  264.      * @param int $value 
  265.      *
  266.      * @return int 
  267.      * @author H2LSOFT */
  268.     public function checkQuotes($value)
  269.     {
  270.         if(is_array($value))
  271.         {
  272.             foreach($value as $key => $val)
  273.                 $value[$key$this->checkQuotes($val);
  274.  
  275.             return $value;
  276.         }
  277.         else
  278.         {
  279.             // return addslashes($value);
  280.             return strtr($valuearray("\x00" => '\x00'"\n" => '\n'"\r" => '\r''\\' => '\\\\'"'" => "\'"'"' => '\"'"\x1a" => '\x1a'));
  281.  
  282.         }
  283.             // return mysql_escape_string($value);
  284.         //return ((!get_magic_quotes_runtime() && !get_magic_quotes_gpc()) ? mysql_real_escape_string($value) : $value);
  285.  
  286.     }
  287.  
  288.     /**
  289.      * strip slashes
  290.      * @param int $value 
  291.      *
  292.      * @return int 
  293.      * @author H2LSOFT */
  294.     public function stripQuotes($value)
  295.     {
  296.         if(is_array($value))
  297.         {
  298.             foreach($value as $key => $val)
  299.                 $value[$key$this->stripQuotes($val);
  300.  
  301.             return $value;
  302.         }
  303.         else
  304.             return stripslashes($value);
  305.     }
  306.  
  307.     /**
  308.      * This method returns the maximum value for a ID, it is useful to know the value of an id after an insertion in a table.
  309.      *
  310.      * @param string $table 
  311.      * @param string $ID 
  312.      *
  313.      * @return int 
  314.      *
  315.      * @since 2.5
  316.      * @author H2LSOFT */
  317.     public function getMaxID($table$ID='ID')
  318.     {
  319.         $sql "SELECT MAX($ID) FROM $table";
  320.         $this->doQuery($sql);
  321.         return $this->getOne();
  322.     }
  323.  
  324.     /**
  325.      * This method returns the maximum value for a ID, it is useful to know the value of an id after an insertion in a table.
  326.      *
  327.      * @param string $table 
  328.      * @param string $ID default ID
  329.      *
  330.      * @return int 
  331.      * @author H2LSOFT */
  332.     public function dbGetMaxID($table$ID='ID')
  333.     {
  334.  
  335.         return $this->getMaxId($table$ID);
  336.     }
  337.  
  338.     /**
  339.      * this method allows to execute database query.
  340.      *
  341.      * If second parameter is filled, the array structure for data is different
  342.      *
  343.      * @param string $query 
  344.      * @param int $fetch_mode 
  345.      *
  346.      * @return boolean 
  347.      *
  348.      * @since 2.4, 2.8
  349.      * @author H2LSOFT */
  350.     public function doQuery($sql$FETCH_MODE=PDO::FETCH_BOTH)
  351.     {        
  352.         if(!is_object($this->db[$this->db_index]))
  353.         {
  354.             $this->dbError(0.1);
  355.             return;
  356.         }
  357.  
  358.         $this->query_count++;
  359.         $this->req_index++;
  360.  
  361.         $this->req[$this->req_index$this->db[$this->db_index]->query($sql);
  362.  
  363.         if(!$this->req[$this->req_index])
  364.         {        
  365.             // without debug ?
  366.             $msgs $this->db[$this->db_index]->errorInfo();
  367.             $msg "{$msgs[2]} (code #{$msgs[1]})";
  368.  
  369.             if(TPLN_SQL_QUERY_DEBUG)
  370.                 $msg .= " <br><br>\n<pre><i><strong>`".$sql.'`</strong></i></pre>';
  371.             $this->DBLastError = $msg;
  372.             $this->dbError(2$msg TPLN_SQL_QUERY_DEBUG);
  373.             return false;
  374.         }
  375.  
  376.         return true;
  377.     }
  378.  
  379.     /**
  380.      * This method allows to generate and execute insert query from an associative array.
  381.      *
  382.      * You can exlude some fields from your insert query
  383.      *
  384.      * @param string $table 
  385.      * @param array $arr 
  386.      * @param array $exlude_fields 
  387.      * @param bool $return_last_id 
  388.      *
  389.      * @return boolean 
  390.      *
  391.      * @since 2.8
  392.      * @author H2LSOFT */
  393.     public function dbInsert($table$arr$exlude_fields=array()$return_last_id=false)
  394.     {
  395.         // protection
  396.         $arr $this->dbProtection($arr);
  397.  
  398.         $fields array();
  399.         $vals array();
  400.         foreach($arr as $key => $val)
  401.         {
  402.             // jokers
  403.             $joker false;
  404.             foreach($exlude_fields as $ef)
  405.             {
  406.                 $tmp explode('*'$ef);
  407.                 if(count($tmp== 2)
  408.                 {
  409.                     $str $tmp[0];
  410.                     //if(eregi("^$str", $key))
  411.                     if(stripos($key$str!== false && stripos($key$str== 0)
  412.                     {
  413.                         $joker true;
  414.                         break;
  415.                     }
  416.                 }
  417.             }
  418.  
  419.             if(!in_array($key$exlude_fields&& !$joker)
  420.             {
  421.                 $fields[$key;
  422.                 $vals[$val;
  423.             }
  424.         }
  425.  
  426.         
  427.  
  428.         $sql "INSERT INTO $table\n ";
  429.         $sql .= "\t(\n\t\t".join(",\n\t\t"$fields)."\n\t)\n";
  430.         $sql .= "VALUES\n";
  431.         $sql .= "\t(\n";
  432.         for($i=0$i count($vals)$i++)
  433.         {
  434.             if(!isset($vals[$i]))
  435.                 $s 'NULL';
  436.             elseif(is_int($vals[$i]|| is_float($vals[$i]|| in_array($vals[$i]array('NOW()''NULL')))
  437.                 $s $vals[$i];
  438.             else
  439.                 $s "'".mysql_escape_string($vals[$i])."'";
  440.             if($i count($vals)-1)$s .= ',';
  441.             $sql .= " \t\t$s\n";
  442.         }
  443.         $sql .= "\t)";
  444.  
  445.         $b $this->doQuery($sql);
  446.  
  447.         // return last insert id
  448.         if($return_last_id)
  449.         {
  450.             return $this->dbGetMaxId($table'ID');
  451.         }
  452.  
  453.  
  454.  
  455.         return $b;
  456.     }
  457.  
  458.     /**
  459.      * This method allows you to generate and execute update query from an associative array.
  460.      *
  461.      * You can exlude some fields from your update query
  462.      *
  463.      * @param string $table 
  464.      * @param array $arr 
  465.      * @param string $where 
  466.      * @param array $exlude_fields 
  467.      *
  468.      * @return boolean 
  469.      *
  470.      * @since 2.8, 2.9
  471.      * @author H2LSOFT */
  472.     public function dbUpdate($table$arr$where=''$exlude_fields=array())
  473.     {
  474.         // protection
  475.         $arr $this->dbProtection($arr);
  476.  
  477.         $fields array();
  478.         $vals array();
  479.         foreach($arr as $key => $val)
  480.         {
  481.             // jokers
  482.             $joker false;
  483.             foreach($exlude_fields as $ef)
  484.             {
  485.                 $tmp explode('*'$ef);
  486.                 if(count($tmp== 2)
  487.                 {
  488.                     $str $tmp[0];
  489.                     //if(eregi("^$str", $key))
  490.                     if(stripos($key$str!== false && stripos($key$str== 0)
  491.                     {
  492.                         $joker true;
  493.                         break;
  494.                     }
  495.                 }
  496.             }
  497.  
  498.             if(!in_array($key$exlude_fields&& !$joker)
  499.             {
  500.                 $fields[$key;
  501.                 $vals[$val;
  502.             }
  503.         }
  504.  
  505.         $sql "UPDATE $table SET\n";
  506.         for($i=0$i count($vals)$i++)
  507.         {
  508.             if(!isset($vals[$i]))
  509.                 $s 'NULL';
  510.             elseif(is_int($vals[$i]|| is_float($vals[$i]|| in_array($vals[$i]array('NOW()''NULL')))
  511.                 $s $vals[$i];
  512.             else
  513.                 $s "'".mysql_escape_string($vals[$i])."'";
  514.             if($i count($vals)-1)$s .= ',';
  515.             $sql .= " \t\t{$fields[$i]} = $s\n";
  516.         }
  517.  
  518.         if(!empty($where))
  519.             $where "WHERE\n\t\t".$where;
  520.         $sql .= $where;
  521.  
  522.         return $this->doQuery($sql);
  523.     }
  524.  
  525.     /**
  526.      * This method allows to generate and execute REPALCE query from an associative array.
  527.      *
  528.      * You can exlude some fields from your update query, use * character to exclude a name generically mce*
  529.      *
  530.      * will exclude all name in the array that begins with mce.
  531.      *
  532.      * @param string $table 
  533.      * @param array $arr 
  534.      * @param  array $exlude_fields 
  535.      *
  536.      * @return boolean 
  537.      *
  538.      * @since 2.9.1
  539.      * @author H2LSOFT */
  540.     public function dbReplace($table$arr$exlude_fields=array())
  541.     {
  542.         // protection
  543.         $arr $this->dbProtection($arr);
  544.         
  545.         $fields array();
  546.         $vals array();
  547.         foreach($arr as $key => $val)
  548.         {
  549.             // jokers
  550.             $joker false;
  551.             foreach($exlude_fields as $ef)
  552.             {
  553.                 $tmp explode('*'$ef);
  554.                 if(count($tmp== 2)
  555.                 {
  556.                     $str $tmp[0];
  557.                     //if(eregi("^$str", $key))
  558.                     if(stripos($key$str!== false && stripos($key$str== 0)
  559.                     {
  560.                         $joker true;
  561.                         break;
  562.                     }
  563.                 }
  564.             }
  565.  
  566.             if(!in_array($key$exlude_fields&& !$joker)
  567.             {
  568.                 $fields[$key;
  569.                 $vals[$val;
  570.             }
  571.         }
  572.  
  573.         $sql "REPLACE $table SET\n";
  574.         for($i=0$i count($vals)$i++)
  575.         {
  576.             if(!isset($vals[$i]))
  577.                 $s 'NULL';
  578.             elseif(is_int($vals[$i]|| is_float($vals[$i]|| in_array($vals[$i]array('NOW()''NULL')))
  579.                 $s $vals[$i];
  580.             else
  581.                 $s "'".mysql_escape_string($vals[$i])."'";
  582.             if($i count($vals)-1)$s .= ',';
  583.             $sql .= " \t\t{$fields[$i]} = $s\n";
  584.         }
  585.  
  586.     /*if(!empty($where))
  587.         $where = "WHERE\n\t\t".$where;
  588.     $sql .= $where;*/
  589.  
  590.         return $this->doQuery($sql);
  591.     }
  592.  
  593.     /**
  594.      * This method allows you to execute a query SELECT with parameters,
  595.      *
  596.      * each parameter is protected. This method uses like the php function vsprintf
  597.      *
  598.      * @param string $sql 
  599.      * @param array $values 
  600.      *
  601.      * @author H2LSOFT */
  602.     public function dbSelect($sql$values)
  603.     {
  604.         $values $this->checkQuotes($values);
  605.         $sql vsprintf ($sql$values);
  606.         $this->doQuery($sql);
  607.     }
  608.  
  609.     /**
  610.      * This method allows to know if the database connection is active
  611.      *
  612.      * @return boolean 
  613.      *
  614.      * @since 2.9.2
  615.      * @author H2LSOFT */
  616.     public function dbIsConnected()
  617.     {
  618.         return ($this->db[$this->db_index]);
  619.     }
  620.  
  621.     /**
  622.      * This method allows you to generate and execute delete query from an associative array.
  623.      *
  624.      * @param string $table 
  625.      * @param string $where 
  626.      *
  627.      * @return boolean 
  628.      *
  629.      * @since 2.8
  630.      * @author H2LSOFT */
  631.     public function dbDelete($table$where='')
  632.     {
  633.         if(!empty($where))
  634.             $where 'WHERE '.$where;
  635.  
  636.         $sql "DELETE FROM $table $where";
  637.  
  638.         return $this->doQuery($sql);
  639.     }
  640.  
  641.     /**
  642.      * This method returns the last SQL error
  643.      *
  644.      * @return string 
  645.      *
  646.      * @see doQuery
  647.      *
  648.      * @author H2LSOFT */
  649.     public function dbGetError()
  650.     {
  651.         return $this->DBLastError;
  652.     }
  653.  
  654.     /**
  655.      * this method allows to return database query results count.
  656.      *
  657.      * @return int 
  658.      *
  659.      * @deprecated
  660.      * @see DBNumRows
  661.      * @author H2LSOFT */
  662.     public function getRowsCount()
  663.     {
  664.         if(!is_object($this->req[$this->req_index]))
  665.         {
  666.             $this->dbError(2.2);
  667.             return;
  668.         }
  669.  
  670.         $rows_count $this->req[$this->req_index]->rowCount();
  671.         return $rows_count;
  672.     }
  673.  
  674.     /**
  675.      * this method allows to return database query results count.
  676.      *
  677.      * @return int 
  678.      *
  679.      * @since 2.9
  680.      * @author H2LSOFT */
  681.     public function dbNumRows()
  682.     {
  683.         return $this->getRowsCount();
  684.     }
  685.  
  686.     /**
  687.      * allows to obtain in structured array form database query results.
  688.      *
  689.      * It is different in the array created which has the fields as keys in the array.
  690.      *
  691.      * The array depends by DoQuery() parameter by default associative array is created.
  692.      *
  693.      * @return array 
  694.      * @author H2LSOFT */
  695.     public function  dbFetch()
  696.     {
  697.         if(!is_object($this->req[$this->req_index]))
  698.         {
  699.             $this->dbError(2.2);
  700.             return;
  701.         }
  702.         $row $this->req[$this->req_index]->fetch(PDO::FETCH_ASSOC);
  703.         return $row;
  704.     }
  705.  
  706.     /**
  707.      * method allows to obtain as array form database query results.
  708.      *
  709.      * @return array 
  710.      *
  711.      * @since 2.8
  712.      * @see DBFetch()
  713.      * @author H2LSOFT */
  714.     public function dbFetchArray()
  715.     {
  716.         if(!is_object($this->req[$this->req_index]))
  717.         {
  718.             $this->dbError(2.2);
  719.             return;
  720.         }
  721.  
  722.         $row $this->req[$this->req_index]->fetch(PDO::FETCH_NUM);
  723.         return $row;
  724.     }
  725.  
  726.     /**
  727.      * allows to obtain in structured array form database query results.
  728.      *
  729.      * It is different in the array created which has the fields as keys in the array.
  730.      *
  731.      * @return array 
  732.      *
  733.      * @since 2.8
  734.      * @author H2LSOFT */
  735.     public function dbFetchAssoc()
  736.     {
  737.         if(!is_object($this->req[$this->req_index]))
  738.         {
  739.             $this->dbError(2.2);
  740.             return;
  741.         }
  742.         $row $this->req[$this->req_index]->fetch(PDO::FETCH_ASSOC);
  743.         return $row;
  744.     }
  745.  
  746.     /**
  747.      * allows to release memory allocated for your server.
  748.      *
  749.      * For example, you can to use this method after long treatments.
  750.      *
  751.      * @author H2LSOFT */
  752.     public function dbFreeResult()
  753.     {
  754.         if(!is_object($this->req[$this->req_index]))
  755.         {
  756.             $this->dbError(2.2);
  757.             return;
  758.         }
  759.  
  760.         $this->req[$this->req_indexnull;
  761.     }
  762.  
  763.     /**
  764.      * This method allows to get your recorset in one time
  765.      *
  766.      * @deprecated
  767.      * @return array 
  768.      * @see dbGetData
  769.      * @author H2LSOFT */
  770.     public function getData()
  771.     {
  772.         $i 0;
  773.         $results array();
  774.         while($row $this->dbFetchAssoc())
  775.             $results[$row;
  776.  
  777.         $this->dbFreeResult();
  778.  
  779.         return $results;
  780.     }
  781.  
  782.     /**
  783.      * This method allows to get your recorset in one time
  784.      *
  785.      * @return array 
  786.      *
  787.      * @since 2.8
  788.      * @author H2LSOFT */
  789.     public function dbGetData()
  790.     {
  791.         return $this->getData();
  792.     }
  793.  
  794.     /**
  795.      * @see dbGetOne()
  796.      *
  797.      * @deprecated
  798.      * @return int 
  799.      * @author H2LSOFT */
  800.     public function getOne()
  801.     {
  802.         $res $this->dbFetchArray();
  803.         $this->dbFreeResult();
  804.         
  805.         if(count($res== 0)
  806.             return '';
  807.         else        
  808.             return @$res[0];
  809.     }
  810.  
  811.     /**
  812.      * method allows to return the first result of a database query.
  813.      *
  814.      * This method is useful for a count database query for exemple.
  815.      *
  816.      * @return int 
  817.      * @author H2LSOFT */
  818.     public function dbGetOne()
  819.     {
  820.         return $this->getOne();
  821.     }
  822.  
  823.     /**
  824.      * this method allows to return the database query fields number.
  825.      *
  826.      * @deprecated
  827.      * @return int 
  828.      * @see dbGetNumFields
  829.      * @author H2LSOFT */
  830.     public function getNumFields()
  831.     {
  832.         if(!is_object($this->req[$this->req_index]))
  833.         {
  834.             $this->dbError(2.2);
  835.             return;
  836.         }
  837.  
  838.         $fields_count $this->req[$this->req_index]->columnCount();
  839.         return $fields_count;
  840.     }
  841.  
  842.     /**
  843.      * allows you to return the database query fields number
  844.      *
  845.      * @return int 
  846.      * @author H2LSOFT */
  847.     public function dbGetNumFields()
  848.     {
  849.         return $this->getNumFields();
  850.     }
  851.  
  852.     /**
  853.      * this method allows to return the database query fields name.
  854.      *
  855.      * @return array 
  856.      * @deprecated
  857.      * @since 1.5
  858.      * @see dbGetFields
  859.      * @author H2LSOFT */
  860.     public function getFields()
  861.     {
  862.         return array_keys($this->req[$this->req_index]->fields);
  863.     }
  864.  
  865.     /**
  866.      * this method allows to return the database query fields name.
  867.      *
  868.      * @return array 
  869.      * @author H2LSOFT */
  870.     public function dbGetFields()
  871.     {
  872.         return $this->getFields();
  873.     }
  874.  
  875.     /**
  876.      * allows to return the field name from your database query.
  877.      *
  878.      * @deprecated
  879.      * @param int $field_no 
  880.      * @return string 
  881.      * @see dbGetFieldName
  882.      * @author H2LSOFT */
  883.     public function getFieldName($field_no)
  884.     {
  885.         if(!is_object($this->req[$this->req_index]))
  886.         {
  887.             $this->dbError(2.2);
  888.             return;
  889.         }
  890.  
  891.         if($field_no || $field_no >= $this->getNumFields())
  892.         {
  893.             $this->dbError(2.3$field_no);
  894.             return;
  895.         }
  896.  
  897.         // take the first line
  898.         $row $this->DBFetchAssoc();
  899.         // $row =  $this->req[$this->req_index]->fetchColumn();
  900.         $col array_keys($row);
  901.  
  902.         return $col[$field_no];
  903.     }
  904.  
  905.     /**
  906.      * allows to return the field name from the database query.
  907.      *
  908.      * @param int $field_no 
  909.      *
  910.      * @return string 
  911.      * @author H2LSOFT */
  912.     public function dbGetFieldName($field_no)
  913.     {
  914.         return $this->getFieldName($field_no);
  915.     }
  916.  
  917.     /**
  918.      * this method allows to obtain the database names of the server.
  919.      *
  920.      * @deprecated
  921.      * @return array 
  922.      * @see dbGetDatabaseList
  923.      * @author H2LSOFT */
  924.     public function getDbList()
  925.     {
  926.         if(!is_object($this->db[$this->db_index]))
  927.         {
  928.             $this->dbError(0.1);
  929.             return;
  930.         }
  931.         $this->doQuery('SHOW DATABASES');
  932.         $dbs array();
  933.         while($row $this->dbFetchArray())
  934.         {
  935.             $dbs[$row[0];
  936.         }
  937.  
  938.         return $dbs;
  939.     }
  940.  
  941.     /**
  942.      * this method allows to obtain the database names of the server.
  943.      *
  944.      * @return array 
  945.      * @author H2LSOFT */
  946.     public function dbGetDatabaseList()
  947.     {
  948.         return $this->getDBList();
  949.     }
  950.  
  951.     /**
  952.      * this method allows to obtain arrays name of the database.
  953.      * @deprecated
  954.      * @see dbGetTableList
  955.      * @author H2LSOFT */
  956.     public function getTableList()
  957.     {
  958.         if(!is_object($this->db[$this->db_index]))
  959.         {
  960.             $this->dbError(0.1);
  961.             return;
  962.         }
  963.  
  964.         $this->doQuery('SHOW TABLES');
  965.         while($row $this->dbFetchArray())
  966.             $res[$row[0];
  967.  
  968.         return $res;
  969.     }
  970.  
  971.     /**
  972.      * this method allows to obtain arrays name of the database.
  973.      *
  974.      * @return array 
  975.      * @author H2LSOFT */
  976.     public function dbGetTableList()
  977.     {
  978.         return $this->getTableList();
  979.     }
  980.  
  981. // constuction the request
  982.     /**
  983.      * en commentaire
  984.      * @param string $query 
  985.      * @author H2LSOFT */
  986.     protected function setQuery($query)
  987.     {
  988.         // encodage de la requete
  989.         // $query = str_replace("\n",' ',$query);
  990.         // $query = str_replace("\r",' ',$query);
  991.  
  992.     /*if(count($this->cons_query) > 0 || is_array($this->user_resulset)) return;
  993.  
  994.     $this->cons_query['STRING'] = $query;
  995.     // on parcours la requete ï¿½ l'envers ;-)'
  996.     // limit ?
  997.     if(preg_match("/LIMIT(.*)/msi", $query, $match))
  998.     {
  999.         $this->cons_query['LIMIT'] = trim($match[1]);
  1000.         $query = str_replace($match[0], '', $query); // on remplace ds la chaine
  1001.     }
  1002.     // order by
  1003.     if(preg_match("/ORDER BY(.*)/msi", $query, $match))
  1004.     {
  1005.         $this->cons_query['ORDER BY'] = trim($match[1]);
  1006.         $query = str_replace($match[0], '', $query); // on remplace ds la chaine
  1007.  
  1008.         // y a t il une clause speciale ? order by filter ?
  1009.         if(isset($_GET['torder_by']))
  1010.             $this->cons_query['ORDER BY'] = "{$_GET['torder_by']} {$_GET['tsens']}, ".$this->cons_query['ORDER BY'];
  1011.     }
  1012.     else
  1013.     {
  1014.         if(isset($_GET['torder_by']))
  1015.             $this->cons_query['ORDER BY'] = "{$_GET['torder_by']} {$_GET['tsens']}";
  1016.     }
  1017.  
  1018.  
  1019.     // group by
  1020.     if(preg_match("/GROUP BY(.*)/msi", $query, $match))
  1021.     {
  1022.         $this->cons_query['GROUP BY'] = trim($match[1]);
  1023.         $query = str_replace($match[0], '', $query); // on remplace ds la chaine
  1024.     }
  1025.     // where
  1026.     if(preg_match("/WHERE(.*)/msi", $query, $match))
  1027.     {
  1028.         $this->cons_query['WHERE'] = trim($match[1]);
  1029.         $query = str_replace($match[0], '', $query); // on remplace ds la chaine
  1030.     }
  1031.     // from
  1032.     if(!preg_match("/FROM(.*)/msi", $query, $match))$this->_DBError(4);
  1033.  
  1034.     $this->cons_query['FROM'] = trim($match[1]);
  1035.     $query = str_replace($match[0], '', $query); // on remplace ds la chaine
  1036.     // select
  1037.     if(!preg_match("/SELECT(.*)/msi", $query, $match))$this->_DBError(3);
  1038.  
  1039.     $this->cons_query['SELECT'] = trim($match[1]);
  1040.     $query = str_replace($match[0], '', $query); // on remplace ds la chaine
  1041.     * @author H2LSOFT */
  1042.     }
  1043.  
  1044. // recreate a request with the new parameters of LIMIT.
  1045.     /**
  1046.      *
  1047.      * @author H2LSOFT */
  1048.     protected function setQueryLimit()
  1049.     {
  1050.         if(empty($this->PageNumber))
  1051.         {
  1052.             $this->PageNumber = 1;
  1053.         }
  1054.         // calcul du nombre de pages
  1055.         if($this->NbRecordPerPage == 0)
  1056.         {
  1057.             $this->PageCount = 1;
  1058.             $this->PageNumber = 1;
  1059.             $this->First = 1;
  1060.             $this->T_first = $this->First;
  1061.             $this->Last = $this->Count;
  1062.         }
  1063.         else
  1064.         {
  1065.             $this->PageCount = ceil($this->Count / $this->NbRecordPerPage)// arrondi a l'entier superieur
  1066.             if($this->PageNumber > $this->PageCount)
  1067.             {
  1068.                 $this->PageNumber = $this->PageCount;
  1069.             }
  1070.  
  1071.             // on determine debut du limit
  1072.             $this->First = ($this->PageNumber - 1$this->NbRecordPerPage;
  1073.             $this->T_first = $this->First ;
  1074.             $this->Last = $this->First + $this->NbRecordPerPage;
  1075.         }
  1076.  
  1077.     /* v2.9
  1078.     if(!is_array($this->user_resulset))
  1079.     {
  1080.         // on reconstruit la requete
  1081.         $query = "SELECT \n";
  1082.             $query .= $this->cons_query['SELECT']." \n";
  1083.         $query .= " FROM \n";
  1084.         $query .= $this->cons_query['FROM']." \n";
  1085.         // Where
  1086.         if(!empty($this->cons_query['WHERE']))
  1087.         {
  1088.             $query .= ' WHERE '." \n";
  1089.             $query .= $this->cons_query['WHERE']." \n";
  1090.         }
  1091.         // Group By
  1092.         if(!empty($this->cons_query['GROUP BY']))
  1093.         {
  1094.             $query .= ' GROUP BY '." \n";
  1095.             $query .= $this->cons_query['GROUP BY']." \n";
  1096.         }
  1097.         // Order By
  1098.         if(!empty($this->cons_query['ORDER BY']))
  1099.         {
  1100.             $query .= ' ORDER BY '." \n";;
  1101.             $query .= $this->cons_query['ORDER BY']." \n";
  1102.         }
  1103.     }
  1104.     * @author H2LSOFT */
  1105.  
  1106.         // limit ?
  1107.         if($this->NbRecordPerPage > 0)
  1108.         {
  1109.             // if($this->First == 0){$this->First = 1;}
  1110.             $this->First++;
  1111.             if($this->Last > $this->Count)
  1112.             {
  1113.                 $this->Last = $this->Count;
  1114.             }
  1115.  
  1116.         /* v2.9
  1117.         if(!is_array($this->user_resulset))
  1118.             $query .= " LIMIT  $this->First,$this->NbRecordPerPage";
  1119.         * @author H2LSOFT */
  1120.         }
  1121.  
  1122.     /* v2.9
  1123.     if(!is_array($this->user_resulset))
  1124.         return $query;
  1125.     * @author H2LSOFT */
  1126.     }
  1127.  
  1128.  
  1129.     /**
  1130.      * return the correct formatted url
  1131.      * @param string $t_pg 
  1132.      *
  1133.      * @return string 
  1134.      * @author H2LSOFT */
  1135.     protected function SRgetUrl($t_pg '')
  1136.     {        
  1137.         $url $this->Url."tpg=$t_pg".$this->url_var;        
  1138.         if(!empty($this->UrlRgxPatterns&& !empty($this->UrlRgxReplace))
  1139.             $url @preg_replace('/'.$this->UrlRgxPatterns.'/i'$this->UrlRgxReplace$url);
  1140.  
  1141.         return $url;
  1142.     }
  1143.  
  1144.     /**
  1145.      * apply rewrite rule in URL
  1146.      *
  1147.      * @param string $patterns 
  1148.      * @param string $replace 
  1149.      * @author H2LSOFT */
  1150.     public function rewriteUrl($patterns$replace)
  1151.     {
  1152.         $this->UrlRgxPatterns = $patterns;
  1153.         $this->UrlRgxReplace = $replace;
  1154.     }
  1155.  
  1156.     /**
  1157.      * asign color alternation
  1158.      *
  1159.      * @param string $color1 
  1160.      * @param string $color2 
  1161.      * @author H2LSOFT */
  1162.     public function setNavColor($color1$color2)
  1163.     {
  1164.         $this->NavColorFirst = $color1;
  1165.         $this->NavColorSecond = $color2;
  1166.     }
  1167.  
  1168. // prise totale des r�sulats
  1169.     /**
  1170.      * en commentaire
  1171.      * @author H2LSOFT */
  1172.     protected function getTotalCount()
  1173.     {
  1174.     /* v2.9
  1175.  
  1176.     $query = 'SELECT ';
  1177.  
  1178.     // on regarde s'il y a un distinct
  1179.     $distinct = false;
  1180.     $d = trim($this->cons_query['SELECT']);
  1181.     if(strpos($d, 'DISTINCT') === false)
  1182.         $query .= ' COUNT(*)';
  1183.     else
  1184.     {
  1185.         $distinct = true;
  1186.         // on capture le champs qui possede le distinct
  1187.         list($f) = explode(',', $this->cons_query['SELECT']);
  1188.         list($f) = explode(' AS ', $f);
  1189.         $query .= " COUNT($f) ";
  1190.     }
  1191.  
  1192.     $query .= 'FROM ';
  1193.     $query .= $this->cons_query['FROM'];
  1194.  
  1195.     if(!empty($this->cons_query['WHERE']))
  1196.     {
  1197.         $query .= ' WHERE ';
  1198.         $query .= $this->cons_query['WHERE'];
  1199.     }
  1200.  
  1201.     $this->DoQuery($query) or die($this->DBGetError());
  1202.  
  1203.     // group by sans clause distinct
  1204.     if(eregi('group by', $query) && !$distinct)
  1205.         $this->Count = $this->getRowsCount();
  1206.     else
  1207.         $this->Count = $this->GetOne();
  1208.     * @author H2LSOFT */
  1209.     }
  1210.  
  1211.     /**
  1212.      *
  1213.      * @return boolean 
  1214.      * @author H2LSOFT */
  1215.     protected function applyPrivateVar()
  1216.     {
  1217.         if(empty($this->Url))
  1218.             $this->Url $_SERVER['PHP_SELF'].'?';        
  1219.  
  1220.         // TPLN variable
  1221.         if($this->itemExists('_Count''data'))
  1222.             $this->parse('data._Count'$this->Count);
  1223.  
  1224.         if($this->itemExists('_First''data'))
  1225.             $this->Parse('data._First'$this->First);
  1226.  
  1227.         if($this->itemExists('_Last''data'))
  1228.             $this->parse('data._Last'$this->Last);
  1229.  
  1230.         if($this->itemExists('_PageCount''data'))
  1231.             $this->parse('data._PageCount'$this->PageCount);
  1232.  
  1233.         if($this->itemExists('_PageNumber''data'))
  1234.             $this->parse('data._PageNumber'$this->PageNumber);
  1235.  
  1236.         // order by ?
  1237.         for($i=0$i count($this->OrderByFields)$i++)
  1238.         {
  1239.             $f $this->OrderByFields[$i];
  1240.             $img '';
  1241.             $tpg (isset($_GET['tpg'])) $_GET['tpg'1;
  1242.             $u $this->SRgetUrl($tpg);
  1243.             if($_GET['torder_by'== $f)
  1244.             {
  1245.                 $sens ($_GET['tsens'== 'asc''desc' 'asc';
  1246.                 $u str_replace("tsens={$_GET['tsens']}""tsens=$sens"$u);
  1247.                 $img "<a href=\"$u\"><img src=\"$this->OrderByImgsPath/order_{$_GET['tsens']}_actived.gif\" style=\"border:0;\" align=\"absmiddle\" /></a>";
  1248.             }
  1249.             else
  1250.             {
  1251.                 $u str_replace("torder_by={$_GET['torder_by']}&tsens={$_GET['tsens']}""torder_by=$f&tsens=desc"$u);
  1252.                 $img "<a href=\"$u\"><img src=\"$this->OrderByImgsPath/order_desc.gif\" style=\"border:0;\" /></a>";
  1253.                 //$u = str_replace("torder_by={$_GET['torder_by']}&tsens={$_GET['tsens']}", "torder_by=$f&tsens=asc", $u);
  1254.                 //$img .= "<a href=\"$u\"><img src=\"$this->OrderByImgsPath/order_asc.gif\" style=\"border:0;\" /></a>";
  1255.             }
  1256.  
  1257.             $this->parse("data._order_by::$f"$img);
  1258.         }
  1259.  
  1260.  
  1261.  
  1262.         // PREVIOUS
  1263.         if($this->blocExists('previous'))
  1264.         {
  1265.             if(!$this->itemExists('_Url''previous'))
  1266.             {
  1267.                 $this->error('1.1'$this->f[$this->f_no]['name']'previous''_Url');
  1268.                 return;
  1269.             }
  1270.  
  1271.             if($this->PageNumber <= $this->PageCount /*&& $this->PageCount != 1 && $this->PageNumber != 1* @author H2LSOFT */)
  1272.             {
  1273.                 $prev_pg $this->PageNumber - 1;
  1274.                 if($prev_pg 1)$prev_pg 1;
  1275.                 $url $this->SRgetUrl($prev_pg);                
  1276.                 $this->parse('data.previous._Url'$url);                
  1277.             }
  1278.             else
  1279.                 $this->eraseBloc('data.previous');
  1280.         }
  1281.  
  1282.         // NEXT
  1283.         if($this->blocExists('next'))
  1284.         {
  1285.             if(!$this->itemExists('_Url''next'))
  1286.             {
  1287.                 $this->error('1.1'$this->f[$this->f_no]['name']'next''_Url');
  1288.                 return;
  1289.             }
  1290.  
  1291.         /*if($this->PageNumber < $this->PageCount /*&& $this->PageCount != 1)
  1292.         {
  1293.         * @author H2LSOFT */    $next_pg $this->PageNumber + 1;
  1294.             $url $this->SRgetUrl($next_pg);
  1295.             $this->parse('data.next._Url'$url);
  1296.         /*}
  1297.         else
  1298.             $this->EraseBloc('data.next');
  1299.         * @author H2LSOFT */
  1300.         }
  1301.  
  1302.         // START
  1303.         if($this->blocExists('start'))
  1304.         {
  1305.             if(!$this->itemExists('_Url''start'))
  1306.             {
  1307.                 $this->error('1.1'$this->f[$this->f_no]['name']'start''_Url');
  1308.                 return;
  1309.             }
  1310.  
  1311.             //if($this->PageCount > 1)
  1312.             //{
  1313.             $url $this->SRgetUrl(1);
  1314.             $this->parse('data.start._Url'$url);
  1315.             //}
  1316.             //else
  1317.             //$this->EraseBloc('data.start');
  1318.         }
  1319.  
  1320.         // END
  1321.         if($this->blocExists('end'))
  1322.         {
  1323.             if(!$this->itemExists('_Url''end'))
  1324.             {
  1325.                 $this->error('1.1'$this->f[$this->f_no]['name']'end''_Url');
  1326.                 return;
  1327.             }
  1328.  
  1329.             //if($this->PageCount > 1)
  1330.             //{
  1331.             $url $this->SRgetUrl($this->PageCount);
  1332.             $this->parse('data.end._Url'$url);
  1333.             //}
  1334.             //else
  1335.             //$this->EraseBloc('data.end');
  1336.         }
  1337.  
  1338.         // PAGER
  1339.         if($this->blocExists('pager'))
  1340.         {
  1341.             // block out & in
  1342.             if(!$this->blocExists('out'))
  1343.             {
  1344.                 $this->error(2$this->f[$this->f_no]['name']'out');
  1345.                 return;
  1346.             }
  1347.             if(!$this->itemExists('_Url''out'))
  1348.             {
  1349.                 $this->error('1.1'$this->f[$this->f_no]['name']'out''_Url');
  1350.                 return;
  1351.             }
  1352.             if(!$this->itemExists('_Page''out'))
  1353.             {
  1354.                 $this->error('1.1'$this->f[$this->f_no]['name']'out''_Page');
  1355.                 return;
  1356.             }
  1357.  
  1358.             if(!$this->blocExists('in'))
  1359.             {
  1360.                 $this->error(2$this->f[$this->f_no]['name']'in');
  1361.                 return;
  1362.             }
  1363.  
  1364.             if(!$this->itemExists('_Page''in'))
  1365.             {
  1366.                 $this->error('1.1'$this->f[$this->f_no]['name']'in''_Page');
  1367.                 return;
  1368.             }
  1369.  
  1370.             if($this->PageCount >= 1)
  1371.             {
  1372.                 $in $this->getBlocInFile('data.pager.in');
  1373.                 $out $this->getBlocInFile('data.pager.out');
  1374.                 // $this->eraseBloc("in");
  1375.                 // $this->eraseBloc("out");
  1376.                 $str '';
  1377.  
  1378.                 for($l 1$l <= $this->PageCount$l++)
  1379.                 {
  1380.                     if($l == $this->PageNumber)
  1381.                     {
  1382.                         $str .= str_replace('{_Page}'$l$in);
  1383.                     }
  1384.                     else
  1385.                     {
  1386.                         $url $this->SRgetUrl($l);
  1387.  
  1388.                         $tmp str_replace('{_Page}'$l$out);
  1389.                         $tmp str_replace('{_Url}'$url$tmp);
  1390.  
  1391.                         $str .= $tmp;
  1392.                     }
  1393.                 }
  1394.  
  1395.                 $this->parseBloc('data.pager'$str);
  1396.             }
  1397.             else
  1398.             {
  1399.                 $this->eraseBloc('data.pager');
  1400.             }
  1401.         }
  1402.  
  1403.         // initialize the navigation
  1404.         if(empty($this->NavColorFirst))
  1405.         {
  1406.             $this->NavColorFirst = TPLN_DB_NavColorFirst;
  1407.         }
  1408.         if(empty($this->NavColorSecond))
  1409.         {
  1410.             $this->NavColorSecond = TPLN_DB_NavColorSecond;
  1411.         }
  1412.     }
  1413.  
  1414.     /**
  1415.      * change URL
  1416.      *
  1417.      * @param string $url 
  1418.      * @author H2LSOFT */
  1419.     public function setUrl($url)
  1420.     {
  1421.         $this->Url $url;
  1422.     }
  1423.  
  1424.     /**
  1425.      * add variable in URL
  1426.      *
  1427.      * @param string $url_add 
  1428.      * @author H2LSOFT */
  1429.     public function urlAddVar($url_add)
  1430.     {
  1431.         $this->url_var = "&$url_add";
  1432.     }
  1433.  
  1434.     /**
  1435.      * this method allows to create field variables
  1436.      * @param array $path 
  1437.      * @param array $fields 
  1438.      *
  1439.      * @return boolean 
  1440.      * @author H2LSOFT */
  1441.     public function createFieldVars($path$fields)
  1442.     {
  1443.         // path defined en array
  1444.         if(is_array($path))
  1445.         {
  1446.             for($i 0;$i count($path);$i++)
  1447.             {
  1448.                 $this->createFieldVars($path[$i]$fields);
  1449.             }
  1450.             return;
  1451.         }
  1452.  
  1453.         $arr @explode('.'$path);
  1454.  
  1455.         if(count($arr== 1)
  1456.         {
  1457.             $this->error('13'$this->f[$this->f_no]['name']$arr[0]);
  1458.             return;
  1459.         }
  1460.  
  1461.         $lastbloc $arr[count($arr)-1];
  1462.  
  1463.         // Verification du mot clefs _Field
  1464.         if(!$this->itemExists('_Field'$lastbloc&& !$this->itemExists('_FieldLabel'$lastbloc))
  1465.         {
  1466.             $this->error('1.1'$this->f[$this->f_no]['name']$lastbloc'_Field');
  1467.             return;
  1468.         }
  1469.  
  1470.         $cur_bloc_ini $this->getBlocInFile($lastbloc);
  1471.         $str '';
  1472.         $all '';
  1473.         $tab array();
  1474.  
  1475.         for($i 0;$i count($fields);$i++)
  1476.         {
  1477.             // on remplace le mot clef field
  1478.             $str str_replace('{_Field}''{'.$fields[$i].'}'$cur_bloc_ini);
  1479.             $str str_replace('{_FieldId}'$i$str);
  1480.             $str str_replace('{_FieldLabel}'str_replace('_'' 'ucfirst($fields[$i]))$str);
  1481.             $all .= $str;
  1482.         }
  1483.         // on remplace le contenu du champs
  1484.         // $this->ParseBloc($path, $all);
  1485.         $this->parseBloc($lastbloc$all);
  1486.         // on retire le dernier du path
  1487.         // on veut que le chemin des peres
  1488.         $path $this->getFathers($path'ARRAY'0);
  1489.         $path array_slice ($path0count($path)-1);
  1490.         $path join($path'.');
  1491.  
  1492.         $this->reloadBlocVars($path);
  1493.     }
  1494.  
  1495.     /**
  1496.      * This method will manage your filter automatically.
  1497.      *
  1498.      * You have to put a preformatted variable in your template {_order_by::my_field}.
  1499.      *
  1500.      * @param array $fields 
  1501.      * @param array $img_dir 
  1502.      *
  1503.      * @since 2.8
  1504.      * @author H2LSOFT */
  1505.     public function showRecordsOrderBy($fields$img_dir='')
  1506.     {
  1507.         $this->OrderByFields = $fields;
  1508.  
  1509.         if(!empty($img_dir))
  1510.             $this->OrderByImgsPath = $img_dir;
  1511.         else
  1512.             $this->OrderByImgsPath = TPLN_WEB_PATH.'/img';
  1513.  
  1514.         if(!isset($_GET['torder_by']|| !in_array($_GET['torder_by']$fields))
  1515.             $_GET['torder_by'$fields[0];
  1516.  
  1517.         if(!isset($_GET['tsens']|| !in_array($_GET['tsens']array('asc','desc')))
  1518.             $_GET['tsens''desc';
  1519.  
  1520.         $this->url_var .= "&torder_by={$_GET['torder_by']}&tsens={$_GET['tsens']}";
  1521.     }
  1522.  
  1523.     /**
  1524.      * allows to parse automatically the datas from your databasse,
  1525.      *
  1526.      * you have only to create your template and this method make all the tasks for you!
  1527.      *
  1528.      * @param string $query 
  1529.      * @param int $nb_result_per_page 
  1530.      * @param string $func_data 
  1531.      *
  1532.      * @return int 
  1533.      * @since TPLN 1.5
  1534.      * @author H2LSOFT */
  1535.     public function showRecords($query$nb_result_per_page 0$func_data '')
  1536.     {
  1537.         // check the second parameter
  1538.         if(!is_int($nb_result_per_page))
  1539.         {
  1540.             $this->dbError(5);
  1541.             return;
  1542.         }
  1543.         if($nb_result_per_page 0)
  1544.         {
  1545.             $this->dbError('5.1');
  1546.             return;
  1547.         }
  1548.  
  1549.         // check the results array
  1550.         $this->breaker_name = (!$this->blocExists('breaker')) '' 'breaker';
  1551.         $this->user_resulset = $query;
  1552.  
  1553.         $this->NbRecordPerPage = $nb_result_per_page;
  1554.  
  1555.         if($this->NbRecordPerPage != && isset($_GET['tpg']))
  1556.             $this->PageNumber = $_GET['tpg']// variable on parameter
  1557.         else
  1558.             $this->PageNumber = null;
  1559.  
  1560.         // sql version
  1561.         if(!is_array($this->user_resulset))
  1562.         {            
  1563.             // $this->setQuery(''); // reconstruit la requete et assigne les variables
  1564.             // $this->_GetTotalCount(); //  nb total d'enregistrements
  1565.  
  1566.             //$res =  $this->db[$this->db_index]->Execute($query);
  1567.             // $this->Count = $res->numRows();
  1568.             // $this->query_count++;
  1569.  
  1570.             // patch query by laurent hayoun ******************************
  1571.             // assign dynamic count values for large table
  1572.             $query_tmp $query;
  1573.             // $query_tmp = str_replace("\n", ' ', $query_tmp);
  1574.             $query_tmp str_replace("\r"' '$query_tmp);
  1575.             $this->cons_query['STRING'$query;
  1576.  
  1577.             // replace subqueries bu _TPLN_REP_ID_
  1578.             $reps_pattern array();
  1579.             $reps_rep array();
  1580.  
  1581.             $query_tmp2 $query_tmp;
  1582.             $query_tmp2 str_replace(')'' )'$query_tmp2);
  1583.             $query_tmp2 str_replace('* )''*)'$query_tmp2);
  1584.             $query_tmp2 str_replace(' ) ) '') ) '$query_tmp2);
  1585.  
  1586.  
  1587.             //preg_match_all("#\(SELECT(.*)\)#msU", $query_tmp, $matches);
  1588.             preg_match_all("#\(SELECT (.*) \) #"$query_tmp2$matches);            
  1589.  
  1590.             if(isset($matches[0]))
  1591.             {
  1592.                 for($i=0$i count($matches[0])$i++)
  1593.                 {
  1594.                     $reps_pattern['_TPLN_REP_'.$i.'_';
  1595.                     $reps_rep[$matches[0][$i];
  1596.                 }
  1597.                 $query_tmp2 str_replace($reps_rep$reps_pattern$query_tmp2);
  1598.             }
  1599.  
  1600.             // get LIMIT
  1601.             if(preg_match("/LIMIT(.*)/s"$query_tmp2$match))
  1602.             {
  1603.                 $str str_replace($reps_pattern$reps_rep$match[1]);
  1604.                 $this->cons_query['LIMIT'trim($str);
  1605.                 $query_tmp2 str_replace($match[0]''$query_tmp2)// delete string
  1606.             }
  1607.             // get ORDER BY
  1608.             if(preg_match("/ORDER BY(.*)/s"$query_tmp2$match))
  1609.             {
  1610.                 $str str_replace($reps_pattern$reps_rep$match[1]);
  1611.                 $this->cons_query['ORDER BY'trim($str);
  1612.                 $query_tmp2 str_replace($match[0]''$query_tmp2)// delete string
  1613.  
  1614.                 // special clause
  1615.                 if(isset($_GET['torder_by']))
  1616.                 {
  1617.                     $t_sens strtoupper($_GET['tsens']);
  1618.                     $this->cons_query['ORDER BY'"{$_GET['torder_by']} $t_sens".$this->cons_query['ORDER BY'];
  1619.                 }
  1620.             }
  1621.             else
  1622.             {
  1623.                 if(isset($_GET['torder_by']))
  1624.                 {
  1625.                     $t_sens strtoupper($_GET['tsens']);
  1626.                     $this->cons_query['ORDER BY'"{$_GET['torder_by']} $t_sens";
  1627.                 }
  1628.             }
  1629.             // get GROUP BY
  1630.             if(preg_match("/GROUP BY(.*)/s"$query_tmp2$match))
  1631.             {
  1632.                 $str str_replace($reps_pattern$reps_rep$match[1]);
  1633.                 $this->cons_query['GROUP BY'trim($str);
  1634.                 $query_tmp2 str_replace($match[0]''$query_tmp2)// delete string
  1635.             }
  1636.             // get WHERE
  1637.             if(preg_match("/WHERE(.*)/s"$query_tmp2$match))
  1638.             {
  1639.                 $str str_replace($reps_pattern$reps_rep$match[1]);
  1640.                 $this->cons_query['WHERE'trim($str);
  1641.                 $query_tmp2 str_replace($match[0]''$query_tmp2)// delete string
  1642.             }
  1643.             // get FROM
  1644.             if(preg_match("/FROM(.*)/s"$query_tmp2$match))
  1645.             {
  1646.                 $str str_replace($reps_pattern$reps_rep$match[1]);
  1647.                 $this->cons_query['FROM'trim($str);
  1648.                 $query_tmp2 str_replace($match[0]''$query_tmp2)// delete string
  1649.             }
  1650.             // get SELECT
  1651.             if(preg_match("/SELECT(.*)/s"$query_tmp2$match))
  1652.             {
  1653.                 $str str_replace($reps_pattern$reps_rep$match[1]);
  1654.                 $this->cons_query['SELECT'trim($str);
  1655.                 $query_tmp2 str_replace($match[0]''$query_tmp2)// delete string
  1656.             }
  1657.             $this->cons_query['COUNT'"*";
  1658.  
  1659.             // get total count query
  1660.             $sql_tmp "SELECT
  1661.                                COUNT({$this->cons_query['COUNT']})
  1662.                         FROM
  1663.                                 {$this->cons_query['FROM']} ";
  1664.             if(!empty($this->cons_query['WHERE']))
  1665.             {
  1666.                 $sql_tmp .= "WHERE
  1667.                     {$this->cons_query['WHERE']} ";
  1668.             }
  1669.             if(!empty($this->cons_query['GROUP BY']))
  1670.             {
  1671.                 $sql_tmp .= "GROUP BY
  1672.                     {$this->cons_query['GROUP BY']} ";
  1673.             }
  1674.  
  1675.             // hack prevent SQL_CALC_NUM_ROWS
  1676.             if(strpos($query'SQL_CALC_FOUND_ROWS'!== false)
  1677.             {
  1678.                 $sql_tmp 'SELECT FOUND_ROWS()';
  1679.             }            
  1680.  
  1681.  
  1682.             $this->doQuery($sql_tmp);
  1683.             $this->Count = $this->getOne();            
  1684.  
  1685.             // end of patch query by laurent hayoun ***********************
  1686.  
  1687.             // y a t il une clause speciale ? order by filter ?
  1688.             if(isset($_GET['torder_by']&& in_array($_GET['torder_by']$this->OrderByFields))
  1689.             {
  1690.                 // clause of the end ?
  1691.                 if(preg_match("#(.*)ORDER BY(.*)$#si"$query))
  1692.                 {
  1693.                     $query .= " ,{$_GET['torder_by']} {$_GET['tsens']}";
  1694.                 }
  1695.                 else
  1696.                 {
  1697.                     $query .= " ORDER BY {$_GET['torder_by']} {$_GET['tsens']}";
  1698.                 }
  1699.             }
  1700.  
  1701.  
  1702.             // $this->doQuery($query);
  1703.             // $this->Count = $this->dbNumRows();
  1704.  
  1705.             $this->setQueryLimit()// request which conyains the limits
  1706.  
  1707.             
  1708.             $tmp_nb_result_per_page $nb_result_per_page;
  1709.             if($tmp_nb_result_per_page == 0)$tmp_nb_result_per_page = -1;        
  1710.             $limit_start ($this->PageNumber-1$nb_result_per_page;
  1711.             if($limit_start 0)$limit_start 0;
  1712.             if($nb_result_per_page == 0)
  1713.             {
  1714.                 $query_limit $query;
  1715.             }
  1716.             else
  1717.             {
  1718.                 $query_limit $query." LIMIT ".$limit_start.', '.$tmp_nb_result_per_page;
  1719.             }
  1720.             
  1721.             $this->doQuery($query_limit);
  1722.  
  1723.         }
  1724.         else
  1725.         {
  1726.             $this->Count = count($this->user_resulset);
  1727.             $this->setQuery('')// rebuild the request and assign the variables
  1728.         }
  1729.  
  1730.         // no records
  1731.         if($this->Count == 0)
  1732.         {
  1733.             $no_records $this->getBlocInFile('data.norecord');
  1734.             $this->eraseBloc('data.norecord');
  1735.             $this->parseBloc('data'$no_records);
  1736.  
  1737.             $this->cons_query = array();
  1738.             return $this->Count;
  1739.         }
  1740.  
  1741.         $sql_results array();
  1742.         if(!is_array($this->user_resulset))
  1743.         {
  1744.             $this->NbResults = $this->dbNumRows()// resultats obtenues != $this->count
  1745.             $sql_results $this->dbGetData()// recupere les résultats
  1746.             
  1747.         }
  1748.         else
  1749.         {
  1750.             $this->setQueryLimit()// requete contenant les limites
  1751.             $this->NbResults = count($this->user_resulset);
  1752.         }
  1753.  
  1754.         if($this->NbResults == 0// il y'en a pas pour une recherche
  1755.         {
  1756.             // to define for the research
  1757.             $no_records $this->getBlocInFile('data.norecord');
  1758.             $this->eraseBloc('data.norecord');
  1759.             $this->parseBloc('data'$no_records);
  1760.  
  1761.             $this->cons_query = array();
  1762.             return $this->Count;
  1763.         }
  1764.         else // if there is results then
  1765.         {
  1766.             $this->eraseBloc('data.norecord')// erase norecord
  1767.             $this->applyPrivateVar()// modify ths items propriaitaire $Count...
  1768.             if(!is_array($this->user_resulset&& count($this->f[$this->f_no]['shortcut_blocs']['loop']['items']== 0)
  1769.             {
  1770.                 $this->cons_query = array();
  1771.                 return $this->Count;
  1772.             }
  1773.             // parsing
  1774.             $this->T_i = 0// for the  _NavColor
  1775.             $this->T_id = $this->T_first// for _Id
  1776.  
  1777.             if($this->T_id == 0)$this->T_id = 1;
  1778.             // $this->T_id++;
  1779.  
  1780.             $this->last_header = '';
  1781.             if(!is_array($this->user_resulset))
  1782.             {
  1783.                 // while($row = $this->dbFetchAssoc())
  1784.                 foreach($sql_results as $row)
  1785.                 {
  1786.                     $this->trtShowRecords($row$func_data);
  1787.                 }
  1788.             }
  1789.             else
  1790.             {
  1791.                 // take just the wished results
  1792.                 if($this->PageCount > 1)
  1793.                 {
  1794.                     $this->user_resulset = array_slice($this->user_resulset$this->First-1$this->NbRecordPerPage);
  1795.                 }
  1796.  
  1797.                 foreach($this->user_resulset as $row)
  1798.                 {
  1799.                     $this->trtShowRecords($row$func_data);
  1800.                 }
  1801.             }
  1802.         }
  1803.  
  1804.         $this->loop('data');
  1805.  
  1806.         $this->cons_query = array();
  1807.         return $this->Count;
  1808.     }
  1809.  
  1810.     var $T_i = 0;
  1811.     var $T_id = 0;
  1812.     var $last_header;
  1813.     var $breaker_name;
  1814.     var $header_count;
  1815.     /**
  1816.      *
  1817.      * @param  array $row 
  1818.      * @param  string $func_data 
  1819.      * @author H2LSOFT */
  1820.     protected function trtShowRecords($row$func_data)
  1821.     {
  1822.         // if(!empty($func_data))$row = $func_data($row);
  1823.         if(!empty($func_data))
  1824.         {
  1825.             // method ?
  1826.             if(strpos($func_data'::'!== false || strpos($func_data'->'!== false// methode ?
  1827.                 eval("\$row = $func_data(\$row);");
  1828.             else
  1829.                 $row $func_data($row);
  1830.         }
  1831.  
  1832.         // breaker
  1833.         if(!empty($this->breaker_name&& $this->last_header != $row['breaker'])
  1834.         {
  1835.             $this->last_header = $row['breaker'];
  1836.             $this->header_count = 0;
  1837.         }
  1838.  
  1839.         $keys @array_keys($row)// take the names of the keys
  1840.         foreach($keys as $key)
  1841.         {
  1842.             // add the nav color
  1843.             if($this->itemExists('_NavColor''loop'))
  1844.             {
  1845.                 $color ($this->T_i % 2$this->NavColorFirst : $this->NavColorSecond;
  1846.                 $this->parse('data.loop._NavColor'$color);
  1847.             }
  1848.  
  1849.             // add the Id
  1850.             if($this->itemExists('_Id''loop'))
  1851.                 $this->parse('data.loop._Id'$this->T_id);
  1852.  
  1853.             if(!is_int($key&& $this->itemExists($key'loop'))
  1854.                 $this->parse("data.loop.$key"$row[$key]);
  1855.  
  1856.             // breaker
  1857.             if(!empty($this->breaker_name))
  1858.             {
  1859.                 if(!is_int($key&& $this->itemExists($key'breaker'))
  1860.                 {
  1861.                     $this->parse("data.loop.breaker.$key"$row[$key]);
  1862.                 }
  1863.             }
  1864.         }
  1865.  
  1866.         $this->T_i++;
  1867.         $this->T_id++;
  1868.  
  1869.         // breaker
  1870.         if(!empty($this->breaker_name))
  1871.         {
  1872.             if($this->header_count == 1)
  1873.             {
  1874.                 $this->eraseBloc('data.loop.breaker');
  1875.                 $this->header_count = 0;
  1876.             }
  1877.  
  1878.             // $this->loop('data.loop.breaker');
  1879.             $this->header_count++;
  1880.         }
  1881.  
  1882.  
  1883.         $this->loop('data.loop');
  1884.     }
  1885.  
  1886.     /**
  1887.      *
  1888.      * @global array $_err 
  1889.      * @param int $err_no 
  1890.      * @param string $msg 
  1891.      * @param int $exit 
  1892.      * @author H2LSOFT */
  1893.     protected function dbError($err_no$msg ''$exit 1)
  1894.     {
  1895.         global $_err;
  1896.  
  1897.         $err_msg $_err['DB']["$err_no"];
  1898.         $err_msg str_replace('[:MSG:]'$msg$err_msg);
  1899.         $this->error_msg "<B>TPLN DB Error $err_no:</B> <table border=1><tr><td>$err_msg</td></tr></table>";
  1900.         $this->error_user_level E_USER_ERROR;
  1901.  
  1902.         $this->outPutMessage($exit);
  1903.     }
  1904.  
  1905.     /**
  1906.      * this method allows to parse all the variables of a block by the database query returned values.
  1907.      *
  1908.      * The parse_function is Parse() method parameter, you could add php methods inside this method.
  1909.      *
  1910.      * @param string $bloc 
  1911.      * @param string $msg 
  1912.      * @param string $func 
  1913.      * @author H2LSOFT */
  1914.     public function parseDbRow($bloc$msg ''$func '')
  1915.     {
  1916.         $this->pathVerify($bloc);
  1917.  
  1918.         if(empty($this->NavColorFirst))$this->NavColorFirst = TPLN_DB_NavColorFirst;
  1919.         if(empty($this->NavColorSecond))$this->NavColorSecond = TPLN_DB_NavColorSecond;
  1920.  
  1921.         $res $this->getRowsCount();
  1922.         if($res == 0)
  1923.         {
  1924.             $this->parseBloc($bloc$msg);
  1925.         }
  1926.         else
  1927.         {
  1928.             $i 0;
  1929.             while($row $this->dbFetchAssoc())
  1930.             {
  1931.                 $keys @array_keys($row)// prise du mon des clefs
  1932.                 $i++;
  1933.  
  1934.                 foreach($keys as $key)
  1935.                 {
  1936.                     if($this->itemExists($key$bloc))
  1937.                     {
  1938.                         //if(!empty($func))
  1939.                         $this->parse("$bloc.$key"$row[$key]$func);
  1940.                         //else
  1941.                         //$this->Parse("$bloc.$key", $row[$key]);
  1942.                     }
  1943.  
  1944.                     if($this->itemExists('_NavColor'$bloc))
  1945.                     {
  1946.                         $color ($i 2$this->NavColorFirst : $this->NavColorSecond;
  1947.                         $this->parse($bloc.'._NavColor'$color);
  1948.                     }
  1949.  
  1950.                 }
  1951.                 $this->loop($bloc);
  1952.             }
  1953.         }
  1954.     }
  1955.  
  1956.     /**
  1957.      * this method allows to parse all the variables of a block by the database query field returned values.
  1958.      *
  1959.      * @param  string $bloc 
  1960.      * @param  string $func 
  1961.      *
  1962.      * @since 2.3, 2.9
  1963.      * @see ParseDBField().
  1964.      * @author H2LSOFT */
  1965.     public function parseDbField($bloc$func '')
  1966.     {
  1967.         $fields $this->getFields();
  1968.  
  1969.         // check the path
  1970.         $this->pathVerify($bloc);
  1971.  
  1972.         // simulate that we take an item
  1973.         // $last_bloc = $this->_GetItem($bloc);
  1974.  
  1975.         if(empty($this->NavColorFirst))$this->NavColorFirst = TPLN_DB_NavColorFirst;
  1976.         if(empty($this->NavColorSecond))$this->NavColorSecond = TPLN_DB_NavColorSecond;
  1977.  
  1978.         // control the path !
  1979.         for($i 0$i count($fields)$i++)
  1980.         {
  1981.             $field_name $fields[$i];
  1982.  
  1983.             // if($this->ItemExists($field_name, $last_bloc))
  1984.             if($this->itemExists('_Field'$bloc))
  1985.             {
  1986.                 //if(!empty($func))
  1987.                 $this->parse("$bloc._Field"$field_name$func);
  1988.                 //else
  1989.                 //$this->Parse("$bloc._Field", $field_name);
  1990.  
  1991.                 if($this->itemExists('_NavColor'$bloc))
  1992.                 {
  1993.                     $color ($i 2$this->NavColorFirst : $this->NavColorSecond;
  1994.                     $this->parse($bloc.'._NavColor'$color);
  1995.                 }
  1996.  
  1997.             }
  1998.             $this->loop($bloc);
  1999.         }
  2000.     }
  2001.  
  2002.     /**
  2003.      * This method allows to convert a date in mysql format:
  2004.      *
  2005.      * DD-MM-YYYY becomes YYYY-MM-DD DD-MM-YYYY HH:MM becomes YYYY-MM-DD HH:MM:SS
  2006.      *
  2007.      * @param string $date 
  2008.      *
  2009.      * @return string 
  2010.      * @see db2Date
  2011.      * @author H2LSOFT */
  2012.     public function date2Db($date)
  2013.     {
  2014.         // date
  2015.         if(strlen($date== 10)
  2016.         {
  2017.             list($d$m$Yexplode('/'$date);
  2018.             $new_date "$Y-$m-$d";
  2019.         }
  2020.         // datetime
  2021.         else
  2022.         {
  2023.             list($d$m$Yexplode('/'$date);
  2024.             $tmp explode(' '$Y);
  2025.             $Y $tmp[0];
  2026.             list($H$iexplode(':'$tmp[1]);
  2027.  
  2028.             $new_date "$Y-$m-$d $H:$i:00";
  2029.         }
  2030.  
  2031.         return $new_date;
  2032.     }
  2033.  
  2034.     /**
  2035.      * This method allows you to convert a date in mysql format to:
  2036.      *
  2037.      * YYYY-MM-DD becomes DD-MM-YYYY YYYY-MM-DD HH:MM:SS becomes DD-MM-YYYY HH:MM
  2038.      * @param string $date 
  2039.      *
  2040.      * @return string 
  2041.      *
  2042.      * @see date2Db
  2043.      * @author H2LSOFT */
  2044.     public function db2Date($date)
  2045.     {
  2046.         if(empty($date|| $date == '0000-00-00' || $date == '0000-00-00 00:00:00')return '';
  2047.  
  2048.         // date
  2049.         if(strlen($date== 10)
  2050.         {
  2051.             list($Y$m$dexplode('-'$date);
  2052.             $new_date "$d/$m/$Y";
  2053.         }
  2054.         else
  2055.         {
  2056.             list($Y$m$dexplode('-'$date);
  2057.             $tmp explode(' '$d);
  2058.             $d $tmp[0];
  2059.             list($H$iexplode(':'$tmp[1]);
  2060.  
  2061.             $new_date "$d/$m/$Y $H:$i";
  2062.         }
  2063.  
  2064.         return $new_date;
  2065.     }
  2066.  
  2067.     /**
  2068.      * Enable protection mode for input data recommended true
  2069.      *
  2070.      * @param boolean $bool 
  2071.      * @since 3.2
  2072.      */
  2073.     public function dbSetProtection($bool)
  2074.     {
  2075.         $this->dbProtectionMode = $bool;
  2076.     }
  2077.  
  2078.     /**
  2079.      * Protect data agains xss attack
  2080.      *
  2081.      * @param array $array 
  2082.      * @return array 
  2083.      * @since 3.2
  2084.      */
  2085.     protected function dbProtection($array)
  2086.     {
  2087.         if($this->dbProtectionMode)
  2088.         {
  2089.             $tmp array();
  2090.             foreach($array as $key => $val)
  2091.             {
  2092.                 $val str_replace('{''&#123; '$val);
  2093.                 $val str_replace('}'' &#125;'$val);
  2094.                 $tmp[$key$val;
  2095.             }
  2096.             $array $tmp;
  2097.         }
  2098.  
  2099.         return $array;
  2100.     }
  2101.  
  2102.  
  2103. }
  2104.  
  2105. ?>

Documentation generated on Sat, 06 Mar 2010 21:33:47 +0100 by phpDocumentor 1.4.3