diff --git a/README.md b/README.md new file mode 100644 index 0000000..6cac2a3 --- /dev/null +++ b/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" diff --git a/SimplePagesPlugin.php b/SimplePagesPlugin.php index 4eb8540..ba08715 100644 --- a/SimplePagesPlugin.php +++ b/SimplePagesPlugin.php @@ -162,6 +162,7 @@ public function hookUpgrade($args) public function hookInitialize() { add_translation_source(dirname(__FILE__) . '/languages'); + add_shortcode('simple_pages', array($this, 'shortcodeSimplePages')); } /** @@ -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 .= '
'; + $html .= '

' . html_escape($simplePage->title) . '

'; + $html .= $view->shortcodes($text); + $html .= '
'; + } + break; + + case 'text': + foreach ($simplePages as $simplePage) { + $text = metadata($simplePage, 'text', array('no_escape' => true)); + $html .= '
'; + $html .= $view->shortcodes($text); + $html .= '
'; + } + 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 ? '' : ''; + break; + + case 'title': + $html .= $list ? '' : ''; + 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; + } } diff --git a/models/SimplePagesPageTable.php b/models/Table/SimplePagesPage.php similarity index 73% rename from models/SimplePagesPageTable.php rename to models/Table/SimplePagesPage.php index 594eaac..e0dc241 100644 --- a/models/SimplePagesPageTable.php +++ b/models/Table/SimplePagesPage.php @@ -11,7 +11,7 @@ * * @package SimplePages */ -class SimplePagesPageTable extends Omeka_Db_Table +class Table_SimplePagesPage extends Omeka_Db_Table { /** * Find all pages, ordered by slug name. @@ -23,38 +23,36 @@ public function findAllPagesOrderBySlug() $select = $this->getSelect()->order('slug'); return $this->fetchObjects($select); } - - public function applySearchFilters($select, $params) + + /** + * Find all slugs. + * + * @return array All slugs of pages. + */ + public function findSlugs() { $alias = $this->getTableAlias(); - $paramNames = array('parent_id', - 'is_published', - 'title', - 'slug', - 'created_by_user_id', - 'modified_by_user_id', - 'template'); - - foreach($paramNames as $paramName) { - if (isset($params[$paramName])) { - $select->where($alias . '.' . $paramName . ' = ?', array($params[$paramName])); - } - } + $select = $this->getSelect(); + $select->reset(Zend_Db_Select::COLUMNS); + $select->from(array(), array($alias . '.slug')); + return $this->fetchCol($select); + } - if (isset($params['sort'])) { - switch($params['sort']) { - case 'alpha': - $select->order("{$alias}.title ASC"); - $select->order("{$alias}.order ASC"); - break; - case 'order': - $select->order("{$alias}.order ASC"); - $select->order("{$alias}.title ASC"); - break; - } - } + /** + * Retrieve a simple page by slug. + * + * @param sllug $slug Slug of the page to retrieve. + * @return SimplePagePage|null The simple page that is returned. + */ + public function findBySlug($slug) + { + $select = $this->getSelect(); + $select->where($this->getTableAlias() . '.slug = ?', $slug); + $select->limit(1); + $select->reset(Zend_Db_Select::ORDER); + return $this->fetchObject($select, array()); } - + /** * Retrieve child pages from list of pages matching page ID. * @@ -189,9 +187,50 @@ public function getSelect() $select = parent::getSelect(); $permissions = new Omeka_Db_Select_PublicPermissions('SimplePages_Page'); $permissions->apply($select, 'simple_pages_pages','created_by_user_id','is_published'); - - + return $select; - + } + + /** + * @param Omeka_Db_Select + * @param array + * @return void + */ + public function applySearchFilters($select, $params) + { + $alias = $this->getTableAlias(); + $boolean = new Omeka_Filter_Boolean; + $genericParams = array(); + foreach ($params as $key => $value) { + if ($value === null || (is_string($value) && trim($value) == '')) { + continue; + } + switch ($key) { + case 'range': + $this->filterByRange($select, $value); + break; + case 'sort': + switch($params['sort']) { + case 'alpha': + $select->order("$alias.title ASC"); + $select->order("$alias.order ASC"); + break; + case 'order': + $select->order("$alias.order ASC"); + $select->order("$alias.title ASC"); + break; + } + break; + default: + $genericParams[$key] = $value; + } + } + + if (!empty($genericParams)) { + parent::applySearchFilters($select, $genericParams); + } + + // If we returning the data itself, we need to group by the record id. + $select->group("$alias.id"); } }