]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/EmailReminder/classes/Email_reminder.php
EmailReminder plugin to send reminders about various things
[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     const INVITE_REMINDER   = 'invite'; // @todo Move this to the invite reminder handler
32
33     public $__table = 'email_reminder';
34
35     public $type;     // type of reminder
36     public $code;     // confirmation code
37     public $days;     // number of days after code was created
38     public $sent;     // timestamp
39     public $created;  // timestamp
40     public $modified; // timestamp
41
42     /**
43      * Get an instance by key
44      *
45      * This is a utility method to get a single instance with a given key value.
46      *
47      * @param string $k Key to use to lookup
48      * @param mixed  $v Value to lookup
49      *
50      * @return QnA_Answer object found, or null for no hits
51      *
52      */
53     function staticGet($k, $v=null)
54     {
55         return Memcached_DataObject::staticGet('email_reminder', $k, $v);
56     }
57
58     /**
59      *
60      * @param type $type
61      * @param type $confirm
62      * @param type $day
63      * @return type
64      */
65     static function needsReminder($type, $confirm, $days) {
66
67         $reminder        = new Email_reminder();
68         $reminder->type  = $type;
69         $reminder->code  = $confirm->code;
70         $reminder->days  = $days;
71
72         $result = $reminder->find(true);
73
74         if (empty($result)) {
75             return true;
76         }
77
78         return false;
79     }
80
81     /**
82      *
83      * @param type $type
84      * @param type $confirm
85      * @param type $day
86      * @return type
87      */
88     static function recordReminder($type, $confirm, $days) {
89
90         $reminder        = new Email_reminder();
91         $reminder->type  = $type;
92         $reminder->code  = $confirm->code;
93         $reminder->days  = $days;
94         $reminder->sent  = $reminder->created = common_sql_now();
95         $result          = $reminder->insert();
96
97         if (empty($result)) {
98             common_log_db_error($reminder, 'INSERT', __FILE__);
99                 throw new ServerException(
100                     _m('Database error inserting reminder record.')
101             );
102         }
103
104         return $result;
105     }
106
107     /**
108      * Data definition for email reminders
109      */
110     public static function schemaDef()
111     {
112         return array(
113             'description' => 'Record of email reminders that have been sent',
114             'fields'      => array(
115                 'type'     => array(
116                     'type'          => 'varchar',
117                     'length'        => 255,
118                     'not null'      => true,
119                     'description'   => 'type of reminder'
120                 ),
121                 'code' => array(
122                     'type'        => 'varchar',
123                     'not null'    => 'true',
124                     'length'      => 255,
125                     'description' => 'confirmation code'
126                  ),
127                 'days' => array(
128                     'type'        => 'int',
129                     'not null'    => 'true',
130                     'description' => 'number of days since code creation'
131                  ),
132                 'sent' => array(
133                     'type'        => 'datetime',
134                     'not null'    => true,
135                     'description' => 'Date and time the reminder was sent'
136                 ),
137                 'created' => array(
138                     'type'        => 'datetime',
139                     'not null'    => true,
140                     'description' => 'Date and time the record was created'
141                 ),
142                 'modified'        => array(
143                     'type'        => 'timestamp',
144                     'not null'    => true,
145                     'description' => 'Date and time the record was last modified'
146                 ),
147             ),
148             'primary key' => array('type', 'code', 'days'),
149             'indexes' => array(
150                 'sent_idx' => array('sent'),
151              ),
152         );
153     }
154 }