PageRenderTime 70ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/_plugins_/siou/odb_commun/inc-html.php

https://bitbucket.org/pombredanne/spip-zone-treemap
PHP | 393 lines | 224 code | 30 blank | 139 comment | 33 complexity | f001364974436201ac17ba24428be36a MD5 | raw file
  1. <?php
  2. /**
  3. * Debut de balise SELECT dans un TR
  4. *
  5. * @param string $label : Titre du label pour le champ select
  6. * @param string $name : nom de la variable PHP/HTML
  7. * @param string $html : code HTML facultatif
  8. * @return string
  9. */
  10. function formSelectTR1($label,$name,$html='') {
  11. $tr = "\t<tr>\n\t\t<td align='left'><label for='$name'>$label</label></td>\n\t\t<td align='left'><select name='$name' id='$name' $html>\n";
  12. return $tr;
  13. }
  14. /**
  15. * Fin de balise SELECT dans un TR
  16. *
  17. * @return string
  18. */
  19. function formSelectTR2() {
  20. return "</select></td>\n\t</tr>\n";
  21. }
  22. /**
  23. * Cree un champ OPTION dans un SELECT
  24. *
  25. * @param string $label : le titre du OPTION
  26. * @param string $value : la valeur transmise dans la variable (cf name du SELECT)
  27. * @param string $selected : contient la valeur a selectionner par defaut
  28. * @return string
  29. */
  30. function formOptionsInSelect($label,$value,$selected="") {
  31. if ((string)trim($selected)==(string)trim($value) && trim($selected!="")) {
  32. //echo "<b>$value==$selected</b> $label<br>";
  33. $selected="selected";
  34. } else {
  35. if($label=='Faux') echo "$label - $value!=$selected<br>";
  36. $selected="";
  37. }
  38. return "\t<option value=\"$value\" $selected>$label</option>\n";
  39. }
  40. /** Ajoute du code javascript
  41. *
  42. * @param string $script : <ul>
  43. * <li>Nom d'un mod&egrave;le de script :
  44. * <ul>
  45. * <li>isDate : permet de detecter si une date est correcte</li>
  46. * <li>changerStyle : permet de changer le style d'un champ de formulaire (backgroundColor+disable)</li>
  47. * </ul>
  48. * </li>
  49. * <li>OU directement code javascript a encapsuler</li>
  50. * </ul>
  51. * @return string : code javascript
  52. */
  53. function putJavascript($script) {
  54. switch ($script)
  55. {
  56. case 'changerStyle' :
  57. $script= <<<FINSCRIPT
  58. function changerStyle(champ,couleurFond,style) {
  59. switch (style)
  60. {
  61. case 'readonly':
  62. champ.readonly=true;
  63. break;
  64. case 'disable':
  65. champ.disabled=true;
  66. break;
  67. case 'enable':
  68. champ.disabled=false;
  69. champ.readonly=false;
  70. break;
  71. }
  72. champ.style.backgroundColor='couleurFond';
  73. }
  74. FINSCRIPT;
  75. break;
  76. case 'isDate' :
  77. $annee_min=(int)(date('Y')-100);
  78. $annee_max=(int)(date('Y')-10);
  79. $script= <<<FINSCRIPT
  80. function isDate(d) {
  81. // http://www.javascriptfr.com/code.aspx?ID=15737
  82. if (d == "") // si la variable est vide on retourne faux
  83. return false;
  84. e = new RegExp("^[0-9]{1,2}\/[0-9]{1,2}\/([0-9]{2}|[0-9]{4})$");
  85. if (!e.test(d)) // On teste l'expression régulière pour valider la forme de la date
  86. return false; // Si pas bon, retourne faux
  87. // On sépare la date en 3 variables pour vérification, parseInt() converti du texte en entier
  88. j = parseInt(d.split("/")[0], 10); // jour
  89. m = parseInt(d.split("/")[1], 10); // mois
  90. a = parseInt(d.split("/")[2], 10); // année
  91. // Si l'année n'est composée que de 2 chiffres on complète automatiquement
  92. if (a < 1000) {
  93. if (a < 89) a+=2000; // Si a < 89 alors on ajoute 2000 sinon on ajoute 1900
  94. else a+=1900;
  95. }
  96. // Définition du dernier jour de février
  97. // Année bissextile si annnée divisible par 4 et que ce n'est pas un siècle, ou bien si divisible par 400
  98. if (a%4 == 0 && a%100 !=0 || a%400 == 0) fev = 29;
  99. else fev = 28;
  100. // Nombre de jours pour chaque mois
  101. nbJours = new Array(31,fev,31,30,31,30,31,31,30,31,30,31);
  102. // Enfin, retourne vrai si le jour est bien entre 1 et le bon nombre de jours, idem pour les mois, sinon retourn faux
  103. return ( m >= 1 && m <=12 && j >= 1 && j <= nbJours[m-1] && a >= $annee_min && a <= $annee_max);
  104. }
  105. FINSCRIPT;
  106. break;
  107. }
  108. return "<script type='text/javascript'><!--\n$script\n//-->\n</script>\n";
  109. }
  110. /**
  111. * Cree un champ INPUT TEXT dans une ligne TR
  112. *
  113. * @param string $label : <td>libelle (premiere cellule)</td>
  114. * @param string $name : nom de la variable PHP/HTML
  115. * @param string $value : valeur par defaut
  116. * @param string $html : code html optionnel dans le <INPUT>
  117. * @param string $fin_ligne : <td>optionnels a ajouter</td>
  118. * @param boolean $password : mettre true pour champ password (false par defaut)
  119. * @return string
  120. */
  121. function formInputTextTR($label,$name,$value='',$html='',$fin_ligne='',$password='') {
  122. if($password) $type='password'; else $type='text';
  123. //if(substr_count($html,'class')==0) $html.=" class='forml'";
  124. $tr = "\t<tr>\n\t\t<td align='left'><label for='$name'>$label</label></td>\n\t\t<td align='left'><input type='$type' name='$name' id='$name' value=\"$value\" $html/></td>\n\t\t$fin_ligne\n\t</tr>\n";
  125. return $tr;
  126. }
  127. /**
  128. * Cree un champ TEXTAREA dans un TR
  129. *
  130. * @param string $label : <td>libelle (premiere cellule)</td>
  131. * @param string $name : nom de la variable PHP/HTML
  132. * @param string $value : valeur par defaut
  133. * @param string $html : code html optionnel dans le <INPUT>
  134. * @return string
  135. */
  136. function formTextAreaTR($label,$name,$value='',$html='',$html_td='') {
  137. $tr = "\t<tr>\n\t\t<td align='left'><label for='$name'>$label</label></td>\n\t\t<td align='left' $html_td><textarea name='$name' id='$name' $html>$value</textarea></td>\n\t</tr>\n";
  138. return $tr;
  139. }
  140. /**
  141. * Cree un champ SELECT contenant les annees de 2002 a annee en cours + 1
  142. *
  143. * @param string $annee : annee selectionnee par defaut
  144. * @return string
  145. */
  146. function formSelectAnnee($annee) {
  147. $str="";
  148. for ($i = (int)date("Y")+1; $i >= 2002; $i--) {
  149. $str.= formOptionsInSelect($i,$i,$annee);
  150. }
  151. return $str;
  152. }
  153. /**
  154. * Champ SELECT Vrai/Faux
  155. * boolean
  156. * @param boolean $isVrai : valeur par defaut
  157. * @return string
  158. */
  159. function formSelectVraiFaux($isVrai) {
  160. $str= formOptionsInSelect('Vrai',1,$isVrai);
  161. $str.= formOptionsInSelect('Faux',0,$isVrai);
  162. return $str;
  163. }
  164. /**
  165. * Affiche une vignette (icone de type de fichier)
  166. *
  167. * @param string $ext : extension du fichier
  168. * @param string $title : title (et alt) de l'icone
  169. * @return string
  170. */
  171. function vignette($ext,$title='') {
  172. define('DIR_ODB_COMMUN',_DIR_PLUGINS."odb/odb_commun/");
  173. switch(trim($ext)) {
  174. case "csv":
  175. $ext="ods";
  176. break;
  177. case "":
  178. $ext="defaut";
  179. }
  180. $src=DIR_ODB_COMMUN."img_pack/vignettes/$ext.png";
  181. if(is_file($src))
  182. $str = "<img src='$src' alt='Fichier $ext' title='$title' align='absmiddle'/>";
  183. else
  184. $str = "<b>[$ext]</b>\n";
  185. return $str;
  186. }
  187. /**
  188. * Affiche une table html
  189. *
  190. * @param string $titre : titre du tableau
  191. * @param array $tbody : tableau de lignes (&lt;tr&gt;...&lt;/tr&gt;)) sans les &lt;tr&gt;
  192. * @param strin $thead : tableau de lignes de &lt;th&gt; a mettre au debut ('' si aucun)
  193. * @param string $icone : nom de fichier de l'icone (cf ../dist/images/)
  194. * @return string : table HTML
  195. */
  196. function odb_html_table($titre,$tbody,$thead='',$icone='vignette-24.png') {
  197. $isMSIE=eregi('msie',$_SERVER['HTTP_USER_AGENT'])>0;
  198. $wrapper=$isMSIE ? 'wrapper.php?file=':'';
  199. $ret="<div class='liste'>\n"
  200. . " <div style='position: relative;'>\n"
  201. . " <div style='position: absolute; top: -12px; left: 3px;'><img src='../dist/images/$wrapper$icone' alt='' /></div>\n"
  202. . " <div style='background-color: white; color: black; padding: 3px; padding-left: 30px; border-bottom: 1px solid #444444;' class='verdana2'>\n"
  203. . " <b>$titre</b>\n"
  204. . " </div>\n"
  205. . " </div>\n"
  206. . " <table id='".getRewriteString(supprimeAccents(strip_tags(substr(html_entity_decode(odb_propre($titre)),0,20))))."' width='100%' cellpadding='2' cellspacing='0' border='0' class='spip'>\n"
  207. ;
  208. if($thead!=='') {
  209. $ret.="<thead>\n";
  210. if(is_array($thead))
  211. foreach($thead as $ligne)
  212. $ret.="\t<tr $js>\n\t\t$ligne\n\t</tr>\n";
  213. else $ret.="\t<tr $js>\n\t\t$thead\n\t</tr>\n";
  214. $ret.="</thead>\n";
  215. }
  216. $ret.="<tbody>\n";
  217. //$js=$isMSIE ? "onmouseover=\"changeclass(this,'tr_liste_over');\" onmouseout=\"changeclass(this,'tr_liste');\"" : '';
  218. $js="onmouseover=\"changeclass(this,'tr_liste_over');\" onmouseout=\"changeclass(this,'tr_liste');\"";
  219. if(is_array($tbody))
  220. foreach($tbody as $ligne)
  221. $ret.="\t<tr class='tr_liste' $js>\n\t\t$ligne\n\t</tr>\n";
  222. $ret.="</tbody>\n";
  223. $ret .= " </table>\n</div>\n";
  224. return $ret;
  225. }
  226. /**
  227. * affiche une boite de message
  228. *
  229. * @param string $texte : texte a afficher
  230. * @param string $type : type de boite
  231. * @return string
  232. */
  233. function boite_important($texte,$type='') {
  234. $style="border: 1px solid red;background-color:#eee;padding:5px;";
  235. switch($type) {
  236. //TODO implementer differents types de boite
  237. case 'attention':
  238. case 'warning':
  239. default:
  240. $panno="<b style='color:red;text-decoration:underline;'>/!\</b>";
  241. break;
  242. }
  243. return "<p style='$style'>$panno $texte</p>\n";
  244. }
  245. /**
  246. * Supprime les accents et enleve les espaces inutiles
  247. *
  248. * @param string str : chaine a traiter
  249. * @param string $stripette : true (par defaut) s'il faut remplacer "-" et "/" par " - " et " / "
  250. * @return string
  251. */
  252. function supprimeAccents($str,$stripette=true) {
  253. if($stripette) {
  254. $str=str_replace("-"," - ",$str);
  255. $str=str_replace("/"," / ",$str);
  256. }
  257. //$str=ereg_replace('^[:blank:]+$',' ',$str);
  258. while(substr_count($str,' ')>0)
  259. $str=str_replace(' ',' ',$str);
  260. $str=utf8_decode($str);
  261. $str = strtr($str,"\xC0\xC1\xC2\xC3\xC4\xC5\xC6","AAAAAAA");
  262. $str = strtr($str,"\xC7","C");
  263. $str = strtr($str,"\xC8\xC9\xCA\xCB","EEEE");
  264. $str = strtr($str,"\xCC\xCD\xCE\xCF","IIII");
  265. $str = strtr($str,"\xD1","N");
  266. $str = strtr($str,"\xD2\xD3\xD4\xD5\xD6\xD8","OOOOOO");
  267. $str = strtr($str,"\xDD","Y");
  268. $str = strtr($str,"\xDF","S");
  269. $str = strtr($str,"\xE0\xE1\xE2\xE3\xE4\xE5\xE6","aaaaaaa");
  270. $str = strtr($str,"\xE7","c");
  271. $str = strtr($str,"\xE8\xE9\xEA\xEB","eeee");
  272. $str = strtr($str,"\xEC\xED\xEE\xEF","iiii");
  273. $str = strtr($str,"\xF1","n");
  274. $str = strtr($str,"\xF2\xF3\xF4\xF5\xF6\xF8","oooooo");
  275. $str = strtr($str,"\xF9\xFA\xFB\xFC","uuuu");
  276. $str = strtr($str,"\xFD\xFF","yy");
  277. return trim($str);
  278. }
  279. /**
  280. * rend une chaine compatible url-rewriting
  281. *
  282. * @see http://www.php.net/manual/en/function.strtr.php#51862
  283. * @param string $sString : chaine a traiter
  284. * @return string
  285. */
  286. function getRewriteString($sString) {
  287. $string = htmlentities(strtolower($sString));
  288. $string = preg_replace("/&(.)(acute|cedil|circ|ring|tilde|uml);/", "$1", $string);
  289. $string = preg_replace("/([^a-z0-9]+)/", "-", html_entity_decode($string));
  290. $string = trim($string, "-");
  291. //echo "$sString&rarr;$string<br>";
  292. return $string;
  293. }
  294. /**
  295. * affiche un taux sous forme d'image
  296. *
  297. * @param string $taux : taux<1 a representer
  298. * @param string $largeur : multiplicateur de largeur
  299. * @return string
  300. */
  301. function afficheTaux($taux,$largeur=1) {
  302. $taux=round(100*$taux,1);
  303. return "<IMG SRC='"._DIR_PLUGIN_ODB_REPARTITION."img_pack/carre.gif' title='$taux %' alt='$taux %' height='5' width='".round($largeur*$taux,0)."'>";
  304. }
  305. /**
  306. * affiche un texte la verticale (image, si gd install)
  307. *
  308. * @param string $texte : chaine a traiter
  309. * @param int $taille : taille du texte (1..5)
  310. * @param int $largeur : largeur de l'image
  311. * @param int $hauteur : hauteur de l'image
  312. * @param string $type : img pour creer le code d'une image ou src pour une image background
  313. */
  314. function texte90($texte,$taille,$largeur,$hauteur,$type='img') {
  315. $texte=str_replace("'","&quot;",$texte);
  316. $src="../plugins/odb/odb_commun/inc-image.php?text=$texte&x=$largeur&y=$hauteur&taille=$taille";
  317. if($type=='img')
  318. return "<img alt='$texte' src='$src'/>";
  319. else return $src;
  320. }
  321. /**
  322. * table des matieres
  323. * Le tableau des index determine le texte a afficher et le nom des ancres html (utilise la fonction getRewriteString)
  324. *
  325. * @param array $tIndex : tableau des index
  326. * @param int $tailleLettrines : taille lettrines en pixels (0 pour pas de lettrine, alors 1e lettre en gras)
  327. * @param string $html : attributs html de la table
  328. * @return string : tableau html [lettrine][entree=>titre]
  329. */
  330. function odb_table_matieres($tIndex,$tailleLettrines=24,$html='',$isTrier=true) {
  331. if($isTrier) asort($tIndex);
  332. $sIndex='';
  333. if($tailleLettrines>0) {
  334. foreach($tIndex as $entree) {
  335. $lettre=$entree[0];
  336. $lettrine[$lettre][]=substr($entree,1);
  337. }
  338. $sIndex="<table $html>\n";
  339. foreach($lettrine as $lettre=>$t1) {
  340. $sIndex.="<tr><td valign='top' style='font-size:".$tailleLettrines."px;font-weight:bolder;'>$lettre</td><td valign='middle'><small></small>";
  341. foreach($t1 as $entree) $sIndex.="<A HREF='#".getRewriteString($lettre.$entree)."'>$entree</a><br/>\n";
  342. $sIndex.="</small></td></tr>\n";
  343. }
  344. $sIndex.="</table>\n";
  345. } else {
  346. foreach($tIndex as $entree) $sIndex.="<A HREF='#".getRewriteString($entree)."'><b>".$entree[0]."</b>".substr($entree,1)."</a><br/>\n";
  347. }
  348. return $sIndex;
  349. }
  350. /**
  351. * Nettoie une chaine de caracteres
  352. *
  353. * @param string $str
  354. * @return string
  355. */
  356. function odb_propre($str) {
  357. //$str=preg_replace('/\s\s+/',' ',$str);
  358. $str=str_replace(array("\r","\n","\t",' '),' ',trim($str));
  359. $str=str_replace(' - ','-',$str);
  360. return trim($str);
  361. }
  362. ?>