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