/**
* Process themes method
*
* @param array $post
* @return void
*/
public function processModules($post)
{
foreach ($post as $key => $value) {
if (strpos($key, 'module_active_') !== false) {
$id = substr($key, strrpos($key, '_') + 1);
$ext = Table\Extensions::findById($id);
if (isset($ext->id)) {
$ext->active = (int) $value;
$ext->save();
}
}
}
$path = BASE_PATH . APP_URI;
if ($path == '') {
$path = '/';
}
$modulePath1 = $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/extensions/modules';
$modulePath2 = __DIR__ . '/../../../../../module';
$phireCookie = null;
if (php_sapi_name() != 'cli') {
$cookie = Cookie::getInstance(array('path' => $path));
if (isset($cookie->phire)) {
if (null === $phireCookie) {
$phireCookie = $cookie->phire;
}
}
}
if (isset($post['remove_modules'])) {
foreach ($post['remove_modules'] as $id) {
$ext = Table\Extensions::findById($id);
if (isset($ext->id)) {
$modPath = file_exists($modulePath1 . '/' . $ext->file) ? $modulePath1 : $modulePath2;
$assets = unserialize($ext->assets);
if (count($assets['tables']) > 0) {
$db = Table\Extensions::getDb();
if (DB_INTERFACE == 'Mysqli' || DB_TYPE == 'mysql') {
$db->adapter()->query('SET foreign_key_checks = 0;');
foreach ($assets['tables'] as $table) {
$db->adapter()->query('DROP TABLE ' . $table);
}
$db->adapter()->query('SET foreign_key_checks = 1;');
} else {
if (DB_INTERFACE == 'Pgsql' || DB_TYPE == 'pgsql') {
foreach ($assets['tables'] as $table) {
$db->adapter()->query('DROP TABLE ' . $table . ' CASCADE');
}
} else {
foreach ($assets['tables'] as $table) {
$db->adapter()->query('DROP TABLE ' . $table);
}
}
}
}
$contentPath = $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH;
$exts = array('.zip', '.tar.gz', '.tar.bz2', '.tgz', '.tbz', '.tbz2');
// Check for a config and remove function
if (file_exists($modPath . '/' . $ext->name . '/config') && file_exists($modPath . '/' . $ext->name . '/config/module.php')) {
$config = (include $modPath . '/' . $ext->name . '/config/module.php');
if (null !== $config[$ext->name]->remove) {
$removeFunc = $config[$ext->name]->remove;
$removeFunc();
}
}
if (file_exists($contentPath . '/extensions/modules/' . $ext->name)) {
$dir = new Dir($contentPath . '/extensions/modules/' . $ext->name);
$dir->emptyDir(null, true);
}
foreach ($exts as $e) {
if (file_exists($contentPath . '/extensions/modules/' . $ext->name . $e) && is_writable($contentPath . '/extensions/modules/' . $ext->name . $e)) {
unlink($contentPath . '/extensions/modules/' . $ext->name . $e);
}
}
if (file_exists(__DIR__ . '/../../../../../module/' . $ext->name)) {
$dir = new Dir(__DIR__ . '/../../../../../module/' . $ext->name);
$dir->emptyDir(null, true);
}
foreach ($exts as $e) {
if (file_exists(__DIR__ . '/../../../../../module/' . $ext->name . $e) && is_writable(__DIR__ . '/../../../../../module/' . $ext->name . $e)) {
unlink(__DIR__ . '/../../../../../module/' . $ext->name . $e);
}
}
if (file_exists($contentPath . '/assets/' . strtolower($ext->name))) {
$dir = new Dir($contentPath . '/assets/' . strtolower($ext->name));
$dir->emptyDir(null, true);
}
if (null !== $phireCookie) {
foreach ($phireCookie->modules as $key => $value) {
if ($value->name == $ext->name) {
$modules = (array) $phireCookie->modules;
unset($modules[$key]);
$phireCookie->modules = $modules;
}
}
}
$ext->delete();
}
}
}
if (null !== $phireCookie) {
$cookie = Cookie::getInstance(array('path' => $path));
$cookie->set('phire', $phireCookie);
}
}