root/buddyloo/files/lib/form/InvitationEditForm.class.php @ 312

Revision 312, 5.2 kB (checked in by d0nut, 5 years ago)

added the mailer to buddyloo

Line 
1<?php
2require_once(WCF_DIR.'lib/form/AbstractForm.class.php');
3require_once(WCF_DIR.'lib/system/session/UserSession.class.php');
4require_once(WCF_DIR.'lib/system/exception/IllegalLinkException.class.php');
5require_once(WCF_DIR.'lib/page/util/menu/UserCPMenu.class.php');
6require_once(WCF_DIR.'lib/util/UserRegistrationUtil.class.php');
7
8/**
9 * invitation edit form
10 *
11 * @author      Torben Brodt
12 * @package     de.easy-coding.wcf.buddyloo
13 * @license     GNU General Public License <http://opensource.org/licenses/gpl-3.0.html>
14 */
15class InvitationEditForm extends AbstractForm {
16        public $input = '';
17        public $body = '';
18        public $inputArray = array();
19        public $emails = array();
20        public $templateName = 'buddylooInvitationEdit';
21       
22        /**
23         * @see Page::readParameters()
24         */
25        public function readParameters() {
26                parent::readParameters();
27               
28                if (isset($_GET['remove'])) {
29                        // delete invitation
30                        $sql = "DELETE FROM     wcf".WCF_N."_buddyloo_invitation
31                                WHERE           userID = ".WCF::getUser()->userID."
32                                                AND email = '".escapeString($_GET['remove'])."'";
33                        WCF::getDB()->sendQuery($sql);
34
35                        // show success message
36                        WCF::getTPL()->assign(array(
37                                'success' => 'remove',
38                                'emails' => $_GET['remove']
39                        ));
40                }
41                else if (isset($_GET['add'])) {
42                        $this->submit();
43                }
44        }
45       
46        /**
47         * @see Form::readFormParameters()
48         */
49        public function readFormParameters() {
50                parent::readFormParameters();
51               
52                if (isset($_POST['input'])) $this->input = StringUtil::trim($_POST['input']);
53                if (isset($_POST['body'])) $this->body = StringUtil::trim($_POST['body']);
54        }
55       
56        /**
57         * @see Form::validate()
58         */
59        public function validate() {
60                parent::validate();
61               
62                if (count($this->inputArray) == 0) {
63                        if (empty($this->input)) {
64                                throw new UserInputException('input');
65                        }
66                       
67                        $this->validateEmails();
68                }
69        }
70       
71        /**
72         * @see Form::save()
73         */
74        public function save() {
75                parent::save();
76               
77                // save user
78                $inserts = '';
79                foreach ($this->inputArray as $email) {
80                        if (!empty($inserts)) $inserts .= ',';
81                        $inserts .= "(".WCF::getUser()->userID.", '".escapeString($email)."')";
82                }
83               
84                if (!empty($inserts)) {
85                        $sql = "INSERT IGNORE INTO      wcf".WCF_N."_buddyloo_invitation
86                                                        (userID, email)
87                                VALUES                  ".$inserts;
88                        WCF::getDB()->sendQuery($sql);
89                }
90
91                $this->saved();
92               
93                // reset input field
94                $this->input = '';
95                $this->body = '';
96               
97                // show success message
98                WCF::getTPL()->assign(array(
99                        'success' => 'add',
100                        'emails' => $this->emails
101                ));
102        }
103
104        /**
105         * @see Page::readData()
106         */
107        public function readData() {
108                parent::readData();
109               
110                $sql = "SELECT          email
111                        FROM            wcf".WCF_N."_buddyloo_invitation
112                        WHERE           userID = ".WCF::getUser()->userID;
113                $result = WCF::getDB()->sendQuery($sql);
114                while ($row = WCF::getDB()->fetchArray($result)) {
115                        $this->emails[] = $row['email'];
116                }
117        }
118       
119        /**
120         * @see Page::assignVariables()
121         */
122        public function assignVariables() {
123                parent::assignVariables();
124               
125                WCF::getTPL()->assign(array(
126                        'input' => $this->input,
127                        'emails' => $this->emails
128                ));
129        }
130
131        /**
132         * @see Page::show()
133         */
134        public function show() {
135                if (!WCF::getUser()->userID) {
136                        require_once(WCF_DIR.'lib/system/exception/PermissionDeniedException.class.php');
137                        throw new PermissionDeniedException();
138                }
139               
140                // set active tab
141                UserCPMenu::getInstance()->setActiveMenuItem('wcf.user.usercp.menu.link.management.invitation');
142               
143                // show form
144                parent::show();
145        }
146       
147        /**
148         * Checks the given emails.
149         */
150        protected function validateEmails() {
151                // explode multiple usernames to an array
152                $emailArray = explode(',', $this->input);
153                $error = array();
154                $in = array();
155               
156                // loop through recipients and check if the email is valid
157                foreach ($emailArray as $email) {
158                        $email = StringUtil::trim($email);
159                        $in[] = "'$email'";
160                        if (empty($email)) continue;
161                       
162                        if (UserRegistrationUtil::isValidEmail($email)) {
163                                $this->inputArray[] = $email;
164                        } else {
165                                $error[] = array('type' => 'invalid', 'email' => $email);
166                        }
167                }
168               
169                // search for already registered users
170                $sql = "SELECT          email
171                        FROM            wcf".WCF_N."_user
172                        WHERE           email IN (".implode(',',$in).")";
173                $result = WCF::getDB()->sendQuery($sql);
174                while ($row = WCF::getDB()->fetchArray($result)) {
175                        $error[] = array('type' => 'duplicate', 'email' => $row['email']);
176                }
177               
178                if (count($error)) {
179                        throw new UserInputException('usernames', $error);
180                }
181        }
182       
183        /**
184         * Sends the email notification for recipients.
185         */
186        protected function sendNotification() {
187                require_once(WCF_DIR.'lib/data/mail/Mail.class.php');
188               
189                // send notifications
190                foreach ($this->inputArray as $recipient) {
191                        // send mail
192                        $subjectData = array(
193                                '$author' => WCF::getUser()->username,
194                                'PAGE_TITLE' => PAGE_TITLE
195                        );
196                        $messageData = array(
197                                'PAGE_TITLE' => PAGE_TITLE,
198                                '$recipient' => $recipient->username,
199                                '$author' => WCF::getUser()->username,
200                                '$pageurl' => FileUtil::addTrailingSlash(PAGE_URL),
201                                '$notice' => (REGISTER_INVITATIONS ? WCF->getLanguage()->get('wcf.buddyloo.invitation.register_invitation') : ""),
202                                '$body' => $this->body
203                        );
204
205                        $mail = new Mail(array($recipient->email), WCF->getLanguage()->get('wcf.buddyloo.invitation.subject', $subjectData), WCF->getLanguage()->get('wcf.buddyloo.invitation.body', $messageData));
206                        $mail->send();
207                }
208        }
209}
210?>
Note: See TracBrowser for help on using the browser.