]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/GroupPrivateMessage/Group_message_profile.php
The overloaded DB_DataObject function staticGet is now called getKV
[quix0rs-gnu-social.git] / plugins / GroupPrivateMessage / Group_message_profile.php
1 <?php
2 /**
3  * Who received a group message
4  *
5  * PHP version 5
6  *
7  * @category Data
8  * @package  StatusNet
9  * @author   Evan Prodromou <evan@status.net>
10  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
11  * @link     http://status.net/
12  *
13  * StatusNet - the distributed open-source microblogging tool
14  * Copyright (C) 2011, StatusNet, Inc.
15  *
16  * This program is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU Affero General Public License as published by
18  * the Free Software Foundation, either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.     See the
24  * GNU Affero General Public License for more details.
25  *
26  * You should have received a copy of the GNU Affero General Public License
27  * along with this program. If not, see <http://www.gnu.org/licenses/>.
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR . '/classes/Memcached_DataObject.php';
35
36 /**
37  * Data class for group direct messages for users
38  *
39  * @category GroupPrivateMessage
40  * @package  StatusNet
41  * @author   Evan Prodromou <evan@status.net>
42  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
43  * @link     http://status.net/
44  *
45  * @see      DB_DataObject
46  */
47 class Group_message_profile extends Managed_DataObject
48 {
49     public $__table = 'group_message_profile'; // table name
50     public $to_profile;                        // int
51     public $group_message_id;                  // char(36)  primary_key not_null
52     public $created;
53
54     /**
55      * return table definition for DB_DataObject
56      *
57      * DB_DataObject needs to know something about the table to manipulate
58      * instances. This method provides all the DB_DataObject needs to know.
59      *
60      * @return array array of column definitions
61      */
62     function table()
63     {
64         return array('to_profile' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
65                      'group_message_id' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
66                      'created' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL);
67     }
68
69     /**
70      * return key definitions for DB_DataObject
71      *
72      * DB_DataObject needs to know about keys that the table has, since it
73      * won't appear in StatusNet's own keys list. In most cases, this will
74      * simply reference your keyTypes() function.
75      *
76      * @return array list of key field names
77      */
78     function keys()
79     {
80         return array_keys($this->keyTypes());
81     }
82
83     /**
84      * return key definitions for Memcached_DataObject
85      *
86      * @return array associative array of key definitions, field name to type:
87      *         'K' for primary key: for compound keys, add an entry for each component;
88      *         'U' for unique keys: compound keys are not well supported here.
89      */
90     function keyTypes()
91     {
92         return array('to_profile' => 'K', 'group_message_id' => 'K');
93     }
94
95     /**
96      * No sequence keys in this table.
97      */
98     function sequenceKey()
99     {
100         return array(false, false, false);
101     }
102
103     function send($gm, $profile)
104     {
105         $gmp = new Group_message_profile();
106
107         $gmp->group_message_id = $gm->id;
108         $gmp->to_profile       = $profile->id;
109         $gmp->created          = common_sql_now();
110
111         $gmp->insert();
112
113         // If it's not for the author, send email notification
114         if ($gm->from_profile != $profile->id) {
115             $gmp->notify();
116         }
117
118         return $gmp;
119     }
120
121     function notify()
122     {
123         // XXX: add more here
124         $this->notifyByMail();
125     }
126
127     function notifyByMail()
128     {
129         $to = User::getKV('id', $this->to_profile);
130
131         if (empty($to) || is_null($to->email) || !$to->emailnotifymsg) {
132             return true;
133         }
134
135         $gm = Group_message::getKV('id', $this->group_message_id);
136
137         $from_profile = Profile::getKV('id', $gm->from_profile);
138
139         $group = $gm->getGroup();
140
141         common_switch_locale($to->language);
142
143         // TRANS: Subject for direct-message notification email.
144         // TRANS: %1$s is the sending user's nickname, %2$s is the group nickname.
145         $subject = sprintf(_m('New private message from %1$s to group %2$s'), $from_profile->nickname, $group->nickname);
146
147         // TRANS: Body for direct-message notification email.
148         // TRANS: %1$s is the sending user's long name, %2$s is the sending user's nickname,
149         // TRANS: %3$s is the message content, %4$s a URL to the message,
150         // TRANS: %5$s is the StatusNet sitename.
151         $body = sprintf(_m("%1\$s (%2\$s) sent a private message to group %3\$s:\n\n".
152                            "------------------------------------------------------\n".
153                            "%4\$s\n".
154                            "------------------------------------------------------\n\n".
155                            "You can reply to their message here:\n\n".
156                            "%5\$s\n\n".
157                            "Do not reply to this email; it will not get to them.\n\n".
158                            "With kind regards,\n".
159                            "%6\$s"),
160                         $from_profile->getBestName(),
161                         $from_profile->nickname,
162                         $group->nickname,
163                         $gm->content,
164                         common_local_url('newmessage', array('to' => $from_profile->id)),
165                         common_config('site', 'name')) . "\n";
166
167         $headers = _mail_prepare_headers('message', $to->nickname, $from_profile->nickname);
168
169         common_switch_locale();
170
171         return mail_to_user($to, $subject, $body, $headers);
172     }
173 }