]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/AnonymousFave/Fave_tally.php
eb23a7cb0ab150766e9f01086fb60e2bba25d6ec
[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 favored
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 class Fave_tally extends Managed_DataObject
48 {
49     ###START_AUTOCODE
50     /* the code below is auto generated do not remove the above tag */
51
52     public $__table = 'fave_tally';          // table name
53     public $notice_id;                       // int(4)  primary_key not_null
54     public $count;                           // int(4)  not_null
55     public $modified;                        // datetime   not_null default_0000-00-00%2000%3A00%3A00
56
57     /* the code above is auto generated do not remove the tag below */
58     ###END_AUTOCODE
59
60     /**
61      * return table definition for DB_DataObject
62      *
63      * @return array array of column definitions
64      */
65
66     function table()
67     {
68         return array(
69             'notice_id' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
70             'count'     => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
71             'modified'  => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL
72         );
73     }
74
75     /**
76      * return key definitions for DB_DataObject
77      *
78      * DB_DataObject needs to know about keys that the table has, since it
79      * won't appear in StatusNet's own keys list. In most cases, this will
80      * simply reference your keyTypes() function.
81      *
82      * @return array list of key field names
83      */
84     function keys()
85     {
86         return array_keys($this->keyTypes());
87     }
88
89     /**
90      * return key definitions for Memcached_DataObject
91      *
92      * Our caching system uses the same key definitions, but uses a different
93      * method to get them. This key information is used to store and clear
94      * cached data, so be sure to list any key that will be used for static
95      * lookups.
96      *
97      * @return array associative array of key definitions, field name to type:
98      *         'K' for primary key: for compound keys, add an entry for each component;
99      *         'U' for unique keys: compound keys are not well supported here.
100      */
101     function keyTypes()
102     {
103         return array('notice_id' => 'K');
104     }
105
106     /**
107      * Magic formula for non-autoincrementing integer primary keys
108      *
109      * If a table has a single integer column as its primary key, DB_DataObject
110      * assumes that the column is auto-incrementing and makes a sequence table
111      * to do this incrementation. Since we don't need this for our class, we
112      * overload this method and return the magic formula that DB_DataObject needs.
113      *
114      * @return array magic three-false array that stops auto-incrementing.
115      */
116     function sequenceKey()
117     {
118         return array(false, false, false);
119     }
120
121     /**
122      * Increment a notice's tally
123      *
124      * @param integer $noticeID ID of notice we're tallying
125      *
126      * @return Fave_tally $tally the tally data object
127      */
128     static function increment($noticeID)
129     {
130         $tally = Fave_tally::ensureTally($noticeID);
131
132         $orig = clone($tally);
133         $tally->count++;
134         $result = $tally->update($orig);
135
136         if (!$result) {
137             $msg = sprintf(
138                 // TRANS: Server exception.
139                 // TRANS: %d is the notice ID (number).
140                 _m("Could not update favorite tally for notice ID %d."),
141                 $noticeID
142             );
143             throw new ServerException($msg);
144         }
145
146         return $tally;
147     }
148
149     /**
150      * Decrement a notice's tally
151      *
152      * @param integer $noticeID ID of notice we're tallying
153      *
154      * @return Fave_tally $tally the tally data object
155      */
156     static function decrement($noticeID)
157     {
158         $tally = Fave_tally::ensureTally($noticeID);
159
160         if ($tally->count > 0) {
161             $orig = clone($tally);
162             $tally->count--;
163             $result = $tally->update($orig);
164
165             if (!$result) {
166                 $msg = sprintf(
167                     // TRANS: Server exception.
168                     // TRANS: %d is the notice ID (number).
169                     _m("Could not update favorite tally for notice ID %d."),
170                     $noticeID
171                 );
172                 throw new ServerException($msg);
173             }
174         }
175
176         return $tally;
177     }
178
179     /**
180      * Ensure a tally exists for a given notice. If we can't find
181      * one create one with the total number of existing faves
182      *
183      * @param integer $noticeID
184      *
185      * @return Fave_tally the tally data object
186      */
187     static function ensureTally($noticeID)
188     {
189         $tally = Fave_tally::getKV('notice_id', $noticeID);
190
191         if (!$tally) {
192             $tally = new Fave_tally();
193             $tally->notice_id = $noticeID;
194             $tally->count = Fave_tally::countExistingFaves($noticeID);
195             $result = $tally->insert();
196             if (!$result) {
197                 $msg = sprintf(
198                     // TRANS: Server exception.
199                     // TRANS: %d is the notice ID (number).
200                     _m("Could not create favorite tally for notice ID %d."),
201                     $noticeID
202                 );
203                 throw new ServerException($msg);
204             }
205         }
206
207         return $tally;
208     }
209
210     /**
211      * Count the number of faves a notice already has. Used to initalize
212      * a tally for a notice.
213      *
214      * @param integer $noticeID ID of the notice to count faves for
215      *
216      * @return integer $total total number of time the notice has been favored
217      */
218     static function countExistingFaves($noticeID)
219     {
220         $fave = new Fave();
221         $fave->notice_id = $noticeID;
222         $total = $fave->count();
223         return $total;
224     }
225 }