PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/www/app/AdminModule/presenters/AdminCategoriesPresenter.php

https://github.com/bazo/Mokuji
PHP | 131 lines | 114 code | 13 blank | 4 comment | 2 complexity | d9c6b5d2a199cfbc8ba3e4e3daabf11c MD5 | raw file
Possible License(s): BSD-3-Clause, MIT
  1. <?php
  2. class Admin_CategoriesPresenter extends Admin_SecurePresenter
  3. {
  4. protected $item;
  5. public function actionDefault()
  6. {
  7. $this->view = 'categories';
  8. $this->template->edit = false;
  9. $this->template->link = $this->createAddNewLink();
  10. }
  11. public function createAddNewLink()
  12. {
  13. $link = Html::el('a')->href($this->link('newCategory!'))->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. //GRID
  17. public function createComponentCategoriesGrid($name)
  18. {
  19. $grid = new DataGrid($this, $name);
  20. $ds = $this->model('categories')->getDs();
  21. $grid->bindDataTable($ds);
  22. $grid->keyName = 'id';
  23. $grid->addColumn('title', 'Title')->addFilter();
  24. $grid->addActionColumn('Actions');
  25. $grid->addAction('Edit', 'editCategory!', Html::el('span')->class('icon icon-edit'), $useAjax = TRUE);
  26. $grid->addAction('Delete', 'confirmForm:confirmDelete!', Html::el('span')->class('icon icon-delete'), $useAjax = TRUE);
  27. return $grid;
  28. }
  29. function createComponentConfirmForm()
  30. {
  31. $form = new ConfirmationDialog();
  32. $form->addConfirmer(
  33. 'delete', // název signálu bude 'confirmDelete!'
  34. array($this, 'deleteCategory'), // callback na funkci při kliku na YES
  35. 'Really delete category?' // otázka (může být i callback vracející string)
  36. );
  37. return $form;
  38. }
  39. public function deleteCategory($params, $dialog)
  40. {
  41. $id = $params;
  42. try{
  43. $this->model('categories')->delete($id);
  44. $this->flash('Category deleted!');
  45. if(!$this->isAjax()) $this->redirect('Categories:');
  46. else
  47. {
  48. $this['page']->refresh('categories');
  49. }
  50. }
  51. catch(DibiDriverException $e)
  52. {
  53. $this->flash($e->getMessage());
  54. }
  55. }
  56. //END GRID
  57. //Form New Category
  58. public function createComponentFormCategory($name)
  59. {
  60. $form = new LiveForm($this, $name);
  61. $form->addText('title','Title')->addRule(Form::FILLED, 'Fill title');
  62. try{
  63. $categories = $this->model('categories')->getIdLevel();
  64. }
  65. catch(DibiDriverException $e)
  66. {
  67. $this->flash($e->getMessage());
  68. $categories = array();
  69. }
  70. $categories = array('0;0' => 'none') + $categories;
  71. $form->addSelect('parent', 'Parent', $categories);
  72. $form->addTextarea('description', 'Description');
  73. $templates = array('category' => 'default');
  74. $form->addSelect('template', 'template', $templates);
  75. $form->addButton('btnClose', 'Close');
  76. $form->addSubmit('btnSave', 'Save')->onClick[] =array($this, 'formNewCategorySubmitted');
  77. return $form;
  78. }
  79. public function formNewCategorySubmitted($button)
  80. {
  81. $form = $button->getForm();
  82. $values = $form->getValues();
  83. unset($values['btnClose']);
  84. try
  85. {
  86. $this->model('categories')->save($values);
  87. $this->flash('Category '.$values['title'].' created');
  88. if(!$this->isAjax())$this->redirect('Categories:');
  89. else
  90. {
  91. $this->invalidateControl('grid');
  92. $this->invalidateControl('form');
  93. }
  94. }
  95. catch(DibiDriverException $e)
  96. {
  97. $this->flash($e->getMessage());
  98. }
  99. }
  100. //END Form New Category
  101. public function formClose(Button $button)
  102. {
  103. $this->invalidateControl('form');
  104. }
  105. public function handleNewCategory()
  106. {
  107. $this->template->edit = true;
  108. $this->invalidateControl('form');
  109. $this->flashCmd('openForm');
  110. }
  111. public function handleEditCategory()
  112. {
  113. $this->template->edit = true;
  114. $this->invalidateControl('form');
  115. $this->flashCmd('openForm');
  116. }
  117. }
  118. ?>