. * */ namespace Vvveb\Controller\Theme; use function Vvveb\__; use Vvveb\Controller\Base; use Vvveb\System\CacheManager; use Vvveb\System\Core\View; use Vvveb\System\Extensions\Themes; use Vvveb\System\Validator; class Market extends Base { function install() { $slug = $this->request->post['slug'] ?? false; try { if ($slug) { $theme = Themes :: getMarketList(['slug' => $slug])['themes']; if ($theme && isset($theme[0])) { $themeInfo = $theme[0]; extract($themeInfo); $url = Themes :: marketUrl(); $downloadLink = "$url$download_link"; $this->view->log[] = sprintf(__('Installing "%s"'), $name); $this->view->log[] = sprintf(__('Downloading "%s"'), $downloadLink); if ($tempFile = Themes :: download($downloadLink)) { $this->view->log[] = sprintf(__('Unpacking "%s"'), $tempFile); if (Themes :: install($tempFile, $slug, false)) { CacheManager::clearObjectCache('vvveb', 'themes_list_' . $this->global['site_id']); $themeName = \Vvveb\humanReadable($slug); $themeName = "$themeName"; $themeActivateUrl = \Vvveb\url(['module' => 'theme/themes', 'action'=> 'activate', 'theme' => $slug]); $successMessage = sprintf(__('Theme %s was successfully installed!'), $themeName, $themeActivateUrl); $this->view->log[] = $successMessage; $successMessage .= "'; $this->view->success[] = $successMessage; } else { $error = sprintf(__('Error installing "%s"!'), $slug); $this->view->log[] = $error; $this->view->errors[] = $error; } unlink($tempFile); } else { $this->view->errors[] = sprintf(__('Error downloading "%s" from %s!'), $slug, $downloadLink); } } else { $this->view->errors[] = sprintf(__('Theme "%s" not found!'), $slug); } } } catch (\Exception $e) { $error = $e->getMessage(); $this->view->errors[] = $error; } if (isset($this->request->get['json'])) { $this->view->setType('json'); } } function index() { $view = View :: getInstance(); $validator = new Validator(['themes']); //allow only fields that are in the validator $request = array_filter($validator->filter($this->request->get)); $themes = []; $request['limit'] = $this->view->limit = 8; try { $themes = Themes :: getMarketList($request); $categories = Themes :: getMarketCategories(['limit' => 100] + $request, 'categories'); $installed = Themes :: getList($this->global['site_id']); foreach ($themes['themes'] as &$theme) { $theme['installed'] = isset($installed[$theme['slug']]); } } catch (\Exception $e) { $view->warning[] = __('Failed to connect to marketplace'); if (DEBUG) { $view->errors[] = $e->getMessage(); } } $view->set($themes); $view->categories = $categories['categories']; $view->set($request); } }