From 39419189aa1087920f950b4fc0c02ee1089e8b1f Mon Sep 17 00:00:00 2001 From: Andrey Kutejko Date: Tue, 7 May 2013 23:13:08 +0300 Subject: [PATCH] move admin js to bottom --- ipf/admin/app.php | 63 ++++++++++------- ipf/admin/model.php | 8 ++- ipf/admin/templates/admin/change.html | 5 +- ipf/admin/templates/admin/changepassword.html | 40 ++++++----- ipf/admin/templates/admin/login.html | 35 ++++++---- ipf/admin/views.php | 33 +++++---- ipf/auth/models/User.php | 17 ++--- ipf/form.php | 68 +++++++++---------- 8 files changed, 151 insertions(+), 118 deletions(-) diff --git a/ipf/admin/app.php b/ipf/admin/app.php index d74e91a..4e4d786 100644 --- a/ipf/admin/app.php +++ b/ipf/admin/app.php @@ -1,16 +1,20 @@ array('AdminLog') )); } - public static function urls(){ + + public static function urls() + { return array( array('regex'=>'fb_rename/$#', 'func'=>'IPF_Admin_Views_FileBrowserRename'), - array('regex'=>'filebrowser(.+)#', 'func'=>'IPF_Admin_Views_FileBrowser'), - array('regex'=>'$#', 'func'=>'IPF_Admin_Views_Index'), + array('regex'=>'filebrowser(.+)#', 'func'=>'IPF_Admin_Views_FileBrowser'), + array('regex'=>'$#', 'func'=>'IPF_Admin_Views_Index'), array('regex'=>'([\w\_\-]+)/([\w\_\-]+)/$#i', 'func'=>'IPF_Admin_Views_ListItems'), array('regex'=>'([\w\_\-]+)/([\w\_\-]+)/reorder/$#i', 'func'=>'IPF_Admin_Views_Reorder'), array('regex'=>'([\w\_\-]+)/([\w\_\-]+)/add/$#i', 'func'=>'IPF_Admin_Views_AddItem'), @@ -22,33 +26,30 @@ class IPF_Admin_App extends IPF_Application{ ); } - static function checkAdminAuth($request){ - $ok = true; - if ($request->user->isAnonymous()) - $ok = false; - elseif ( (!$request->user->is_staff) && (!$request->user->is_superuser) ) - $ok = false; + static function checkAdminAuth($request) + { + $ok = true; + if ($request->user->isAnonymous()) + $ok = false; + elseif ( (!$request->user->is_staff) && (!$request->user->is_superuser) ) + $ok = false; - if ($ok) - return true; - else - return new IPF_HTTP_Response_Redirect(IPF_HTTP_URL_urlForView('IPF_Admin_Views_Login')); - } + if ($ok) + return true; + else + return new IPF_HTTP_Response_Redirect(IPF_HTTP_URL_urlForView('IPF_Admin_Views_Login')); + } static function GetAppModelFromSlugs($lapp, $lmodel) { - foreach (IPF_Project::getInstance()->appList() as $app) - { - if ($app->getSlug() == $lapp) - { - foreach($app->modelList() as $m) - { + foreach (IPF_Project::getInstance()->appList() as $app) { + if ($app->getSlug() == $lapp) { + foreach($app->modelList() as $m) { if (strtolower($m) == $lmodel) return array('app' => $app, 'modelname' => $m); } } } - return null; } @@ -72,12 +73,24 @@ class IPF_Admin_App extends IPF_Application{ $perms = array(); - foreach (IPF_Auth_App::checkPermissions($request, $app, $m, $adminPerms) as $permName=>$permValue) - { + foreach (IPF_Auth_App::checkPermissions($request, $app, $m, $adminPerms) as $permName=>$permValue) { if ($permValue) $perms[] = $permName; } return $perms; } + + public static function renderForm($form) + { + return $form->htmlOutput( + '
%2$s %1$s%3$s%4$s
', + '
%s
', + '', + '

%s

', + true, + '
%s
', + false); + } } + diff --git a/ipf/admin/model.php b/ipf/admin/model.php index d0d49c7..43f60ad 100644 --- a/ipf/admin/model.php +++ b/ipf/admin/model.php @@ -559,7 +559,9 @@ class IPF_Admin_Model 'mode'=>'add', 'page_title'=>$this->titleAdd(), 'classname'=>$this->verbose_name(), - 'form'=>$form, + 'form' => $form, + 'form_html' => IPF_Admin_App::renderForm($form), + 'extra_js' => $form->extra_js(), 'inlineInstances'=>$this->inlineInstances, 'lapp'=>$lapp, 'perms'=>$perms, @@ -656,7 +658,9 @@ class IPF_Admin_Model 'page_title'=>$this->titleEdit(), 'classname'=>$this->verbose_name(), 'object'=>$o, - 'form'=>$form, + 'form' => $form, + 'form_html' => IPF_Admin_App::renderForm($form), + 'extra_js' => $form->extra_js(), 'inlineInstances'=>$this->inlineInstances, 'lapp'=>$lapp, 'perms'=>$perms, diff --git a/ipf/admin/templates/admin/change.html b/ipf/admin/templates/admin/change.html index c4a230c..58e107f 100644 --- a/ipf/admin/templates/admin/change.html +++ b/ipf/admin/templates/admin/change.html @@ -17,7 +17,7 @@ {/if}
{block form} - {$form.render_admin()} + {$form_html} {/block}
{if $inlineInstances} @@ -89,5 +89,8 @@ $(document).ready(function(){ }); {/literal} +{foreach $extra_js as $js} +{$js|safe} +{/foreach} {/block} diff --git a/ipf/admin/templates/admin/changepassword.html b/ipf/admin/templates/admin/changepassword.html index 7719838..c9fd71c 100644 --- a/ipf/admin/templates/admin/changepassword.html +++ b/ipf/admin/templates/admin/changepassword.html @@ -5,26 +5,30 @@ {block breadcrumbs}{/block} {block content} -
-

{$page_title}

-
-
-
- {if $form.errors} -

Please correct the error below.

- {/if} -
- {$form.render_admin()} -
-
- - -
-
-
-
+

{$page_title}

+
+
+
+ {if $form.errors} +

Please correct the error below.

+ {/if} +
+ {$form_html} +
+
+ + +
+
+
+
+{/block} +{block scripts} +{foreach $extra_js as $js} +{$js|safe} +{/foreach} {/block} diff --git a/ipf/admin/templates/admin/login.html b/ipf/admin/templates/admin/login.html index 55e98fa..c347baf 100644 --- a/ipf/admin/templates/admin/login.html +++ b/ipf/admin/templates/admin/login.html @@ -5,18 +5,25 @@ {block usertools}{/block} {block content} -
-
-
- {if $form.message}
  • {$form.message}
{/if} - {$form.render_admin()} -
- -
-
-
-
- +
+
+
+ {if $form.message}
  • {$form.message}
{/if} + {$form_html} +
+ +
+
+
+
{/block} + +{block scripts} +{foreach $extra_js as $js} +{$js|safe} +{/foreach} + +{/block} + diff --git a/ipf/admin/views.php b/ipf/admin/views.php index 2d64b99..4afe2da 100644 --- a/ipf/admin/views.php +++ b/ipf/admin/views.php @@ -259,7 +259,8 @@ function IPF_Admin_Views_ChangePassword($request, $match) 'page_title'=>'Change Password: '.$user->username, 'classname'=>'User', 'object'=>$user, - 'form'=>$form, + 'form' => $form, + 'form_html' => IPF_Admin_App::renderForm($form), 'lapp'=>$lapp, 'lmodel'=>$lmodel, 'admin_title' => IPF::get('admin_title'), @@ -273,7 +274,8 @@ function IPF_Admin_Views_ChangePassword($request, $match) return new IPF_HTTP_Response_NotFound(); } -function IPF_Admin_Views_Login($request, $match){ +function IPF_Admin_Views_Login($request, $match) +{ $success_url = ''; if (!empty($request->REQUEST['next'])) $success_url = $request->REQUEST['next']; @@ -282,7 +284,7 @@ function IPF_Admin_Views_Login($request, $match){ if ($request->method == 'POST') { $form = new IPF_Auth_Forms_Login($request->POST); - if ($form->isValid()){ + if ($form->isValid()) { $users = new User(); if (false === ($user = $users->checkCreditentials($form->cleaned_data['username'], $form->cleaned_data['password']))) { $form->message = __('The login or the password is not valid. The login and the password are case sensitive.'); @@ -291,19 +293,21 @@ function IPF_Admin_Views_Login($request, $match){ return new IPF_HTTP_Response_Redirect($success_url); } } - } - else + } else { $form = new IPF_Auth_Forms_Login(array('next'=>$success_url)); + } $context = array( - 'page_title' => IPF::get('admin_title'), - 'form' => $form, - 'admin_title' => IPF::get('admin_title'), - 'indexpage_url'=>IPF::get('indexpage_url','/'), + 'page_title' => IPF::get('admin_title'), + 'form' => $form, + 'form_html' => IPF_Admin_App::renderForm($form), + 'admin_title' => IPF::get('admin_title'), + 'indexpage_url'=>IPF::get('indexpage_url','/'), ); return IPF_Shortcuts::RenderToResponse('admin/login.html', $context, $request); } -function IPF_Admin_Views_Logout($request, $match){ +function IPF_Admin_Views_Logout($request, $match) +{ IPF_Auth_App::logout($request); $context = array( 'page_title' => IPF::get('admin_title'), @@ -313,14 +317,15 @@ function IPF_Admin_Views_Logout($request, $match){ return IPF_Shortcuts::RenderToResponse('admin/logout.html', $context, $request); } -function cmp($a, $b){ - if ($a['name'] == $b['name']) { +function cmp($a, $b) +{ + if ($a['name'] == $b['name']) return 0; - } return ($a['name'] < $b['name']) ? -1 : 1; } -function dir_recursive($dir, $path=DIRECTORY_SEPARATOR, $level=''){ +function dir_recursive($dir, $path=DIRECTORY_SEPARATOR, $level='') +{ $dirtree = array(); if ($level=='') $dirtree[] = array('path'=>'', 'name'=>'Root Folder'); diff --git a/ipf/auth/models/User.php b/ipf/auth/models/User.php index 2edb48a..a09afc4 100644 --- a/ipf/auth/models/User.php +++ b/ipf/auth/models/User.php @@ -45,24 +45,25 @@ class IPFAuthAdminUserForm extends IPF_Form_Extra_CheckGroup $account = array('username', 'password'); } - - if (IPF_Auth_App::ArePermissionsEnabled()) - { + + $permissions = array('is_active', 'is_staff', 'is_superuser'); + if (IPF_Auth_App::ArePermissionsEnabled()) { + $permissions[] = 'Permissions'; + $permissions[] = 'Roles'; + $this->fields['Roles']->label = 'Groups'; $this->fields['Roles']->help_text = 'In addition to the permissions manually assigned, this user will also get all permissions granted to each group he/she is in.'; parent::SetupForm($this); - } - else - { + } else { unset($this->fields['Permissions']); unset($this->fields['Roles']); } - + $this->field_groups = array( array('fields' => $account), array('fields' => array('email', 'first_name', 'last_name'), 'label' => 'Personal info'), - array('fields' => array('is_active', 'is_staff', 'is_superuser','Permissions','Roles'), 'label' => 'Permissions'), + array('fields' => $permissions, 'label' => 'Permissions'), ); } diff --git a/ipf/form.php b/ipf/form.php index c490ca9..3d4e93d 100644 --- a/ipf/form.php +++ b/ipf/form.php @@ -122,41 +122,32 @@ class IPF_Form implements Iterator return (isset($this->errors['__all__'])) ? $this->errors['__all__'] : array(); } - protected function htmlOutput($normal_row, $error_row, $row_ender, - $help_text_html, $errors_on_separate_row, $group_title=null) + public function htmlOutput($normal_row, $error_row, $row_ender, + $help_text_html, $errors_on_separate_row, $group_title=null, + $extra_js=true) { $top_errors = (isset($this->errors['__all__'])) ? $this->errors['__all__'] : array(); array_walk($top_errors, 'IPF_Form_htmlspecialcharsArray'); $output = array(); $hidden_fields = array(); - - $groups = array(); - - if (count($this->field_groups)) - { - foreach ($this->field_groups as $field_group) - { - if (array_key_exists('fields', $field_group) && is_array($field_group['fields'])) - { - $_fields = array(); - - foreach ($field_group['fields'] as $field_name) - { - if (array_key_exists($field_name, $this->fields)) - $_fields[$field_name] = $this->fields[$field_name]; - } - - if (count($_fields)) - { - $_group = array('fields'=>$_fields); - - if (array_key_exists('label', $field_group)) - $_group['label'] = $field_group['label']; - - $groups[] = $_group; - } - } - } + + foreach ($this->field_groups as $field_group) { + if (!$field_group['fields']) + throw new IPF_Exception('Empty field group.'); + foreach ($field_group['fields'] as $field_name) + if (!array_key_exists($field_name, $this->fields)) + throw new IPF_Exception('Unknown field "' . $field_name . '".'); + } + + $groups = array(); + foreach ($this->field_groups as $field_group) { + $_fields = array(); + foreach ($field_group['fields'] as $field_name) + $_fields[$field_name] = $this->fields[$field_name]; + $groups[] = array( + 'fields' => $_fields, + 'label' => @$field_group['label'], + ); } if (count($groups)) { @@ -167,7 +158,7 @@ class IPF_Form implements Iterator } foreach ($groups as $group) { - if ($render_group_title && array_key_exists('label', $group)) + if ($render_group_title && isset($group['label'])) $output[] = sprintf($group_title, $group['label']); foreach ($group['fields'] as $name=>$field) { @@ -236,15 +227,20 @@ class IPF_Form implements Iterator } } - $extra_js = array(); - foreach ($groups as $group) - foreach ($group['fields'] as $name => $field) - $extra_js = array_merge($extra_js, $field->widget->extra_js()); - $output[] = implode("\n", array_unique($extra_js)); + if ($extra_js) + $output = array_merge($output, $this->extra_js()); return new IPF_Template_SafeString($this->before_render . implode("\n", $output) . $this->after_render, true); } + public function extra_js() + { + $extra_js = array(); + foreach ($this->fields as $name => $field) + $extra_js = array_merge($extra_js, $field->widget->extra_js()); + return array_unique($extra_js); + } + public function render_p() { return $this->htmlOutput('

%1$s%2$s %3$s%4$s

', '%s', '

', ' %s', true); -- 2.49.0