| 1 |
<?php |
| 2 |
/** |
| 3 |
* |
| 4 |
*/ |
| 5 |
|
| 6 |
// Disallow direct access. |
| 7 |
defined('ABSPATH') or die("Access denied"); |
| 8 |
|
| 9 |
/** |
| 10 |
* |
| 11 |
*/ |
| 12 |
class CJTInstallerModel { |
| 13 |
|
| 14 |
/** |
| 15 |
* |
| 16 |
*/ |
| 17 |
const OPERATION_STATE_INSTALLED = 'installed'; |
| 18 |
|
| 19 |
/** |
| 20 |
* |
| 21 |
*/ |
| 22 |
const INSTALLATION_STATE = 'state.CJTInstallerModel.operations'; |
| 23 |
|
| 24 |
/** |
| 25 |
* |
| 26 |
*/ |
| 27 |
const NOTICED_DISMISSED_OPTION_NAME = 'settings.CJTInstallerModel.noticeDismissed'; |
| 28 |
|
| 29 |
/** |
| 30 |
* |
| 31 |
*/ |
| 32 |
const WARNING_NOTICE_DISMISSED_OPTION_NAME = 'settings.CJTInstallerModel.warningNoticeDismissed'; |
| 33 |
|
| 34 |
/** |
| 35 |
* put your comment there... |
| 36 |
* |
| 37 |
* @var mixed |
| 38 |
*/ |
| 39 |
protected $input; |
| 40 |
|
| 41 |
/** |
| 42 |
* put your comment there... |
| 43 |
* |
| 44 |
* @var mixed |
| 45 |
*/ |
| 46 |
protected $installedDbVersion; |
| 47 |
|
| 48 |
/** |
| 49 |
* put your comment there... |
| 50 |
* |
| 51 |
*/ |
| 52 |
public function __construct() { |
| 53 |
$this->installedDbVersion = get_option(CJTPlugin::DB_VERSION_OPTION_NAME); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* put your comment there... |
| 58 |
* |
| 59 |
* @param mixed $dismiss |
| 60 |
*/ |
| 61 |
public function dismissNotice($dismiss = null) { |
| 62 |
// Read current value! |
| 63 |
$currentValue = get_user_option(self::NOTICED_DISMISSED_OPTION_NAME); |
| 64 |
if ($dismiss !== null) { |
| 65 |
// Dismiss can' be reverted! |
| 66 |
update_user_option(get_current_user_id(), self::NOTICED_DISMISSED_OPTION_NAME, true); |
| 67 |
} |
| 68 |
return $currentValue; |
| 69 |
} |
| 70 |
|
| 71 |
public function dismissWarningNotice($dismiss = null) { |
| 72 |
// Read current value! |
| 73 |
$currentValue = get_user_option(self::WARNING_NOTICE_DISMISSED_OPTION_NAME); |
| 74 |
|
| 75 |
if ($dismiss !== null) { |
| 76 |
// Dismiss can' be reverted! |
| 77 |
update_user_option(get_current_user_id(), self::WARNING_NOTICE_DISMISSED_OPTION_NAME, true); |
| 78 |
} |
| 79 |
return $currentValue; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* put your comment there... |
| 84 |
* |
| 85 |
*/ |
| 86 |
public function getInstalledDbVersion() { |
| 87 |
return $this->installedDbVersion; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* put your comment there... |
| 92 |
* |
| 93 |
*/ |
| 94 |
public function getInternalVersionName() { |
| 95 |
return str_replace(array('.', '-'), '', $this->installedDbVersion); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Get installer operations for current CJT version! |
| 100 |
* |
| 101 |
* @return array Operations list metadata. |
| 102 |
*/ |
| 103 |
public function getOperations() |
| 104 |
{ |
| 105 |
|
| 106 |
// Read all install/upgrade operations for all versions! |
| 107 |
$operations = get_option(self::INSTALLATION_STATE); |
| 108 |
$operations = is_array($operations) ? $operations : array(); |
| 109 |
|
| 110 |
// Check if cached: Use only installer cache for 'current' CJT version. |
| 111 |
if (!isset($operations[CJTPlugin::DB_VERSION])) |
| 112 |
{ |
| 113 |
// Import installer reflection! |
| 114 |
cssJSToolbox::import('framework:installer:reflection.class.php'); |
| 115 |
|
| 116 |
// Get Installer operations. |
| 117 |
cssJSToolbox::import('includes:installer:installer:installer.class.php'); |
| 118 |
|
| 119 |
$operations[CJTPlugin::DB_VERSION]['operations']['install'] = CJTInstallerReflection::getInstance('CJTInstaller', 'CJTInstaller')->getOperations(); |
| 120 |
|
| 121 |
if ($this->isUpgrade()) |
| 122 |
{ |
| 123 |
// Get upgrade operations , Also cache upgrader info for later use! |
| 124 |
$operations[CJTPlugin::DB_VERSION]['upgrader'] = $upgrader = $this->getUpgrader(); |
| 125 |
|
| 126 |
// Check if upgrader exists! |
| 127 |
if (!file_exists(cssJSToolbox::resolvePath($upgrader['file']))) |
| 128 |
{ |
| 129 |
throw new Exception("Could not find upgrade/downgrade agent for installer '{$this->installedDbVersion}'! Incompatible version numbers! Upgrader/Downgrwader is no being supported by current versions!!"); |
| 130 |
} |
| 131 |
|
| 132 |
// Import upgrader + reflect its operations! |
| 133 |
cssJSToolbox::import($upgrader['file']); |
| 134 |
|
| 135 |
$operations[CJTPlugin::DB_VERSION]['operations']['upgrade'] = CJTInstallerReflection::getInstance($upgrader['class'], 'CJTUpgradeNonTabledVersions')->getOperations(); |
| 136 |
} |
| 137 |
|
| 138 |
update_option(self::INSTALLATION_STATE, $operations); |
| 139 |
} |
| 140 |
|
| 141 |
$operations[CJTPlugin::DB_VERSION] = apply_filters( |
| 142 |
CJTPluggableHelper::FILTER_INSTALLER_OPERATIONS, |
| 143 |
$operations[CJTPlugin::DB_VERSION], |
| 144 |
$this->isUpgrade() |
| 145 |
); |
| 146 |
|
| 147 |
return $operations[CJTPlugin::DB_VERSION]; |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* put your comment there... |
| 152 |
* |
| 153 |
*/ |
| 154 |
public function getUpgrader() { |
| 155 |
// Upgrader file. |
| 156 |
$upgrader['file'] = "includes:installer:upgrade:{$this->getInstalledDbVersion()}:upgrade.class.php"; |
| 157 |
// Upgrader class name. |
| 158 |
$upgrader['class'] = "CJTV{$this->getInternalVersionName()}Upgrade"; |
| 159 |
return $upgrader; |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Allow executing of a single installation operation! |
| 164 |
* Both Install and Upgrade operations can be executed throught here |
| 165 |
* |
| 166 |
* |
| 167 |
* @return void |
| 168 |
*/ |
| 169 |
public function install() |
| 170 |
{ |
| 171 |
|
| 172 |
// Initialize. |
| 173 |
$result = FALSE; |
| 174 |
|
| 175 |
// Read input! |
| 176 |
$rOperation = $this->input['operation']; |
| 177 |
$type = $rOperation['type']; |
| 178 |
|
| 179 |
// Get allowed operations with their state! |
| 180 |
$operations = (array) get_option(self::INSTALLATION_STATE); |
| 181 |
$vOperations =& $operations[CJTPlugin::DB_VERSION]; |
| 182 |
|
| 183 |
// Invalid operation! |
| 184 |
if (!isset($vOperations['operations'][$type][$rOperation['name']])) |
| 185 |
{ |
| 186 |
throw new Exception('Invalid operation'); |
| 187 |
} |
| 188 |
|
| 189 |
// Install only if not installed! |
| 190 |
$operation =& $vOperations['operations'][$type][$rOperation['name']]; |
| 191 |
if ((!isset($operation['state'])) || |
| 192 |
($operation['state'] != self::OPERATION_STATE_INSTALLED)) |
| 193 |
{ |
| 194 |
// Import installer and get installer object! |
| 195 |
switch ($type) |
| 196 |
{ |
| 197 |
|
| 198 |
case 'install': |
| 199 |
|
| 200 |
cssJSToolbox::import('includes:installer:installer:installer.class.php'); |
| 201 |
|
| 202 |
$installer = CJTInstaller::getInstance(); |
| 203 |
|
| 204 |
break; |
| 205 |
|
| 206 |
case 'upgrade': |
| 207 |
|
| 208 |
cssJSToolbox::import($vOperations['upgrader']['file']); |
| 209 |
|
| 210 |
$installer = new $vOperations['upgrader']['class'](); |
| 211 |
|
| 212 |
break; |
| 213 |
|
| 214 |
} |
| 215 |
|
| 216 |
// Execute the requested operation |
| 217 |
$installer->{$rOperation['name']}(); |
| 218 |
|
| 219 |
$result = array |
| 220 |
( |
| 221 |
'state' => self::OPERATION_STATE_INSTALLED |
| 222 |
); |
| 223 |
|
| 224 |
} |
| 225 |
|
| 226 |
return $result; |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* put your comment there... |
| 231 |
* |
| 232 |
*/ |
| 233 |
public function isCommonRelease() { |
| 234 |
// Check weather the current installed version is in the same |
| 235 |
// release with the running version. |
| 236 |
// Get instaleled version components. |
| 237 |
$ivComponent = explode('.', str_replace('-', '.', $this->getInstalledDbVersion())); |
| 238 |
$nvComponent = explode('.', str_replace('-', '.', CJTPlugin::DB_VERSION)); |
| 239 |
// Use PE-Edition sign if not sign used! |
| 240 |
if (!isset($ivComponent[2])) { |
| 241 |
$ivComponent[2] = 'PE'; |
| 242 |
} |
| 243 |
if (!isset($nvComponent[2])) { |
| 244 |
$nvComponent[2] = 'PE'; |
| 245 |
} |
| 246 |
// TRUE if same, FALSE otherwise. |
| 247 |
return (($ivComponent[0] == $nvComponent[0]) && ($ivComponent[2] == $nvComponent[2])); |
| 248 |
} |
| 249 |
|
| 250 |
/** |
| 251 |
* put your comment there... |
| 252 |
* |
| 253 |
*/ |
| 254 |
public function isUpgrade() { |
| 255 |
// If the version is not the same and not equal to current version then its upgrading! |
| 256 |
$isUpgrade = (($this->installedDbVersion != CJTPlugin::DB_VERSION) && ($this->installedDbVersion != '')); |
| 257 |
return $isUpgrade; |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* put your comment there... |
| 262 |
* |
| 263 |
* @param mixed $input |
| 264 |
*/ |
| 265 |
public function setInput(& $input) { |
| 266 |
$this->input = $input; |
| 267 |
return $this; // Chaining! |
| 268 |
} |
| 269 |
|
| 270 |
} // End class. |
| 271 |
|