You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

99 lines
2.9 KiB

  1. <?php
  2. /**
  3. * Class helper_plugin_bureaucracy_fieldyesno
  4. *
  5. * Creates a checkbox
  6. */
  7. class helper_plugin_bureaucracy_fieldyesno extends helper_plugin_bureaucracy_field {
  8. /**
  9. * Arguments:
  10. * - cmd
  11. * - label
  12. * - =yesvalue
  13. * - !falsevalue
  14. * - ^ (optional)
  15. *
  16. * @param array $args The tokenized definition, only split at spaces
  17. */
  18. public function initialize($args) {
  19. $this->init($args);
  20. $newargs = array();
  21. foreach ($args as $arg) {
  22. switch ($arg[0]) {
  23. case '=':
  24. if($arg == '==1') {
  25. $this->opt['value'] = '1';
  26. }elseif($arg == '==0') {
  27. $this->opt['value'] = '0';
  28. }else{
  29. $this->opt['true_value'] = substr($arg, 1);
  30. }
  31. break;
  32. case '!':
  33. $this->opt['false_value'] = substr($arg, 1);
  34. break;
  35. default:
  36. $newargs[] = $arg;
  37. }
  38. }
  39. $this->standardArgs($newargs);
  40. $this->opt['optional'] = true;
  41. }
  42. /**
  43. * Get an arbitrary parameter
  44. *
  45. * @param string $key
  46. * @return mixed|null
  47. */
  48. public function getParam($key) {
  49. if ($key === 'value') {
  50. if ($this->opt['value'] === '1') {
  51. return isset($this->opt['true_value']) ?
  52. $this->opt['true_value'] :
  53. null;
  54. } elseif ($this->opt['value'] === '0') {
  55. return isset($this->opt['false_value']) ?
  56. $this->opt['false_value'] :
  57. null;
  58. }
  59. }
  60. return parent::getParam($key);
  61. }
  62. /**
  63. * Whether the field is true (used for depending fieldsets)
  64. *
  65. * @return bool whether field is set
  66. */
  67. public function isSet_() {
  68. return $this->opt['value'] === '1';
  69. }
  70. /**
  71. * Render the field as XHTML
  72. *
  73. * @params array $params Additional HTML specific parameters
  74. * @params Doku_Form $form The target Doku_Form object
  75. * @params int $formid unique identifier of the form which contains this field
  76. */
  77. public function renderfield($params, Doku_Form $form, $formid) {
  78. $id = 'bureaucracy__'.md5(rand());
  79. if(isset($this->opt['id'])) {
  80. $id = $this->opt['id'];
  81. }
  82. $params = array_merge(
  83. array('value' => false),
  84. $this->opt,
  85. $params
  86. );
  87. $check = $params['value'] ? 'checked="checked"' : '';
  88. $this->tpl = '<label class="@@CLASS@@" for="'.$id.'"><span>@@DISPLAY@@</span>'.
  89. '<input type="hidden" name="@@NAME@@" value="0" />' .
  90. '<input type="checkbox" name="@@NAME@@" value="1" id="'.$id.'" ' . $check . ' />' .
  91. '</label>';
  92. parent::renderfield($params, $form, $formid);
  93. }
  94. }