| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 4 |
* @subpackage core |
| 5 |
* @author E4J s.r.l. |
| 6 |
* @copyright Copyright (C) 2023 E4J s.r.l. All Rights Reserved. |
| 7 |
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 |
* @link https://vikwp.com |
| 9 |
*/ |
| 10 |
|
| 11 |
// No direct access |
| 12 |
defined('ABSPATH') or die('No script kiddies please!'); |
| 13 |
|
| 14 |
/** |
| 15 |
* VikBooking menuactions controller. |
| 16 |
* |
| 17 |
* @since 1.16.0 (J) - 1.6.0 (WP) |
| 18 |
*/ |
| 19 |
class VikBookingControllerMenuactions extends JControllerAdmin |
| 20 |
{ |
| 21 |
/** |
| 22 |
* AJAX endpoint to update the admin menu actions (quick actions). |
| 23 |
* |
| 24 |
* @return void |
| 25 |
*/ |
| 26 |
public function update() |
| 27 |
{ |
| 28 |
$actions = JFactory::getApplication()->input->get('actions', [], 'array'); |
| 29 |
|
| 30 |
if (!$actions) { |
| 31 |
VBOHttpDocument::getInstance()->close(500, 'Invalid data received'); |
| 32 |
} |
| 33 |
|
| 34 |
// parse new actions |
| 35 |
foreach ($actions as &$menu_action) { |
| 36 |
if (!is_array($menu_action) || !isset($menu_action['scope']) || !isset($menu_action['actions'])) { |
| 37 |
unset($menu_action); |
| 38 |
continue; |
| 39 |
} |
| 40 |
|
| 41 |
if (!is_array($menu_action['actions'])) { |
| 42 |
$menu_action['actions'] = []; |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
// unset last reference |
| 47 |
unset($menu_action); |
| 48 |
|
| 49 |
if (!$actions) { |
| 50 |
VBOHttpDocument::getInstance()->close(500, 'No actions to store'); |
| 51 |
} |
| 52 |
|
| 53 |
if (!$config_field_nm = $this->getConfigFieldName()) { |
| 54 |
VBOHttpDocument::getInstance()->close(500, 'No valid user'); |
| 55 |
} |
| 56 |
|
| 57 |
$config = VBOFactory::getConfig(); |
| 58 |
$current_actions = $config->getArray($config_field_nm, []); |
| 59 |
|
| 60 |
if (!$current_actions) { |
| 61 |
// store configuration setting |
| 62 |
$config->set($config_field_nm, $actions); |
| 63 |
|
| 64 |
// terminate with success |
| 65 |
VBOHttpDocument::getInstance()->json($actions); |
| 66 |
} |
| 67 |
|
| 68 |
// merge missing menu scopes, if any |
| 69 |
foreach ($current_actions as $curr_menu_action) { |
| 70 |
if (!is_array($curr_menu_action) || !isset($curr_menu_action['scope']) || !isset($curr_menu_action['actions'])) { |
| 71 |
continue; |
| 72 |
} |
| 73 |
|
| 74 |
if (!is_array($curr_menu_action['actions'])) { |
| 75 |
$curr_menu_action['actions'] = []; |
| 76 |
} |
| 77 |
|
| 78 |
$scope_found = false; |
| 79 |
foreach ($actions as $menu_action) { |
| 80 |
if ($menu_action['scope'] == $curr_menu_action['scope']) { |
| 81 |
// scope found, nothing to set |
| 82 |
$scope_found = true; |
| 83 |
break; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
if (!$scope_found) { |
| 88 |
// append missing menu scope |
| 89 |
$actions[] = $curr_menu_action; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
// update configuration setting |
| 94 |
$config->set($config_field_nm, $actions); |
| 95 |
|
| 96 |
// output result |
| 97 |
VBOHttpDocument::getInstance()->json($actions); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* AJAX endpoint to retrieve the admin menu actions (quick actions). |
| 102 |
* |
| 103 |
* @return void |
| 104 |
*/ |
| 105 |
public function retrieve() |
| 106 |
{ |
| 107 |
if (!$config_field_nm = $this->getConfigFieldName()) { |
| 108 |
VBOHttpDocument::getInstance()->close(500, 'No valid user'); |
| 109 |
} |
| 110 |
|
| 111 |
$config = VBOFactory::getConfig(); |
| 112 |
$current_actions = $config->getArray($config_field_nm, []); |
| 113 |
|
| 114 |
// current origin |
| 115 |
$origin = JUri::root(); |
| 116 |
|
| 117 |
// parse actions |
| 118 |
foreach ($current_actions as &$menu_action) { |
| 119 |
if (!is_array($menu_action) || !isset($menu_action['scope']) || empty($menu_action['actions']) || !is_array($menu_action['actions'])) { |
| 120 |
unset($menu_action); |
| 121 |
continue; |
| 122 |
} |
| 123 |
|
| 124 |
// check for images with a different origin |
| 125 |
foreach ($menu_action['actions'] as &$quick_action) { |
| 126 |
if (!is_array($quick_action) || empty($quick_action['img']) || empty($quick_action['origin'])) { |
| 127 |
continue; |
| 128 |
} |
| 129 |
|
| 130 |
if (preg_match("/^https?:/i", $quick_action['img'])) { |
| 131 |
$quick_action['img'] = str_replace($quick_action['origin'], $origin, $quick_action['img']); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
// unset last reference |
| 136 |
unset($quick_action); |
| 137 |
} |
| 138 |
|
| 139 |
// unset last reference |
| 140 |
unset($menu_action); |
| 141 |
|
| 142 |
// output result |
| 143 |
VBOHttpDocument::getInstance()->json($current_actions); |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Returns the apposite configuration field name for the current user. |
| 148 |
* |
| 149 |
* @return string the configuration field name to fetch. |
| 150 |
*/ |
| 151 |
protected function getConfigFieldName() |
| 152 |
{ |
| 153 |
$admin_user_name = JFactory::getUser()->name; |
| 154 |
|
| 155 |
if (!$admin_user_name) { |
| 156 |
return ''; |
| 157 |
} |
| 158 |
|
| 159 |
return "admin_menu_actions_{$admin_user_name}"; |
| 160 |
} |
| 161 |
} |
| 162 |
|