PageRenderTime 47ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/wordpress/wp-content/plugins/custom-content-type-manager/includes/elements/checkbox.php

http://ownerpress.googlecode.com/
PHP | 262 lines | 123 code | 18 blank | 121 comment | 7 complexity | b69696b7e90821aa63da4da6ce56ccf2 MD5 | raw file
Possible License(s): Apache-2.0, AGPL-1.0, GPL-2.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * CCTM_checkbox
  4. *
  5. * Implements an HTML text input.
  6. *
  7. */
  8. class CCTM_checkbox extends FormElement
  9. {
  10. /**
  11. * The $props array acts as a template which defines the properties for each instance of this type of field.
  12. * When added to a post_type, an instance of this data structure is stored in the array of custom_fields.
  13. * Some properties are required of all fields (see below), some are automatically generated (see below), but
  14. * each type of custom field (i.e. each class that extends FormElement) can have whatever properties it needs
  15. * in order to work, e.g. a dropdown field uses an 'options' property to define a list of possible values.
  16. *
  17. *
  18. *
  19. * The following properties MUST be implemented:
  20. * 'name' => Unique name for an instance of this type of field; corresponds to wp_postmeta.meta_key for each post
  21. * 'label' =>
  22. * 'description' => a description of this type of field.
  23. *
  24. * The following properties are set automatically:
  25. *
  26. * 'type' => the name of this class, minus the CCTM_ prefix.
  27. * 'sort_param' => populated via the drag-and-drop behavior on "Manage Custom Fields" page.
  28. */
  29. public $props = array(
  30. 'label' => '',
  31. 'name' => '',
  32. 'description' => '',
  33. // checked_by_default determines whether 'checked_value' or 'unchecked_value' is passed to
  34. // the current value for new field instances. This value should be 1 (checked) or 0 (unchecked)
  35. 'checked_by_default' => '0',
  36. 'checked_value' => '1',
  37. 'unchecked_value' => '0',
  38. 'class' => '',
  39. 'extra' => '',
  40. // 'type' => '', // auto-populated: the name of the class, minus the CCTM_ prefix.
  41. // 'sort_param' => '', // handled automatically
  42. );
  43. //------------------------------------------------------------------------------
  44. /**
  45. * This function provides a name for this type of field. This should return plain
  46. * text (no HTML). The returned value should be localized using the __() function.
  47. * @return string
  48. */
  49. public function get_name() {
  50. return __('Checkbox',CCTM_TXTDOMAIN);
  51. }
  52. //------------------------------------------------------------------------------
  53. /**
  54. * Used to drive a thickbox pop-up when a user clicks "See Example"
  55. */
  56. public function get_example_image() {
  57. return '';
  58. }
  59. //------------------------------------------------------------------------------
  60. /**
  61. * This function gives a description of this type of field so users will know
  62. * whether or not they want to add this type of field to their custom content
  63. * type. The returned value should be localized using the __() function.
  64. * @return string text description
  65. */
  66. public function get_description() {
  67. return __('Checkbox fields implement the standard <input="checkbox"> element.
  68. "Extra" parameters, e.g. "alt" can be specified in the definition.',CCTM_TXTDOMAIN);
  69. }
  70. //------------------------------------------------------------------------------
  71. /**
  72. * This function should return the URL where users can read more information about
  73. * the type of field that they want to add to their post_type. The string may
  74. * be localized using __() if necessary (e.g. for language-specific pages)
  75. * @return string e.g. http://www.yoursite.com/some/page.html
  76. */
  77. public function get_url() {
  78. return 'http://code.google.com/p/wordpress-custom-content-type-manager/wiki/Checkbox';
  79. }
  80. //------------------------------------------------------------------------------
  81. /**
  82. *
  83. *
  84. * @param mixed $def associative array containing the full definition for this type of element.
  85. * @param string HTML to be used in the WP manager for an instance of this type of element.
  86. */
  87. public function get_create_field_instance() {
  88. if ( $this->props['checked_by_default']) {
  89. $current_value = $this->props['checked_value'];
  90. }
  91. else {
  92. $current_value = $this->props['unchecked_value'];
  93. }
  94. return $this->get_edit_field_instance($current_value); // pass on to
  95. }
  96. //------------------------------------------------------------------------------
  97. /**
  98. *
  99. * @param string $current_value
  100. * @return string
  101. <input type="checkbox" name="[+name+]" class="formgenerator_checkbox" id="[+id+]" value="[+checked_value+]" [+is_checked+] [+extra+]/>
  102. <label for="[+id+]" class="formgenerator_label formgenerator_checkbox_label" id="formgenerator_label_[+name+]">[+label+]</label>';
  103. $output = sprintf('
  104. %s
  105. <input type="text" name="%s" class="%s" id="%s" %s value="%s"/>
  106. '
  107. , $this->wrap_label()
  108. , $this->get_field_name()
  109. , $this->get_field_class($this->name, 'text') . ' ' . $this->class
  110. , $this->get_field_id()
  111. , stripslashes($this->extra)
  112. , $current_value
  113. );
  114. return $this->wrap_outer($output);
  115. */
  116. public function get_edit_field_instance($current_value) {
  117. # print $current_value; exit;
  118. # print_r($this->props); exit;
  119. #print_r($def); exit;
  120. $is_checked = '';
  121. if ($current_value == $this->checked_value) {
  122. $is_checked = 'checked="checked"';
  123. }
  124. $output = sprintf('
  125. <input type="checkbox" name="%s" class="%s" id="%s" value="%s" %s %s/>
  126. '
  127. , $this->get_field_name()
  128. , $this->get_field_class($this->name, 'checkbox') . ' ' . $this->class
  129. , $this->get_field_id()
  130. , $this->checked_value
  131. , $this->extra
  132. , $is_checked
  133. );
  134. $output .= $this->wrap_label();
  135. return $this->wrap_outer($output);
  136. }
  137. //------------------------------------------------------------------------------
  138. /**
  139. *
  140. *
  141. * @param unknown $current_values
  142. <style>
  143. input.cctm_error {
  144. background: #fed; border: 1px solid red;
  145. }
  146. </style>
  147. */
  148. public function get_edit_field_definition($def) {
  149. $is_checked = '';
  150. if ($def['checked_by_default']) {
  151. $is_checked = 'checked="checked"';
  152. }
  153. // Label
  154. $out = '<div class="'.self::wrapper_css_class .'" id="label_wrapper">
  155. <label for="label" class="'.self::label_css_class.'">'
  156. .__('Label', CCTM_TXTDOMAIN).'</label>
  157. <input type="text" name="label" class="'.self::css_class_prefix.'text" id="label" value="'.$def['label'] .'"/>
  158. ' . $this->get_translation('label').'
  159. </div>';
  160. // Name
  161. $out .= '<div class="'.self::wrapper_css_class .'" id="name_wrapper">
  162. <label for="name" class="formgenerator_label formgenerator_text_label" id="name_label">'
  163. . __('Name', CCTM_TXTDOMAIN) .
  164. '</label>
  165. <input type="text" name="name" class="'.$this->get_field_class('name','text').'" id="name" value="'.$def['name'] .'"/>'
  166. . $this->get_translation('name') .'
  167. </div>';
  168. // Value when Checked
  169. $out .= '<div class="'.self::wrapper_css_class .'" id="checked_value_wrapper">
  170. <label for="checked_value" class="formgenerator_label formgenerator_text_label" id="checked_value_label">'
  171. . __('Value when checked', CCTM_TXTDOMAIN) .
  172. '</label>
  173. <input type="text" name="checked_value" size="8" class="'.$this->get_field_class('checked_value','text').'" id="checked_value" value="'.$def['checked_value'] .'"/>'
  174. . $this->get_translation('checked_value') .'
  175. </div>';
  176. // Value when Unchecked
  177. $out .= '<div class="'.self::wrapper_css_class .'" id="unchecked_value_wrapper">
  178. <label for="unchecked_value" class="formgenerator_label formgenerator_text_label" id="unchecked_value_label">'
  179. . __('Value when Unchecked', CCTM_TXTDOMAIN) .
  180. '</label>
  181. <input type="text" name="unchecked_value" size="8" class="'.$this->get_field_class('unchecked_value','text').'" id="unchecked_value" value="'.$def['unchecked_value'] .'"/>'
  182. . $this->get_translation('unchecked_value') .'
  183. </div>';
  184. // Is Checked by Default?
  185. $out .= '<div class="'.self::wrapper_css_class .'" id="checked_by_default_wrapper">
  186. <label for="checked_by_default" class="formgenerator_label formgenerator_checkbox_label" id="checked_by_default_label">'
  187. . __('Checked by default?', CCTM_TXTDOMAIN) .
  188. '</label>
  189. <br />
  190. <input type="checkbox" name="checked_by_default" class="'.$this->get_field_class('checked_by_default','checkbox').'" id="checked_by_default" value="1" '. $is_checked.'/> <span>'.$this->descriptions['checked_by_default'].'</span>
  191. </div>';
  192. // Extra
  193. $out .= '<div class="'.self::wrapper_css_class .'" id="extra_wrapper">
  194. <label for="extra" class="'.self::label_css_class.'">'
  195. .__('Extra', CCTM_TXTDOMAIN) .'</label>
  196. <input type="text" name="extra" class="'.$this->get_field_class('extra','text').'" id="extra" value="'
  197. .htmlentities(stripslashes($def['extra'])).'"/>
  198. ' . $this->get_translation('extra').'
  199. </div>';
  200. // Class
  201. $out .= '<div class="'.self::wrapper_css_class .'" id="class_wrapper">
  202. <label for="class" class="'.self::label_css_class.'">'
  203. .__('Class', CCTM_TXTDOMAIN) .'</label>
  204. <input type="text" name="class" class="'.$this->get_field_class('class','text').'" id="class" value="'
  205. .strip_tags(stripslashes($def['class'])).'"/>
  206. ' . $this->get_translation('class').'
  207. </div>';
  208. // Description
  209. $out .= '<div class="'.self::wrapper_css_class .'" id="description_wrapper">
  210. <label for="description" class="'.self::label_css_class.'">'
  211. .__('Description', CCTM_TXTDOMAIN) .'</label>
  212. <textarea name="description" class="'.$this->get_field_class('description','textarea').'" id="description" rows="5" cols="60">'.$def['description'].'</textarea>
  213. ' . $this->get_translation('description').'
  214. </div>';
  215. return $out;
  216. }
  217. //------------------------------------------------------------------------------
  218. /**
  219. * Here we do some smoothing of the checkbox warts... normally if the box is not
  220. * checked, no value is sent in the $_POST array. But that's a pain in the ass
  221. * when it comes time to read from the database, so here we toggle between
  222. * 'checked_value' and 'unchecked_value' to force a value under all circumstances.
  223. *
  224. * See parent function for full documentation.
  225. *
  226. * @param mixed $posted_data $_POST data
  227. * @param string $field_name: the unique name for this instance of the field
  228. * @return string whatever value you want to store in the wp_postmeta table where meta_key = $field_name
  229. */
  230. public function save_post_filter($posted_data, $field_name) {
  231. if ( isset($posted_data[ FormElement::post_name_prefix . $field_name ]) ) {
  232. return $this->checked_value;
  233. }
  234. else {
  235. return $this->unchecked_value;
  236. }
  237. }
  238. }
  239. /*EOF*/