getTarget('part');
$action = $system->getTarget('action');
ob_start();
include (__DIR__ . __DS__ . 'page.' . $part . '.' . $action . '.php');
$response->setFragment('CONTENT', ob_get_clean());
}
/**
* generic module maintainance method for install, upgrade, uninstall and developement
* validation
*/
private function doModuleMaintainance($action) {
$system = System::getInstance();
$part = $system->getTarget('part');
$param = $system->getRequest()->getParam();
$CAT = 'co.mcms.mmpiadmincontent:modules';
$TYPE = $action;
$STEP = isset($param['step']) ? (int) $param['step'] : 1;
$MOD = $param['mod'];
unset($param);
ob_start();
$basepath = getModulePath($MOD);
include ($basepath . __DS__ . '_info_.php');
$setupFile = implode(__DS__, array(
$basepath,'_admin_','install','setup-' . $info['version'] . '.php'));
unset($param, $basepath, $part, $action);
if(file_exists($setupFile))
include ($setupFile);
else
echo 'setup file not found for version ' . $info['version'];
return ob_get_clean();
}
/**
* Common validate action helper.
* Compares all tables of the modules with their definitions and provides fixes for
* simple
* problems.
*
* @param string $basefile
* setup file name, most likely __FILE__
* @param string $mod
* module identifier
* @param string $version
* version of the module
*/
protected function doDefaultValidate($basefile, $mod, $version) {
$db = System::getInstance()->getDB();
$tablesdef = simplexml_load_file(dirname($basefile) . __DS__ . 'tables-' . $version . '.xml');
foreach($tablesdef as $tabledef) {
$tableInfo = \Mammut\DB\TableInfo::fromXML($tabledef);
$tableName = 'm_' . str_replace('.', '#', $mod) . '_' . $tableInfo->getName();
$tableInfo->setName($tableName); // we need to add the module prefix
echo "-- table defined: " . $tableInfo->getName() . ' (columns: ' . $tableInfo->getColumnCount() . "), ";
if($db->tableExists($tableInfo->getName())) {
$realTableInfo = $db->table($tableInfo->getName())->getInfo();
$diff = $tableInfo->compareTo($realTableInfo);
if(isset($diff->missingColumns))
echo '
---- missing columns: ' . implode(', ', $diff->missingColumns) . " getName() . "\"/>";
if(isset($diff->additionalColumns))
echo '
---- removeable columns: ' . implode(', ', $diff->additionalColumns) . " getName() . "\"/>";
if(isset($diff->differentColumns)) {
foreach($diff->differentColumns as $name=>$colDiffs) {
foreach($colDiffs as $type=>$colDiff) {
switch($type) {
case 'size':
echo '
--- different sizes of ' . $name . ':' . $colDiff['this'] . '!=' . $colDiff['other'];
break;
case 'type':
echo '
--- different types of ' . $name . ':' . $colDiff['this'] . '!=' . $colDiff['other'];
break;
case 'enum':
echo '
--- different enum options of ' . $name . ':' . implode(',', $colDiff['this']) . '!=' . implode(',', $colDiff['other']);
break;
}
}
}
}
if(isset($diff->missingColumns) || isset($diff->additionalColumns) || isset($diff->differentColumns))
echo "
mismatch! 
\n";
else
echo "found and ok 
\n";
}
else
echo "missing! check to create getName() . "\"/>
\n";
}
}
protected function doDefaultValidateFixes($basefile, $mod, $version) {
$ok = true;
echo 'fixing database...
';
$db = System::getInstance()->getDB();
$tablesdef = simplexml_load_file(dirname($basefile) . __DS__ . 'tables-' . $version . '.xml');
foreach($tablesdef as $tabledef) {
$tableInfo = \Mammut\DB\TableInfo::fromXML($tabledef);
$tableName = 'm_' . str_replace('.', '#', $mod) . '_' . $tableInfo->getName();
$tableInfo->setName($tableName); // we need to add the module prefix
if(isset($_REQUEST['createtable']) && in_array($tableInfo->getName(), $_REQUEST['createtable'])) {
echo "-- creating table : " . $tableInfo->getName();
try {
if($db->tableExists($tableInfo->getName()))
$db->dropTable($tableInfo->getName());
$db->createTable($tableInfo);
echo "... OK 
";
}
catch(\Mammut\DB\SQLException $e) {
echo "...error! 
" . $e->getMessage() . "
" . $e->getQuery() . '
';
}
}
if(isset($_REQUEST['createcolumns']) && in_array($tableInfo->getName(), $_REQUEST['createcolumns'])) {
$realTableInfo = $db->table($tableInfo->getName())->getInfo();
echo "-- addding columns to table : " . $tableInfo->getName() . '
';
try {
$missing = array();
$precol = false;
for($i = 0; $i < $tableInfo->getColumnCount(); $i++) {
$c1 = $tableInfo->getColumn($i);
$found = false;
for($j = 0; $j < $realTableInfo->getColumnCount(); $j++) {
$c2 = $realTableInfo->getColumn($j);
if($c1->getName() == $c2->getName())
$found = true;
}
if(!$found) {
echo 'adding column ' . $c1->getName() . ' ';
echo $precol ? ' after ' . $precol->getName() : ' at the beginning';
$db->table($tableInfo->getName())->addColumn($c1, $precol);
echo '
';
}
$precol = $c1;
}
echo "... OK 
";
}
catch(\Mammut\DB\SQLException $e) {
echo "...error! 
" . $e->getMessage() . "
" . $e->getQuery() . '
';
}
}
if(isset($_REQUEST['removecolumns']) && in_array($tableInfo->getName(), $_REQUEST['removecolumns'])) {
$realTableInfo = $db->table($tableInfo->getName())->getInfo();
echo "-- removing columns to table : " . $tableInfo->getName() . '
';
try {
$missing = array();
$precol = false;
for($i = 0; $i < $realTableInfo->getColumnCount(); $i++) {
$c1 = $realTableInfo->getColumn($i);
$found = false;
for($j = 0; $j < $tableInfo->getColumnCount(); $j++) {
$c2 = $tableInfo->getColumn($j);
if($c1->getName() == $c2->getName())
$found = true;
}
if(!$found) {
$db->table($tableInfo->getName())->removeColumn($c1->getName());
}
}
echo "... OK 
";
}
catch(\Mammut\DB\SQLException $e) {
echo "...error! 
" . $e->getMessage() . "
" . $e->getQuery() . '
';
}
}
}
echo 'finished!
';
return $ok;
}
public function runInstall(Response $response) {
$response->setFragment('CONTENT', $this->doModuleMaintainance('install'));
}
public function runUpgrade(Response $response) {
$response->setFragment('CONTENT', $this->doModuleMaintainance('upgrade'));
}
public function runValidate(Response $response) {
if(!defined('DEBUG'))
throw new \BadMethodCallException('validate is only avaible in debug mode');
$response->setFragment('CONTENT', $this->doModuleMaintainance('validate'));
}
public function runUninstall(Response $response) {
$response->setFragment('CONTENT', $this->doModuleMaintainance('uninstall'));
}
public function runSettings(Response $response) {
$system = System::getInstance();
$db = $system->getDB();
$param = $system->getRequest()->getParam();
$mod = $param['mod'];
if($mod) {
$modInfo = getModuleInfo($mod);
if($modInfo['admin_path']) {
include ($modInfo['admin_path'] . __DS__ . 'moda.' . $modInfo['modname'] . '.php');
$admobj = 'MMMDA' . ucfirst($modInfo['modname']);
$admobj = new $admobj();
$admobj->handleIAction($response);
}
else {
echo 'Missing admin class';
}
}
else
echo 'Invalid module name, does not exist';
}
}