PageRenderTime 49ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/_plugins_/simple_calendrier/trunk/inc/simplecal_classement.php

https://bitbucket.org/pombredanne/spip-zone-treemap
PHP | 407 lines | 278 code | 74 blank | 55 comment | 73 complexity | bf0d20345d398111f10a9fa59c49ec23 MD5 | raw file
  1. <?php
  2. /**
  3. * Plugin Simple Calendrier v2 pour SPIP 3.0
  4. * Licence GNU/GPL
  5. * 2010-2012
  6. *
  7. * cf. paquet.xml pour plus d'infos.
  8. */
  9. if (!defined("_ECRIRE_INC_VERSION")) return;
  10. function simplecal_classement($param_annee, $param_mois, $param_mode, $param_idrub) {
  11. if (!empty($param_idrub)){
  12. $param_idrub = intval($param_idrub);
  13. } else {
  14. $param_idrub = 0;
  15. }
  16. //die("annee='$param_annee' - mois='$param_mois' - mode='$param_mode' - rub='$param_idrub'");
  17. $sous_titre = "";
  18. $nom_mois = array(
  19. 1 => ucfirst(_T('date_mois_1')),
  20. 2 => ucfirst(_T('date_mois_2')),
  21. 3 => ucfirst(_T('date_mois_3')),
  22. 4 => ucfirst(_T('date_mois_4')),
  23. 5 => ucfirst(_T('date_mois_5')),
  24. 6 => ucfirst(_T('date_mois_6')),
  25. 7 => ucfirst(_T('date_mois_7')),
  26. 8 => ucfirst(_T('date_mois_8')),
  27. 9 => ucfirst(_T('date_mois_9')),
  28. 10 => ucfirst(_T('date_mois_10')),
  29. 11 => ucfirst(_T('date_mois_11')),
  30. 12 => ucfirst(_T('date_mois_12'))
  31. );
  32. // Filtres
  33. $filtre = "";
  34. $liste_a = simplecal_get_liste_annees($param_idrub);
  35. if (count($liste_a)>0){
  36. $filtre .= '<ul id="simplecal-filtres">';
  37. // Restriction a la rubrique ?
  38. if ($param_idrub != 0){
  39. $param_rub = "id_rubrique=$param_idrub";
  40. } else {
  41. $param_rub = "";
  42. }
  43. // Lien Tous
  44. $filtre .= '<li>';
  45. $actif = (!$param_annee && !$param_mois && !$param_mode);
  46. if ($actif){
  47. $filtre .= '<span>'._T('simplecal:tous').'</span>';
  48. } else {
  49. $href_tous = generer_url_ecrire("evenements", $param_rub);
  50. $filtre .= '<a href="'.$href_tous.'">'._T('simplecal:tous').'</a>';
  51. }
  52. $filtre .= '<small> ['.simplecal_get_nb_tous($param_idrub).']</small>';
  53. $filtre .= '</li>';
  54. // Lien A venir
  55. $filtre .= '<li class="marge-bas1">';
  56. $actif = (!$param_annee && !$param_mois && $param_mode);
  57. if ($actif){
  58. $filtre .= '<span>'._T('simplecal:a_venir').'</span>';
  59. $sous_titre = _T('simplecal:a_venir');
  60. } else {
  61. $tmp = "mode=avenir";
  62. if ($param_idrub != 0){
  63. $tmp .= "&".$param_rub;
  64. }
  65. $href_avenir = generer_url_ecrire("evenements", $tmp);
  66. $filtre .= '<a href="'.$href_avenir.'">'._T('simplecal:a_venir').'</a>';
  67. }
  68. $filtre .= '<small> ['.simplecal_get_nb_avenir($param_idrub).']</small>';
  69. $filtre .= '</li>';
  70. // Pour chaque Annee
  71. foreach ($liste_a as $row){
  72. $annee = $row['annee'];
  73. $nb_a = $row['nb'];
  74. $actif = ($param_annee && $param_annee==$annee && !$param_mois);
  75. $filtre .= '<li>';
  76. if ($actif) {
  77. $filtre .= '<span>'.$annee.'</span>';
  78. $sous_titre = $annee;
  79. } else {
  80. $tmp = "annee=".$annee;
  81. if ($param_idrub != 0){
  82. $tmp .= "&".$param_rub;
  83. }
  84. $href_a = generer_url_ecrire("evenements",$tmp);
  85. $filtre .= '<a href="'.$href_a.'">'.$annee.'</a>';
  86. }
  87. $filtre .= '<small> ['.$nb_a.']</small>';
  88. //---
  89. $liste_m = simplecal_get_liste_mois($annee, $param_idrub);
  90. //---
  91. if (count($liste_m)>0){
  92. $filtre .= '<ul>';
  93. // Pour chaque Mois
  94. foreach ($liste_m as $row_m){
  95. $mois = $row_m['mois'];
  96. $nb_m = $row_m['nb'];
  97. $actif = ($param_annee && $param_annee==$annee && $param_mois && $param_mois==$mois);
  98. $filtre .= '<li>';
  99. if ($actif) {
  100. $filtre .= '<span>'.$nom_mois[intval($mois)].'</span>';
  101. $sous_titre = $nom_mois[intval($mois)]." ".$annee;
  102. } else {
  103. $tmp = "annee=".$annee."&mois=".$mois;
  104. if ($param_idrub != 0){
  105. $tmp .= "&".$param_rub;
  106. }
  107. $href_m = generer_url_ecrire("evenements",$tmp);
  108. $filtre .= '<a href="'.$href_m.'"'.$classe.'>'.$nom_mois[intval($mois)].'</a>';
  109. }
  110. $filtre .= '<small> ['.$nb_m.']</small>';
  111. $filtre .= '</li>';
  112. }
  113. $filtre .= "</ul>";
  114. }
  115. //---
  116. $filtre .= '</li>';
  117. }
  118. $filtre .= "</ul>";
  119. }
  120. $entete = "";
  121. if ($filtre){
  122. $entete .= '<strong>'.strtoupper(_T('simplecal:filtres')).' :</strong>';
  123. if ($param_idrub != 0){
  124. $entete .= ' <small>('._T('simplecal:filtres_rubrique_concernee').')</small>';
  125. }
  126. }
  127. $s = $entete.$filtre;
  128. return $s;
  129. }
  130. function simplecal_get_liste_annees($id_rubrique){
  131. /*
  132. select DATE_FORMAT(date_debut,'%Y') as annee
  133. from spip_evenements
  134. where date_debut not like '%0000%'
  135. union
  136. select DATE_FORMAT(date_fin,'%Y') as annee
  137. from spip_evenements
  138. where date_fin not like '%0000%'
  139. */
  140. $from = "spip_evenements";
  141. $order_by = "annee desc";
  142. $select1 = "distinct DATE_FORMAT(date_debut,'%Y') as annee";
  143. $select2 = "distinct DATE_FORMAT(date_fin,'%Y') as annee";
  144. $where1 = "date_debut not like '%0000%'";
  145. $where2 = "date_fin not like '%0000%'";
  146. // Pour le calcul des annees invisibles (ex : du xx/yy/2009 au xx/yy/2011 => 2010 invisible
  147. $select3 = "distinct DATE_FORMAT(date_debut,'%Y') as annee1, DATE_FORMAT(date_fin,'%Y') as annee2";
  148. $where3 = $where1." and ".$where2;
  149. if ($id_rubrique!=0){
  150. $where_rub = " and id_rubrique = ".$id_rubrique;
  151. } else {
  152. $where_rub = "";
  153. }
  154. // ----------------------
  155. // Acces restreint ?
  156. // ----------------------
  157. $where_rub_exclure = simplecal_get_where_rubrique_exclure();
  158. // ----------------------
  159. $liste_a1 = sql_allfetsel($select1, $from, $where1.$where_rub.$where_rub_exclure, "", $order_by, "");
  160. $liste_a2 = sql_allfetsel($select2, $from, $where2.$where_rub.$where_rub_exclure, "", $order_by, "");
  161. $liste_a3 = sql_allfetsel($select3, $from, $where3.$where_rub.$where_rub_exclure, "", "", "");
  162. $annees = array();
  163. foreach ($liste_a1 as $row){
  164. $a = $row['annee'];
  165. if (!in_array($a, $annees)){
  166. $annees[] = $a;
  167. }
  168. }
  169. foreach ($liste_a2 as $row){
  170. $a = $row['annee'];
  171. if (!in_array($a, $annees)){
  172. $annees[] = $a;
  173. }
  174. }
  175. foreach ($liste_a3 as $row){
  176. $a1 = intval($row['annee1']);
  177. $a2 = intval($row['annee2']);
  178. if ($a2 - $a1 > 1){
  179. for ($a = $a1 ; $a<=$a2 ; $a++){
  180. if (!in_array("$a", $annees)){
  181. $annees[] = $a;
  182. }
  183. }
  184. }
  185. }
  186. rsort($annees);
  187. $tab = array();
  188. foreach ($annees as $annee){
  189. $date_min = $annee."-01-01";
  190. $date_max = $annee."-12-31";
  191. $where = "((date_debut like '%".$annee."%'";
  192. $where .= " OR date_fin like '%".$annee."%')";
  193. $where .= " OR (date_debut <= '$date_max' AND date_fin >= '$date_min'))";
  194. $nb = sql_countsel($from, $where.$where_rub.$where_rub_exclure);
  195. $tab[] = array("annee"=>$annee, "nb"=>$nb);
  196. }
  197. return $tab;
  198. }
  199. function simplecal_get_liste_mois($annee, $id_rubrique){
  200. $from = "spip_evenements";
  201. $order_by = "mois desc";
  202. $select1 = "distinct DATE_FORMAT(date_debut,'%m') as mois";
  203. $select2 = "distinct DATE_FORMAT(date_fin,'%m') as mois";
  204. $where1 = "date_debut like '%".$annee."%'";
  205. $where2 = "date_fin like '%".$annee."%'";
  206. // Pour le calcul des mois invisibles - ex : du xx/09/2011 au xx/02/2012
  207. // => 10,11,12 invisibles pour 2011
  208. // => 01 invisible pour 2012
  209. $select3 = "distinct DATE_FORMAT(date_debut,'%Y-%m') as anmois1, DATE_FORMAT(date_fin,'%Y-%m') as anmois2";
  210. $where3 = "DATE_FORMAT(date_debut,'%Y') <= DATE_FORMAT(date_fin,'%Y') ";
  211. if ($id_rubrique!=0){
  212. $where_rub = " and id_rubrique = ".$id_rubrique;
  213. } else {
  214. $where_rub = "";
  215. }
  216. // ----------------------
  217. // Acces restreint ?
  218. // ----------------------
  219. $where_rub_exclure = simplecal_get_where_rubrique_exclure();
  220. // ----------------------
  221. $liste_m1 = sql_allfetsel($select1, $from, $where1.$where_rub.$where_rub_exclure, "", $order_by, "");
  222. $liste_m2 = sql_allfetsel($select2, $from, $where2.$where_rub.$where_rub_exclure, "", $order_by, "");
  223. $liste_m3 = sql_allfetsel($select3, $from, $where3.$where_rub.$where_rub_exclure, "", "", "");
  224. $tab_mois = array();
  225. foreach ($liste_m1 as $row){
  226. $m = $row['mois'];
  227. if (!in_array($m, $tab_mois)){
  228. $tab_mois[] = $m;
  229. }
  230. }
  231. foreach ($liste_m2 as $row){
  232. $m = $row['mois'];
  233. if (!in_array($m, $tab_mois)){
  234. $tab_mois[] = $m;
  235. }
  236. }
  237. foreach ($liste_m3 as $row){
  238. $am1 = $row['anmois1'];
  239. $am2 = $row['anmois2'];
  240. $a1 = intval(substr($am1,0,4));
  241. $a2 = intval(substr($am2,0,4));
  242. $m1 = intval(substr($am1,5,2));
  243. $m2 = intval(substr($am2,5,2));
  244. // ex : du xx/09/2011 au xx/02/2013
  245. // 2011 : 09,10,11,12
  246. // 2012 : 01,02,03,04,05,06,07,08,09,10,11,12
  247. // 2013 : 01,02
  248. for ($a = $a1; $a<=$a2; $a++){
  249. // On ne traite que l'annee concernee
  250. if ($a == $annee){
  251. // Annees identiques => Les mois de m1 a m2
  252. if ($a1 == $a2){
  253. for ($m = $m1 ; $m<=$m2 ; $m++){
  254. $m < 10 ? $s="0$m":$s="$m";
  255. if (!in_array($s, $tab_mois)){
  256. $tab_mois[] = $s;
  257. }
  258. }
  259. }
  260. // Annees differentes => Les mois de a1 (de m1 a decembre)
  261. if ($a1 != $a2 and $a1 == $a){
  262. for ($m = $m1 ; $m<=12 ; $m++){
  263. $m < 10 ? $s="0$m":$s="$m";
  264. if (!in_array($s, $tab_mois)){
  265. $tab_mois[] = $s;
  266. }
  267. }
  268. }
  269. // Annees differentes => Les mois de a2 (de janvier a m2)
  270. if ($a1 != $a2 and $a2 == $a){
  271. for ($m = 1 ; $m<=$m2 ; $m++){
  272. $m < 10 ? $s="0$m":$s="$m";
  273. if (!in_array($s, $tab_mois)){
  274. $tab_mois[] = $s;
  275. }
  276. }
  277. }
  278. // Annees differentes => Les mois de l'annee invisible (de janvier a decembre)
  279. if ($a1 != $a2 and $a1 != $a and $a2 != $a){
  280. for ($m = 1 ; $m<=12 ; $m++){
  281. $m < 10 ? $s="0$m":$s="$m";
  282. if (!in_array($s, $tab_mois)){
  283. $tab_mois[] = $s;
  284. }
  285. }
  286. }
  287. }
  288. }
  289. }
  290. rsort($tab_mois);
  291. $tab = array();
  292. foreach ($tab_mois as $mois){
  293. $date_min = $annee."-".$mois."01";
  294. $date_max = $annee."-".$mois."31";
  295. $where = "((date_debut like '%".$annee."-".$mois."%'";
  296. $where .= " OR date_fin like '%".$annee."-".$mois."%')";
  297. $where .= " OR (date_debut <= '$date_max' AND date_fin >= '$date_min'))";
  298. $nb = sql_countsel($from, $where.$where_rub.$where_rub_exclure);
  299. $tab[] = array("mois"=>$mois, "nb"=>$nb);
  300. }
  301. return $tab;
  302. }
  303. function simplecal_get_nb_tous($id_rubrique){
  304. $from = "spip_evenements as e";
  305. if ($id_rubrique != 0){
  306. $where = "id_rubrique=$id_rubrique";
  307. } else {
  308. $where = "";
  309. }
  310. // ----------------------
  311. // Acces restreint ?
  312. // ----------------------
  313. $where_rub_exclure = simplecal_get_where_rubrique_exclure(!empty($where));
  314. // ----------------------
  315. $nb = sql_countsel($from, $where.$where_rub_exclure);
  316. return $nb;
  317. }
  318. function simplecal_get_nb_avenir($id_rubrique){
  319. $from = "spip_evenements as e";
  320. $where = " (e.date_debut >= DATE_FORMAT(NOW(),'%Y-%m-%d')";
  321. $where .= " OR e.date_fin >= DATE_FORMAT(NOW(),'%Y-%m-%d'))";
  322. if ($id_rubrique != 0){
  323. $where .= " AND id_rubrique=$id_rubrique";
  324. }
  325. // ----------------------
  326. // Acces restreint ?
  327. // ----------------------
  328. $where_rub_exclure = simplecal_get_where_rubrique_exclure();
  329. // ----------------------
  330. $nb = sql_countsel($from, $where.$where_rub_exclure);
  331. return $nb;
  332. }
  333. ?>