]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/EmailSummary/EmailSummaryPlugin.php
Merge branch 'master' into testing
[quix0rs-gnu-social.git] / plugins / EmailSummary / EmailSummaryPlugin.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * Sends an email summary of the inbox to users in the network
7  *
8  * PHP version 5
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Affero General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Affero General Public License for more details.
19  *
20  * You should have received a copy of the GNU Affero General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * @category  Sample
24  * @package   StatusNet
25  * @author    Evan Prodromou <evan@status.net>
26  * @copyright 2010 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET')) {
32     exit(1);
33 }
34
35 /**
36  * Plugin for sending email summaries to users
37  *
38  * @category  Email
39  * @package   StatusNet
40  * @author    Brion Vibber <brionv@status.net>
41  * @author    Evan Prodromou <evan@status.net>
42  * @copyright 2010 StatusNet, Inc.
43  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
44  * @link      http://status.net/
45  */
46 class EmailSummaryPlugin extends Plugin
47 {
48     /**
49      * Database schema setup
50      *
51      * @return boolean hook value
52      */
53     function onCheckSchema()
54     {
55         $schema = Schema::get();
56
57         // For storing user-submitted flags on profiles
58         $schema->ensureTable('email_summary_status',
59                              array(new ColumnDef('user_id', 'integer', null,
60                                                  false, 'PRI'),
61                                    new ColumnDef('send_summary', 'tinyint', null,
62                                                  false, null, 1),
63                                    new ColumnDef('last_summary_id', 'integer', null,
64                                                  true),
65                                    new ColumnDef('created', 'datetime', null,
66                                                  false),
67                                    new ColumnDef('modified', 'datetime', null,
68                                                  false),
69                              )
70         );
71         return true;
72     }
73
74     /**
75      * Load related modules when needed
76      *
77      * @param string $cls Name of the class to be loaded
78      *
79      * @return boolean hook value; true means continue processing, false means stop.
80      *
81      */
82     function onAutoload($cls)
83     {
84         $dir = dirname(__FILE__);
85
86         switch ($cls)
87             {
88             case 'SiteEmailSummaryHandler':
89             case 'UserEmailSummaryHandler':
90                 include_once $dir . '/'.strtolower($cls).'.php';
91             return false;
92             case 'Email_summary_status':
93                 include_once $dir . '/'.$cls.'.php';
94                 return false;
95             default:
96                 return true;
97             }
98     }
99
100     /**
101      * Version info for this plugin
102      *
103      * @param array &$versions array of version data
104      *
105      * @return boolean hook value; true means continue processing, false means stop.
106      */
107     function onPluginVersion(&$versions)
108     {
109         $versions[] = array('name' => 'EmailSummary',
110                             'version' => STATUSNET_VERSION,
111                             'author' => 'Evan Prodromou',
112                             'homepage' => 'http://status.net/wiki/Plugin:EmailSummary',
113                             'rawdescription' =>
114                             // TRANS: Plugin description.
115                             _m('Send an email summary of the inbox to users.'));
116         return true;
117     }
118
119     /**
120      * Register our queue handlers
121      *
122      * @param QueueManager $qm Current queue manager
123      *
124      * @return boolean hook value
125      */
126     function onEndInitializeQueueManager($qm)
127     {
128         $qm->connect('sitesum', 'SiteEmailSummaryHandler');
129         $qm->connect('usersum', 'UserEmailSummaryHandler');
130         return true;
131     }
132
133     /**
134      * Add a checkbox to turn off email summaries
135      *
136      * @param Action $action Action being executed (emailsettings)
137      *
138      * @return boolean hook value
139      */
140     function onEndEmailFormData($action)
141     {
142         $user = common_current_user();
143
144         $action->elementStart('li');
145         $action->checkbox('emailsummary',
146                           // TRANS: Checkbox label in e-mail preferences form.
147                           _m('Send me a periodic summary of updates from my network'),
148                           Email_summary_status::getSendSummary($user->id));
149         $action->elementEnd('li');
150         return true;
151     }
152
153     /**
154      * Add a checkbox to turn off email summaries
155      *
156      * @param Action $action Action being executed (emailsettings)
157      *
158      * @return boolean hook value
159      */
160     function onEndEmailSaveForm($action)
161     {
162         $sendSummary = $action->boolean('emailsummary');
163
164         $user = common_current_user();
165
166         if (!empty($user)) {
167
168             $ess = Email_summary_status::staticGet('user_id', $user->id);
169
170             if (empty($ess)) {
171
172                 $ess = new Email_summary_status();
173
174                 $ess->user_id      = $user->id;
175                 $ess->send_summary = $sendSummary;
176                 $ess->created      = common_sql_now();
177                 $ess->modified     = common_sql_now();
178
179                 $ess->insert();
180
181             } else {
182
183                 $orig = clone($ess);
184
185                 $ess->send_summary = $sendSummary;
186                 $ess->modified     = common_sql_now();
187
188                 $ess->update($orig);
189             }
190         }
191
192         return true;
193     }
194 }