]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/AnonymousFave/Fave_tally.php
New DB_DataObject for storing favorites 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)  primary_key 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      * @return array key definitions
83      */
84
85     function keys()
86     {
87         return array('notice_id' => 'K');
88     }
89
90     /**
91      * return key definitions for DB_DataObject
92      *
93      * @return array key definitions
94      */
95
96     function keyTypes()
97     {
98         return $this->keys();
99     }
100
101     /**
102      * Magic formula for non-autoincrementing integer primary keys
103      *
104      * @return array magic three-false array that stops auto-incrementing.
105      */
106
107     function sequenceKey()
108     {
109         return array(false, false, false);
110     }
111
112     /**
113      * Get a single object with multiple keys
114      *
115      * @param array $kv Map of key-value pairs
116      *
117      * @return User_flag_profile found object or null
118      */
119
120     function pkeyGet($kv)
121     {
122         return Memcached_DataObject::pkeyGet('Fave_tally', $kv);
123     }
124
125     /**
126      * Increment a notice's tally
127      *
128      * @param integer $notice_id   ID of notice we're tallying
129      *
130      * @return integer             the total times the notice has been faved
131      */
132
133     static function increment($notice_id)
134     {
135         $tally = Fave_tally::ensureTally($notice_id);
136         $count = $tally->count + 1;
137         $tally->count = $count;
138         $result = $tally->update();
139         $tally->free();
140
141         if ($result === false) {
142             $msg = sprintf(
143                 _m("Couldn't update favorite tally for notice ID %d.", $notice_id)
144             );
145             throw new ServerException($msg);
146         }
147
148         return $count;
149     }
150
151     /**
152      * Decrement a notice's tally
153      *
154      * @param integer $notice_id   ID of notice we're tallying
155      *
156      * @return integer             the total times the notice has been faved
157      */
158
159     static function decrement($notice_id)
160     {
161         $tally = Fave_tally::ensureTally($notice_id);
162
163         $count = 0;
164
165         if ($tally->count > 0) {
166             $count = $tally->count - 1;
167             $tally->count = $count;
168             $result = $tally->update();
169             $tally->free();
170
171             if ($result === false) {
172                 $msg = sprintf(
173                     _m("Couldn't update favorite tally for notice ID %d.", $notice_id)
174                 );
175                 throw new ServerException($msg);
176             }
177         }
178
179         return $count;
180     }
181
182     /**
183      * Ensure a tally exists for a given notice. If we can't find
184      * one create one.
185      *
186      * @param integer $notice_id
187      *
188      * @return Fave_tally the tally data object
189      */
190
191     static function ensureTally($notice_id)
192     {
193         $tally = new Fave_tally();
194         $result = $tally->get($notice_id);
195
196         if (empty($result)) {
197             $tally->notice_id = $notice_id;
198             $tally->count = 0;
199             if ($tally->insert() === false) {
200                 $msg = sprintf(
201                     _m("Couldn't create favorite tally for notice ID %d.", $notice_id)
202                 );
203                 throw new ServerException($msg);
204             }
205         }
206
207         return $tally;
208     }
209 }