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: $cfg = cRegistry::getConfig();
188:
189:
190: $this->_uninstallCheckDependencies();
191:
192:
193: $this->_PimPluginRelationsCollection->setWhere('idplugin', parent::_getPluginId());
194: $this->_PimPluginRelationsCollection->query();
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: $cfg = cRegistry::getConfig();
292: $db = cRegistry::getDb();
293:
294: $tempSqlFilename = $cfg['path']['contenido'] . $cfg['path']['plugins'] . $this->_getPluginFoldername() . DIRECTORY_SEPARATOR . 'plugin_uninstall.sql';
295:
296: if (!cFileHandler::exists($tempSqlFilename)) {
297: return;
298: }
299:
300: $tempSqlContent = cFileHandler::read($tempSqlFilename);
301: $tempSqlContent = str_replace("\r\n", "\n", $tempSqlContent);
302: $tempSqlContent = explode("\n", $tempSqlContent);
303: $tempSqlLines = count($tempSqlContent);
304:
305: $pattern = '/^(DELETE FROM|DROP TABLE) `?' . parent::SQL_PREFIX . '`?\b/';
306:
307: for ($i = 0; $i < $tempSqlLines; $i++) {
308: if (preg_match($pattern, $tempSqlContent[$i])) {
309: $tempSqlContent[$i] = str_replace(parent::SQL_PREFIX, $cfg['sql']['sqlprefix'] . '_pi', $tempSqlContent[$i]);
310: $db->query($tempSqlContent[$i]);
311: }
312: }
313: }
314:
315: 316: 317:
318: public function uninstallDir() {
319: $cfg = cRegistry::getConfig();
320:
321:
322: $folderpath = $cfg['path']['contenido'] . $cfg['path']['plugins'] . $this->_getPluginFoldername();
323: cDirHandler::recursiveRmdir($folderpath);
324:
325: if (parent::$_GuiPage instanceof cGuiPage) {
326:
327:
328: if (!cFileHandler::exists($folderpath)) {
329: parent::info(sprintf(i18n('The pluginfolder <strong>%s</strong> has been successfully uninstalled.', 'pim'), $this->_getPluginFoldername()));
330: } else if (cFileHandler::exists($folderpath)) {
331: parent::error(sprintf(i18n('The pluginfolder <strong>%s</strong> could not be uninstalled.', 'pim'), $this->_getPluginFoldername()));
332: }
333: }
334: }
335:
336: 337: 338: 339: 340:
341: protected function _writeNewExecutionOrder() {
342:
343:
344: $i = 1;
345:
346: $pimPluginColl = new PimPluginCollection();
347: $pimPluginColl->setOrder('executionorder ASC');
348: $pimPluginColl->query();
349: while ($pimPluginSql = $pimPluginColl->next()) {
350: $pimPluginSql->set('executionorder', $i);
351: $pimPluginSql->store();
352:
353: $i++;
354: }
355:
356: return true;
357: }
358:
359: }
360: ?>