Profile_tag_subscription.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. * Table Definition for profile_tag_subscription
  18. */
  19. defined('GNUSOCIAL') || die();
  20. class Profile_tag_subscription extends Managed_DataObject
  21. {
  22. ###START_AUTOCODE
  23. /* the code below is auto generated do not remove the above tag */
  24. public $__table = 'profile_tag_subscription'; // table name
  25. public $profile_tag_id; // int(4) not_null
  26. public $profile_id; // int(4) not_null
  27. public $created; // datetime()
  28. public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
  29. /* the code above is auto generated do not remove the tag below */
  30. ###END_AUTOCODE
  31. public static function schemaDef()
  32. {
  33. return array(
  34. 'fields' => array(
  35. 'profile_tag_id' => array('type' => 'int', 'not null' => true, 'description' => 'foreign key to profile_tag'),
  36. 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'foreign key to profile table'),
  37. 'created' => array('type' => 'datetime', 'description' => 'date this record was created'),
  38. 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
  39. ),
  40. 'primary key' => array('profile_tag_id', 'profile_id'),
  41. 'foreign keys' => array(
  42. 'profile_tag_subscription_profile_tag_id_fkey' => array('profile_list', array('profile_tag_id' => 'id')),
  43. 'profile_tag_subscription_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
  44. ),
  45. 'indexes' => array(
  46. 'profile_tag_subscription_profile_id_created_profile_tag_id_idx' => array('profile_id', 'created', 'profile_tag_id'),
  47. ),
  48. );
  49. }
  50. public static function add($peopletag, $profile)
  51. {
  52. if ($peopletag->private) {
  53. return false;
  54. }
  55. if (Event::handle('StartSubscribePeopletag', array($peopletag, $profile))) {
  56. $args = array('profile_tag_id' => $peopletag->id,
  57. 'profile_id' => $profile->id);
  58. $existing = Profile_tag_subscription::pkeyGet($args);
  59. if (!empty($existing)) {
  60. return $existing;
  61. }
  62. $sub = new Profile_tag_subscription();
  63. $sub->profile_tag_id = $peopletag->id;
  64. $sub->profile_id = $profile->id;
  65. $sub->created = common_sql_now();
  66. $result = $sub->insert();
  67. if (!$result) {
  68. common_log_db_error($sub, 'INSERT', __FILE__);
  69. // TRANS: Exception thrown when inserting a list subscription in the database fails.
  70. throw new Exception(_('Adding list subscription failed.'));
  71. }
  72. $ptag = Profile_list::getKV('id', $peopletag->id);
  73. $ptag->subscriberCount(true);
  74. Event::handle('EndSubscribePeopletag', array($peopletag, $profile));
  75. return $ptag;
  76. }
  77. }
  78. public static function remove($peopletag, $profile)
  79. {
  80. $sub = Profile_tag_subscription::pkeyGet(array('profile_tag_id' => $peopletag->id,
  81. 'profile_id' => $profile->id));
  82. if (empty($sub)) {
  83. // silence is golden?
  84. return true;
  85. }
  86. if (Event::handle('StartUnsubscribePeopletag', array($peopletag, $profile))) {
  87. $result = $sub->delete();
  88. if (!$result) {
  89. common_log_db_error($sub, 'DELETE', __FILE__);
  90. // TRANS: Exception thrown when deleting a list subscription from the database fails.
  91. throw new Exception(_('Removing list subscription failed.'));
  92. }
  93. $peopletag->subscriberCount(true);
  94. Event::handle('EndUnsubscribePeopletag', array($peopletag, $profile));
  95. return true;
  96. }
  97. }
  98. // called if a tag gets deleted / made private
  99. public static function cleanup($profile_list)
  100. {
  101. $subs = new self();
  102. $subs->profile_tag_id = $profile_list->id;
  103. $subs->find();
  104. while ($subs->fetch()) {
  105. $profile = Profile::getKV('id', $subs->profile_id);
  106. Event::handle('StartUnsubscribePeopletag', array($profile_list, $profile));
  107. // Delete anyway
  108. $subs->delete();
  109. Event::handle('StartUnsubscribePeopletag', array($profile_list, $profile));
  110. }
  111. }
  112. public function insert()
  113. {
  114. $result = parent::insert();
  115. if ($result) {
  116. self::blow(
  117. 'profile_list:subscriber_count:%d',
  118. $this->profile_tag_id
  119. );
  120. }
  121. return $result;
  122. }
  123. public function delete($useWhere = false)
  124. {
  125. $result = parent::delete($useWhere);
  126. if ($result !== false) {
  127. self::blow(
  128. 'profile_list:subscriber_count:%d',
  129. $this->profile_tag_id
  130. );
  131. }
  132. return $result;
  133. }
  134. }