- Display tally in notice output
+
+ function onEndShowNoticeInfo($item)
+ {
+ common_debug("XXXXXXXXXXX onEndShowNoticeInfo");
+
+ $tally = Fave_tally::ensureTally($item->notice->id);
+
+ if (!empty($tally)) {
+ $item->out->elementStart(
+ 'div',
+ array(
+ 'id' => 'notice-' . $item->notice->id . '-tally',
+ 'class' => 'notice-tally'
+ )
+ );
+ $item->out->raw(sprintf(_m("favored %d times"), $tally->count));
+ $item->out->elementEnd('div');
+ }
+ return true;
+ }
+
function onStartShowNoticeOptions($item) {
if (!common_logged_in()) {
function onStartShowNoticeOptions($item) {
if (!common_logged_in()) {
+ function onEndFavorNotice($profile, $notice)
+ {
+ $tally = Fave_tally::increment($notice->id);
+ }
+
+ function onEndDisfavorNotice($profile, $notice)
+ {
+ $tally = Fave_tally::decrement($notice->id);
+ }
+
function createAnonProfile() {
// Get the anon user's IP, and turn it into a nickname
function createAnonProfile() {
// Get the anon user's IP, and turn it into a nickname
public $__table = 'fave_tally'; // table name
public $notice_id; // int(4) primary_key not_null
public $__table = 'fave_tally'; // table name
public $notice_id; // int(4) primary_key not_null
- public $count; // int(4) primary_key not_null
+ public $count; // int(4) not_null
public $modified; // datetime not_null default_0000-00-00%2000%3A00%3A00
/* Static get */
public $modified; // datetime not_null default_0000-00-00%2000%3A00%3A00
/* Static get */
/**
* return key definitions for DB_DataObject
*
/**
* return key definitions for DB_DataObject
*
- * @return array key definitions
+ * DB_DataObject needs to know about keys that the table has, since it
+ * won't appear in StatusNet's own keys list. In most cases, this will
+ * simply reference your keyTypes() function.
+ *
+ * @return array list of key field names
- return array('notice_id' => 'K');
+ return array_keys($this->keyTypes());
- * return key definitions for DB_DataObject
+ * return key definitions for Memcached_DataObject
- * @return array key definitions
+ * Our caching system uses the same key definitions, but uses a different
+ * method to get them. This key information is used to store and clear
+ * cached data, so be sure to list any key that will be used for static
+ * lookups.
+ *
+ * @return array associative array of key definitions, field name to type:
+ * 'K' for primary key: for compound keys, add an entry for each component;
+ * 'U' for unique keys: compound keys are not well supported here.
+ return array('notice_id' => 'K');
}
/**
* Magic formula for non-autoincrementing integer primary keys
*
}
/**
* Magic formula for non-autoincrementing integer primary keys
*
+ * If a table has a single integer column as its primary key, DB_DataObject
+ * assumes that the column is auto-incrementing and makes a sequence table
+ * to do this incrementation. Since we don't need this for our class, we
+ * overload this method and return the magic formula that DB_DataObject needs.
+ *
* @return array magic three-false array that stops auto-incrementing.
*/
* @return array magic three-false array that stops auto-incrementing.
*/
function sequenceKey()
{
return array(false, false, false);
function sequenceKey()
{
return array(false, false, false);
/**
* Increment a notice's tally
*
/**
* Increment a notice's tally
*
- * @param integer $notice_id ID of notice we're tallying
+ * @param integer $noticeID ID of notice we're tallying
- * @return integer the total times the notice has been faved
+ * @return Fave_tally $tally the tally data object
- static function increment($notice_id)
+ static function increment($noticeID)
- $tally = Fave_tally::ensureTally($notice_id);
- $count = $tally->count + 1;
- $tally->count = $count;
- $result = $tally->update();
- $tally->free();
+ common_debug("XXXXXXXXX Fave_tally::increment()");
+ $tally = Fave_tally::ensureTally($noticeID);
+
+ $orig = clone($tally);
+ $tally->count++;
+ $result = $tally->update($orig);
- if ($result === false) {
- _m("Couldn't update favorite tally for notice ID %d.", $notice_id)
+ _m("Couldn't update favorite tally for notice ID %d."),
+ $notice_id
);
throw new ServerException($msg);
}
);
throw new ServerException($msg);
}
}
/**
* Decrement a notice's tally
*
}
/**
* Decrement a notice's tally
*
- * @param integer $notice_id ID of notice we're tallying
+ * @param integer $noticeID ID of notice we're tallying
- * @return integer the total times the notice has been faved
+ * @return Fave_tally $tally the tally data object
- static function decrement($notice_id)
+ static function decrement($noticeID)
- $tally = Fave_tally::ensureTally($notice_id);
+ common_debug("XXXXXXXXX Fave_tally::decrement()");
+ $tally = Fave_tally::ensureTally($noticeID);
- $count = $tally->count - 1;
- $tally->count = $count;
- $result = $tally->update();
- $tally->free();
+ $orig = clone($tally);
+ $tally->count--;
+ $result = $tally->update($orig);
- if ($result === false) {
- _m("Couldn't update favorite tally for notice ID %d.", $notice_id)
+ _m("Couldn't update favorite tally for notice ID %d."),
+ $notice_id
);
throw new ServerException($msg);
}
}
);
throw new ServerException($msg);
}
}
}
/**
* Ensure a tally exists for a given notice. If we can't find
* one create one.
*
}
/**
* Ensure a tally exists for a given notice. If we can't find
* one create one.
*
- * @param integer $notice_id
+ * @param integer $noticeID
*
* @return Fave_tally the tally data object
*/
*
* @return Fave_tally the tally data object
*/
- static function ensureTally($notice_id)
+ static function ensureTally($noticeID)
- $tally = new Fave_tally();
- $result = $tally->get($notice_id);
+ $tally = Fave_tally::staticGet('notice_id', $notice_id);
+ if (!$tally) {
+ common_debug("Fave_tally::ensureTally - creating tally for notice " . $notice_id);
+ $tally = new Fave_tally();
$tally->notice_id = $notice_id;
$tally->count = 0;
$tally->notice_id = $notice_id;
$tally->count = 0;
- if ($tally->insert() === false) {
+ $result = $tally->insert();
+ if (!$result) {
- _m("Couldn't create favorite tally for notice ID %d.", $notice_id)
+ _m("Couldn't create favorite tally for notice ID %d."),
+ $notice_id
);
throw new ServerException($msg);
}
);
throw new ServerException($msg);
}