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