1<?php
2/**
3 * additional setting classes specific to this plugin
4 *
5 * @author    Michael Wilmes <michael.wilmes@gmail.com>
6 */
7
8if (!class_exists('setting_autogroup')) {
9    /**
10     * Class setting_autogroup
11     */
12    class setting_autogroup extends setting_string {
13
14        /**
15         * Create an array from a string
16         *
17         * @param string $string
18         * @return array
19         */
20        protected function _from_string($string){
21            $array = explode("\n", $string);
22            $array = array_map('trim', $array);
23            $array = array_filter($array);
24            $array = array_unique($array);
25            return $array;
26        }
27
28        /**
29         * Create a string from an array
30         *
31         * @param array $array
32         * @return string
33         */
34        protected function _from_array($array){
35            return join("\n", (array) $array);
36        }
37
38         /**
39         * update setting with user provided value $input
40         * if value fails error check, save it
41         *
42         * @param string $input
43         * @return bool true if changed, false otherwise (incl. on error)
44         */
45        function update($input) {
46            if (is_null($input)) return false;
47            if ($this->is_protected()) return false;
48            dbglog(print_r(urlencode($input),true));
49            $lines = $this->_from_string($input);
50            dbglog(print_r($lines,true));
51            $everything = $this->_from_array($lines);
52
53            $value = is_null($this->_local) ? $this->_default : $this->_local;
54            if ($value == $everything) return false;
55
56            $plugin = plugin_load('action','autogroup');
57
58            foreach($lines as $line=>$item){
59                $parts = explode(',',$item,3);
60                if (count($parts) != 3) {
61                    $this->_error = true;
62                    $this->_input = $input;
63                    msg(sprintf($plugin->getLang('not_3_parts'),$item,$line+1), -1);
64                    return false;
65                }
66                if (!in_array($parts[1], array('mail','name','user'))) {
67                    $this->_error = true;
68                    $this->_input = $input;
69                    msg(sprintf($plugin->getLang('bad_attribute'),$item, $line+1), -1);
70                    return false;
71                }
72                if (preg_match ($parts[2], '') === false) {
73                    $this->_error = true;
74                    $this->_input = $input;
75                    msg(sprintf($plugin->getLang('bad_regex'),$item,$line+1), -1);
76                    return false;
77                }
78            }
79
80            $this->_local = $everything;
81            return true;
82        }
83
84        /**
85         * Build html for label and input of setting
86         *
87         * @param admin_plugin_config $plugin object of config plugin
88         * @param bool            $echo   true: show inputted value, when error occurred, otherwise the stored setting
89         * @return string[] with content array(string $label_html, string $input_html)
90         */
91        function html(admin_plugin_config $plugin, $echo=false) {
92            $disable = '';
93
94            if ($this->is_protected()) {
95                $value = $this->_protected;
96                $disable = 'disabled="disabled"';
97            } else {
98                if ($echo && $this->_error) {
99                    $value = $this->_input;
100                } else {
101                    $value = is_null($this->_local) ? $this->_default : $this->_local;
102                }
103            }
104
105            $key = htmlspecialchars($this->_key);
106            $value = htmlspecialchars($value);
107
108            $label = '<label for="config___'.$key.'">'.$this->prompt($plugin).'</label>';
109            $input = '<textarea rows="3" cols="40" id="config___'.$key.'" name="config['.$key.']" class="edit" '.$disable.'>'.$value.'</textarea>';
110            return array($label,$input);
111        }
112    }
113}