database.php
104 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 | // include parent class in order to extend the configuration without errors |
| 15 | VAPLoader::import('libraries.config.abstract'); |
| 16 | |
| 17 | /** |
| 18 | * Utility class working with a physical configuration stored into the Joomla database. |
| 19 | * |
| 20 | * @since 1.6 |
| 21 | * @since 1.7 Renamed from UIConfigDatabase |
| 22 | */ |
| 23 | class VAPConfigDatabase extends VAPConfig |
| 24 | { |
| 25 | /** |
| 26 | * Class constructor. |
| 27 | * |
| 28 | * @param array $options An array of options. |
| 29 | */ |
| 30 | public function __construct(array $options = array()) |
| 31 | { |
| 32 | if (!isset($options['table'])) |
| 33 | { |
| 34 | $options['table'] = '#__vikappointments_config'; |
| 35 | } |
| 36 | |
| 37 | if (!isset($options['key'])) |
| 38 | { |
| 39 | $options['key'] = 'param'; |
| 40 | } |
| 41 | |
| 42 | if (!isset($options['value'])) |
| 43 | { |
| 44 | $options['value'] = 'setting'; |
| 45 | } |
| 46 | |
| 47 | parent::__construct($options); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * @override |
| 52 | * Retrieves the value of the setting stored in the Joomla database. |
| 53 | * |
| 54 | * @param string $key The name of the setting. |
| 55 | * |
| 56 | * @return mixed The value of the setting if exists, otherwise false. |
| 57 | */ |
| 58 | protected function retrieve($key) |
| 59 | { |
| 60 | $dbo = JFactory::getDbo(); |
| 61 | |
| 62 | $q = $dbo->getQuery(true); |
| 63 | |
| 64 | $q->select($dbo->qn($this->options['value'])) |
| 65 | ->from($dbo->qn($this->options['table'])) |
| 66 | ->where($dbo->qn($this->options['key']) . ' = ' . $dbo->q($key)); |
| 67 | |
| 68 | $dbo->setQuery($q, 0, 1); |
| 69 | return $dbo->loadResult() ?? false; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @override |
| 74 | * Registers the value of the setting into the Joomla database. |
| 75 | * All the array and objects will be stringified in JSON. |
| 76 | * |
| 77 | * @param string $key The name of the setting. |
| 78 | * @param mixed $val The value of the setting. |
| 79 | * |
| 80 | * @return bool True in case of success, otherwise false. |
| 81 | */ |
| 82 | protected function register($key, $val) |
| 83 | { |
| 84 | // get config table |
| 85 | $config = JModelVAP::getInstance('configuration'); |
| 86 | |
| 87 | if (!$config) |
| 88 | { |
| 89 | // Models not yet loaded... |
| 90 | // Auto include the default models folder. |
| 91 | JModelLegacy::addIncludePath(VAPADMIN . DIRECTORY_SEPARATOR . 'models'); |
| 92 | |
| 93 | // try again to load the configuration model |
| 94 | $config = JModelVAP::getInstance('configuration'); |
| 95 | } |
| 96 | |
| 97 | // save configuration setting |
| 98 | return $config->save(array( |
| 99 | 'param' => $key, |
| 100 | 'setting' => $val, |
| 101 | )); |
| 102 | } |
| 103 | } |
| 104 |