Author: sevein
Date: Thu Aug 2 16:20:11 2012
New Revision: 12032
Log:
Add getSiblings() method with sorting, cultureFallback and ACL support. Default
number of results is five.
Modified:
trunk/lib/model/QubitInformationObject.php
Modified: trunk/lib/model/QubitInformationObject.php
==============================================================================
--- trunk/lib/model/QubitInformationObject.php Thu Aug 2 14:03:51 2012
(r12031)
+++ trunk/lib/model/QubitInformationObject.php Thu Aug 2 16:20:11 2012
(r12032)
@@ -1627,6 +1627,279 @@
TreeView
*****************************************************/
+ public function getSiblings(array $options = array())
+ {
+ // The max number of items that will be shown
+ // The final amount may be smaller if there are no result enough
+ $limit = 5;
+ if (isset($options['limit']))
+ {
+ $limit = $options['limit'];
+ }
+
+ // Show 'previous' or 'next' siblings
+ $position = 'next';
+ if (isset($options['position']))
+ {
+ $position = $options['position'];
+ }
+
+ // They way the results are sorted
+ // 'none' (default), 'title' or 'identifierTitle'
+ $sort = sfConfig::get('app_sort_treeview_informationobject', 'title');
+
+ // This is the array of objects that we are buldind
+ $results = array();
+
+ // We are using $current to store the last sibling found in the last
+ // iteration of the following loop
+ $current = $this;
+
+ // Iteration counter
+ $iterationCount = 0;
+
+ // Query the database and discard results by ACL, repeating the same
+ // operation until the number of results requested is fulfilled
+ do
+ {
+ $iterationCount++;
+
+ $criteria = new Criteria;
+ $criteria->add(QubitInformationObject::PARENT_ID, $this->parentId);
+
+ switch ($sort)
+ {
+ case 'identifierTitle':
+
+ $criteria = QubitCultureFallback::addFallbackCriteria($criteria,
'QubitInformationObject');
+
+ if ('next' == $position)
+ {
+ die("TODO");
+ $criteria->addAscendingOrderByColumn('identifier');
+ $criteria->addAscendingOrderByColumn('title');
+ }
+ else // 'previous'
+ {
+ die("TODO");
+ $criteria->addDescendingOrderByColumn('identifier');
+ $criteria->addDescendingOrderByColumn('title');
+ }
+
+ break;
+
+ case 'title':
+
+ $criteria = QubitCultureFallback::addFallbackCriteria($criteria,
'QubitInformationObject');
+
+ if ('next' == $position)
+ {
+ // Add where clause with culture fallback support
+ $criteria->add(
+ 'title',
+ '(CASE
+ WHEN (current.TITLE IS NOT NULL AND current.TITLE <> "")
+ THEN current.TITLE
+ ELSE source.TITLE
+ END) > '.Propel::getConnection()->quote($current->title),
+ Criteria::CUSTOM);
+
+ $criteria->addAscendingOrderByColumn('title');
+ }
+ else // 'previous'
+ {
+ // Add where clause with culture fallback support
+ $criteria->add(
+ 'title',
+ '(CASE
+ WHEN (current.TITLE IS NOT NULL AND current.TITLE <> "")
+ THEN current.TITLE
+ ELSE source.TITLE
+ END) < '.Propel::getConnection()->quote($current->title),
+ Criteria::CUSTOM);
+
+ $criteria->addDescendingOrderByColumn('title');
+ }
+
+ break;
+
+ default:
+
+ if ('next' == $position)
+ {
+ $criteria->add(QubitInformationObject::LFT, $current->lft,
Criteria::GREATER_THAN);
+ $criteria->addAscendingOrderByColumn(QubitInformationObject::LFT);
+ }
+ else // 'previous'
+ {
+ $criteria->add(QubitInformationObject::LFT, $current->lft,
Criteria::LESS_THAN);
+ $criteria->addDescendingOrderByColumn(QubitInformationObject::LFT);
+ }
+ }
+
+ // This is the number of items we were asked for.
+ $criteria->setLimit($limit);
+
+ // Iterate over results and store them in the $results array
+ foreach (QubitInformationObject::get($criteria) as $item)
+ {
+ // We will need this later to control the loop
+ $last = $item;
+
+ // ACL checks
+ if (!QubitAcl::check($item, 'read'))
+ {
+ continue;
+ }
+
+ $results[] = $item;
+ }
+
+ // If $last is not set at this time it means that we could not
+ // find any results and it is worthless to continue
+ if (!isset($last))
+ {
+ break;
+ }
+
+ // Stop the loop if there are no more items to search for
+ // Depending on the sorting mode this is done in different ways
+ switch ($sort)
+ {
+ case 'identifierTitle':
+
+ if ('next' == $position)
+ {
+ // Look for the last sibling
+ if (!isset($conditionLimit))
+ {
+ $criteria = new Criteria;
+ $criteria->add(QubitInformationObject::PARENT_ID,
$this->parentId);
+ $criteria = QubitCultureFallback::addFallbackCriteria($criteria,
'QubitInformationObject');
+ $criteria->addDescendingOrderByColumn('identifier');
+ $criteria->addDescendingOrderByColumn('title');
+ $criteria->setLimit(1);
+
+ if (null === $informationObject =
QubitInformationObject::getOne($criteria))
+ {
+ throw new sfException;
+ }
+
+ $conditionLimit = $informationObject->id;
+ }
+
+ if ($conditionLimit == $last->id)
+ {
+ break 2;
+ }
+ }
+ else // 'previous'
+ {
+ // Look for the first sibling
+ if (!isset($conditionLimit))
+ {
+ $criteria = new Criteria;
+ $criteria->add(QubitInformationObject::PARENT_ID,
$this->parentId);
+ $criteria = QubitCultureFallback::addFallbackCriteria($criteria,
'QubitInformationObject');
+ $criteria->addAscendingOrderByColumn('identifier');
+ $criteria->addAscendingOrderByColumn('title');
+ $criteria->setLimit(1);
+
+ if (null === $informationObject =
QubitInformationObject::getOne($criteria))
+ {
+ throw new sfException;
+ }
+
+ $conditionLimit = $informationObject->id;
+ }
+
+ if ($conditionLimit == $last->id)
+ {
+ break 2;
+ }
+ }
+
+ break;
+
+ case 'title':
+
+ if ('next' == $position)
+ {
+ // Look for the last sibling
+ if (!isset($conditionLimit))
+ {
+ $criteria = new Criteria;
+ $criteria->add(QubitInformationObject::PARENT_ID,
$this->parentId);
+ $criteria = QubitCultureFallback::addFallbackCriteria($criteria,
'QubitInformationObject');
+ $criteria->addDescendingOrderByColumn('title');
+ $criteria->setLimit(1);
+
+ if (null === $informationObject =
QubitInformationObject::getOne($criteria))
+ {
+ throw new sfException;
+ }
+
+ $conditionLimit = $informationObject->id;
+ }
+
+ if ($conditionLimit == $last->id)
+ {
+ break 2;
+ }
+ }
+ else // 'previous'
+ {
+ // Look for the first sibling
+ if (!isset($conditionLimit))
+ {
+ $criteria = new Criteria;
+ $criteria->add(QubitInformationObject::PARENT_ID,
$this->parentId);
+ $criteria = QubitCultureFallback::addFallbackCriteria($criteria,
'QubitInformationObject');
+ $criteria->addAscendingOrderByColumn('title');
+ $criteria->setLimit(1);
+
+ if (null === $informationObject =
QubitInformationObject::getOne($criteria))
+ {
+ throw new sfException;
+ }
+
+ $conditionLimit = $informationObject->id;
+ }
+
+ if ($conditionLimit == $last->id)
+ {
+ break 2;
+ }
+ }
+
+ break;
+
+ default:
+
+ if ('next' == $position)
+ {
+ if (1 == $this->parent->rgt - $last->rgt)
+ {
+ break 2;
+ }
+ }
+ else // 'previous'
+ {
+ if (1 == $last->lft - $this->parent->lft)
+ {
+ break 2;
+ }
+ }
+ }
+
+ // In the next iteration we will need the last sibling found
+ $current = $last;
+ }
+ while ($limit > count($results));
+
+ return $results;
+ }
+
public static function addTreeViewSortCriteria($criteria)
{
switch (sfConfig::get('app_sort_treeview_informationobject'))
--
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.