From: Andrey Kutejko Date: Sat, 16 Mar 2019 18:07:19 +0000 (+0100) Subject: remove globals X-Git-Url: https://git.andy128k.dev/?a=commitdiff_plain;h=082c09b4aa02422764ebdc898a9615a4c5617fa5;p=ipf-legacy-orm.git remove globals --- diff --git a/src/adminmodel.php b/src/adminmodel.php index 47c0a2c..1631d66 100644 --- a/src/adminmodel.php +++ b/src/adminmodel.php @@ -104,19 +104,17 @@ class IPF_Admin_Model extends IPF_Admin_Component ? '' : ''; case 'timestamp': - return Text::escape(self::formatDate($value)); + return Text::escape($this->formatDate($value)); default: return Text::escape($value); } } - protected static function formatDate($str, $format='Y-m-d H:i:s', $tz=null) + private function formatDate($str) { - if (!$tz) - $tz = IPF::get('time_zone'); $date = new DateTime($str, new DateTimeZone('UTC')); - $date->setTimeZone(new DateTimeZone($tz)); - return gmdate($format, $date->format('U') + $date->getOffset()); + $date->setTimeZone(new DateTimeZone($this->tz)); + return gmdate('Y-m-d H:i:s', $date->format('U') + $date->getOffset()); } protected function columnTitle($column) @@ -446,53 +444,6 @@ class IPF_Admin_Model_RelationFilter extends IPF_Admin_Model_Filter } } -class IPF_Admin_Model_OwnedFilter extends IPF_Admin_Model_Filter -{ - private $column, $selected = null; - - function __construct($modelName, $column, $title) - { - $this->column = $column; - $this->title = $title; - $this->paramName = 'filter_'.$this->column; - } - - function setParams($params) - { - $this->selected = \PFF\Arr::get($params, $this->paramName); - } - - function applyToQuery($q) - { - // check ??? - if ($this->selected) - $q->addWhere($this->column.' = ?', array($this->selected)); - } - - function getItems() - { - $items = []; - - // reset filter - $items[] = array( - 'id' => null, - 'label' => __('All'), - 'selected' => !$this->selected, - ); - - foreach (\PFF\Container::auth()->userQuery()->fetchAll() as $val) { - $id = $val->id; - $items[] = array( - 'id' => $id, - 'label' => (string)$val, - 'selected' => $this->selected == $id, - ); - } - - return $items; - } -} - class BooleanFilter extends IPF_Admin_Model_Filter { private $column, $trueTitle, $falseTitle, $selected; diff --git a/src/adminmodelinline.php b/src/adminmodelinline.php index 485fede..33ed2bb 100644 --- a/src/adminmodelinline.php +++ b/src/adminmodelinline.php @@ -12,15 +12,6 @@ abstract class IPF_Admin_ModelInline abstract function getModelName(); - public function getApplication() - { - foreach (IPF_Project::getInstance()->appList() as $app) - foreach (IPF_Legacy_ORM_App::appModelList($app) as $model) - if ($model == $this->getModelName()) - return $app; - return null; - } - function getAddNum() { return 4; diff --git a/src/app.php b/src/app.php index 8db81a0..96d9f3e 100644 --- a/src/app.php +++ b/src/app.php @@ -2,6 +2,15 @@ class IPF_Legacy_ORM_App extends IPF_Application { + private $apps; + private $project_root; + + public function configure(\Pimple\Container $container, IPF_Settings $settings) + { + $this->apps = $container['apps']; + $this->project_root = $container['settings']->get('project_root'); + } + private static $appModels = array(); public static function appModelList($app) @@ -9,7 +18,7 @@ class IPF_Legacy_ORM_App extends IPF_Application if (!array_key_exists($app->getName(), self::$appModels)) { $models = array(); try { - $it = new DirectoryIterator($app->getPath().DIRECTORY_SEPARATOR.'models'); + $it = new DirectoryIterator($app->getPath() . DIRECTORY_SEPARATOR . 'models'); foreach ($it as $file) { $e = explode('.', $file->getFileName(), 2); if (count($e) == 2 && $e[1] === 'php') { @@ -28,11 +37,10 @@ class IPF_Legacy_ORM_App extends IPF_Application public function commands() { return array( - new IPF_Legacy_ORM_Command_BuildModels, - new IPF_Legacy_ORM_Command_Sql, - new IPF_Legacy_ORM_Command_SyncDB, - new IPF_Legacy_ORM_Command_Fixtures, + new IPF_Legacy_ORM_Command_BuildModels($this->apps), + new IPF_Legacy_ORM_Command_Sql($this->apps), + new IPF_Legacy_ORM_Command_SyncDB($this->apps), + new IPF_Legacy_ORM_Command_Fixtures($this->apps, $this->project_root . DIRECTORY_SEPARATOR . 'project'), ); } } - diff --git a/src/commands/buildmodels.php b/src/commands/buildmodels.php index 9dca874..12ce6b7 100644 --- a/src/commands/buildmodels.php +++ b/src/commands/buildmodels.php @@ -5,17 +5,21 @@ class IPF_Legacy_ORM_Command_BuildModels public $command = 'buildmodels'; public $description = 'Build all model classes'; + private $apps; + + function __construct($apps) + { + $this->apps = $apps; + } + public function run($args=null) { print "Build all model classes\n\n"; - $project = IPF_Project::getInstance(); - $paths = array(); - foreach ($project->appList() as $app) + foreach ($this->apps as $app) $paths[] = $app->getPath(); IPF_ORM::generateModelsFromYaml($paths, array()); } } - diff --git a/src/commands/fixtures.php b/src/commands/fixtures.php index 746d22f..16633a9 100644 --- a/src/commands/fixtures.php +++ b/src/commands/fixtures.php @@ -5,14 +5,21 @@ class IPF_Legacy_ORM_Command_Fixtures public $command = 'fixtures'; public $description = 'Load fixtures into database'; + private $apps; + private $project_root; + + function __construct(array $apps, $project_root) + { + $this->apps = $apps; + $this->project_root = $project_root; + } + public function run($args=null) { print "Load project fixtures to database\n"; - $project = IPF_Project::getInstance(); - - $paths = array(IPF::get('project_root').DIRECTORY_SEPARATOR.'project'); - foreach ($project->appList() as $app) + $paths = array($this->project_root); + foreach ($this->apps as $app) $paths[] = $app->getPath(); $fixtures = array(); @@ -65,4 +72,3 @@ class IPF_Legacy_ORM_Command_Fixtures } } } - diff --git a/src/commands/sql.php b/src/commands/sql.php index 31f55c9..b5fe603 100644 --- a/src/commands/sql.php +++ b/src/commands/sql.php @@ -5,12 +5,18 @@ class IPF_Legacy_ORM_Command_Sql public $command = 'sql'; public $description = 'Show all SQL DDL from model classes'; + private $apps; + + function __construct(array $apps) + { + $this->apps = $apps; + } + public function run($args=null) { print "Show all SQL DDL from model classes\n"; - foreach (IPF_Project::getInstance()->appList() as $app) + foreach ($this->apps as $app) print IPF_ORM::generateSqlFromModels($app)."\n"; } } - diff --git a/src/commands/syncdb.php b/src/commands/syncdb.php index 02b550f..c2a2225 100644 --- a/src/commands/syncdb.php +++ b/src/commands/syncdb.php @@ -5,17 +5,21 @@ class IPF_Legacy_ORM_Command_SyncDB public $command = 'syncdb'; public $description = 'Create tables from model classes'; + private $apps; + + function __construct(array $apps) + { + $this->apps = $apps; + } + public function run($args=null) { print "Create tables from model classes\n"; - $project = IPF_Project::getInstance(); - $models = array(); - foreach ($project->appList() as $app) + foreach ($this->apps as $app) $models = array_merge($models, IPF_Legacy_ORM_App::appModelList($app)); IPF_ORM::createTablesFromModels($models); } } - diff --git a/src/modelform.php b/src/modelform.php index 01563df..7f3c0bc 100644 --- a/src/modelform.php +++ b/src/modelform.php @@ -138,14 +138,14 @@ class IPF_Form_Model extends IPF_Form $form_field = new IPF_Form_Field_Float($params); break; case 'date': - $format = IPF::get('date_format'); + $format = @$extra['date_format']; if ($format) $params['widget_attrs'] = array('format' => $format); $form_field = new IPF_Form_Field_Date($params); break; case 'datetime': case 'timestamp': - $format = IPF::get('datetime_format'); + $format = @$extra['datetime_format']; if ($format) $params['widget_attrs'] = array('format' => $format); $form_field = new IPF_Form_Field_Datetime($params); diff --git a/src/orm/template/listener/owned.php b/src/orm/template/listener/owned.php deleted file mode 100644 index 7c99b69..0000000 --- a/src/orm/template/listener/owned.php +++ /dev/null @@ -1,34 +0,0 @@ -columnName = $columnName; - } - - public function preInsert(IPF_ORM_Event $event) - { - $this->setOwner($event->getInvoker()); - } - - public function preUpdate(IPF_ORM_Event $event) - { - $this->setOwner($event->getInvoker()); - } - - private function setOwner($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/src/orm/template/owned.php b/src/orm/template/owned.php deleted file mode 100644 index 8134201..0000000 --- a/src/orm/template/owned.php +++ /dev/null @@ -1,49 +0,0 @@ -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(IPF_ORM_Table $table) - { - $table->setColumn($this->columnName, 'integer', null, array( - 'exclude' => $this->exclude, - 'verbose' => $this->verbose, - )); - - $fks = $table->getOption('foreignKeys', array()); - $fks[] = array( - 'local' => $this->columnName, - 'foreign' => 'id', - 'foreignTable' => 'auth_users', - 'onUpdate' => null, - 'onDelete' => 'CASCADE', - ); - $table->setOption('foreignKeys', $fks); - - $table->listeners['Owned_'.$this->columnName] = new IPF_ORM_Template_Listener_Owned($this->columnName); - } -} -