PageRenderTime 84ms CodeModel.GetById 41ms app.highlight 11ms RepoModel.GetById 29ms app.codeStats 0ms

/www/app/Modules/Users/presenters/UsersUsersPresenter.php

https://github.com/bazo/Mokuji
PHP | 207 lines | 175 code | 22 blank | 10 comment | 9 complexity | 2e65f10280eb8e76ea52ed4bdb0f531a MD5 | raw file
  1<?php
  2/* 
  3 * To change this template, choose Tools | Templates
  4 * and open the template in the editor.
  5 */
  6
  7/**
  8 * Description of AdminUsersPresenter
  9 *
 10 * @author Martin
 11 */
 12class Users_UsersPresenter extends AdminBaseModulePresenter{
 13    
 14    public function createComponentUsersCss($name)
 15    {
 16       $css = parent::createComponentCss($name);
 17       $css->sourcePath = dirname(__FILE__).'/../css' ;
 18       return $css;
 19    }
 20    
 21    public function actionDefault()
 22    {
 23        $this->template->edit = false; 
 24    }
 25    
 26    public function createComponentPage($name)
 27    {
 28        $page = new Page($this,$name);
 29
 30        $item = $page->addItem("Link");
 31        $item->contentFactory = array($this,'createAddNewLink');
 32
 33        $item = $page->addItem("grid");
 34        $item->contentFactory = array($this,'createComponentUsersGrid');
 35        $item->hasSnippets = true;
 36    }
 37
 38    public function createAddNewLink($name, $page)
 39    {
 40        $link = Html::el('a')->href($this->link('newUser!'))->class('ajax')->add(Html::el('div')->id('create-new-link')->add(Html::el('div')->id('icon'))->add(Html::el('span')->add('Create new')));
 41        return $link;
 42    }
 43
 44    public function createComponentUsersGrid($name, $item)
 45    {
 46        $grid = new DataGrid($this, $name);
 47        $model = new UsersModuleModel();
 48        $ds = $model->getDs();
 49        $grid->bindDataTable($ds);
 50        return $grid;
 51    }
 52
 53    public function createComponentFormUser($name)
 54    {
 55        $form = new LiveForm($this, $name);
 56        $form->addText('name', 'Name')->addRule(Form::FILLED);
 57        $form->addText('login', 'Login')->addRule(Form::FILLED);
 58        $form->addText('email', 'E-mail')->addRule(Form::EMAIL);
 59        $form->addButton('btnClose', 'Close');
 60        $form->addSubmit('btnSave', 'Save');
 61        return $form;
 62    }
 63
 64    public function handleNewUser()
 65    {
 66        $this->template->edit = true;
 67        $this->invalidateControl('form'); 
 68    }
 69    
 70    private function gatherActions()
 71    {
 72        $service = Environment::getService('Nette\Loaders\RobotLoader');
 73        $class_list = $service->list;
 74        $actions = array();
 75        foreach($class_list as $class => $file)
 76        {   
 77            //zachtime annotation exception lebo nette si generuje nejake annotation claasy do robotloodera
 78            try{
 79                $r = new ReflectionClass($class);
 80                if($r->isSubclassOf('Admin_SecurePresenter') && $r->getName() != 'BaseModulePresenter')
 81                {
 82                     $methods = $r->getMethods(ReflectionMethod::IS_PUBLIC);
 83                     foreach($methods as $method)
 84                     {
 85                         if(String::lower($method->class) == $class)
 86                         {
 87                             if( strpos($method->getName(), 'action') !== false || strpos($method->getName(), 'handle') !== false)
 88                             {
 89                                 $actions[$class][] = $method->getName();
 90                             }
 91                         }
 92                     }    
 93                }
 94            }
 95            catch(ReflectionException $e) {}
 96        }
 97        $actions = array_merge($actions, Environment::getApplication()->getModulesPermissions());
 98        $model = new UsersModuleModel();
 99        $model->saveActions($actions);
100        return $actions;    
101    }
102    
103    function createComponentConfirmForm()
104        {
105            $form = new ConfirmationDialog();
106            $form->addConfirmer(
107                            'delete', // název signálu bude 'confirmDelete!'
108                            array($this, 'deleteRole'), // callback na funkci při kliku na YES
109                            'Really delete role? User will be left without any privileges!!'  // otázka (může být i callback vracející string)
110                            );
111            return $form;
112    }
113    
114    public function createComponentDatagridRoles($name)
115    {
116        $grid = new DataGrid($this, $name);
117        $model = new UsersModuleModel();
118        $grid->bindDataTable($model->getRolesDs());
119        
120        $grid->keyName = 'id';
121        $grid->addColumn('name', 'Name')->addFilter();
122
123        $grid->addActionColumn('Actions');
124        $grid->addAction('Permissions', 'editPermissions!', Html::el('span')->class('icon icon-edit'), $useAjax = TRUE);
125        $grid->addAction('Delete', 'confirmForm:confirmDelete!', Html::el('span')->class('icon icon-delete'), $useAjax = TRUE);
126        
127        return $grid;
128    }
129    
130    public function createComponentFormPermissionEditor($name)
131    {
132        $model = new UsersModuleModel();
133        $all_actions = $model->getActions();
134        $allowed_actions = $model->getAllowedActions(Environment::getSession('group_id')->value);
135        $form = new AppForm($this, $name);
136        $form->addHidden('group_id')->setValue(Environment::getSession('group_id')->value);
137        $form = $this->addPermissionCheckboxes($form, $all_actions, $allowed_actions);
138        foreach($allowed_actions as $row)
139        {
140            $form['allowed_'.$row->resource_id]->setValue(1);
141        }
142        $form->addGroup('  ')->setOption('container', Html::el('fieldset')->id('buttons'));
143        $form->addButton('btnClose', 'Close');
144        $form->addSubmit('btnSave', 'Save');
145        $form->onSubmit[] = array($this, 'SavePermissions');
146        return $form;
147    }
148    
149    public function savePermissions(AppForm $form)
150    {
151        $values = $form->getValues();
152        unset($values['btnSave']);
153        $group_id = (int)$values['group_id'];
154        unset($values['group_id']);
155        $allowed = array();
156        foreach($values as $cb =>$value)
157        {
158            if($value == true)
159            {
160                $allowed[] = (int)str_replace('allowed_','', $cb);
161            }    
162        }
163        $model = new UsersModuleModel();
164        $model->savePermissions($group_id, $allowed);
165        $this->template->edit = false;
166        $this->invalidateControl('form');
167        $this->flash('Permissions saved');
168    }
169    
170    private function addPermissionCheckboxes(AppForm $form, $all_actions, $allowed_actions)
171    {
172        $actions = array();
173        foreach($all_actions as $row)
174        {
175            $actions[$row->privilege][] = array('resource' => $row->resource, 'id' => $row->id);    
176        }
177
178        foreach($actions as $privilege => $resources)
179        {
180            $privilege = str_replace('_', ':', $privilege);
181            $privilege = str_replace('presenter', '', $privilege);
182            $form->addGroup($privilege, true);
183            foreach ($resources as $resource)
184            {
185                $form->addCheckbox('allowed_'.$resource['id'], $resource['resource']);
186            }
187        }
188        return $form;
189    }
190    
191    public function handleEditPermissions($id)
192    {
193        Environment::getSession('group_id')->value = $id;
194        $this->template->edit = true;
195        $this->invalidateControl('form');
196    }
197    
198    public function actionGroups()
199    {
200        $model = new UsersModuleModel();
201        $roles = $model->getRoles();
202        $actions = $this->gatherActions();
203        $this->template->edit = false;
204        $this->template->actions = $actions;
205    }
206}
207?>