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