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