| @@ -1,0 +1,149 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * @package VikBooking | |
| 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 | +/** | |
| 15 | + * Utility class working with a physical configuration stored into the Joomla database. | |
| 16 | + * | |
| 17 | + * @since 1.5 | |
| 18 | + */ | |
| 19 | +class VBOConfigRegistryDatabase extends VBOConfigRegistry | |
| 20 | +{ | |
| 21 | + /** | |
| 22 | + * Class constructor. | |
| 23 | + * | |
| 24 | + * @param array $options An array of options. | |
| 25 | + */ | |
| 26 | + public function __construct(array $options = []) | |
| 27 | + { | |
| 28 | + if (!isset($options['table'])) | |
| 29 | + { | |
| 30 | + // use default plugin database table | |
| 31 | + $options['table'] = '#__vikbooking_config'; | |
| 32 | + } | |
| 33 | + | |
| 34 | + if (!isset($options['key'])) | |
| 35 | + { | |
| 36 | + // use default plugin "param" column | |
| 37 | + $options['key'] = 'param'; | |
| 38 | + } | |
| 39 | + | |
| 40 | + if (!isset($options['value'])) | |
| 41 | + { | |
| 42 | + // use default plugin "setting" column | |
| 43 | + $options['value'] = 'setting'; | |
| 44 | + } | |
| 45 | + | |
| 46 | + if (!isset($options['db'])) | |
| 47 | + { | |
| 48 | + // use default CMS database driver | |
| 49 | + $options['db'] = JFactory::getDbo(); | |
| 50 | + } | |
| 51 | + | |
| 52 | + // delegate construction to parent | |
| 53 | + parent::__construct($options); | |
| 54 | + } | |
| 55 | + | |
| 56 | + /** | |
| 57 | + * @override | |
| 58 | + * Retrieves the value of the setting stored in the Joomla database. | |
| 59 | + * | |
| 60 | + * @param string $key The name of the setting. | |
| 61 | + * | |
| 62 | + * @return mixed The value of the setting if exists, otherwise false. | |
| 63 | + */ | |
| 64 | + protected function retrieve($key) | |
| 65 | + { | |
| 66 | + $db = $this->options['db']; | |
| 67 | + | |
| 68 | + $q = $db->getQuery(true); | |
| 69 | + | |
| 70 | + $q->select($db->qn($this->options['value'])) | |
| 71 | + ->from($db->qn($this->options['table'])) | |
| 72 | + ->where($db->qn($this->options['key']) . ' = ' . $db->q($key)); | |
| 73 | + | |
| 74 | + $db->setQuery($q, 0, 1); | |
| 75 | + $record = $db->loadObject(); | |
| 76 | + | |
| 77 | + if ($record) | |
| 78 | + { | |
| 79 | + return $record->{$this->options['value']}; | |
| 80 | + } | |
| 81 | + | |
| 82 | + return false; | |
| 83 | + } | |
| 84 | + | |
| 85 | + /** | |
| 86 | + * @override | |
| 87 | + * Registers the value of the setting into the Joomla database. | |
| 88 | + * All the array and objects will be stringified in JSON. | |
| 89 | + * | |
| 90 | + * @param string $key The name of the setting. | |
| 91 | + * @param mixed $val The value of the setting. | |
| 92 | + * | |
| 93 | + * @return boolean True in case of success, otherwise false. | |
| 94 | + */ | |
| 95 | + protected function register($key, $val) | |
| 96 | + { | |
| 97 | + $db = $this->options['db']; | |
| 98 | + | |
| 99 | + if (is_array($val) || is_object($val)) | |
| 100 | + { | |
| 101 | + // stringify array/object | |
| 102 | + $val = json_encode($val); | |
| 103 | + } | |
| 104 | + | |
| 105 | + // prepare object to save | |
| 106 | + $data = new stdClass; | |
| 107 | + $data->{$this->options['key']} = $key; | |
| 108 | + $data->{$this->options['value']} = $val; | |
| 109 | + | |
| 110 | + // check whether the setting already exists | |
| 111 | + if ($this->has($key)) | |
| 112 | + { | |
| 113 | + // update existing record | |
| 114 | + $result = $db->updateObject($this->options['table'], $data, 'param', $updateNulls = true); | |
| 115 | + } | |
| 116 | + else | |
| 117 | + { | |
| 118 | + // insert new record | |
| 119 | + $data->id = 0; | |
| 120 | + $result = $db->insertObject($this->options['table'], $data, 'id'); | |
| 121 | + } | |
| 122 | + | |
| 123 | + return $result; | |
| 124 | + } | |
| 125 | + | |
| 126 | + /** | |
| 127 | + * @override | |
| 128 | + * Deletes the setting from the instance where it's stored. | |
| 129 | + * | |
| 130 | + * @param string $key The name of the setting. | |
| 131 | + * | |
| 132 | + * @return boolean True in case of success, otherwise false. | |
| 133 | + * | |
| 134 | + * @since 1.16.0 (J) - 1.6.0 (WP) | |
| 135 | + */ | |
| 136 | + protected function delete($key) | |
| 137 | + { | |
| 138 | + $db = $this->options['db']; | |
| 139 | + | |
| 140 | + $query = $db->getQuery(true) | |
| 141 | + ->delete($db->qn($this->options['table'])) | |
| 142 | + ->where($db->qn('param') . '=' . $db->q($key)); | |
| 143 | + | |
| 144 | + $db->setQuery($query); | |
| 145 | + $db->execute(); | |
| 146 | + | |
| 147 | + return (bool) $db->getAffectedRows(); | |
| 148 | + } | |
| 149 | +} | |