/** * execute command * * @param InputInterface $input * @param OutputInterface $output */ public function execute(InputInterface $input, OutputInterface $output) { $options['unlimited'] = true; $options['list_for'] = 'admin'; $ModulesDb = new \System\Core\Models\ModulesDb($this->Db); $module_list = $ModulesDb->listModulesQb($options); unset($ModulesDb, $options); if (is_array($module_list) && array_key_exists('total', $module_list) && $module_list['total'] > '0' && array_key_exists('items', $module_list)) { $output->writeln('There are total ' . $module_list['total'] . ' modules in db.'); $output->writeln('------------------------------'); foreach ($module_list['items'] as $row) { $output->write('ID: "' . $row->module_id . '"'); $output->write(' Module: "' . $row->module_system_name . '"'); $output->writeln(' Version: "' . $row->module_version . '"'); // module in each sites. if (property_exists($row, 'module_sites')) { $Table = new \Symfony\Component\Console\Helper\Table($output); $Table->setHeaders(['Site ID', 'Enabled']); foreach ($row->module_sites as $msrow) { $Table->addRows([[$msrow->site_id, $msrow->module_enable == '1' ? 'Yes' : 'No']]); } // endforeach; unset($msrow); $Table->render(); unset($Table); $output->writeln('------------------------------'); } } // endforeach; unset($row); } else { $output->writeln('<error>Unable to list module or there is no module.</error>'); } unset($module_list); }
/** * 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); }
/** * load functions file. * * @param mixed $file path to file name start from inside module. this can be file name (string) or multiple file names (array). * @param string $module module name. * @param boolean $load_again set to false to require_once, set to true to require. use false to prevent function/class exists. * @return boolean return true on success loading, return false on failed loading. */ public function loadFunctions($file, $module = '', $load_again = false) { if (is_array($file)) { $last = false; foreach ($file as $a_file) { $last = $this->loadFunctions($a_file, $module, $load_again); } return $last; } $this->Db = $this->Silexapp['Db']; if ($module != null) { // load function on specified module. check that module is enabled. $cache = new \System\Libraries\Cache(); $cache->setSubFolder('system/Libraries/Modules.php'); if ($cache->contains('Modules-registerAutoload-' . $this->Db->getCurrentSiteId())) { $modules_list = $cache->fetch('Modules-registerAutoload-' . $this->Db->getCurrentSiteId()); } else { $moduledb = new \System\Core\Models\ModulesDb($this->Db); $options['module_enable'] = 1; $options['site_id'] = intval($this->Db->getCurrentSiteId()); $options['unlimited'] = true; $modules_list = $moduledb->listModulesQb($options); $cache->save('Modules-registerAutoload-' . $this->Db->getCurrentSiteId(), $modules_list, 60 * 60 * 24 * 30); unset($moduledb, $options); } if (is_array($modules_list) && array_key_exists('items', $modules_list) && is_array($modules_list['items'])) { foreach ($modules_list['items'] as $row) { if ($module == $row->module_system_name) { // found in module enabled. $module_found = true; break; } } // endforeach; unset($row); } unset($cache, $modules_list); if (!isset($module_found) || isset($module_found) && $module_found == false) { return false; } } if ($module == null) { $file = SYSTEM_PATH . DS . 'Functions' . DS . $file; } else { $file = MODULE_PATH . DS . $module . DS . 'Functions' . DS . $file; } return $this->loadFile($file, $load_again); }
/** * disable module on multiple sites. * * @param OutputInterface $output * @param string $module_system_name module system name same as module folder name. * @param array $site_ids array of site ids. * @return int return number of updated count. */ private function disableModuleSites(OutputInterface $output, $module_system_name, array $site_ids = []) { $output_count = 0; // get all site ids for checking. ------------------------------------------ $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); // get selected module where site_ids are in --------------------------- if (!empty($site_ids)) { $options['module_system_name'] = $module_system_name; $options['list_for'] = 'admin'; $options['site_ids'] = $site_ids; $options['unlimited'] = true; $ModulesDb = new \System\Core\Models\ModulesDb($this->Db); $list_modules = $ModulesDb->listModulesQb($options); unset($options); $module_site_ids = []; if (is_array($list_modules) && array_key_exists('items', $list_modules)) { foreach ($list_modules['items'] as $row) { $module_id = $row->module_id; $module_site_ids = array_merge($module_site_ids, [$row->site_id]); } unset($row); } } unset($list_modules, $ModulesDb); // check the sites, site_ids values before change status. -------------- if (!empty($site_ids)) { // there are site ids specified. check that site_id is really in the `sites` table. foreach ($site_ids as $site_id) { if ($site_id != null && in_array($site_id, $sites)) { // selected site id is exists in sites table. it is real site which means ok to update. if (isset($module_id) && intval($module_id) != '0') { if (in_array($site_id, $module_site_ids)) { // this module and site id is already exists in db. yeah you are able to disable it. $result = $this->disableModuleSite($output, intval($module_id), intval($site_id)); $sites = array_diff($sites, [$site_id]); if ($result === true) { $output_count++; } } elseif (!in_array($site_id, $module_site_ids)) { // this module had never added to this site, use add and then disable. $result = $this->addDisableModuleSite($output, intval($module_id), intval($site_id)); $sites = array_diff($sites, [$site_id]); if ($result === true) { $output_count++; } } } } } // endforeach; unset($site_id); } unset($module_site_ids, $result, $sites); return $output_count; }
/** * get all available modules. * * @return array return array with total and items in key. items key contain list of all modules available. */ public function getAllAvailableModules() { $this->Db = $this->Silexapp['Db']; $cache = new \System\Libraries\Cache(); $cache->setSubFolder('system/Libraries/Modules.php'); if ($cache->contains('Modules-registerAutoload-' . $this->Db->getCurrentSiteId())) { $modules_list = $cache->fetch('Modules-registerAutoload-' . $this->Db->getCurrentSiteId()); } else { $moduledb = new \System\Core\Models\ModulesDb($this->Db); $options['module_enable'] = 1; $options['site_id'] = intval($this->Db->getCurrentSiteId()); $options['unlimited'] = true; $modules_list = $moduledb->listModulesQb($options); $cache->save('Modules-registerAutoload-' . $this->Db->getCurrentSiteId(), $modules_list, 60 * 60 * 24 * 30); unset($moduledb, $options); } unset($cache, $this->Db); return $modules_list; }
/** * 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; }