Author: sevein
Date: Mon Aug  6 11:50:42 2012
New Revision: 12047

Log:
Use getTreeViewSiblings instead of raw SQL queries for the treeview

Modified:
   trunk/apps/qubit/modules/informationobject/actions/treeViewAction.class.php
   
trunk/apps/qubit/modules/informationobject/actions/treeViewComponent.class.php
   trunk/apps/qubit/modules/informationobject/templates/_treeView.php
   trunk/apps/qubit/modules/informationobject/templates/treeViewSuccess.php
   trunk/lib/model/QubitInformationObject.php

Modified: 
trunk/apps/qubit/modules/informationobject/actions/treeViewAction.class.php
==============================================================================
--- trunk/apps/qubit/modules/informationobject/actions/treeViewAction.class.php 
Mon Aug  6 00:01:54 2012        (r12046)
+++ trunk/apps/qubit/modules/informationobject/actions/treeViewAction.class.php 
Mon Aug  6 11:50:42 2012        (r12047)
@@ -19,8 +19,6 @@
 
 class InformationObjectTreeViewAction extends sfAction
 {
-  const SIBLINGS = 10;
-
   public function execute($request)
   {
     if ('all' == $request->show)
@@ -33,10 +31,11 @@
     }
 
     // Sorting action
+    // This should probably be in a separate action
     if (in_array($request->show, array('moveAfter', 'moveBefore')))
     {
       // Check authorization
-      if (!QubitAcl::check($this->resource, 'update'))
+      if (QubitInformationObject::ROOT_ID == $this->resource->id || 
!QubitAcl::check($this->resource, 'update'))
       {
         QubitAcl::forwardUnauthorized();
       }
@@ -65,65 +64,31 @@
       return sfView::NONE;
     }
 
-    $sql = 'SELECT
-        io.*,
-        i18n.*,
-        slug.slug,
-        pubstat.status_id as publication_status_id
-      FROM '.QubitInformationObject::TABLE_NAME.' io
-      JOIN '.QubitInformationObjectI18n::TABLE_NAME.' i18n
-        ON io.id = i18n.id
-      JOIN '.QubitSlug::TABLE_NAME.' slug
-        ON io.id = slug.object_id
-      JOIN '.QubitStatus::TABLE_NAME.' pubstat
-        ON io.id = pubstat.object_id
-      WHERE
-        io.parent_id = ?
-        AND i18n.culture = ?';
-
     switch ($request->show)
     {
       case 'all':
       case 'item':
       default:
 
-        $sql .= '
-          ORDER BY io.lft ASC
-          LIMIT '.self::SIBLINGS;
-
-        $this->items = QubitPdo::fetchAll($sql, array(
-          $this->resource->id,
-          $this->context->user->getCulture()));
+        // TODO: use getTreeViewSiblings or create new getTreeViewDescendants
+        // also checking ACL permissions per record
+        $criteria = new Criteria;
+        $criteria->add(QubitInformationObject::PARENT_ID, $this->resource->id);
+        $criteria = QubitInformationObject::addTreeViewSortCriteria($criteria);
+        $criteria->setLimit(10);
+        $this->items = QubitInformationObject::get($criteria);
 
         break;
 
-      case 'nextSiblings':
+      case 'prevSiblings':
 
-        $sql .= '
-            AND io.lft > ?
-          ORDER BY io.lft ASC
-          LIMIT '.self::SIBLINGS;
-
-        $this->items = QubitPdo::fetchAll($sql, array(
-          $this->resource->parentId,
-          $this->context->user->getCulture(),
-          $this->resource->lft));
+        $this->items = 
array_reverse($this->resource->getTreeViewSiblings(array('limit' => 5, 
'position' => 'previous')));
 
         break;
 
-      case 'prevSiblings':
+      case 'nextSiblings':
 
-        $sql .= '
-            AND io.lft < ?
-          ORDER BY io.lft DESC
-          LIMIT '.self::SIBLINGS;
-
-        // Notice usage of array_reverse to invert the order
-        $this->items = array_reverse(
-          QubitPdo::fetchAll($sql, array(
-            $this->resource->parentId,
-            $this->context->user->getCulture(),
-            $this->resource->lft)));
+        $this->items = $this->resource->getTreeViewSiblings(array('limit' => 
5, 'position' => 'next'));
 
         break;
     }

Modified: 
trunk/apps/qubit/modules/informationobject/actions/treeViewComponent.class.php
==============================================================================
--- 
trunk/apps/qubit/modules/informationobject/actions/treeViewComponent.class.php  
    Mon Aug  6 00:01:54 2012        (r12046)
+++ 
trunk/apps/qubit/modules/informationobject/actions/treeViewComponent.class.php  
    Mon Aug  6 11:50:42 2012        (r12047)
@@ -19,156 +19,19 @@
 
 class InformationObjectTreeViewComponent extends sfComponent
 {
-  const SIBLINGS = 10;
-
   public function execute($request)
   {
     $this->resource = $request->getAttribute('sf_route')->resource;
     $this->parent = $this->resource->parent;
 
-    $this->treeview = array();
-
-    // Find ancestors
-    $sql = 'SELECT
-        io.*,
-        i18n.*,
-        slug.slug,
-        pubstat.status_id as publication_status_id
-      FROM '.QubitInformationObject::TABLE_NAME.' io
-      JOIN '.QubitInformationObjectI18n::TABLE_NAME.' i18n
-        ON io.id = i18n.id
-      JOIN '.QubitSlug::TABLE_NAME.' slug
-        ON io.id = slug.object_id
-      JOIN '.QubitStatus::TABLE_NAME.' pubstat
-        ON io.id = pubstat.object_id
-      WHERE
-        io.id != '.QubitInformationObject::ROOT_ID.'
-        AND i18n.culture = ?
-        AND io.lft < ?
-        AND io.rgt > ?
-      ORDER BY io.lft';
-
-    $this->treeview['ancestors'] = QubitPdo::fetchAll($sql, array(
-      $this->context->user->getCulture(),
-      $this->resource->lft,
-      $this->resource->rgt));
-
-    // Previous N siblings
-    $sql = 'SELECT
-        io.*,
-        i18n.*,
-        slug.slug,
-        pubstat.status_id as publication_status_id
-      FROM '.QubitInformationObject::TABLE_NAME.' io
-      JOIN '.QubitInformationObjectI18n::TABLE_NAME.' i18n
-        ON io.id = i18n.id
-      JOIN '.QubitSlug::TABLE_NAME.' slug
-        ON io.id = slug.object_id
-      JOIN '.QubitStatus::TABLE_NAME.' pubstat
-        ON io.id = pubstat.object_id
-      WHERE
-        io.parent_id = ?
-        AND i18n.culture = ?
-        AND io.lft < ?
-      ORDER BY io.lft DESC
-      LIMIT '.self::SIBLINGS;
-
-    // Notice usage of array_reverse to invert the order
-    $this->treeview['prevSiblings'] = array_reverse(
-      QubitPdo::fetchAll($sql, array(
-        $this->resource->parentId,
-        $this->context->user->getCulture(),
-        $this->resource->lft)));
-
-    // Next N siblings
-    $sql = 'SELECT
-        io.*,
-        i18n.*,
-        slug.slug,
-        pubstat.status_id as publication_status_id
-      FROM '.QubitInformationObject::TABLE_NAME.' io
-      JOIN '.QubitInformationObjectI18n::TABLE_NAME.' i18n
-        ON io.id = i18n.id
-      JOIN '.QubitSlug::TABLE_NAME.' slug
-        ON io.id = slug.object_id
-      JOIN '.QubitStatus::TABLE_NAME.' pubstat
-        ON io.id = pubstat.object_id
-      WHERE
-        io.parent_id = ?
-        AND i18n.culture = ?
-        AND io.lft > ?
-      ORDER BY io.lft ASC
-      LIMIT '.self::SIBLINGS;
-
-    $this->treeview['nextSiblings'] = QubitPdo::fetchAll($sql, array(
-      $this->resource->parentId,
-      $this->context->user->getCulture(),
-      $this->resource->lft));
-
-    // Slice prevSiblings and nextSiblings arrays
-    $prevSiblingsCount = count($this->treeview['prevSiblings']);
-    $nextSiblingsCount = count($this->treeview['nextSiblings']);
-
-    // Half part of total of siblings to be shown
-    $half = floor(self::SIBLINGS / 2);
-
-    // Calculate number of items to slice from prevSiblings
-    if ($prevSiblingsCount < self::SIBLINGS)
-    {
-      $prevSiblingsSlice = $prevSiblingsCount - $half;
-
-      if ($prevSiblingsSlice < 0)
-      {
-        $prevSiblingsSlice = 0;
-      }
-    }
-    else if ($prevSiblingsCount == self::SIBLINGS)
-    {
-      if ($nextSiblingsCount < $half)
-      {
-        $prevSiblingsSlice = $nextSiblingsCount;
-      }
-      else
-      {
-        $prevSiblingsSlice = $half;
-      }
-    }
-
-    // Calculate number of items to slice from nextSiblings
-    if ($nextSiblingsCount < self::SIBLINGS)
-    {
-      $nextSiblingsSlice = $nextSiblingsCount - $half;
-
-      if ($nextSiblingsSlice < 0)
-      {
-        $nextSiblingsSlice = 0;
-      }
-    }
-    else if ($nextSiblingsCount == self::SIBLINGS)
-    {
-      if ($prevSiblingsCount < $half)
-      {
-        $nextSiblingsSlice = $prevSiblingsCount;
-      }
-      else
-      {
-        $nextSiblingsSlice = $half;
-      }
-    }
-
-    function array_cut($operation, &$array, $iterate)
-    {
-      while (($iterate--) != false)
-      {
-        call_user_func_array("array_$operation", array(&$array));
-      }
-
-      return $array;
-    }
+    // We don't want to support sorting when sorting by other than lft
+    $this->sortable ='none' == 
sfConfig::get('app_sort_treeview_informationobject') && 
QubitAcl::check($this->resource, 'update');
 
-    array_cut('shift', $this->treeview['prevSiblings'], $prevSiblingsSlice);
-    array_cut('pop', $this->treeview['nextSiblings'], $nextSiblingsSlice);
+    // At this point we don't need to do any ACL check on ancestors
+    $this->ancestors = $this->resource->getAncestors()->orderBy('lft');
 
-    $this->sortable = QubitAcl::check($this->resource, 'update') ? 'true' : 
'false';
+    // Get a list of siblings doing ACL checks
+    $this->prevSiblings = 
array_reverse($this->resource->getTreeViewSiblings(array('limit' => 5, 
'position' => 'previous')));
+    $this->nextSiblings = $this->resource->getTreeViewSiblings(array('limit' 
=> 5, 'position' => 'next'));
   }
 }

Modified: trunk/apps/qubit/modules/informationobject/templates/_treeView.php
==============================================================================
--- trunk/apps/qubit/modules/informationobject/templates/_treeView.php  Mon Aug 
 6 00:01:54 2012        (r12046)
+++ trunk/apps/qubit/modules/informationobject/templates/_treeView.php  Mon Aug 
 6 11:50:42 2012        (r12047)
@@ -1,13 +1,15 @@
-<div id="treeview" data-current-id="<?php echo $resource->id ?>" 
data-sortable="<?php echo $sortable ?>">
+<div id="treeview" data-current-id="<?php echo $resource->id ?>" 
data-sortable="<?php echo $sortable ? 'true' : 'false' ?>">
 
   <ul class="unstyled">
+
     <li class="back" style="<?php echo QubitInformationObject::ROOT_ID == 
$resource->parentId ? 'display: none;' : '' ?>" data-xhr-location="<?php echo 
url_for(array('module' => 'informationobject', 'action' => 'treeView')) ?>">
       <i></i>
       <?php echo link_to(__('Show all'), array('module' => 
'informationobject', 'action' => 'browse')) ?>
     </li>
 
     <?php // Ancestors ?>
-    <?php foreach ($treeview['ancestors'] as $item): ?>
+    <?php foreach ($ancestors as $item): ?>
+      <?php if (QubitInformationObject::ROOT_ID == $item->id) continue; ?>
       <?php echo render_treeview_node(
         $item,
         array('ancestor' => true),
@@ -15,17 +17,17 @@
     <?php endforeach; ?>
 
     <?php // More button ?>
-    <?php if (isset($treeview['prevSiblings']) && 0 < 
count($treeview['prevSiblings'])): ?>
-      <?php if (1 < $treeview['prevSiblings'][0]->lft - $parent->lft): ?>
+    <?php if (isset($prevSiblings) && 0 < count($prevSiblings)): ?>
+      <?php if (1 < $prevSiblings[0]->lft - $parent->lft): ?>
         <?php echo render_treeview_node(
           null,
           array('more' => true),
-          array('xhr-location' => url_for(array('module' => 
'informationobject', 'action' => 'treeView', 'slug' => 
$treeview['prevSiblings'][0]->slug)))); ?>
+          array('xhr-location' => url_for(array('module' => 
'informationobject', 'action' => 'treeView', 'slug' => 
$prevSiblings[0]->slug)))); ?>
       <?php endif; ?>
     <?php endif; ?>
 
     <?php // N prev items ?>
-    <?php foreach ($treeview['prevSiblings'] as $prev): ?>
+    <?php foreach ($prevSiblings as $prev): ?>
       <?php echo render_treeview_node(
         $prev,
         array('expand' => 1 < $prev->rgt - $prev->lft),
@@ -39,7 +41,7 @@
       array('xhr-location' => url_for(array($resource, 'module' => 
'informationobject', 'action' => 'treeView')))); ?>
 
     <?php // N next items ?>
-    <?php foreach ($treeview['nextSiblings'] as $next): ?>
+    <?php foreach ($nextSiblings as $next): ?>
       <?php echo render_treeview_node(
         $next,
         array('expand' => 1 < $next->rgt - $next->lft),

Modified: 
trunk/apps/qubit/modules/informationobject/templates/treeViewSuccess.php
==============================================================================
--- trunk/apps/qubit/modules/informationobject/templates/treeViewSuccess.php    
Mon Aug  6 00:01:54 2012        (r12046)
+++ trunk/apps/qubit/modules/informationobject/templates/treeViewSuccess.php    
Mon Aug  6 11:50:42 2012        (r12047)
@@ -2,14 +2,14 @@
   <?php echo render_treeview_node(
     null,
     array('more' => true),
-    array('xhr-location' => url_for(array('module' => 'informationobject', 
'action' => 'treeView', 'slug' => $items[0]->slug)))); ?>
+    array('xhr-location' => url_for(array($items[0], 'module' => 
'informationobject', 'action' => 'treeView')))); ?>
 <?php endif; ?>
 
 <?php foreach ($items as $item): ?>
   <?php echo render_treeview_node(
     $item,
     array('expand' => 1 < $item->rgt - $item->lft, 'active' => 
$sf_request->resourceId == $item->id),
-    array('xhr-location' => url_for(array('module' => 'informationobject', 
'action' => 'treeView', 'slug' => $item->slug)))); ?>
+    array('xhr-location' => url_for(array($item, 'module' => 
'informationobject', 'action' => 'treeView')))); ?>
 <?php endforeach; ?>
 
 <?php if ('prevSiblings' != $sf_request->show): ?>
@@ -18,6 +18,6 @@
     <?php echo render_treeview_node(
       null,
       array('more' => true),
-      array('xhr-location' => url_for(array('module' => 'informationobject', 
'action' => 'treeView', 'slug' => $item->slug)))); ?>
+      array('xhr-location' => url_for(array($item, 'module' => 
'informationobject', 'action' => 'treeView')))); ?>
   <?php endif; ?>
 <?php endif; ?>

Modified: trunk/lib/model/QubitInformationObject.php
==============================================================================
--- trunk/lib/model/QubitInformationObject.php  Mon Aug  6 00:01:54 2012        
(r12046)
+++ trunk/lib/model/QubitInformationObject.php  Mon Aug  6 11:50:42 2012        
(r12047)
@@ -1627,7 +1627,7 @@
    TreeView
   *****************************************************/
 
-  public function getSiblings(array $options = array())
+  public function getTreeViewSiblings(array $options = array())
   {
     // The max number of items that will be shown
     // The final amount may be smaller if there are no result enough
@@ -1862,6 +1862,7 @@
 
       // In the next iteration we will need the last sibling found
       $current = $last;
+      unset($last);
     }
     while ($limit > count($results));
 

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