]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/EmailSummary/EmailSummaryPlugin.php
IMPORTANT: Making prev. Memcached_DataObject working again with schemaDef
[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', Email_summary_status::schemaDef());
59         return true;
60     }
61
62     /**
63      * Load related modules when needed
64      *
65      * @param string $cls Name of the class to be loaded
66      *
67      * @return boolean hook value; true means continue processing, false means stop.
68      *
69      */
70     function onAutoload($cls)
71     {
72         $dir = dirname(__FILE__);
73
74         switch ($cls)
75             {
76             case 'SiteEmailSummaryHandler':
77             case 'UserEmailSummaryHandler':
78                 include_once $dir . '/'.strtolower($cls).'.php';
79             return false;
80             case 'Email_summary_status':
81                 include_once $dir . '/'.$cls.'.php';
82                 return false;
83             default:
84                 return true;
85             }
86     }
87
88     /**
89      * Version info for this plugin
90      *
91      * @param array &$versions array of version data
92      *
93      * @return boolean hook value; true means continue processing, false means stop.
94      */
95     function onPluginVersion(&$versions)
96     {
97         $versions[] = array('name' => 'EmailSummary',
98                             'version' => STATUSNET_VERSION,
99                             'author' => 'Evan Prodromou',
100                             'homepage' => 'http://status.net/wiki/Plugin:EmailSummary',
101                             'rawdescription' =>
102                             // TRANS: Plugin description.
103                             _m('Send an email summary of the inbox to users.'));
104         return true;
105     }
106
107     /**
108      * Register our queue handlers
109      *
110      * @param QueueManager $qm Current queue manager
111      *
112      * @return boolean hook value
113      */
114     function onEndInitializeQueueManager($qm)
115     {
116         $qm->connect('sitesum', 'SiteEmailSummaryHandler');
117         $qm->connect('usersum', 'UserEmailSummaryHandler');
118         return true;
119     }
120
121     /**
122      * Add a checkbox to turn off email summaries
123      *
124      * @param Action $action Action being executed (emailsettings)
125      *
126      * @return boolean hook value
127      */
128     function onEndEmailFormData($action)
129     {
130         $user = common_current_user();
131
132         $action->elementStart('li');
133         $action->checkbox('emailsummary',
134                           // TRANS: Checkbox label in e-mail preferences form.
135                           _m('Send me a periodic summary of updates from my network'),
136                           Email_summary_status::getSendSummary($user->id));
137         $action->elementEnd('li');
138         return true;
139     }
140
141     /**
142      * Add a checkbox to turn off email summaries
143      *
144      * @param Action $action Action being executed (emailsettings)
145      *
146      * @return boolean hook value
147      */
148     function onEndEmailSaveForm($action)
149     {
150         $sendSummary = $action->boolean('emailsummary');
151
152         $user = common_current_user();
153
154         if (!empty($user)) {
155
156             $ess = Email_summary_status::getKV('user_id', $user->id);
157
158             if (empty($ess)) {
159
160                 $ess = new Email_summary_status();
161
162                 $ess->user_id      = $user->id;
163                 $ess->send_summary = $sendSummary;
164                 $ess->created      = common_sql_now();
165                 $ess->modified     = common_sql_now();
166
167                 $ess->insert();
168
169             } else {
170
171                 $orig = clone($ess);
172
173                 $ess->send_summary = $sendSummary;
174                 $ess->modified     = common_sql_now();
175
176                 $ess->update($orig);
177             }
178         }
179
180         return true;
181     }
182 }