Author: sevein
Date: Wed May  2 17:48:25 2012
New Revision: 11602

Log:
Refactor (once again) the institution browse code

Modified:
   branches/2.0/apps/qubit/modules/repository/actions/browseAction.class.php

Modified: 
branches/2.0/apps/qubit/modules/repository/actions/browseAction.class.php
==============================================================================
--- branches/2.0/apps/qubit/modules/repository/actions/browseAction.class.php   
Wed May  2 17:46:40 2012        (r11601)
+++ branches/2.0/apps/qubit/modules/repository/actions/browseAction.class.php   
Wed May  2 17:48:25 2012        (r11602)
@@ -24,72 +24,58 @@
  * @version    svn:$Id$
  */
 
-class RepositoryBrowseAction extends DefaultBrowseAction
+class RepositoryBrowseAction extends sfAction
 {
-  public static
-    $FACETS = array(
-      'types',
-      'contact.i18n.region');
-
-  protected function buildFacetTable($name, $facet)
+  public function execute($request)
   {
-    $ids = array();
-    foreach ($facet['terms'] as $term)
+    if (!isset($request->limit))
     {
-      $ids[$term['term']] = $term['count'];
+      $request->limit = sfConfig::get('app_hits_per_page');
     }
 
-    switch ($name)
-    {
-      case 'types':
-        $criteria = new Criteria;
-        $criteria->add(QubitObjectTermRelation::ID, array_keys($ids), 
Criteria::IN);
-        $types = QubitObjectTermRelation::get($criteria);
-
-        foreach ($types as $type)
-        {
-          $typeNames[$type->id] = $type->term->getName(array(
-            'cultureFallback' => true,
-            'culture' => $this->context->user->getCulture()));
-        }
-
-        foreach ($facet['terms'] as $term)
-        {
-          $this->pager->facets[strtr($name, '.', '_')]['terms'][$term['term']] 
= array(
-            'count' => $term['count'],
-            'term' => $typeNames[$term['term']]);        }
-
-        break;
+    $queryBool = new Elastica_Query_Bool();
+    $queryBool->addShould(new Elastica_Query_MatchAll());
 
-      case 'contact.i18n.region':
-        foreach ($facet['terms'] as $term)
+    $this->filters = array();
+    foreach ($this->request->getGetParameters() as $param => $value)
+    {
+      if (in_array(strtr($param, '_', '.'), array('types', 
'contact.i18n.region')))
+      {
+        foreach (explode(',', $value) as $facetValue)
         {
-          $this->pager->facets[strtr($name, '.', '_')]['terms'][$term['term']] 
= array(
-            'count' => $term['count'],
-            'term' => $term['term']);
+          // don't include empty filters (querystring sanitization)
+          if ('' != preg_replace('/[\s\t\r\n]*/', '', $facetValue))
+          {
+            $this->filters[$param][] = $facetValue;
+
+            $queryBool->addMust(new Elastica_Query_Term(
+              array(strtr($param, '_', '.') => $facetValue)));
+          }
         }
+      }
+    }
 
-        break;
-
-      default:
-
-        return parent::buildFacetTable($name);
+    $query = new Elastica_Query();
+    $query->setSort(array('_score' => 'desc', 'slug' => 'asc'));
+    $query->setLimit($request->limit);
+    $query->setQuery($queryBool);
 
+    if (!empty($request->page))
+    {
+      $query->setFrom(($request->page - 1) * $request->limit);
     }
-  }
 
-  protected function earlyExecute()
-  {
-    $this->query = new Elastica_Query(new Elastica_Query_MatchAll());
-  }
-
-  public function execute($request)
-  {
-    parent::execute($request);
+    foreach (array('types', 'contact.i18n.region') as $item)
+    {
+      $facet = new Elastica_Facet_Terms($item);
+      $facet->setField($item);
+      $facet->setSize(50);
+      $query->addFacet($facet);
+    }
 
     try
     {
-      $resultSet = 
QubitSearch::getInstance()->index->getType('QubitRepository')->search($this->query);
+      $resultSet = 
QubitSearch::getInstance()->index->getType('QubitRepository')->search($query);
     }
     catch (Exception $e)
     {
@@ -99,19 +85,55 @@
     }
 
     $this->pager = new QubitSearchPager($resultSet);
-    $this->pager->setPage($request->page);
+    $this->pager->setPage($request->page ? $request->page : 1);
     $this->pager->setMaxPerPage($request->limit);
 
     if ($this->pager->hasResults())
     {
       foreach ($this->pager->getFacets() as $name => $facet)
       {
-        $this->buildFacetTable($name, $facet);
+        if (isset($facet['terms']))
+        {
+          $ids = array();
+          foreach ($facet['terms'] as $term)
+          {
+            $ids[$term['term']] = $term['count'];
+          }
+        }
+
+        switch ($name)
+        {
+          case 'types':
+            $criteria = new Criteria;
+            $criteria->add(QubitTerm::ID, array_keys($ids), Criteria::IN);
+            $types = QubitTerm::get($criteria);
+
+            foreach ($types as $item)
+            {
+              $typeNames[$item->id] = $item->name;
+            }
+
+            foreach ($facet['terms'] as $term)
+            {
+              $facets[strtr($name, '.', '_')]['terms'][$term['term']] = array(
+                'count' => $term['count'],
+                'term' => $typeNames[$term['term']]);        }
+
+            break;
+
+          case 'contact.i18n.region':
+            foreach ($facet['terms'] as $term)
+            {
+              $facets[strtr($name, '.', '_')]['terms'][$term['term']] = array(
+                'count' => $term['count'],
+                'term' => $term['term']);
+            }
+
+            break;
+        }
       }
-    }
-    else if (empty($this->error))
-    {
-      // $this->error = $this->context->i18n->__('No %1% found.', array('%1%' 
=> __($this->field)));
+
+      $this->pager->facets = $facets;
     }
   }
 }

-- 
You received this message because you are subscribed to the Google Groups 
"Qubit Toolkit Commits" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/qubit-commits?hl=en.

Reply via email to