root/buddyloo/files/lib/system/event/listener/BuddylooRegisterFormListener.class.php @ 323

Revision 323, 2.7 kB (checked in by d0nut, 5 years ago)

buddyloo can do caching

Line 
1<?php
2// WCF include
3require_once(WCF_DIR.'lib/system/event/EventListener.class.php');
4
5/**
6 * checks if the friend is invited
7 *
8 * @author      Torben Brodt
9 * @package     de.easy-coding.wcf.buddyloo
10 * @license     GNU General Public License <http://opensource.org/licenses/gpl-3.0.html>
11 */
12class BuddylooRegisterFormListener implements EventListener {
13        protected $eventObj;
14        protected $className;
15       
16        //data
17        protected $friends = array();
18
19        /**
20         * @see EventListener::execute()
21         */
22        public function execute($eventObj, $className, $eventName) {   
23                $this->eventObj = $eventObj;
24                $this->className = $className;
25
26                switch ($eventName) {
27                        case 'assignVariables':
28                                if(!REGISTER_INVITATIONS) return;
29                                $this->assignVariables();
30                                break;
31                        case 'validate':
32                                if(!REGISTER_INVITATIONS) return;
33                                $this->fetchInvitations();
34                                $this->validate();
35                                break;
36                        case 'saved':
37                                $this->fetchInvitations();
38                                $this->saved();
39                                break;
40                }
41        }
42       
43        /**
44         * show info... registration just for invited users
45         */
46        protected function assignVariables() {
47                WCF::getTPL()->append('userMessages', '<p class="info">'.WCF::getLanguage()->get('wcf.buddyloo.invitation.register.description').'</p>');
48        }
49       
50        /**
51         * fetchs invitations
52         */
53        protected function fetchInvitations() {
54                $email = $this->eventObj->email;
55                $sql = "SELECT          userID
56                        FROM            wcf".WCF_N."_buddyloo_invitation
57                        WHERE           email = '".escapeString($this->eventObj->email)."'";
58                $result = WCF::getDB()->sendQuery($sql);
59                while ($row = WCF::getDB()->fetchArray($result)) {
60                        $this->friends[] = $row['userID'];
61                }
62        }
63       
64        /**
65         * check if the user had an invitation and add him to friendlist
66         */
67        protected function validate() {
68                if(count($this->friends) == 0) {
69                        throw new UserInputException('email', 'notValid');
70                }
71        }
72       
73        /**
74         * check if the user had an invitation and add him to friendlist
75         */
76        protected function saved() {
77                $inserts = '';
78                foreach($this->friends as $friend)  {
79                        if (!empty($inserts)) $inserts .= ',';
80                        $inserts .= "(".$friend.", ".$this->eventObj->user->userID.")";
81                }
82               
83                if(!empty($inserts)) {
84                        $sql = "INSERT IGNORE INTO      wcf".WCF_N."_buddyloo
85                                                        (userID, whiteUserID)
86                                VALUES                  ".$inserts;
87                        WCF::getDB()->sendQuery($sql);
88               
89                        $sql = "DELETE FROM             wcf".WCF_N."_buddyloo_invitation
90                                WHERE                   email = '".escapeString($this->eventObj->user->email)."'; ";
91                        WCF::getDB()->sendQuery($sql);
92                       
93                        $this->updateCache();
94                }
95        }
96       
97               
98
99        /**
100         * updates cache
101         */
102        protected function updateCache() {
103                // clear cache
104                WCF::getCache()->clear(WBB_DIR.'cache', 'cache.buddyloo.*.php');
105
106                // add resource
107                WCF::getCache()->addResource('buddyloo.whitelist', 
108                        WCF_DIR.'cache/cache.buddyloo.whitelist.php',
109                        WCF_DIR.'lib/system/cache/CacheBuilderBuddylooWhiteList.class.php');
110        }
111}
112?>
Note: See TracBrowser for help on using the browser.