| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 4 |
* @subpackage com_vikbooking |
| 5 |
* @author Alessio Gaggii - e4j - Extensionsforjoomla.com |
| 6 |
* @copyright Copyright (C) 2018 e4j - Extensionsforjoomla.com. All rights reserved. |
| 7 |
* @license GNU General Public License version 2 or later; see LICENSE |
| 8 |
* @link https://vikwp.com |
| 9 |
*/ |
| 10 |
|
| 11 |
// No direct access to this file |
| 12 |
defined('ABSPATH') or die('No script kiddies please!'); |
| 13 |
|
| 14 |
/** |
| 15 |
* This class is used to handle the extra query field for the extension updates. |
| 16 |
* |
| 17 |
* USAGE: |
| 18 |
* |
| 19 |
* $update = new UriUpdateHandler(); // or new UriUpdateHandler('com_example') |
| 20 |
* |
| 21 |
* $update->addExtraField('order_number', $order_number); |
| 22 |
* $update->addExtraField('domain', $domain); |
| 23 |
* |
| 24 |
* // OR |
| 25 |
* |
| 26 |
* $update->setExtraFields(array( |
| 27 |
* 'order_number' => $order_number, |
| 28 |
* 'domain' => $domain, |
| 29 |
* )); |
| 30 |
* |
| 31 |
* $update->register(); |
| 32 |
* |
| 33 |
* @since 1.0 |
| 34 |
*/ |
| 35 |
class UriUpdateHandler |
| 36 |
{ |
| 37 |
/** |
| 38 |
* The component (or plugin/module) instance that need to be updated. |
| 39 |
* |
| 40 |
* @var mixed |
| 41 |
*/ |
| 42 |
private $component = null; |
| 43 |
|
| 44 |
/** |
| 45 |
* The query string containing the extra fields to append to the update URI. |
| 46 |
* |
| 47 |
* @var string |
| 48 |
*/ |
| 49 |
private $extraFields = ''; |
| 50 |
|
| 51 |
/** |
| 52 |
* Class constructor. |
| 53 |
* @param mixed $element The element to load. Null to load the current component. |
| 54 |
* |
| 55 |
* @uses getComponent() Load the specified/current component. |
| 56 |
*/ |
| 57 |
public function __construct($element = null) |
| 58 |
{ |
| 59 |
$this->getComponent($element); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Load the specified/current component. |
| 64 |
* |
| 65 |
* @param mixed $element The element to load. Null to load the current component. |
| 66 |
* |
| 67 |
* @return mixed The loaded component. |
| 68 |
*/ |
| 69 |
public function getComponent($element = null) |
| 70 |
{ |
| 71 |
if ($element === null) { |
| 72 |
$element = JFactory::getApplication()->input->get('option'); |
| 73 |
} |
| 74 |
|
| 75 |
jimport('joomla.application.component.helper'); |
| 76 |
$this->component = JComponentHelper::getComponent($element); |
| 77 |
|
| 78 |
return $this->component; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Set the parameters into the additional query string. |
| 83 |
* |
| 84 |
* @param array $params The associative array to push. |
| 85 |
* |
| 86 |
* @return UriUpdateHandler This object to support chaining. |
| 87 |
* |
| 88 |
* @uses addExtraField() Append a single parameter to the query string. |
| 89 |
*/ |
| 90 |
public function setExtraFields(array $params = array()) |
| 91 |
{ |
| 92 |
$this->extraFields = ''; |
| 93 |
|
| 94 |
foreach ($params as $key => $val ) { |
| 95 |
$this->addExtraField($key, $val); |
| 96 |
} |
| 97 |
|
| 98 |
return $this; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Push a single value into the additional query string. |
| 103 |
* |
| 104 |
* @param string $key The name of the query param. |
| 105 |
* @param string $val The value of the query param. |
| 106 |
* |
| 107 |
* @return UriUpdateHandler This object to support chaining. |
| 108 |
*/ |
| 109 |
public function addExtraField($key, $val) |
| 110 |
{ |
| 111 |
if (is_scalar($val)) { |
| 112 |
$this->extraFields .= (empty($this->extraFields) ? '' : '&') . $key."=".urlencode($val); |
| 113 |
} |
| 114 |
|
| 115 |
return $this; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Commit the changes by updating the extra_fields column of the |
| 120 |
* #__update_sites_extensions database table. |
| 121 |
* |
| 122 |
* @return boolean True on success, otherwise false. |
| 123 |
*/ |
| 124 |
public function register() |
| 125 |
{ |
| 126 |
if (!$this->component) { |
| 127 |
return false; |
| 128 |
} |
| 129 |
|
| 130 |
$dbo = JFactory::getDbo(); |
| 131 |
|
| 132 |
// load the update site record, if it exists |
| 133 |
$q = $dbo->getQuery(true); |
| 134 |
|
| 135 |
$q->select('update_site_id') |
| 136 |
->from('#__update_sites_extensions') |
| 137 |
->where('extension_id = '.$dbo->q($this->component->id)); |
| 138 |
|
| 139 |
$dbo->setQuery($q); |
| 140 |
$updateSite = $dbo->loadResult(); |
| 141 |
|
| 142 |
$success = false; |
| 143 |
|
| 144 |
if ($updateSite) { |
| 145 |
// update the update site record |
| 146 |
$q = $dbo->getQuery(true); |
| 147 |
|
| 148 |
$q->update($dbo->qn('#__update_sites')) |
| 149 |
->set($dbo->qn('extra_query') . ' = ' . $dbo->q($this->extraFields)) |
| 150 |
->set($dbo->qn('enabled') . ' = 1') |
| 151 |
->set($dbo->qn('last_check_timestamp') . ' = 0') |
| 152 |
->where($dbo->qn('update_site_id') .' = '.$dbo->q($updateSite)); |
| 153 |
|
| 154 |
$dbo->setQuery($q); |
| 155 |
$dbo->execute(); |
| 156 |
|
| 157 |
$success = (bool) $dbo->getAffectedRows(); |
| 158 |
|
| 159 |
// Delete any existing updates (essentially flushes the updates cache for this update site) |
| 160 |
$q = $dbo->getQuery(true); |
| 161 |
|
| 162 |
$q->delete('#__updates') |
| 163 |
->where('update_site_id = '.$dbo->q($updateSite)); |
| 164 |
|
| 165 |
$dbo->setQuery($q); |
| 166 |
$dbo->execute(); |
| 167 |
} |
| 168 |
|
| 169 |
return $success; |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Check the schema of the extension to make sure the system will use |
| 174 |
* the current version. |
| 175 |
* |
| 176 |
* @param string $version The current version of the component. |
| 177 |
* |
| 178 |
* @return UriUpdateHandler This object to support chaining. |
| 179 |
*/ |
| 180 |
public function checkSchema($version) |
| 181 |
{ |
| 182 |
if (!$this->component) { |
| 183 |
return false; |
| 184 |
} |
| 185 |
|
| 186 |
$ok = false; |
| 187 |
|
| 188 |
$dbo = JFactory::getDbo(); |
| 189 |
|
| 190 |
$q = $dbo->getQuery(true); |
| 191 |
|
| 192 |
$q->select('version_id') |
| 193 |
->from('#__schemas') |
| 194 |
->where('extension_id = ' . $this->component->id); |
| 195 |
|
| 196 |
$dbo->setQuery($q, 0, 1); |
| 197 |
$version_id = $dbo->loadResult(); |
| 198 |
|
| 199 |
if ($version_id) { |
| 200 |
|
| 201 |
if ($version_id == $version) { |
| 202 |
$ok = true; |
| 203 |
} else { |
| 204 |
$q->clear() |
| 205 |
->delete('#__schemas') |
| 206 |
->where('extension_id = ' . $this->component->id); |
| 207 |
|
| 208 |
$dbo->setQuery($q); |
| 209 |
$dbo->execute(); |
| 210 |
} |
| 211 |
|
| 212 |
} |
| 213 |
|
| 214 |
if (!$ok) { |
| 215 |
$q->clear() |
| 216 |
->insert('#__schemas') |
| 217 |
->columns(array('extension_id', 'version_id')) |
| 218 |
->values($this->component->id . ', ' . $dbo->q($version)); |
| 219 |
|
| 220 |
$dbo->setQuery($q); |
| 221 |
$ok = (bool) $dbo->execute(); |
| 222 |
} |
| 223 |
|
| 224 |
return $ok; |
| 225 |
} |
| 226 |
|
| 227 |
} |
| 228 |
|