protected function removeModuleShowPagesRecursive(dmModule $module, array $modulesToCheck) { $moduleKey = $module->getKey(); if (!in_array($moduleKey, $modulesToCheck) || !$module->hasPage()) { foreach ($module->getChildren() as $child) { $this->removeModuleShowPagesRecursive($child, $modulesToCheck); } return; } $showPages = dmDb::pdo('SELECT p.id, p.module, p.record_id FROM dm_page p WHERE p.module = ? AND p.action = ?', array($moduleKey, 'show'))->fetchAll(PDO::FETCH_ASSOC); $showPageRecordIds = array(); foreach ($showPages as $showPage) { $showPageRecordIds[] = $showPage['record_id']; } if ($module->hasListPage()) { if (!empty($showPageRecordIds)) { $query = sprintf('SELECT r.id FROM %s r WHERE r.id IN (%s)', $module->getTable()->getTableName(), implode(',', $showPageRecordIds)); $records = array_flip(dmDb::pdo($query)->fetchAll(PDO::FETCH_COLUMN)); } else { $records = array(); } $parentModule = $module; $parentRecordIds = false; } else { $select = 'r.id'; if ($module->hasLocal($module->getParent())) { $select .= ', r.' . $module->getTable()->getRelationHolder()->getLocalByClass($module->getParent()->getModel())->getLocal(); } if (!empty($showPageRecordIds)) { $query = sprintf('SELECT %s FROM %s r WHERE r.id IN (%s)', $select, $module->getTable()->getTableName(), implode(',', $showPageRecordIds)); $_records = dmDb::pdo($query)->fetchAll(PDO::FETCH_ASSOC); $records = array(); foreach ($_records as $_record) { $records[$_record['id']] = $_record; } } else { $records = array(); } $parentModule = $module->getNearestAncestorWithPage(); $parentRecordIds = $this->getParentRecordIds($module, $parentModule); } foreach ($showPages as $showPage) { $pageIsUseless = false; if (!isset($records[$showPage['record_id']])) { $pageIsUseless = true; } elseif (!$module->hasListPage()) { $record = $records[$showPage['record_id']]; /* * If the parent is a show page, verify that it exists, * unless the child page is useless */ if ($parentRecordIds !== false) { $parentRecordId = isset($parentRecordIds[$record['id']]) ? $parentRecordIds[$record['id']] : null; } else { $parentRecordId = dmDb::create($module->getModel(), $record)->getAncestorRecordId($parentModule->getModel()); } if (!$parentRecordId) { $pageIsUseless = true; } } if ($pageIsUseless) { //delete node only if it's found if ($page = dmDb::table('DmPage')->find($showPage['id'])) { $page->getNode()->delete(); } } } foreach ($module->getChildren() as $child) { $this->removeModuleShowPagesRecursive($child, $modulesToCheck); } }