xref: /plugin/bez/meta/Tpl.php (revision 16c7b168a60daa2c9b9ddcfa51e05d123a2a17dc)
1<?php
2
3namespace dokuwiki\plugin\bez\meta;
4
5class Tpl {
6
7    /** @var \action_plugin_bez */
8    private $action;
9
10    private $conf;
11
12    private $variables = array();
13
14    //form values from $_POST or from database
15    private $values = array();
16
17    public function __construct(\action_plugin_bez_default $action, $conf) {
18
19        $this->action = $action;
20        $this->conf = $conf;
21
22        //constas
23        $this->set('client', $this->model->user_nick);
24
25        $info = $action->getInfo();
26        $this->set('version', $info['date']);
27
28        //common one
29        $this->set('users', $this->action->get_model()->userFactory->get_all());
30        $this->set('groups', $this->action->get_model()->userFactory->get_groups());
31    }
32
33    public function action($default=null) {
34        $action = $this->action->get_action();
35        if ($action == '' && !is_null($default)) {
36            return $default;
37        }
38        return $action;
39    }
40
41    public function param($id, $default='') {
42        return $this->action->get_param($id, $default);
43    }
44
45    public function url() {
46        return call_user_func_array(array($this->action, 'url'), func_get_args());
47    }
48
49    public function mailto($to, $subject, $body) {
50        return 'mailto:'.$to.'?subject='.rawurlencode($subject).'&body='.rawurlencode($body);
51    }
52
53    public function static_acl($table, $field) {
54        return $this->action->get_model()->acl->check_static_field($table, $field);
55    }
56
57    /*users info function for shorten the code*/
58    public function user_name($login=NULL) {
59        $name = $this->action->get_model()->userFactory->get_user_full_name($login);
60        if ($name === '') {
61            return $login;
62        }
63        return $name;
64    }
65
66    public function user_email($login=NULL) {
67        return $this->action->get_model()->userFactory->get_user_email($login);
68    }
69    /*end users info functions*/
70
71    public function prevent_rendering() {
72
73    }
74
75    public function set($id, $value) {
76        $this->variables[$id] = $value;
77    }
78
79    public function get($id, $default='') {
80        $arr = explode(' ', $id);
81        $var = $this->variables;
82        foreach($arr as $item) {
83            if (isset($var[$item])) {
84                $var = $var[$item];
85            } else {
86                return $default;
87            }
88        }
89        return $var;
90    }
91
92    public function set_values($values) {
93        foreach ($values as $name => $value) {
94            $this->values[$name] = $value;
95        }
96    }
97
98    public function value($name) {
99        return (isset($this->values[$name]) ? $this->values[$name] : '');
100    }
101
102    public function getLang($id) {
103        return $this->action->getLang($id);
104    }
105
106    /**
107     * @return mixed
108     */
109    public function current_user() {
110        return $this->action->get_model()->user_nick;
111    }
112
113    public function user_acl_level() {
114        return $this->action->get_model()->acl->get_level();
115    }
116
117    public function date($date) {
118        return dformat(strtotime($date), '%Y-%m-%d');
119    }
120
121    public function datetime($datetime) {
122        return dformat(strtotime($datetime), '%Y-%m-%d %H:%M');
123    }
124
125    public function date_fuzzy_age($datetime) {
126        return datetime_h(strtotime($datetime));
127    }
128
129    public function date_diff_days($rDate, $lDate='now', $format='%R%a') {
130        $interval = date_diff(date_create($lDate), date_create($rDate));
131        return $interval->format("$format ".$this->getLang('days'));
132    }
133
134    public function date_diff_hours($rDate, $lDate='now') {
135        $interval = date_diff(date_create($lDate), date_create($rDate));
136        return $interval->format('%h:%I');
137    }
138}
139