| 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('.', '', $this->installedDbVersion); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* put your comment there... |
| 84 |
* |
| 85 |
*/ |
| 86 |
public function getOperations() { |
| 87 |
// If installation is didn't never run before thise would be unset! |
| 88 |
if (!($operations = get_option(self::INSTALLATION_STATE))) { |
| 89 |
// Import installer reflection! |
| 90 |
cssJSToolbox::import('framework:installer:reflection.class.php'); |
| 91 |
// Get Installer operations. |
| 92 |
cssJSToolbox::import('includes:installer:installer:installer.class.php'); |
| 93 |
$operations['operations']['install'] = CJTInstallerReflection::getInstance('CJTInstaller', 'CJTInstaller')->getOperations(); |
| 94 |
if ($this->isUpgrade()) { |
| 95 |
// Get upgrade operations , Also cache upgrader info for later use! |
| 96 |
$operations['upgrader'] = $upgrader = $this->getUpgrader(); |
| 97 |
cssJSToolbox::import($upgrader['file']); |
| 98 |
$operations['operations']['upgrade'] = CJTInstallerReflection::getInstance($upgrader['class'], 'CJTUpgradeNonTabledVersions')->getOperations(); |
| 99 |
} |
| 100 |
// Cache operations! |
| 101 |
update_option(self::INSTALLATION_STATE, $operations); |
| 102 |
} |
| 103 |
return $operations; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* put your comment there... |
| 108 |
* |
| 109 |
*/ |
| 110 |
public function getUpgrader() { |
| 111 |
// Upgrader file. |
| 112 |
$upgrader['file'] = "includes:installer:upgrade:{$this->getInstalledDbVersion()}:upgrade.class.php"; |
| 113 |
// Upgrader class name. |
| 114 |
$upgrader['class'] = "CJTV{$this->getInternalVersionName()}Upgrade"; |
| 115 |
return $upgrader; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Allow executing of a single installation operation! |
| 120 |
* Both Install and Upgrade operations can be executed throught here |
| 121 |
* |
| 122 |
* |
| 123 |
* @return void |
| 124 |
*/ |
| 125 |
public function install() { |
| 126 |
// Read input! |
| 127 |
$rOperation = $this->input['operation']; |
| 128 |
$type = $rOperation['type']; |
| 129 |
// Get allowed operations with thier state! |
| 130 |
$operations = (array) get_option(self::INSTALLATION_STATE); |
| 131 |
// Invalid operation! |
| 132 |
if (!isset($operations['operations'][$type][$rOperation['name']])) { |
| 133 |
throw new Exception('Invalid operation'); |
| 134 |
} |
| 135 |
else { |
| 136 |
// Install only if not installed! |
| 137 |
$operation =& $operations['operations'][$type][$rOperation['name']]; |
| 138 |
if ($operation['state'] != self::OPERATION_STATE_INSTALLED) { |
| 139 |
// Import installer and get installer object! |
| 140 |
switch ($type) { |
| 141 |
case 'install': |
| 142 |
cssJSToolbox::import('includes:installer:installer:installer.class.php'); |
| 143 |
$installer = CJTInstaller::getInstance(); |
| 144 |
break; |
| 145 |
case 'upgrade': |
| 146 |
$upgrader = $operations['upgrader']; |
| 147 |
cssJSToolbox::import($upgrader['file']); |
| 148 |
$installer = new $upgrader['class'](); |
| 149 |
break; |
| 150 |
} |
| 151 |
// Execute the requested operation, save state only when succesed! |
| 152 |
if ($installer->{$rOperation['name']}()) { |
| 153 |
$operation['state'] = self::OPERATION_STATE_INSTALLED; |
| 154 |
// Update operations cache to reflect the new state! |
| 155 |
update_option(self::INSTALLATION_STATE, $operations); |
| 156 |
// Say OK! |
| 157 |
$this->response = array('state' => self::OPERATION_STATE_INSTALLED); |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* put your comment there... |
| 165 |
* |
| 166 |
*/ |
| 167 |
public function isUpgrade() { |
| 168 |
// If the version is not the same and not equal to current version then its upgrading! |
| 169 |
$isUpgrade = (($this->installedDbVersion != CJTPlugin::DB_VERSION) && ($this->installedDbVersion != '')); |
| 170 |
return $isUpgrade; |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* put your comment there... |
| 175 |
* |
| 176 |
* @param mixed $input |
| 177 |
*/ |
| 178 |
public function setInput(& $input) { |
| 179 |
$this->input = $input; |
| 180 |
return $this; // Chaining! |
| 181 |
} |
| 182 |
|
| 183 |
} // End class. |
| 184 |
|