-
Notifications
You must be signed in to change notification settings - Fork 6
/
functions.php
222 lines (185 loc) · 4.22 KB
/
functions.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
/**
* Build where clause string
* @param array $fields associative array key value
* @return string whereclause
*/
function buildWhereClause(Array $fields)
{
// no fields? no whereclause
if (count($fields) == 0) { return ''; }
// setup whereclause
$whereclause = ' WHERE ';
$wherefields = array();
// loop trough fields and build whereclause
foreach ($fields AS $key => $value)
{
$wherefields[] = mysql_escape_string($key) . " LIKE '%" . mysql_escape_string($value) . "%' ";
}
// add all fields to string
$whereclause .= implode(' AND ', $wherefields);
return $whereclause;
}
/**
* Build order by
* @param string $sidx sort index
* @param string $sord sort order
* @return string orderby
*/
function buildOrderBy($sidx, $sord)
{
// no sort index? no sorting
if (!$sidx) { return ''; }
return " ORDER BY " . mysql_escape_string($sidx) . " " . mysql_escape_string($sord);
}
/**
* Build limit string
* @param int $page page index
* @param int $limit page limit
* @return string limit
*/
function buildLimit($page, $limit)
{
// validate page
if (!ctype_digit((string) $page))
{
throw new Exception('Page not digit');
}
// validate limit
if (!ctype_digit((string) $limit))
{
throw new Exception('Limit not digit');
}
// set paging
$limit_page = $limit * $page - $limit;
// set limit
return ' LIMIT '. $limit_page . ", " . $limit;
}
/**
* Connects to database
* @param array $database associative array with database information
* @return mysql resource
*/
function connect2database(Array $database)
{
// connect to database server
if (!$connection = mysql_connect($database['host'], $database['username'], $database['password']))
{
throw new Exception('Failed to connect to database');
}
// select database
if (!mysql_select_db($database['name'], $connection))
{
throw new Exception('Failed to select the database');
}
return $connection;
}
/**
* Gets films array
* @param array $where associative array key value
* @param string $sidx sorting index
* @param string $sord sorting order
* @param int $page sorting page
* @param int $limit sorting limit
* @return array films data
*/
function getFilms($where, $sidx, $sord, $page, $limit)
{
$data = array();
// setup query to fetch films
$query = " SELECT
*
FROM
film
";
// build where, orderby and limit
$query .= buildWhereClause($where);
$query .= buildOrderBy($sidx, $sord);
$query .= buildLimit($page, $limit);
// execute query
$result = mysql_query($query);
// validate result
if (!$result)
{
throw new Exception('Query failed unable to fetch data ' . mysql_error());
}
// fill array data
while ($row = mysql_fetch_assoc($result))
{
$data[] = $row;
}
return $data;
}
/**
* Get films count
* @param array $where associative array key value
* @return int total films count
*/
function getFilmsCount($where)
{
// retrieve total records count
$query = " SELECT
count(*) as count
FROM
film
";
// build where
$query .= buildWhereClause($where);
// execute query
$result = mysql_query($query);
// validate result
if (!$result)
{
throw new Exception('Query failed unable to fetch data ' . mysql_error());
}
// retrieve count
$row = mysql_fetch_assoc($result);
return $row['count'];
}
/**
* Decodes filters which are returned by jqgrid
* @param $filters Filters returned by jqgrid
* @return array Filters
*/
function decodeFilters($filters)
{
// setup the filter array
$aFilters = array();
// Decode filter objects
$oFilterObjects = json_decode($filters);
// if there are any filter objects
if ($oFilterObjects && count($oFilterObjects->rules) > 0)
{
// loop trough the filter objets and set key => value
foreach ($oFilterObjects->rules as $oFilter)
{
$aFilters[$oFilter->field] = $oFilter->data;
}
}
return $aFilters;
}
function getAllTitles()
{
// retrieve total records count
$query = " SELECT
title
FROM
film
GROUP BY
title
";
// execute query
$result = mysql_query($query);
// validate result
if (!$result)
{
throw new Exception('Query failed unable to fetch data ' . mysql_error());
}
$titles = array();
// retrieve count
while ($row = mysql_fetch_assoc($result))
{
$titles[] = $row['title'];
}
return $titles;
}