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)
|
|
|
|
{
|
|
|
|
$where = array();
|
|
|
|
foreach ($dbname2searchTerm as $dbname=>$term) {
|
|
|
|
$isRange = false;
|
|
|
|
if (strstr($term, '~')) {
|
|
|
|
$info = explode('~', $term);
|
2012-08-29 23:37:02 +02:00
|
|
|
$input1 = isset($info[0])?Application_Common_DateHelper::ConvertToUtcDateTimeString($info[0]):null;
|
|
|
|
$input2 = isset($info[1])?Application_Common_DateHelper::ConvertToUtcDateTimeString($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) {
|
|
|
|
$sub[] = $dbname." >= '".$input1."'";
|
|
|
|
}
|
|
|
|
if ($input2 != null) {
|
|
|
|
$sub[] = $dbname." <= '".$input2."'";
|
|
|
|
}
|
|
|
|
if (!empty($sub)) {
|
|
|
|
$where[] = "(".implode(' AND ', $sub).")";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (trim($input1) !== "") {
|
|
|
|
$where[] = $dbname." ILIKE "."'%".$input1."%'";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-29 16:58:03 +02:00
|
|
|
|
2012-08-23 16:41:40 +02:00
|
|
|
return implode(" AND ", $where);
|
|
|
|
}
|
2012-07-11 00:51:32 +02:00
|
|
|
/*
|
|
|
|
* query used to return data for a paginated/searchable datatable.
|
|
|
|
*/
|
|
|
|
public static function findEntries($con, $displayColumns, $fromTable, $data, $dataProp = "aaData")
|
|
|
|
{
|
2012-08-23 16:41:40 +02:00
|
|
|
$librarySetting = Application_Model_Preference::getCurrentLibraryTableSetting();
|
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
|
|
|
|
$orig2searchTerm= array();
|
|
|
|
foreach ($data as $key=>$d) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// map that maps current column position to original position
|
|
|
|
$current2orig = $librarySetting['ColReorder'];
|
2012-08-29 16:58:03 +02:00
|
|
|
|
2012-08-23 16:41:40 +02:00
|
|
|
// map that maps dbname to searchTerm
|
|
|
|
$dbname2searchTerm = array();
|
|
|
|
foreach ($current2dbname as $currentPos=>$dbname) {
|
|
|
|
$dbname2searchTerm[$dbname] = $orig2searchTerm[$current2orig[$currentPos]];
|
|
|
|
}
|
2012-08-29 16:58:03 +02:00
|
|
|
|
2012-07-11 00:51:32 +02:00
|
|
|
$where = array();
|
2012-08-29 16:58:03 +02:00
|
|
|
|
2012-08-23 16:41:40 +02:00
|
|
|
$advancedWhere = self::buildWhereClauseForAdvancedSearch($dbname2searchTerm);
|
|
|
|
if ($advancedWhere != "") {
|
|
|
|
$where[] = $advancedWhere;
|
|
|
|
}
|
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;
|
|
|
|
|
|
|
|
if (isset($searchTerms)) {
|
|
|
|
$searchCols = array();
|
|
|
|
for ($i = 0; $i < $data["iColumns"]; $i++) {
|
|
|
|
if ($data["bSearchable_".$i] == "true") {
|
|
|
|
$searchCols[] = $data["mDataProp_{$i}"];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$outerCond = array();
|
|
|
|
|
|
|
|
foreach ($searchTerms as $term) {
|
|
|
|
$innerCond = array();
|
|
|
|
|
|
|
|
foreach ($searchCols as $col) {
|
|
|
|
$escapedTerm = pg_escape_string($term);
|
|
|
|
$innerCond[] = "{$col}::text ILIKE '%{$escapedTerm}%'";
|
|
|
|
}
|
|
|
|
$outerCond[] = "(".join(" OR ", $innerCond).")";
|
|
|
|
}
|
|
|
|
$where[] = "(".join(" AND ", $outerCond).")";
|
|
|
|
}
|
|
|
|
// 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"]);
|
|
|
|
if (count($where) > 0) {
|
|
|
|
$where = join(" AND ", $where);
|
|
|
|
$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 {
|
|
|
|
$r = $con->query($sqlTotalRows);
|
|
|
|
$totalRows = $r->fetchColumn(0);
|
|
|
|
|
|
|
|
if (isset($sqlTotalDisplayRows)) {
|
|
|
|
$r = $con->query($sqlTotalDisplayRows);
|
|
|
|
$totalDisplayRows = $r->fetchColumn(0);
|
2012-07-16 03:17:13 +02:00
|
|
|
} else {
|
2012-07-11 00:51:32 +02:00
|
|
|
$totalDisplayRows = $totalRows;
|
|
|
|
}
|
|
|
|
|
|
|
|
$r = $con->query($sql);
|
|
|
|
$r->setFetchMode(PDO::FETCH_ASSOC);
|
|
|
|
$results = $r->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']);
|
|
|
|
if ($bl->isStatic()) {
|
|
|
|
$r['bl_type'] = 'static';
|
|
|
|
} else {
|
|
|
|
$r['bl_type'] = '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(
|
|
|
|
"sEcho" => intval($data["sEcho"]),
|
|
|
|
"iTotalDisplayRecords" => intval($totalDisplayRows),
|
|
|
|
"iTotalRecords" => intval($totalRows),
|
|
|
|
$dataProp => $results
|
|
|
|
);
|
|
|
|
}
|
2012-04-10 14:45:48 +02:00
|
|
|
}
|