Fave_tally.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Data class for favorites talley
  18. *
  19. * @category Data
  20. * @package GNUsocial
  21. * @author Zach Copley <zach@status.net>
  22. * @copyright 2010 StatusNet, Inc.
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. defined('GNUSOCIAL') || die();
  26. /**
  27. * Data class for favorites tally
  28. *
  29. * A class representing a total number of times a notice has been favored
  30. *
  31. * @category Action
  32. * @package GNUsocial
  33. * @author Zach Copley <zach@status.net>
  34. * @copyright 2010 StatusNet, Inc.
  35. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  36. */
  37. class Fave_tally extends Managed_DataObject
  38. {
  39. ###START_AUTOCODE
  40. /* the code below is auto generated do not remove the above tag */
  41. public $__table = 'fave_tally'; // table name
  42. public $notice_id; // int(4) primary_key not_null
  43. public $count; // int(4) not_null
  44. public $created; // datetime()
  45. public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
  46. /* the code above is auto generated do not remove the tag below */
  47. ###END_AUTOCODE
  48. public static function schemaDef()
  49. {
  50. return array(
  51. 'fields' => array(
  52. 'notice_id' => array('type' => 'int', 'not null' => true, 'description' => 'notice id'),
  53. 'count' => array('type' => 'int', 'not null' => true, 'description' => 'the fave tally count'),
  54. 'created' => array('type' => 'datetime', 'description' => 'date this record was created'),
  55. 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
  56. ),
  57. 'primary key' => array('notice_id'),
  58. 'foreign keys' => array(
  59. 'fave_tally_notice_id_fkey' => array('notice', array('notice_id' => 'id')),
  60. ),
  61. );
  62. }
  63. /**
  64. * Increment a notice's tally
  65. *
  66. * @param integer $noticeID ID of notice we're tallying
  67. *
  68. * @return Fave_tally $tally the tally data object
  69. */
  70. public static function increment($noticeID)
  71. {
  72. $tally = Fave_tally::ensureTally($noticeID);
  73. $orig = clone($tally);
  74. $tally->count++;
  75. $result = $tally->update($orig);
  76. if (!$result) {
  77. $msg = sprintf(
  78. // TRANS: Server exception.
  79. // TRANS: %d is the notice ID (number).
  80. _m("Could not update favorite tally for notice ID %d."),
  81. $noticeID
  82. );
  83. throw new ServerException($msg);
  84. }
  85. return $tally;
  86. }
  87. /**
  88. * Decrement a notice's tally
  89. *
  90. * @param integer $noticeID ID of notice we're tallying
  91. *
  92. * @return Fave_tally $tally the tally data object
  93. */
  94. public static function decrement($noticeID)
  95. {
  96. $tally = Fave_tally::ensureTally($noticeID);
  97. if ($tally->count > 0) {
  98. $orig = clone($tally);
  99. $tally->count--;
  100. $result = $tally->update($orig);
  101. if (!$result) {
  102. $msg = sprintf(
  103. // TRANS: Server exception.
  104. // TRANS: %d is the notice ID (number).
  105. _m("Could not update favorite tally for notice ID %d."),
  106. $noticeID
  107. );
  108. throw new ServerException($msg);
  109. }
  110. }
  111. return $tally;
  112. }
  113. /**
  114. * Ensure a tally exists for a given notice. If we can't find
  115. * one create one with the total number of existing faves
  116. *
  117. * @param integer $noticeID
  118. *
  119. * @return Fave_tally the tally data object
  120. */
  121. public static function ensureTally($noticeID)
  122. {
  123. $tally = Fave_tally::getKV('notice_id', $noticeID);
  124. if (!$tally) {
  125. $tally = new Fave_tally();
  126. $tally->notice_id = $noticeID;
  127. $tally->count = Fave_tally::countExistingFaves($noticeID);
  128. $tally->created = common_sql_now();
  129. $result = $tally->insert();
  130. if (!$result) {
  131. $msg = sprintf(
  132. // TRANS: Server exception.
  133. // TRANS: %d is the notice ID (number).
  134. _m("Could not create favorite tally for notice ID %d."),
  135. $noticeID
  136. );
  137. throw new ServerException($msg);
  138. }
  139. }
  140. return $tally;
  141. }
  142. /**
  143. * Count the number of faves a notice already has. Used to initalize
  144. * a tally for a notice.
  145. *
  146. * @param integer $noticeID ID of the notice to count faves for
  147. *
  148. * @return integer $total total number of time the notice has been favored
  149. */
  150. public static function countExistingFaves($noticeID)
  151. {
  152. $fave = new Fave();
  153. $fave->notice_id = $noticeID;
  154. $total = $fave->count();
  155. return $total;
  156. }
  157. }