/** * execute command * * @param InputInterface $input * @param OutputInterface $output */ public function execute(InputInterface $input, OutputInterface $output) { $names = $input->getOption('names'); if ($names == null) { $output->writeln('<error>Sorry, you did not enter module folder name.</error>'); unset($names); return false; } if ($names != null) { $names_exp = explode(',', str_replace(', ', ',', $names)); $style = new \Symfony\Component\Console\Formatter\OutputFormatterStyle('white', 'green'); $output->getFormatter()->setStyle('success', $style); unset($style); if (is_array($names_exp) && !empty($names_exp)) { foreach ($names_exp as $name) { if ($name != null) { $output->write('Module "' . $name . '": '); // check module exists in db. $options['module_system_name'] = $name; $ModulesDb = new \System\Core\Models\ModulesDb($this->Db); $module_data = $ModulesDb->getModule($options); unset($ModulesDb, $options); if ($module_data == null) { $output->writeln('<error>module is not exists in database.</error>'); } else { $sql = 'DELETE `m`, `ms`'; $sql .= ' FROM `' . $this->Db->getTableName('modules') . '` AS `m`'; $sql .= ' LEFT JOIN `' . $this->Db->getTableName('module_sites') . '` AS `ms` ON m.module_id = ms.module_id'; $sql .= ' WHERE `m`.`module_system_name` = :module_system_name'; $stmt = $this->Db->Conn->prepare($sql); $stmt->bindValue('module_system_name', $name, \PDO::PARAM_STR); $result = $stmt->execute(); $stmt->closeCursor(); unset($sql, $stmt); $output->writeln('<success>deleted successfully.</success>'); } unset($module_data); } // endif $name not null. } // endforeach; } // endif is array $name_exp } // also clear cache about this thing. --------------------------- $Command = $this->getApplication()->find('System\\Core\\Console\\Cache'); $arguments = ['command' => 'System\\Core\\Console\\Cache', '--subfolder' => 'system/Libraries/Modules.php']; $CmdInput = new \Symfony\Component\Console\Input\ArrayInput($arguments); $Command->run($CmdInput, new \Symfony\Component\Console\Output\NullOutput()); unset($arguments, $CmdInput, $Command); // end clear cache ------------------------------------------------ unset($name, $names, $names_exp, $result); }
/** * prepare to add module for multiple sites. * * @param \Symfony\Component\Console\Output\OutputInterface $output symfony output interface. * @param string $module_system_name module systen name. * @param array $module_metadata module metadata. * @param array $site_ids array of site id that will be enabled. * @return integer return number of sites per module that were added. */ private function prepareAddModuleSites(OutputInterface $output, $module_system_name, array $module_metadata, array $site_ids = []) { $output_count = 0; $output_subtracted = 0; // get all site ids for checking and adding. ---------------- $options['list_for'] = 'admin'; $options['unlimited'] = true; $SitesDb = new \System\Core\Models\SitesDb($this->Db); $list_sites = $SitesDb->listSites($options); unset($options); $sites = []; if (is_array($list_sites) && array_key_exists('items', $list_sites)) { foreach ($list_sites['items'] as $row) { $sites = array_merge($sites, [$row->site_id]); } unset($row); } unset($list_sites, $SitesDb); // check module exists or not ------------------------------- $options['module_system_name'] = $module_system_name; $ModulesDb = new \System\Core\Models\ModulesDb($this->Db); $module = $ModulesDb->getModule($options); unset($ModulesDb, $options); $module_id = null; $module_site_ids = []; if ($module == null || empty($module)) { // module not exists. $module_id = null; $module_site_ids = $site_ids; } else { // module exists. $module_id = $module->module_id; $module_site_ids = $site_ids; if (is_array($module) || is_object($module) && property_exists($module, 'module_sites') && is_array($module->module_sites)) { foreach ($module->module_sites as $row) { if (in_array($row->site_id, $site_ids)) { // current site_id in module_sites table is in add list. $module_site_ids = array_diff($module_site_ids, [$row->site_id]); $output_subtracted++; } } // endforeach; unset($row); } } unset($module); // end check module exists or not -------------------------- // check for site id is in real sites table. -------------------- if (!empty($site_ids)) { // now, loop to check again that site_id add list that is really exists in sites table. foreach ($module_site_ids as $site_id) { if (!in_array($site_id, $sites)) { // site_id in add list is not really exists in sites table, remove it. $module_site_ids = array_diff($module_site_ids, [$site_id]); } } // endforeach; unset($site_id); } else { $module_site_ids = $sites; } // really add module to sites. -------------------------------- $result = $this->addModuleSites($output, $module_system_name, $module_metadata, $module_id, $module_site_ids); if ($result === true) { $output_count = count($module_site_ids); } unset($module_id, $module_site_ids, $result, $sites); return $output_count + $output_subtracted; }