From: Andrey Kutejko Date: Mon, 15 Jul 2013 21:57:07 +0000 (+0300) Subject: owned template X-Git-Tag: 0.5~164 X-Git-Url: https://git.andy128k.dev/?a=commitdiff_plain;h=053902d142603c5750dd9662da533420238568dc;p=ipf.git owned template --- diff --git a/ipf/orm/template/listener/owned.php b/ipf/orm/template/listener/owned.php new file mode 100644 index 0000000..a968232 --- /dev/null +++ b/ipf/orm/template/listener/owned.php @@ -0,0 +1,34 @@ +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; + + $request = IPF_Project::getInstance()->request; + if ($request && !$request->user->isAnonymous()) { + $obj->$columnName = $request->user->id; + } + } +} + diff --git a/ipf/orm/template/owned.php b/ipf/orm/template/owned.php new file mode 100644 index 0000000..40ce263 --- /dev/null +++ b/ipf/orm/template/owned.php @@ -0,0 +1,41 @@ +columnName = $options['column']; + if (array_key_exists('name', $options)) + $this->name = $options['name']; + if (array_key_exists('exclude', $options)) + $this->exclude = $options['exclude']; + if (array_key_exists('verbose', $options)) + $this->verbose = $options['verbose']; + } + } + + public function getColumnName() + { + return $this->columnName; + } + + public function setTableDefinition() + { + $this->hasColumn($this->columnName, 'integer', null, array( + 'exclude' => $this->exclude, + 'notblank' => true, + 'notnull' => true, + 'verbose' => $this->verbose, + )); + $this->hasOne('User as '.$this->name, array('local' => $this->columnName, 'foreign' => 'id', 'onDelete' => 'CASCADE')); + $this->getTable()->listeners['Owned_'.$this->columnName] = new IPF_ORM_Template_Listener_Owned($this->columnName); + } +} + diff --git a/ipf/project.php b/ipf/project.php index 7a48770..c6a4e7a 100644 --- a/ipf/project.php +++ b/ipf/project.php @@ -4,6 +4,7 @@ final class IPF_Project { private $apps = array(); public $router = null; + public $request = null; public $sqlProfiler = null; static private $instance = NULL; @@ -106,8 +107,10 @@ final class IPF_Project $cli->run(); } else { $this->loadAllModels(); - $request = new IPF_HTTP_Request; + + $this->request = new IPF_HTTP_Request; $this->router->dispatch($request); + $this->request = null; } return true;