]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Fave.php
change NOTICE_GATEWAY to Notice::GATEWAY so autoloading works
[quix0rs-gnu-social.git] / classes / Fave.php
1 <?php
2 /**
3  * Table Definition for fave
4  */
5 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
6
7 class Fave extends Memcached_DataObject
8 {
9     ###START_AUTOCODE
10     /* the code below is auto generated do not remove the above tag */
11
12     public $__table = 'fave';                            // table name
13     public $notice_id;                       // int(4)  primary_key not_null
14     public $user_id;                         // int(4)  primary_key not_null
15     public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP
16
17     /* Static get */
18     function staticGet($k,$v=null)
19     { return Memcached_DataObject::staticGet('Fave',$k,$v); }
20
21     /* the code above is auto generated do not remove the tag below */
22     ###END_AUTOCODE
23
24     static function addNew($user, $notice) {
25         $fave = new Fave();
26         $fave->user_id = $user->id;
27         $fave->notice_id = $notice->id;
28         if (!$fave->insert()) {
29             common_log_db_error($fave, 'INSERT', __FILE__);
30             return false;
31         }
32         return $fave;
33     }
34
35     function &pkeyGet($kv)
36     {
37         return Memcached_DataObject::pkeyGet('Fave', $kv);
38     }
39
40     function stream($user_id, $offset=0, $limit=NOTICES_PER_PAGE, $own=false)
41     {
42         $ids = Notice::stream(array('Fave', '_streamDirect'),
43                               array($user_id, $own),
44                               ($own) ? 'fave:ids_by_user_own:'.$user_id :
45                               'fave:ids_by_user:'.$user_id,
46                               $offset, $limit);
47         return $ids;
48     }
49
50     function _streamDirect($user_id, $own, $offset, $limit, $since_id, $max_id, $since)
51     {
52         $fav = new Fave();
53         $qry = null;
54
55         if ($own) {
56             $qry  = 'SELECT fave.* FROM fave ';
57             $qry .= 'WHERE fave.user_id = ' . $user_id . ' ';
58         } else {
59              $qry =  'SELECT fave.* FROM fave ';
60              $qry .= 'INNER JOIN notice ON fave.notice_id = notice.id ';
61              $qry .= 'WHERE fave.user_id = ' . $user_id . ' ';
62              $qry .= 'AND notice.is_local != ' . Notice::GATEWAY . ' ';
63         }
64
65         if ($since_id != 0) {
66             $qry .= 'AND notice_id > ' . $since_id . ' ';
67         }
68
69         if ($max_id != 0) {
70             $qry .= 'AND notice_id <= ' . $max_id . ' ';
71         }
72
73         if (!is_null($since)) {
74             $qry .= 'AND modified > \'' . date('Y-m-d H:i:s', $since) . '\' ';
75         }
76
77         // NOTE: we sort by fave time, not by notice time!
78
79         $qry .= 'ORDER BY modified DESC ';
80
81         if (!is_null($offset)) {
82             $qry .= "LIMIT $offset, $limit";
83         }
84
85         $fav->query($qry);
86
87         $ids = array();
88
89         while ($fav->fetch()) {
90             $ids[] = $fav->notice_id;
91         }
92
93         $fav->free();
94         unset($fav);
95
96         return $ids;
97     }
98 }