Exemplo n.º 1
0
 /**
  * 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 theme 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('Theme "' . $name . '": ');
                     // check theme exists in db.
                     $options['theme_system_name'] = $name;
                     $ThemesDb = new \System\Core\Models\ThemesDb($this->Db);
                     $theme_data = $ThemesDb->getTheme($options);
                     unset($options, $ThemesDb);
                     if ($theme_data == null) {
                         $output->writeln('<error>theme is not exists in database.</error>');
                     } else {
                         $sql = 'DELETE `t`, `ts`';
                         $sql .= ' FROM `' . $this->Db->getTableName('themes') . '` AS `t`';
                         $sql .= ' LEFT JOIN `' . $this->Db->getTableName('theme_sites') . '` AS `ts` ON t.theme_id = ts.theme_id';
                         $sql .= ' WHERE `t`.`theme_system_name` = :theme_system_name';
                         $stmt = $this->Db->Conn->prepare($sql);
                         $stmt->bindValue('theme_system_name', $name, \PDO::PARAM_STR);
                         $result = $stmt->execute();
                         $stmt->closeCursor();
                         unset($sql, $stmt);
                         $output->writeln('<success>deleted successfully.</success>');
                     }
                     unset($theme_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/Core/Models/ThemesDb.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);
 }
Exemplo n.º 2
0
 /**
  * did change a theme per site.
  * 
  * @param InputInterface $input
  * @param OutputInterface $output
  * @param string $theme_system_name theme system name (theme folder name).
  * @param integer $site_id site id that this theme will be change.
  * @return mixed return 0 if do nothing, return false if not found the theme in database, return true if success changed.
  */
 private function changeAThemeSite(InputInterface $input, OutputInterface $output, $theme_system_name, $site_id)
 {
     // get options
     $theme_enable = $input->getOption('enabled');
     $theme_default = $input->getOption('default');
     $theme_default_admin = $input->getOption('defaultAdmin');
     $theme_settings = $input->getOption('settings');
     // check input option and re-format values for insert/update
     $record_prepare = [];
     $record_value = [];
     if ($theme_enable == null && $theme_default == null && $theme_default_admin == null && $theme_settings == null) {
         // not enter anything. no need to do anything.
         return (int) 0;
     }
     if ($theme_enable != null) {
         $record_prepare['theme_enable'] = ':theme_enable';
         $record_value['theme_enable'] = [intval($theme_enable), \PDO::PARAM_INT];
     }
     if ($theme_default != null) {
         $record_prepare['theme_default'] = ':theme_default';
         $record_value['theme_default'] = [intval($theme_default), \PDO::PARAM_INT];
         if (intval($theme_default) == 1) {
             $record_prepare['theme_enable'] = ':theme_enable';
             $record_value['theme_enable'] = [1, \PDO::PARAM_INT];
         }
     }
     if ($theme_default_admin != null) {
         $record_prepare['theme_default_admin'] = ':theme_default_admin';
         $record_value['theme_default_admin'] = [intval($theme_default_admin), \PDO::PARAM_INT];
         if (intval($theme_default_admin) == 1) {
             $record_prepare['theme_enable'] = ':theme_enable';
             $record_value['theme_enable'] = [1, \PDO::PARAM_INT];
         }
     }
     if ($theme_settings != null && $theme_settings != 'NULL') {
         $record_prepare['theme_settings'] = ':theme_settings';
         $record_value['theme_settings'] = [json_encode(eval('return ' . $theme_settings . ';')), \PDO::PARAM_STR];
     } elseif ($theme_settings != null && $theme_settings == 'NULL') {
         $record_prepare['theme_settings'] = ':theme_settings';
         $record_value['theme_settings'] = [null, \PDO::PARAM_STR];
     }
     unset($theme_default, $theme_default_admin, $theme_enable, $theme_settings);
     // get theme data for checking and get some values.
     $options['theme_system_name'] = $theme_system_name;
     $ThemesDb = new \System\Core\Models\ThemesDb($this->Db);
     $theme = $ThemesDb->getTheme($options);
     unset($options, $ThemesDb);
     if ($theme == null || is_object($theme) && !property_exists($theme, 'theme_sites')) {
         // not found a theme in db.
         unset($record_prepare, $record_value, $theme);
         return false;
     } else {
         // get the theme id
         $theme_id = $theme->theme_id;
         // check that this theme in specified site is already exists or not.
         $use_method = 'insert';
         if (is_array($theme->theme_sites)) {
             foreach ($theme->theme_sites as $ts) {
                 if ($ts->site_id == $site_id) {
                     $use_method = 'update';
                     break;
                 }
             }
             // endforeach;
             unset($ts);
         }
         unset($theme);
         // if there are default setting for front or admin, reset all other theme on this site id to non default first.
         if (is_array($record_value) && array_key_exists('theme_default', $record_value) && $record_value['theme_default'][0] == (int) 1) {
             // this cmd set default theme for front. reset all other theme on this site to default = 0.
             $sql = 'UPDATE `' . $this->Db->getTableName('theme_sites') . '` SET `theme_default` = 0 WHERE `theme_id` != ' . intval($theme_id) . ' AND `site_id` = ' . intval($site_id);
             $stmt = $this->Db->Conn->prepare($sql);
             $stmt->execute();
             unset($sql, $stmt);
         }
         if (is_array($record_value) && array_key_exists('theme_default_admin', $record_value) && $record_value['theme_default_admin'][0] == (int) 1) {
             // this cmd set default theme for admin. reset all other theme on this site to default = 0.
             $sql = 'UPDATE `' . $this->Db->getTableName('theme_sites') . '` SET `theme_default_admin` = 0 WHERE `theme_id` != ' . intval($theme_id) . ' AND `site_id` = ' . intval($site_id);
             $stmt = $this->Db->Conn->prepare($sql);
             $stmt->execute();
             unset($sql, $stmt);
         }
         // if there is just only enable = 0 and no default for front or admin, set default of these to 0 too.
         if (is_array($record_value) && array_key_exists('theme_enable', $record_value) && $record_value['theme_enable'][0] == (int) 0) {
             if (is_array($record_value) && !array_key_exists('theme_default', $record_value)) {
                 $record_prepare['theme_default'] = ':theme_default';
                 $record_value['theme_default'] = [intval(0), \PDO::PARAM_INT];
             }
             if (is_array($record_value) && !array_key_exists('theme_default_admin', $record_value)) {
                 $record_prepare['theme_default_admin'] = ':theme_default_admin';
                 $record_value['theme_default_admin'] = [intval(0), \PDO::PARAM_INT];
             }
         }
         // make change to db.
         $qb = $this->Db->Conn->createQueryBuilder();
         if ($use_method == 'insert') {
             $qb->insert($this->Db->getTableName('theme_sites'));
             $record_prepare['theme_id'] = ':theme_id';
             $record_value['theme_id'] = [intval($theme_id), \PDO::PARAM_INT];
             $record_prepare['site_id'] = ':site_id';
             $record_value['site_id'] = [intval($site_id), \PDO::PARAM_INT];
         } else {
             $qb->update($this->Db->getTableName('theme_sites'));
             $record_value['theme_id'] = [intval($theme_id), \PDO::PARAM_INT];
             $record_value['site_id'] = [intval($site_id), \PDO::PARAM_INT];
         }
         foreach ($record_prepare as $key => $item) {
             $qb->set($key, $item);
         }
         unset($item, $key, $record_prepare);
         if ($use_method == 'update') {
             $qb->andWhere('theme_id = :theme_id')->andWhere('site_id = :site_id');
         }
         foreach ($record_value as $key => $item) {
             $qb->setParameter($key, $item[0], $item[1]);
         }
         unset($item, $key, $record_value);
         $qb->execute();
         $qb->resetQueryParts();
         // end make change to db.
         unset($qb, $theme_id, $use_method);
         return true;
     }
     unset($record_prepare, $record_value, $theme);
     return false;
 }
Exemplo n.º 3
0
 /**
  * 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 theme folder name.</error>');
         unset($names);
         return false;
     }
     if ($names != null) {
         // get theme config
         $Config = new \System\Libraries\Config();
         $Config->load('theme');
         $theme_dir = $Config->get('theme_dir', 'theme');
         unset($Config);
         $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('Theme "' . $name . '": ');
                     if (is_dir($theme_dir . DS . $name)) {
                         if (is_file($theme_dir . DS . $name . DS . $name . '.php')) {
                             $Metadata = new \System\Libraries\Themes\Metadata();
                             $theme_metadata = $Metadata->readThemeMetadata($name);
                             if (is_array($theme_metadata) && array_key_exists('Version', $theme_metadata) && $theme_metadata['Version'] != null) {
                                 // check theme exists or not.
                                 $options['theme_system_name'] = $name;
                                 $ThemesDb = new \System\Core\Models\ThemesDb($this->Db);
                                 $theme = $ThemesDb->getTheme($options);
                                 unset($options, $ThemesDb);
                                 if ($theme == null || empty($theme)) {
                                     // theme is not exists.
                                     $qb = $this->Db->Conn->createQueryBuilder();
                                     $qb->insert($this->Db->getTableName('themes'))->values(['theme_system_name' => ':theme_system_name', 'theme_version' => ':theme_version'])->setParameter('theme_system_name', $name, \PDO::PARAM_STR)->setParameter('theme_version', $theme_metadata['Version'], \PDO::PARAM_STR)->execute();
                                     $theme_id = $this->Db->Conn->lastInsertId();
                                     $qb->resetQueryParts();
                                     $output->writeln('<success>added successfully.</success>');
                                 } else {
                                     $output->writeln('<info>already exists.</info>');
                                 }
                             } else {
                                 $output->writeln('<error>could not add, there is no metadata in theme file.</error>');
                             }
                             unset($Metadata, $theme_metadata);
                         } else {
                             $output->writeln('<error>could not add, theme metadata file not found.</error>');
                         }
                     } else {
                         $output->writeln('<error>could not add, theme folder is not exists.</error>');
                     }
                 }
                 // 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/Core/Models/ThemesDb.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, $theme_dir);
 }