| 1 |
<?php |
| 2 |
|
| 3 |
namespace ResponsiveMenu\Controllers; |
| 4 |
use ResponsiveMenu\View\View; |
| 5 |
use ResponsiveMenu\Management\OptionManager; |
| 6 |
use ResponsiveMenu\Validation\Validator; |
| 7 |
use ResponsiveMenu\Tasks\UpdateOptionsTask; |
| 8 |
use ResponsiveMenu\Collections\OptionsCollection; |
| 9 |
|
| 10 |
class AdminController { |
| 11 |
|
| 12 |
public function __construct(OptionManager $manager, View $view) { |
| 13 |
$this->manager = $manager; |
| 14 |
$this->view = $view; |
| 15 |
} |
| 16 |
|
| 17 |
public function index($nav_menus, $location_menus) { |
| 18 |
return $this->view->render( |
| 19 |
'admin/main.html.twig', |
| 20 |
[ |
| 21 |
'options' => $this->manager->all(), |
| 22 |
'nav_menus' => $nav_menus, |
| 23 |
'location_menus' => $location_menus |
| 24 |
] |
| 25 |
); |
| 26 |
} |
| 27 |
|
| 28 |
public function rebuild($nav_menus, $location_menus) { |
| 29 |
return $this->view->render( |
| 30 |
'admin/main.html.twig', |
| 31 |
[ |
| 32 |
'options' => $this->manager->all(), |
| 33 |
'nav_menus' => $nav_menus, |
| 34 |
'location_menus' => $location_menus, |
| 35 |
'alert' => ['success' => 'Responsive Menu Database Rebuilt Successfully.'] |
| 36 |
] |
| 37 |
); |
| 38 |
} |
| 39 |
|
| 40 |
public function update($valid_nonce, $new_options, $nav_menus, $location_menus) { |
| 41 |
$validator = new Validator(); |
| 42 |
$errors = []; |
| 43 |
if(!$valid_nonce): |
| 44 |
$alert = ['danger' => 'CSRF token not valid']; |
| 45 |
$options = new OptionsCollection($new_options); |
| 46 |
elseif($validator->validate($new_options)): |
| 47 |
try { |
| 48 |
$options = $this->manager->updateOptions($new_options); |
| 49 |
$task = new UpdateOptionsTask; |
| 50 |
$task->run($options, $this->view); |
| 51 |
$alert = ['success' => 'Responsive Menu Options Updated Successfully.']; |
| 52 |
} catch (\Exception $e) { |
| 53 |
$alert = ['danger' => $e->getMessage()]; |
| 54 |
} |
| 55 |
else: |
| 56 |
$options = new OptionsCollection($new_options); |
| 57 |
$errors = $validator->getErrors(); |
| 58 |
$alert = ['danger' => $errors]; |
| 59 |
endif; |
| 60 |
|
| 61 |
return $this->view->render( |
| 62 |
'admin/main.html.twig', |
| 63 |
[ |
| 64 |
'options' => $options, |
| 65 |
'alert' => $alert, |
| 66 |
'nav_menus' => $nav_menus, |
| 67 |
'location_menus' => $location_menus, |
| 68 |
'errors' => $errors |
| 69 |
] |
| 70 |
); |
| 71 |
} |
| 72 |
|
| 73 |
public function reset($default_options, $nav_menus, $location_menus) { |
| 74 |
try { |
| 75 |
$options = $this->manager->updateOptions($default_options); |
| 76 |
$task = new UpdateOptionsTask; |
| 77 |
$task->run($options, $this->view); |
| 78 |
$alert = ['success' => 'Responsive Menu Options Reset Successfully']; |
| 79 |
} catch(\Exception $e) { |
| 80 |
$alert = ['danger' => $e->getMessage()]; |
| 81 |
} |
| 82 |
return $this->view->render( |
| 83 |
'admin/main.html.twig', |
| 84 |
[ |
| 85 |
'options' => $options, |
| 86 |
'alert' => $alert, |
| 87 |
'nav_menus' => $nav_menus, |
| 88 |
'location_menus' => $location_menus |
| 89 |
] |
| 90 |
); |
| 91 |
} |
| 92 |
|
| 93 |
public function import($imported_options, $nav_menus, $location_menus) { |
| 94 |
$errors = []; |
| 95 |
if(!empty($imported_options)): |
| 96 |
$validator = new Validator(); |
| 97 |
if($validator->validate($imported_options)): |
| 98 |
try { |
| 99 |
unset($imported_options['button_click_trigger']); |
| 100 |
$options = $this->manager->updateOptions($imported_options); |
| 101 |
$task = new UpdateOptionsTask; |
| 102 |
$task->run($options, $this->view); |
| 103 |
$alert = ['success' => 'Responsive Menu Options Imported Successfully.']; |
| 104 |
} catch(\Exception $e) { |
| 105 |
$options = $this->manager->all(); |
| 106 |
$alert = ['danger' => $e->getMessage()]; |
| 107 |
} |
| 108 |
else: |
| 109 |
$options = new OptionsCollection($imported_options); |
| 110 |
$errors = $validator->getErrors(); |
| 111 |
$alert = ['danger' => $errors]; |
| 112 |
endif; |
| 113 |
else: |
| 114 |
$options = $this->manager->all(); |
| 115 |
$alert = ['danger' => 'No import file selected']; |
| 116 |
endif; |
| 117 |
|
| 118 |
return $this->view->render( |
| 119 |
'admin/main.html.twig', |
| 120 |
[ |
| 121 |
'options' => $options, |
| 122 |
'alert' => $alert, |
| 123 |
'nav_menus' => $nav_menus, |
| 124 |
'location_menus' => $location_menus, |
| 125 |
'errors' => $errors |
| 126 |
] |
| 127 |
); |
| 128 |
} |
| 129 |
|
| 130 |
public function export() { |
| 131 |
return json_encode( |
| 132 |
$this->manager->all()->toArray() |
| 133 |
); |
| 134 |
} |
| 135 |
|
| 136 |
} |
| 137 |
|