Group_message_profile.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * Who received a group message
  4. *
  5. * PHP version 5
  6. *
  7. * @category Data
  8. * @package StatusNet
  9. * @author Evan Prodromou <evan@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) 2011, 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 group direct messages for users
  35. *
  36. * @category GroupPrivateMessage
  37. * @package StatusNet
  38. * @author Evan Prodromou <evan@status.net>
  39. * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
  40. * @link http://status.net/
  41. *
  42. * @see DB_DataObject
  43. */
  44. class Group_message_profile extends Managed_DataObject
  45. {
  46. public $__table = 'group_message_profile'; // table name
  47. public $to_profile; // int
  48. public $group_message_id; // varchar(36) primary_key not_null
  49. public $created; // datetime() not_null
  50. public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
  51. public static function schemaDef()
  52. {
  53. return array(
  54. 'fields' => array(
  55. 'to_profile' => array('type' => 'int', 'not null' => true, 'description' => 'id of group direct message'),
  56. 'group_message_id' => array('type' => 'char', 'not null' => true, 'length' => 36, 'description' => 'related group message uuid'),
  57. 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
  58. 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
  59. ),
  60. 'primary key' => array('to_profile', 'group_message_id'),
  61. 'foreign keys' => array(
  62. 'group_message_profile_to_profile_fkey' => array('profile', array('to_profile' => 'id')),
  63. 'group_message_profile_group_message_id_fkey' => array('group_message', array('group_message_id' => 'id')),
  64. ),
  65. );
  66. }
  67. function send($gm, $profile)
  68. {
  69. $gmp = new Group_message_profile();
  70. $gmp->group_message_id = $gm->id;
  71. $gmp->to_profile = $profile->id;
  72. $gmp->created = common_sql_now();
  73. $gmp->insert();
  74. // If it's not for the author, send email notification
  75. if ($gm->from_profile != $profile->id) {
  76. $gmp->notify();
  77. }
  78. return $gmp;
  79. }
  80. function notify()
  81. {
  82. // XXX: add more here
  83. $this->notifyByMail();
  84. }
  85. function notifyByMail()
  86. {
  87. $to = User::getKV('id', $this->to_profile);
  88. if (empty($to) || is_null($to->email) || !$to->emailnotifymsg) {
  89. return true;
  90. }
  91. $gm = Group_message::getKV('id', $this->group_message_id);
  92. $from_profile = Profile::getKV('id', $gm->from_profile);
  93. $group = $gm->getGroup();
  94. common_switch_locale($to->language);
  95. // TRANS: Subject for direct-message notification email.
  96. // TRANS: %1$s is the sending user's nickname, %2$s is the group nickname.
  97. $subject = sprintf(_m('New private message from %1$s to group %2$s'), $from_profile->nickname, $group->nickname);
  98. // TRANS: Body for direct-message notification email.
  99. // TRANS: %1$s is the sending user's long name, %2$s is the sending user's nickname,
  100. // TRANS: %3$s is the message content, %4$s a URL to the message,
  101. // TRANS: %5$s is the StatusNet sitename.
  102. $body = sprintf(_m("%1\$s (%2\$s) sent a private message to group %3\$s:\n\n".
  103. "------------------------------------------------------\n".
  104. "%4\$s\n".
  105. "------------------------------------------------------\n\n".
  106. "You can reply to their message here:\n\n".
  107. "%5\$s\n\n".
  108. "Do not reply to this email; it will not get to them.\n\n".
  109. "With kind regards,\n".
  110. "%6\$s"),
  111. $from_profile->getBestName(),
  112. $from_profile->nickname,
  113. $group->nickname,
  114. $gm->content,
  115. common_local_url('newmessage', array('to' => $from_profile->id)),
  116. common_config('site', 'name')) . "\n";
  117. $headers = _mail_prepare_headers('message', $to->nickname, $from_profile->nickname);
  118. common_switch_locale();
  119. return mail_to_user($to, $subject, $body, $headers);
  120. }
  121. }