]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/AnonymousFave/Fave_tally.php
More info for a proper, fancy-url lighttpd setup
[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 $created;                         // datetime()   not_null
56     public $modified;                        // datetime   not_null default_0000-00-00%2000%3A00%3A00
57
58     /* the code above is auto generated do not remove the tag below */
59     ###END_AUTOCODE
60
61     public static function schemaDef()
62     {
63         return array(
64             'fields' => array(
65                 'notice_id' => array('type' => 'int', 'not null' => true, 'description' => 'notice id'),
66                 'count' => array('type' => 'int', 'not null' => true, 'description' => 'the fave tally count'),
67                 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
68                 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
69             ),
70             'primary key' => array('notice_id'),
71             'foreign keys' => array(
72                 'fave_tally_notice_id_fkey' => array('notice', array('notice_id' => 'id')),
73             ),
74         );
75     }
76
77     /**
78      * Increment a notice's tally
79      *
80      * @param integer $noticeID ID of notice we're tallying
81      *
82      * @return Fave_tally $tally the tally data object
83      */
84     static function increment($noticeID)
85     {
86         $tally = Fave_tally::ensureTally($noticeID);
87
88         $orig = clone($tally);
89         $tally->count++;
90         $result = $tally->update($orig);
91
92         if (!$result) {
93             $msg = sprintf(
94                 // TRANS: Server exception.
95                 // TRANS: %d is the notice ID (number).
96                 _m("Could not update favorite tally for notice ID %d."),
97                 $noticeID
98             );
99             throw new ServerException($msg);
100         }
101
102         return $tally;
103     }
104
105     /**
106      * Decrement a notice's tally
107      *
108      * @param integer $noticeID ID of notice we're tallying
109      *
110      * @return Fave_tally $tally the tally data object
111      */
112     static function decrement($noticeID)
113     {
114         $tally = Fave_tally::ensureTally($noticeID);
115
116         if ($tally->count > 0) {
117             $orig = clone($tally);
118             $tally->count--;
119             $result = $tally->update($orig);
120
121             if (!$result) {
122                 $msg = sprintf(
123                     // TRANS: Server exception.
124                     // TRANS: %d is the notice ID (number).
125                     _m("Could not update favorite tally for notice ID %d."),
126                     $noticeID
127                 );
128                 throw new ServerException($msg);
129             }
130         }
131
132         return $tally;
133     }
134
135     /**
136      * Ensure a tally exists for a given notice. If we can't find
137      * one create one with the total number of existing faves
138      *
139      * @param integer $noticeID
140      *
141      * @return Fave_tally the tally data object
142      */
143     static function ensureTally($noticeID)
144     {
145         $tally = Fave_tally::getKV('notice_id', $noticeID);
146
147         if (!$tally) {
148             $tally = new Fave_tally();
149             $tally->notice_id = $noticeID;
150             $tally->count = Fave_tally::countExistingFaves($noticeID);
151             $result = $tally->insert();
152             if (!$result) {
153                 $msg = sprintf(
154                     // TRANS: Server exception.
155                     // TRANS: %d is the notice ID (number).
156                     _m("Could not create favorite tally for notice ID %d."),
157                     $noticeID
158                 );
159                 throw new ServerException($msg);
160             }
161         }
162
163         return $tally;
164     }
165
166     /**
167      * Count the number of faves a notice already has. Used to initalize
168      * a tally for a notice.
169      *
170      * @param integer $noticeID ID of the notice to count faves for
171      *
172      * @return integer $total total number of time the notice has been favored
173      */
174     static function countExistingFaves($noticeID)
175     {
176         $fave = new Fave();
177         $fave->notice_id = $noticeID;
178         $total = $fave->count();
179         return $total;
180     }
181 }