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