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));
+ }
+}
+