PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/www/app/AdminModule/presenters/AdminMenusPresenter.php

https://github.com/bazo/Mokuji
PHP | 164 lines | 141 code | 20 blank | 3 comment | 0 complexity | be1168f308ecc11e3041fa4c013f3a6f MD5 | raw file
Possible License(s): BSD-3-Clause, MIT
  1. <?php
  2. class Admin_MenusPresenter extends Admin_SecurePresenter
  3. {
  4. private $new = false, $edit =false;
  5. public function actionDefault()
  6. {
  7. $this->template->new = $this->new;
  8. $this->template->edit = $this->edit;
  9. $this->template->link = $this->createAddNewLink();
  10. }
  11. public function createAddNewLink()
  12. {
  13. $link = Html::el('a')->href($this->link('newMenu!'))->class('ajax')->add(Html::el('div')->id('create-new-link')->add(Html::el('div')->id('icon'))->add(Html::el('span')->add('Create new')));
  14. return $link;
  15. }
  16. public function createComponentFormNewMenu($name)
  17. {
  18. $form = new LiveForm($this, $name);
  19. $form->addText('name', 'Name')->addRule(Form::FILLED, 'Please enter a name.');
  20. $templates = $this->model('Menu')->getTemplates();
  21. $form->addSelect('template', 'Template', $templates);
  22. $form->addButton('btnClose','Close');
  23. $form->addSubmit('save','Save')->onClick[] = array($this, 'formNewMenuSubmitted');
  24. $form['save']->getControlPrototype()->class('ajax');
  25. return $form;
  26. }
  27. public function createComponentFormEditMenu($name)
  28. {
  29. $menu_id = $this->getParam('id');
  30. $templates = $this->model('Menu')->getTemplates();
  31. $data = $this->model('Menu')->getById($menu_id);
  32. $form = new LiveForm($this, $name);
  33. $form->addText('name', 'Name')->addRule(Form::FILLED, 'Please enter a name.')->setValue($data->name);
  34. $form->addSelect('template', 'Template', $templates);
  35. $form->addHidden('id')->setValue($menu_id);
  36. $form->setDefaults($data);
  37. $form->addButton('btnClose','Close');
  38. $form->addSubmit('save','Save')->onClick[] = array($this, 'formEditMenuSubmitted');
  39. return $form;
  40. }
  41. public function formNewMenuSubmitted($button)
  42. {
  43. $form = $button->getForm();
  44. $values = $form->getValues();
  45. $name = $values['name'];
  46. unset($values['btnClose']);
  47. try
  48. {
  49. $this->model('menu')->createMenu($values);
  50. $this->flash('Menu '.$name.' created successfully');
  51. $this->invalidateControl('form');
  52. $this->invalidateControl('grid');
  53. }
  54. catch (DibiDriverException $e)
  55. {
  56. $this->flash($e->getMessage());
  57. }
  58. }
  59. public function formEditMenuSubmitted($button)
  60. {
  61. $form = $button->getForm();
  62. $values = $form->getValues();
  63. $name = $values['name'];
  64. unset($values['btnClose']);
  65. try
  66. {
  67. $this->model('menu')->update($values);
  68. $this->flash('Menu '.$name.' updated successfully');
  69. $this->invalidateControl('form');
  70. //$this->redirect('MenuItems:Default', array('name' => $name));
  71. $this->invalidateControl('grid');
  72. }
  73. catch (DibiDriverException $e)
  74. {
  75. $this->flash($e->getMessage());
  76. }
  77. }
  78. public function formMenuClose($button)
  79. {
  80. $this->invalidateControl('form');
  81. }
  82. public function createComponentGrid($name)
  83. {
  84. $grid = new DataGrid($this, $name);
  85. $ds = $this->model('menu')->getDs();
  86. $grid->bindDataTable($ds);
  87. $grid->keyName = 'id';
  88. $grid->addColumn('name', 'Name')->addTextFilter();
  89. $grid['name']->formatCallback[] = array($this, 'createNameLink');
  90. // přidáme sloupec pro akce
  91. $grid->addActionColumn('Actions');
  92. // a naplníme datagrid akcemi pomocí továrničky
  93. $grid->addAction('Items', 'MenuItems:default', Html::el('span')->class('icon icon-items'), $useAjax = true);
  94. $grid->addAction('Edit', 'editMenu!', Html::el('span')->class('icon icon-edit'), $useAjax = true);
  95. $grid->addAction('Delete', 'confirmForm:confirmDelete!', Html::el('span')->class('icon icon-delete'), $useAjax = true);
  96. return $grid;
  97. }
  98. public function createNameLink($value, DibiRow $data)
  99. {
  100. return Html::el('a',$value)->class('ajax')->href($this->link('MenuItems:default', array('id' => $data['id'])));
  101. }
  102. function createComponentConfirmForm()
  103. {
  104. $form = new ConfirmationDialog();
  105. $form->addConfirmer(
  106. 'delete', // název signálu bude 'confirmDelete!'
  107. array($this, 'deleteMenu'), // callback na funkci při kliku na YES
  108. array($this, 'questionDelete') // otázka (může být i callback vracející string)
  109. );
  110. return $form;
  111. }
  112. public function questionDelete($dialog, $params)
  113. {
  114. return 'Really delete menu '.$params['name'].'?';
  115. }
  116. public function deleteMenu($params, $dialog)
  117. {
  118. $name = $params;
  119. try{
  120. $menu_id = $this->model('menu')->getByName($name)->id;
  121. $this->model('menu')->deleteById($menu_id);
  122. $this->flash('Menu '.$name.' deleted!');
  123. $this->invalidateControl('grid');
  124. }
  125. catch(DibiDriverException $e)
  126. {
  127. $this->flash($e->getMessage());
  128. }
  129. }
  130. public function handleNewMenu()
  131. {
  132. $this->template->new = true;
  133. $this->invalidateControl('form');
  134. $this->flashCmd('openForm');
  135. }
  136. public function handleEditMenu($name)
  137. {
  138. $this->template->menu = $name;
  139. $this->template->edit = true;
  140. $this->invalidateControl('form');
  141. $this->flashCmd('openForm');
  142. }
  143. }
  144. ?>