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) {
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=false) {
74        if (!$login) {
75            $login = $this->current_user();
76        }
77        return $this->action->get_model()->userFactory->get_user_email($login);
78    }
79    /*end users info functions*/
80
81    public function set($id, $value) {
82        $this->variables[$id] = $value;
83    }
84
85    public function get($id, $default='') {
86        $arr = explode(' ', $id);
87        $var = $this->variables;
88        foreach($arr as $item) {
89            if (isset($var[$item])) {
90                $var = $var[$item];
91            } else {
92                return $default;
93            }
94        }
95        return $var;
96    }
97
98    public function set_values($values) {
99        foreach ($values as $name => $value) {
100            $this->values[$name] = $value;
101        }
102    }
103
104    public function value($name) {
105        return (isset($this->values[$name]) ? $this->values[$name] : '');
106    }
107
108    public function getLang($id) {
109        return $this->action->getLang($id);
110    }
111
112    /**
113     * @return mixed
114     */
115    public function current_user() {
116        return $this->action->get_model()->user_nick;
117    }
118
119    public function user_acl_level() {
120        return $this->action->get_model()->get_level();
121    }
122
123    public function date($date) {
124        return dformat(strtotime($date), '%Y-%m-%d');
125    }
126
127    public function datetime($datetime) {
128        return dformat(strtotime($datetime), '%Y-%m-%d %H:%M');
129    }
130
131    public function date_fuzzy_age($datetime) {
132        return datetime_h(strtotime($datetime));
133    }
134
135    public function date_diff_days($rDate, $lDate='now', $format='%R%a') {
136        $interval = date_diff(date_create($lDate), date_create($rDate));
137        return $interval->format("$format ".$this->getLang('days'));
138    }
139
140    public function date_diff_hours($rDate, $lDate='now') {
141        $interval = date_diff(date_create($lDate), date_create($rDate));
142        return $interval->format('%h:%I');
143    }
144
145    public function time_to_float($time) {
146        list($hour, $minute) = explode(':', $time);
147        $hour = (float) $hour;
148        $minute = (float) $minute;
149
150        return $hour + $minute/60;
151    }
152
153    public function float_to_time($float) {
154        $hours = floor($float);
155        $minutes = ($float - $hours) * 60;
156
157        return sprintf('%d:%02d', $hours, $minutes);
158    }
159}
160