[lux commit] r137 - trunk/Lux/View/Helper

0 views
Skip to first unread message

codesite...@google.com

unread,
Sep 29, 2008, 1:48:02 PM9/29/08
to lux...@googlegroups.com
Author: rodrigo.moraes
Date: Mon Sep 29 10:47:03 2008
New Revision: 137

Added:
trunk/Lux/View/Helper/ActionList.php (contents, props changed)
trunk/Lux/View/Helper/ActionListAlphabet.php (contents, props changed)

Log:
Re-added list helpers, much simpler and flexible enough.

Added: trunk/Lux/View/Helper/ActionList.php
==============================================================================
--- (empty file)
+++ trunk/Lux/View/Helper/ActionList.php Mon Sep 29 10:47:03 2008
@@ -0,0 +1,177 @@
+<?php
+/**
+ *
+ * Helper to build a list of action links.
+ *
+ * @category Lux
+ *
+ * @package Lux_View
+ *
+ * @author Rodrigo Moraes <rodrigo...@gmail.com>
+ *
+ * @license http://opensource.org/licenses/bsd-license.php BSD
+ *
+ * @version $Id$
+ *
+ */
+class Lux_View_Helper_ActionList extends Solar_View_Helper
+{
+ /**
+ *
+ * User-defined configuration values.
+ *
+ * Keys are...
+ *
+ * `list_type`
+ * : (string) The type of list to use; default is 'ul'. Only 'ul'
and 'ol'
+ * are honored.
+ *
+ * `list_id`
+ * : (string) The ID attribute for the list.
+ *
+ * `list_class`
+ * :(string) The class attribute for the list.
+ *
+ * `first_class`
+ * : (string) The CSS class for the first item in the list.
+ *
+ * `curr_class`
+ * : (string) The CSS class for the currently active item in the list.
+ *
+ * `last_class`
+ * : (string) The CSS class for the last item in the list.
+ *
+ * @var array
+ *
+ */
+ protected $_Lux_View_Helper_ActionList = array(
+ 'list_type' => 'ul',
+ 'list_id' => null,
+ 'list_class' => null,
+ 'first_class' => 'first',
+ 'curr_class' => 'curr',
+ 'last_class' => 'last',
+ );
+
+ /**
+ *
+ * Returns a list of action links.
+ *
+ * @param array $items List of items using. See examples for the
format:
+
+ * {{code: php
+ *
+ * $items = array();
+ *
+ * // Simple item: create action using the provided locale code.
The key
+ * // is used to check if this is the active item.
+ * $items['/path/to/action'] = 'ACTION_LOCALE_CODE';
+ *
+ * // Named item: same as above, but using a array for the
definition.
+ * // The key doesn't need to be the action, allowing "named"
items.
+ * // Optionally, a CSS class name can be passed as a third value.
+ * $items['item-name'] = array(
+ * '/path/to/action',
+ * 'ACTION_LOCALE_CODE',
+ * 'css-class-for-this-item'
+ * );
+ *
+ * // No-action item: just escape and add the content to the list
+ * $items[] = 'some text to add to the list';
+ *
+ * }}
+ *
+ * @param string $current The currently "active" item in the list, if
any.
+ *
+ * @param array $config Additional config options to build the list.
+ *
+ * @return string A HTML list.
+ *
+ */
+ public function actionList($items, $current = null, $config = null)
+ {
+ if ($config) {
+ $config = array_merge($this->_config, (array) $config);
+ } else {
+ $config = $this->_config;
+ }
+
+ $attribs = $this->_view->attribs(array(
+ 'id' => $config['list_id'],
+ 'class' => $config['list_class'],
+ ));
+
+ // make sure we have ol or ul
+ $list_type = strtolower($config['list_type']);
+ if ($list_type != 'ol') {
+ $list_type = 'ul';
+ }
+
+ // start the list
+ $html = "<$list_type$attribs>\n";
+
+ $iter = 1;
+ $total = count($items);
+ foreach ($items as $key => $spec) {
+ // reset class and attributes for this item
+ $class = array();
+ $attribs = '';
+
+ // add 'first' class?
+ if (($iter == 1) && $config['first_class']) {
+ $class[] = $config['first_class'];
+ }
+
+ // add 'last' class?
+ if (($iter == $total) && $config['last_class']) {
+ $class[] = $config['last_class'];
+ }
+
+ // add 'current' class?
+ if (($key === $current) && $config['curr_class']) {
+ $class[] = $config['curr_class'];
+ }
+
+ // is this an action item?
+ if (is_string($key) || is_array($spec)) {
+ // using array for the item info?
+ if (is_array($spec)) {
+ // make sure we have always three values in the array
+ $spec = array_pad(array_values($spec), 3, null);
+
+ // add a special class for this item?
+ if ($spec[2]) {
+ $class[] = $spec[2];
+ }
+
+ // extract href and text
+ $key = $spec[0];
+ $spec = $spec[1];
+ }
+
+ // build the action
+ $href = $this->_view->escape($key);
+ $text = $this->_view->escape($spec);
+ $content = "<a href=\"$href\">$text</a>";
+ } else {
+ // no action: just escape the content
+ $content = $this->_view->escape($spec);
+ }
+
+ // add any CSS class?
+ if (! empty($class)) {
+ $attribs = $this->_view->attribs(array(
+ 'class' => $class,
+ ));
+ }
+
+ // set the item
+ $html .= " <li$attribs>$content</li>\n";
+ $iter++;
+ }
+
+ // done.
+ $html .= "</$list_type>";
+ return $html;
+ }
+}

Added: trunk/Lux/View/Helper/ActionListAlphabet.php
==============================================================================
--- (empty file)
+++ trunk/Lux/View/Helper/ActionListAlphabet.php Mon Sep 29 10:47:03 2008
@@ -0,0 +1,56 @@
+<?php
+/**
+ *
+ * Helper to build an alphabet with action links.
+ *
+ * @category Lux
+ *
+ * @package Lux_View
+ *
+ * @author Rodrigo Moraes <rodrigo...@gmail.com>
+ *
+ * @license http://opensource.org/licenses/bsd-license.php BSD
+ *
+ * @version $Id$
+ *
+ */
+class Lux_View_Helper_ActionListAlphabet extends Lux_View_Helper_ActionList
+{
+ /**
+ *
+ * Builds an alphabet with action links.
+ *
+ * @param string $uri Uri for items action links, formatted using
sprintf().
+ *
+ * @param string $current The currently "active" item in the list, if
any.
+ *
+ * @param array $config Additional config options to build the list.
+ *
+ * @param array $pre Optional items for the beginning of the list.
+ *
+ * @param array $post Optional items for the end of the list.
+ *
+ * @return string The list of action items.
+ *
+ */
+ public function actionListAlphabet($uri, $current = null, $config =
null,
+ $pre = null, $post = null)
+ {
+ // start the list with $pre items
+ $items = (array) $pre;
+
+ // build the alphabet
+ $list = range('a', 'z');
+
+ // set action info for each letter.
+ foreach($list as $letter) {
+ $items[$letter] = array(sprintf($uri, $letter), $letter);
+ }
+
+ // add $post items to the end of the list
+ $items = array_merge($items, (array) $post);
+
+ // done
+ return $this->actionList($items, $current, $config);
+ }
+}

Reply all
Reply to author
Forward
0 new messages