Skip to content

Commit

Permalink
added ability to choose category in the backend
Browse files Browse the repository at this point in the history
  • Loading branch information
Statia 26 committed Jul 30, 2018
1 parent d6346ea commit 3683e4b
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 52 deletions.
2 changes: 1 addition & 1 deletion src/controllers/MenuItemsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function actionEdit($menuId = null)
$data['menu'] = Olivemenus::$plugin->olivemenus->getMenuById($menuId);
$data['sections'] = Olivemenus::$plugin->olivemenuItems->getSectionsWithEntries();
$data['menuItemsMarkup'] = Olivemenus::$plugin->olivemenuItems->getMenuItemsAdminMarkup($menuId);

$data['categories'] = Craft::$app->categories->getAllGroups();
return $this->renderTemplate('olivemenus/_menu-items', $data);
}

Expand Down
15 changes: 8 additions & 7 deletions src/services/OlivemenuItemsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Craft;
use craft\base\Component;
use craft\elements\Entry;
use craft\elements\Category;

/**
* OlivemenusService Service
Expand All @@ -42,7 +43,7 @@ public function getSectionsWithEntries() {
foreach($sections as $handle => $values) {
if (!empty($sections[$handle])) {
foreach ($values as $index => $value) {
$sections[$handle][$index]['entries'] = $this->getFirstEntriesBySection($value['handle']);
$sections[$handle][$index]['entries'] = $this->getEntriesBySection($value['handle']);
}
}
}
Expand Down Expand Up @@ -182,12 +183,6 @@ private function getEntriesBySection($handle) {
->all();
}

private function getFirstEntriesBySection($handle) {
return Entry::find()
->section($handle)
->one();
}

private function sortMenuItemsByParents($arrMenuItems) {
$counter = 0;
$arrMenuItemsSorted = [];
Expand Down Expand Up @@ -230,6 +225,12 @@ private function getItemAdminMarkup($menuItem) {
->id($menuItem['entry_id'])
->one();

if (!$entry) {
$entry = Category::find()
->id($menuItem['entry_id'])
->one();
}

$localHTML .= '<li id="menu-item-' .$menuItem['id']. '">';
$localHTML .= '<div>';
$localHTML .= '<div class="item-heading">';
Expand Down
8 changes: 8 additions & 0 deletions src/services/OlivemenusService.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Craft;
use craft\base\Component;
use craft\elements\Entry;
use craft\elements\Category;

/**
* OlivemenusService Service
Expand Down Expand Up @@ -157,6 +158,13 @@ private function getMenuItemHTML($menu_item) {
->one();

if (!empty($entry) ) $menu_item_url = $entry->url;
else {
$entry = Category::find()
->id($menu_item['entry_id'])
->one();

if (!empty($entry) ) $menu_item_url = $entry->url;
}
}

if ($data_json) {
Expand Down
126 changes: 82 additions & 44 deletions src/templates/_menu-items.twig
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,12 @@
{{ csrfInput() }}
<div class="fields">
{% for single in sections.single %}
{% set local_id = single.entries.id %}
{% set local_title = single.entries.title %}
{% set local_url = single.entries.url %}
<div class="field checkboxfield">
<input id="single-{{local_id}}" type="checkbox" value="{{local_id}}" name="single-{{local_id}}" data-url="{{local_url}}" class="fieldtoggle checkbox" />
<label for="single-{{local_id}}">{{local_title}}</label>
</div>
{% for entry in single.entries %}
<div class="field checkboxfield">
<input id="single-{{entry.id}}" type="checkbox" value="{{entry.id}}" name="single-{{entry.id}}" data-url="{{entry.url}}" class="fieldtoggle checkbox" />
<label for="single-{{entry.id}}">{{entry.title}}</label>
</div>
{% endfor %}
{% endfor %}
</div>
<input type="submit" class="btn" value="{{ 'Add to menu'|t('olivemenus') }}" />
Expand All @@ -46,13 +45,12 @@
<input id="singles-search-input" class="search text nicetext fullwidth" placeholder="{{ 'Start typing to search...'|t('olivemenus') }}" type="text" />
<ul class="search-list">
{% for single in sections.single %}
{% set local_id = single.entries.id %}
{% set local_title = single.entries.title %}
{% set local_url = single.entries.url %}
<li class="field checkboxfield">
<input id="single-search-{{local_id}}" type="checkbox" value="{{local_id}}" name="single-search-{{local_id}}" data-url="{{local_url}}" class="fieldtoggle checkbox" />
<label for="single-search-{{local_id}}">{{local_title}}</label>
</li>
{% for entry in single.entries %}
<li class="field checkboxfield">
<input id="single-search-{{entry.id}}" type="checkbox" value="{{entry.id}}" name="single-search-{{entry.id}}" data-url="{{entry.url}}" class="fieldtoggle checkbox" />
<label for="single-search-{{entry.id}}">{{entry.title}}</label>
</li>
{% endfor %}
{% endfor %}
</ul>
</div>
Expand All @@ -72,9 +70,6 @@
<h4 class="ui-accordion-header">{{channel.name}}</h4>
<div class="ui-accordion-content">
{% if channel.entries is defined and channel.entries is not empty %}
{% set entries = craft.entries()
.section(channel.name)
.all() %}
<div class="os-tabs ui-tabs">
<ul class="ui-tabs-nav">
<li><a href="#{{channel.handle}}-all">{{ "View all"|t('olivemenus') }}</a></li>
Expand All @@ -84,13 +79,10 @@
<form method="POST">
{{ csrfInput() }}
<div class="fields">
{% for entry in entries %}
{% set local_id = entry.id %}
{% set local_title = entry.title %}
{% set local_url = entry.url %}
{% for entry in channel.entries %}
<div class="field checkboxfield">
<input id="channel-{{local_id}}" type="checkbox" value="{{local_id}}" name="channel-{{local_id}}" data-url="{{local_url}}" class="fieldtoggle checkbox" />
<label for="channel-{{local_id}}">{{local_title}}</label>
<input id="channel-{{entry.id}}" type="checkbox" value="{{entry.id}}" name="channel-{{entry.id}}" data-url="{{entry.url}}" class="fieldtoggle checkbox" />
<label for="channel-{{entry.id}}">{{entry.title}}</label>
</div>
{% endfor %}
</div>
Expand All @@ -103,13 +95,10 @@
<div class="input">
<input id="{{channel.handle}}-search-input" class="search text nicetext fullwidth" placeholder="Start typing to search..." type="text" />
<ul class="search-list">
{% for entry in entries %}
{% set local_id = entry.id %}
{% set local_title = entry.title %}
{% set local_url = entry.url %}
{% for entry in channel.entries %}
<li class="field checkboxfield">
<input id="channel-search-{{local_id}}" type="checkbox" value="{{local_id}}" name="channel-search-{{local_id}}" data-url="{{local_url}}" class="fieldtoggle checkbox" />
<label for="channel-search-{{local_id}}">{{local_title}}</label>
<input id="channel-search-{{entry.id}}" type="checkbox" value="{{entry.id}}" name="channel-search-{{entry.id}}" data-url="{{entry.url}}" class="fieldtoggle checkbox" />
<label for="channel-search-{{entry.id}}">{{entry.title}}</label>
</li>
{% endfor %}
</ul>
Expand All @@ -136,9 +125,6 @@
<h4 class="ui-accordion-header">{{structure.name}}</h4>
<div class="ui-accordion-content">
{% if structure.entries is defined and structure.entries is not empty %}
{% set entries = craft.entries()
.section(structure.name)
.all() %}
<div class="os-tabs ui-tabs">
<ul class="ui-tabs-nav">
<li><a href="#{{structure.handle}}-all">{{ "View all"|t('olivemenus') }}</a></li>
Expand All @@ -148,13 +134,10 @@
<form method="POST">
{{ csrfInput() }}
<div class="fields">
{% for entry in entries %}
{% set local_id = entry.id %}
{% set local_title = entry.title %}
{% set local_url = entry.url %}
{% for entry in structure.entries %}
<div class="field checkboxfield">
<input id="structure-search-{{local_id}}" type="checkbox" value="{{local_id}}" name="structure-{{local_id}}" data-url="{{local_url}}" class="fieldtoggle checkbox" />
<label for="structure-search-{{local_id}}">{{local_title}}</label>
<input id="structure-search-{{entry.id}}" type="checkbox" value="{{entry.id}}" name="structure-{{entry.id}}" data-url="{{entry.url}}" class="fieldtoggle checkbox" />
<label for="structure-search-{{entry.id}}">{{entry.title}}</label>
</div>
{% endfor %}
</div>
Expand All @@ -167,13 +150,10 @@
<div class="input">
<input id="{{structure.handle}}-search-input" class="search text nicetext fullwidth" placeholder="Start typing to search..." type="text" />
<ul class="search-list">
{% for entry in entries %}
{% set local_id = entry.id %}
{% set local_title = entry.title %}
{% set local_url = entry.url %}
{% for entry in structure.entries %}
<li class="field checkboxfield">
<input id="structure-search-{{local_id}}" type="checkbox" value="{{local_id}}" name="structure-search-{{local_id}}" data-url="{{local_url}}" class="fieldtoggle checkbox" />
<label for="structure-search-{{local_id}}">{{local_title}}</label>
<input id="structure-search-{{entry.id}}" type="checkbox" value="{{entry.id}}" name="structure-search-{{entry.id}}" data-url="{{entry.url}}" class="fieldtoggle checkbox" />
<label for="structure-search-{{entry.id}}">{{entry.title}}</label>
</li>
{% endfor %}
</ul>
Expand All @@ -192,6 +172,64 @@
<p>{{ 'There are no structures defined.'|t('olivemenus') }}</p>
{% endif %}
</div>
<h4 class="ui-accordion-header">{{ "Categories"|t('olivemenus') }}</h4>
<div class="ui-accordion-content">
{% if categories is defined and categories is not empty %}
<div class="os-accordion accordion">
{% for category in categories %}
<h4 class="ui-accordion-header">{{category.name}}</h4>
<div class="ui-accordion-content">
{% set entries = craft.categories()
.group(category.handle)
.all() %}
{% if entries|length %}
<div class="os-tabs ui-tabs">
<ul class="ui-tabs-nav">
<li><a href="#{{category.handle}}-all">{{ "View all"|t('olivemenus') }}</a></li>
<li><a href="#{{category.handle}}-search">{{ "Search"|t('olivemenus') }}</a></li>
</ul>
<div id="{{category.handle}}-all" class="ui-tabs-panel">
<form method="POST">
{{ csrfInput() }}
<div class="fields">
{% for entry in entries %}
<div class="field checkboxfield">
<input id="category-search-{{entry.id}}" type="checkbox" value="{{entry.id}}" name="category-{{entry.id}}" data-url="{{entry.url}}" class="fieldtoggle checkbox" />
<label for="category-search-{{entry.id}}">{{entry.title}}</label>
</div>
{% endfor %}
</div>
<input type="submit" class="btn" value="{{ 'Add to menu'|t('olivemenus') }}">
</form>
</div>
<div id="{{category.handle}}-search" class="ui-tabs-panel">
<form method="POST">
{{ csrfInput() }}
<div class="input">
<input id="{{category.handle}}-search-input" class="search text nicetext fullwidth" placeholder="Start typing to search..." type="text" />
<ul class="search-list">
{% for entry in entries %}
<li class="field checkboxfield">
<input id="category-search-{{entry.id}}" type="checkbox" value="{{entry.id}}" name="category-search-{{entry.id}}" data-url="{{entry.url}}" class="fieldtoggle checkbox" />
<label for="category-search-{{entry.id}}">{{entry.title}}</label>
</li>
{% endfor %}
</ul>
</div>
<input type="submit" class="btn" value="{{ 'Add to menu'|t('olivemenus') }}">
</form>
</div>
</div>
{% else %}
<p>{{ 'There are no categories.'|t('olivemenus') }}</p>
{% endif %}
</div>
{% endfor %}
</div>
{% else %}
<p>{{ 'There are no categories groups defined.'|t('olivemenus') }}</p>
{% endif %}
</div>
<h4 class="ui-accordion-header">{{ 'Custom menu'|t('olivemenus') }}</h4>
<div class="ui-accordion-content">
<form class="custom-url" method="POST">
Expand Down

0 comments on commit 3683e4b

Please sign in to comment.