1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13:
14: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
15:
16: 17: 18: 19: 20: 21: 22:
23: class PimPluginSetupUpdate extends PimPluginSetup {
24:
25:
26: 27: 28: 29: 30:
31: protected $_PimPluginCollection;
32:
33: 34: 35: 36: 37:
38: private function _setPimPluginCollection() {
39: return $this->_PimPluginCollection = new PimPluginCollection();
40: }
41:
42:
43:
44: 45: 46: 47: 48: 49: 50:
51: public function __construct() {
52:
53:
54:
55: $this->_setPimPluginCollection();
56:
57:
58: $this->_checkSamePlugin();
59:
60:
61: $this->_updateSql();
62:
63:
64: $delete = new PimPluginSetupUninstall();
65: $delete->uninstall();
66:
67:
68: $new = new PimPluginSetupInstall();
69: $new->install();
70:
71:
72: parent::info(i18n('The plugin has been successfully updated. To apply the changes please login into backend again.', 'pim'));
73: }
74:
75: 76: 77: 78: 79:
80: private function _checkSamePlugin() {
81: $this->_PimPluginCollection->setWhere('idplugin', parent::_getPluginId());
82: $this->_PimPluginCollection->query();
83: while ($result = $this->_PimPluginCollection->next()) {
84:
85: if (parent::$XmlGeneral->uuid != $result->get('uuid')) {
86: parent::error(i18n('You have to update the same plugin', 'pim'));
87: }
88: }
89: }
90:
91: 92: 93: 94: 95: 96: 97: 98: 99: 100:
101: private function _updateSql() {
102:
103: $cfg = cRegistry::getConfig();
104: $db = cRegistry::getDb();
105:
106:
107:
108: $tempSqlFilename = "plugin_update_" . str_replace('.', '', $this->_getInstalledPluginVersion()) . "_to_" . str_replace('.', '', parent::$XmlGeneral->version) . ".sql";
109:
110:
111: $tempSqlFilename = parent::$_PimPluginArchiveExtractor->extractArchiveFileToVariable($tempSqlFilename, 0);
112:
113: if (cFileHandler::exists($tempSqlFilename)) {
114:
115:
116: $tempSqlContent = cFileHandler::read($tempSqlFilename);
117: $tempSqlContent = str_replace("\r\n", "\n", $tempSqlContent);
118: $tempSqlContent = explode("\n", $tempSqlContent);
119: $tempSqlLines = count($tempSqlContent);
120:
121: $pattern = '/^(CREATE TABLE IF NOT EXISTS|INSERT INTO|UPDATE|ALTER TABLE) `?' . parent::SQL_PREFIX . '`?\b/';
122:
123: for ($i = 0; $i < $tempSqlLines; $i++) {
124: if (preg_match($pattern, $tempSqlContent[$i])) {
125: $tempSqlContent[$i] = str_replace(parent::SQL_PREFIX, $cfg['sql']['sqlprefix'] . '_pi', $tempSqlContent[$i]);
126: $db->query($tempSqlContent[$i]);
127: }
128: }
129:
130:
131: PimPluginSetup::_setUpdateSqlFileExist(true);
132: } else {
133: return false;
134: }
135: }
136:
137: 138: 139: 140: 141:
142: private function _getInstalledPluginVersion() {
143: $this->_PimPluginCollection->setWhere('idplugin', parent::_getPluginId());
144: $this->_PimPluginCollection->query();
145: while ($result = $this->_PimPluginCollection->next()) {
146: return $result->get('version');
147: }
148: }
149:
150: }
151: