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