2012-03-08 18:30:56 +01:00
|
|
|
<?php
|
|
|
|
|
2012-07-16 03:17:13 +02:00
|
|
|
class Application_Model_Datatables
|
|
|
|
{
|
2012-08-23 16:41:40 +02:00
|
|
|
private static function buildWhereClauseForAdvancedSearch($dbname2searchTerm)
|
|
|
|
{
|
2012-09-05 20:50:26 +02:00
|
|
|
$where['clause'] = array();
|
|
|
|
$where['params'] = array();
|
2012-08-23 16:41:40 +02:00
|
|
|
foreach ($dbname2searchTerm as $dbname=>$term) {
|
|
|
|
$isRange = false;
|
|
|
|
if (strstr($term, '~')) {
|
|
|
|
$info = explode('~', $term);
|
2012-09-04 16:03:24 +02:00
|
|
|
if ($dbname == 'utime' || $dbname == 'mtime') {
|
|
|
|
$input1 = isset($info[0])?Application_Common_DateHelper::ConvertToUtcDateTimeString($info[0]):null;
|
|
|
|
$input2 = isset($info[1])?Application_Common_DateHelper::ConvertToUtcDateTimeString($info[1]):null;
|
2012-10-31 17:32:40 +01:00
|
|
|
} else if($dbname == 'bit_rate' || $dbname == 'sample_rate') {
|
|
|
|
$input1 = isset($info[0])?doubleval($info[0]) * 1000:null;
|
|
|
|
$input2 = isset($info[1])?doubleval($info[1]) * 1000:null;
|
2012-09-04 16:03:24 +02:00
|
|
|
} else {
|
|
|
|
$input1 = isset($info[0])?$info[0]:null;
|
|
|
|
$input2 = isset($info[1])?$info[1]:null;
|
|
|
|
}
|
2012-08-23 16:41:40 +02:00
|
|
|
$isRange = true;
|
|
|
|
} else {
|
|
|
|
$input1 = $term;
|
|
|
|
}
|
2012-08-29 16:58:03 +02:00
|
|
|
|
2012-08-23 16:41:40 +02:00
|
|
|
if ($isRange) {
|
|
|
|
$sub = array();
|
|
|
|
if ($input1 != null) {
|
2012-09-05 20:50:26 +02:00
|
|
|
$sub[] = $dbname." >= :" . $dbname . "1";
|
2012-08-23 16:41:40 +02:00
|
|
|
}
|
|
|
|
if ($input2 != null) {
|
2012-09-05 20:50:26 +02:00
|
|
|
$sub[] = $dbname." <= :" . $dbname . "2";
|
2012-08-23 16:41:40 +02:00
|
|
|
}
|
|
|
|
if (!empty($sub)) {
|
2012-09-05 20:50:26 +02:00
|
|
|
$where['clause'][$dbname] = "(".implode(' AND ', $sub).")";
|
2012-09-10 20:16:36 +02:00
|
|
|
if ($input1 != null) {
|
|
|
|
$where['params'][$dbname."1"] = $input1;
|
|
|
|
}
|
2012-09-05 20:50:26 +02:00
|
|
|
if ($input2 != null) {
|
|
|
|
$where['params'][$dbname."2"] = $input2;
|
|
|
|
}
|
2012-08-23 16:41:40 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (trim($input1) !== "") {
|
2012-09-05 20:50:26 +02:00
|
|
|
$where['clause'][$dbname] = $dbname." ILIKE :" . $dbname."1";
|
|
|
|
$where['params'][$dbname."1"] = "%".$input1."%";
|
2012-08-23 16:41:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-09-05 20:50:26 +02:00
|
|
|
return $where;
|
2012-08-23 16:41:40 +02:00
|
|
|
}
|
2012-07-11 00:51:32 +02:00
|
|
|
/*
|
|
|
|
* query used to return data for a paginated/searchable datatable.
|
|
|
|
*/
|
2012-08-31 19:34:21 +02:00
|
|
|
public static function findEntries($con, $displayColumns, $fromTable,
|
|
|
|
$data, $dataProp = "aaData")
|
2012-07-11 00:51:32 +02:00
|
|
|
{
|
2012-09-19 22:34:46 +02:00
|
|
|
$librarySetting =
|
|
|
|
Application_Model_Preference::getCurrentLibraryTableColumnMap();
|
2012-08-31 19:34:21 +02:00
|
|
|
//$displayColumns[] = 'owner';
|
2012-08-29 16:58:03 +02:00
|
|
|
|
2012-08-23 16:41:40 +02:00
|
|
|
// map that maps original column position to db name
|
|
|
|
$current2dbname = array();
|
|
|
|
// array of search terms
|
2012-08-31 19:34:21 +02:00
|
|
|
$orig2searchTerm = array();
|
|
|
|
foreach ($data as $key => $d) {
|
2012-08-23 16:41:40 +02:00
|
|
|
if (strstr($key, "mDataProp_")) {
|
|
|
|
list($dump, $index) = explode("_", $key);
|
|
|
|
$current2dbname[$index] = $d;
|
2012-08-29 16:58:03 +02:00
|
|
|
} elseif (strstr($key, "sSearch_")) {
|
|
|
|
list($dump, $index) = explode("_", $key);
|
2012-08-23 16:41:40 +02:00
|
|
|
$orig2searchTerm[$index] = $d;
|
|
|
|
}
|
|
|
|
}
|
2012-08-29 16:58:03 +02:00
|
|
|
|
2012-08-23 16:41:40 +02:00
|
|
|
// map that maps dbname to searchTerm
|
|
|
|
$dbname2searchTerm = array();
|
2012-09-14 20:53:25 +02:00
|
|
|
foreach ($current2dbname as $currentPos => $dbname) {
|
2012-09-17 22:46:49 +02:00
|
|
|
$new_index = $librarySetting($currentPos);
|
|
|
|
// TODO : Fix this retarded hack later. Just a band aid for
|
|
|
|
// now at least we print some warnings so that we don't
|
2012-09-17 22:47:33 +02:00
|
|
|
// forget about this -- cc-4462
|
2012-09-17 22:46:49 +02:00
|
|
|
if ( array_key_exists($new_index, $orig2searchTerm) ) {
|
|
|
|
$dbname2searchTerm[$dbname] = $orig2searchTerm[$new_index];
|
|
|
|
} else {
|
|
|
|
Logging::warn("Trying to reorder to unknown index
|
|
|
|
printing as much debugging as possible...");
|
|
|
|
$debug = array(
|
|
|
|
'$new_index' => $new_index,
|
|
|
|
'$currentPos' => $currentPos,
|
|
|
|
'$orig2searchTerm' => $orig2searchTerm);
|
|
|
|
Logging::warn($debug);
|
|
|
|
}
|
2012-08-23 16:41:40 +02:00
|
|
|
}
|
2012-08-29 16:58:03 +02:00
|
|
|
|
2012-07-11 00:51:32 +02:00
|
|
|
$where = array();
|
2012-09-19 22:34:46 +02:00
|
|
|
/* Holds the parameters for binding after the statement has been
|
|
|
|
prepared */
|
2012-09-05 20:50:26 +02:00
|
|
|
$params = array();
|
2012-08-29 16:58:03 +02:00
|
|
|
|
2012-09-19 23:05:20 +02:00
|
|
|
if (isset($data['advSearch']) && $data['advSearch'] === 'true') {
|
2012-09-19 22:39:56 +02:00
|
|
|
$advancedWhere = self::buildWhereClauseForAdvancedSearch($dbname2searchTerm);
|
|
|
|
if (!empty($advancedWhere['clause'])) {
|
|
|
|
$where[] = join(" AND ", $advancedWhere['clause']);
|
|
|
|
$params = $advancedWhere['params'];
|
|
|
|
}
|
2012-08-23 16:41:40 +02:00
|
|
|
}
|
2012-07-11 00:51:32 +02:00
|
|
|
|
|
|
|
if ($data["sSearch"] !== "") {
|
|
|
|
$searchTerms = explode(" ", $data["sSearch"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$selectorCount = "SELECT COUNT(*) ";
|
|
|
|
$selectorRows = "SELECT ".join(",", $displayColumns)." ";
|
|
|
|
|
|
|
|
$sql = $selectorCount." FROM ".$fromTable;
|
|
|
|
$sqlTotalRows = $sql;
|
|
|
|
|
2012-08-31 19:34:21 +02:00
|
|
|
|
2012-07-11 00:51:32 +02:00
|
|
|
if (isset($searchTerms)) {
|
|
|
|
$searchCols = array();
|
|
|
|
for ($i = 0; $i < $data["iColumns"]; $i++) {
|
|
|
|
if ($data["bSearchable_".$i] == "true") {
|
|
|
|
$searchCols[] = $data["mDataProp_{$i}"];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$outerCond = array();
|
2012-09-05 20:50:26 +02:00
|
|
|
$simpleWhere = array();
|
2012-07-11 00:51:32 +02:00
|
|
|
|
|
|
|
foreach ($searchTerms as $term) {
|
|
|
|
|
|
|
|
foreach ($searchCols as $col) {
|
2012-09-05 20:50:26 +02:00
|
|
|
$simpleWhere['clause']["simple_".$col] = "{$col}::text ILIKE :simple_".$col;
|
2012-09-10 20:37:38 +02:00
|
|
|
$simpleWhere['params']["simple_".$col] = "%".$term."%";
|
2012-07-11 00:51:32 +02:00
|
|
|
}
|
2012-09-05 20:50:26 +02:00
|
|
|
$outerCond[] = "(".implode(" OR ", $simpleWhere['clause']).")";
|
2012-07-11 00:51:32 +02:00
|
|
|
}
|
2012-09-05 20:50:26 +02:00
|
|
|
$where[] = "(" .implode(" AND ", $outerCond). ")";
|
|
|
|
$params = array_merge($params, $simpleWhere['params']);
|
2012-07-11 00:51:32 +02:00
|
|
|
}
|
|
|
|
// End Where clause
|
|
|
|
|
|
|
|
// Order By clause
|
|
|
|
$orderby = array();
|
2012-07-16 03:17:13 +02:00
|
|
|
for ($i = 0; $i < $data["iSortingCols"]; $i++) {
|
2012-07-11 00:51:32 +02:00
|
|
|
$num = $data["iSortCol_".$i];
|
|
|
|
$orderby[] = $data["mDataProp_{$num}"]." ".$data["sSortDir_".$i];
|
|
|
|
}
|
|
|
|
$orderby[] = "id";
|
|
|
|
$orderby = join("," , $orderby);
|
|
|
|
// End Order By clause
|
|
|
|
|
|
|
|
$displayLength = intval($data["iDisplayLength"]);
|
2012-09-05 20:50:26 +02:00
|
|
|
$needToBind = false;
|
2012-07-11 00:51:32 +02:00
|
|
|
if (count($where) > 0) {
|
2012-09-05 20:50:26 +02:00
|
|
|
$needToBind = true;
|
|
|
|
$where = join(" OR ", $where);
|
2012-07-11 00:51:32 +02:00
|
|
|
$sql = $selectorCount." FROM ".$fromTable." WHERE ".$where;
|
|
|
|
$sqlTotalDisplayRows = $sql;
|
|
|
|
|
|
|
|
$sql = $selectorRows." FROM ".$fromTable." WHERE ".$where." ORDER BY ".$orderby;
|
|
|
|
|
|
|
|
//limit the results returned.
|
|
|
|
if ($displayLength !== -1) {
|
|
|
|
$sql .= " OFFSET ".$data["iDisplayStart"]." LIMIT ".$displayLength;
|
|
|
|
}
|
2012-07-16 03:17:13 +02:00
|
|
|
} else {
|
2012-07-11 00:51:32 +02:00
|
|
|
$sql = $selectorRows." FROM ".$fromTable." ORDER BY ".$orderby;
|
|
|
|
|
|
|
|
//limit the results returned.
|
|
|
|
if ($displayLength !== -1) {
|
|
|
|
$sql .= " OFFSET ".$data["iDisplayStart"]." LIMIT ".$displayLength;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
2012-08-31 19:34:21 +02:00
|
|
|
|
2012-07-11 00:51:32 +02:00
|
|
|
$r = $con->query($sqlTotalRows);
|
|
|
|
$totalRows = $r->fetchColumn(0);
|
|
|
|
|
|
|
|
if (isset($sqlTotalDisplayRows)) {
|
2012-09-06 17:21:36 +02:00
|
|
|
$totalDisplayRows = Application_Common_Database::prepareAndExecute($sqlTotalDisplayRows, $params, 'column');
|
2012-07-16 03:17:13 +02:00
|
|
|
} else {
|
2012-07-11 00:51:32 +02:00
|
|
|
$totalDisplayRows = $totalRows;
|
|
|
|
}
|
|
|
|
|
2012-09-05 20:50:26 +02:00
|
|
|
//TODO
|
|
|
|
if ($needToBind) {
|
2012-09-06 17:21:36 +02:00
|
|
|
$results = Application_Common_Database::prepareAndExecute($sql, $params);
|
2012-09-05 20:50:26 +02:00
|
|
|
} else {
|
|
|
|
$stmt = $con->query($sql);
|
|
|
|
$stmt->setFetchMode(PDO::FETCH_ASSOC);
|
|
|
|
$results = $stmt->fetchAll();
|
|
|
|
}
|
|
|
|
|
2012-07-31 23:46:37 +02:00
|
|
|
// we need to go over all items and fix length for playlist
|
|
|
|
// in case the playlist contains dynamic block
|
|
|
|
foreach ($results as &$r) {
|
2012-08-21 22:37:33 +02:00
|
|
|
//this function is also called for Manage Users so in
|
|
|
|
//this case there will be no 'ftype'
|
|
|
|
if (isset($r['ftype'])) {
|
|
|
|
if ($r['ftype'] == 'playlist') {
|
|
|
|
$pl = new Application_Model_Playlist($r['id']);
|
|
|
|
$r['length'] = $pl->getLength();
|
2012-08-29 16:58:03 +02:00
|
|
|
} elseif ($r['ftype'] == "block") {
|
2012-08-21 22:37:33 +02:00
|
|
|
$bl = new Application_Model_Block($r['id']);
|
2012-09-11 20:05:40 +02:00
|
|
|
$r['bl_type'] = $bl->isStatic() ? 'static' : 'dynamic';
|
|
|
|
$r['length'] = $bl->getLength();
|
2012-08-15 18:51:53 +02:00
|
|
|
}
|
2012-07-31 23:46:37 +02:00
|
|
|
}
|
|
|
|
}
|
2012-07-16 03:17:13 +02:00
|
|
|
} catch (Exception $e) {
|
2012-07-11 00:51:32 +02:00
|
|
|
Logging::debug($e->getMessage());
|
|
|
|
}
|
|
|
|
return array(
|
2012-08-31 19:34:21 +02:00
|
|
|
"sEcho" => intval($data["sEcho"]),
|
2012-07-11 00:51:32 +02:00
|
|
|
"iTotalDisplayRecords" => intval($totalDisplayRows),
|
2012-08-31 19:34:21 +02:00
|
|
|
"iTotalRecords" => intval($totalRows),
|
|
|
|
$dataProp => $results
|
2012-07-11 00:51:32 +02:00
|
|
|
);
|
|
|
|
}
|
2012-04-10 14:45:48 +02:00
|
|
|
}
|