1<?php
2/**
3 * Base class for bureaucracy actions.
4 *
5 * All bureaucracy actions have to inherit from this class.
6 *
7 * ATM this class is pretty empty but, in the future it could be used to add
8 * helper functions which can be utilized by the different actions.
9 *
10 * @author Michael Klier <chi@chimeric.de>
11 */
12class helper_plugin_bureaucracy_action extends syntax_plugin_bureaucracy {
13
14    /**
15     * Return false to prevent DokuWiki reusing instances of the plugin
16     *
17     * @return bool
18     */
19    public function isSingleton() {
20        return false;
21    }
22
23    /**
24     * Handle the user input [required]
25     *
26     * This function needs to be implemented to accept the user data collected
27     * from the form. Data has to be grabbed from $_POST['bureaucracy'] using
28     * the indicies in the 'idx' members of the $data items.
29     *
30     * @param helper_plugin_bureaucracy_field[] $fields the list of fields in the form
31     * @param string                            $thanks the thank you message as defined in the form
32     *                                                  or default one. Might be modified by the action
33     *                                                  before returned
34     * @param array                             $argv   additional arguments passed to the action
35     * @return bool|string false on error, $thanks on success
36     */
37    public function run($fields, $thanks, $argv){
38        msg('ERROR: called action %s did not implement a run() function');
39        return false;
40    }
41
42    /**
43     * Adds some language related replacement patterns
44     */
45    function prepareLanguagePlaceholder() {
46        global $ID;
47        global $conf;
48
49        $this->patterns['__lang__'] = '/@LANG@/';
50        $this->values['__lang__'] = $conf['lang'];
51
52        $this->patterns['__trans__'] = '/@TRANS@/';
53        $this->values['__trans__'] = '';
54
55        /** @var helper_plugin_translation $trans */
56        $trans = plugin_load('helper', 'translation');
57        if (!$trans) return;
58
59        $this->values['__trans__'] = $trans->getLangPart($ID);
60        $this->values['__lang__'] = $trans->realLC('');
61    }
62
63    /**
64     * Adds replacement pattern for fieldlabels (e.g @@Label@@)
65     *
66     * @param helper_plugin_bureaucracy_field $field
67     */
68    function prepareFieldReplacement($field) {
69        $label = $field->getParam('label');
70
71        if(!is_null($label)) {
72            $this->patterns[$label] = $field->getReplacementPattern();
73            $this->values[$label] = $field->getReplacementValue();
74        }
75    }
76
77    /**
78     * Adds <noinclude></noinclude> to replacement patterns
79     */
80    function prepareNoincludeReplacement() {
81        $this->patterns['__noinclude__'] = '/<noinclude>(.*?)<\/noinclude>/is';
82        $this->values['__noinclude__'] = '';
83    }
84
85    /**
86     * Generate field replacements
87     *
88     * @param helper_plugin_bureaucracy_field[]  $fields  List of field objects
89     * @return array
90     */
91    function prepareFieldReplacements($fields) {
92        foreach ($fields as $field) {
93            //field replacements
94            $this->prepareFieldReplacement($field);
95        }
96    }
97
98    /**
99     * Returns ACL access level of the user or the (virtual) 'runas' user
100     *
101     * @param string $id pageid
102     * @return int
103     */
104    protected function aclcheck($id) {
105        $runas = $this->getConf('runas');
106
107        if($runas) {
108            $auth = auth_aclcheck($id, $runas, array());
109        } else {
110            $auth = auth_quickaclcheck($id);
111        }
112        return $auth;
113
114    }
115
116    /**
117     * Available methods
118     *
119     * @return array
120     */
121    public function getMethods() {
122        $result = array();
123        $result[] = array(
124            'name' => 'run',
125            'desc' => 'Handle the user input',
126            'params' => array(
127                'fields' => 'helper_plugin_bureaucracy_field[]',
128                'thanks' => 'string',
129                'argv'   => 'array'
130            ),
131            'return' => array('false on error, thanks message on success' => 'bool|string')
132        );
133        return $result;
134    }
135
136}
137