function enable_disable_system_modules() { ajx_current("empty"); if (!can_manage_configuration(logged_user())) { flash_error(lang('no access permissions')); ajx_current("empty"); return; } $module_list = json_decode(array_var($_REQUEST, 'modules'), true); try { DB::beginWork(); foreach ($module_list as $module_id => $enabled) { $tab_panel = TabPanels::instance()->findById($module_id); if ($tab_panel instanceof TabPanel) { $tab_panel->setEnabled($enabled > 0); $tab_panel->save(); if ($enabled > 0) { DB::execute("INSERT INTO " . TABLE_PREFIX . "tab_panel_permissions (permission_group_id, tab_panel_id) VALUES (" . logged_user()->getPermissionGroupId() . ",'" . $tab_panel->getId() . "') ON DUPLICATE KEY UPDATE tab_panel_id=tab_panel_id;"); } if ($tab_panel->getPluginId() > 0) { $plugin = Plugins::findById($tab_panel->getPluginId()); if ($plugin instanceof Plugin) { if ($enabled) { $plugin->activate(); } else { $plugin->deactivate(); } } } } } DB::commit(); } catch (Exception $e) { DB::rollback(); Logger::log("Error occurred when trying to enable/disable modules\n" . $e->getMessage() . "\n" . print_r($module_list, 1)); } }
function update_plugins() { trace(__FILE__, 'update_plugins()'); $plugins = array_var($_POST, 'plugins'); //$plugins = array('invoices' => '1'); if (is_null($plugins)) { $this->redirectTo('administration', 'plugins'); } $reference = Plugins::getAllPlugins(); trace(__FILE__, 'update_plugins() - getAllPlugins done'); $errors = array(); foreach ($plugins as $name => $yes_no) { //If it is not a plugin continue $plugin_file_path = APPLICATION_PATH . '/plugins/' . $name . '/init.php'; trace(__FILE__, "update_plugins() - plugin_file_path=[{$plugin_file_path}]"); if (!file_exists($plugin_file_path)) { continue; } trace(__FILE__, "update_plugins() - plugin_file_path=[{$plugin_file_path}] exists"); // get existing id $id = $reference[$name]; $nicename = ucwords(str_replace('_', ' ', $name)); trace(__FILE__, "update_plugins() - id={$id} name={$name} nicename={$nicename}"); if ($yes_no && '-' == $id) { trace(__FILE__, "update_plugins() - activating [{$name}]"); try { // Check if plugin exists in database $plugin = Plugins::findOne(array('conditions' => array('`name` = ?', $name))); trace(__FILE__, "update_plugins() - findOne"); if ($plugin == NULL) { $plugin = new Plugin(); } trace(__FILE__, "update_plugins() - setName"); $plugin->setName($name); trace(__FILE__, "update_plugins() - setInstalled"); $plugin->setInstalled(true); DB::beginWork(); trace(__FILE__, "update_plugins() - including [{$plugin_file_path}]"); // get the file loaded here include $plugin_file_path; // get activation routine ready trace(__FILE__, "update_plugins() - activate: {$name}"); $activate = $name . '_activate'; if (function_exists($activate)) { trace(__FILE__, "update_plugins() - calling: {$activate}"); $activate(); } // save to db now $plugin->save(); DB::commit(); } catch (Exception $e) { trace(__FILE__, 'update_plugins() - ' . $e->getMessage()); DB::rollback(); $errors[] = $nicename . ' (' . $e->getMessage() . ')'; } } elseif (!$yes_no && '-' != $id) { try { trace(__FILE__, "update_plugins() - deactivating [{$name}]"); $plugin = Plugins::findById($id); DB::beginWork(); $deactivate = $name . '_deactivate'; if (function_exists($deactivate)) { trace(__FILE__, "update_plugins() - calling {$deactivate}"); //Check if user choose to purge data if ($plugins[$name . "_data"] == "0") { trace(__FILE__, "update_plugins() - calling {$deactivate}(true)"); $deactivate(true); } else { $deactivate(); } } $plugin->setInstalled(false); $plugin->save(); DB::commit(); } catch (Exception $e) { DB::rollback(); $errors[] = $nicename . ' (' . $e->getMessage() . ')'; } } } if (count($errors)) { flash_error(lang('plugin activation failed', implode(", ", $errors))); } else { flash_success(lang('plugins updated')); } $this->redirectTo('administration', 'plugins'); }