-
Notifications
You must be signed in to change notification settings - Fork 2
/
newsfeeditem.php
81 lines (69 loc) · 1.86 KB
/
newsfeeditem.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
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* NewsfeedItem
*
* @abstract
* @package Anqh
* @author Antti Qvickström
* @copyright (c) 2010-2013 Antti Qvickström
* @license http://www.opensource.org/licenses/mit-license.php MIT license
*/
abstract class Anqh_NewsfeedItem implements NewsfeedItem_Interface {
/**
* @var array Aggregate types
*/
public static $aggregate = array();
/**
* Add a Newsfeed item.
*
* @static
* @param Model_User $user
* @param string $class e.g. 'user'
* @param string $type e.g. 'login'
* @param array $data Data to be user with item
* @return boolean
*/
protected static function add(Model_User $user, $class, $type, array $data = null) {
$item = new Model_NewsfeedItem;
// Update item if previous added less than time window specified time
$update = DB::update($item->get_table_name())
->set(array('stamp' => time()))
->where('user_id', '=', $user->id)
->and_where('class', '=', $class)
->and_where('type', '=', $type)
->and_where('stamp', '>', strtotime(Newsfeed::UPDATE_WINDOW));
$data
? $update->and_where('data', '=', @json_encode($data))
: $update->and_where('data', 'IS', null);
// Update any?
if ($update->execute()) {
return true;
}
// No new enough item found, insert
$item->set_fields(array(
'user_id' => $user->id,
'class' => $class,
'type' => $type,
'data' => $data,
'stamp' => time(),
));
$item->save();
return $item->loaded();
}
/**
* Get anchors to aggregated newsfeed item targets.
*
* @static
* @param Model_NewsfeedItem $item
* @return string
*/
public static function get_links($item) {
$links = array();
foreach ($item->data as $aggregated) {
if ($link = static::get_link($aggregated)) {
$links[] = $link;
}
}
return $links;
}
}