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 favorited
43 * @author Zach Copley <zach@status.net>
44 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
45 * @link http://status.net/
48 class Fave_tally extends Memcached_DataObject
51 /* the code below is auto generated do not remove the above tag */
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
59 function staticGet($k, $v = NULL) { return Memcached_DataObject::staticGet('Fave_tally', $k, $v); }
61 /* the code above is auto generated do not remove the tag below */
65 * return table definition for DB_DataObject
67 * @return array array of column definitions
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
80 * return key definitions for DB_DataObject
82 * @return array key definitions
87 return array('notice_id' => 'K');
91 * return key definitions for DB_DataObject
93 * @return array key definitions
102 * Magic formula for non-autoincrementing integer primary keys
104 * @return array magic three-false array that stops auto-incrementing.
107 function sequenceKey()
109 return array(false, false, false);
113 * Get a single object with multiple keys
115 * @param array $kv Map of key-value pairs
117 * @return User_flag_profile found object or null
120 function pkeyGet($kv)
122 return Memcached_DataObject::pkeyGet('Fave_tally', $kv);
126 * Increment a notice's tally
128 * @param integer $notice_id ID of notice we're tallying
130 * @return integer the total times the notice has been faved
133 static function increment($notice_id)
135 $tally = Fave_tally::ensureTally($notice_id);
136 $count = $tally->count + 1;
137 $tally->count = $count;
138 $result = $tally->update();
141 if ($result === false) {
143 _m("Couldn't update favorite tally for notice ID %d.", $notice_id)
145 throw new ServerException($msg);
152 * Decrement a notice's tally
154 * @param integer $notice_id ID of notice we're tallying
156 * @return integer the total times the notice has been faved
159 static function decrement($notice_id)
161 $tally = Fave_tally::ensureTally($notice_id);
165 if ($tally->count > 0) {
166 $count = $tally->count - 1;
167 $tally->count = $count;
168 $result = $tally->update();
171 if ($result === false) {
173 _m("Couldn't update favorite tally for notice ID %d.", $notice_id)
175 throw new ServerException($msg);
183 * Ensure a tally exists for a given notice. If we can't find
186 * @param integer $notice_id
188 * @return Fave_tally the tally data object
191 static function ensureTally($notice_id)
193 $tally = new Fave_tally();
194 $result = $tally->get($notice_id);
196 if (empty($result)) {
197 $tally->notice_id = $notice_id;
199 if ($tally->insert() === false) {
201 _m("Couldn't create favorite tally for notice ID %d.", $notice_id)
203 throw new ServerException($msg);