]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/GroupPrivateMessage/Group_message_profile.php
Merge branch '1.0.x' into people_tags_rebase
[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
48 class Group_message_profile extends Memcached_DataObject
49 {
50     public $__table = 'group_message_profile'; // table name
51     public $to_profile;                        // int
52     public $group_message_id;                  // char(36)  primary_key not_null
53     public $created;
54
55     /**
56      * Get an instance by key
57      *
58      * This is a utility method to get a single instance with a given key value.
59      *
60      * @param string $k Key to use to lookup (usually 'user_id' for this class)
61      * @param mixed  $v Value to lookup
62      *
63      * @return Group_message object found, or null for no hits
64      *
65      */
66     function staticGet($k, $v=null)
67     {
68         return Memcached_DataObject::staticGet('Group_message_profile', $k, $v);
69     }
70
71     /**
72      * return table definition for DB_DataObject
73      *
74      * DB_DataObject needs to know something about the table to manipulate
75      * instances. This method provides all the DB_DataObject needs to know.
76      *
77      * @return array array of column definitions
78      */
79     function table()
80     {
81         return array('to_profile' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
82                      'group_message_id' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
83                      'created' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL);
84     }
85
86     /**
87      * return key definitions for DB_DataObject
88      *
89      * DB_DataObject needs to know about keys that the table has, since it
90      * won't appear in StatusNet's own keys list. In most cases, this will
91      * simply reference your keyTypes() function.
92      *
93      * @return array list of key field names
94      */
95     function keys()
96     {
97         return array_keys($this->keyTypes());
98     }
99
100     /**
101      * return key definitions for Memcached_DataObject
102      *
103      * @return array associative array of key definitions, field name to type:
104      *         'K' for primary key: for compound keys, add an entry for each component;
105      *         'U' for unique keys: compound keys are not well supported here.
106      */
107     function keyTypes()
108     {
109         return array('to_profile' => 'K', 'group_message_id' => 'K');
110     }
111
112     /**
113      * No sequence keys in this table.
114      */
115     function sequenceKey()
116     {
117         return array(false, false, false);
118     }
119
120     function send($gm, $profile)
121     {
122         $gmp = new Group_message_profile();
123         
124         $gmp->group_message_id = $gm->id;
125         $gmp->to_profile       = $profile->id;
126         $gmp->created          = common_sql_now();
127
128         $gmp->insert();
129
130         $gmp->notify();
131
132         return $gmp;
133     }
134
135     function notify()
136     {
137         // XXX: add more here
138         $this->notifyByMail();
139     }
140
141     function notifyByMail() 
142     {
143         $to = User::staticGet('id', $this->to_profile);
144
145         if (empty($to) || is_null($to->email) || !$to->emailnotifymsg) {
146             return true;
147         }
148
149         $gm = Group_message::staticGet('id', $this->group_message_id);
150
151         $from_profile = Profile::staticGet('id', $gm->from_profile);
152
153         $group = $gm->getGroup();
154
155         common_switch_locale($to->language);
156
157         // TRANS: Subject for direct-message notification email.
158         // TRANS: %s is the sending user's nickname.
159         $subject = sprintf(_('New private message from %s to group %s'), $from_profile->nickname, $group->nickname);
160
161         // TRANS: Body for direct-message notification email.
162         // TRANS: %1$s is the sending user's long name, %2$s is the sending user's nickname,
163         // TRANS: %3$s is the message content, %4$s a URL to the message,
164         // TRANS: %5$s is the StatusNet sitename.
165         $body = sprintf(_("%1\$s (%2\$s) sent a private message to group %3\$s:\n\n".
166                           "------------------------------------------------------\n".
167                           "%4\$s\n".
168                           "------------------------------------------------------\n\n".
169                           "You can reply to their message here:\n\n".
170                           "%5\$s\n\n".
171                           "Don't reply to this email; it won't get to them.\n\n".
172                           "With kind regards,\n".
173                           "%6\$s\n"),
174                         $from_profile->getBestName(),
175                         $from_profile->nickname,
176                         $group->nickname,
177                         $gm->content,
178                         common_local_url('newmessage', array('to' => $from_profile->id)),
179                         common_config('site', 'name'));
180
181         $headers = _mail_prepare_headers('message', $to->nickname, $from_profile->nickname);
182
183         common_switch_locale();
184
185         return mail_to_user($to, $subject, $body, $headers);
186     }
187 }