Overview

Packages

  • Geolib

Classes

  • Geo
  • GeoBody
  • GeoCell
  • GeoDebug
  • GeoHead
  • GeoHtml
  • GeoImg
  • GeoLink
  • GeoList
  • GeoSelect
  • GeoTable
  • GeoTag

Functions

  • b
  • div
  • geoAbsLink
  • geoAnchor
  • geoBody
  • geoButton
  • geoCell
  • geoCheckbox
  • geoDb
  • geoEnd
  • geoFieldSet
  • geoForm
  • geoHead
  • geoHidden
  • geoHtml
  • geoif
  • geoIList
  • geoImg
  • geoImgSubmit
  • geoInput
  • geoIsMultiArr
  • geoItem
  • geoJSLink
  • geoLabel
  • geoLegend
  • geolib_autoloader
  • geoLink
  • geoList
  • geoMultiArr
  • geoNoScript
  • geoPassword
  • geoPre
  • geoRadio
  • geoRadios
  • geoScript
  • geoSelect
  • geoStart
  • geoSubmit
  • geoTable
  • geoTabs
  • geoTag
  • geoText
  • geoTextArea
  • geoTrace
  • geoUpload
  • geovar
  • h1
  • h2
  • h3
  • h4
  • i
  • idiv
  • ispan
  • itag
  • p
  • span
  • Overview
  • Package
  • Function
  1: <?php
  2: 
  3: /**
  4:  * Document for class GeoSelect
  5:  *
  6:  * PHP Version 5.6
  7:  *
  8:  * @category Class
  9:  * @package  Geolib
 10:  * @author   Peter Pitchford <peter@geotonics.com>
 11:  * @license  GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html
 12:  * @link     http://geotonics.com/#geolib
 13:  */
 14:  
 15: /**
 16: * Geo - Class to create HTML select tags
 17: *
 18:  * @category Html_Tag
 19:  * @package  Geolib
 20:  * @author   Peter Pitchford <peter@geotonics.com>
 21:  * @license  GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html
 22:  * @version  Release: .1
 23:  * @link     http://geotonics.com/#geolib
 24:  * @since    Class available since Release .1
 25: */
 26: class GeoSelect extends GeoTag
 27: {
 28:     // takes array of options in the form array(id, name);
 29:     private $selected;
 30:     private $size;
 31:     
 32:     
 33:     
 34:     /**
 35:      * Construct html select tag
 36:      *
 37:      * @param string            $name     Name of select tag
 38:      * @param array             $options  1 or 2 dimensional array of options
 39:      *     If 1 dimensional each row is  $name=>$value
 40:      *     If 2 dimensional each array is 0=>name 1=>value 2=>class
 41:      * @param string            $selected name of selected option
 42:      * @param string            $id       id of select tag
 43:      * @param array|string|bool $atts     attributes of select tag
 44:      *     If array, is atts of select tag.
 45:      *     If string, is a single default att
 46:      *     If PHP true, att is onchange=>javascript to submit form
 47:      * @param string            $size     size att of select tag
 48:      * @param string            $class    class of select tag
 49:      * @param string            $style    style of select tag
 50:      */
 51:     public function __construct(
 52:         $name = null,
 53:         $options = null,
 54:         $selected = null,
 55:         $id = null,
 56:         $atts = null,
 57:         $size = null,
 58:         $class = null,
 59:         $style = null
 60:     ) {
 61:         
 62:         // allows simpler key->value options array, but no additional option atts
 63:         // for other options, use 2d array
 64:         //GeoDebug::db($options, 'options');
 65:         
 66:         if ($options && is_array($options) && !geoIsMultiArr($options)) {
 67:             foreach ($options as $key => $value) {
 68:                 if (is_object($value)) {
 69:                     if ($value->name) {
 70:                         $value = $value->name;
 71:                     } elseif (isset($value->machinename)) {
 72:                         $value = $value->machinename;
 73:                     } else {
 74:                         continue;
 75:                     }
 76:                 }
 77:                 $options2[] = array(
 78:                     "id" => $key,
 79:                     "name" => $value
 80:                 );
 81:             }
 82:             $options = $options2;
 83:         }
 84:         if ($atts === true) {
 85:             $atts = "this.form.submit()";
 86:         }
 87:         
 88:         if ($atts && !is_array($atts)) {
 89:             $newatts['onchange'] = $atts;
 90:             $atts                = $newatts;
 91:         }
 92: 
 93:         if (!isset($id) && strpos($name, "[") === false) {
 94:             $id = $name;
 95:         }
 96:         
 97:         $atts['name'] = $name;
 98:         
 99:         if ($size) {
100:             $atts['size'] = $size;
101:         }
102:         
103:         $this->init("select", $options, $class, $id, $style, $atts);
104:         //GeoDebug::db($this, 'this');
105:         $this->setSelected($selected);
106:     }
107:     
108:     /**
109:      * Set selected option
110:      *
111:      * @param string $selected name of selected option
112:      *
113:      * @return void
114:      */
115:     public function setSelected($selected)
116:     {
117:         if (is_array($selected)) {
118:             array_unshift($this->text, $selected);
119:             $this->selected = $selected['id'];
120:         } elseif (isset($selected)) {
121:             $this->selected = $selected;
122:         } elseif (isset($this->atts['name']) && isset($_POST[$this->atts['name']])) {
123:             $this->selected = $_POST[$this->atts['name']];
124:         }
125:     }
126:     
127:     /**
128:      * Output HTML select tag
129:      *
130:      * @return html select tag
131:      */
132:     public function tag()
133:     {
134:         if ($this->text) {
135:             $result      = '';
136:             $hasSelected = null;
137:             foreach ($this->text as $optNum => $option) {
138:                 //GeoDebug::db($option, $optNum);
139:                 if (is_array($option)) {
140:                     $option = array_values($option);
141:                 } else {
142:                     $option = array(
143:                         $option,
144:                         $option
145:                     );
146:                 }
147:                 
148:                 $atts          = array();
149:                 $atts['value'] = $option[0];
150:                
151:                 if (isset($this->selected) && ((string)$option[0] == (string)$this->selected) && !$hasSelected) {
152:                     $atts["selected"] = "selected";
153:                     $hasSelected      = true;
154:                    
155:                 }
156: 
157:                 if (isset($option[2])) {
158:                     $atts["class"] = $option[2];
159:                 }
160: 
161:                 if (isset($option[1])) {
162:                     // disallow empty options
163:                     $result .= geoTag("option", $option[1], null, null, null, $atts);
164:                 }
165:             }
166:             $this->text = $result;
167:         }
168: 
169:         return parent::baseTag();
170:     }
171: }
172: 
API documentation generated by ApiGen