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 PimPluginSetupUninstall extends PimPluginSetup {
24: 25: 26: 27: 28:
29: private $_PluginFoldername;
30:
31: 32: 33:
34: protected $_PimPluginCollection;
35:
36: 37: 38:
39: protected $_PimPluginRelationsCollection;
40:
41: 42: 43:
44: protected $_ApiAreaCollection;
45:
46: 47: 48:
49: protected $_ApiActionCollection;
50:
51: 52: 53:
54: protected $_ApiFileCollection;
55:
56: 57: 58:
59: protected $_ApiFrameFileCollection;
60:
61: 62: 63:
64: protected $_ApiNavMainCollection;
65:
66: 67: 68:
69: protected $_ApiNavSubCollection;
70:
71: 72: 73:
74: protected $_ApiTypeCollection;
75:
76:
77:
78: 79: 80: 81: 82: 83:
84: public function setPluginFoldername($foldername) {
85: return $this->_PluginFoldername = cSecurity::escapeString($foldername);
86: }
87:
88: 89: 90: 91: 92:
93: private function _setPimPluginCollection() {
94: return $this->_PimPluginCollection = new PimPluginCollection();
95: }
96:
97: 98: 99: 100: 101:
102: private function _setPimPluginRelationsCollection() {
103: return $this->_PimPluginRelationsCollection = new PimPluginRelationsCollection();
104: }
105:
106: 107: 108: 109: 110:
111: private function _setApiAreaCollection() {
112: return $this->_ApiAreaCollection = new cApiAreaCollection();
113: }
114:
115: 116: 117: 118: 119:
120: private function _setApiActionCollection() {
121: return $this->_ApiActionCollection = new cApiActionCollection();
122: }
123:
124: 125: 126: 127: 128:
129: private function _setApiFileCollection() {
130: return $this->_ApiFileCollection = new cApiFileCollection();
131: }
132:
133: 134: 135: 136: 137:
138: private function _setApiFrameFileCollection() {
139: return $this->_ApiFrameFileCollection = new cApiFrameFileCollection();
140: }
141:
142: 143: 144: 145: 146:
147: private function _setApiNavMainCollection() {
148: return $this->_ApiNavMainCollection = new cApiNavMainCollection();
149: }
150:
151: 152: 153: 154: 155:
156: private function _setApiNavSubCollection() {
157: return $this->_ApiNavSubCollection = new cApiNavSubCollection();
158: }
159:
160: 161: 162: 163: 164:
165: private function _setApiTypeCollection() {
166: return $this->_ApiTypeCollection = new cApiTypeCollection();
167: }
168:
169: 170: 171: 172: 173:
174: protected function _getPluginFoldername() {
175: return $this->_PluginFoldername;
176: }
177:
178:
179: 180: 181:
182: public function __construct() {
183:
184:
185:
186: $this->_setPimPluginCollection();
187: $this->_setPimPluginRelationsCollection();
188:
189:
190: $this->_setApiAreaCollection();
191: $this->_setApiActionCollection();
192: $this->_setApiFileCollection();
193: $this->_setApiFrameFileCollection();
194: $this->_setApiNavMainCollection();
195: $this->_setApiNavSubCollection();
196: $this->_setApiTypeCollection();
197: }
198:
199: 200: 201: 202: 203: 204: 205: 206: 207:
208: public function uninstall($sql = true) {
209:
210:
211: $this->_uninstallCheckDependencies();
212:
213:
214: $this->_PimPluginRelationsCollection->setWhere('idplugin', parent::_getPluginId());
215: $this->_PimPluginRelationsCollection->query();
216:
217:
218: $relations = array();
219:
220: while (($relation = $this->_PimPluginRelationsCollection->next()) !== false) {
221:
222:
223: $index = $relation->get('type');
224:
225:
226:
227: $value = $relation->get('iditem');
228: $relations[$index][] = $value;
229: }
230:
231:
232: if (!empty($relations['action'])) {
233: $this->_ApiActionCollection->deleteByWhereClause("idaction IN('" . join("', '", $relations['action']) . "')");
234: }
235:
236:
237: if (!empty($relations['framefl'])) {
238: $this->_ApiFrameFileCollection->deleteByWhereClause("idframefile IN('" . join("', '", $relations['framefl']) . "')");
239: }
240:
241:
242: if (!empty($relations['area'])) {
243: $this->_ApiFileCollection->deleteByWhereClause("idarea IN('" . join("', '", $relations['area']) . "')");
244: $this->_ApiFrameFileCollection->deleteByWhereClause("idarea IN('" . join("', '", $relations['area']) . "')");
245: $this->_ApiAreaCollection->deleteByWhereClause("idarea IN('" . join("', '", $relations['area']) . "')");
246: }
247:
248:
249: if (!empty($relations['navm'])) {
250: $this->_ApiNavMainCollection->deleteByWhereClause("idnavm IN('" . join("', '", $relations['navm']) . "')");
251: }
252:
253:
254: if (!empty($relations['navs'])) {
255: $this->_ApiNavSubCollection->deleteByWhereClause("idnavs IN('" . join("', '", $relations['navs']) . "')");
256: }
257:
258:
259: if (!empty($relations['ctype'])) {
260: $this->_ApiTypeCollection->deleteByWhereClause("idtype IN('" . join("', '", $relations['ctype']) . "')");
261: }
262:
263:
264: $this->_PimPluginCollection->resetQuery();
265: $this->_PimPluginCollection->setWhere('idplugin', parent::_getPluginId());
266: $this->_PimPluginCollection->query();
267: $pimPluginSql = $this->_PimPluginCollection->next();
268:
269:
270: $this->setPluginFoldername($pimPluginSql->get('folder'));
271:
272:
273:
274: if ($sql == true && PimPluginSetup::_getUpdateSqlFileExist() == false) {
275: $this->_uninstallDeleteSpecificSql();
276: }
277:
278:
279: $pluginname = $pimPluginSql->get('name');
280:
281:
282: $this->_PimPluginRelationsCollection->deleteByWhereClause('idplugin = ' . parent::_getPluginId());
283: $this->_PimPluginCollection->deleteByWhereClause('idplugin = ' . parent::_getPluginId());
284:
285:
286: $this->_writeNewExecutionOrder();
287:
288:
289: if (parent::$_GuiPage instanceof cGuiPage && parent::getMode() == 3) {
290: parent::info(sprintf(i18n('The plugin <strong>%s</strong> has been successfully removed. To apply the changes please login into backend again.', 'pim'), $pluginname));
291: }
292: }
293:
294: 295: 296: 297: 298: 299:
300: private function _uninstallCheckDependencies() {
301:
302:
303:
304: $result = $this->checkDependencies();
305:
306:
307: if ($result === false) {
308: parent::error(sprintf(i18n('This plugin is required by the plugin <strong>%s</strong>, so you can not remove it.', 'pim'), parent::_getPluginName()));
309: }
310: }
311:
312: 313: 314: 315: 316: 317:
318: protected function _uninstallDeleteSpecificSql() {
319:
320: $cfg = cRegistry::getConfig();
321: $db = cRegistry::getDb();
322:
323: $tempSqlFilename = $cfg['path']['contenido'] . $cfg['path']['plugins'] . $this->_getPluginFoldername() . DIRECTORY_SEPARATOR . 'plugin_uninstall.sql';
324:
325: if (!cFileHandler::exists($tempSqlFilename)) {
326: return;
327: }
328:
329: $tempSqlContent = cFileHandler::read($tempSqlFilename);
330: $tempSqlContent = str_replace("\r\n", "\n", $tempSqlContent);
331: $tempSqlContent = explode("\n", $tempSqlContent);
332: $tempSqlLines = count($tempSqlContent);
333:
334: $pattern = '/^(DELETE FROM|DROP TABLE) `?' . parent::SQL_PREFIX . '`?\b/';
335:
336: for ($i = 0; $i < $tempSqlLines; $i++) {
337: if (preg_match($pattern, $tempSqlContent[$i])) {
338: $tempSqlContent[$i] = str_replace(parent::SQL_PREFIX, $cfg['sql']['sqlprefix'] . '_pi', $tempSqlContent[$i]);
339: $db->query($tempSqlContent[$i]);
340: }
341: }
342: }
343:
344: 345: 346: 347: 348: 349:
350: public function uninstallDir() {
351:
352: $cfg = cRegistry::getConfig();
353:
354:
355: $folderpath = $cfg['path']['contenido'] . $cfg['path']['plugins'] . $this->_getPluginFoldername();
356: cDirHandler::recursiveRmdir($folderpath);
357:
358: if (parent::$_GuiPage instanceof cGuiPage) {
359:
360:
361: if (!cFileHandler::exists($folderpath)) {
362: parent::info(sprintf(i18n('The pluginfolder <strong>%s</strong> has been successfully uninstalled.', 'pim'), $this->_getPluginFoldername()));
363: } elseif (cFileHandler::exists($folderpath)) {
364: parent::error(sprintf(i18n('The pluginfolder <strong>%s</strong> could not be uninstalled.', 'pim'), $this->_getPluginFoldername()));
365: }
366: }
367: }
368:
369: 370: 371: 372: 373: 374: 375: 376:
377: protected function _writeNewExecutionOrder() {
378:
379:
380: $i = 1;
381:
382: $pimPluginColl = new PimPluginCollection();
383: $pimPluginColl->setOrder('executionorder ASC');
384: $pimPluginColl->query();
385: while ($pimPluginSql = $pimPluginColl->next()) {
386: $pimPluginSql->set('executionorder', $i);
387: $pimPluginSql->store();
388:
389: $i++;
390: }
391:
392: return true;
393: }
394:
395: }
396: