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

Page as shortcode #24

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
130 changes: 130 additions & 0 deletions README.md
@@ -0,0 +1,130 @@
Simple Pages (plugin for Omeka)
===============================

[Simple Pages] is a plugin for [Omeka] that allows administrators to create
simple web pages for their public site.

Keep it simple. Use this plugin to add an “About” page or other basic content to
your site.


Installation
------------

Uncompress files and rename plugin folder "SimplePages".

Then install it like any other Omeka plugin and follow the config instructions.


Usage
-----

Simply add and edit pages via the main menu.

See the [dedicated page] for more information.


Shortcodes
----------

The shortcode allows to add blocks, lists or navigation into a page.

The general syntax is `[simple_pages key=value]`, where keys can be:

- slug
The slug of the selected page to display.

- id
The id of the selected page to display.

- slugs
The slugs of the selected pages to display.

- ids
The ids of the selected pages to display.

Generally, only one of these four keys are used.

- output
Select the type of content to return. Can be "block" (default, the full
content), "text" (same as block but without title), "link" (list of links to
simple pages), "title" (list of title of simple pages), or "navigation" (the
hierarchical list of pages under a root page).

For lists, two other arguments are available:

- sort
Can be "alpha" or "order": the method to sort pages. If not set, slugs will be
sorted by the order of the `slugs` argument, and `ids` by the default order
("order").

- num
Allows to limit the number of pages to display (10 by default). This is used
only when there are no limitation (no keys or a range of ids).

For links, a specific argument is available:

- titles
It allows to choose alternative titles (generally shorter) for links. This is
usefull when shortcodes are used to build alternative menus in blocks. They
should be separated with ";" and this character shouldn't appear in the title.

All arguments are optional.


Warning
-------

Use it at your own risk.

It's always recommended to backup your files and database regularly so you can
roll back if needed.


Troubleshooting
---------------

See online issues on the [Simple Pages issues] page on GitHub.


License
-------

This plugin is published under [GNU/GPL].

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 3 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


Contact
-------

Current maintainers:

* Roy Rosenzweig Center for History and New Media


Copyright
---------

* Copyright Roy Rosenzweig Center for History and New Media, 2010-2014
* Copyright Daniel Berthereau, 2014 (improvements, see [Daniel-KM])


[Omeka]: https://omeka.org
[Simple Pages]: http://omeka.org/codex/Plugins/SimplePages
[dedicated page]: http://omeka.org/codex/Plugins/SimplePages_2.0
[Simple Pages issues]: http://omeka.org/forums/forum/plugins
[GNU/GPL]: https://www.gnu.org/licenses/gpl-3.0.html "GNU/GPL v3"
[Daniel-KM]: https://github.com/Daniel-KM "Daniel Berthereau"
145 changes: 145 additions & 0 deletions SimplePagesPlugin.php
Expand Up @@ -162,6 +162,7 @@ public function hookUpgrade($args)
public function hookInitialize()
{
add_translation_source(dirname(__FILE__) . '/languages');
add_shortcode('simple_pages', array($this, 'shortcodeSimplePages'));
}

/**
Expand Down Expand Up @@ -361,4 +362,148 @@ public function filterApiImportOmekaAdapters($adapters, $args)
$adapters['simple_pages'] = $simplePagesAdapter;
return $adapters;
}

/**
* Shortcode for displaying simple pages as blocks, texts, links, or lists.
*
* @todo No check is done on recursive shortcodes inside text of pages.
*
* @param array $args
* @param Omeka_View $view
* @return string
*/
public static function shortcodeSimplePages($args, $view)
{
$params = array();
$simplePages = array();
// List is used to display list of links or titles.
$list = false;

if (isset($args['slug'])) {
$params['slug'] = $args['slug'];
}

if (isset($args['slugs'])) {
$params['slug'] = explode(',', $args['slugs']);
$list = true;
}

if (isset($args['id'])) {
$params['id'] = $args['id'];
}

if (isset($args['ids'])) {
$params['range'] = $args['ids'];
$list = true;
}

if (isset($args['output'])) {
$output = $args['output'];
}
// Default is to return a simple page as a block, except if there is no
// parameter, where the navigation list of all simple pages is returned.
else {
$output = empty($params) ? 'navigation' : 'block';
}

// Set other params only if needed.
if (!empty($params)) {
if (isset($args['sort'])) {
$params['sort'] = $args['sort'];
}
// Default order is "order" to simplify shortcode, except for slugs,
// where it is the specified order, sorted below.
elseif (!isset($args['slugs'])) {
$params['sort'] = 'order';
}

if (isset($args['num'])) {
$limit = $args['num'];
} else {
$limit = 10;
}

$simplePages = get_records('SimplePagesPage', $params, $limit);
if (empty($simplePages)) {
return '';
}

// Order slugs by slug if needed (order by field is not used in model).
if (isset($args['slugs']) && !isset($args['sort'])) {
$orderedPages = array_fill_keys($params['slug'], null);
foreach ($simplePages as $simplePage) {
$orderedPages[$simplePage->slug] = $simplePage;
}
$simplePages = array_filter($orderedPages);
}
}

$html = '';
switch ($output) {
case 'block':
foreach ($simplePages as $simplePage) {
$text = metadata($simplePage, 'text', array('no_escape' => true));
$html .= '<div class="simple-page-block">';
$html .= '<h3>' . html_escape($simplePage->title) . '</h3>';
$html .= $view->shortcodes($text);
$html .= '</div>';
}
break;

case 'text':
foreach ($simplePages as $simplePage) {
$text = metadata($simplePage, 'text', array('no_escape' => true));
$html .= '<div class="simple-page-text">';
$html .= $view->shortcodes($text);
$html .= '</div>';
}
break;

case 'link':
// Check if different titles should be used, generally shorter.
// They should be as many as slugs, in the same order. If empty,
// the original title is used. They must be separated by a ";"
// and this character, like any other regex metacharacters,
// shouldn't appear in the title.
$titles = isset($args['titles']) ? array_map('trim', explode(';', $args['titles'])) : array();
$i = 0;

$html .= $list ? '<ul class="simple-page-titles">' : '';
foreach ($simplePages as $simplePage) {
$title = !empty($titles[$i]) ? $titles[$i] : $simplePage->title;
$i++;
$html .= $list ? '<li class="simple-page-title">' : '<span class="simple-page-title">';
$html .= link_to($simplePage, null, html_escape($title));
$html .= $list ? '</li>' : '</span>';
}
$html .= $list ? '</ul>' : '';
break;

case 'title':
$html .= $list ? '<ul class="simple-page-titles">' : '';
foreach ($simplePages as $simplePage) {
$html .= $list ? '<li class="simple-page-title">' : '<span class="simple-page-title">';
$html .= html_escape($simplePage->title);
$html .= $list ? '</li>' : '</span>';
}
$html .= $list ? '</ul>' : '';
break;

case 'navigation':
$order = isset($args['sort']) && $args['sort'] == 'alpha' ? 'alpha' : 'order';
// Display full navigation.
if (empty($simplePages)) {
$html .= simple_pages_navigation(0, $order);
}
// Display navigation under each specified parent page.
else {
foreach ($simplePages as $simplePage) {
$html .= simple_pages_navigation($simplePage->id, $order);
}
}
break;
}

return $html;
}
}