Fave_tally.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. if (!defined('STATUSNET')) {
  30. exit(1);
  31. }
  32. require_once INSTALLDIR . '/classes/Memcached_DataObject.php';
  33. /**
  34. * Data class for favorites tally
  35. *
  36. * A class representing a total number of times a notice has been favored
  37. *
  38. * @category Action
  39. * @package StatusNet
  40. * @author Zach Copley <zach@status.net>
  41. * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
  42. * @link http://status.net/
  43. */
  44. class Fave_tally extends Managed_DataObject
  45. {
  46. ###START_AUTOCODE
  47. /* the code below is auto generated do not remove the above tag */
  48. public $__table = 'fave_tally'; // table name
  49. public $notice_id; // int(4) primary_key not_null
  50. public $count; // int(4) not_null
  51. public $created; // datetime() not_null
  52. public $modified; // datetime not_null default_0000-00-00%2000%3A00%3A00
  53. /* the code above is auto generated do not remove the tag below */
  54. ###END_AUTOCODE
  55. public static function schemaDef()
  56. {
  57. return array(
  58. 'fields' => array(
  59. 'notice_id' => array('type' => 'int', 'not null' => true, 'description' => 'notice id'),
  60. 'count' => array('type' => 'int', 'not null' => true, 'description' => 'the fave tally count'),
  61. 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
  62. 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
  63. ),
  64. 'primary key' => array('notice_id'),
  65. 'foreign keys' => array(
  66. 'fave_tally_notice_id_fkey' => array('notice', array('notice_id' => 'id')),
  67. ),
  68. );
  69. }
  70. /**
  71. * Increment a notice's tally
  72. *
  73. * @param integer $noticeID ID of notice we're tallying
  74. *
  75. * @return Fave_tally $tally the tally data object
  76. */
  77. static function increment($noticeID)
  78. {
  79. $tally = Fave_tally::ensureTally($noticeID);
  80. $orig = clone($tally);
  81. $tally->count++;
  82. $result = $tally->update($orig);
  83. if (!$result) {
  84. $msg = sprintf(
  85. // TRANS: Server exception.
  86. // TRANS: %d is the notice ID (number).
  87. _m("Could not update favorite tally for notice ID %d."),
  88. $noticeID
  89. );
  90. throw new ServerException($msg);
  91. }
  92. return $tally;
  93. }
  94. /**
  95. * Decrement a notice's tally
  96. *
  97. * @param integer $noticeID ID of notice we're tallying
  98. *
  99. * @return Fave_tally $tally the tally data object
  100. */
  101. static function decrement($noticeID)
  102. {
  103. $tally = Fave_tally::ensureTally($noticeID);
  104. if ($tally->count > 0) {
  105. $orig = clone($tally);
  106. $tally->count--;
  107. $result = $tally->update($orig);
  108. if (!$result) {
  109. $msg = sprintf(
  110. // TRANS: Server exception.
  111. // TRANS: %d is the notice ID (number).
  112. _m("Could not update favorite tally for notice ID %d."),
  113. $noticeID
  114. );
  115. throw new ServerException($msg);
  116. }
  117. }
  118. return $tally;
  119. }
  120. /**
  121. * Ensure a tally exists for a given notice. If we can't find
  122. * one create one with the total number of existing faves
  123. *
  124. * @param integer $noticeID
  125. *
  126. * @return Fave_tally the tally data object
  127. */
  128. static function ensureTally($noticeID)
  129. {
  130. $tally = Fave_tally::getKV('notice_id', $noticeID);
  131. if (!$tally) {
  132. $tally = new Fave_tally();
  133. $tally->notice_id = $noticeID;
  134. $tally->count = Fave_tally::countExistingFaves($noticeID);
  135. $result = $tally->insert();
  136. if (!$result) {
  137. $msg = sprintf(
  138. // TRANS: Server exception.
  139. // TRANS: %d is the notice ID (number).
  140. _m("Could not create favorite tally for notice ID %d."),
  141. $noticeID
  142. );
  143. throw new ServerException($msg);
  144. }
  145. }
  146. return $tally;
  147. }
  148. /**
  149. * Count the number of faves a notice already has. Used to initalize
  150. * a tally for a notice.
  151. *
  152. * @param integer $noticeID ID of the notice to count faves for
  153. *
  154. * @return integer $total total number of time the notice has been favored
  155. */
  156. static function countExistingFaves($noticeID)
  157. {
  158. $fave = new Fave();
  159. $fave->notice_id = $noticeID;
  160. $total = $fave->count();
  161. return $total;
  162. }
  163. }