]> git.andy128k.dev Git - ipf-legacy-orm.git/commitdiff
remove globals
authorAndrey Kutejko <andy128k@gmail.com>
Sat, 16 Mar 2019 18:07:19 +0000 (19:07 +0100)
committerAndrey Kutejko <andy128k@gmail.com>
Sat, 16 Mar 2019 18:32:11 +0000 (19:32 +0100)
src/adminmodel.php
src/adminmodelinline.php
src/app.php
src/commands/buildmodels.php
src/commands/fixtures.php
src/commands/sql.php
src/commands/syncdb.php
src/modelform.php
src/orm/template/listener/owned.php [deleted file]
src/orm/template/owned.php [deleted file]

index 47c0a2c9ce10aa8b8be23bb9df6c290c1a6b7fe6..1631d660d0d5014f786fe02f2ec3368438f5f31f 100644 (file)
@@ -104,19 +104,17 @@ class IPF_Admin_Model extends IPF_Admin_Component
                     ? '<span class="positive">&#x2714;</span>'
                     : '<span class="negative">&#x2718;</span>';
             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;
index 485fede08a07592e742029e00dd1a4fa2c07e27a..33ed2bb9607c6d23eb8ee87d0608788557b6c1a2 100644 (file)
@@ -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;
index 8db81a04101b1e1c17569b56f2c30e54a1201393..96d9f3e32bfdd34fc2b65f77abb6f0ef2f79cac7 100644 (file)
@@ -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'),
         );
     }
 }
-
index 9dca874a3758dec1844ac214d230772596e97a87..12ce6b7d4c97247d0556e25c63896820f3e150fa 100644 (file)
@@ -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());
     }
 }
-
index 746d22ff47e02aba9b65f4929273261dba38c56f..16633a909639b23bc4183d6ef5b19087dc0c887c 100644 (file)
@@ -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
         }
     }
 }
-
index 31f55c9e3f13e0a682bf12de2497cb6c399cad7e..b5fe6030780cf73e8794994df8fe61f2621bb146 100644 (file)
@@ -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";
     }
 }
-
index 02b550faff3f1298db3af1dc753697031dd5f048..c2a222524a92e826afa84b3d5e1b4dc28ffeddf8 100644 (file)
@@ -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);
     }
 }
-
index 01563dffd29f88024e371727d6c6b8a0ac7cc7f3..7f3c0bccfa501973fd3501fc3e7a9fceda0ba383 100644 (file)
@@ -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 (file)
index 7c99b69..0000000
+++ /dev/null
@@ -1,34 +0,0 @@
-<?php
-
-class IPF_ORM_Template_Listener_Owned
-{
-    private $columnName;
-
-    public function __construct($columnName)
-    {
-        $this->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 (file)
index 8134201..0000000
+++ /dev/null
@@ -1,49 +0,0 @@
-<?php
-
-class IPF_ORM_Template_Owned extends IPF_ORM_Template
-{
-    private $name = 'owner';
-    private $columnName = 'owner_id';
-    private $exclude = true;
-    private $verbose = 'owner';
-
-    public function __construct(array $options=array())
-    {
-        if ($options) {
-            if (array_key_exists('column', $options))
-                $this->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);
-    }
-}
-