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