Code snippets for symfony 1.x

Navigation

Refine Tags

Snippets tagged "menu"

CSS link class adder

<?php
 
/**
 * Automatically adds a class name to a link (or a parent tag) if the link route matches
 * the current module or route.
 * (based on the work made by scott meves on 2007-02-24 : http://snippets.symfony-project.org/snippet/153)
 *
 * @author  ESCANDELL Stéphane <stephane.escandell[a]gmail.com>
 * @date    2010-07-16
 *
 *
 * @param  <name> : string name of the link, i.e. string to appear between the <a> tags
 * @param  <internal_uri> string 'module/action' or '@rule' of the action
 * @param  <options> array additional style options
 * @return string XHTML compliant <a href> tag (encapsulated by an optionnal tag)
 * @see    link_to
 * 
 *
 *
 * <b>Available Options:</b>
 *    -> All classic options from link_to() method
 *
 *    -> 'moduleOnly'
 *      recognized values:
 *        -> true
 *        -> false
 *      If set to <true>, the link is identify as matched only if the module of the route associated to <internal_uri>
 *      is matching to the current module.
 *      If not set, the default value is false
 *
 *    -> 'currentClass'
 *      recognized values : 
 *        -> string 
 *      The class identifier added to the link (or the parent tag, see below) if <internal_uri> is matching to
 *      the current page.
 *      If not set, the default class added is 'active'.
 *
 *    -> 'separator'
 *      recognized values:
 *        -> string
 *      Used only if the <class> option is set. If the link match, this separator will be used between the <class>
 *      option and <currentClass> option (the generated class will be <class>.<separator>.<currentClass>).
 *      If not set, the default value is a space caracter ' '.
 *
 *    ->  'tag'
 *      recognized values:
 *        -> html tag name
 *      Identify an html tag to encapsulate the link. If this option appears, the <currentClass> name will be added
 *      to this tag rather than the <a> tag.
 *
 *    -> 'tagOptions'
 *      recognized values:
 *        -> array of html parameters
 *      Used only if the parameter <tag> exists. <tagOptions> is used to generate the tag to encapsulate the link with
 *      user specific options.
 *
 * <b>Examples:</b>
 * <code>
 *   <?php echo link_to_styled( 'home',
 *                           '@homepage',
 *                           array('title' => 'Home')); ?>
 *   if the current route is '@homepage', then:
 *     => <a href="path/to/homepage/action" class="active" title="Home">home</a>
 * 
 *   <?php echo link_to_styled( 'home',
 *                           '@homepage',
 *                           array('title' => 'Home', 'currentClass' => 'myActiveClass')); ?>
 *   if the current route is '@homepage', then:
 *     => <a href="path/to/homepage/action" class="myActiveClass" title="Home">home</a>
 *
 *   <?php echo link_to_styled( 'home',
 *                           '@homepage',
 *                           array('title' => 'Home', 'separator' => '_')); ?>
 *   if the current route is '@homepage', then:
 *     => <a href="path/to/homepage/action" class="active" title="Home">home</a>
 *
 *   <?php echo link_to_styled( 'home',
 *                           '@homepage',
 *                           array('title' => 'Home', 'class' => 'myClass tab', 'separator' => '_')); ?>
 *   if the current route is '@homepage', then:
 *     => <a href="path/to/homepage/action" class="myClass tab_active" title="Home">home</a>
 *
 *   <?php echo link_to_styled( 'home',
 *                           '@homepage',
 *                           array('title' => 'Home', 'currentClass' => 'myActiveClass',
 *                                 'tag' => 'li')); ?>
 *   if the current route is '@homepage', then:
 *     => <li class="myActiveClass"><a href="path/to/homepage/action" title="Home">home</a></li>
 *
 *   <?php echo link_to_styled( 'home',
 *                           '@homepage',
 *                           array('title' => 'Home',
 *                                 'tag' => 'li',
 *                                  'tagOptions' => array('class' => 'anotherClass',
 *                                                        'width' => '250px')
 *                                )); ?>
 *   if the current route is '@homepage', then:
 *     => <li class="anotherClass active" width="250px"><a href="path/to/homepage/action" title="Home">home</a></li>
 *
 *   <?php echo link_to_styled( 'home',
 *                           '@homepage',
 *                           array('title' => 'Home',
 *                                 'currentClass' => myActiveClass,
 *                                 'tag' => 'li',
 *                                  'tagOptions' => array('class' => 'anotherClass other',
 *                                                        'width' => '250px')
 *                                )); ?>
 *   if the current route is '@homepage', then:
 *     => <li class="anotherClass other myActiveClass" width="250px"><a href="path/to/homepage/action" title="Home">home</a></li>
 * </code>
 *
 *
 * Changelog :
 *    2010/07/16 :
 *      Author : ESCANDELL Stéphane <stephane.escandell[a]gmail.com>
 *      Modification :
 *        First version (based on the work made by scott meves on 2007-02-24 : http://snippets.symfony-project.org/snippet/153)
 */
function link_to_styled($name, $internal_uri, $options = array())
{
  static $context = null;
  static $currentModuleName = null;
  static $applicationConfiguration = null;
 
  if(is_null($context))
  {
    $context = sfContext::getInstance();
  }
  if(is_null($currentModuleName))
  {
    $currentModuleName = $context->getModuleName();
  }
  if(is_null($applicationConfiguration))
  {
    $applicationConfiguration = $context->getConfiguration();
  }
 
  $options    = _parse_attributes($options);
  $separator  = isset($options['separator']) ? $options['separator'] : ' ';
  $tag        = isset($options['tag']) ? $options['tag'] : null;
  $tagOptions = isset($options['tagOptions']) ? $options['tagOptions'] : array();
 
  $isCurrent = false;
  // In order to findRoute() works, we need to delete the reference to
  // the file controller used
  //
  $route = $context->getRouting()->findRoute( str_replace('/'.$applicationConfiguration->getApplication().'_'.$applicationConfiguration->getEnvironment().'.php',
                                                      '',
                                                      url_for($internal_uri,false)
                                                      ));
  if(isset($options['moduleOnly']) && true===$options['moduleOnly'])
  {
    $isCurrent = isset($route['parameters']) ? (isset($route['parameters']['module']) ? $route['parameters']['module']==$currentModuleName : false) : false;
  }
  else
  {
    $isCurrent = url_for($internal_uri,false) == url_for($context->getRouting()->getCurrentInternalUri(),false);
  }
 
  if($isCurrent)
  {
    if(is_null($tag))
    {
      if(!isset($options['class']))
        $options['class'] = isset($options['currentClass']) ? $options['currentClass'] : 'active';
      else
        $options['class'] .= isset($options['currentClass']) ? $separator.$options['currentClass'] : $separator.'active';
    }
    else
    {
      if(!isset($tagOptions['class']))
        $tagOptions['class']  = isset($options['currentClass']) ? $options['currentClass'] : 'active';
      else
        $tagOptions['class'] .= isset($options['currentClass']) ? $separator.$options['currentClass'] : $separator.'active';
    }
  }
 
 
  unset($options['moduleOnly'],$options['currentClass'],$options['separator'],$options['tag'],$options['tagOptions']);
 
  if(is_null($tag))
    return link_to($name, $internal_uri, $options);
  else
    return content_tag($tag, link_to($name, $internal_uri, $options), $tagOptions);
}
 
by Stéphane Escandell on 2010-07-16, tagged css  helper  link  menu  navigation 
(2 comments)

Navigation Jump Menu Drop Down

If you want to have a quick drop down (select) menu that automatically takes users to a specific page on your site, this method works well and results in clean URLS. This snippet is for content / urls stored in a database.

There may be a better way to do this, so suggestions are welcome!

First, set up a route like this:

show_content:
  url: content/:title
  params: { module: content, action: show }

That way we can have urls like "content/about" or "content/news". (Note you will want to make sure your content titles are URL friendly themselves, perhaps using a stripped_title field instead of the title field that is parsed to replace spaces and punctuation.)

In the peer class of the "content" table we want to create a method we can use to populate our jump menu:

public static function getUrlOptions()
{
  $options = array();
  $c = new Criteria();
  $c->addAscendingOrderByColumn(self::NAME);
  $contents = ContentPeer::doSelect($c);
 
  foreach ($contents as $content) {
    $url = sfContext::getInstance()->getController()->genUrl('content/show?id='.$content->getId());
    $options[$url] = $content->getTitle();
  }
 
  return $options;
}

in the template we rely on those options and create a select menu with a javascript behavior:

Jump to: <?php echo select_tag('title', options_for_select(ContentPeer::getUrlOptions(), $currentContentUrl), array('onchange'=>'window.document.location = this.value')) ?>

And finally, in the action we store the current url so we can have it pre-selected in the jump menu on the subsequent page:

    $this->content = ContentPeer::retrieveByTitle($this->getRequestParameter('title'));
    $this->currentContentUrl = $this->getController()->genUrl(sfRouting::getInstance()->getCurrentInternalUri());

You now end up with a select menu like this:

<select name="category" onchange="...">
  <option value="/content/news">News</option>
  <option value="/content/sports">Sports</option>
  ...
</select>

Selecting an option from the list will redirect your visitor to the URL stored as the option value.

by scott meves on 2007-04-09, tagged jump  menu  select