| 1 |
<?php |
| 2 |
|
| 3 |
namespace ResponsiveMenu\Controllers; |
| 4 |
use ResponsiveMenu\View\View as View; |
| 5 |
use ResponsiveMenu\Repositories\OptionRepository as OptionRepository; |
| 6 |
use ResponsiveMenu\Factories\OptionFactory as OptionFactory; |
| 7 |
use ResponsiveMenu\Factories\AdminSaveFactory as SaveFactory; |
| 8 |
use ResponsiveMenu\WPML\WPML as WPML; |
| 9 |
|
| 10 |
class Admin { |
| 11 |
|
| 12 |
public function __construct(OptionRepository $repository, View $view) { |
| 13 |
$this->repository = $repository; |
| 14 |
$this->view = $view; |
| 15 |
} |
| 16 |
|
| 17 |
public function update($default_options) { |
| 18 |
|
| 19 |
$options = array_merge($default_options, $_POST['menu']); |
| 20 |
|
| 21 |
$option_factory = new OptionFactory; |
| 22 |
foreach($options as $key => $val) |
| 23 |
$this->repository->update($option_factory->build($key, $val)); |
| 24 |
|
| 25 |
$options = $this->repository->all(); |
| 26 |
$save_factory = new SaveFactory(); |
| 27 |
$flash['errors'] = $save_factory->build($options); |
| 28 |
$flash['success'] = __('Responsive Menu Options Updated Successfully', 'responsive-menu'); |
| 29 |
|
| 30 |
$wpml = new WPML; |
| 31 |
$wpml->saveFromOptions($options); |
| 32 |
|
| 33 |
$this->view->render('main', ['options' => $options, 'flash' => $flash]); |
| 34 |
|
| 35 |
} |
| 36 |
|
| 37 |
public function reset($default_options) { |
| 38 |
|
| 39 |
$option_factory = new OptionFactory; |
| 40 |
foreach($default_options as $key => $val) |
| 41 |
$this->repository->update($option_factory->build($key, $val)); |
| 42 |
|
| 43 |
$options = $this->repository->all(); |
| 44 |
$save_factory = new SaveFactory(); |
| 45 |
$flash['errors'] = $save_factory->build($options); |
| 46 |
$flash['success'] = __('Responsive Menu Options Reset Successfully', 'responsive-menu'); |
| 47 |
|
| 48 |
$wpml = new WPML; |
| 49 |
$wpml->saveFromOptions($options); |
| 50 |
|
| 51 |
$this->view->render('main', ['options' => $options, 'flash' => $flash]); |
| 52 |
|
| 53 |
} |
| 54 |
|
| 55 |
public function index() { |
| 56 |
$this->view->render('main', ['options' => $this->repository->all()]); |
| 57 |
} |
| 58 |
|
| 59 |
public function import() { |
| 60 |
|
| 61 |
if(!empty($_FILES['responsive_menu_import_file']['tmp_name'])): |
| 62 |
$file = file_get_contents($_FILES['responsive_menu_import_file']['tmp_name']); |
| 63 |
$decoded = json_decode($file); |
| 64 |
|
| 65 |
$option_factory = new OptionFactory; |
| 66 |
foreach($decoded as $key => $val) |
| 67 |
$this->repository->update($option_factory->build($key, $val)); |
| 68 |
|
| 69 |
$options = $this->repository->all(); |
| 70 |
$save_factory = new SaveFactory(); |
| 71 |
$flash['errors'] = $save_factory->build($options); |
| 72 |
$flash['success'] = __('Responsive Menu Options Reset Successfully', 'responsive-menu'); |
| 73 |
else: |
| 74 |
$flash['errors'][] = __('No file selected', 'responsive-menu'); |
| 75 |
$options = $this->repository->all(); |
| 76 |
endif; |
| 77 |
|
| 78 |
$wpml = new WPML; |
| 79 |
$wpml->saveFromOptions($options); |
| 80 |
|
| 81 |
$this->view->render('main', ['options' => $options, 'flash' => $flash]); |
| 82 |
} |
| 83 |
|
| 84 |
public function export() { |
| 85 |
nocache_headers(); |
| 86 |
header( 'Content-Type: application/json; charset=utf-8' ); |
| 87 |
header( 'Content-Disposition: attachment; filename=export.json' ); |
| 88 |
header( "Expires: 0" ); |
| 89 |
$final = []; |
| 90 |
foreach($this->repository->all()->all() as $option) |
| 91 |
$final[$option->getName()] = $option->getValue(); |
| 92 |
echo json_encode($final); |
| 93 |
exit(); |
| 94 |
} |
| 95 |
|
| 96 |
} |
| 97 |
|