Email_reminder.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 email reminders
  18. *
  19. * @category Data
  20. * @package EmailReminder
  21. * @author Zach Copley <zach@status.net>
  22. * @copyright 2011 StatusNet, Inc.
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. defined('GNUSOCIAL') || die();
  26. class Email_reminder extends Managed_DataObject
  27. {
  28. public $__table = 'email_reminder';
  29. public $type; // type of reminder varchar(191) not 255 because utf8mb4 takes more space
  30. public $code; // confirmation code varchar(191) not 255 because utf8mb4 takes more space
  31. public $days; // number of days after code was created
  32. public $sent; // timestamp
  33. public $created; // timestamp
  34. public $modified; // timestamp
  35. /**
  36. * Do we need to send a reminder?
  37. *
  38. * @param string $type type of reminder
  39. * @param Object $object an object with a 'code' property
  40. * (Confirm_address or Invitation)
  41. * @param int $days Number of days after the code was created
  42. * @return boolean true if any Email_reminder records were found
  43. */
  44. public static function needsReminder($type, $object, $days = null)
  45. {
  46. $reminder = new Email_reminder();
  47. $reminder->type = $type;
  48. $reminder->code = $object->code;
  49. if (!empty($days)) {
  50. $reminder->days = $days;
  51. }
  52. $result = $reminder->find();
  53. if (!empty($result)) {
  54. return false;
  55. }
  56. return true;
  57. }
  58. /**
  59. * Record a record of sending the reminder
  60. *
  61. * @param string $type type of reminder
  62. * @param Object $object an object with a 'code' property
  63. * (Confirm_address or Invitation)
  64. * @param int $days Number of days after the code was created
  65. * @return int $result row ID of the new reminder record
  66. */
  67. public static function recordReminder($type, $object, $days)
  68. {
  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' => 191,
  95. 'not null' => true,
  96. 'description' => 'type of reminder'
  97. ),
  98. 'code' => array(
  99. 'type' => 'varchar',
  100. 'not null' => true,
  101. 'length' => 191,
  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. }