Skip to content

Commit

Permalink
Create an Order trait.
Browse files Browse the repository at this point in the history
  • Loading branch information
Laurent authored and lchenay committed May 10, 2012
1 parent 91c12a4 commit d113c3d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
38 changes: 38 additions & 0 deletions library/Centurion/Contrib/core/traits/Order/Model/DbTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

class Core_Traits_Order_Model_DbTable extends Centurion_Traits_Model_DbTable_Abstract
{

protected $_requiredColumns = array('order');

public function init()
{
Centurion_Signal::factory('on_select_joinInner')->connect(array($this, 'onJoinInner'), $this->_model->getSelectClass());
}

/**
* add filters to the default select query
* @param $select Zend_Db_Table_Select
* @see Core_Traits_Version_Model_DbTable::onSelect()
*/
public function onJoinInner($signal, $sender, $select, $name)
{
if (!$select instanceof Centurion_Db_Table_Select) {
return;
}

if (is_array($name)) {
$name = current($name);
}

if ($name !== $this->_model->info(Zend_Db_Table_Abstract::NAME)) {
return;
}

if (!Centurion_Db_Table_Abstract::getFiltersStatus()) {
return;
}

$select->order('auth_user.order asc');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

interface Core_Traits_Order_Model_DbTable_Interface
{

}
18 changes: 15 additions & 3 deletions library/Centurion/Controller/CRUD.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ public function init()
)
);

$this->view->sortable = $this->_sortable;
$this->view->sortable = $this->isSortable();

if ($this->_sortable && $this->_defaultOrder == null)
if ($this->isSortable() && $this->_defaultOrder == null)
$this->_defaultOrder = 'order asc';

$this->_formViewScript = sprintf('%s/form.phtml', $this->_request->getControllerName());
Expand Down Expand Up @@ -133,9 +133,21 @@ public function generateList()
return parent::generateList();
}

/**
* @return bool
*/
public function isSortable()
{
if ($this->_getModel() instanceof Core_Traits_Order_Model_DbTable_Interface) {
return true;
}

return $this->_sortable;
}

public function orderAction($rowset = null)
{
if ($this->_sortable) {
if ($this->isSortable()) {
$order = 0;
foreach ($rowset as $row) {
$row->order = $order++;
Expand Down

0 comments on commit d113c3d

Please sign in to comment.