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