-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.php
137 lines (122 loc) · 3.64 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
<?php
class com_meego_planet_utils
{
public static function get_item($identifier)
{
if (mgd_is_guid($identifier))
{
return self::get_item_by_guid($identifier);
}
return self::get_item_by_url($identifier);
}
private static function get_item_by_guid($guid)
{
if (!mgd_is_guid($guid))
{
throw new InvalidArgumentException("Argument must be a valid GUID");
}
try
{
$item = new com_meego_planet_item($guid);
}
catch (midgard_error_exception $e)
{
throw new midgardmvc_exception_notfound($e->getMessage());
}
return $item;
}
private static function get_item_by_url($url)
{
if (!filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED))
{
throw new InvalidArgumentException("Argument must be a valid URL");
}
$q = new midgard_query_select
(
new midgard_query_storage('com_meego_planet_item')
);
$q->set_constraint
(
new midgard_query_constraint
(
new midgard_query_property('url'),
'=',
new midgard_query_value($url)
)
);
$q->execute();
if ($q->resultscount == 0)
{
throw new midgardmvc_exception_notfound("Item {$url} not found");
}
$objects = $q->list_objects();
return $objects[0];
}
public static function get_items($cb_execution_start = null, $type = 'com_meego_planet_item_with_author')
{
$q = new midgard_query_select
(
new midgard_query_storage($type)
);
if ($cb_execution_start)
{
$q->connect('execution-start', $cb_execution_start);
}
$q->execute();
return $q->list_objects();
}
public static function get_items_for_feed(com_meego_planet_feed $feed)
{
return com_meego_planet_utils::get_items
(
function($q) use ($feed)
{
$q->set_constraint
(
new midgard_query_constraint
(
new midgard_query_property('feed'),
'=',
new midgard_query_value($feed->id)
)
);
},
'com_meego_planet_item'
);
}
public static function page_by_args(midgard_query_select $q, array $args)
{
$q->set_limit(20);
}
private static function get_user_by_author($author)
{
static $authors = array();
if (isset($authors[$author]))
{
return $authors[$author];
}
// Query matching user so we get the username
$person = new midgard_person($author);
$authors[$author] = $person->firstname;
return $authors[$author];
}
public static function prepare_item_for_display($item)
{
$username = self::get_user_by_author($item->author);
$item->avatar = '';
$item->avatarurl = '';
if ($username)
{
try
{
$item->avatar = midgardmvc_core::get_instance()->dispatcher->generate_url('meego_avatar', array('username' => $username), '/');
$item->avatarurl = "http://meego.com/users/{$username}";
}
catch (Exception $e)
{
midgardmvc_core::get_instance()->context->delete();
}
}
return $item;
}
}