-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.php
220 lines (184 loc) · 5.39 KB
/
utils.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
/**
* @package com_meego_devprogram
* @author Ferenc Szekely, http://www.nemein.com
* @copyright The Midgard Project, http://www.midgard-project.org
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
*/
class com_meego_devprogram_utils
{
/**
* Returns the curretnly logged in user's object
*
* @return object midgard_user object of the current user
*/
public static function get_current_user()
{
$mvc = midgardmvc_core::get_instance();
return $mvc->authentication->get_user();
}
/**
* Requires a user to be logged in
* If not logged then redirect to the login page otherwise return user
* object
*
* @param string optional redirect page after succesful login
* @return object midgard_user object
*/
public static function require_login($redirect = '')
{
$mvc = midgardmvc_core::get_instance();
if (! $mvc->authentication->is_user())
{
$login_url = '/mgd:login';
if (strlen($redirect))
{
$login_url .= '?redirect=' . $redirect;
}
$mvc->head->relocate($login_url);
}
return $mvc->authentication->get_user();
}
/**
* Retrieves the user guid of the user specifie by login name
*
* @param string login name (ie. user name) stored in midgard_user table
* @return guid guid of the user
*/
public static function get_guid_of_user($login = '')
{
$guid = null;
$qb = new midgard_query_builder('midgard_user');
$qb->add_constraint('login', '=', $login);
$users = $qb->execute();
if (count($users))
{
$guid = $users[0]->person;
}
unset($qb);
return $guid;
}
/**
* Retrieves the user specified by guid
*
* @param guid user's person guid
* @return object midgard_user object
*/
public static function get_user_by_person_guid($guid = '')
{
$user = null;
if (mgd_is_guid($guid))
{
$qb = new midgard_query_builder('midgard_person');
$qb->add_constraint('guid', '=', $guid);
$users = $qb->execute();
if (count($users))
{
$user = $users[0];
}
unset($qb);
}
return $user;
}
/**
* Returns urls based on routes
*
* @param string route
* @param array arguments of the action
* @return string url
*/
public function get_url($route = '', $args)
{
$mvc = midgardmvc_core::get_instance();
return $mvc->dispatcher->generate_url($route, $args, $mvc->dispatcher->get_request());
}
/**
* Checks if the currently logged in user is a creator of the object
*
* @param object any object that is part of the schemas
* @return boolean
*/
public function is_current_user_creator($object = null)
{
$retval = false;
$mvc = midgardmvc_core::get_instance();
if ($mvc->authentication->is_user())
{
if (is_object($object))
{
$user = $mvc->authentication->get_user();
if ($object->metadata->creator == $user->person)
{
$retval = true;
}
}
}
unset($mvc);
return $retval;
}
/**
* Checks if the currently logged in user is a creator of the object
* or an administrator
*
* @param object any object that is part of the schemas
* @return boolean
*/
public function is_current_user_creator_or_admin($object = null)
{
$retval = false;
$mvc = midgardmvc_core::get_instance();
if ($mvc->authentication->is_user())
{
if ( $mvc->authentication->get_user()->is_admin()
|| self::is_current_user_creator($object))
{
$retval = true;
}
}
unset($mvc);
return $retval;
}
/**
* A simple way to generate a unique name for an object.
*
* It determines the class of the object, looks up similar objects in db
* and generates a new name based on the title.
*
* Alters the generated name as long as it does not become unique by
* adding a date to the end of it.
*
* @param object any object that is defined in the schema
* @return string a new name
*
*/
public function generate_unique_name($newobject)
{
$name = null;
if (is_object($newobject))
{
$names = array();
$class = get_class($newobject);
$qb = new midgard_query_builder($class);
$objects = $qb->execute();
// fill in an array with current names
foreach ($objects as $object)
{
$names[] = $object->name;
}
$unique = false;
$name = strtolower($newobject->title);
$name = str_replace(' ', '-', $name);
do {
if (array_search($name, $names) === false)
{
$unique = true;
}
else
{
$name .= '-' . date("Ymd");
}
} while (! $unique);
}
return $name;
}
}