From e0d9b7fbbc2a27d96f7d3b7a13e02d471f958805 Mon Sep 17 00:00:00 2001 From: Andrey Kutejko Date: Sat, 20 Apr 2013 01:55:29 +0300 Subject: [PATCH] orderable template --- ipf/orm/record.php | 17 +++++++---- ipf/orm/template.php | 7 ++--- ipf/orm/template/listener/orderable.php | 38 +++++++++++++++++++++++++ ipf/orm/template/orderable.php | 24 ++++++++++++++++ 4 files changed, 76 insertions(+), 10 deletions(-) create mode 100644 ipf/orm/template/listener/orderable.php create mode 100644 ipf/orm/template/orderable.php diff --git a/ipf/orm/record.php b/ipf/orm/record.php index 402b660..3510d9d 100644 --- a/ipf/orm/record.php +++ b/ipf/orm/record.php @@ -1265,7 +1265,8 @@ abstract class IPF_ORM_Record extends IPF_ORM_Record_Abstract implements Countab 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]; @@ -1291,22 +1292,26 @@ abstract class IPF_ORM_Record extends IPF_ORM_Record_Abstract implements Countab } } - 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 +} + diff --git a/ipf/orm/template.php b/ipf/orm/template.php index 3de6e4d..9bca51f 100644 --- a/ipf/orm/template.php +++ b/ipf/orm/template.php @@ -4,6 +4,7 @@ abstract class IPF_ORM_Template extends IPF_ORM_Record_Abstract { protected $_invoker; protected $_plugin; + public function setTable(IPF_ORM_Table $table) { $this->_table = $table; @@ -27,7 +28,6 @@ abstract class IPF_ORM_Template extends IPF_ORM_Record_Abstract public function addChild(IPF_ORM_Template $template) { $this->_plugin->addChild($template); - return $this; } @@ -48,11 +48,10 @@ abstract class IPF_ORM_Template extends IPF_ORM_Record_Abstract public function setUp() { - } public function setTableDefinition() { - } -} \ No newline at end of file +} + diff --git a/ipf/orm/template/listener/orderable.php b/ipf/orm/template/listener/orderable.php new file mode 100644 index 0000000..8b210e1 --- /dev/null +++ b/ipf/orm/template/listener/orderable.php @@ -0,0 +1,38 @@ +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; + } +} + diff --git a/ipf/orm/template/orderable.php b/ipf/orm/template/orderable.php new file mode 100644 index 0000000..2f95bf7 --- /dev/null +++ b/ipf/orm/template/orderable.php @@ -0,0 +1,24 @@ +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)); + } +} + -- 2.49.0