| 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 |
* put your comment there... |
| 31 |
* |
| 32 |
* @var mixed |
| 33 |
*/ |
| 34 |
protected $input; |
| 35 |
|
| 36 |
/** |
| 37 |
* put your comment there... |
| 38 |
* |
| 39 |
* @var mixed |
| 40 |
*/ |
| 41 |
protected $installedDbVersion; |
| 42 |
|
| 43 |
/** |
| 44 |
* put your comment there... |
| 45 |
* |
| 46 |
*/ |
| 47 |
public function __construct() { |
| 48 |
$this->installedDbVersion = get_option(CJTPlugin::DB_VERSION_OPTION_NAME); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* put your comment there... |
| 53 |
* |
| 54 |
* @param mixed $dismiss |
| 55 |
*/ |
| 56 |
public function dismissNotice($dismiss = null) { |
| 57 |
// Read current value! |
| 58 |
$currentValue = get_user_option(self::NOTICED_DISMISSED_OPTION_NAME); |
| 59 |
if ($dismiss !== null) { |
| 60 |
// Dismiss can' be reverted! |
| 61 |
update_user_option(get_current_user_id(), self::NOTICED_DISMISSED_OPTION_NAME, true); |
| 62 |
} |
| 63 |
return $currentValue; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* put your comment there... |
| 68 |
* |
| 69 |
*/ |
| 70 |
public function getInstalledDbVersion() { |
| 71 |
return $this->installedDbVersion; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* put your comment there... |
| 76 |
* |
| 77 |
*/ |
| 78 |
public function getInternalVersionName() { |
| 79 |
return str_replace(array('.', '-'), '', $this->installedDbVersion); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Get installer operations for current CJT version! |
| 84 |
* |
| 85 |
* @return array Operations list metadata. |
| 86 |
*/ |
| 87 |
public function getOperations() { |
| 88 |
// Read all install/upgrade operations for all versions! |
| 89 |
$operations = get_option(self::INSTALLATION_STATE); |
| 90 |
$operations = is_array($operations) ? $operations : array(); |
| 91 |
// Check if cached: Use only installer cache for 'current' CJT version. |
| 92 |
if (!isset($operations[CJTPlugin::DB_VERSION])) { |
| 93 |
// Import installer reflection! |
| 94 |
cssJSToolbox::import('framework:installer:reflection.class.php'); |
| 95 |
// Get Installer operations. |
| 96 |
cssJSToolbox::import('includes:installer:installer:installer.class.php'); |
| 97 |
$operations[CJTPlugin::DB_VERSION]['operations']['install'] = CJTInstallerReflection::getInstance('CJTInstaller', 'CJTInstaller')->getOperations(); |
| 98 |
if ($this->isUpgrade()) { |
| 99 |
// Get upgrade operations , Also cache upgrader info for later use! |
| 100 |
$operations[CJTPlugin::DB_VERSION]['upgrader'] = $upgrader = $this->getUpgrader(); |
| 101 |
// Check if upgrader exists! |
| 102 |
if (!file_exists(cssJSToolbox::resolvePath($upgrader['file']))) { |
| 103 |
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!!"); |
| 104 |
} |
| 105 |
// Import upgrader + reflect its operations! |
| 106 |
cssJSToolbox::import($upgrader['file']); |
| 107 |
$operations[CJTPlugin::DB_VERSION]['operations']['upgrade'] = CJTInstallerReflection::getInstance($upgrader['class'], 'CJTUpgradeNonTabledVersions')->getOperations(); |
| 108 |
} |
| 109 |
update_option(self::INSTALLATION_STATE, $operations); |
| 110 |
} |
| 111 |
return $operations[CJTPlugin::DB_VERSION]; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* put your comment there... |
| 116 |
* |
| 117 |
*/ |
| 118 |
public function getUpgrader() { |
| 119 |
// Upgrader file. |
| 120 |
$upgrader['file'] = "includes:installer:upgrade:{$this->getInstalledDbVersion()}:upgrade.class.php"; |
| 121 |
// Upgrader class name. |
| 122 |
$upgrader['class'] = "CJTV{$this->getInternalVersionName()}Upgrade"; |
| 123 |
return $upgrader; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Allow executing of a single installation operation! |
| 128 |
* Both Install and Upgrade operations can be executed throught here |
| 129 |
* |
| 130 |
* |
| 131 |
* @return void |
| 132 |
*/ |
| 133 |
public function install() { |
| 134 |
// Initialize. |
| 135 |
$result = FALSE; |
| 136 |
// Read input! |
| 137 |
$rOperation = $this->input['operation']; |
| 138 |
$type = $rOperation['type']; |
| 139 |
// Get allowed operations with their state! |
| 140 |
$operations = (array) get_option(self::INSTALLATION_STATE); |
| 141 |
$vOperations =& $operations[CJTPlugin::DB_VERSION]; |
| 142 |
// Invalid operation! |
| 143 |
if (!isset($vOperations['operations'][$type][$rOperation['name']])) { |
| 144 |
throw new Exception('Invalid operation'); |
| 145 |
} |
| 146 |
else { |
| 147 |
// Install only if not installed! |
| 148 |
$operation =& $vOperations['operations'][$type][$rOperation['name']]; |
| 149 |
if ((!isset($operation['state'])) || ($operation['state'] != self::OPERATION_STATE_INSTALLED)) { |
| 150 |
// Import installer and get installer object! |
| 151 |
switch ($type) { |
| 152 |
case 'install': |
| 153 |
cssJSToolbox::import('includes:installer:installer:installer.class.php'); |
| 154 |
$installer = CJTInstaller::getInstance(); |
| 155 |
break; |
| 156 |
case 'upgrade': |
| 157 |
$upgrader = $vOperations['upgrader']; |
| 158 |
cssJSToolbox::import($upgrader['file']); |
| 159 |
$installer = new $upgrader['class'](); |
| 160 |
break; |
| 161 |
} |
| 162 |
// Execute the requested operation, save state only when succesed! |
| 163 |
if ($installer->{$rOperation['name']}()) { |
| 164 |
$operation['state'] = self::OPERATION_STATE_INSTALLED; |
| 165 |
// Update operations cache to reflect the new state! |
| 166 |
update_option(self::INSTALLATION_STATE, $operations); |
| 167 |
// Say OK! |
| 168 |
$result = array('state' => self::OPERATION_STATE_INSTALLED); |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
return $result; |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* put your comment there... |
| 177 |
* |
| 178 |
*/ |
| 179 |
public function isCommonRelease() { |
| 180 |
// Check weather the current installed version is in the same |
| 181 |
// release with the running version. |
| 182 |
// Get instaleled version components. |
| 183 |
$ivComponent = explode('.', str_replace('-', '.', $this->getInstalledDbVersion())); |
| 184 |
$nvComponent = explode('.', str_replace('-', '.', CJTPlugin::DB_VERSION)); |
| 185 |
// Use PE-Edition sign if not sign used! |
| 186 |
if (!isset($ivComponent[2])) { |
| 187 |
$ivComponent[2] = 'PE'; |
| 188 |
} |
| 189 |
if (!isset($nvComponent[2])) { |
| 190 |
$nvComponent[2] = 'PE'; |
| 191 |
} |
| 192 |
// TRUE if same, FALSE otherwise. |
| 193 |
return (($ivComponent[0] == $nvComponent[0]) && ($ivComponent[2] == $nvComponent[2])); |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* put your comment there... |
| 198 |
* |
| 199 |
*/ |
| 200 |
public function isUpgrade() { |
| 201 |
// If the version is not the same and not equal to current version then its upgrading! |
| 202 |
$isUpgrade = (($this->installedDbVersion != CJTPlugin::DB_VERSION) && ($this->installedDbVersion != '')); |
| 203 |
return $isUpgrade; |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* put your comment there... |
| 208 |
* |
| 209 |
* @param mixed $input |
| 210 |
*/ |
| 211 |
public function setInput(& $input) { |
| 212 |
$this->input = $input; |
| 213 |
return $this; // Chaining! |
| 214 |
} |
| 215 |
|
| 216 |
} // End class. |
| 217 |
|