ac_common.php

Go to the documentation of this file.
00001 <?php
00002 
00003 /*
00004  *   This file is part of PhpCompta.
00005  *
00006  *   PhpCompta is free software; you can redistribute it and/or modify
00007  *   it under the terms of the GNU General Public License as published by
00008  *   the Free Software Foundation; either version 2 of the License, or
00009  *   (at your option) any later version.
00010  *
00011  *   PhpCompta is distributed in the hope that it will be useful,
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *   GNU General Public License for more details.
00015  *
00016  *   You should have received a copy of the GNU General Public License
00017  *   along with PhpCompta; if not, write to the Free Software
00018  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00021 // Copyright Author Dany De Bontridder ddebontridder@yahoo.fr
00022 
00023 /**
00024  * @file
00025  * @brief common utilities for a lot of procedure, classe
00026  */
00027 
00028 include_once("constant.php");
00029 require_once('class_database.php');
00030 require_once('class_periode.php');
00031 
00032 /* !\brief to protect again bad characters which can lead to a cross scripting attack
00033   the string to be diplayed must be protected
00034  */
00035 
00036 function h($p_string)
00037 {
00038     return htmlspecialchars($p_string);
00039 }
00040 
00041 function span($p_string, $p_extra='')
00042 {
00043     return '<span ' . $p_extra . '>' . $p_string . '</span>';
00044 }
00045 
00046 function hi($p_string)
00047 {
00048     return '<i>' . htmlspecialchars($p_string) . '</i>';
00049 }
00050 
00051 function hb($p_string)
00052 {
00053     return '<b>' . htmlspecialchars($p_string) . '</b>';
00054 }
00055 
00056 function th($p_string, $p_extra='')
00057 {
00058     return '<th  ' . $p_extra . '>' . htmlspecialchars($p_string) . '</th>';
00059 }
00060 
00061 function h2info($p_string)
00062 {
00063     return '<h2 class="info">' . htmlspecialchars($p_string) . '</h2>';
00064 }
00065 
00066 function h2($p_string, $p_class)
00067 {
00068     return '<h2 ' . $p_class . '>' . htmlspecialchars($p_string) . '</h2>';
00069 }
00070 function h1($p_string, $p_class)
00071 {
00072     return '<h1 ' . $p_class . '>' . htmlspecialchars($p_string) . '</h1>';
00073 }
00074 /* !\brief surround the string with td
00075  * \param $p_string string to surround by TD
00076  * \param $p_extra extra info (class, style, javascript...)
00077  * \return string surrounded by td
00078  */
00079 
00080 function td($p_string='', $p_extra='')
00081 {
00082     return '<td  ' . $p_extra . '>' . $p_string . '</td>';
00083 }
00084 
00085 function tr($p_string, $p_extra='')
00086 {
00087     return '<tr  ' . $p_extra . '>' . $p_string . '</tr>';
00088 }
00089 
00090 /* !\brief escape correctly php string to javascript */
00091 
00092 function j($p_string)
00093 {
00094     $a = preg_replace("/\r?\n/", "\\n", addslashes($p_string));
00095     $a = str_replace("'", '\'', $a);
00096     return $a;
00097 }
00098 
00099 /**
00100  * format the number for the CSV export
00101  * @param $p_number number
00102  */
00103 function nb($p_number)
00104 {
00105     $r = sprintf('%.2f', $p_number);
00106     $r = str_replace('.', ',', $r);
00107 
00108     return $r;
00109 }
00110 
00111 /**
00112  * format the number with a sep. for the thousand
00113  * @param $p_number number
00114  */
00115 function nbm($p_number)
00116 {
00117 
00118     if (trim($p_number) == '')
00119         return '';
00120     if ($p_number == 0)
00121         return 0;
00122     $a = doubleval($p_number);
00123 
00124     $r = number_format($a, 2, ",", ".");
00125     if (trim($r) == '')
00126     {
00127         var_dump($r);
00128         var_dump($p_number);
00129         var_dump($a);
00130         exit();
00131     }
00132 
00133     return $r;
00134 }
00135 
00136 /* !
00137  * \brief  log error into the /tmp/phpcompta_error.log it doesn't work on windows
00138  *
00139  * \param p_log message
00140  * \param p_line line number
00141  * \param p_message is the message
00142  *
00143  * \return nothing
00144  *
00145  */
00146 
00147 function echo_error($p_log, $p_line="", $p_message="")
00148 {
00149     echo "ERREUR :" . $p_log . " " . $p_line . " " . $p_message;
00150     $fdebug = fopen($_ENV['TMP'] . DIRECTORY_SEPARATOR . "phpcompta_error.log", "a+");
00151     if ($fdebug != null)
00152     {
00153         fwrite($fdebug, date("Ymd H:i:s") . $p_log . " " . $p_line . " " . $p_message . "\n");
00154         fclose($fdebug);
00155     }
00156 }
00157 
00158 /* !
00159  * \brief  Compare 2 dates
00160  * \param p_date
00161  * \param p_date_oth
00162  *
00163  * \return
00164  *      - == 0 les dates sont identiques
00165  *      - > 0 date1 > date2
00166  *      - < 0 date1 < date2
00167  */
00168 
00169 function cmpDate($p_date, $p_date_oth)
00170 {
00171     date_default_timezone_set('Europe/Brussels');
00172 
00173     $l_date = isDate($p_date);
00174     $l2_date = isDate($p_date_oth);
00175     if ($l_date == null || $l2_date == null)
00176     {
00177         throw new Exception("erreur date [$p_date] [$p_date_oth]");
00178     }
00179     $l_adate = explode(".", $l_date);
00180     $l2_adate = explode(".", $l2_date);
00181     $l_mkdate = mktime(0, 0, 0, $l_adate[1], $l_adate[0], $l_adate[2]);
00182     $l2_mkdate = mktime(0, 0, 0, $l2_adate[1], $l2_adate[0], $l2_adate[2]);
00183     // si $p_date > $p_date_oth return > 0
00184     return $l_mkdate - $l2_mkdate;
00185 }
00186 
00187 /***!
00188  * @brief check if the argument is a number
00189  *
00190  * \param $p_int number to test
00191  *
00192  * \return
00193  *        - 1 it's a number
00194  *        - 0 it is not
00195  */
00196 function isNumber(&$p_int)
00197 {
00198     if (strlen(trim($p_int)) == 0)
00199         return 0;
00200     if (is_numeric($p_int) === true)
00201         return 1;
00202     else
00203         return 0;
00204 }
00205 
00206 /***
00207  * \brief Verifie qu'une date est bien formaté
00208  *           en d.m.y et est valable
00209  * \param $p_date
00210  *
00211  * \return
00212  *      - null si la date est invalide ou malformaté
00213  *      - $p_date si tout est bon
00214  *
00215  */
00216 
00217 function isDate($p_date)
00218 {
00219     if (strlen(trim($p_date)) == 0)
00220         return null;
00221     if (preg_match("/^[0-9]{1,2}\.[0-9]{1,2}\.20[0-9]{2}$/", $p_date) == 0)
00222     {
00223 
00224         return null;
00225     }
00226     else
00227     {
00228         $l_date = explode(".", $p_date);
00229 
00230         if (sizeof($l_date) != 3)
00231             return null;
00232 
00233         if ($l_date[2] > COMPTA_MAX_YEAR || $l_date[2] < COMPTA_MIN_YEAR)
00234         {
00235             return null;
00236         }
00237 
00238         if (checkdate($l_date[1], $l_date[0], $l_date[2]) == false)
00239         {
00240             return null;
00241         }
00242     }
00243     return $p_date;
00244 }
00245 
00246 /* !
00247  * \brief Default page header for each page
00248  *
00249  * \param p_theme default theme
00250  * \param $p_script
00251  * \param $p_script2  another js script
00252  * Must be called only once
00253  * \return none
00254  */
00255 
00256 function html_page_start($p_theme="", $p_script="", $p_script2="")
00257 {
00258     // check not called twiced
00259     static  $already_call=0;
00260     if ( $already_call==1)return;
00261     $already_call=1;
00262 
00263     $cn = new Database();
00264     if ($p_theme != "")
00265     {
00266         $Res = $cn->exec_sql("select the_filestyle from theme
00267                            where the_name='" . $p_theme . "'");
00268         if (Database::num_row($Res) == 0)
00269             $style = "style.css";
00270         else
00271         {
00272             $s = Database::fetch_array($Res, 0);
00273             $style = $s['the_filestyle'];
00274         }
00275     }
00276     else
00277     {
00278         $style = "style.css";
00279     } // end if
00280         $title="PhpCompta";
00281 
00282         if ( isset ($_REQUEST['ac'])) {
00283                 if (strpos($_REQUEST['ac'],'/') <> 0)
00284                 {
00285                         $m=  explode('/',$_REQUEST['ac']);
00286                         $title=$m[count($m)-1]."  ".$title;
00287                 }
00288                 else
00289                         $title=$_REQUEST['ac']."  ".$title;
00290         }
00291     echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 FINAL//EN">';
00292     echo "<HTML>";
00293 
00294     if ($p_script2 != "")
00295         $p_script2 = '<script src="' . $p_script2 . '" type="text/javascript"></script>';
00296 
00297     echo "<HEAD>
00298     <TITLE>$title</TITLE>
00299     <META http-equiv=\"Content-Type\" content=\"text/html; charset=UTF8\">
00300     <LINK REL=\"stylesheet\" type=\"text/css\" href=\"$style\" media=\"screen\">
00301     <link rel=\"stylesheet\" type=\"text/css\" href=\"style-print.css\" media=\"print\">" .
00302     $p_script2 . "
00303     ";
00304     echo '<script language="javascript" src="js/calendar.js"></script>
00305     <script type="text/javascript" src="js/lang/calendar-en.js"></script>
00306     <script language="javascript" src="js/calendar-setup.js"></script>
00307     <LINK REL="stylesheet" type="text/css" href="calendar-blue.css" media="screen">
00308     </HEAD>
00309     ';
00310 
00311     echo "<BODY $p_script>";
00312 // language
00313     if (isset($_SESSION['g_lang']))
00314     {
00315                 set_language();
00316     }
00317 
00318 }
00319 
00320 /* !
00321  * \brief Minimal  page header for each page, used for small popup window
00322  *
00323  * \param p_theme default theme
00324  * \param $p_script
00325  * \param $p_script2  another js script
00326  *
00327  * \return none
00328  */
00329 
00330 function html_min_page_start($p_theme="", $p_script="", $p_script2="")
00331 {
00332 
00333     $cn = new Database();
00334     if ($p_theme != "")
00335     {
00336         $Res = $cn->exec_sql("select the_filestyle from theme
00337                            where the_name='" . $p_theme . "'");
00338         if (Database::num_row($Res) == 0)
00339             $style = "style.css";
00340         else
00341         {
00342             $s = Database::fetch_array($Res, 0);
00343             $style = $s['the_filestyle'];
00344         }
00345     }
00346     else
00347     {
00348         $style = "style.css";
00349     } // end if
00350     echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 FINAL//EN">';
00351     echo "<HTML>";
00352 
00353 
00354     if ($p_script2 != "")
00355         $p_script2 = '<script src="' . $p_script2 . '" type="text/javascript"></script>';
00356 
00357     echo "<HEAD>
00358     <TITLE>PhpCompta</TITLE>
00359     <META http-equiv=\"Content-Type\" content=\"text/html; charset=UTF8\">
00360     <LINK REL=\"stylesheet\" type=\"text/css\" href=\"$style\" media=\"screen\">
00361     <link rel=\"stylesheet\" type=\"text/css\" href=\"style-print.css\" media=\"print\">" .
00362     $p_script2 . "
00363     <script src=\"js/scripts.js\" type=\"text/javascript\"></script>";
00364     echo '</HEAD>
00365     ';
00366 
00367     echo "<BODY $p_script>";
00368     /* If we are on the user_login page */
00369     if (basename($_SERVER['PHP_SELF']) == 'user_login.php')
00370     {
00371         return;
00372     }
00373 }
00374 
00375 /* !
00376  * \brief end tag
00377  *
00378  */
00379 
00380 function html_page_stop()
00381 {
00382     echo "</BODY>";
00383     echo "</HTML>";
00384 }
00385 
00386 /* !
00387  * \brief Echo no access and stop
00388  *
00389  * \return nothing
00390  */
00391 
00392 function NoAccess($js=1)
00393 {
00394     if ($js == 1)
00395     {
00396         echo "<script>";
00397         echo "alert ('" . _('Cette action ne vous est pas autorisée Contactez votre responsable') . "');";
00398         echo "</script>";
00399     }
00400     else
00401     {
00402         echo '<div class="redcontent">';
00403         echo '<h2 class="error">' . _(' Cette action ne vous est pas autorisée Contactez votre responsable') . '</h2>';
00404         echo '</div>';
00405     }
00406     exit - 1;
00407 }
00408 /**
00409  * replaced by sql_string
00410  * @deprecated
00411  */
00412 function FormatString($p_string)
00413 {
00414     return sql_string($p_string);
00415 }
00416 /* !
00417  * \brief Fix the problem with the quote char for the database
00418  *
00419  * \param $p_string
00420  * \return a string which won't let strange char for the database
00421  */
00422 
00423 function sql_string($p_string)
00424 {
00425     $p_string = trim($p_string);
00426     if (strlen($p_string) == 0)
00427         return null;
00428     $p_string = str_replace("'", "''", $p_string);
00429     $p_string = str_replace('\\', '\\\\', $p_string);
00430     return $p_string;
00431 }
00432 
00433 /* !
00434   /* \brief store the string which print
00435  *           the content of p_array in a table
00436  *           used to display the menu
00437  * \param  $p_array array like ( 0=>HREF reference, 1=>visible item (name),2=>Help(opt),
00438  * 3=>selected (opt) 4=>javascript (normally a onclick event) (opt)
00439  * \param $p_dir direction of the menu (H Horizontal  V vertical)
00440  * \param $class CSS for TD tag
00441  * \param $class_ref CSS for the A tag
00442  * \param $default selected item
00443  * \param $p_extra extra code for the table tag (CSS or javascript)
00444  *
00445   /* \return : string */
00446 
00447 function ShowItem($p_array, $p_dir='V', $class="mtitle", $class_ref="mtitle", $default="", $p_extra="")
00448 {
00449 
00450     $ret = "<TABLE $p_extra>";
00451     // direction Vertical
00452     if ($p_dir == 'V')
00453     {
00454         foreach ($p_array as $all => $href)
00455         {
00456             $javascript = (isset($href[4])) ? $href[4] : "";
00457             $title = "";
00458             $set = "XX";
00459             if (isset($href[2]))
00460                 $title = $href[2];
00461             if (isset($href[3]))
00462                 $set = $href[3];
00463 
00464             if ($set == $default)
00465                 $ret.='<TR><TD CLASS="selectedcell"><A class="' . $class_ref . '" HREF="' . $href[0] . '" title="' . $title . '" ' . $javascript . '>' . $href[1] . '</A></TD></TR>';
00466             else
00467                 $ret.='<TR><TD CLASS="' . $class . '"><A class="' . $class_ref . '" HREF="' . $href[0] . '" title="' . $title . '" ' . $javascript . '>' . $href[1] . '</A></TD></TR>';
00468         }
00469     }
00470     //direction Horizontal
00471     else if ($p_dir == 'H')
00472     {
00473 
00474         $ret.="<TR>";
00475         foreach ($p_array as $all => $href)
00476         {
00477             $title = "";
00478             $javascript = (isset($href[4])) ? $href[4] : "";
00479 
00480             $set = "A";
00481             if (isset($href[2]))
00482                 $title = $href[2];
00483 
00484             if (isset($href[3]))
00485                 $set = $href[3];
00486 
00487             if ($default === $href[0] || $set === $default)
00488             {
00489                 $ret.='<TD CLASS="selectedcell"><A class="' . $class_ref . '" HREF="' . $href[0] . '" title="' . $title . '" ' . $javascript . '>' . $href[1] . '</A></TD>';
00490             }
00491             else
00492             {
00493                 $ret.='<TD CLASS="' . $class . '"><A class="' . $class_ref . '" HREF="' . $href[0] . '" title="' . $title . '" ' . $javascript . '>' . $href[1] . '</A></TD>';
00494             }
00495         }
00496         $ret.="</TR>";
00497     }
00498     $ret.="</TABLE>";
00499     return $ret;
00500 }
00501 
00502 /* !
00503  * \brief warns
00504  *
00505  * \param p_string error message
00506  * gen :
00507  *      - none
00508  * \return:
00509  *      - none
00510  */
00511 
00512 function echo_warning($p_string)
00513 {
00514     echo '<H2 class="error">' . $p_string . "</H2>";
00515 }
00516 
00517 /* !
00518  * \brief Show the periode which found thanks its id
00519  *
00520  *
00521  * \param  $p_cn database connection
00522  * \param p_id
00523  * \param pos Start or end
00524  *
00525  * \return: string
00526  */
00527 
00528 function getPeriodeName($p_cn, $p_id, $pos='p_start')
00529 {
00530     if ($pos != 'p_start' and
00531             $pos != 'p_end')
00532         echo_error('ac_common.php' . "-" . __LINE__ . '  UNDEFINED PERIODE');
00533     $ret = $p_cn->get_value("select to_char($pos,'Mon YYYY') as t from parm_periode where p_id=$p_id");
00534     return $ret;
00535 }
00536 
00537 /* !
00538  * \brief Return the period corresponding to the
00539  *           date
00540  *
00541  * \param p_cn database connection
00542  * \param p_date the month + year 'MM.YYYY'
00543  *
00544  * \return:
00545  *       parm_periode.p_id
00546  */
00547 
00548 function getPeriodeFromMonth($p_cn, $p_date)
00549 {
00550     $R = $p_cn->get_value("select p_id from parm_periode where
00551                         to_char(p_start,'DD.MM.YYYY') = '01.$p_date'");
00552     if ($R == "")
00553         return -1;
00554     return $R;
00555 }
00556 
00557 /* !\brief Decode the html for the widegt richtext and remove newline
00558  * \param $p_html string to decode
00559  * \return the html code without new line
00560  */
00561 
00562 function Decode($p_html)
00563 {
00564     $p_html = str_replace('%0D', '', $p_html);
00565     $p_html = str_replace('%0A', '', $p_html);
00566     $p_html = urldecode($p_html);
00567     return $p_html;
00568 }
00569 
00570 /* !\brief Create the condition to filter on the j_tech_per
00571  *        thanks a from and to date.
00572  * \param $p_cn database conx
00573  * \param $p_from start date (date)
00574  * \param $p_to  end date (date)
00575  * \param $p_form if the p_from and p_to are date or p_id
00576  * \param $p_field column name
00577  * \return a string containg the query
00578  */
00579 
00580 function sql_filter_per($p_cn, $p_from, $p_to, $p_form='p_id', $p_field='jr_tech_per')
00581 {
00582 
00583     if ($p_form != 'p_id' &&
00584             $p_form != 'date')
00585     {
00586         echo_error(__FILE__, __LINE__, 'Mauvais parametres ');
00587         exit(-1);
00588     }
00589     if ($p_form == 'p_id')
00590     {
00591         // retrieve the date
00592         $pPeriode = new Periode($p_cn);
00593         $a_start = $pPeriode->get_date_limit($p_from);
00594         $a_end = $pPeriode->get_date_limit($p_to);
00595         if ($a_start == null || $a_end == null)
00596             throw new Exception(__FILE__ . __LINE__ . 'Attention periode ' .
00597                     ' non trouvee periode p_from=' . $p_from .
00598                     'p_to_periode = ' . $p_to);
00599 
00600 
00601         $p_from = $a_start['p_start'];
00602         $p_to = $a_end['p_end'];
00603     }
00604     if ($p_from == $p_to)
00605         $periode = " $p_field = (select p_id from parm_periode " .
00606                 " where " .
00607                 " p_start = to_date('$p_from','DD.MM.YYYY')) ";
00608     else
00609         $periode = "$p_field in (select p_id from parm_periode " .
00610                 " where p_start >= to_date('$p_from','DD.MM.YYYY') and p_end <= to_date('$p_to','DD.MM.YYYY')) ";
00611     return $periode;
00612 }
00613 
00614 /* !\brief alert in javascript
00615  * \param $p_msg is the message
00616  * \param $buffer if false, echo directly and execute the javascript, if $buffer is true, the alert javascript
00617  * is in the return string
00618  * \return string with alert javascript if $buffer is true
00619  */
00620 
00621 function alert($p_msg, $buffer=false)
00622 {
00623     $r = '<script language="javascript">';
00624     $r.= 'alert(\'' . j($p_msg) . '\')';
00625     $r.= '</script>';
00626 
00627     if ($buffer)
00628         return $r;
00629     echo $r;
00630 }
00631 
00632 /**
00633  * @brief set the lang thanks the _SESSION['g_lang'] var.
00634  */
00635 function set_language()
00636 {
00637     // desactivate local check
00638     if ( defined("LOCALE") && LOCALE==0 ) return;
00639     $dir = "";
00640     // set differently the language depending of the operating system
00641     if (what_os() == 1)
00642     {
00643         $dir = setlocale(LC_MESSAGES, $_SESSION['g_lang']);
00644         if ($dir == "")
00645         {
00646             $g_lang = 'fr_FR.utf8';
00647             $dir = setlocale(LC_MESSAGES, $g_lang);
00648             echo '<span class="notice">' . $_SESSION['g_lang'] . ' domaine non supporté</h2>';
00649         }
00650         bindtextdomain('messages', './lang');
00651         textdomain('messages');
00652         bind_textdomain_codeset('messages', 'UTF8');
00653 
00654         return;
00655     }
00656     // for windows
00657     putenv('LANG=' . $_SESSION['g_lang']);
00658     $dir = setlocale(LC_ALL, $_SESSION['g_lang']);
00659     bindtextdomain('messages', '.\\lang');
00660     textdomain('messages');
00661     bind_textdomain_codeset('messages', 'UTF8');
00662 }
00663 
00664 /**
00665  * @brief try to determine on what os you are running the pĥpcompte
00666  * server
00667  * @return
00668  *  0 it is a windows
00669  *  1 it is a Unix like
00670  */
00671 function what_os()
00672 {
00673     $inc_path = get_include_path();
00674 
00675     if (strpos($inc_path, ";") != 0)
00676     {
00677         $os = 0;   /* $os is 0 for windoz */
00678     }
00679     else
00680     {
00681         $os = 1;   /* $os is 1 for unix */
00682     }
00683     return $os;
00684 }
00685 
00686 /**
00687  * @brief shrink the date, make a date shorter for the printing
00688  * @param $p_date format DD.MM.YYYY
00689  * @return date in the format DDMMYY (size = 13 mm in arial 8)
00690  */
00691 function shrink_date($p_date)
00692 {
00693     $date = str_replace('.', '', $p_date);
00694     $str_date = substr($date, 0, 4) . substr($date, 6, 2);
00695     return $str_date;
00696 }
00697 /**
00698  * @brief shrink the date, make a date shorter for the printing
00699  * @param $p_date format DD.MM.YYYY
00700  * @return date in the format DDMMYY (size = 13 mm in arial 8)
00701  */
00702 function smaller_date($p_date)
00703 {
00704     $str_date = substr($p_date, 0, 6) . substr($p_date, 8, 2);
00705     return $str_date;
00706 }
00707 
00708 /**
00709  * @brief format the date, when taken from the database the format
00710  * is MM-DD-YYYY
00711  * @param $p_date format
00712  * @param
00713  * @return date in the format DD.MM.YYYY
00714  */
00715 function format_date($p_date, $p_from_format = 'YYYY-MM-DD',$p_to_format='DD.MM.YYYY')
00716 {
00717     if ($p_from_format == 'YYYY-MM-DD')
00718     {
00719         $date = explode('-', $p_date);
00720         if (count($date) != 3)
00721             return $p_date;
00722     }
00723     if ($p_from_format == 'DD.MM.YYYY')
00724     {
00725         $temp_date = explode('.', $p_date);
00726         if (count($temp_date) != 3)
00727             return $p_date;
00728         $date[0] = $temp_date[2]; // 0 is year
00729         $date[1] = $temp_date[1]; // 1 for month
00730         $date[2] = $temp_date[0]; // 2 for day
00731     }
00732 
00733     switch ($p_to_format)
00734     {
00735         case 'DD.MM.YYYY':
00736             $str_date = $date[2] . '.' . $date[1] . '.' . $date[0];
00737             break;
00738         case 'YYYY-MM-DD':
00739             $str_date = $date[0] . '-' . $date[1] . '-' . $date[2];
00740             break;
00741        case 'YYYYMMDD':
00742             $str_date = $date[0] . $date[1] . $date[2];
00743             break;
00744 
00745                 }
00746     return $str_date;
00747 }
00748 
00749 
00750 
00751 /**
00752  * Should a dialog box that you are disconnected for ajax
00753  */
00754 function ajax_disconnected($div)
00755 {
00756     /**
00757      * if $_SESSION['g_user'] is not set : echo a warning
00758      */
00759     if (!isset($_SESSION['g_user']))
00760     {
00761         $script = 'var a=$("' . $div . '");a.style.height="70%";a.style.width="70%";';
00762         $script.='a.style.top=posY-20+offsetY;a.style.left=posX+offsetX;';
00763         $script = create_script($script);
00764         $html = $script;
00765         $html.=HtmlInput::anchor_close($div);
00766         $html.='<div>';
00767         $html.=h2('Données non disponibles', 'class="info" style="width:auto"');
00768         $html.=h2('Veuillez vous reconnecter', '');
00769         $html.=alert("Déconnecté", true);
00770         $html = escape_xml($html);
00771 
00772         header('Content-type: text/xml; charset=UTF-8');
00773         echo <<<EOF
00774 <?xml version="1.0" encoding="UTF-8"?>
00775 <data>
00776 <ctl>$div</ctl>
00777 <code>$html</code>
00778 </data>
00779 EOF;
00780         exit();
00781     }
00782 }
00783 
00784 /**
00785  *
00786  * @param int $selected module selected
00787  */
00788 function show_module($selected)
00789 {
00790     global $g_user;
00791     $cn = Dossier::connect();
00792     $amodule = $cn->get_array("select
00793         me_code,me_menu,me_url,me_javascript,p_order,me_type,me_description
00794         from v_all_menu
00795         where
00796         user_name=$1
00797         and p_type_display='M'
00798         order by p_order", array($g_user->login));
00799 
00800     if ($selected != -1)
00801     {
00802         require_once('template/module.php');
00803         $file = $cn->get_array("select me_file,me_parameter,me_javascript,me_type,me_description from v_all_menu
00804             where me_code=$1 and user_name=$2", array($selected,$g_user->login));
00805         if ( count($file ) == 0 )
00806         {
00807                 echo '</div>';
00808                 echo '</div>';
00809                 echo '<div class="content">';
00810                 echo_warning(_("Module inexistant")."[ $selected ] ");
00811                 echo '</div>';
00812                 exit();
00813         }
00814         if ($file[0]['me_file'] != '')
00815         {
00816             if ($file[0]['me_parameter'] != "")
00817             {
00818                 // if there are paramter put them in superglobal
00819                 $array=compute_variable($file[0]['me_parameter']);
00820                 put_global($array);
00821             }
00822 
00823                 // if file is not a plugin, include the file, otherwise
00824                 // include the plugin launcher
00825                 if ($file[0]['me_type'] != 'PL')
00826                         {
00827                                 require_once $file[0]['me_file'];
00828                         }
00829                         else
00830                         {
00831                                 // nothing  : direct call to plugin
00832                         }
00833         }
00834         if ( $file[0]['me_javascript'] != '')
00835         {
00836                 create_script($file[0]['me_javascript']);
00837         }
00838     }
00839 }
00840 /**
00841  * Find the default module or the first one
00842  * @global $g_user $g_user
00843  * @return default module (string)
00844  */
00845 function find_default_module()
00846 {
00847     global $g_user;
00848     $cn = Dossier::connect();
00849 
00850     $default_module = $cn->get_array("select me_code
00851             from profile_menu join profile_user using (p_id)
00852             where
00853             p_type_display='M' and
00854             user_name=$1 and pm_default=1", array($g_user->login));
00855 
00856     if (empty($default_module))
00857     {
00858                 $default_module = $cn->get_array("select me_code
00859                         from profile_menu join profile_user using (p_id)
00860                         where
00861                         user_name=$1 and p_order=(select min(p_order) from profile_menu join profile_user using (p_id)
00862                         where user_name=$2) limit 1", array($g_user->login, $g_user->login));
00863                 /*
00864                  * if nothing found, there is no profile for this user => exit
00865                  */
00866                 if ( empty ($default_module))
00867                 {
00868                         echo_warning(_("Utilisateur n'a pas de profile"));
00869                         exit();
00870                 }
00871                 return $default_module[0]['me_code'];
00872     }
00873 
00874     if (count($default_module) > 1)
00875     {
00876                 echo_error(_("Plusieurs modules sont le module par défaut"), __LINE__, __FILE__);
00877     }
00878     elseif (count($default_module) == 1)
00879     {
00880         return $default_module[0]['me_code'];
00881     }
00882 }
00883 
00884 /**
00885  * show the module
00886  * @global $g_user
00887  * @param $module the $_REQUEST['ac'] exploded into an array
00888  * @param  $idx the index
00889  */
00890 function show_menu($module, $idx)
00891 {
00892     global $g_user;
00893     $cn = Dossier::connect();
00894     $amenu = $cn->get_array("select
00895         me_menu,me_code,me_url,me_javascript,me_type,me_description
00896         from v_all_menu
00897         where
00898         me_code_dep=$1 and user_name=$2 order by p_order", array($module[$idx], $g_user->login));
00899 
00900     if (!empty($amenu) && count($amenu) > 1)
00901     {
00902                 require 'template/menu.php';
00903     }
00904     elseif (count($amenu) == 1)
00905     {
00906                 echo '<div class="topmenu">';
00907                 echo h2info($amenu[0]['me_menu']);
00908                 echo '</div>';
00909                 $module[$idx] = $amenu[0]['me_code'];
00910     }
00911 
00912     if (empty($amenu) || count($amenu) == 1)
00913     {
00914                 $file = $cn->get_array("select me_file,me_parameter,me_javascript,me_type
00915                 from menu_ref
00916                 join profile_menu using (me_code)
00917                 join profile_user using (p_id)
00918                 where
00919                 me_code=$1 and
00920                 user_name=$2 and
00921                 (me_file is not null or trim(me_file) <>'' or
00922                 me_javascript is not null or trim (me_javascript) <> '')", array($module[$idx],$g_user->login));
00923 
00924                 if (count($file)==0)
00925                 {
00926                         echo "Configuration incorrecte pour ce module ".$module[$idx];
00927                         exit;
00928                 }
00929 
00930                 if ($file[0]['me_file'] != "")
00931                 {
00932                         if ($file[0]['me_parameter'] !== "")
00933                         {
00934                                 // if there are paramter put them in superglobal
00935                                 $array=compute_variable($file[0]['me_parameter']);
00936                                 put_global($array);
00937                         }
00938 
00939                         // if file is not a plugin, include the file, otherwise
00940                         // include the plugin launcher
00941                         if ( $file[0]['me_type'] != 'PL')
00942                                 require_once $file[0]['me_file'];
00943                         else
00944                                 require 'extension_get.inc.php';
00945 
00946                         exit();
00947                 }
00948                 if ( $file[0]['me_javascript'] != '')
00949                 {
00950                         echo create_script($file[0]['me_javascript']);
00951                 }
00952     }
00953 }
00954 /**
00955  * Put in superglobal (get,post,request) the value contained in
00956  * the parameter field (me_parameter)
00957  * @param $array [key] [value]
00958  */
00959 function put_global($array)
00960 {
00961     for ($i=0;$i<count($array);$i++)
00962     {
00963         $key=$array[$i]['key'];
00964         $value=$array[$i]['value'];
00965         $_GET[$key]=$value;
00966         $_POST[$key]=$value;
00967         $_REQUEST[$key]=$value;
00968     }
00969 }
00970 /**
00971  * the string has the format a=b&c=d, it is parsed and an array[][key,value]
00972  * is returned
00973  * @param $p_string
00974  * @return $array usable in put_global
00975  */
00976 function compute_variable($p_string)
00977 {
00978     $array=array();
00979     if ($p_string == '') return $array;
00980 
00981     $var=explode("&",$p_string);
00982     if (empty ($var))   return $array;
00983     for ($i=0;$i < count($var);$i++)
00984     {
00985         $var2=explode('=',$var[$i]);
00986         $array[$i]['key']=$var2[0];
00987         $array[$i]['value']=$var2[1];
00988     }
00989     return $array;
00990 }
00991 function ajax_xml_error($p_code,$p_string)
00992 {
00993     $html = escape_xml($p_string);
00994     header('Content-type: text/xml; charset=UTF-8');
00995                 echo <<<EOF
00996 <?xml version="1.0" encoding="UTF-8"?>
00997 <data>
00998 <code>$p_code</code>
00999 <value>$html</value>
01000 </data>
01001 EOF;
01002 }
01003 
01004 ?>