Profile_tag_subscription.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * Table Definition for profile_tag_subscription
  4. */
  5. require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
  6. class Profile_tag_subscription extends Managed_DataObject
  7. {
  8. ###START_AUTOCODE
  9. /* the code below is auto generated do not remove the above tag */
  10. public $__table = 'profile_tag_subscription'; // table name
  11. public $profile_tag_id; // int(4) not_null
  12. public $profile_id; // int(4) not_null
  13. public $created; // datetime() not_null default_0000-00-00%2000%3A00%3A00
  14. public $modified; // datetime() not_null default_CURRENT_TIMESTAMP
  15. /* the code above is auto generated do not remove the tag below */
  16. ###END_AUTOCODE
  17. public static function schemaDef()
  18. {
  19. return array(
  20. 'fields' => array(
  21. 'profile_tag_id' => array('type' => 'int', 'not null' => true, 'description' => 'foreign key to profile_tag'),
  22. 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'foreign key to profile table'),
  23. 'created' => array('type' => 'datetime', 'not null' => true, 'default' => '0000-00-00 00:00:00', 'description' => 'date this record was created'),
  24. 'modified' => array('type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'),
  25. ),
  26. 'primary key' => array('profile_tag_id', 'profile_id'),
  27. 'foreign keys' => array(
  28. 'profile_tag_subscription_profile_list_id_fkey' => array('profile_list', array('profile_tag_id' => 'id')),
  29. 'profile_tag_subscription_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
  30. ),
  31. 'indexes' => array(
  32. // @fixme probably we want a (profile_id, created) index here?
  33. 'profile_tag_subscription_profile_id_idx' => array('profile_id'),
  34. 'profile_tag_subscription_created_idx' => array('created'),
  35. ),
  36. );
  37. }
  38. static function add($peopletag, $profile)
  39. {
  40. if ($peopletag->private) {
  41. return false;
  42. }
  43. if (Event::handle('StartSubscribePeopletag', array($peopletag, $profile))) {
  44. $args = array('profile_tag_id' => $peopletag->id,
  45. 'profile_id' => $profile->id);
  46. $existing = Profile_tag_subscription::pkeyGet($args);
  47. if(!empty($existing)) {
  48. return $existing;
  49. }
  50. $sub = new Profile_tag_subscription();
  51. $sub->profile_tag_id = $peopletag->id;
  52. $sub->profile_id = $profile->id;
  53. $sub->created = common_sql_now();
  54. $result = $sub->insert();
  55. if (!$result) {
  56. common_log_db_error($sub, 'INSERT', __FILE__);
  57. // TRANS: Exception thrown when inserting a list subscription in the database fails.
  58. throw new Exception(_('Adding list subscription failed.'));
  59. }
  60. $ptag = Profile_list::getKV('id', $peopletag->id);
  61. $ptag->subscriberCount(true);
  62. Event::handle('EndSubscribePeopletag', array($peopletag, $profile));
  63. return $ptag;
  64. }
  65. }
  66. static function remove($peopletag, $profile)
  67. {
  68. $sub = Profile_tag_subscription::pkeyGet(array('profile_tag_id' => $peopletag->id,
  69. 'profile_id' => $profile->id));
  70. if (empty($sub)) {
  71. // silence is golden?
  72. return true;
  73. }
  74. if (Event::handle('StartUnsubscribePeopletag', array($peopletag, $profile))) {
  75. $result = $sub->delete();
  76. if (!$result) {
  77. common_log_db_error($sub, 'DELETE', __FILE__);
  78. // TRANS: Exception thrown when deleting a list subscription from the database fails.
  79. throw new Exception(_('Removing list subscription failed.'));
  80. }
  81. $peopletag->subscriberCount(true);
  82. Event::handle('EndUnsubscribePeopletag', array($peopletag, $profile));
  83. return true;
  84. }
  85. }
  86. // called if a tag gets deleted / made private
  87. static function cleanup($profile_list) {
  88. $subs = new self();
  89. $subs->profile_tag_id = $profile_list->id;
  90. $subs->find();
  91. while($subs->fetch()) {
  92. $profile = Profile::getKV('id', $subs->profile_id);
  93. Event::handle('StartUnsubscribePeopletag', array($profile_list, $profile));
  94. // Delete anyway
  95. $subs->delete();
  96. Event::handle('StartUnsubscribePeopletag', array($profile_list, $profile));
  97. }
  98. }
  99. function insert()
  100. {
  101. $result = parent::insert();
  102. if ($result) {
  103. self::blow('profile_list:subscriber_count:%d',
  104. $this->profile_tag_id);
  105. }
  106. return $result;
  107. }
  108. function delete($useWhere=false)
  109. {
  110. $result = parent::delete($useWhere);
  111. if ($result !== false) {
  112. self::blow('profile_list:subscriber_count:%d',
  113. $this->profile_tag_id);
  114. }
  115. return $result;
  116. }
  117. }