1<?php
2/**
3 * Class helper_plugin_bureaucracy_fieldsubmit
4 *
5 * Creates a submit button
6 */
7class helper_plugin_bureaucracy_fieldsubmit extends helper_plugin_bureaucracy_field {
8    protected $mandatory_args = 1;
9    static $captcha_displayed = array();
10    static $captcha_checked = array();
11
12    /**
13     * Arguments:
14     *  - cmd
15     *  - label (optional)
16     *  - ^ (optional)
17     *
18     * @param array $args The tokenized definition, only split at spaces
19     */
20    public function initialize($args) {
21        parent::initialize($args);
22        // make always optional to prevent being marked as required
23        $this->opt['optional'] = true;
24    }
25
26    /**
27     * Render the field as XHTML
28     *
29     * @params array     $params Additional HTML specific parameters
30     * @params Doku_Form $form   The target Doku_Form object
31     * @params int       $formid unique identifier of the form which contains this field
32     */
33    public function renderfield($params, Doku_Form $form, $formid) {
34        if(!isset(helper_plugin_bureaucracy_fieldsubmit::$captcha_displayed[$formid])) {
35            helper_plugin_bureaucracy_fieldsubmit::$captcha_displayed[$formid] = true;
36            /** @var helper_plugin_captcha $helper */
37            $helper = null;
38            if(@is_dir(DOKU_PLUGIN.'captcha')) $helper = plugin_load('helper','captcha');
39            if(!is_null($helper) && $helper->isEnabled()){
40                $form->addElement($helper->getHTML());
41            }
42        }
43        $attr = array();
44        if(isset($this->opt['id'])) {
45            $attr['id'] = $this->opt['id'];
46        }
47        $this->tpl = form_makeButton('submit','', '@@DISPLAY|' . $this->getLang('submit') . '@@', $attr);
48        parent::renderfield($params, $form, $formid);
49    }
50
51    /**
52     * Handle a post to the field
53     *
54     * Accepts and validates a posted captcha value.
55     *
56     * @param string $value The passed value
57     * @param helper_plugin_bureaucracy_field[] $fields (reference) form fields (POST handled upto $this field)
58     * @param int    $index  index number of field in form
59     * @param int    $formid unique identifier of the form which contains this field
60     * @return bool Whether the posted form has a valid captcha
61     */
62    public function handle_post($value, &$fields, $index, $formid) {
63        if ($this->hidden) {
64            return true;
65        }
66        if(!isset(helper_plugin_bureaucracy_fieldsubmit::$captcha_checked[$formid])) {
67            helper_plugin_bureaucracy_fieldsubmit::$captcha_checked[$formid] = true;
68            // check CAPTCHA
69            /** @var helper_plugin_captcha $helper */
70            $helper = null;
71            if(@is_dir(DOKU_PLUGIN.'captcha')) $helper = plugin_load('helper','captcha');
72            if(!is_null($helper) && $helper->isEnabled()){
73                return $helper->check();
74            }
75        }
76        return true;
77    }
78
79    /**
80     * Get an arbitrary parameter
81     *
82     * @param string $name
83     * @return mixed|null
84     */
85    public function getParam($name) {
86        return ($name === 'value') ? null : parent::getParam($name);
87    }
88
89}
90