3 * Data class for favorites talley
9 * @author Zach Copley <zach@status.net>
10 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
11 * @link http://status.net/
13 * StatusNet - the distributed open-source microblogging tool
14 * Copyright (C) 2010, StatusNet, Inc.
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.
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.
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/>.
30 if (!defined('STATUSNET')) {
34 require_once INSTALLDIR . '/classes/Memcached_DataObject.php';
37 * Data class for favorites tally
39 * A class representing a total number of times a notice has been favored
43 * @author Zach Copley <zach@status.net>
44 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
45 * @link http://status.net/
47 class Fave_tally extends Managed_DataObject
50 /* the code below is auto generated do not remove the above tag */
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
58 /* the code above is auto generated do not remove the tag below */
61 public static function schemaDef()
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'),
70 'primary key' => array('notice_id'),
71 'foreign keys' => array(
72 'fave_tally_notice_id_fkey' => array('notice', array('notice_id' => 'id')),
78 * Increment a notice's tally
80 * @param integer $noticeID ID of notice we're tallying
82 * @return Fave_tally $tally the tally data object
84 static function increment($noticeID)
86 $tally = Fave_tally::ensureTally($noticeID);
88 $orig = clone($tally);
90 $result = $tally->update($orig);
94 // TRANS: Server exception.
95 // TRANS: %d is the notice ID (number).
96 _m("Could not update favorite tally for notice ID %d."),
99 throw new ServerException($msg);
106 * Decrement a notice's tally
108 * @param integer $noticeID ID of notice we're tallying
110 * @return Fave_tally $tally the tally data object
112 static function decrement($noticeID)
114 $tally = Fave_tally::ensureTally($noticeID);
116 if ($tally->count > 0) {
117 $orig = clone($tally);
119 $result = $tally->update($orig);
123 // TRANS: Server exception.
124 // TRANS: %d is the notice ID (number).
125 _m("Could not update favorite tally for notice ID %d."),
128 throw new ServerException($msg);
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
139 * @param integer $noticeID
141 * @return Fave_tally the tally data object
143 static function ensureTally($noticeID)
145 $tally = Fave_tally::getKV('notice_id', $noticeID);
148 $tally = new Fave_tally();
149 $tally->notice_id = $noticeID;
150 $tally->count = Fave_tally::countExistingFaves($noticeID);
151 $result = $tally->insert();
154 // TRANS: Server exception.
155 // TRANS: %d is the notice ID (number).
156 _m("Could not create favorite tally for notice ID %d."),
159 throw new ServerException($msg);
167 * Count the number of faves a notice already has. Used to initalize
168 * a tally for a notice.
170 * @param integer $noticeID ID of the notice to count faves for
172 * @return integer $total total number of time the notice has been favored
174 static function countExistingFaves($noticeID)
177 $fave->notice_id = $noticeID;
178 $total = $fave->count();