forked from jozemberi/movie-recommendation-system
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
135 lines (97 loc) · 4.25 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
session_start();
// CHOOSE TEST or REAL MODEL to LOAD
$rec_data_file = file_get_contents("./data/user_centric_recommendations_model_test.json");
//$rec_data_file = file_get_contents("./data/user_centric_recommendations_model.json");
require_once './helpers/smarty.php';
require_once './data/movies_array.php';
require_once './helpers/curl_download.php';
require_once './helpers/recommender.php';
if (!isset($_SESSION['visitor_taste'])) {
$_SESSION['visitor_taste'] = array();
}
if (!isset($_SESSION['visitor_lang'])) {
$_SESSION['visitor_lang'] = 'hr';
}
if (isset($_GET['lang'])) {
$lang = $_GET['lang'];
if ( $lang === 'hr' || $lang === 'en') {
$_SESSION['visitor_lang'] = $lang;
}
}
// for localization (web page language)
$page_name = 'index';
$lang = $_SESSION['visitor_lang'];
$lang_file = 'lang/' . $lang . '/' . $page_name . '.conf';
// load info about movies
$string = file_get_contents("./data/movies_info.json");
$movies_data = json_decode($string, true);
$total_records = sizeof($movies_data); // total number of results (movies) = 1682
$per_page = 10; //how many per page
$total_pages = ceil($total_records / $per_page);
$page = 1;
if (isset($_GET['page'])) {
$page = $_GET['page'];
if ((!is_numeric($page)) || $page > $total_pages || $page < 1) {
// invalid page number control
$page = 1;
}
}
// load recommendation data
$recommendation_data = json_decode($rec_data_file, true);
$re = new Recommender();
$recommendation_results = $re->getRecommendations($recommendation_data, $_SESSION['visitor_taste']);
$total_recommendation_records = sizeof($recommendation_results);
$per_recommendation_page = 10;
$total_recommendation_pages = ceil($total_recommendation_records / $per_recommendation_page);
$recommendation_page = 1;
if (isset($_GET['r_page'])) {
$recommendation_page = $_GET['r_page'];
if ((!is_numeric($recommendation_page)) ||
$recommendation_page > $total_recommendation_pages ||
$recommendation_page < 1) {
// invalid recommendation page number control
$recommendation_page = 1;
}
}
$recommendation_offset = ($recommendation_page - 1) * $per_recommendation_page;
$preserve_keys = true; // important because keys are id-s
// get array with recommendations that will be shown
$show_recommendations = array_slice($recommendation_results,
$recommendation_offset, $per_recommendation_page, $preserve_keys);
// configure recommendation pagination
$r_pagination['curPage'] = $recommendation_page;
$r_pagination['url'] = 'index.php?page=' . $page . '&r_page=%x';
$r_pagination['totalPages'] = $total_recommendation_pages;
$offset = ($page - 1) * $per_page;
if ($page == $total_pages) { // last page case
$per_page = $total_records - (($total_pages - 1) * $per_page);
}
$length = $per_page / 2; // how much from offset to splice (two boxes case)
// slice movies_data array in two, one for left box, one for center
$movies_info_left_box = array_slice($movies_data, $offset, $length, $preserve_keys);
$movies_info_center_box = array_slice($movies_data, $offset + $length, $length, $preserve_keys);
// configure movies pagination
$pagination['curPage'] = $page;
$pagination['url'] = 'index.php?page=%x&r_page=' . $recommendation_page;
$pagination['totalPages'] = $total_pages;
$change_lang_url = 'index.php?page=' . $page . '&r_page=' . $recommendation_page;
$smarty = new Smarty();
$smarty->assign('page_title', ''); // default title is loaded from config file for index.php...
// but this still have to be set to some value..
$smarty->assign('lang_file', $lang_file);
$smarty->assign('lang', $lang);
$smarty->assign('change_lang_url', $change_lang_url);
// fill left box with movie info
$smarty->assign('movies_info_left_box', $movies_info_left_box);
// fill center box with movie info
$smarty->assign('movies_info_center_box', $movies_info_center_box);
// for case that there is no movie info data, only movie title
$smarty->assign('movies_basic_data', $movies);
// content pagination
$smarty->assign('pagination', $pagination);
// recommendation pagination
$smarty->assign('r_pagination', $r_pagination);
$smarty->assign('show_recommendations', $show_recommendations);
$smarty->assign('liked_movies', $_SESSION['visitor_taste']);
$smarty->display('index.tpl');