]> git.andy128k.dev Git - ipf.git/commitdiff
remove global url helpers
authorAndrey Kutejko <andy128k@gmail.com>
Sat, 16 Mar 2019 17:21:32 +0000 (18:21 +0100)
committerAndrey Kutejko <andy128k@gmail.com>
Sat, 16 Mar 2019 17:21:32 +0000 (18:21 +0100)
ipf/admin/app.php
ipf/admin/component.php
ipf/admin/form/widgets/htmlinput.php
ipf/auth/admin.php
ipf/http/url.php [deleted file]

index 0d08d1f00d141579f9d3e43523fcec26bd12b5e7..ee5c58ad4c7ad3dd48a04aff43920c585260f728 100644 (file)
@@ -108,6 +108,7 @@ class IPF_Admin_App extends IPF_Application
                     $component->app = $app;
                     $component->admin_log = $this->container['admin_log'];
                     $component->auth_app = $this->container['apps']['auth'];
+                    $component->router = $this->container['router'];
                     $components[] = $component;
                 }
             }
@@ -152,7 +153,10 @@ class IPF_Admin_LoginRequired extends IPF_Router_Shortcut
 {
     public function response($container, $request)
     {
-        return new IPF_HTTP_Response_Redirect(IPF_HTTP_URL::urlForView(array('IPF_Admin_User_Controller', 'login')));
+        /** @var IPF_Router $router */
+        $router = $container['router'];
+        $loginPage = $router->reverse(['IPF_Admin_User_Controller', 'login']);
+        return new IPF_HTTP_Response_Redirect($loginPage);
     }
 }
 
index 73677a409b981ad32cbb4574b9f32e5557be6902..72f0c9040f4b08b5c98dfdcba6b59d0cdc3c0149 100644 (file)
@@ -20,6 +20,8 @@ abstract class IPF_Admin_Component
     public $admin_log;
     /** @var IPF_Auth_App */
     public $auth_app;
+    /** @var IPF_Router */
+    public $router;
 
     public function slug()
     {
@@ -113,16 +115,16 @@ abstract class IPF_Admin_Component
         return false;
     }
 
-    protected abstract function _getForm($obj, $data);
+    protected abstract function _getForm($obj, $data, $extra);
 
-    protected function _getEditForm($obj, $data)
+    protected function _getEditForm($obj, $data, $extra)
     {
-        return $this->_getForm($obj, $data);
+        return $this->_getForm($obj, $data, $extra);
     }
 
-    protected function _getAddForm($obj, $data)
+    protected function _getAddForm($obj, $data, $extra)
     {
-        return $this->_getForm($obj, $data);
+        return $this->_getForm($obj, $data, $extra);
     }
 
     public function listTemplate()
@@ -170,15 +172,19 @@ abstract class IPF_Admin_Component
     {
         $url = @$this->request->POST['ipf_referrer'];
         if (!$url)
-            $url = IPF_HTTP_URL::urlForView(array('IPF_Admin_Components_Controller', 'listItems'), array($this->app->slug(), $this->slug()));
+            $url = $this->router->reverse(array('IPF_Admin_Components_Controller', 'listItems'), array($this->app->slug(), $this->slug()));
         return new IPF_HTTP_Response_Redirect($url);
     }
 
     public function addItem()
     {
+        $extra = array(
+            'filebrowser_url' => $this->router->reverse(array('IPF_Admin_FileBrowser_Controller', 'index')),
+        );
+
         $errors = false;
         if ($this->request->method == 'POST') {
-            $form = $this->_getAddForm(null, $this->request->getFormData());
+            $form = $this->_getAddForm(null, $this->request->getFormData(), $extra);
             $this->_setupAddForm($form);
 
             if ($form->isValid()) {
@@ -188,7 +194,7 @@ abstract class IPF_Admin_Component
             }
             $errors = true;
         } else {
-            $form = $this->_getAddForm(null, null);
+            $form = $this->_getAddForm(null, null, $extra);
             $this->_setupAddForm($form);
         }
 
@@ -238,9 +244,13 @@ abstract class IPF_Admin_Component
         if (!$object)
             throw new IPF_HTTP_Error404;
 
+        $extra = array(
+            'filebrowser_url' => $this->router->reverse(array('IPF_Admin_FileBrowser_Controller', 'index')),
+        );
+
         $errors = false;
         if ($this->request->method == 'POST') {
-            $form = $this->_getEditForm($object, $this->request->getFormData());
+            $form = $this->_getEditForm($object, $this->request->getFormData(), $extra);
             $this->_setupEditForm($form);
 
             if ($form->isValid()) {
@@ -250,7 +260,7 @@ abstract class IPF_Admin_Component
             }
             $errors = true;
         } else {
-            $form = $this->_getEditForm($object, null);
+            $form = $this->_getEditForm($object, null, $extra);
             $this->_setupEditForm($form);
         }
 
@@ -391,7 +401,7 @@ abstract class IPF_Admin_Component
             if ($item['id'] !== null) {
                 $params[$paramName] = $item['id'];
             }
-            $url = '?'.IPF_HTTP_URL::generateParams($params, false);
+            $url = '?'.$this->encodeParams($params);
 
             $ul->append(Tag::li()
                 ->toggleClass('selected', $item['selected'])
@@ -404,4 +414,12 @@ abstract class IPF_Admin_Component
 
         return $ul->html();
     }
+
+    private function encodeParams($params)
+    {
+        $params_list = array();
+        foreach ($params as $key => $value)
+            $params_list[] = urlencode($key) . '=' . urlencode($value);
+        return implode('&', $params_list);
+    }
 }
index 7e9a934b46ea6cfe1a851486fc7c9c5505cc1f79..f96ddd39e9ea34569fda3fec2dff2fa19200bd81 100644 (file)
@@ -12,9 +12,7 @@ class IPF_Admin_Form_Widget_HTMLInput extends IPF_Form_Widget
     {
         $this->force_absolute_urls  = \PFF\Arr::pop($attrs, 'force_absolute_urls',  false);
         $this->editor_config        = \PFF\Arr::pop($attrs, 'editor_config',        array());
-
-        $this->filebrowser_url = IPF_HTTP_URL::urlForView(array('IPF_Admin_FileBrowser_Controller', 'index'));
-
+        $this->filebrowser_url      = \PFF\Arr::pop($attrs, 'filebrowser_url');
         parent::__construct($attrs);
     }
 
index 7f35324a84de23556ae96d5ec9358005c83bc782..76db597de48caf40ffcc42b0b1f6779051a58929 100644 (file)
@@ -234,15 +234,14 @@ class AdminUser extends Component
         return $this->authApp()->findUser($id);
     }
 
-    protected function _getForm($user, $data)
+    protected function _getForm($user, $data, $extra)
     {
         $connection = $this->getConnection();
 
-        $extra = array(
-            'auth_app' => $this->authApp(),
-            'permissions_choices' => $this->permissionChoices($connection),
-            'roles_choices' => \PFF\Collection::columnByProperty(Role::all($connection), 'id', 'name'),
-        );
+        $extra['auth_app'] = $this->authApp();
+        $extra['permissions_choices'] = $this->permissionChoices($connection);
+        $extra['roles_choices'] = \PFF\Collection::columnByProperty(Role::all($connection), 'id', 'name');
+
         if ($user) {
             $extra['initial'] = array(
                 'permissions' => \PFF\Arr::pluck($user->permissions($connection), 'id'),
@@ -361,14 +360,12 @@ class AdminRole extends Component
         return IPF_Database::queryOneObject($this->getConnection(), Role::class, 'SELECT * FROM auth_role WHERE id = ?', [$id], []);
     }
 
-    protected function _getForm($role, $data)
+    protected function _getForm($role, $data, $extra)
     {
         $connection = $this->getConnection();
 
-        $extra = array(
-            'auth_app' => $this->authApp(),
-            'permissions_choices' => $this->permissionChoices($connection),
-        );
+        $extra['auth_app'] = $this->authApp();
+        $extra['permissions_choices'] = $this->permissionChoices($connection);
 
         if ($role) {
             $extra['initial'] = array(
diff --git a/ipf/http/url.php b/ipf/http/url.php
deleted file mode 100644 (file)
index 0d8fd5b..0000000
+++ /dev/null
@@ -1,26 +0,0 @@
-<?php
-
-class IPF_HTTP_URL
-{
-    public static function generateParams($params=array(), $encode=true)
-    {
-        $params_list = array();
-        foreach ($params as $key => $value)
-            $params_list[] = urlencode($key).'='.urlencode($value);
-        return implode($encode ? '&amp;' : '&', $params_list);
-    }
-
-    public static function generate($action, $params=array(), $encode=true)
-    {
-        if (count($params) > 0)
-            $action .= '?' . self::generateParams($params, $encode);
-        return $action;
-    }
-
-    public static function urlForView($view, $params=array(), $get_params=array(), $encoded=true)
-    {
-        $action = IPF_Project::getInstance()->router->reverse($view, $params);
-        return self::generate($action, $get_params, $encoded);
-    }
-}
-