Author: david
Date: Tue Sep  1 13:20:25 2009
New Revision: 3140

Log:
Lazy-load only the visible terms in the term treeView by default. Implement 
ajax response for dynamically adding and expanding nodes as required. Fixes 
Issue #869.

Modified:
   trunk/apps/qubit/modules/term/actions/contextMenuComponent.class.php
   trunk/apps/qubit/modules/term/actions/treeViewAction.class.php
   trunk/lib/model/QubitTerm.php

Modified: trunk/apps/qubit/modules/term/actions/contextMenuComponent.class.php
==============================================================================
--- trunk/apps/qubit/modules/term/actions/contextMenuComponent.class.php        
Tue Sep  1 13:07:52 2009        (r3139)
+++ trunk/apps/qubit/modules/term/actions/contextMenuComponent.class.php        
Tue Sep  1 13:20:25 2009        (r3140)
@@ -38,24 +38,22 @@
 
     if (null !== $this->term->id)
     {
+      $lineage  = null;
       $criteria = new Criteria;
 
-      // Show terms in this taxonomy and the root term
-      $criterion1 = $criteria->getNewCriterion(QubitTerm::TAXONOMY_ID, 
$this->term->taxonomyId, Criteria::EQUAL);
-      $criterion2 = $criteria->getNewCriterion(QubitTerm::ID, 
QubitTerm::ROOT_ID, Criteria::EQUAL);
-      $criterion1->addOr($criterion2);
-      $criteria->add($criterion1);
-
-      $criteria->addAscendingOrderByColumn(QubitTerm::LFT);
-
-      // Omit non-preferred terms
-      $criteria->addJoin(QubitTerm::ID, QubitRelation::OBJECT_ID, 
Criteria::LEFT_JOIN);
-      $criterion1 = $criteria->getNewCriterion(QubitRelation::TYPE_ID, 
QubitTerm::TERM_RELATION_EQUIVALENCE_ID, Criteria::NOT_EQUAL);
-      $criterion2 = $criteria->getNewCriterion(QubitRelation::TYPE_ID, null, 
Criteria::ISNULL);
-      $criterion1->addOr($criterion2);
-      $criteria->add($criterion1);
+      $ancestors = $this->term->getAncestors()->andSelf()->orderBy('lft');
+      foreach ($ancestors as $node)
+      {
+        $lineage[] = $node->id;
+      }
+
+      // Add root term (but not it's children) to term tree
+      $this->termTree = array($ancestors->offsetGet(0));
+
+      // Add ancestors and their siblings
+      $this->buildTermTree($ancestors->offsetGet(1), $lineage);
 
-      if (0 < count($this->termTree = QubitTerm::get($criteria)))
+      if (0 < count($this->termTree))
       {
         $showContextMenu = true;
       }
@@ -72,4 +70,29 @@
       return sfView::NONE;
     }
   }
+
+  /**
+   * Recursively build the "family" tree containing only the nodes in the
+   * expanded branches.
+   *
+   * @param $node the current "parent" node
+   * @param $lineage the lineage of the pivot node
+   */
+  protected function buildTermTree($node, $lineage)
+  {
+    $this->termTree[] = $node;
+
+    foreach ($node->getChildren() as $child)
+    {
+      // Recurse
+      if (in_array($child->id, $lineage))
+      {
+        $this->buildTermTree($child, $lineage);
+      }
+      else
+      {
+        $this->termTree[] = $child;
+      }
+    }
+  }
 }

Modified: trunk/apps/qubit/modules/term/actions/treeViewAction.class.php
==============================================================================
--- trunk/apps/qubit/modules/term/actions/treeViewAction.class.php      Tue Sep 
 1 13:07:52 2009        (r3139)
+++ trunk/apps/qubit/modules/term/actions/treeViewAction.class.php      Tue Sep 
 1 13:20:25 2009        (r3140)
@@ -17,25 +17,25 @@
  * along with Qubit Toolkit.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-class InformationObjectTreeViewAction extends sfAction
+class TermTreeViewAction extends sfAction
 {
   public function execute($request)
   {
     // Get data
-    $informationObject = 
QubitInformationObject::getById($this->getRequestParameter('id'));
-    $informationObjects = $informationObject->getChildren()->orderBy('lft');
+    $term = QubitTerm::getById($this->getRequestParameter('id'));
+    $terms = $term->getChildren()->orderBy('lft');
 
     // Objects
     $treeViewObject = array();
     $treeViewObjects = array();
 
-    foreach ($informationObjects as $informationObject)
+    foreach ($terms as $term)
     {
-      $treeViewObject['label'] = (string) 
$informationObject->getLabel(array('truncate' => 50)); // call render_title
-      $treeViewObject['href'] = 
$this->getController()->genUrl('informationobject/show?id='.$informationObject->getId());
-      $treeViewObject['id'] = $informationObject->getId();
-      $treeViewObject['parentId'] = $informationObject->getParentId();
-      $treeViewObject['isLeaf'] = (string) 
count($informationObject->getDescendants()) == 0;
+      $treeViewObject['label'] = (string) $term->getName(array('truncate' => 
50));
+      $treeViewObject['href'] = 
$this->getController()->genUrl('term/show?id='.$term->getId());
+      $treeViewObject['id'] = $term->getId();
+      $treeViewObject['parentId'] = $term->getParentId();
+      $treeViewObject['isLeaf'] = (string) count($term->getDescendants()) == 0;
 
       $treeViewObjects[] = $treeViewObject;
     }

Modified: trunk/lib/model/QubitTerm.php
==============================================================================
--- trunk/lib/model/QubitTerm.php       Tue Sep  1 13:07:52 2009        (r3139)
+++ trunk/lib/model/QubitTerm.php       Tue Sep  1 13:20:25 2009        (r3140)
@@ -825,4 +825,17 @@
     return QubitTerm::get($criteria);
   }
 
+  /**
+   * Get the direct descendents of the current term
+   *
+   * @param array $options optional paramters
+   * @return QubitQuery collection of QubitTerm objects
+   */
+  public function getChildren($options = array())
+  {
+    $criteria = new Criteria;
+    $criteria->add(QubitTerm::PARENT_ID, $this->id, Criteria::EQUAL);
+
+    return QubitTerm::get($criteria, $options);
+  }
 }

--~--~---------~--~----~------------~-------~--~----~
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.ca/group/qubit-commits?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to