11<?php
22
33date_default_timezone_set ('Europe/Paris ' );
4- // error_reporting(0);
4+ error_reporting (0 );
55
66global $ _CONFIG ;
77$ _CONFIG ['data ' ] = 'data ' ;
@@ -110,6 +110,7 @@ class Movies implements Iterator, Countable, ArrayAccess {
110110 public $ total_not_seen = 0 ;
111111 public $ total_seen = 0 ;
112112 public $ total_search = 0 ;
113+ public $ total_genre = 0 ;
113114
114115 function __construct ($ logged = FALSE ) {
115116 $ this ->logged = $ logged ;
@@ -207,12 +208,24 @@ public function byNote($begin = 0, $end = PAGINATION) {
207208 return array_slice ($ sorted , $ begin , $ end , TRUE );
208209 }
209210
211+ // function to get only movies with a genre given
212+ public function byGenre ($ genre , $ begin = 0 , $ end = PAGINATION ) {
213+ $ result = array ();
214+ $ genre = mb_convert_case (htmlspecialchars ($ genre ), MB_CASE_LOWER , "UTF-8 " );
215+ foreach ($ this ->data as $ m ) {
216+ if (strpos (mb_convert_case ($ m ['genre ' ], MB_CASE_LOWER , "UTF-8 " ), $ genre )!==false )
217+ { $ result [$ m ['id ' ]] = $ m ; }
218+ }
219+ krsort ($ result );
220+ $ this ->total_genre = sizeof ($ result );
221+ return array_slice ($ result , $ begin , $ end , TRUE );
222+ }
223+
210224 // search function (case no sensitive). Currently: search exact whole expression
211- public function search ($ term , $ begin = 0 ) {
212- $ result= array ();
225+ public function search ($ term , $ begin = 0 , $ end = PAGINATION ) {
226+ $ result = array ();
213227 $ s = mb_convert_case (htmlspecialchars ($ term ), MB_CASE_LOWER , "UTF-8 " );
214- foreach ($ this ->data as $ m )
215- {
228+ foreach ($ this ->data as $ m ) {
216229 $ found = (strpos (mb_convert_case ($ m ['title ' ], MB_CASE_LOWER , "UTF-8 " ),$ s )!==false )
217230 || (strpos (mb_convert_case ($ m ['original_title ' ], MB_CASE_LOWER , "UTF-8 " ),$ s )!==false )
218231 || (strpos (mb_convert_case ($ m ['synopsis ' ], MB_CASE_LOWER , "UTF-8 " ),$ s )!==false )
@@ -221,7 +234,7 @@ public function search($term, $begin = 0) {
221234 }
222235 krsort ($ result );
223236 $ this ->total_search = sizeof ($ result );
224- return array_slice ($ result , $ begin , PAGINATION , TRUE );
237+ return array_slice ($ result , $ begin , $ end , TRUE );
225238 }
226239
227240 /*
@@ -720,7 +733,7 @@ function displayGenres($genres) {
720733 ksort ($ genre );
721734 $ result = '' ;
722735 foreach ($ genre as $ value )
723- $ result .= '<li><i class="icon-tag"></i> ' .trim (mb_convert_case ($ value , MB_CASE_TITLE , "UTF-8 " )).'</li> ' ;
736+ $ result .= '<li><i class="icon-tag"></i> <a href="?genre= ' .trim (mb_convert_case ($ value , MB_CASE_LOWER , " UTF-8 " )). ' "> ' . trim ( mb_convert_case ( $ value , MB_CASE_TITLE , "UTF-8 " )).'</a> </li> ' ;
724737 return $ result .PHP_EOL ;
725738}
726739// shortcut the synopsis (= summary) of the movie with [...]
@@ -1355,7 +1368,7 @@ function signin() {
13551368 $ tpl ->assign ('movie ' , $ movies ->search (htmlspecialchars ($ _GET ['search ' ]), $ page *PAGINATION ));
13561369 } else { $ tpl ->assign ('movie ' , $ movies ->search (htmlspecialchars ($ _GET ['search ' ]))); }
13571370 $ tpl ->assign ('pagination ' , displayPagination ($ page , $ movies ->total_search , '?search= ' .htmlspecialchars ($ _GET ['search ' ]).'& ' ));
1358- $ tpl ->assign ('page_title ' , !empty ($ page ) ? 'Box office · Page ' .($ page +1 ) : 'Box office ' );
1371+ $ tpl ->assign ('page_title ' , !empty ($ page ) ? 'Search · Page ' .($ page +1 ) : 'Search ' );
13591372 $ tpl ->assign ('menu_links ' , Path::menu ('search ' ));
13601373 $ tpl ->assign ('menu_links_admin ' , Path::menuAdmin ('search ' ));
13611374 $ tpl ->assign ('search ' , htmlspecialchars ($ _GET ['search ' ]));
@@ -1364,6 +1377,28 @@ function signin() {
13641377 $ tpl ->draw ('list ' );
13651378 exit ();
13661379}
1380+ // movies sorted with a given genre asked
1381+ if (isset ($ _GET ['genre ' ])) {
1382+ if (empty ($ _GET ['genre ' ])) { notFound (); }
1383+ $ movies = new Movies ();
1384+ $ movies ->byGenre (htmlspecialchars ($ _GET ['genre ' ])); // used to update $movies->total_genre
1385+ // if no result found, genre does not exist
1386+ if ($ movies ->total_genre == 0 ) { notFound (); }
1387+
1388+ $ page = isset ($ _GET ['page ' ]) ? (int ) $ _GET ['page ' ] : 0 ;
1389+ // check if pagination is asked
1390+ if (!empty ($ _GET ['page ' ])) {
1391+ checkPagination ($ page , $ movies ->total_genre );
1392+ $ tpl ->assign ('movie ' , $ movies ->byGenre (htmlspecialchars ($ _GET ['genre ' ]), $ page *PAGINATION ));
1393+ } else { $ tpl ->assign ('movie ' , $ movies ->byGenre (htmlspecialchars ($ _GET ['genre ' ]))); }
1394+ $ tpl ->assign ('pagination ' , displayPagination ($ page , $ movies ->total_genre , '?genre= ' .htmlspecialchars ($ _GET ['genre ' ]).'& ' ));
1395+ $ tpl ->assign ('page_title ' , !empty ($ page ) ? ucfirst (htmlspecialchars ($ _GET ['genre ' ])).' · Page ' .($ page +1 ) : ucfirst (htmlspecialchars ($ _GET ['genre ' ])));
1396+ $ tpl ->assign ('menu_links ' , Path::menu ('genre ' ));
1397+ $ tpl ->assign ('menu_links_admin ' , Path::menuAdmin ('genre ' ));
1398+ $ tpl ->assign ('token ' , getToken ());
1399+ $ tpl ->draw ('list ' );
1400+ exit ();
1401+ }
13671402// all movies asked [need to be after other page they need pagination!]
13681403// HOME PAGE
13691404if (empty ($ _GET ) || isset ($ _GET ['page ' ])) {
@@ -1376,7 +1411,7 @@ function signin() {
13761411 $ tpl ->assign ('movie ' , $ movies ->lastMovies ($ page *PAGINATION ));
13771412 } else { $ tpl ->assign ('movie ' , $ movies ->lastMovies ()); }
13781413 $ tpl ->assign ('pagination ' , displayPagination ($ page , $ movies ->count ()));
1379- $ tpl ->assign ('page_title ' , !empty ($ page ) ? 'Home · Page ' .($ page +1 ) : 'Home ' );
1414+ $ tpl ->assign ('page_title ' , !empty ($ page ) ? 'Page ' .($ page +1 ) : '' );
13801415 $ tpl ->assign ('menu_links ' , Path::menu ('home ' ));
13811416 $ tpl ->assign ('menu_links_admin ' , Path::menuAdmin ('home ' ));
13821417 $ tpl ->assign ('token ' , getToken ());
0 commit comments