Email_reminder.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2011, StatusNet, Inc.
  5. *
  6. * Data class for email reminders
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. * @category Data
  22. * @package EmailReminder
  23. * @author Zach Copley <zach@status.net>
  24. * @copyright 2011 StatusNet, Inc.
  25. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  26. * @link http://status.net/
  27. */
  28. class Email_reminder extends Managed_DataObject
  29. {
  30. public $__table = 'email_reminder';
  31. public $type; // type of reminder
  32. public $code; // confirmation code
  33. public $days; // number of days after code was created
  34. public $sent; // timestamp
  35. public $created; // timestamp
  36. public $modified; // timestamp
  37. /**
  38. * Do we need to send a reminder?
  39. *
  40. * @param string $type type of reminder
  41. * @param Object $object an object with a 'code' property
  42. * (Confirm_address or Invitation)
  43. * @param int $days Number of days after the code was created
  44. * @return boolean true if any Email_reminder records were found
  45. */
  46. static function needsReminder($type, $object, $days = null) {
  47. $reminder = new Email_reminder();
  48. $reminder->type = $type;
  49. $reminder->code = $object->code;
  50. if (!empty($days)) {
  51. $reminder->days = $days;
  52. }
  53. $result = $reminder->find();
  54. if (!empty($result)) {
  55. return false;
  56. }
  57. return true;
  58. }
  59. /**
  60. * Record a record of sending the reminder
  61. *
  62. * @param string $type type of reminder
  63. * @param Object $object an object with a 'code' property
  64. * (Confirm_address or Invitation)
  65. * @param int $days Number of days after the code was created
  66. * @return int $result row ID of the new reminder record
  67. */
  68. static function recordReminder($type, $object, $days) {
  69. $reminder = new Email_reminder();
  70. $reminder->type = $type;
  71. $reminder->code = $object->code;
  72. $reminder->days = $days;
  73. $reminder->sent = $reminder->created = common_sql_now();
  74. $result = $reminder->insert();
  75. if (empty($result)) {
  76. common_log_db_error($reminder, 'INSERT', __FILE__);
  77. throw new ServerException(
  78. // TRANS: Server exception thrown when a reminder record could not be inserted into the database.
  79. _m('Database error inserting reminder record.')
  80. );
  81. }
  82. return $result;
  83. }
  84. /**
  85. * Data definition for email reminders
  86. */
  87. public static function schemaDef()
  88. {
  89. return array(
  90. 'description' => 'Record of email reminders that have been sent',
  91. 'fields' => array(
  92. 'type' => array(
  93. 'type' => 'varchar',
  94. 'length' => 255,
  95. 'not null' => true,
  96. 'description' => 'type of reminder'
  97. ),
  98. 'code' => array(
  99. 'type' => 'varchar',
  100. 'not null' => 'true',
  101. 'length' => 255,
  102. 'description' => 'confirmation code'
  103. ),
  104. 'days' => array(
  105. 'type' => 'int',
  106. 'not null' => 'true',
  107. 'description' => 'number of days since code creation'
  108. ),
  109. 'sent' => array(
  110. 'type' => 'datetime',
  111. 'not null' => true,
  112. 'description' => 'Date and time the reminder was sent'
  113. ),
  114. 'created' => array(
  115. 'type' => 'datetime',
  116. 'not null' => true,
  117. 'description' => 'Date and time the record was created'
  118. ),
  119. 'modified' => array(
  120. 'type' => 'timestamp',
  121. 'not null' => true,
  122. 'description' => 'Date and time the record was last modified'
  123. ),
  124. ),
  125. 'primary key' => array('type', 'code', 'days'),
  126. 'indexes' => array(
  127. 'sent_idx' => array('sent'),
  128. ),
  129. );
  130. }
  131. }