Author: sevein
Date: Tue May  1 17:43:55 2012
New Revision: 11588

Log:
Add facets to term browse page

Modified:
   branches/2.0/apps/qubit/modules/term/actions/browseTermAction.class.php
   branches/2.0/apps/qubit/modules/term/templates/browseTermSuccess.php

Modified: 
branches/2.0/apps/qubit/modules/term/actions/browseTermAction.class.php
==============================================================================
--- branches/2.0/apps/qubit/modules/term/actions/browseTermAction.class.php     
Tue May  1 15:00:51 2012        (r11587)
+++ branches/2.0/apps/qubit/modules/term/actions/browseTermAction.class.php     
Tue May  1 17:43:55 2012        (r11588)
@@ -51,12 +51,42 @@
         break;
     }
 
+    $queryBool->addMust(new Elastica_Query_Term(array('publicationStatusId' => 
QubitTerm::PUBLICATION_STATUS_PUBLISHED_ID)));
+
+    $this->filters = array();
+    foreach ($this->request->getGetParameters() as $param => $value)
+    {
+      if (in_array(strtr($param, '_', '.'), 
array('digitalObject.mediaTypeId')))
+      {
+        foreach (explode(',', $value) as $facetValue)
+        {
+          // 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)));
+          }
+        }
+      }
+    }
+
     $query = new Elastica_Query();
     $query->setSort(array('_score' => 'desc', 'slug' => 'asc'));
     $query->setLimit($request->limit);
-    $queryBool->addMust(new Elastica_Query_Term(array('publicationStatusId' => 
QubitTerm::PUBLICATION_STATUS_PUBLISHED_ID)));
     $query->setQuery($queryBool);
 
+    $facet = new Elastica_Facet_Terms('digitalObject.mediaTypeId');
+    $facet->setField('digitalObject.mediaTypeId');
+    $facet->setSize(50);
+    $query->addFacet($facet);
+
+    $facet = new Elastica_Facet_Range('dates.startDate');
+    $facet->setField('dates.startDate');
+    $facet->addRange(null, null);
+    $query->addFacet($facet);
+
     try
     {
       $resultSet = 
QubitSearch::getInstance()->index->getType('QubitInformationObject')->search($query);
@@ -75,6 +105,59 @@
 
     if ($this->pager->hasResults())
     {
+      foreach ($resultSet->getFacets() as $name => $facet)
+      {
+        if (isset($facet['terms']))
+        {
+          $ids = array();
+          foreach ($facet['terms'] as $term)
+          {
+            $ids[$term['term']] = $term['count'];
+          }
+        }
+
+        switch ($name)
+        {
+          case 'digitalObject.mediaTypeId':
+            $criteria = new Criteria;
+            $criteria->add(QubitTerm::ID, array_keys($ids), Criteria::IN);
+
+            $mediaTypes = QubitTerm::get($criteria);
+
+            foreach ($mediaTypes as $mediaType)
+            {
+              $mediaTypeNames[$mediaType->id] = $mediaType->getName(array(
+                'cultureFallback' => true,
+                'culture' => $this->context->user->getCulture()));
+            }
+
+            foreach ($facet['terms'] as $term)
+            {
+              $facets[strtr($name, '.', '_')]['terms'][$term['term']] = array(
+                'count' => $term['count'],
+                'term' => $mediaTypeNames[$term['term']]);
+            }
+
+            break;
+
+          case 'dates.startDate':
+            // FIXME: is this the best way?
+            $facets[strtr($name, '.', '_')] = $facet['ranges'][0];
+
+            break;
+        }
+      }
+
+      $this->pager->facets = $facets;
+
+      $this->pager->levelsOfDescription = array();
+      foreach (QubitTerm::getLevelsOfDescription() as $lod)
+      {
+        $this->pager->levelsOfDescription[$lod->id] = $lod->getName(array(
+          'cultureFallback' => true,
+          'culture' => $this->context->user->getCulture()));
+      }
+
       $ancestorsIds = array();
       foreach ($this->pager->getResults() as $hit)
       {

Modified: branches/2.0/apps/qubit/modules/term/templates/browseTermSuccess.php
==============================================================================
--- branches/2.0/apps/qubit/modules/term/templates/browseTermSuccess.php        
Tue May  1 15:00:51 2012        (r11587)
+++ branches/2.0/apps/qubit/modules/term/templates/browseTermSuccess.php        
Tue May  1 17:43:55 2012        (r11588)
@@ -30,6 +30,37 @@
 
     <div class="span3" id="facets">
 
+      <?php if (isset($pager->facets['digitalObject_mediaTypeId'])): ?>
+        <div class="section">
+
+          <h2 class="visible-phone widebtn btn-huge" data-toggle="collapse" 
data-target="#mediatypes"><?php echo __('Media Type') ?></h2>
+          <h2 class="hidden-phone"><?php echo __('Media Type') ?></h2>
+
+          <div class="scrollable" id="mediatypes">
+            <ul>
+              <li <?php if ('' == 
$sf_request->getParameter('digitalObject_mediaTypeId')) echo 'class="active"' 
?>><?php echo link_to(__('All'), array('digitalObject_mediaTypeId' => null, 
'page' => null) + $sf_request->getParameterHolder()->getAll()) ?></li>
+              <?php 
foreach($pager->facets['digitalObject_mediaTypeId']['terms'] as $id => $term): 
?>
+                <li <?php if (in_array($id, 
@$filters['digitalObject_mediaTypeId'])) echo 'class="active"' ?>><?php echo 
link_to(__($term['term']).'<span>'.$term['count'].'</span>', 
array('digitalObject_mediaTypeId' => (@$filters['digitalObject_mediaTypeId'] ? 
implode(',', array_diff(array_merge(@$filters['digitalObject_mediaTypeId'], 
array($id)), array_intersect(@$filters['digitalObject_mediaTypeId'], 
array($id)))) : $id), 'page' => null) + 
$sf_request->getParameterHolder()->getAll()) ?></li>
+              <?php endforeach; ?>
+            </ul>
+          </div>
+
+        </div>
+      <?php endif; ?>
+
+      <div class="section">
+
+        <h2 class="visible-phone widebtn btn-huge" data-toggle="collapse" 
data-target="#dates"><?php echo __('Creation date') ?></h2>
+        <h2 class="hidden-phone"><?php echo __('Creation date') ?></h2>
+
+        <div class="scrollable dates" id="dates">
+          <input type="text" value="<?php echo 
$pager->facets['dates_startDate']['min'] ?>" name="from" /> - <input 
type="text" value="<?php echo $pager->facets['dates_startDate']['max'] ?>" 
name="to" />
+        </div>
+
+      </div>
+
+      <hr />
+
       <?php echo link_to(__('Show all %1%', array('%1%' => 
$resource->taxonomy->__toString())),
         array('module' => 'taxonomy', 'action' => 'browse', 'id' => 
$resource->taxonomyId),
         array('class' => 'widebtn')) // HACK Use id deliberately because 
"Subjects" and "Places" menus still use id ?>

-- 
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