Skip to content

Commit

Permalink
Add Navee migration + add panels to settings
Browse files Browse the repository at this point in the history
  • Loading branch information
engram-design committed Jul 25, 2019
1 parent 8b58185 commit 409c0d1
Show file tree
Hide file tree
Showing 8 changed files with 252 additions and 37 deletions.
28 changes: 27 additions & 1 deletion src/controllers/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use verbb\navigation\Navigation;
use verbb\navigation\migrations\AmNavPlugin;
use verbb\navigation\migrations\NaveePlugin;

use Craft;
use craft\web\Controller;
Expand All @@ -12,7 +13,7 @@ class BaseController extends Controller
// Public Methods
// =========================================================================

public function actionMigrate()
public function actionAmNavMigrate()
{
// Backup!
Craft::$app->getDb()->backup();
Expand All @@ -39,6 +40,31 @@ public function actionMigrate()
]);
}

public function actionNaveeMigrate()
{
// Backup!
Craft::$app->getDb()->backup();

$settings = Navigation::$plugin->getSettings();
$request = Craft::$app->getRequest();

$migration = new NaveePlugin();

ob_start();
$migration->up();
$output = ob_get_contents();
ob_end_clean();

$output = nl2br($output);

Craft::$app->getSession()->setNotice(Craft::t('navigation', 'Navee migrated.'));

return $this->renderTemplate('navigation/settings', [
'output' => $output,
'settings' => $settings,
]);
}

public function actionSettings()
{
$settings = Navigation::$plugin->getSettings();
Expand Down
134 changes: 134 additions & 0 deletions src/migrations/NaveePlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php
namespace verbb\navigation\migrations;

use verbb\navigation\Navigation;
use verbb\navigation\elements\Node;
use verbb\navigation\models\Nav as NavModel;

use Craft;
use craft\db\Migration;
use craft\db\Query;
use craft\helpers\Json;

class NaveePlugin extends Migration
{
public $propagate = true;
public $assignToDefaultSite = false;

private $processedNodes = [];

// Public Methods
// =========================================================================

public function safeUp()
{
try {
if (!$this->db->tableExists('{{%navee_navigations}}')) {
return true;
}

$NaveeNavs = (new Query())
->select(['*'])
->from(['{{%navee_navigations}}'])
->all();

foreach ($NaveeNavs as $key => $NaveeNav) {
$nav = Navigation::$plugin->navs->getNavByHandle($NaveeNav['handle']);

echo "\n > Migrating nav `{$NaveeNav['handle']}` ...\n";

if (!$nav) {
$nav = new NavModel();
}

$nav->name = $NaveeNav['name'];
$nav->handle = $NaveeNav['handle'];
$nav->maxLevels = $NaveeNav['maxLevels'];
$nav->structureId = $NaveeNav['structureId'];

if (!Navigation::$plugin->navs->saveNav($nav)) {
echo " > ERROR: Unable to migrate nav `{$NaveeNav['handle']}` ...\n";

Craft::dump($nav->getErrors());

continue;
}

foreach (Craft::$app->getSites()->getAllSites() as $site) {
$NaveeNodes = (new Query())
->select(['*'])
->from(['{{%navee_nodes}}'])
->leftJoin('{{%elements_sites}} elements_sites', '[[elements_sites.elementId]] = [[navee_nodes.id]]')
->where(['navigationId' => $NaveeNav['id'], 'siteId' => $site['id']])
->all();

foreach ($NaveeNodes as $key => $NaveeNode) {
try {
$node = new Node();

$NaveeElement = (new Query())
->select(['*'])
->from(['{{%elements}}'])
->leftJoin('{{%content}} content', '[[content.elementId]] = [[elements.id]]')
->where(['elements.id' => $NaveeNode['elementId'], 'type' => 'Navee_Node', 'siteId' => $site['id']])
->one();

if (!$NaveeElement) {
echo " > ERROR: Unable to find element for node `{$NaveeNode['id']}` ...\n";

continue;
}

$node->title = $NaveeElement['title'];
$node->enabled = $NaveeElement['enabled'];
$node->siteId = $site->id;
$node->navId = $nav->id;
$node->url = $NaveeNode['customUri'];
$node->classes = $NaveeNode['class'];

if ($NaveeNode['target'] === '_blank') {
$node->newWindow = true;
}

if ($NaveeNode['entryId']) {
$node->type = \craft\elements\Entry::class;
$node->elementId = $NaveeNode['entryId'];
} else if ($NaveeNode['categoryId']) {
$node->type = \craft\elements\Category::class;
$node->elementId = $NaveeNode['categoryId'];
} else if ($NaveeNode['assetId']) {
$node->type = \craft\elements\Asset::class;
$node->elementId = $NaveeNode['assetId'];
}

if (Craft::$app->getElements()->saveElement($node, true, $this->propagate)) {


} else {
echo " > ERROR: Unable to save node `{$NaveeNode['title']}` ...\n";

Craft::dump($node->getErrors());
}
} catch (\Throwable $e) {
echo " > ERROR: Unable to save node `{$NaveeNode['id']}` ...\n";

Craft::dump($e->getMessage());

continue;
}
}
}
}

} catch (\Throwable $e) {
Craft::dump($e->getMessage());
}

return true;
}

public function safeDown()
{
return false;
}
}
4 changes: 2 additions & 2 deletions src/resources/dist/css/navigation.css

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions src/resources/src/scss/navigation.scss
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,13 @@
}


// ==========================================================================
// Settings
// ==========================================================================

.navigation-settings-tabs li {
width: 100%;
max-width: 100% !important;
}


Original file line number Diff line number Diff line change
@@ -1,36 +1,6 @@
{% extends 'navigation/_layouts' %}
{% import '_includes/forms' as forms %}
{% import 'navigation/_macros' as macros %}

{% set crumbs = [
{ label: craft.navigation.getPluginName(), url: url('navigation/settings') },
{ label: 'Settings' | t('app'), url: url('navigation/settings') }
] %}

{% set fullPageForm = true %}

{% block blockContent %}

<input type="hidden" name="action" value="plugins/save-plugin-settings">
<input type="hidden" name="pluginHandle" value="navigation">

{% namespace 'settings' %}
{{ forms.textField({
id: 'pluginName',
name: 'pluginName',
label: 'Plugin Name' | t('app'),
value: settings.pluginName,
first: true,
autofocus: true,
instructions: 'Plugin name for the end user.' | t('navigation'),
warning: macros.configWarning('pluginName', 'navigation'),
}) }}
{% endnamespace %}

<hr>

<h2>{{ 'Migrate A&M Nav plugin' | t('navigation') }}</h2>

<p>{{ 'If you\'re migrating from a Craft 2 site that used the [A&M Nav](https://github.com/am-impact/amnav) plugin, you can use this utility to convert those menus and navigation items to the Navigation plugin.' | t('navigation') | md }}</p>

{% if craft.app.getIsMultiSite() %}
Expand All @@ -57,14 +27,12 @@ <h3>{{ 'Multi Site' | t('navigation') }}</h3>
}) }}
{% endif %}

<a class="btn submit btn-migrate" href="#" data-action="navigation/base/migrate">{{ 'Migrate' | t('navigation') }}</a>
<a class="btn submit btn-migrate" href="#" data-action="navigation/base/am-nav-migrate">{{ 'Migrate' | t('navigation') }}</a>

{% if output is defined %}

<hr>

<code>{{ output | raw }}</code>

{% endif %}

{% endblock %}
{% endif %}
13 changes: 13 additions & 0 deletions src/templates/settings/_panes/general.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% import '_includes/forms' as forms %}
{% import 'navigation/_macros' as macros %}

{{ forms.textField({
id: 'pluginName',
name: 'pluginName',
label: 'Plugin Name' | t('app'),
value: settings.pluginName,
first: true,
autofocus: true,
instructions: 'Plugin name for the end user.' | t('navigation'),
warning: macros.configWarning('pluginName', 'navigation'),
}) }}
9 changes: 9 additions & 0 deletions src/templates/settings/_panes/navee.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<a class="btn submit btn-migrate" href="#" data-action="navigation/base/navee-migrate">{{ 'Migrate' | t('navigation') }}</a>

{% if output is defined %}

<hr>

<code>{{ output | raw }}</code>

{% endif %}
55 changes: 55 additions & 0 deletions src/templates/settings/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{% extends 'navigation/_layouts' %}
{% import '_includes/forms' as forms %}
{% import 'navigation/_macros' as macros %}

{% set crumbs = [
{ label: craft.navigation.getPluginName(), url: url('navigation/settings') },
{ label: 'Settings' | t('app'), url: url('navigation/settings') }
] %}

{% set fullPageForm = true %}

{% set navItems = {
'general': { title: 'General Settings' | t('navigation') },
'am-nav': { title: 'A&M Nav Migration' | t('navigation') },
'navee': { title: 'Navee Migration' | t('navigation') },
} %}

{% set selectedItem = 'general' %}

{% block sidebar %}
<nav class="navigation-settings-tabs" id="tabs">
<ul>
{% for id, item in navItems %}
{% if item.heading is defined %}
<li class="heading"><span>{{ item.heading }}</span></li>
{% else %}
<li>
<a id="tab-{{ loop.index }}" href="#tab-{{ id }}" class="tab {% if id == selectedItem %}sel{% endif %}">
{{ item.title }}
</a>
</li>
{% endif %}
{% endfor %}
</ul>
</nav>
{% endblock %}

{% block blockContent %}

<input type="hidden" name="action" value="plugins/save-plugin-settings">
<input type="hidden" name="pluginHandle" value="navigation">

{% for id, item in navItems %}
{% if item.title is defined %}
<div id="tab-{{ id }}" {% if not loop.first %}class="hidden"{% endif %}>
<h2>{{ item.title }}</h2>

{% namespace 'settings' %}
{% include 'navigation/settings/_panes/' ~ id ignore missing %}
{% endnamespace %}
</div>
{% endif %}
{% endfor %}

{% endblock %}

0 comments on commit 409c0d1

Please sign in to comment.