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

Revision 320, 5.5 kB (checked in by d0nut, 5 years ago)

later fixes

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