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
 /**
  * Returns the element value
  *
  * @return mixed
  */
 public function getValue()
 {
     $name = $this->_name;
     $value = null;
     /**
      * Get the related form
      */
     $form = $this->_form;
     if (is_object($form)) {
         /**
          * Gets the possible value for the widget
          */
         $value = $form->getValue($name);
         /**
          * Check if the tag has a default value
          */
         if (is_null($value) && Tag::hasValue($name)) {
             $value = Tag::getValue($name);
         }
     }
     /**
      * Assign the default value if there is no form available
      */
     if (is_null($value)) {
         $value = $this->_value;
     }
     return $value;
 }