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        $str =  $this->action->getLang($id);
110        if ($id == 'cost') {
111            $currency = $this->action->getConf('currency');
112            if ($currency != '') {
113                $str .= " ($currency)";
114            }
115        }
116        return $str;
117    }
118
119    /**
120     * @return mixed
121     */
122    public function current_user() {
123        return $this->action->get_model()->user_nick;
124    }
125
126    public function user_acl_level() {
127        return $this->action->get_model()->get_level();
128    }
129
130    public function date($date) {
131        return dformat(strtotime($date), '%Y-%m-%d');
132    }
133
134    public function datetime($datetime) {
135        return dformat(strtotime($datetime), '%Y-%m-%d %H:%M');
136    }
137
138    public function date_fuzzy_age($datetime) {
139        return datetime_h(strtotime($datetime));
140    }
141
142    public function date_diff_days($rDate, $lDate='now', $format='%R%a') {
143        $interval = date_diff(date_create($lDate), date_create($rDate));
144        return $interval->format("$format ".$this->getLang('days'));
145    }
146
147    public function date_diff_hours($rDate, $lDate='now') {
148        $interval = date_diff(date_create($lDate), date_create($rDate));
149        return $interval->format('%h:%I');
150    }
151
152    public function time_to_float($time) {
153        list($hour, $minute) = explode(':', $time);
154        $hour = (float) $hour;
155        $minute = (float) $minute;
156
157        return $hour + $minute/60;
158    }
159
160    public function float_to_time($float) {
161        $hours = floor($float);
162        $minutes = ($float - $hours) * 60;
163
164        return sprintf('%d:%02d', $hours, $minutes);
165    }
166}
167