/includes/skip/elements/forms/select.php

  1. <?php  
  2. /** 
  3. * Skip Select Class 
  4. * @package Skip\Forms 
  5. * @since 1.0 
  6. * @ignore 
  7. */ 
  8.  
  9. namespace skip\v1_0_0; 
  10.  
  11. class Select extends Form_Element{ 
  12. /** 
  13. * Constructor 
  14. * @since 1.0 
  15. * @param string $name Name of Color field. 
  16. * @param array/string $args List of Arguments. 
  17. */ 
  18. function __construct( $name, $label = FALSE, $args = array() ) { 
  19. $defaults = array( 
  20. 'size' => '',  
  21. 'multiselect' => FALSE,  
  22. ); 
  23.  
  24. $args = wp_parse_args( $args, $defaults ); 
  25.  
  26. $args[ 'label' ] = $label; 
  27. $args[ 'close_tag' ] = TRUE; 
  28.  
  29. parent::__construct( 'select', $name, $args ); 
  30.  
  31. if( '' != $args[ 'size' ] ) 
  32. $this->add_param( 'size', $args[ 'size' ] ); 
  33.  
  34. if( $args[ 'multiselect' ] ) 
  35. $this->add_param( 'multiple', 'multiple' ); 
  36.  
  37. /** 
  38. * Adding element 
  39. *  
  40. * Adds an option to the select field. 
  41. *  
  42. * <code> 
  43. * $element = array( 
  44. * 'label' => 'Option 1',  
  45. * 'value' => 1 
  46. * ); 
  47. * </code> 
  48. * Example for an element which can be added.  
  49. *  
  50. * @since 1.0 
  51. * @param array/string $element The element 
  52. */ 
  53. public function add_element( $element = array() ) { 
  54. $defaults = array( 
  55. 'id' => id(),  
  56. 'value' => '',  
  57. 'label' => $element['value'],  
  58. 'disabled' => FALSE,  
  59. 'params' => array() 
  60. ); 
  61.  
  62. $element = wp_parse_args( $element, $defaults ); 
  63.  
  64. if( !$element['disabled'] ) 
  65. unset( $element['disabled'] ); 
  66.  
  67. parent::add_element( $element ); 
  68.  
  69. /** 
  70. * Rendering Select field 
  71. * @package Skip 
  72. * @since 1.0 
  73. * @return string $html Returns The HTML Code. 
  74. */  
  75. public function render() { 
  76. $this->content = ''; 
  77.  
  78. foreach( $this->elements AS $element ): 
  79. $params = $element['params']; 
  80. unset( $element['params'] ); 
  81.  
  82. $label = $element['label']; 
  83. unset( $element['label'] ); 
  84.  
  85. $params = array_merge( $element, $params ); 
  86.  
  87. if( $params[ 'value' ] == $this->value ) 
  88. $params[ 'selected' ] = 'selected'; 
  89.  
  90. $this->content.= '<option' . $this->params( $params ) . '>' . $label . '</option>'; 
  91. endforeach; 
  92.  
  93. return parent::render(); 
  94. }  
  95.  
  96. /** 
  97. * Select getter Function 
  98. * @see skip_select() 
  99. * @ignore 
  100. */ 
  101. function get_select( $name, $elements, $label = FALSE, $args = array(), $return = 'html' ) { 
  102. $select = new Select( $name, $label, $args ); 
  103.  
  104. if( count( $elements ) > 0 ): 
  105. if( is_array( $elements ) ): 
  106. foreach ( $elements AS $element ) 
  107. $select->add_element( $element ); 
  108. else: 
  109. $values = explode( ', ', $elements ); 
  110. foreach ( $values AS $value ) 
  111. $select->add_element( array( 'value' => $value ) ); 
  112. endif; 
  113. endif; 
  114.  
  115. return $select->render(); 
  116. }  
  117. /** 
  118. * <pre>skip_select( $name, $elements, $args );</pre> 
  119. *  
  120. * Adding a select field. 
  121. *  
  122. * <b>Default Usage</b> 
  123. * <code> 
  124. * skip_select( 'myselect', '1, 2, 3, 4, 5, 6, 7, 8, 9, 10' ); 
  125. * </code> 
  126. * This will create an automated saved select field with values 1 till 10. 
  127. *  
  128. * <b>Parameters</b> 
  129. *  
  130. * <code> 
  131. * $name // (string) (required) The name of the field. 
  132. * $elements // (array/string) (required) The elements to show in Select. 
  133. * $args // (array/string) (optional) Values for further settings. 
  134. * </code> 
  135. *  
  136. * <b>$args Settings</b> 
  137. *  
  138. * <ul> 
  139. * <li>id (string) ID if the HTML Element.</li>  
  140. * <li>label (string) Label for Element.</li>  
  141. * <li>default (string) Default Value if no Value is set before ('checked' if value have to be checked on default).</li> 
  142. * <li>classes (string) Name of CSS Classes which will be inserted into HTML seperated by empty space.</li> 
  143. * <li>before_element (string) Content before the element.</li> 
  144. * <li>after_element (string) Content after the element.</li> 
  145. * <li>save (boolean) TRUE if value of field have to be saved in Database, FALSE if not (default TRUE).</li> 
  146. * </ul> 
  147. *  
  148. * <b>Example</b> 
  149. *  
  150. * Creating a select field with values Opion 1 till 3 in a form. 
  151. * <code> 
  152. * skip_form_start( 'myformname' ); 
  153. *  
  154. * $elements = array( 
  155. * array( 'label' => 'Option 1', 'value' => '1' ),  
  156. * array( 'label' => 'Option 2', 'value' => '2' ),  
  157. * array( 'label' => 'Option 3', 'value' => '3' ),  
  158. * ); 
  159. *  
  160. * $args = array( 
  161. * 'id' = 'myelementid',  
  162. * 'label' => 'Option' 
  163. * ); 
  164. * skip_select( 'myselect', $elements, $args ); 
  165. *  
  166. * skip_form_end(); 
  167. * </code> 
  168. *  
  169. * Getting back the saved data. 
  170. * <code> 
  171. * $value = skip_value( 'myformname', 'myselect' ); 
  172. * </code> 
  173. * @package Skip\Forms 
  174. * @since 1.0 
  175. * @param string $name The name of the checkbox field. 
  176. * @param array/string $elements The elements to show in Select. 
  177. * @param array/string $args List of Arguments. 
  178. */ 
  179. function select( $name, $elements, $label = FALSE, $args = array() ) { 
  180. echo get_select( $name, $elements, $label, $args ); 
.