]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/EmailReminder/classes/Email_reminder.php
235305f04722a3c26587edfa9a2287012879b9dc
[quix0rs-gnu-social.git] / plugins / EmailReminder / classes / Email_reminder.php
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
29 class Email_reminder extends Managed_DataObject
30 {
31     public $__table = 'email_reminder';
32
33     public $type;     // type of reminder
34     public $code;     // confirmation code
35     public $days;     // number of days after code was created
36     public $sent;     // timestamp
37     public $created;  // timestamp
38     public $modified; // timestamp
39
40     /**
41      * Do we need to send a reminder?
42      *
43      * @param string $type      type of reminder
44      * @param Object $object    an object with a 'code' property
45      *                          (Confirm_address or Invitation)
46      * @param int    $days      Number of days after the code was created
47      * @return boolean true if any Email_reminder records were found
48      */
49     static function needsReminder($type, $object, $days = null) {
50
51         $reminder        = new Email_reminder();
52         $reminder->type  = $type;
53         $reminder->code  = $object->code;
54         if (!empty($days)) {
55             $reminder->days  = $days;
56         }
57         $result = $reminder->find();
58
59         if (!empty($result)) {
60             return false;
61         }
62
63         return true;
64     }
65
66     /**
67      * Record a record of sending the reminder
68      *
69      * @param string $type      type of reminder
70      * @param Object $object    an object with a 'code' property
71      *                          (Confirm_address or Invitation)
72      * @param int    $days      Number of days after the code was created
73      * @return int   $result    row ID of the new reminder record
74      */
75     static function recordReminder($type, $object, $days) {
76
77         $reminder        = new Email_reminder();
78         $reminder->type  = $type;
79         $reminder->code  = $object->code;
80         $reminder->days  = $days;
81         $reminder->sent  = $reminder->created = common_sql_now();
82         $result          = $reminder->insert();
83
84         if (empty($result)) {
85             common_log_db_error($reminder, 'INSERT', __FILE__);
86                 throw new ServerException(
87                     // TRANS: Server exception thrown when a reminder record could not be inserted into the database.
88                     _m('Database error inserting reminder record.')
89             );
90         }
91
92         return $result;
93     }
94
95     /**
96      * Data definition for email reminders
97      */
98     public static function schemaDef()
99     {
100         return array(
101             'description' => 'Record of email reminders that have been sent',
102             'fields'      => array(
103                 'type'     => array(
104                     'type'          => 'varchar',
105                     'length'        => 255,
106                     'not null'      => true,
107                     'description'   => 'type of reminder'
108                 ),
109                 'code' => array(
110                     'type'        => 'varchar',
111                     'not null'    => 'true',
112                     'length'      => 255,
113                     'description' => 'confirmation code'
114                  ),
115                 'days' => array(
116                     'type'        => 'int',
117                     'not null'    => 'true',
118                     'description' => 'number of days since code creation'
119                  ),
120                 'sent' => array(
121                     'type'        => 'datetime',
122                     'not null'    => true,
123                     'description' => 'Date and time the reminder was sent'
124                 ),
125                 'created' => array(
126                     'type'        => 'datetime',
127                     'not null'    => true,
128                     'description' => 'Date and time the record was created'
129                 ),
130                 'modified'        => array(
131                     'type'        => 'timestamp',
132                     'not null'    => true,
133                     'description' => 'Date and time the record was last modified'
134                 ),
135             ),
136             'primary key' => array('type', 'code', 'days'),
137             'indexes' => array(
138                 'sent_idx' => array('sent'),
139              ),
140         );
141     }
142 }