Guest User

Untitled

a guest
Jul 19th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. /**
  5. * Administrační komponenta pro manipulaci a definici s obsahem tabulky.
  6. * @property-read TableModel
  7. */
  8. abstract class AdminControl extends BaseControl
  9. {
  10.  
  11. ...
  12.  
  13. /**
  14. * Component factory
  15. * @see Nette/ComponentContainer#createComponent()
  16. */
  17. protected function createComponent($name)
  18. {
  19. if (preg_match('~(.*)Form~', $name)) {
  20. $this->createComponentForm($name);
  21. return;
  22. }
  23.  
  24. parent::createComponent($name);
  25. }
  26.  
  27.  
  28. /**
  29. * Component factory.
  30. * @param string
  31. * @return void
  32. */
  33. abstract protected function createComponentForm($name);
  34.  
  35. /**
  36. * Form submit handler.
  37. * @param AppForm $form
  38. * @return void
  39. */
  40. public function formSubmitHandler(AppForm $form)
  41. {
  42. // was submitted?
  43. if ($form->isSubmitted() && $form->isValid()) {
  44. $values = new MyDibiRow($form->getValues());
  45.  
  46. preg_match('~(?<action>.*)Form~', $form->getName(), $matches);
  47. $ucname = ucfirst($matches['action']);
  48. $method = 'handle' . $ucname;
  49.  
  50. if ($ucname === $matches['action'] || !method_exists($this, $method) || $this->getReflection()->getMethod($method)->getName() !== $method) {
  51. throw new InvalidArgumentException("Unknown action '{$matches['action']}'. Method '$method' does not exists.");
  52. }
  53.  
  54. if (isset($values[$this->getModel()->primary])) {
  55. $key = $values[$this->getModel()->primary];
  56. unset($values[$this->getModel()->primary]);
  57.  
  58. } else {
  59. $key = NULL;
  60. }
  61.  
  62. try {
  63. $this->$method($key, $values); // tryes to call handle<Action>($key, $values) method
  64. }
  65. catch (BadRequestException $e) {
  66. $this->presenter->flashMessage($e->getMessage(), 'error');
  67. }
  68. catch (Exception $e) {
  69. $this->presenter->flashMessage("Operace se nezdařila, zkuste ji provést znovu později.", 'error');
  70. Debug::processException($e);
  71. }
  72.  
  73. $this->signal = NULL;
  74. $this->invalidateControl();
  75. $this['grid']->invalidateControl();
  76. }
  77. if (!$this->isAjax()) $this->redirect('this');
  78. }
  79.  
  80.  
  81.  
  82. protected function handleAdd($key, $values)
  83. {
  84. $this->getModel()->insert($values);
  85. $this->presenter->flashMessage('Úspěšně přidáno', 'success');
  86. }
  87.  
  88.  
  89. protected function handleEdit($key, $values)
  90. {
  91. $this->getModel()->update($key, $values);
  92. $this->presenter->flashMessage('Úspěšně upraveno', 'success');
  93. }
  94.  
  95.  
  96. protected function handleDelete($key, $values = NULL)
  97. {
  98. $this->getModel()->delete($key);
  99. $this->presenter->flashMessage('Úspěšně odstraněno', 'success');
  100. }
  101. }
Add Comment
Please, Sign In to add comment