-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinjector.php
495 lines (423 loc) · 18.2 KB
/
injector.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
<?php
class com_meego_packages_injector
{
var $mvc = null;
var $request = null;
var $part = 'applications';
var $connected = false;
/**
* Sets language, connectes signal handlers
*/
private function get_connected()
{
$this->mvc = midgardmvc_core::get_instance();
$this->mvc->i18n->set_translation_domain('com_meego_packages');
$default_language = $this->mvc->configuration->default_language;
if (! isset($default_language))
{
$default_language = 'en_US';
}
$this->mvc->i18n->set_language($default_language, false);
$request = $this->mvc->dispatcher->get_request();
//midgard_object_class::connect_default('midgard_attachment', 'action_loaded_hook', array('com_meego_packages_injector', 'register_download'), array($request));
midgard_object_class::connect_default('com_meego_ratings_rating', 'action_created', array('com_meego_packages_injector', 'register_rate_comment'), array($request));
midgard_object_class::connect_default('midgardmvc_ui_forms_form_instance', 'action_created', array('com_meego_packages_injector', 'register_qa_post'), array($request));
// Default title for Packages pages, override in controllers when possible
$this->mvc->head->set_title($this->mvc->i18n->get('title_apps'));
$_mc = midgard_connection::get_instance();
$res = $_mc->enable_replication(false);
$this->connected = true;
}
/**
* @todo: docs
*/
public function inject_process(midgardmvc_core_request $request)
{
$this->mvc = midgardmvc_core::get_instance();
// We inject the template to provide MeeGo styling
$request->add_component_to_chain($this->mvc->component->get('com_meego_packages'), true);
}
/**
* Some template hack
*/
public function inject_template(midgardmvc_core_request $request)
{
$this->mvc = midgardmvc_core::get_instance();
if (! $this->connected)
{
self::get_connected();
}
if (! $this->mvc->configuration->exists('default'))
{
// workaround to avoid an exception while templating
return true;
}
$this->request = $request;
$route = $this->request->get_route();
if ($route->id == "apps_index")
{
$route->template_aliases['topbar'] = 'cmp-welcome-text';
}
else
{
$route->template_aliases['topbar'] = 'cmp-menubar';
}
// login link with redirect specified
$request->set_data_item('redirect_link', $this->mvc->context->get_request(0)->get_path());
// placeholder for a link to list staging apps
$request->set_data_item('staging_link', false);
// set if user is admin
$request->set_data_item('admin', false);
// admins get a link to category management UI
$request->set_data_item('category_admin_url', false);
// flag to show newest and hottest blocks
$request->set_data_item('bottomblocks', false);
if ($this->mvc->authentication->is_user())
{
if ($this->mvc->authentication->get_user()->is_admin())
{
$request->set_data_item('admin', true);
$category_admin_url = $this->mvc->dispatcher->generate_url
(
'basecategories_admin_index', array(), $request
);
$request->set_data_item('category_admin_url', $category_admin_url);
}
}
$matched = $route->get_matched();
if ( ! $matched
&& $route->id == 'search')
{
// if we have a search we may be lucky and get a nicely set matched array
$matched = $this->request->get_query();
}
if ( is_array($matched)
&& array_key_exists('os', $matched)
&& array_key_exists('version', $matched)
&& array_key_exists('ux', $matched))
{
$os = $matched['os'];
$ux = $matched['ux'];
$redirect = false;
$decoded = '';
if (array_key_exists('basecategory', $matched))
{
$decoded = rawurldecode($matched['basecategory']);
$matched['basecategory'] = $decoded;
if (array_key_exists($decoded, $this->mvc->configuration->basecategory_css_map))
{
$matched['basecategory_css'] = $this->mvc->configuration->basecategory_css_map[$decoded];
}
else
{
$matched['basecategory_css'] = strtolower($decoded);
}
}
else
{
$matched['basecategory'] = false;
$matched['basecategory_css'] = '';
}
if (! array_key_exists($matched['os'], $this->mvc->configuration->os_map))
{
$redirect = true;
$os = $this->mvc->configuration->default['os'];
}
// if the matched UX is not configured then we shout out loud
if (! array_key_exists($matched['ux'], $this->mvc->configuration->os_ux[$os]))
{
$redirect = true;
$ux = $this->mvc->configuration->latest[$os]['ux'];
}
$matched['prettyversion'] = $matched['version'];
if (array_key_exists('versions', $this->mvc->configuration->os_map[$matched['os']]))
{
if (array_key_exists($matched['version'], $this->mvc->configuration->os_map[$matched['os']]['versions']))
{
$matched['prettyversion'] = $this->mvc->configuration->os_map[$matched['os']]['versions'][$matched['version']];
}
}
if ($redirect)
{
//throw new midgardmvc_exception_notfound("Please pick a valid UX, " . $matched['ux'] . " does not exist.", 404);
com_meego_packages_controllers_basecategory::redirect($os, $matched['version'], $ux);
}
// gather available UXes for the popups
// @todo: this piece of code is only needed for some of the routes
// so we should not run it when not needed
$uxes = array();
$versions = array();
$repositories = com_meego_packages_controllers_application::get_top_project_repos();
$latest = $this->mvc->configuration->latest;
foreach ($repositories as $repository)
{
if ( array_key_exists($repository->repoos, $latest)
&& $repository->repoosversion == $latest[$repository->repoos]['version'])
{
if (! strlen($repository->repoosux))
{
// No UX means a core or universal repo, so we populate all UXes
foreach ($this->mvc->configuration->os_ux[$repository->repoos] as $configured_ux => $configured_ux_title)
{
$uxes[$repository->repoos . $configured_ux] = com_meego_packages_controllers_application::populate_repo_ux($repository, $configured_ux);
}
}
else
{
$uxes[$repository->repoos . $repository->repoosux] = com_meego_packages_controllers_application::populate_repo_ux($repository);
}
}
if ($matched['os'] == $repository->repoos)
{
// all versions of the matched, current UX
if ( $repository->repoosux == ''
|| $repository->repoosux == 'universal'
|| $repository->repoosux == $matched['ux']
&& ! array_key_exists($repository->repoosversion, $versions))
{
$prettyversion = $repository->repoosversion;
if (array_key_exists('versions', $this->mvc->configuration->os_map[$repository->repoos]))
{
if (array_key_exists($repository->repoosversion, $this->mvc->configuration->os_map[$repository->repoos]['versions']))
{
$prettyversion = $this->mvc->configuration->os_map[$repository->repoos]['versions'][$repository->repoosversion];
}
}
$_repo = com_meego_packages_controllers_application::populate_repo_ux($repository, $matched['ux']);
$versions[$repository->repoosversion] = array
(
'version' => $repository->repoosversion,
'url' => $_repo['url'],
'prettyversion' => $prettyversion
);
}
}
// if we are not serving a staging_ route then
// check if the repo's project has a staging project configured
if ( $route->id == "basecategories_os_version_ux"
&& substr($route->id, 0, 8) != 'staging_'
&& $repository->repoos == $os
&& $this->mvc->configuration->top_projects[$repository->projectname]['staging'])
{
//echo "call count of apps\n"; ob_flush();
$cnt = com_meego_packages_controllers_application::count_number_of_apps($repository->repoos, $repository->repoosversion, 0, $matched['ux'], 'staging');
if ($cnt)
{
$workflows = com_meego_packages_controllers_workflow::get_open_workflows_for_osux($repository->repoos, $repository->repoosversion, $matched['ux']);
if (count($workflows))
{
// if there is at least 1 workflow then we set and show the link in the templates
$link = $this->mvc->dispatcher->generate_url
(
'staging_basecategories_os_version_ux',
array
(
'os' => $os,
'version' => (string) $repository->repoosversion,
'ux' => $matched['ux']
),
'com_meego_packages'
);
$request->set_data_item('staging_link', $link);
}
}
}
}
krsort($uxes);
ksort($versions);
$request->set_data_item('uxes', $uxes);
$request->set_data_item('versions', $versions);
}
// in case there is no matched stuff from the request we will use the defaults configured
if (! is_array($matched))
{
$matched = array();
}
if ( is_array($matched)
&& ( ! array_key_exists('os', $matched)
|| ! array_key_exists('version', $matched)
|| ! array_key_exists('prettyversion', $matched)
|| ! array_key_exists('ux', $matched)))
{
$default_os = $this->mvc->configuration->default['os'];
$matched = array_merge($matched, $this->mvc->configuration->latest[$default_os]);
$matched['os'] = $this->mvc->configuration->default['os'];
$matched['prettyversion'] = $matched['version'];
if (array_key_exists('versions', $this->mvc->configuration->os_map[$matched['os']]))
{
if (array_key_exists($matched['version'], $this->mvc->configuration->os_map[$matched['os']]['versions']))
{
$matched['prettyversion'] = $this->mvc->configuration->os_map[$matched['os']]['versions'][$matched['version']];
}
}
$this->part = 'packages';
}
// Add the CSS and JS files needed by Packages
$this->add_head_elements();
$matched['configured_ux'] = ucwords($this->mvc->configuration->os_ux[$matched['os']][$matched['ux']]);
if (array_key_exists('client', $this->mvc->configuration->os_ux[$matched['os']]))
{
$matched['appsclient'] = $this->mvc->configuration->os_ux[$matched['os']]['client'];
}
else
{
$matched['appsclient'] = false;
}
$request->set_data_item('matched', $matched);
if (count($matched))
{
// if we have matched then show the newest and hottest blocks
$request->set_data_item('bottomblocks', true);
}
$request->set_data_item('submit_app_url', $this->mvc->configuration->submit_app_url);
// don't show newest and hottest blocks if any of these were requested explicitly
if ( $route->id == "newest_apps"
|| $route->id == "hottest_apps")
{
$request->set_data_item('bottomblocks', false);
}
$request->set_data_item('staging_area', false);
if (substr($route->id, 0, 8) == 'staging_')
{
$request->set_data_item('staging_area', true);
$request->set_data_item('staging_back_link', false);
$request->set_data_item('community_qa_url', $this->mvc->configuration->community_qa_url);
$link = false;
if (substr($route->id, 0, 28) == 'staging_apps_by_basecategory')
{
$link = $this->mvc->dispatcher->generate_url
(
'staging_basecategories_os_version_ux',
array
(
'os' => $matched['os'],
'version' => (string) $matched['version'],
'ux' => $matched['ux']
),
'com_meego_packages'
);
}
if (substr($route->id, 0, 21) == 'staging_apps_by_name')
{
$link = '..';
}
if ($link)
{
$request->set_data_item('staging_back_link', $link);
}
}
}
/**
* Adds js and css files to head
*/
private function add_head_elements()
{
// can be used for selective loads
$route_id = $this->request->get_route()->id;
$this->mvc->head->add_jsfile(MIDGARDMVC_STATIC_URL . '/com_meego_packages/js/init_qa.js');
$this->mvc->head->add_jsfile(MIDGARDMVC_STATIC_URL . '/eu_urho_widgets/js/jquery.rating/jquery.rating.js');
if ($this->part == 'applications')
{
$this->mvc->head->add_jsfile(MIDGARDMVC_STATIC_URL . '/com_meego_packages/js/init_rating_widget_big.js');
}
else
{
$this->mvc->head->add_jsfile(MIDGARDMVC_STATIC_URL . '/com_meego_packages/js/init_rating_widget_small.js');
}
$this->mvc->head->add_link
(
array
(
'rel' => 'stylesheet',
'type' => 'text/css',
'href' => MIDGARDMVC_STATIC_URL . '/com_meego_packages/css/packages.css'
)
);
$this->mvc->head->add_link
(
array
(
'rel' => 'stylesheet',
'type' => 'text/css',
'href' => MIDGARDMVC_STATIC_URL . '/eu_urho_widgets/js/jquery.rating/jquery.rating.css'
)
);
$this->mvc->head->add_link
(
array
(
'rel' => 'stylesheet',
'type' => 'text/css',
'href' => MIDGARDMVC_STATIC_URL . '/com_meego_ratings/css/cmr-ratings.css'
)
);
if ( $route_id == "apps_by_name"
|| $route_id == "staging_apps_by_name")
{
// slide gallery stuff
$this->mvc->head->add_jsfile(MIDGARDMVC_STATIC_URL . '/com_meego_packages/js/jquery.slide-gallery.js');
$this->mvc->head->add_jsfile(MIDGARDMVC_STATIC_URL . '/com_meego_packages/js/gallery.js');
$this->mvc->head->add_link
(
array
(
'rel' => 'stylesheet',
'type' => 'text/css',
'href' => MIDGARDMVC_STATIC_URL . '/com_meego_packages/css/jquery.slide-gallery.css'
)
);
}
}
/**
* A signal handler for attachment download
*/
public function register_download($object = null, $request = null)
{
if ( $request
&& $object->mimetype == 'text/x-suse-ymp')
{
if ($request->argv[0] == 'mgd:attachment')
{
echo $object->guid . ':' . end($request->argv) . "\n";
ob_flush();
}
}
}
/**
* Signal handler for creating an activity object from new comment or rating
* object creation
*/
public function register_rate_comment($object = null, $request = null)
{
if ($object->rating > 0)
{
$verb = 'rate';
$summary = 'The user rated';
if ($object->comment > 0)
{
$verb .= ', comment';
$summary .= ' and commented';
}
}
else if ($object->comment > 0)
{
$verb = 'comment';
$summary = 'The user commented';
}
if ($summary)
{
$summary .= ' an application.';
}
$res = midgardmvc_account_controllers_activity::create_activity($object->metadata->creator, $verb, $object->to, $summary, 'Apps', $object->metadata->created);
}
/**
* Signal handler for creating an activity object from new QA post
*/
public function register_qa_post($object = null, $request = null)
{
$verb = 'review';
$summary = 'The user has reviewed an application.';
$res = midgardmvc_account_controllers_activity::create_activity($object->metadata->creator, $verb, $object->relatedobject, $summary, 'Apps', $object->metadata->created);
}
}
?>