Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow template override, misc improvements #3

Merged
merged 9 commits into from
Nov 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 76 additions & 49 deletions usermanual/UserManualPlugin.php
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,77 +1,104 @@
<?php

namespace Craft;

class UserManualPlugin extends BasePlugin
{
public function getName()
{
$pluginName = Craft::t('User Manual');
$pluginNameOverride = $this->getSettings()->pluginNameOverride;

function getName()
{
return Craft::t('User Manual');
}
return ($pluginNameOverride) ? $pluginNameOverride : $pluginName;
}

function getVersion()
{
return '1.0.0';
}
public function getVersion()
{
return '1.0.0';
}

function getDeveloper()
{
return 'Hill Holliday';
}
public function getDeveloper()
{
return 'Hill Holliday';
}

function getDeveloperUrl()
{
return 'http://hhcc.com';
}
public function getDeveloperUrl()
{
return 'http://hhcc.com';
}

function hasCpSection()
public function hasCpSection()
{
return true;
}

public function addTwigExtension()
{
Craft::import('plugins.usermanual.twigextensions.UserManualTwigExtension');
return new UserManualTwigExtension();
}
public function addTwigExtension()
{
Craft::import('plugins.usermanual.twigextensions.UserManualTwigExtension');

public function registerCpRoutes() {
return array(
'usermanual/(?P<userManualPath>[a-zA-Z0-9\-\_\/]+)' => 'usermanual/index'
);
}
return new UserManualTwigExtension();
}

public function registerCpRoutes()
{
return [
'usermanual/(?P<userManualPath>[a-zA-Z0-9\-\_\/]+)' => 'usermanual/index',
];
}

protected function defineSettings()
{
return array(
'channels' => array(AttributeType::Mixed, 'default' => ""),
);
return [
'pluginNameOverride' => AttributeType::String,
'templateOverride' => AttributeType::Template,
'section' => AttributeType::Number,
];
}

public function getSettingsHtml(){

$options= [[
'label' => 'Please select',
'value' => ''
]];

foreach(craft()->sections->getAllSections() as $section){
$options[] = [
'label' => $section['name'],
'value' => $section['handle']
];
}

return craft()->templates->render('UserManual/settings',array(
'settings' => $this->getSettings(),
'options' => $options
));
}
public function getSettingsHtml()
{
$options = [[
'label' => '',
'value' => '',
]];

foreach (craft()->sections->getAllSections() as $section) {
if (!$section->hasUrls) {
continue;
}
$options[] = [
'label' => $section['name'],
'value' => $section['id'],
];
}

return craft()->templates->render('usermanual/settings', [
'settings' => $this->getSettings(),
'options' => $options,
'siteTemplatesPath' => craft()->path->getSiteTemplatesPath(),
]);
}

public function onAfterInstall()
{
craft()->request->redirect(UrlHelper::getCpUrl('settings/plugins/usermanual/'));
}

public function getSettings()
{
$settings = parent::getSettings();
foreach ($settings as $name => $value) {
$configValue = craft()->config->get($name, 'usermanual');
$settings->$name = is_null($configValue) ? $value : $configValue;
}

// Allow handles from config
if (!is_numeric($settings->section)) {
$section = craft()->sections->getSectionByHandle('homepage');
if ($section) {
$settings->section = $section->id;
}
}
return $settings;
}
}
6 changes: 6 additions & 0 deletions usermanual/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
return [
'pluginNameOverride' => null,
'templateOverride' => null,
'section' => null,
];
15 changes: 0 additions & 15 deletions usermanual/services/UserManual_SettingsService.php

This file was deleted.

12 changes: 12 additions & 0 deletions usermanual/templates/_body.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{# Using bracket notation for an accurate test, due to
a Twig bug: http://craftcms.stackexchange.com/questions/2116/twig-is-defined-always-returning-true/2117#2117 #}

{% if entry['body'] is defined %}
{{ entry.body }}
{% else %}
<p>
<a href="{{ url('settings/fields/new') }}">Create a field</a> with the handle of <code>body</code>
<b>or</b>
use the <a href="{{ url('settings/plugins/usermanual#settings-templateOverride-field') }}">Template Override</a> option to define your own fields.
</p>
{% endif %}
25 changes: 14 additions & 11 deletions usermanual/templates/index.html
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
{% extends "_layouts/cp" %}


{% set channelSelected = (craft.userManual.getSettings() is defined) ? craft.userManual.getSettings() : "Homepage" %}
{% set title = "User Manual"|t %}
{% set sectionSelected = (craft.userManual.settings.section is defined) ? craft.userManual.settings.section : "Homepage" %}
{% set title = craft.userManual.name|t %}

{% set sidebar %}
<nav>
{% set help = craft.entries.section(channelSelected) %}
{% set help = craft.entries.sectionId(sectionSelected) %}
<ul>
{% nav page in help %}
{% nav page in help %}
{% set active = page.slug == craft.request.lastSegment or loop.first and craft.request.lastSegment == 'usermanual' %}
<li>
<a {% if page.slug == craft.request.lastSegment or loop.first and craft.request.lastSegment == 'usermanual' %}class="sel"{% endif %} href="/{{craft.userManual.getCpUrl()}}/usermanual/{{page.uri}}">{{page.title}}</a>
<a {% if active %}class="sel"{% endif %}
href="{{ url('usermanual/' ~ page.uri) }}">
{{page.title}}
</a>
{% ifchildren %}
<ul>
{% children %}
</ul>
<ul>
{% children %}
</ul>
{% endifchildren %}
</li>
{% endnav %}
Expand All @@ -23,8 +26,8 @@
{% endset %}

{% set content %}
{% includeCssResource('usermanual/css/help.css') %}
{% includeCssResource('usermanual/css/help.css') %}
<div id="user-manual">
{{ getHelpDocument(channelSelected) }}
{{ getHelpDocument(sectionSelected)|raw }}
</div>
{% endset %}
68 changes: 57 additions & 11 deletions usermanual/templates/settings.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,58 @@
{% import "_includes/forms" as forms %}
<p>Select which Craft section should be used as the "User Manual" section:</p>
{{ forms.select({
label: "My Setting"|t,
id: 'channels',
name: 'channels',
instructions: "Input your setting"|t,
value: settings.channels,
options: options,
autofocus: true
})
}}

{% macro configWarning(setting, file) -%}
{{ "This is being overridden by the {setting} config setting."|t({
setting: '<a href="https://github.com/hillholliday/Craft-User-Manual/config-settings#'~setting~'" target="_blank">'~setting~'</a>'
})|raw }}
{%- endmacro %}

{% from _self import configWarning %}

{% set configOverride = craft.config.get('pluginNameOverride', 'usermanual') is not null %}
{{ forms.textField({
label: "Plugin Name"|t,
id: 'pluginNameOverride',
name: 'pluginNameOverride',
instructions: "Intuitive, human-readable plugin name for the end user."|t,
value: settings.pluginNameOverride,
autofocus: true,
first: true,
errors: '',
warning: configOverride ? configWarning('pluginNameOverride'),
disabled: configOverride,
readonly: configOverride,
})}}

{% set configOverride = craft.config.get('section', 'usermanual') is not null %}
{% set inputMacro = configOverride ? 'textField' : 'selectField' %}
{% set opts = {
label: "User Manual Section"|t,
id: 'section',
name: 'section',
instructions: 'Entries in this section must have associated urls.'|t,
value: settings.section,
options: options,
warning: configOverride ? configWarning('pluginNameOverride'),
disabled: configOverride,
readonly: configOverride,
} %}
{{ configOverride ? forms.textField(opts) : forms.selectField(opts) }}

{% set instructions %}
For more control over the output, you may optionally override the default template.<br>
Path is relative to <code>{{ siteTemplatesPath }}</code>.
{% endset %}
{% set configOverride = craft.config.get('templateOverride', 'usermanual') is not null %}

{{ forms.textField({
label: "Template Override"|t,
id: 'templateOverride',
name: 'templateOverride',
placeholder: '_includes/userManualContent.twig',
instructions: instructions|raw,
value: settings.templateOverride,
errors: '',
warning: configOverride ? configWarning('pluginNameOverride'),
disabled: configOverride,
readonly: configOverride,
})}}
Loading