admin.php
149 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikAppointments |
| 4 | * @subpackage core |
| 5 | * @author E4J s.r.l. |
| 6 | * @copyright Copyright (C) 2021 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 | jimport('joomla.application.component.controlleradmin'); |
| 15 | |
| 16 | /** |
| 17 | * Extends the JControllerAdmin methods. |
| 18 | * |
| 19 | * @since 1.7 |
| 20 | */ |
| 21 | class VAPControllerAdmin extends JControllerAdmin |
| 22 | { |
| 23 | /** |
| 24 | * Method to save the submitted ordering values for records via AJAX. |
| 25 | * |
| 26 | * @return void |
| 27 | */ |
| 28 | public function saveOrderAjax() |
| 29 | { |
| 30 | $input = JFactory::getApplication()->input; |
| 31 | |
| 32 | /** |
| 33 | * Added token validation. |
| 34 | * |
| 35 | * @since 1.7 |
| 36 | */ |
| 37 | if (!JSession::checkToken()) |
| 38 | { |
| 39 | // missing CSRF-proof token |
| 40 | UIErrorFactory::raiseError(403, JText::translate('JINVALID_TOKEN')); |
| 41 | } |
| 42 | |
| 43 | // retrieve order from request |
| 44 | $order = $input->get('order', array(), 'array'); |
| 45 | $filters = $input->get('filters', array(), 'array'); |
| 46 | |
| 47 | // init table |
| 48 | $table = $this->getModel()->getTable(); |
| 49 | |
| 50 | // iterate specified records |
| 51 | foreach ($order as $id => $ordering) |
| 52 | { |
| 53 | $data = array( |
| 54 | 'id' => (int) $id, |
| 55 | 'ordering' => (int) $ordering, |
| 56 | ); |
| 57 | |
| 58 | // update ordering |
| 59 | $table->save($data); |
| 60 | } |
| 61 | |
| 62 | $where = ''; |
| 63 | |
| 64 | if ($filters) |
| 65 | { |
| 66 | // create ordering conditions |
| 67 | $dbo = JFactory::getDbo(); |
| 68 | |
| 69 | $where = array(); |
| 70 | |
| 71 | foreach ($filters as $k => $v) |
| 72 | { |
| 73 | // insert condition only if the table owns the specified property |
| 74 | if (property_exists($table, $k)) |
| 75 | { |
| 76 | $where[] = $dbo->qn($k) . ' = ' . $dbo->q($v); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // join the conditions with AND glue |
| 81 | $where = implode(' AND ', $where); |
| 82 | } |
| 83 | |
| 84 | // rearrange table global ordering |
| 85 | $table->reorder($where); |
| 86 | |
| 87 | // stop the process |
| 88 | exit; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Echoes the given JSON by using the right content type. |
| 93 | * |
| 94 | * @param mixed $json Either a JSON string or a non-scalar value. |
| 95 | * |
| 96 | * @return void |
| 97 | */ |
| 98 | public function sendJSON($json) |
| 99 | { |
| 100 | VAPHttpDocument::getInstance(JFactory::getApplication())->json($json); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Method to get a model object. |
| 105 | * |
| 106 | * @param string $name The model name. |
| 107 | * @param string $prefix The class prefix. |
| 108 | * @param array $config Configuration array for model. |
| 109 | * |
| 110 | * @return mixed Model object on success; otherwise false on failure. |
| 111 | */ |
| 112 | public function getModel($name = '', $prefix = '', $config = array()) |
| 113 | { |
| 114 | if (!$name) |
| 115 | { |
| 116 | // use the name of the controller |
| 117 | $name = ucfirst($this->getControllerName()); |
| 118 | } |
| 119 | |
| 120 | if (!$prefix) |
| 121 | { |
| 122 | // always use the default model prefix |
| 123 | $prefix = 'VikAppointmentsModel'; |
| 124 | } |
| 125 | |
| 126 | // invoke parent |
| 127 | return parent::getModel($name, $prefix, $config); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Returns the name of the controller. |
| 132 | * |
| 133 | * NOTE: the native getName() method returns the string on the |
| 134 | * left of "Controller", which is always "VikAppointments". |
| 135 | * |
| 136 | * @return string |
| 137 | */ |
| 138 | public function getControllerName() |
| 139 | { |
| 140 | // fetches the string on the right of "Controller" |
| 141 | if (preg_match("/Controller(.*?)$/i", get_class($this), $match)) |
| 142 | { |
| 143 | return strtolower($match[1]); |
| 144 | } |
| 145 | |
| 146 | return null; |
| 147 | } |
| 148 | } |
| 149 |