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