]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/EmailSummary/EmailSummaryPlugin.php
Merge branch 'master' of gitorious.org:statusnet/mainline into 0.9.x
[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
47 class EmailSummaryPlugin extends Plugin
48 {
49     /**
50      * Database schema setup
51      *
52      * @return boolean hook value
53      */
54
55     function onCheckSchema()
56     {
57         $schema = Schema::get();
58
59         // For storing user-submitted flags on profiles
60
61         $schema->ensureTable('email_summary_status',
62                              array(new ColumnDef('user_id', 'integer', null,
63                                                  false, 'PRI'),
64                                    new ColumnDef('send_summary', 'tinyint', null,
65                                                  false, null, 1),
66                                    new ColumnDef('last_summary_id', 'integer', null,
67                                                  true),
68                                    new ColumnDef('created', 'datetime', null,
69                                                  false),
70                                    new ColumnDef('modified', 'datetime', null,
71                                                  false),
72                                    )
73                              );
74         return true;
75     }
76
77     /**
78      * Load related modules when needed
79      *
80      * @param string $cls Name of the class to be loaded
81      *
82      * @return boolean hook value; true means continue processing, false means stop.
83      * 
84      */
85     
86     function onAutoload($cls)
87     {
88         $dir = dirname(__FILE__);
89
90         switch ($cls)
91         {
92          case 'SiteEmailSummaryHandler':
93          case 'UserEmailSummaryHandler':
94             include_once $dir . '/'.strtolower($cls).'.php';
95             return false;
96          case 'Email_summary_status':
97             include_once $dir . '/'.$cls.'.php';
98             return false;
99          default:
100             return true;
101         }
102     }
103
104     /**
105      * Version info for this plugin
106      *
107      * @param array &$versions array of version data
108      *
109      * @return boolean hook value; true means continue processing, false means stop.
110      * 
111      */
112     
113     function onPluginVersion(&$versions)
114     {
115         $versions[] = array('name' => 'EmailSummary',
116                             'version' => STATUSNET_VERSION,
117                             'author' => 'Evan Prodromou',
118                             'homepage' => 'http://status.net/wiki/Plugin:EmailSummary',
119                             'rawdescription' =>
120                             _m('Send an email summary of the inbox to users.'));
121         return true;
122     }
123
124     /**
125      * Register our queue handlers
126      * 
127      * @param QueueManager $qm Current queue manager
128      * 
129      * @return boolean hook value
130      */
131     
132     function onEndInitializeQueueManager($qm)
133     {
134         $qm->connect('sitesum', 'SiteEmailSummaryHandler');
135         $qm->connect('usersum', 'UserEmailSummaryHandler');
136         return true;
137     }
138     
139     /**
140      * Add a checkbox to turn off email summaries
141      * 
142      * @param Action $action Action being executed (emailsettings)
143      * 
144      * @return boolean hook value
145      */
146     
147     function onEndEmailFormData($action)
148     {
149         $user = common_current_user();
150         
151         $action->elementStart('li');
152         $action->checkbox('emailsummary',
153                           // TRANS: Checkbox label in e-mail preferences form.
154                           _('Send me a periodic summary of updates from my network.'),
155                           Email_summary_status::getSendSummary($user->id));
156         $action->elementEnd('li');
157         return true;
158     }
159     
160     /**
161      * Add a checkbox to turn off email summaries
162      * 
163      * @param Action $action Action being executed (emailsettings)
164      * 
165      * @return boolean hook value
166      */
167     
168     function onEndEmailSaveForm($action)
169     {
170         $sendSummary = $action->boolean('emailsummary');
171         
172         $user = common_current_user();
173         
174         if (!empty($user)) {
175             
176             $ess = Email_summary_status::staticGet('user_id', $user->id);
177             
178             if (empty($ess)) {
179                 
180                 $ess = new Email_summary_status();
181
182                 $ess->user_id      = $user->id;
183                 $ess->send_summary = $sendSummary;
184                 $ess->created      = common_sql_now();
185                 $ess->modified     = common_sql_now();
186                 
187                 $ess->insert();
188                 
189             } else {
190                 
191                 $orig = clone($ess);
192                 
193                 $ess->send_summary = $sendSummary;
194                 $ess->modified     = common_sql_now();
195                 
196                 $ess->update($orig);
197             }
198         }
199         
200         return true;
201     }
202 }