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: public function __construct() {
47:
48:
49:
50: $this->_setPimPluginCollection();
51:
52:
53: $this->_checkSamePlugin();
54:
55:
56: $this->_updateSql();
57:
58:
59: $delete = new PimPluginSetupUninstall();
60: $delete->uninstall();
61:
62:
63: $new = new PimPluginSetupInstall();
64: $new->install();
65:
66:
67: parent::info(i18n('The plugin has been successfully updated. To apply the changes please login into backend again.', 'pim'));
68: }
69:
70: 71: 72:
73: private function _checkSamePlugin() {
74: $this->_PimPluginCollection->setWhere('idplugin', parent::_getPluginId());
75: $this->_PimPluginCollection->query();
76: while ($result = $this->_PimPluginCollection->next()) {
77:
78: if (parent::$XmlGeneral->uuid != $result->get('uuid')) {
79: parent::error(i18n('You have to update the same plugin', 'pim'));
80: }
81: }
82: }
83:
84: 85: 86: 87: 88:
89: private function _updateSql() {
90: $cfg = cRegistry::getConfig();
91: $db = cRegistry::getDb();
92:
93:
94:
95: $tempSqlFilename = "plugin_update_" . str_replace('.', '', $this->_getInstalledPluginVersion()) . "_to_" . str_replace('.', '', parent::$XmlGeneral->version) . ".sql";
96:
97:
98: $tempSqlFilename = parent::$_PimPluginArchiveExtractor->extractArchiveFileToVariable($tempSqlFilename, 0);
99:
100: if (cFileHandler::exists($tempSqlFilename)) {
101:
102:
103: $tempSqlContent = cFileHandler::read($tempSqlFilename);
104: $tempSqlContent = str_replace("\r\n", "\n", $tempSqlContent);
105: $tempSqlContent = explode("\n", $tempSqlContent);
106: $tempSqlLines = count($tempSqlContent);
107:
108: $pattern = '/^(CREATE TABLE IF NOT EXISTS|INSERT INTO|UPDATE|ALTER TABLE) `?' . parent::SQL_PREFIX . '`?\b/';
109:
110: for ($i = 0; $i < $tempSqlLines; $i++) {
111: if (preg_match($pattern, $tempSqlContent[$i])) {
112: $tempSqlContent[$i] = str_replace(parent::SQL_PREFIX, $cfg['sql']['sqlprefix'] . '_pi', $tempSqlContent[$i]);
113: $db->query($tempSqlContent[$i]);
114: }
115: }
116:
117:
118: PimPluginSetup::_setUpdateSqlFileExist(true);
119: } else {
120: return false;
121: }
122: }
123:
124: 125: 126:
127: private function _getInstalledPluginVersion() {
128: $this->_PimPluginCollection->setWhere('idplugin', parent::_getPluginId());
129: $this->_PimPluginCollection->query();
130: while ($result = $this->_PimPluginCollection->next()) {
131: return $result->get('version');
132: }
133: }
134:
135: }