]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/AnonymousFave/Fave_tally.php
- Increment/decrement notice fave tally
[quix0rs-gnu-social.git] / plugins / AnonymousFave / Fave_tally.php
1 <?php
2 /**
3  * Data class for favorites talley
4  *
5  * PHP version 5
6  *
7  * @category Data
8  * @package  StatusNet
9  * @author   Zach Copley <zach@status.net>
10  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
11  * @link     http://status.net/
12  *
13  * StatusNet - the distributed open-source microblogging tool
14  * Copyright (C) 2010, StatusNet, Inc.
15  *
16  * This program is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU Affero General Public License as published by
18  * the Free Software Foundation, either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.     See the
24  * GNU Affero General Public License for more details.
25  *
26  * You should have received a copy of the GNU Affero General Public License
27  * along with this program. If not, see <http://www.gnu.org/licenses/>.
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR . '/classes/Memcached_DataObject.php';
35
36 /**
37  * Data class for favorites tally
38  *
39  * A class representing a total number of times a notice has been favorited
40  *
41  * @category Action
42  * @package  StatusNet
43  * @author   Zach Copley <zach@status.net>
44  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
45  * @link     http://status.net/
46  */
47
48 class Fave_tally extends Memcached_DataObject
49 {
50     ###START_AUTOCODE
51     /* the code below is auto generated do not remove the above tag */
52
53     public $__table = 'fave_tally';          // table name
54     public $notice_id;                       // int(4)  primary_key not_null
55     public $count;                           // int(4)  not_null
56     public $modified;                        // datetime   not_null default_0000-00-00%2000%3A00%3A00
57
58     /* Static get */
59     function staticGet($k, $v = NULL) { return Memcached_DataObject::staticGet('Fave_tally', $k, $v); }
60
61     /* the code above is auto generated do not remove the tag below */
62     ###END_AUTOCODE
63
64     /**
65      * return table definition for DB_DataObject
66      *
67      * @return array array of column definitions
68      */
69
70     function table()
71     {
72         return array(
73             'notice_id' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
74             'count'     => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
75             'modified'  => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL
76         );
77     }
78
79     /**
80      * return key definitions for DB_DataObject
81      *
82      * DB_DataObject needs to know about keys that the table has, since it
83      * won't appear in StatusNet's own keys list. In most cases, this will
84      * simply reference your keyTypes() function.
85      *
86      * @return array list of key field names
87      */
88
89     function keys()
90     {
91         return array_keys($this->keyTypes());
92     }
93
94     /**
95      * return key definitions for Memcached_DataObject
96      *
97      * Our caching system uses the same key definitions, but uses a different
98      * method to get them. This key information is used to store and clear
99      * cached data, so be sure to list any key that will be used for static
100      * lookups.
101      *
102      * @return array associative array of key definitions, field name to type:
103      *         'K' for primary key: for compound keys, add an entry for each component;
104      *         'U' for unique keys: compound keys are not well supported here.
105      */
106
107     function keyTypes()
108     {
109         return array('notice_id' => 'K');
110     }
111
112     /**
113      * Magic formula for non-autoincrementing integer primary keys
114      *
115      * If a table has a single integer column as its primary key, DB_DataObject
116      * assumes that the column is auto-incrementing and makes a sequence table
117      * to do this incrementation. Since we don't need this for our class, we
118      * overload this method and return the magic formula that DB_DataObject needs.
119      *
120      * @return array magic three-false array that stops auto-incrementing.
121      */
122
123
124     function sequenceKey()
125     {
126         return array(false, false, false);
127     }
128
129     /**
130      * Get a single object with multiple keys
131      *
132      * @param array $kv Map of key-value pairs
133      *
134      * @return User_flag_profile found object or null
135      */
136
137     function pkeyGet($kv)
138     {
139         return Memcached_DataObject::pkeyGet('Fave_tally', $kv);
140     }
141
142     /**
143      * Increment a notice's tally
144      *
145      * @param integer $noticeID ID of notice we're tallying
146      *
147      * @return Fave_tally $tally the tally data object
148      */
149
150     static function increment($noticeID)
151     {
152         common_debug("XXXXXXXXX Fave_tally::increment()");
153         $tally = Fave_tally::ensureTally($noticeID);
154
155         $orig = clone($tally);
156         $tally->count++;
157         $result = $tally->update($orig);
158
159         if (!$result) {
160             $msg = sprintf(
161                 _m("Couldn't update favorite tally for notice ID %d."),
162                 $notice_id
163             );
164             throw new ServerException($msg);
165         }
166
167         return $tally;
168     }
169
170     /**
171      * Decrement a notice's tally
172      *
173      * @param integer $noticeID ID of notice we're tallying
174      *
175      * @return Fave_tally $tally the tally data object
176      */
177
178     static function decrement($noticeID)
179     {
180         common_debug("XXXXXXXXX Fave_tally::decrement()");
181
182         $tally = Fave_tally::ensureTally($noticeID);
183
184         if ($tally->count > 0) {
185             $orig = clone($tally);
186             $tally->count--;
187             $result = $tally->update($orig);
188
189             if (!$result) {
190                 $msg = sprintf(
191                     _m("Couldn't update favorite tally for notice ID %d."),
192                     $notice_id
193                 );
194                 throw new ServerException($msg);
195             }
196         }
197
198         return $tally;
199     }
200
201     /**
202      * Ensure a tally exists for a given notice. If we can't find
203      * one create one.
204      *
205      * @param integer $noticeID
206      *
207      * @return Fave_tally the tally data object
208      */
209
210     static function ensureTally($noticeID)
211     {
212         $tally = Fave_tally::staticGet('notice_id', $notice_id);
213
214         if (!$tally) {
215             common_debug("Fave_tally::ensureTally - creating tally for notice " . $notice_id);
216             $tally = new Fave_tally();
217             $tally->notice_id = $notice_id;
218             $tally->count = 0;
219             $result = $tally->insert();
220             if (!$result) {
221                 $msg = sprintf(
222                     _m("Couldn't create favorite tally for notice ID %d."),
223                     $notice_id
224                 );
225                 throw new ServerException($msg);
226             }
227         }
228
229         return $tally;
230     }
231 }