<?php
/**
 * Copyright (c) 2012-2017, Agence Française Informatique (AFI). All rights reserved.
 *
 * BOKEH is free software; you can redistribute it and/or modify
 * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
 * the Free Software Foundation.
 *
 * There are special exceptions to the terms and conditions of the AGPL as it
 * is applied to this software (see README file).
 *
 * BOKEH 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 AFFERO GENERAL PUBLIC LICENSE for more details.
 *
 * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
 * along with BOKEH; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
 */


class ZendAfi_View_Helper_FormRadio extends ZendAfi_View_Helper_FormElement {

  protected
    $_inputType = 'radio',
    $_isArray = false;


  public function formRadio($name, $value = null, $attribs = null, $options = null, $listsep = "<br />\n") {
    $info = $this->_getInfo($name, $value, $attribs, $options, $listsep);
    extract($info);

    $label_attribs = array('style' => 'white-space: nowrap;');
    foreach ($attribs as $key => $val) {
      $tmp    = false;
      $keyLen = strlen($key);
      if ((6 < $keyLen) && (substr($key, 0, 6) == 'label_')) {
        $tmp = substr($key, 6);
      } elseif ((5 < $keyLen) && (substr($key, 0, 5) == 'label')) {
        $tmp = substr($key, 5);
      }

      if ($tmp) {
        // make sure first char is lowercase
        $tmp[0] = strtolower($tmp[0]);
        $label_attribs[$tmp] = $val;
        unset($attribs[$key]);
      }
    }

    $labelPlacement = 'append';
    foreach ($label_attribs as $key => $val) {
      switch (strtolower($key)) {
        case 'placement':
          unset($label_attribs[$key]);
          $val = strtolower($val);
          if (in_array($val, array('prepend', 'append'))) {
            $labelPlacement = $val;
          }
          break;
      }
    }

    // the radio button values and labels
    $options = (array) $options;

    // build the element
    $xhtml = '';
    $list  = array();

    // should the name affect an array collection?
    $name = $this->view->escape($name);
    if ($this->_isArray && ('[]' != substr($name, -2))) {
      $name .= '[]';
    }

    // ensure value is an array to allow matching multiple times
    $value = (array) $value;

    // XHTML or HTML end tag?
    $endTag = ' />';
    if (($this->view instanceof Zend_View_Abstract) && !$this->view->doctype()->isXhtml()) {
      $endTag= '>';
    }

    // add radio buttons to the list.
    require_once 'Zend/Filter/Alnum.php';
    $filter = new Zend_Filter_Alnum();
    foreach ($options as $opt_value => $opt_label) {

      // Should the label be escaped?
      if ($escape) {
        $opt_label = $this->view->escape($opt_label);
      }

      $input_attribs = [];
      if ((true === $disable) || (is_array($disable) && in_array($opt_value, $disable)))
        $input_attribs ['disabled'] = 'disabled';

      if (in_array($opt_value, $value))
        $input_attribs['checked'] = 'checked';

      // generate ID
      $optId = $id . '-' . $filter->filter($opt_value);

      $radio = $this->view->tag('label',
                                $this->view->tag('input',
                                                 null,
                                                 array_merge(['type' => $this->_inputType,
                                                              'name' => $name,
                                                              'id' => $optId,
                                                              'value' => $opt_value],
                                                             $input_attribs,
                                                             $attribs),
                                                 $label_attribs)
                                . $opt_label,
                                ['class' => 'multi-element-label']);

      // add to the array of radio buttons
      $list[] = $radio;
    }

    // done!
    $xhtml .= implode($listsep, $list);

    return $xhtml;
  }
}