<commit_before><?php

namespace RolesCapabilities\Event;

use Cake\Event\Event;
use Cake\Event\EventListenerInterface;
use Cake\ORM\TableRegistry;
use Cake\Utility\Inflector;

class AddPersonalPermissionsListener implements EventListenerInterface
{
    protected $allowedActions = [
        'view', 'edit', 'delete'
    ];

    /**
     * Implemented Events
     *
     * @return array
     */
    public function implementedEvents()
    {
        return [
            'CsvMigrations.View.topMenu.beforeRender' => 'addPersonalPermissionsButton',
            'CsvMigrations.View.Body.Bottom' => 'addPersonalPermissionsModal',
        ];
    }

    /**
     *  addPersonalPermissionsButton method
     *
     * @param Cake\Event\Event $event of the current request
     * @param array $menu of the view page.
     * @param array $user currently logged in.
     * @return void
     */
    public function addPersonalPermissionsButton(Event $event, array $menu, array $user)
    {
        $content = $this->_addDropdownButton($event, $menu);
        $content .= $this->_addModalWindow($event);

        $event->result = $content;
    }

    /**
     *  addPersonalPermissionsModal method
     *
     * @param Cake\Event\Event $event of the current request
     * @param array $options of the view page.
     * @return void
     */
    public function addPersonalPermissionsModal(Event $event, array $options)
    {
        $content = $this->_addModalWindow($event);

        $event->result = $content;
    }

    /**
     *  _addButton method
     *
     * @param Cake\Event\Event $event of the current request
     * @return string   code of the button
     */
    protected function _addButton(Event $event)
    {
        return $event->subject()->Html->link(
            __('Permissions'),
            '#',
            [
                'class' => 'btn btn-default',
                'data-toggle' => "modal",
                'data-target' => "#permissions-modal-add"
            ]
        );
    }

    /**
     *  _addDropdownButton method
     *
     * @param Cake\Event\Event $event of the current request
     * @return string   code of button
     */
    protected function _addDropdownButton(Event $event, array $menu)
    {
        $viewUrl = '/roles-capabilities/personal-permissions/index';
        $addUrl = '/roles-capabilities/personal-permissions/addNew';
        if (!empty($menu[0]['url']['controller']) && !empty($menu[0]['url'][0])) {
            $viewUrl .= '?model=' . $menu[0]['url']['controller'] . '&foreign_key=' . $menu[0]['url'][0];
        }

        $button = [];

        $button[] = '<div class="btn-group btn-group-sm">';
        $button[] = '<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">';
        $button[] = '<i class="fa fa-shield"></i>' . __('Permissions') . '<span class="caret"></span>';
        $button[] = '<span class="sr-only">Toggle Dropdown</span>';
        $button[] = '</button>';
        $button[] = '<ul class="dropdown-menu">';
        $button[] = '<li><a href="' . $addUrl . '" data-toggle="modal" data-target="#permissions-modal-add">Add Permission</a></li>';
        $button[] = '<li><a href="' . $viewUrl . '">View Permissions</a></li>';
        $button[] = '</ul>';
        $button[] = '</div>';

        return implode("\n", $button);
    }

    /**
     *  _addModalWindow method
     *
     * @param Cake\Event\Event $event of the current request
     * @return string   code of modal window
     */
    protected function _addModalWindow(Event $event)
    {
        $controllerName = $event->subject()->request->params['controller'];
        $postContent = [];

        $usersTable = TableRegistry::get('users');
        $users = $usersTable->find('list')
            ->where([
                'active' => 1
            ])
            ->limit(100)
            ->toArray();
        $users[''] = '';
        asort($users);

        $actions[''] = '';
        foreach ($this->allowedActions as $action) {
            $actions[$action] = Inflector::humanize($action);
        }

        $postContent[] = '<div class="modal fade" id="permissions-modal-add" tabindex="-1" role="dialog" aria-labelledby="mySetsLabel">';
        $postContent[] = $event->subject()->Form->create('RolesCapabilities.PersonalPermissions', ['url' => '/roles-capabilities/personal-permissions/add', 'id' => 'modal-form-permissions-add']);
        $postContent[] = '<div class="modal-dialog" role="document">';
        $postContent[] = '<div class="modal-content">';
        $postContent[] = '<div class="modal-header">';
        $postContent[] = '<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>';
        $postContent[] = '<h4 class="modal-title" id="mySetsLabel">' . __('Add Permissions') . '</h4>';
        $postContent[] = '</div>'; // modal-header
        $postContent[] = '<div class="modal-body">';
        $postContent[] = '<div class="sets-feedback-container"></div>';
        $postContent[] = $event->subject()->Form->hidden('foreign_key', ['value' => $event->subject()->request->params['pass'][0]]);
        $postContent[] = $event->subject()->Form->hidden('model', ['value' => $event->subject()->request->params['controller']]);
        $postContent[] = '<div class="row"><div class="col-xs-12 col-md-12">';
        $postContent[] = $event->subject()->Form->label(__('User'));
        $postContent[] = $event->subject()->Form->select('user_id', $users, ['class' => 'select2', 'multiple' => false, 'required' => true]);
        $postContent[] = '</div></div>';

        $postContent[] = '<div class="row"><div class="col-xs-12 col-md-12">';
        $postContent[] = $event->subject()->Form->label(__('Permission'));
        $postContent[] = $event->subject()->Form->select('type', $actions, ['class' => 'select2', 'multiple' => false, 'required' => true]);
        $postContent[] = '</div></div>';
        $postContent[] = '<div class="row"><div class="col-xs-12 col-md-12">';
        $postContent[] = $event->subject()->Form->input('is_active', ['type' => 'checkbox']);
        $postContent[] = '</div></div>';

        $postContent[] = '</div>'; //modal-body
        $postContent[] = '<div class="modal-footer">';
        $postContent[] = $event->subject()->Form->button(__('Submit'), ['name' => 'btn_operation', 'value' => 'submit', 'class' => 'btn btn-primary']);
        $postContent[] = '<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>';
        $postContent[] = '</div>';
        $postContent[] = '</div>'; // modal-content
        $postContent[] = '</div>'; //modal-dialog
        $postContent[] = $event->subject()->Form->end();
        $postContent[] = '</div>'; // modal

        return implode("\n", $postContent);
    }

    /**
     *  _addJSHandler method
     *
     * @param Cake\Event\Event $event of the current request
     * @return string   JS code for perosnal permissions
     */
    protected function _addJSHandler(Event $event)
    {
        return $event->subject()->Html->script(['RolesCapabilities.personal_permissions'], ['block' => 'scriptBottom']);
    }
}
<commit_msg>Added list existing personal permissions into add new permission modal window (#task 3636)<commit_after><?php

namespace RolesCapabilities\Event;

use Cake\Event\Event;
use Cake\Event\EventListenerInterface;
use Cake\ORM\TableRegistry;
use Cake\Utility\Inflector;

class AddPersonalPermissionsListener implements EventListenerInterface
{
    protected $allowedActions = [
        'view', 'edit', 'delete'
    ];

    /**
     * Implemented Events
     *
     * @return array
     */
    public function implementedEvents()
    {
        return [
            'CsvMigrations.View.topMenu.beforeRender' => 'addPersonalPermissionsButton',
            //'CsvMigrations.View.Body.Bottom' => 'addPersonalPermissionsModal',
        ];
    }

    /**
     *  addPersonalPermissionsButton method
     *
     * @param Cake\Event\Event $event of the current request
     * @param array $menu of the view page.
     * @param array $user currently logged in.
     * @return void
     */
    public function addPersonalPermissionsButton(Event $event, array $menu, array $user)
    {
        $content = $this->_addDropdownButton($event, $menu);
        $content .= $this->_addModalWindow($event, $menu);
        $content .= $this->_addModalWindowView($event, $menu);

        $event->result = $content;
    }

    /**
     *  addPersonalPermissionsModal method
     *
     * @param Cake\Event\Event $event of the current request
     * @param array $options of the view page.
     * @return void
     */
    public function addPersonalPermissionsModal(Event $event, array $options)
    {
        $content = $this->_addModalWindow($event);

        $event->result = $content;
    }

    /**
     *  _addButton method
     *
     * @param Cake\Event\Event $event of the current request
     * @return string   code of the button
     */
    protected function _addButton(Event $event)
    {
        return $event->subject()->Html->link(
            __('Permissions'),
            '#',
            [
                'class' => 'btn btn-default',
                'data-toggle' => "modal",
                'data-target' => "#permissions-modal-add"
            ]
        );
    }

    /**
     *  _addDropdownButton method
     *
     * @param Cake\Event\Event $event of the current request
     * @return string   code of button
     */
    protected function _addDropdownButton(Event $event, array $menu)
    {
        $viewUrl = '/roles-capabilities/personal-permissions/indexModal';
        $addUrl = '/roles-capabilities/personal-permissions/addNew';
        if (!empty($menu[0]['url']['controller']) && !empty($menu[0]['url'][0])) {
            $viewUrl .= '?model=' . $menu[0]['url']['controller'] . '&foreign_key=' . $menu[0]['url'][0];
        }

        $button = [];

        $button[] = '<div class="btn-group btn-group-sm">';
        $button[] = '<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">';
        $button[] = '<i class="fa fa-shield"></i>' . __('Permissions') . '<span class="caret"></span>';
        $button[] = '<span class="sr-only">Toggle Dropdown</span>';
        $button[] = '</button>';
        $button[] = '<ul class="dropdown-menu">';
        $button[] = '<li><a href="' . $addUrl . '" data-toggle="modal" data-target="#permissions-modal-add">Add Permission</a></li>';
        $button[] = '<li><a href="' . $viewUrl . '" data-toggle="modal" data-target="#permissions-modal-view">View Permissions</a></li>';
        $button[] = '</ul>';
        $button[] = '</div>';

        return implode("\n", $button);
    }

    /**
     *  _addModalWindow method
     *
     * @param Cake\Event\Event $event of the current request
     * @return string   code of modal window
     */
    protected function _addModalWindow(Event $event, array $menu)
    {
        $controllerName = $event->subject()->request->params['controller'];
        $postContent = [];

        $usersTable = TableRegistry::get('users');
        $users = $usersTable->find('list')
            ->where([
                'active' => 1
            ])
            ->limit(100)
            ->toArray();
        $users[''] = '';
        asort($users);

        $actions[''] = '';
        foreach ($this->allowedActions as $action) {
            $actions[$action] = Inflector::humanize($action);
        }

        $permissions = [];
        if (!empty($menu[0]['url']['controller']) && !empty($menu[0]['url'][0])) {
            $conditions = [];
            $conditions['model'] = $menu[0]['url']['controller'];
            $conditions['foreign_key'] = $menu[0]['url'][0];

            $permissionTable = TableRegistry::get('RolesCapabilities.PersonalPermissions');
            $query = $permissionTable->find('all', [
                'conditions' => $conditions,
                'contain' => ['Users'],
                'limit' => 100,
            ]);
            $permissions = $query->all();
        }

        $postContent[] = '<div class="modal fade" id="permissions-modal-add" tabindex="-1" role="dialog" aria-labelledby="mySetsLabel">';
        $postContent[] = '<div class="modal-dialog" role="document">';
        $postContent[] = '<div class="modal-content">';
        $postContent[] = '<div class="modal-header">';
        $postContent[] = '<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>';
        $postContent[] = '<h4 class="modal-title" id="mySetsLabel">' . __('Add Permissions') . '</h4>';
        $postContent[] = '</div>'; // modal-header
        $postContent[] = '<div class="modal-body">';
        $postContent[] = $event->subject()->Form->create('RolesCapabilities.PersonalPermissions', ['url' => '/roles-capabilities/personal-permissions/add', 'id' => 'modal-form-permissions-add']);
        $postContent[] = '<div class="sets-feedback-container"></div>';
        $postContent[] = $event->subject()->Form->hidden('foreign_key', ['value' => $event->subject()->request->params['pass'][0]]);
        $postContent[] = $event->subject()->Form->hidden('model', ['value' => $event->subject()->request->params['controller']]);
        $postContent[] = '<div class="row"><div class="col-xs-12 col-md-12">';
        $postContent[] = $event->subject()->Form->label(__('User'));
        $postContent[] = $event->subject()->Form->select('user_id', $users, ['class' => 'select2', 'multiple' => false, 'required' => true]);
        $postContent[] = '</div></div>';

        $postContent[] = '<div class="row"><div class="col-xs-12 col-md-12">';
        $postContent[] = $event->subject()->Form->label(__('Permission'));
        $postContent[] = $event->subject()->Form->select('type', $actions, ['class' => 'select2', 'multiple' => false, 'required' => true]);
        $postContent[] = '</div></div>';

        $postContent[] = '<div class="sets-feedback-container"></div>';

        foreach ($permissions as $permission) {
            $postContent[] = '<div class="row">';
            $postContent[] = '<div class="col-xs-6">' . $permission->user->username . '</div>';
            $postContent[] = '<div class="col-xs-6">' . $permission->type . '</div>';
            $postContent[] = '</div>';
        }

        $postContent[] = '</div>'; //modal-body
        $postContent[] = '<div class="modal-footer">';
        $postContent[] = $event->subject()->Form->button(__('Submit'), ['name' => 'btn_operation', 'value' => 'submit', 'class' => 'btn btn-primary']);
        $postContent[] = '<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>';
        $postContent[] = $event->subject()->Form->end();
        $postContent[] = '</div>';
        $postContent[] = '</div>'; // modal-content
        $postContent[] = '</div>'; //modal-dialog
        $postContent[] = '</div>'; // modal

        return implode("\n", $postContent);
    }

    /**
     * _addModalWindowView method
     *
     * @param Cake\Event\Event $event of the current request
     * @return string   code of modal window
     */
    protected function _addModalWindowView(Event $event, array $menu)
    {
        $permissions = [];

        if (!empty($menu[0]['url']['controller']) && !empty($menu[0]['url'][0])) {
            $conditions = [];
            $conditions['model'] = $menu[0]['url']['controller'];
            $conditions['foreign_key'] = $menu[0]['url'][0];

            $permissionTable = TableRegistry::get('RolesCapabilities.PersonalPermissions');
            $query = $permissionTable->find('all', [
                'conditions' => $conditions,
                'contain' => ['Users'],
                'limit' => 100,
            ]);
            $permissions = $query->all();
        }

        $postContent[] = '<div class="modal fade" id="permissions-modal-view" tabindex="-1" role="dialog" aria-labelledby="mySetsLabel">';
        $postContent[] = '<div class="modal-dialog" role="document">';
        $postContent[] = '<div class="modal-content">';
        $postContent[] = '<div class="modal-header">';
        $postContent[] = '<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>';
        $postContent[] = '<h4 class="modal-title" id="mySetsLabel">' . __('Add Permissions') . '</h4>';
        $postContent[] = '</div>'; // modal-header
        $postContent[] = '<div class="modal-body">';
        $postContent[] = $event->subject()->Form->create('RolesCapabilities.PersonalPermissions', ['url' => '/roles-capabilities/personal-permissions/delete', 'id' => 'modal-form-permissions-view']);
        $postContent[] = '<div class="sets-feedback-container"></div>';

        foreach ($permissions as $permission) {
            $postContent[] = $permission->user->username;
        }

        $postContent[] = '</div>'; //modal-body
        $postContent[] = '<div class="modal-footer">';
        $postContent[] = '<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>';
        $postContent[] = $event->subject()->Form->end();
        $postContent[] = '</div>';
        $postContent[] = '</div>'; // modal-content
        $postContent[] = '</div>'; //modal-dialog
        $postContent[] = '</div>'; // modal

        return implode("\n", $postContent);
    }

    /**
     *  _addJSHandler method
     *
     * @param Cake\Event\Event $event of the current request
     * @return string   JS code for perosnal permissions
     */
    protected function _addJSHandler(Event $event)
    {
        return $event->subject()->Html->script(['RolesCapabilities.personal_permissions'], ['block' => 'scriptBottom']);
    }
}

{'fraction_non_alphanumeric': 0.19842378849700967, 'fraction_numerical': 0.004415627969370075, 'mean_word_length': 2.7540914813260597, 'pattern_counts': {'":': 0, '<': 139, '<?xml version=': 0, '>': 309, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7310911', 'n_tokens_mistral': 5198, 'n_tokens_neox': 5095, 'n_words': 1000}