Ejemplo n.º 1
0
 /**
  * Generates a SELECT tag
  *
  * @param array parameters
  * @param array data
  */
 public static function selectField($parameters, $data = null)
 {
     if (!is_array($parameters)) {
         $params = array($parameters, $data);
     } else {
         $params = $parameters;
     }
     $id = null;
     if (isset($params[0])) {
         $id = $params[0];
     } else {
         if (isset($params['id'])) {
             $params[0] = $params['id'];
             $id = $params[0];
         }
     }
     /**
      * Automatically assign the id if the name is not an array
      */
     if (false === strpos($id, '[')) {
         if (!isset($params['id'])) {
             $params['id'] = $id;
         }
     }
     if (!isset($params['name'])) {
         $params['name'] = $id;
     } else {
         $name = $params['name'];
         if (!$name) {
             $params['name'] = $id;
         }
     }
     if (!isset($params['value'])) {
         $value = BaseTag::getValue($id, $params);
     } else {
         $value = $params['value'];
         unset($params['value']);
     }
     if (isset($params['useEmpty'])) {
         $useEmpty = $params['useEmpty'];
         if (!isset($params['emptyValue'])) {
             $emptyValue = '';
         } else {
             $emptyValue = $params['emptyValue'];
             unset($params['emptyValue']);
         }
         if (!isset($params['emptyText'])) {
             $emptyText = 'Choose...';
         } else {
             $emptyText = $params['emptyText'];
             unset($params['emptyText']);
         }
         unset($params['useEmpty']);
     }
     if (!isset($params[1])) {
         $options = $data;
     } else {
         $options = $params[1];
     }
     if (is_object($options)) {
         /**
          * The options is a resultset
          */
         if (!isset($params['using'])) {
             throw new Exception('The \'using\' parameter is required');
         } else {
             $using = $params['using'];
             if (!is_array($using) && !is_object($using)) {
                 throw new Exception('The \'using\' parameter should be an array');
             }
         }
     }
     unset($params['using']);
     $code = BaseTag::renderAttributes('<select', $params) . '>' . PHP_EOL;
     if ($useEmpty) {
         /**
          * Create an empty value
          */
         $code .= '<option value="' . $emptyValue . '">' . $emptyText . '</option>' . PHP_EOL;
     }
     if (is_object($options)) {
         /**
          * Create the SELECT's option from a resultset
          */
         $code .= self::_optionsFromResultset($options, $using, $value, '</option>' . PHP_EOL);
     } else {
         if (is_array($options)) {
             /**
              * Create the SELECT's option from an array
              */
             $code .= self::_optionsFromArray($options, $value, '</option>' . PHP_EOL);
         } else {
             throw new Exception('Invalid data provided to SELECT helper');
         }
     }
     $code .= '</select>';
     return $code;
 }
Ejemplo n.º 2
0
 /**
  * Generate the HTML to label the element
  *
  * @param array attributes
  * @return string
  */
 public function label($attributes = null)
 {
     /**
      * Check if there is an "id" attribute defined
      */
     $internalAttributes = $this->getAttributes();
     if (!isset($internalAttributes["id"])) {
         $name = $this->_name;
     } else {
         $name = $internalAttributes["id"];
     }
     if (is_array($attributes)) {
         if (!isset($attributes["for"])) {
             $attributes["for"] = $name;
         }
     } else {
         $attributes = array('for' => $name);
     }
     $code = Tag::renderAttributes('<label', $attributes);
     /**
      * Use the default label or leave the same name as label
      */
     $label = $this->_label;
     if ($label) {
         $code .= '>' . $label . '</label>';
     } else {
         $code .= '>' . $name . '</label>';
     }
     return $code;
 }