Development

sfChecklistsHelper

You must first sign up to be able to contribute.

Version 6 (modified by Jennifer, 7 years ago)
--

As superhaggis (irc nick) show us a very nice checklist widget from C82 : http://www.c82.net/samples/checklist-samples.html or : Data Recovery Services I though it might be usefull as an helper. This is why I come up with a plugin. I hope this is the good way, notice that you have to install it as a global plugin as I didn't made a post-installation script for the specific local plugin installation.

the README says:

ReadMe for sfChecklistsPlugin thanks to : http://www.c82.net/samples/checklist-samples.html


Use :

(template for diplaying)
------------------------
<h1>checklists</h1>
<?php echo form_tag('test/showchecklists') ?>
<?php echo checklists_tag("myCheckList", array('toto', 'titi', 'bernard', 'jean', 'robert', 'joseph'), array('style'=>'cl3','multiple'=>1)) ?>
<?php echo submit_tag('ok') ?>
</form>


(action for form validation)
----------------------------
public function executeShowchecklists()
{
  $this->checklists = $this->getRequestParameter('myCheckList');
  return sfView::SUCCESS;
}

(template for form validation)
------------------------------
<h1>show checklists</h1>
<ul>
<?php foreach ($checklists as $aCheck) {
	echo "<li>$aCheck</li>";
} ?>
</ul>

options are :
-------------

using embedded css:
'style' =>'cl1' or 'cl2' or 'cl3' (for the three defauly style embedded in a checklists.css)

or you can define you own using
'css' => <your css name> (in web/css or global sf/css)
'style' => class name corresponding to a style of your css

Hope you like it.


edit by Nick Winfield aka superhaggis I wrote a set of helper functions based on object_select_tag(), options_for_select() and checkbox_tag().

/PEAR_ROOT/data/symfony/web/sf/js/checklistmultiselect.js

/*-----------------------------------------------------------+
 | addLoadEvent: Add event handler to body when window loads |
 +-----------------------------------------------------------*/
function addLoadEvent(func) {
    var oldonload = window.onload;

    if (typeof window.onload != "function") {
        window.onload = func;
    } else {
        window.onload = function () {
            oldonload();
            func();
        }
    }
}

/*------------------------------------+
 | Functions to run when window loads |
 +------------------------------------*/
addLoadEvent(function () {
    initChecklist();
});

/*----------------------------------------------------------+
 | initChecklist: Add :hover functionality on labels for IE |
 +----------------------------------------------------------*/
function initChecklist() {
    if (document.all && document.getElementById) {
        // Get all unordered lists
        var lists = document.getElementsByTagName("ul");

        for (i = 0; i < lists.length; i++) {
            var theList = lists[i];

            // Only work with those having the class "checklist"
            if (theList.className.indexOf("checklist") > -1) {
                var labels = theList.getElementsByTagName("label");

                // Assign event handlers to labels within
                for (var j = 0; j < labels.length; j++) {
                    var theLabel = labels[j];
                    theLabel.onmouseover = function() { this.className += " hover"; };
                    theLabel.onmouseout = function() { this.className = this.className.replace(" hover", ""); };
                }
            }
        }
    }
}

/PEAR_ROOT/data/symfony/web/sf/css/checklistmultiselect.css

/* CSS for checklists */
.checklist  
{   
  border:     1px solid #ccc;
  list-style: none;
  height:     10em;
  overflow:   auto;
  width:      20em;
} 
 
.checklist, .checklist li 
{ 
  margin:  0;
  padding: 0;  
}

.checklist label 
{ 
  display:     block;
  height:      1%;
  padding:     0 0.2em 0 25px;
  text-indent: -25px;  
}

.checklist label:hover, .checklist label.hover 
{ 
  background: #777; 
  color:      #fff;  
}

/PEAR_ROOT/symfony/helper/ObjectHelper.php

/**
 * Returns a special checkbox list that mimics a multi selectbox.
 *
 * @param object An object.
 * @param string An object column.
 * @param array Input options (related_class option is mandatory).
 * @param bool Input default value.
 *
 * @see http://c82.net/samples/checklist-basic.html
 * @return string A list string which represents a checkbox list.
 *
 */  
function object_checkboxmultiselect_tag ($object, $method, $options = array(), $default_value = null)
{   
    sfContext::getInstance()->getResponse()->addJavascript('/sf/js/checklistmultiselect.js');
    sfContext::getInstance()->getResponse()->addStylesheet('/sf/css/checklistmultiselect.css');
    
    $options       = _parse_attributes($options);
  
    $related_class = isset($options['related_class']) ? $options['related_class'] : '';
    
    if (!isset($options['related_class']) && preg_match('/^get(.+?)Id$/', $method, $match))
    {
        $related_class = $match[1];
    }
  
    unset($options['related_class']);
 
    $select_options = _get_values_for_object_select_tag($object, $related_class);
 
    $value = _get_object_value($object, $method, $default_value);
 
    $option_tags = options_for_checkboxmultiselect(_convert_method_to_name($method, $options), 
                                                   $select_options, 
                                                   $value, 
                                                   $options);
  
    return checkboxlist_tag(_convert_method_to_name($method, $options), $option_tags, $options);
}

/PEAR_ROOT/symfony/helper/FormHelper.php

function options_for_checkboxmultiselect($name, $options = array(), $selected = '', $html_options = array())
{   
    $html_options = _parse_attributes($html_options);
    
    if (is_array($selected))
    {    
        $valid = array_values($selected);
        $valid = array_map('strval', $valid);
    }
                             
    $html = '';
        
    foreach ($options as $key => $value)
    {   
        $checked = false;
 
        $option_options = array('value' => $key);
    
        if (isset($selected)
            && (is_array($selected)
            && in_array(strval($key), $valid, true))
            || (strval($key) == strval($selected)))
        {
            $checked = true;
        }
        $stripped_name = strtolower(preg_replace('/ |-/', '_', $name . ' ' . $value));
    
        $checkbox = multicheckbox_tag($name, $value, $checked, $option_options);
         
        $label = content_tag('label',
                             $checkbox,
                             array_merge(array('for' => $stripped_name)),
                             _convert_options($options)) . "\n";
        
        $html .= content_tag('li', $label, array(), _convert_options($options));
    }   
        
    return $html;
}

function multicheckbox_tag($name, $value = '1', $checked = false, $options = array())
{ 
  $request = sfContext::getInstance()->getRequest();
    
  if ($request->hasErrors())
  { 
    $checked = $request->getParameter($name, null);
  } 
    
  $html_options = array_merge(array(
                    'type' => 'checkbox',
                    'name' => $name . '[]',
                    'id' => strtolower(preg_replace('/ |-/', '_', $name . '_' . $value)),
                    'value' => $value), 
                    _convert_options($options));
    
  if ($checked)
  {   
    $html_options['checked'] = 'checked';
  }
   
  return tag('input', $html_options) . ' ' . $value;
}

function checkboxlist_tag($name, $option_tags = null, $options = array())
{
  return content_tag('ul', $option_tags, array_merge(array('class' => 'checklist'), _convert_options($options)));
} 

This is a first draft of the helpers, so input would be greatly appreciated. This functionality could probably be merged with object_select_tag() et al, in keeping with the DRY concept.

Hope people find this useful!

EDIT: changed ereg_replace() to preg_replace()

Attachments