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

Revision 270, 4.2 kB (checked in by d0nut, 5 years ago)
  • users can send board-invitations by mail
  • admin can decide to limit registrations to invited users
  • admin can decide not to show friendship connections
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 $inputArray = array();
18        public $emails = array();
19        public $templateName = 'buddylooInvitationEdit';
20       
21        /**
22         * @see Page::readParameters()
23         */
24        public function readParameters() {
25                parent::readParameters();
26               
27                if (isset($_GET['remove'])) {
28                        // delete invitation
29                        $sql = "DELETE FROM     wcf".WCF_N."_buddyloo_invitation
30                                WHERE           userID = ".WCF::getUser()->userID."
31                                                AND email = '".escapeString($_GET['remove'])."'";
32                        WCF::getDB()->sendQuery($sql);
33
34                        // show success message
35                        WCF::getTPL()->assign(array(
36                                'success' => 'remove',
37                                'emails' => $_GET['remove']
38                        ));
39                }
40                else if (isset($_GET['add'])) {
41                        $this->submit();
42                }
43        }
44       
45        /**
46         * @see Form::readFormParameters()
47         */
48        public function readFormParameters() {
49                parent::readFormParameters();
50               
51                if (isset($_POST['input'])) $this->input = StringUtil::trim($_POST['input']);
52        }
53       
54        /**
55         * @see Form::validate()
56         */
57        public function validate() {
58                parent::validate();
59               
60                if (count($this->inputArray) == 0) {
61                        if (empty($this->input)) {
62                                throw new UserInputException('input');
63                        }
64                       
65                        $this->validateEmails();
66                }
67        }
68       
69        /**
70         * @see Form::save()
71         */
72        public function save() {
73                parent::save();
74               
75                // save user
76                $inserts = '';
77                foreach ($this->inputArray as $email) {
78                        if (!empty($inserts)) $inserts .= ',';
79                        $inserts .= "(".WCF::getUser()->userID.", '".escapeString($email)."')";
80                }
81               
82                if (!empty($inserts)) {
83                        $sql = "INSERT IGNORE INTO      wcf".WCF_N."_buddyloo_invitation
84                                                        (userID, email)
85                                VALUES                  ".$inserts;
86                        WCF::getDB()->sendQuery($sql);
87                }
88
89                $this->saved();
90               
91                // reset input field
92                $this->input = '';
93               
94                // show success message
95                WCF::getTPL()->assign(array(
96                        'success' => 'add',
97                        'emails' => $this->emails
98                ));
99        }
100
101        /**
102         * @see Page::readData()
103         */
104        public function readData() {
105                parent::readData();
106               
107                $sql = "SELECT          email
108                        FROM            wcf".WCF_N."_buddyloo_invitation
109                        WHERE           userID = ".WCF::getUser()->userID;
110                $result = WCF::getDB()->sendQuery($sql);
111                while ($row = WCF::getDB()->fetchArray($result)) {
112                        $this->emails[] = $row['email'];
113                }
114        }
115       
116        /**
117         * @see Page::assignVariables()
118         */
119        public function assignVariables() {
120                parent::assignVariables();
121               
122                WCF::getTPL()->assign(array(
123                        'input' => $this->input,
124                        'emails' => $this->emails
125                ));
126        }
127
128        /**
129         * @see Page::show()
130         */
131        public function show() {
132                if (!WCF::getUser()->userID) {
133                        require_once(WCF_DIR.'lib/system/exception/PermissionDeniedException.class.php');
134                        throw new PermissionDeniedException();
135                }
136               
137                // set active tab
138                UserCPMenu::getInstance()->setActiveMenuItem('wcf.user.usercp.menu.link.management.invitation');
139               
140                // show form
141                parent::show();
142        }
143       
144        /**
145         * Checks the given emails.
146         */
147        protected function validateEmails() {
148                // explode multiple usernames to an array
149                $emailArray = explode(',', $this->input);
150                $error = array();
151                $in = array();
152               
153                // loop through recipients and check if the email is valid
154                foreach ($emailArray as $email) {
155                        $email = StringUtil::trim($email);
156                        $in[] = "'$email'";
157                        if (empty($email)) continue;
158                       
159                        if (UserRegistrationUtil::isValidEmail($email)) {
160                                $this->inputArray[] = $email;
161                        } else {
162                                $error[] = array('type' => 'invalid', 'email' => $email);
163                        }
164                }
165               
166                // search for already registered users
167                $sql = "SELECT          email
168                        FROM            wcf".WCF_N."_user
169                        WHERE           email IN (".implode(',',$in).")";
170                $result = WCF::getDB()->sendQuery($sql);
171                while ($row = WCF::getDB()->fetchArray($result)) {
172                        $error[] = array('type' => 'duplicate', 'email' => $row['email']);
173                }
174               
175                if (count($error)) {
176                        throw new UserInputException('usernames', $error);
177                }
178        }
179}
180?>
Note: See TracBrowser for help on using the browser.