/
utils.php
252 lines (210 loc) · 7.16 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
/**
* @package com_meego_packages
* @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_packages_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 specified by it person property
*
* @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))
{
$storage = new midgard_query_storage('midgard_user');
$q = new midgard_query_select($storage);
$q->set_constraint(new midgard_query_constraint(
new midgard_query_property('person'),
'=',
new midgard_query_value($guid)
));
$q->execute();
$q->toggle_readonly(false);
$users = $q->list_objects();
if (count($users))
{
$user = $users[0];
}
unset($storage);
unset($q);
unset($users);
}
return $user;
}
/**
* Retrieves the user specified by guid
*
* @param guid user's guid
* @return object midgard_user object
*/
public static function get_user_by_guid($guid = '')
{
$user = null;
if (mgd_is_guid($guid))
{
$storage = new midgard_query_storage('midgard_user');
$q = new midgard_query_select($storage);
$q->set_constraint(new midgard_query_constraint(
new midgard_query_property('guid'),
'=',
new midgard_query_value($guid)
));
$q->execute();
$q->toggle_readonly(false);
$users = $q->list_objects();
if (count($users))
{
$user = $users[0];
}
unset($storage);
unset($q);
unset($users);
}
return $user;
}
/**
* Returns the link of the avatar image that can be used in e.g. img src
*
* @param string user name
* @return string url
*/
public function get_avatar($username = null)
{
$retval = midgardmvc_core::get_instance()->configuration->default_avatar;
if ($username)
{
midgardmvc_core::get_instance()->authorization->enter_sudo('midgardmvc_core');
// determine the person guid
$storage = new midgard_query_storage('midgard_user');
$q = new midgard_query_select($storage);
$q->set_constraint(new midgard_query_constraint(
new midgard_query_property('login'),
'=',
new midgard_query_value($username)
));
$q->execute();
$q->toggle_readonly(false);
$users = $q->list_objects();
midgardmvc_core::get_instance()->authorization->leave_sudo();
if (count($users))
{
$user = $users[0];
}
$account = midgardmvc_account_injector::get_account($user->person);
if ($account->guid)
{
$retval = $account->avatarurl;
}
}
return $retval;
}
/**
* Returns the translated i18n string
*
* @param string id of the string
* @return string
*/
public function get_i18n_string(array $args)
{
if (array_key_exists('id', $args))
{
$this->data['i18n'] = midgardmvc_core::get_instance()->i18n->get($args['id']);
}
}
/**
* Returns the stripped down form on a workflow
*
* @param object com_meego_package_details object
*/
public function get_stripped_forms_for_package($package = null)
{
$retval = array();
$object = new com_meego_package($package->packageguid);
$workflows = midgardmvc_helper_workflow_utils::get_workflows_for_object($object);
if (is_array($workflows))
{
$this->mvc->component->load_library('Workflow');
foreach ($workflows as $name => $workflow_data)
{
$args = array(
'package' => $package->packagename,
'version' => $package->packageversion,
'project' => $package->repoprojectname,
'repository' => $package->reponame,
'arch' => $package->repoarch,
'workflow' => $name
);
$workflow_definition = new $workflow_data['provider'];
$values = $workflow_definition->start($object);
$workflow = $workflow_definition->get();
if (isset($values['review_form']))
{
$form = new midgardmvc_ui_forms_form($values['review_form']);
$fields = midgardmvc_ui_forms_generator::list_fields($form);
foreach ($fields as $field)
{
$retval[$name][$field->title]['widget'] = $field->widget;
$retval[$name][$field->title]['options'] = $field->options;
}
}
else if (isset($values['execution']))
{
$args['execution'] = $values['execution'];
$execution = new midgardmvc_helper_workflow_execution_interactive($workflow, $args['execution']);
$variables = $execution->getVariables();
if (isset($variables['review_form']))
{
$form = new midgardmvc_ui_forms_form($variables['review_form']);
$fields = midgardmvc_ui_forms_generator::list_fields($form);
foreach ($fields as $field)
{
$retval[$name][$field->title]['widget'] = $field->widget;
$retval[$name][$field->title]['options'] = $field->options;
}
}
}
}
}
return $retval;
}
}
?>