hdalab/filter.php
changeset 115 46c0f7a935d1
parent 110 6455b65fa3b7
child 118 fdf808d7d374
--- a/hdalab/filter.php	Tue Jan 17 00:19:27 2012 +0100
+++ b/hdalab/filter.php	Mon Jan 30 18:20:21 2012 +0100
@@ -11,7 +11,7 @@
 $tag_count = (isset($_REQUEST['tagcount']) ? pg_escape_string($_REQUEST['tagcount']) : 30);
 $cont_count = 0;
 
-if ($label || $periode || $contentlist) {
+if ($label or $periode or gettype($contentlist) != "NULL") {
     $globalsql = "SELECT id, title, description, url FROM hdabo_datasheet E";
     $globalids = array();
     $globalfilters = array();
@@ -44,11 +44,15 @@
             $globalfilters[] = "EXISTS (SELECT * FROM hdabo_taggedsheet G WHERE G.tag_id IN (" . join(",", $ids) . ") AND G.order <= $max_tag_order AND G.datasheet_id = E.id)";
         }
     }
-    if ($contentlist) {
-        $globalfilters[] = "id IN ($contentlist)";
+    if (gettype($contentlist) != "NULL") {
+        if ($contentlist) {
+            $globalfilters[] = "id IN ($contentlist)";
+        } else {
+            $globalfilters[] = "id IS NULL";
+        }
     }
     $globalsql = $globalsql.(count($globalfilters) ? " WHERE ".join($globalfilters," AND ") : "" );
-    if (!$label && !$periode && !$contentlist) {
+    if (!$label && !$periode && gettype($contentlist)=="NULL") {
         $globalsql = $globalsql." ORDER BY RANDOM()";
     }
     $rq = pg_query($globalsql);
@@ -66,26 +70,37 @@
         $countries[$ligne[0]] = array("isocode" => $ligne[1], "score" => 0);
     }
     pg_free_result($rq);
+    $rq = pg_query("SELECT DISTINCT label FROM hdabo_tag WHERE category_id=5");
+    $disciplines = array();
+    while($ligne = pg_fetch_row($rq)) {
+        $disciplines[$ligne[0]] = array("label" => $ligne[0], "score" => 0);
+    }
+    pg_free_result($rq);
     $tags = array();
     $contentids = join(",",array_keys($contenus));
-    $rq = pg_query("SELECT L.datasheet_id, L.tag_id, M.label, L.order FROM hdabo_taggedsheet L, hdabo_tag M WHERE L.datasheet_id IN ($contentids) AND L.tag_id = M.id AND L.order <= $max_tag_order ORDER BY L.order ASC");
-    while($ligne = pg_fetch_row($rq)) {
-        $match_tag = in_array($ligne[1], $globalids);
-        $contenus[$ligne[0]]["tags"][] = array("id" => $ligne[1], "label" => $ligne[2], "order" => $ligne[3], "match" => $match_tag);
-        $tagscore = 2*$max_tag_order-$ligne[3];
-        if (!isset($tags[$ligne[1]])) {
-            $tags[$ligne[1]] = array("id" => $ligne[1], "label" => $ligne[2], "score" => 0);
+    if ($contentids) {
+        $rq = pg_query("SELECT L.datasheet_id, L.tag_id, M.label, L.order FROM hdabo_taggedsheet L, hdabo_tag M WHERE L.datasheet_id IN ($contentids) AND L.tag_id = M.id AND L.order <= $max_tag_order ORDER BY L.order ASC");
+        while($ligne = pg_fetch_row($rq)) {
+            $match_tag = in_array($ligne[1], $globalids);
+            $contenus[$ligne[0]]["tags"][] = array("id" => $ligne[1], "label" => $ligne[2], "order" => $ligne[3], "match" => $match_tag);
+            $tagscore = 2*$max_tag_order-$ligne[3];
+            if (!isset($tags[$ligne[1]])) {
+                $tags[$ligne[1]] = array("id" => $ligne[1], "label" => $ligne[2], "score" => 0);
+            }
+            $tags[$ligne[1]]["score"] += $tagscore;
+            if ($match_tag) {
+                $contenus[$ligne[0]]["score"] += $tagscore;
+                $tags[$ligne[1]]["match"] = True;
+            }
+            if (isset($countries[$ligne[2]])) {
+                $countries[$ligne[2]]["score"] += $tagscore;
+            }
+            if (isset($disciplines[$ligne[2]])) {
+                $disciplines[$ligne[2]]["score"] += $tagscore;
+            }
         }
-        $tags[$ligne[1]]["score"] += $tagscore;
-        if ($match_tag) {
-            $contenus[$ligne[0]]["score"] += $tagscore;
-            $tags[$ligne[1]]["match"] = True;
-        }
-        if (isset($countries[$ligne[2]])) {
-            $countries[$ligne[2]]["score"] += $tagscore;
-        }
+        pg_free_result($rq);
     }
-    pg_free_result($rq);
     function triscore($a, $b) {
         return $b["score"] - $a["score"];
     }
@@ -96,16 +111,20 @@
         $contenus = array_values($contenus);
     }
     usort($tags,"triscore");
+    usort($disciplines,"triscore");
     $countries = array_values($countries);
     $tags = array_slice($tags, 0, $tag_count);
-    $rq = pg_query("SELECT U.start_year, U.end_year, SUM(".(2*$max_tag_order)." - V.order)/(U.end_year + 1 - U.start_year) score FROM hdaviz_years U, hdabo_taggedsheet V WHERE U.tag_id = V.tag_id AND V.order <= $max_tag_order AND V.datasheet_id IN ($contentids) GROUP BY U.start_year, U.end_year");
+    $disciplines = array_slice($disciplines, 0, 10);
     $years = array();
-    while($ligne = pg_fetch_row($rq)) {
-        foreach(range($ligne[0], $ligne[1]) as $year) {
-            $years[$year] = $ligne[2] + ( isset($years[$year]) ? $years[$year] : 0 );
+    if ($contentids) {
+        $rq = pg_query("SELECT U.start_year, U.end_year, SUM(".(2*$max_tag_order)." - V.order)/(U.end_year + 1 - U.start_year) score FROM hdaviz_years U, hdabo_taggedsheet V WHERE U.tag_id = V.tag_id AND V.order <= $max_tag_order AND V.datasheet_id IN ($contentids) GROUP BY U.start_year, U.end_year");
+        while($ligne = pg_fetch_row($rq)) {
+            foreach(range($ligne[0], $ligne[1]) as $year) {
+                $years[$year] = $ligne[2] + ( isset($years[$year]) ? $years[$year] : 0 );
+            }
         }
+        pg_free_result($rq);
     }
-    pg_free_result($rq);
 } else {
     $rq = pg_query("SELECT id, title, description, url FROM hdabo_datasheet ORDER BY RANDOM() LIMIT $content_count");
     while($ligne = pg_fetch_assoc($rq)) {
@@ -128,6 +147,9 @@
     $rq = pg_query("SELECT C.isocode, SUM(".(2*$max_tag_order)." - B.order) score FROM hdabo_tag A, hdabo_taggedsheet B, hdaviz_cc C WHERE A.id = B.tag_id AND B.order <= $max_tag_order AND A.label = C.label GROUP BY C.isocode");
     $countries = pg_fetch_all($rq);
     pg_free_result($rq);
+    $rq = pg_query("SELECT A.label, SUM(".(2*$max_tag_order)." - B.order) score FROM hdabo_tag A, hdabo_taggedsheet B WHERE A.id = B.tag_id AND B.order <= $max_tag_order AND A.category_id=5 GROUP BY A.label ORDER BY score DESC LIMIT 10");
+    $disciplines = pg_fetch_all($rq);
+    pg_free_result($rq);
     $rq = pg_query("SELECT U.start_year, U.end_year, SUM(".(2*$max_tag_order)." - hdabo_taggedsheet.order)/(U.end_year + 1 - U.start_year) score FROM hdaviz_years U, hdabo_taggedsheet WHERE U.tag_id = hdabo_taggedsheet.tag_id AND hdabo_taggedsheet.order <= $max_tag_order GROUP BY U.start_year, U.end_year");
     $years = array();
     while($ligne = pg_fetch_row($rq)) {
@@ -149,6 +171,6 @@
         }
     }
 }
-$output = array("count" => $cont_count, "contents" => $contenus, "tags" => $tags, "sparkline" => $yearchange, "countries" => $countries);
+$output = array("count" => $cont_count, "contents" => $contenus, "tags" => $tags, "sparkline" => $yearchange, "countries" => $countries, "disciplines" => $disciplines);
 echo json_encode($output);
 ?>
\ No newline at end of file