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.
 
 
 
 
 

120 lines
3.6 KiB

  1. <?php
  2. /**
  3. * Class helper_plugin_bureaucracy_fieldnumber
  4. *
  5. * Creates a single line input field, where input is validated to be numeric
  6. */
  7. class helper_plugin_bureaucracy_fieldnumber extends helper_plugin_bureaucracy_fieldtextbox {
  8. private $autoinc = false;
  9. /**
  10. * Arguments:
  11. * - cmd
  12. * - label
  13. * - ++ (optional)
  14. * - 0000 (optional)
  15. * - ^ (optional)
  16. *
  17. * @param array $args The tokenized definition, only split at spaces
  18. */
  19. public function initialize($args) {
  20. $pp = array_search('++', $args, true);
  21. if ($pp !== false) {
  22. unset($args[$pp]);
  23. $this->autoinc = true;
  24. }
  25. parent::initialize($args);
  26. if ($this->autoinc) {
  27. global $ID;
  28. $key = $this->get_key();
  29. $c_val = p_get_metadata($ID, 'bureaucracy ' . $key);
  30. if (is_null($c_val)) {
  31. if (!isset($this->opt['value'])) {
  32. $this->opt['value'] = 0;
  33. }
  34. p_set_metadata($ID, array('bureaucracy' => array($key => $this->opt['value'])));
  35. } else {
  36. $this->opt['value'] = $c_val;
  37. }
  38. }
  39. $this->opt['value'] = $this->addLeadingzeros($this->opt['value']);
  40. }
  41. /**
  42. * Validate field value
  43. *
  44. * @throws Exception when not a number
  45. */
  46. protected function _validate() {
  47. $value = $this->getParam('value');
  48. if (!is_null($value) && !is_numeric($value)){
  49. throw new Exception(sprintf($this->getLang('e_numeric'),hsc($this->getParam('display'))));
  50. }
  51. parent::_validate();
  52. }
  53. /**
  54. * Handle a post to the field
  55. *
  56. * Accepts and validates a posted value.
  57. *
  58. * @param string $value The passed value or array or null if none given
  59. * @param array $fields (reference) form fields (POST handled upto $this field)
  60. * @param int $index index number of field in form
  61. * @param int $formid unique identifier of the form which contains this field
  62. * @return bool Whether the passed value is valid
  63. */
  64. public function handle_post($value, &$fields, $index, $formid) {
  65. $value = $this->addLeadingzeros($value);
  66. return parent::handle_post($value, $fields, $index, $formid);
  67. }
  68. /**
  69. * Returns the cleaned key for this field required for metadata
  70. *
  71. * @return string key
  72. */
  73. private function get_key() {
  74. return preg_replace('/\W/', '', $this->opt['label']) . '_autoinc';
  75. }
  76. /**
  77. * Executed after performing the action hooks
  78. *
  79. * Increases counter and purge cache
  80. */
  81. public function after_action() {
  82. if ($this->autoinc) {
  83. global $ID;
  84. p_set_metadata($ID, array('bureaucracy' => array($this->get_key() => $this->opt['value'] + 1)));
  85. // Force rerendering by removing the instructions cache file
  86. $cache_fn = getCacheName(wikiFN($ID).$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'],'.'.'i');
  87. if (file_exists($cache_fn)) {
  88. unlink($cache_fn);
  89. }
  90. }
  91. }
  92. /**
  93. * Add leading zeros, depending on the corresponding field option
  94. *
  95. * @param int|string $value number
  96. * @return string
  97. */
  98. protected function addLeadingzeros(&$value) {
  99. if (isset($this->opt['leadingzeros'])) {
  100. $length = strlen($value);
  101. for($i = $length; $i < $this->opt['leadingzeros']; $i++) {
  102. $value = '0' . $value;
  103. }
  104. return $value;
  105. }
  106. return $value;
  107. }
  108. }