/includes/skip/elements/forms/checkbox.php

  1. <?php 
  2. /** 
  3. * Skip Checkbox class 
  4. * @package Skip\Forms 
  5. * @since 1.0 
  6. * @ignore 
  7. */ 
  8.  
  9. namespace skip\v1_0_0; 
  10.  
  11. class Checkbox extends Form_Element{ 
  12.  
  13. /** 
  14. * Constructor 
  15. * @since 1.0 
  16. * @param string $name The name of the checkbox field. 
  17. * @param string $value The value of the checkbox element which will be saved if box is checked. 
  18. * @param array/string $args List of Arguments. 
  19. */ 
  20. function __construct( $name, $value, $label = FALSE, $args = array() ) { 
  21. global $skip_hidden_elements; 
  22.  
  23. $defaults = array( 
  24. 'checked' => FALSE,  
  25. ); 
  26. $args = wp_parse_args( $args, $defaults ); 
  27. $args[ 'label' ] = $label; 
  28. $args[ 'close_tag' ] = FALSE; // No Close tag for Input type Button 
  29.  
  30. parent::__construct( 'input', $name, $args ); 
  31.  
  32. $this->add_param( 'type', 'checkbox' ); 
  33. $this->add_param( 'value', $value ); // Overwriting value from DB 
  34.  
  35. if( '' != $this->value || $this->value == 'checked' || $args[ 'checked' ] ) 
  36. $this->add_param( 'checked', 'checked' ); 
  37.  
  38. }  
  39. /** 
  40. * Checkbox getter Function 
  41. * @see checkbox() 
  42. * @ignore 
  43. */ 
  44. function get_checkbox( $name, $value, $label = FALSE, $args = array() ) { 
  45. $checkbox = new Checkbox( $name, $value, $label, $args ); 
  46. return $checkbox->render(); 
  47.  
  48. /** 
  49. * <pre>skip_checkbox( $name, $value, $elements, $args );</pre> 
  50. *  
  51. * Adding a checkbox field. 
  52. *  
  53. * <b>Default Usage</b> 
  54. * <code> 
  55. * skip_checkbox( 'overeighteen', 'yes' ); 
  56. * </code> 
  57. * This will create an automated saved checkbox field. 
  58. *  
  59. * <b>Parameters</b> 
  60. *  
  61. * <code> 
  62. * $name // (string) (required) The name of the field. 
  63. * $value // (string) (required) The value of the checkbox element which will be saved if box is checked. 
  64. * $args // (array/string) (optional) Values for further settings. 
  65. * </code> 
  66. *  
  67. * <b>$args Settings</b> 
  68. *  
  69. * <ul> 
  70. * <li>id (string) ID if the HTML Element.</li>  
  71. * <li>label (string) Label for Element.</li>  
  72. * <li>default (string) Default Value if no Value is set before ('checked' if value have to be checked on default).</li> 
  73. * <li>classes (string) Name of CSS Classes which will be inserted into HTML seperated by empty space.</li> 
  74. * <li>before_element (string) Content before the element.</li> 
  75. * <li>after_element (string) Content after the element.</li> 
  76. * <li>save (boolean) TRUE if value of field have to be saved in Database, FALSE if not (default TRUE).</li> 
  77. * </ul> 
  78. *  
  79. * <b>Example</b> 
  80. *  
  81. * Creating a labeled Checkbox in an automatic saved form. 
  82. * <code> 
  83. * skip_form_start( 'myformname' ); 
  84. *  
  85. * $args = array( 
  86. * 'id' = 'myelementid',  
  87. * 'label' => 'City' 
  88. * ); 
  89. * skip_autocomplete( 'overeighteen', 'yes', $args ); 
  90. *  
  91. * skip_form_end(); 
  92. * </code> 
  93. *  
  94. * Getting back the saved data. 
  95. * <code> 
  96. * $city = skip_value( 'myformname', 'overeighteen' ); 
  97. * </code> 
  98. * @package Skip\Forms 
  99. * @since 1.0 
  100. * @param string $name The name of the checkbox field. 
  101. * @param string $value The value of the checkbox element which will be saved if box is checked. 
  102. * @param array/string $args List of Arguments. 
  103. */ 
  104. function checkbox( $name, $value, $label = FALSE, $args = array() ) { 
  105. echo get_checkbox( $name, $value, $label, $args ); 
.