admin.php
292 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.6 |
| 20 | */ |
| 21 | class UIControllerAdmin extends JControllerAdmin |
| 22 | { |
| 23 | /** |
| 24 | * @override |
| 25 | * Redirects the browser or returns false if no redirect is set. |
| 26 | * |
| 27 | * @return boolean False if no redirect exists. |
| 28 | */ |
| 29 | public function redirect($url = null) |
| 30 | { |
| 31 | // if empty URL get parent property |
| 32 | if (empty($url) && isset($this->redirect)) |
| 33 | { |
| 34 | $url = $this->redirect; |
| 35 | } |
| 36 | |
| 37 | // proceed only if the URL is not empty |
| 38 | if (!empty($url)) |
| 39 | { |
| 40 | $app = JFactory::getApplication(); |
| 41 | |
| 42 | // enqueue message (only if set) |
| 43 | if (!empty($this->message)) |
| 44 | { |
| 45 | $this->messageType = !empty($this->messageType) ? $this->messageType : ''; |
| 46 | $app->enqueueMessage($this->message, $this->messageType); |
| 47 | } |
| 48 | |
| 49 | // get Item ID |
| 50 | $itemid = $app->input->getInt('Itemid'); |
| 51 | |
| 52 | // append Item ID if set and it is not contained in the URL |
| 53 | if ($itemid && strpos($url, 'Itemid=') === false) |
| 54 | { |
| 55 | $url .= '&Itemid=' . $itemid; |
| 56 | } |
| 57 | |
| 58 | // do redirect |
| 59 | $app->redirect(JRoute::rewrite($url, false)); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Protected method to delete the given records. |
| 65 | * |
| 66 | * @param mixed $id The primary key value or a list of values. |
| 67 | * @param mixed $table The database table or an associative array |
| 68 | * containing the DB requirements: |
| 69 | * - table The DB table name (mandatory). |
| 70 | * - pk The primary key column name (optional). |
| 71 | * @param array $where An array of WHERE statements to use (with AND glue). |
| 72 | * |
| 73 | * @return boolean |
| 74 | */ |
| 75 | protected function _delete($id, $table, array $where = array()) |
| 76 | { |
| 77 | if (empty($id)) |
| 78 | { |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | $dbo = JFactory::getDbo(); |
| 83 | |
| 84 | if (!is_array($table)) |
| 85 | { |
| 86 | // if the table is a string we need to push |
| 87 | // it in an associative array |
| 88 | $table = array('table' => $table); |
| 89 | } |
| 90 | |
| 91 | // get database requirements |
| 92 | $pk = isset($table['pk']) ? $table['pk'] : 'id'; |
| 93 | $table = isset($table['table']) ? $table['table'] : ''; |
| 94 | |
| 95 | $q = $dbo->getQuery(true) |
| 96 | ->delete($dbo->qn($table)); |
| 97 | |
| 98 | // create PRIMARY KEY statement |
| 99 | if (is_array($id)) |
| 100 | { |
| 101 | // quote the IDs |
| 102 | $id = array_map(array($dbo, 'q'), $id); |
| 103 | // create IN statement |
| 104 | $q->where($dbo->qn($pk) . ' IN (' . implode(',', $id) . ')'); |
| 105 | } |
| 106 | // skip if we should delete all ignoring the PK |
| 107 | else if ($id != '*') |
| 108 | { |
| 109 | $q->where($dbo->qn($pk) . ' = ' . $dbo->q($id)); |
| 110 | } |
| 111 | |
| 112 | // create WHERE statement with custom claus |
| 113 | foreach ($where as $column => $values) |
| 114 | { |
| 115 | if (is_array($values) && count($values) > 1) |
| 116 | { |
| 117 | $values = array_map(array($dbo, 'q'), $values); |
| 118 | $q->where($dbo->qn($column) . ' IN (' . implode(',', $values) . ')'); |
| 119 | } |
| 120 | else |
| 121 | { |
| 122 | $values = is_array($values) ? array_shift($values) : $values; |
| 123 | $q->where($dbo->qn($column) . ' = ' . $dbo->q($values)); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | $dbo->setQuery($q); |
| 128 | $dbo->execute(); |
| 129 | |
| 130 | return (bool) $dbo->getAffectedRows(); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Protected method to change the ordering of a record. |
| 135 | * |
| 136 | * @param mixed $id The primary key value. |
| 137 | * @param mixed $table The database table or an associative array |
| 138 | * containing the DB requirements: |
| 139 | * - table The DB table name (mandatory). |
| 140 | * - pk The primary key column name (optional). |
| 141 | * - ordering The ordering column name (optional). |
| 142 | * @param string $mode The code to move the record UP ('up') or DOWN ('down'). |
| 143 | * @param array $where An array of WHERE statements to use (with AND glue). |
| 144 | * |
| 145 | * @return void |
| 146 | */ |
| 147 | protected function _move($id, $table, $mode = 'up', array $where = array()) |
| 148 | { |
| 149 | $dbo = JFactory::getDbo(); |
| 150 | |
| 151 | if (!is_array($table)) |
| 152 | { |
| 153 | // if the table is a string we need to push |
| 154 | // it in an associative array |
| 155 | $table = array('table' => $table); |
| 156 | } |
| 157 | |
| 158 | // get database requirements |
| 159 | $ordcol = isset($table['ordering']) ? $table['ordering'] : 'ordering'; |
| 160 | $idcol = isset($table['pk']) ? $table['pk'] : 'id'; |
| 161 | $table = isset($table['table']) ? $table['table'] : ''; |
| 162 | |
| 163 | // get selected record |
| 164 | $q = $dbo->getQuery(true) |
| 165 | ->select($dbo->qn(array($idcol, $ordcol))) |
| 166 | ->from($dbo->qn($table)) |
| 167 | ->where($dbo->qn($idcol) . ' = ' . $dbo->q($id)); |
| 168 | |
| 169 | $dbo->setQuery($q, 0, 1); |
| 170 | $data = $dbo->loadObject(); |
| 171 | |
| 172 | if (!$data) |
| 173 | { |
| 174 | // row not found |
| 175 | return; |
| 176 | } |
| 177 | |
| 178 | // get next/prev record |
| 179 | $q = $dbo->getQuery(true) |
| 180 | ->select($dbo->qn(array($idcol, $ordcol))) |
| 181 | ->from($dbo->qn($table)) |
| 182 | ->where($dbo->qn($ordcol) . ' ' . ($mode == 'up' ? '<' : '>') . ' ' . $data->{$ordcol}) |
| 183 | ->order($dbo->qn($ordcol) . ' ' . ($mode == 'up' ? 'DESC' : 'ASC')); |
| 184 | |
| 185 | // create WHERE statement with custom claus |
| 186 | foreach ($where as $column => $values) |
| 187 | { |
| 188 | if (is_array($values) && count($values) > 1) |
| 189 | { |
| 190 | $values = array_map(array($dbo, 'q'), $values); |
| 191 | $q->where($dbo->qn($column) . ' IN (' . implode(',', $values) . ')'); |
| 192 | } |
| 193 | else |
| 194 | { |
| 195 | $values = is_array($values) ? array_shift($values) : $values; |
| 196 | $q->where($dbo->qn($column) . ' = ' . $dbo->q($values)); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | $dbo->setQuery($q, 0, 1); |
| 201 | $next = $dbo->loadObject(); |
| 202 | |
| 203 | if (!$next) |
| 204 | { |
| 205 | // the row is probably the first/last |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | // swap orderings |
| 210 | $tmp = $data->{$ordcol}; |
| 211 | $data->{$ordcol} = $next->{$ordcol}; |
| 212 | $next->{$ordcol} = $tmp; |
| 213 | |
| 214 | // update the records |
| 215 | $dbo->updateObject($table, $data, $idcol); |
| 216 | $dbo->updateObject($table, $next, $idcol); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Protected method to change the status of the given records. |
| 221 | * |
| 222 | * @param mixed $id The primary key value or a list of values. |
| 223 | * @param mixed $status The status key to set. |
| 224 | * @param mixed $table The database table or an associative array |
| 225 | * containing the DB requirements: |
| 226 | * - table The DB table name (mandatory). |
| 227 | * - pk The primary key column name (optional). |
| 228 | * - status The status column name (optional). |
| 229 | * @param array $where An array of WHERE statements to use (with AND glue). |
| 230 | * |
| 231 | * @return boolean |
| 232 | */ |
| 233 | protected function _publish($id, $status, $table, array $where = array()) |
| 234 | { |
| 235 | if (empty($id)) |
| 236 | { |
| 237 | return false; |
| 238 | } |
| 239 | |
| 240 | $dbo = JFactory::getDbo(); |
| 241 | |
| 242 | if (!is_array($table)) |
| 243 | { |
| 244 | // if the table is a string we need to push |
| 245 | // it in an associative array |
| 246 | $table = array('table' => $table); |
| 247 | } |
| 248 | |
| 249 | // get database requirements |
| 250 | $pk = isset($table['pk']) ? $table['pk'] : 'id'; |
| 251 | $col = isset($table['status']) ? $table['status'] : 'status'; |
| 252 | $table = isset($table['table']) ? $table['table'] : ''; |
| 253 | |
| 254 | $q = $dbo->getQuery(true) |
| 255 | ->update($dbo->qn($table)) |
| 256 | ->set($dbo->qn($col) . ' = ' . $dbo->q($status)); |
| 257 | |
| 258 | // create PRIMARY KEY statement |
| 259 | if (is_array($id)) |
| 260 | { |
| 261 | // quote the IDs |
| 262 | $id = array_map(array($dbo, 'q'), $id); |
| 263 | // create IN statement |
| 264 | $q->where($dbo->qn($pk) . ' IN (' . implode(',', $id) . ')'); |
| 265 | } |
| 266 | else |
| 267 | { |
| 268 | $q->where($dbo->qn($pk) . ' = ' . $dbo->q($id)); |
| 269 | } |
| 270 | |
| 271 | // create WHERE statement with custom claus |
| 272 | foreach ($where as $column => $values) |
| 273 | { |
| 274 | if (is_array($values) && count($values) > 1) |
| 275 | { |
| 276 | $values = array_map(array($dbo, 'q'), $values); |
| 277 | $q->where($dbo->qn($column) . ' IN (' . implode(',', $values) . ')'); |
| 278 | } |
| 279 | else |
| 280 | { |
| 281 | $values = is_array($values) ? array_shift($values) : $values; |
| 282 | $q->where($dbo->qn($column) . ' = ' . $dbo->q($values)); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | $dbo->setQuery($q); |
| 287 | $dbo->execute(); |
| 288 | |
| 289 | return (bool) $dbo->getAffectedRows(); |
| 290 | } |
| 291 | } |
| 292 |