Skip to content

Commit

Permalink
Link to and only show editable sites #23
Browse files Browse the repository at this point in the history
  • Loading branch information
janhenckens committed Feb 14, 2019
1 parent 98df147 commit 1518729
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 43 deletions.
34 changes: 20 additions & 14 deletions src/controllers/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,17 @@ public function actionIndex()
$sites = Craft::$app->sites->getAllSites();
$siteIds = Craft::$app->sites->getAllSiteIds();
$primarySite = Craft::$app->sites->getPrimarySite();
if(count($sites) == 1) {
if (count($sites) == 1) {
$data['defaultSite'] = Craft::$app->sites->getPrimarySite();
} else {
$sites = Craft::$app->sites->getEditableSites();
$canEditDefault = false;
$canEditDefault = array_walk($sites, function ($site) use ($primarySite, $canEditDefault) {
if ($site->id === $primarySite->id) {
$canEditDefault = array_filter($sites, function ($site) use ($primarySite, $canEditDefault) {
if ($site->id == $primarySite->id) {
return true;
}
});

if ($canEditDefault) {
$data['defaultSite'] = $primarySite;
} else {
Expand All @@ -77,20 +78,22 @@ public function actionIndex()
return $this->renderTemplate('navigate/_index', $data);
}

public function actionAdd() {
public function actionAdd()
{
return $this->renderTemplate('navigate/_settings');
}

public function actionSave() {
public function actionSave()
{
$this->requirePostRequest();
if(isset(Craft::$app->request->getBodyParams()['data']['id'])) {
if (isset(Craft::$app->request->getBodyParams()['data']['id'])) {
$model = Navigate::$plugin->navigate->getNavigationById(Craft::$app->request->getBodyParams()['data']['id']);
} else {
$model = new NavigationModel();
}

$model->setAttributes(Craft::$app->request->getBodyParams()['data']);
if(!$model->validate()) {
if (!$model->validate()) {
return $this->renderTemplate('navigate/_settings', [
'navigation' => $model,
'errors' => $model->getErrors(),
Expand All @@ -104,8 +107,9 @@ public function actionSave() {

}

public function actionEdit($navId = null, $siteHandle) {
if($navId && $siteHandle) {
public function actionEdit($navId = null, $siteHandle)
{
if ($navId && $siteHandle) {
$navigation = Navigate::$plugin->navigate->getNavigationById($navId);
$sites = Craft::$app->sites->getEditableSites();
$site = Craft::$app->sites->getSiteByHandle($siteHandle);
Expand All @@ -123,27 +127,29 @@ public function actionEdit($navId = null, $siteHandle) {

return $this->renderTemplate('navigate/_edit', [
'nodes' => Navigate::$plugin->nodes->getNodesByNavIdAndSiteById($navId, $site->id),
'nodeTypes' =>$nodeTypes,
'nodeTypes' => $nodeTypes,
'navigation' => $navigation,
'site' => $site,
]);
}
}

public function actionSettings($navId = null) {
public function actionSettings($navId = null)
{
$data = [];
$data['sources'] = Navigate::$plugin->nodes->types;
if($navId) {
if ($navId) {
$data['navigation'] = Navigate::$plugin->navigate->getNavigationById($navId);
}
return $this->renderTemplate('navigate/_settings', $data);
}

public function actionDelete() {
public function actionDelete()
{
$this->requirePostRequest();
$this->requireAcceptsJson();

if(Navigate::$plugin->navigate->deleteNavigationById(Craft::$app->request->post('id'))) {
if (Navigate::$plugin->navigate->deleteNavigationById(Craft::$app->request->post('id'))) {
// Return data
$returnData['success'] = true;
return $this->asJson($returnData);
Expand Down
2 changes: 1 addition & 1 deletion src/templates/_includes/_sites.twig
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<div id="revision-btn" class="btn menubtn" data-icon="world">{{ thisSite.name }}</div>
<div class="menu">
<ul class="padded">
{% for site in craft.app.sites.getAllSites() %}
{% for site in craft.app.sites.getEditableSites %}
<li>
<a href="/admin/navigate/edit/{{ navigation.id }}/{{ site.handle }}"
{% if site.handle == thisSite.handle %}class="sel"{% endif %}>
Expand Down
61 changes: 33 additions & 28 deletions src/templates/_index.twig
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
{% set createUrl = url('navigate/add') %}

{% block actionButton %}
{% if (not currentUser.admin and settings.anyoneCanAdd) or currentUser.admin %}
<div id="button-container">
<div class="btngroup submit">
<a class="btn submit add icon" href="{{ createUrl }}">
{{ 'New navigation'|t('navigate') }}
</a>
{% if (not currentUser.admin and settings.anyoneCanAdd) or currentUser.admin %}
<div id="button-container">
<div class="btngroup submit">
<a class="btn submit add icon" href="{{ createUrl }}">
{{ 'New navigation'|t('navigate') }}
</a>
</div>
</div>
</div>
{% endif %}
{% endblock %}

Expand All @@ -37,43 +37,48 @@
{% if navigations|length %}
<table id="navigations" class="data fullwidth collapsible">
<thead>
<tr><th scope="col">{{ 'Name'|t('navigate') }}</th>
<tr>
<th scope="col">{{ 'Name'|t('navigate') }}</th>
<th scope="col">{{ 'Handle'|t('navigate') }}</th>
<th scope="col">{{ 'Settings'|t('navigate') }}</th>
<th scope="col">{{ 'Remove'|t('navigate') }}</th>
</tr></thead>
</tr>
</thead>
<tbody>
{% for navigation in navigations %}
<tr data-id="{{ navigation.id }}" data-name="{{ navigation.title }}">
<th scope="row" data-title="Title"><a href="{{ url('navigate/edit/'~navigation.id ~ '/' ~ defaultSite.handle) }}">{{ navigation.title }}</a></th>
<td data-title="Handle"><code>{{ navigation.handle }}</code></td>
<td data-title="Settings">
{% if not navigation.adminOnly or currentUser.admin %}
<a href="{{ url('navigate/settings/'~navigation.id) }}" role="button"><span data-icon="settings"></span></a>
{% endif %}
</td>
<td class="thin">
{% if (not currentUser.admin and settings.anyoneCanAdd) or currentUser.admin %}
<a class="delete icon" title="Delete" role="button"></a>
{% endif %}
</td>
</tr>
<tr data-id="{{ navigation.id }}" data-name="{{ navigation.title }}">
<th scope="row" data-title="Title"><a
href="{{ url('navigate/edit/'~navigation.id ~ '/' ~ defaultSite.handle) }}">{{ navigation.title }}</a>
</th>
<td data-title="Handle"><code>{{ navigation.handle }}</code></td>
<td data-title="Settings">
{% if not navigation.adminOnly or currentUser.admin %}
<a href="{{ url('navigate/settings/'~navigation.id) }}" role="button"><span
data-icon="settings"></span></a>
{% endif %}
</td>
<td class="thin">
{% if (not currentUser.admin and settings.anyoneCanAdd) or currentUser.admin %}
<a class="delete icon" title="Delete" role="button"></a>
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
</table>

{% else %}
<div id="nonavigations">
{{ 'No navigations found, click "New navigation" in the top right corner to get started'|t('navigate') }}
{{ 'No navigations found, click "New navigation" in the top right corner to get started'|t('navigate') }}
</div>
{% endif %}
{% endblock %}

{% js %}
var adminTable = new Craft.AdminTable({
tableSelector: '#navigations',
noItemsSelector: '#nonavigations',
deleteAction: 'navigate/default/delete',
confirmDeleteMessage: '{{ "Are you sure you want to delete “{name}”?"|t('navigate') }}',
tableSelector: '#navigations',
noItemsSelector: '#nonavigations',
deleteAction: 'navigate/default/delete',
confirmDeleteMessage: '{{ "Are you sure you want to delete “{name}”?"|t('navigate') }}',
});
{% endjs %}

0 comments on commit 1518729

Please sign in to comment.