return $this->header;
}
- public function ListItemsQuery(){
- if (method_exists($this->model,'ordering'))
+ public function ListItemsQuery()
+ {
+ if (method_exists($this->model,'ordering')) {
$ord = $this->model->ordering();
- else
+ } elseif ($this->model->getTable()->hasTemplate('IPF_ORM_Template_Orderable')) {
+ $ord = $this->model->getTable()->getTemplate('IPF_ORM_Template_Orderable')->getColumnName();
+ } else {
$ord = '1 desc';
+ }
$this->q = IPF_ORM_Query::create()->from($this->modelName)->orderby($ord);
}
}
}
- protected function _orderable(){
- return method_exists($this, 'list_order');
+ public function _orderable()
+ {
+ return $this->_orderableColumn() !== null;
+ }
+
+ public function _orderableColumn()
+ {
+ if (method_exists($this, 'list_order'))
+ return $this->list_order();
+ elseif ($this->model->getTable()->hasTemplate('IPF_ORM_Template_Orderable'))
+ return $this->model->getTable()->getTemplate('IPF_ORM_Template_Orderable')->getColumnName();
+ else
+ return null;
}
}
$user_perms = IPF_Auth_App::checkPermissions($request, $app, $m, array('view', 'change'));
$ma = ($user_perms['view'] && $user_perms['change']) ? IPF_Admin_Model::getModelAdmin($m) : null;
- if ($ma !==null && method_exists($ma, 'list_order'))
- {
- $ord_field = $ma->list_order();
+ if ($ma !==null && $ma->_orderable()) {
+ $ord_field = $ma->_orderableColumn();
$ids = explode(',',(string)$request->POST['ids']);
$prev_ids = explode(',',(string)$request->POST['prev_ids']);
return (string) $this->_oid;
}
- public function ModelAdmin(){
+ public function ModelAdmin()
+ {
$cn = get_class($this);
if (isset(IPF_Admin_Model::$models[$cn]))
return IPF_Admin_Model::$models[$cn];
}
}
- public function SetCustom($name, $val){
+ public function SetCustom($name, $val)
+ {
$this->_custom[$name] = $val;
}
- public function GetCustom($name){
+ public function GetCustom($name)
+ {
if (isset($this->_custom[$name]))
return $this->_custom[$name];
return null;
}
- public function _reorder($ids, $ord_field, $drop_id, $prev_ids, $ord=1){
- foreach($ids as $id){
+ public function _reorder($ids, $ord_field, $drop_id, $prev_ids, $ord=1)
+ {
+ foreach($ids as $id) {
$item = $this->getTable()->find($id);
$item[$ord_field] = $ord;
$item->save();
$ord++;
}
}
-}
\ No newline at end of file
+}
+
{
protected $_invoker;
protected $_plugin;
+
public function setTable(IPF_ORM_Table $table)
{
$this->_table = $table;
public function addChild(IPF_ORM_Template $template)
{
$this->_plugin->addChild($template);
-
return $this;
}
public function setUp()
{
-
}
public function setTableDefinition()
{
-
}
-}
\ No newline at end of file
+}
+
--- /dev/null
+<?php
+
+class IPF_ORM_Template_Listener_Orderable extends IPF_ORM_Record_Listener
+{
+ private $columnName = 'ord';
+
+ public function __construct($columnName)
+ {
+ $this->columnName = $columnName;
+ }
+
+ public function preInsert(IPF_ORM_Event $event)
+ {
+ $this->setOrderValue($event->getInvoker());
+ }
+
+ public function preUpdate(IPF_ORM_Event $event)
+ {
+ $this->setOrderValue($event->getInvoker());
+ }
+
+ private function setOrderValue($obj)
+ {
+ $columnName = $this->columnName;
+ if ($obj->$columnName)
+ return;
+
+ $res = IPF_ORM_Query::create()
+ ->select('max('.$this->columnName.') as x_ord')
+ ->from(get_class($obj))
+ ->execute();
+ if (isset($res[0]->x_ord))
+ $obj->$columnName = (int)$res[0]->x_ord + 1;
+ else
+ $obj->$columnName = 1;
+ }
+}
+
--- /dev/null
+<?php
+
+class IPF_ORM_Template_Orderable extends IPF_ORM_Template
+{
+ private $columnName = 'ord';
+
+ public function __construct(array $options=array())
+ {
+ if ($options && array_key_exists('name', $options))
+ $this->columnName = $options['name'];
+ }
+
+ public function getColumnName()
+ {
+ return $this->columnName;
+ }
+
+ public function setTableDefinition()
+ {
+ $this->hasColumn($this->columnName, 'integer', null, '');
+ $this->addListener(new IPF_ORM_Template_Listener_Orderable($this->columnName));
+ }
+}
+