]> git.andy128k.dev Git - ipf.git/commitdiff
admin multiplechoice widget
authorAndrey Kutejko <andy128k@gmail.com>
Mon, 16 Apr 2018 07:45:27 +0000 (09:45 +0200)
committerAndrey Kutejko <andy128k@gmail.com>
Mon, 16 Apr 2018 07:45:27 +0000 (09:45 +0200)
ipf/admin/assets/js/admin.js
ipf/admin/form/fields/multiplechoice.php [new file with mode: 0644]
ipf/admin/form/widgets/selectmultipleinputcheckbox.php [new file with mode: 0644]
ipf/auth/admin.php

index 94432e70bb3a935447c308ee3bde3d13fc3680db..7afafd5b2b4c549d6e06e7fdb01817d593e5b1c9 100644 (file)
@@ -51,13 +51,14 @@ $(function(){
         });
     }
 
-    $('.checkgroup').closest('ul').each(function(){
-        var master = $(this),
-            tools = $('<ul class="object-tools"><li><a href="#" class="checkall">Check All</a></li><li><a href="#">Uncheck All</a></li></ul>');
-        master.before(tools).addClass('checkgroup_master');
-        tools.find('a').click(function(){
-            var check = $(this).hasClass('checkall');
-            master.find('input').prop('checked', check);
+    $('.admincheckgroup').each(function(){
+        var group = $(this);
+        group.find('.object-tools .checkall').click(function(){
+            group.find('.checkgroup input').prop('checked', true);
+            return false;
+        });
+        group.find('.object-tools .uncheckall').click(function(){
+            group.find('.checkgroup input').prop('checked', false);
             return false;
         });
     });
diff --git a/ipf/admin/form/fields/multiplechoice.php b/ipf/admin/form/fields/multiplechoice.php
new file mode 100644 (file)
index 0000000..0b3cdd5
--- /dev/null
@@ -0,0 +1,6 @@
+<?php
+
+class IPF_Admin_Form_Field_MultipleChoice extends IPF_Form_Field_MultipleChoice
+{
+    public $widget = 'IPF_Admin_Form_Widget_SelectMultipleInputCheckbox';
+}
diff --git a/ipf/admin/form/widgets/selectmultipleinputcheckbox.php b/ipf/admin/form/widgets/selectmultipleinputcheckbox.php
new file mode 100644 (file)
index 0000000..7bb53b3
--- /dev/null
@@ -0,0 +1,49 @@
+<?php
+
+use \PFF\HtmlBuilder\Tag as Tag;
+
+class IPF_Admin_Form_Widget_SelectMultipleInputCheckbox extends IPF_Form_Widget_SelectMultipleInput
+{
+    public function render($name, $value, $extra_attrs=array())
+    {
+        if ($value === null || $value == '')
+            $value = array();
+
+        $wrapper = Tag::div()
+            ->attr('class', 'admincheckgroup');
+
+        $tools = Tag::ul(['class' => 'object-tools'],
+            Tag::li(null,
+                Tag::a(['href' => '#', 'class' => 'checkall'], 'Check All')
+            ),
+            Tag::li(null,
+                Tag::a(['href' => '#', 'class' => 'uncheckall'], 'Uncheck All')
+            )
+        );
+        $wrapper->append($tools);
+
+        $checkgroup = Tag::ul()
+            ->attrs($this->attrs)
+            ->attrs($extra_attrs)
+            ->attr('class', 'checkgroup checkgroup_master');
+        foreach ($this->choices as $option_label => $option_value) {
+            $checkgroup->append(Tag::li(null,
+                Tag::label(null,
+                    Tag::input()
+                        ->attr('type', 'checkbox')
+                        ->attr('name', "{$name}[]")
+                        ->attr('value', $option_value)
+                        ->toggleAttr('checked', 'checked', in_array($option_value, $value)),
+                    ' ',
+                    $option_label)));
+        }
+        $wrapper->append($checkgroup);
+
+        return $wrapper->html();
+    }
+
+    public function idForLabel($id)
+    {
+        return null;
+    }
+}
index c8ad689937b2e689723eebe9f89ecb2869d576b8..f60fd0b77c627eb9c2098af3a9a4272e025310b5 100644 (file)
@@ -70,19 +70,15 @@ class UserForm extends \IPF_ObjectForm
             $permissions[] = 'permissions';
             $permissions[] = 'roles';
 
-            $this->fields['permissions'] = new \IPF_Form_Field_MultipleChoice(array(
+            $this->fields['permissions'] = new \IPF_Admin_Form_Field_MultipleChoice(array(
                 'label'     => __('Permissions'),
                 'choices'   => $permissions_choices,
-                'widget'    => 'IPF_Form_Widget_SelectMultipleInputCheckbox',
-                'widget_attrs' => array('class' => 'checkgroup'),
             ));
 
-            $this->fields['roles'] = new \IPF_Form_Field_MultipleChoice(array(
+            $this->fields['roles'] = new \IPF_Admin_Form_Field_MultipleChoice(array(
                 'label'     => __('Groups'),
                 'help_text' => __('In addition to the permissions manually assigned, this user will also get all permissions granted to each group he/she is in.'),
                 'choices'   => array_column(Role::all(), 'id', 'name'),
-                'widget'    => 'IPF_Form_Widget_SelectMultipleInputCheckbox',
-                'widget_attrs' => array('class' => 'checkgroup'),
             ));
         }
 
@@ -297,11 +293,9 @@ class RoleForm extends \IPF_ObjectForm
         ));
 
         if ($auth_app->arePermissionsEnabled()) {
-            $this->fields['permissions'] = new \IPF_Form_Field_MultipleChoice(array(
+            $this->fields['permissions'] = new \IPF_Admin_Form_Field_MultipleChoice(array(
                 'label'     => __('Permissions'),
                 'choices'   => $permissions_choices,
-                'widget'    => 'IPF_Form_Widget_SelectMultipleInputCheckbox',
-                'widget_attrs' => array('class' => 'checkgroup'),
             ));
         }
     }