<?php

      /**

       * Smarty plugin

       * @package Smarty

       * @subpackage plugins

       */



      /**

       * Smarty {html_select_object} plugin

       *

       * Type: function<br>

       * Name: html_select_object<br>

       * Purpose: Prints the dropdown for an object selection.

       * Takes a list of data objects and the attribute

       * names for the value and output data

      * There are two modes for direct field and

       * getter based data access

       *

       * ChangeLog:<br>

       * - 1.0 initial release

       * @link http://www.bar54.de/html_select_object.0.html}

       *

       * @version 1.0

       * @author Benjamin Klatt

       * @param array

       * @param Smarty

       * @return string

       */

      function smarty_function_html_select_object($params, &$smarty) {

      require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');



      /* The name of the select tag. If not set no attribute will be added */

      $name = null;



      /* The id attribute of the select tag. If not set no attribute will be added */

      $id = null;



      /* The name of the data field to take the value data from */

      $valuefield = null;



      /* The name of the data field to take the output for the dropdown from */

      $outputfield = null;



      /* The mode how to access the object data.

        * possible are 'getter' (default) and 'direct'

        */

      $mode = 'getter';



      /* The selected value. If not set nothing will be defined as selected */

      $selected = null;



      /* <select size>'s of the <select> tags.

        If not set, uses default dropdown. */

      $size = null;



      /* Unparsed attributes for the <select> tag.

        An example might be in the template: all_extra ='class ="foo"'. */

      $extra_attributes = null;



      /* Optional placeholder for the first items value in the dropdown.

        If not set, it won't be shown */

      $emptyvalue = null;



      /* Optional placeholder for the first items output string */

    $emptyoutput = null;





      /* The name array containing the data objects */

      $array = array();

      $result = null;



      /* Process the submitted parameters */

      foreach ($params as $_key=>$_value) {

      switch ($_key) {

      case 'name':

      case 'id';

      case 'valuefield';

      case 'outputfield';

      case 'mode':

      case 'selected':

      case 'size':

      case 'emptyvalue':

      case 'emptyoutput':

      $$_key = (string)$_value;

      break;



      case 'empty':

      $emptyvalue = (string)$_value;

      $emptyoutput = (string)$_value;

      break;



      case 'array':

      if(is_array($_value)){

      $array = $_value;

      } else {

      $smarty->trigger_error("html_select_object: '$_key'

      has to be an array of objects", E_USER_NOTICE);

     }

      break;



      default:

      if(!is_array($_value)) {

      $extra_attributes.= ' '.$_key.'="'

      .smarty_function_escape_special_chars($_value).'"';

      } else {

      $smarty->trigger_error("html_select_object: extra attribute '$_key'

      cannot be an array", E_USER_NOTICE);

      }

      break;

      }

      }



      // set the flag whether to use getters or not

      $useGetter = ($mode == 'getter');





      // build up the number array

      $result .= '<select';

      if(null != $name){

      $result .= ' name="'.$name.'"';

      }

      if(null != $id){

      $result .= ' id="'.$id.'"';

      }

      if (null !== $size){

      $result .= ' size="' . $size . '"';

      }

      if (null !== $extra_attributes){

      $result .= ' ' . $extra_attributes;

   }

      $result .= '>'."\n";



      if (null !== $emptyvalue && null !== $emptyoutput){

      $result .= '<option value="'.$emptyvalue.'">'.$emptyoutput.'</option>'."\n";

      }







      // append the object options

      foreach($array as $item){



      // <option ...>

      $result .= '<option';

      if($useGetter){

      $functionname = "get".ucfirst($valuefield);

      $value = $item->$functionname();

      } else {

      $value = $item->$valuefield;

      }



      $result .= ' value="'.$value.'"';

      if($value == $selected){

      $result .= ' selected';

      }

     $result .= '>';



      // output

      if($useGetter){

      $functionname = "get".ucfirst($outputfield);

      $result .= $item->$functionname();

      } else {

      $result .= $item->$outputfield;

      }



      // </option>

      $result .= '</option>'."\n";



      }



      // append the closing select

      $result .= '</select>';



      return $result;

      }
 ?>