| 1 | <?php |
|---|
| 2 | // wcf imports |
|---|
| 3 | require_once(WCF_DIR.'lib/system/event/EventListener.class.php'); |
|---|
| 4 | |
|---|
| 5 | // wbb imports |
|---|
| 6 | require_once(WBB_DIR.'lib/data/thread/ViewableThreadTagging.class.php'); |
|---|
| 7 | require_once(WBB_DIR.'lib/data/board/Board.class.php'); |
|---|
| 8 | |
|---|
| 9 | // tagging imports |
|---|
| 10 | require_once(WCF_DIR.'lib/util/TaggingReloadedUtil.class.php'); |
|---|
| 11 | require_once(WBB_DIR.'lib/data/board/BoardListTaggingReloaded.class.php'); |
|---|
| 12 | |
|---|
| 13 | /** |
|---|
| 14 | * Displays the tags for threads/boards |
|---|
| 15 | * |
|---|
| 16 | * @author Torben Brodt |
|---|
| 17 | * @package de.easy-coding.wbb.taggingreloaded |
|---|
| 18 | * @license GNU General Public License <http://opensource.org/licenses/gpl-3.0.html> |
|---|
| 19 | */ |
|---|
| 20 | class TaggingReloadedWBBPageListener implements EventListener { |
|---|
| 21 | protected $postID=0, $threadID=0, $boards=array(), $tag; // params |
|---|
| 22 | |
|---|
| 23 | // data |
|---|
| 24 | protected $tags = array(); |
|---|
| 25 | protected $threadList = array(); |
|---|
| 26 | |
|---|
| 27 | protected $eventObj; |
|---|
| 28 | protected $className; |
|---|
| 29 | |
|---|
| 30 | /** |
|---|
| 31 | * @see EventListener::execute() |
|---|
| 32 | */ |
|---|
| 33 | public function execute($eventObj, $className, $eventName) { |
|---|
| 34 | $this->eventObj = $eventObj; |
|---|
| 35 | $this->className = $className; |
|---|
| 36 | |
|---|
| 37 | switch ($eventName) { |
|---|
| 38 | case 'readData': |
|---|
| 39 | $this->readData(); |
|---|
| 40 | break; |
|---|
| 41 | case 'assignVariables': |
|---|
| 42 | $this->assignVariables(); |
|---|
| 43 | break; |
|---|
| 44 | } |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | /** |
|---|
| 48 | * reads similar tags |
|---|
| 49 | */ |
|---|
| 50 | protected function queryTagsByTag() { |
|---|
| 51 | // order by weight and cut |
|---|
| 52 | $sql = "SELECT wcf2.tag, |
|---|
| 53 | SUM(wcf2.weight) AS weight |
|---|
| 54 | FROM wcf".WCF_N."_taggingreloaded wcf |
|---|
| 55 | NATURAL JOIN wbb".WBB_N."_taggingreloaded wbb |
|---|
| 56 | NATURAL JOIN wbb".WBB_N."_post p |
|---|
| 57 | JOIN wbb".WBB_N."_taggingreloaded wbb2 |
|---|
| 58 | ON p.postID = wbb2.postID |
|---|
| 59 | JOIN wcf".WCF_N."_taggingreloaded wcf2 |
|---|
| 60 | ON wbb2.taggingID = wcf2.taggingID |
|---|
| 61 | WHERE wcf.tag = '".escapeString($this->tag)."' |
|---|
| 62 | GROUP BY wcf2.tag |
|---|
| 63 | ORDER BY weight DESC |
|---|
| 64 | LIMIT 50"; |
|---|
| 65 | |
|---|
| 66 | return $sql; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | /** |
|---|
| 70 | * reads by board |
|---|
| 71 | */ |
|---|
| 72 | protected function queryTagsByBoard() { |
|---|
| 73 | // order by weight and cut |
|---|
| 74 | $sql = "SELECT tag, |
|---|
| 75 | SUM(weight) AS weight |
|---|
| 76 | FROM wcf".WCF_N."_taggingreloaded wcf |
|---|
| 77 | NATURAL JOIN wbb".WBB_N."_taggingreloaded wbb |
|---|
| 78 | NATURAL JOIN wbb".WBB_N."_post p |
|---|
| 79 | JOIN wbb".WBB_N."_thread t |
|---|
| 80 | ON p.threadID = t.threadID |
|---|
| 81 | WHERE t.boardID IN (".implode(',', $this->boards).") |
|---|
| 82 | GROUP BY tag |
|---|
| 83 | ORDER BY weight DESC |
|---|
| 84 | LIMIT 50"; |
|---|
| 85 | |
|---|
| 86 | return $sql; |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | /** |
|---|
| 90 | * reads (threaddata) by post |
|---|
| 91 | */ |
|---|
| 92 | protected function queryTagsByPost() { |
|---|
| 93 | // order by weight and cut |
|---|
| 94 | $sql = "SELECT tag, |
|---|
| 95 | SUM(weight) AS weight |
|---|
| 96 | FROM wcf".WCF_N."_taggingreloaded wcf |
|---|
| 97 | NATURAL JOIN wbb".WBB_N."_taggingreloaded wbb |
|---|
| 98 | NATURAL JOIN wbb".WBB_N."_post p |
|---|
| 99 | JOIN wbb".WBB_N."_post p2 |
|---|
| 100 | ON p.threadID = p2.threadID |
|---|
| 101 | WHERE p.postID = {$this->postID} |
|---|
| 102 | GROUP BY tag |
|---|
| 103 | ORDER BY weight DESC |
|---|
| 104 | LIMIT 50"; |
|---|
| 105 | // SUM( weight ) * IF(COUNT(tag), 1.2, 1 ) |
|---|
| 106 | |
|---|
| 107 | return $sql; |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | /** |
|---|
| 111 | * reads by thread |
|---|
| 112 | */ |
|---|
| 113 | protected function queryTagsByThread() { |
|---|
| 114 | // order by weight and cut |
|---|
| 115 | $sql = "SELECT tag, |
|---|
| 116 | SUM(weight) AS weight |
|---|
| 117 | FROM wcf".WCF_N."_taggingreloaded wcf |
|---|
| 118 | NATURAL JOIN wbb".WBB_N."_taggingreloaded wbb |
|---|
| 119 | NATURAL JOIN wbb".WBB_N."_post p |
|---|
| 120 | WHERE p.threadID = {$this->threadID} |
|---|
| 121 | GROUP BY tag |
|---|
| 122 | ORDER BY weight DESC |
|---|
| 123 | LIMIT 50"; |
|---|
| 124 | // SUM( weight ) * IF(COUNT(tag), 1.2, 1 ) |
|---|
| 125 | |
|---|
| 126 | return $sql; |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | /** |
|---|
| 130 | * reads threads by tag |
|---|
| 131 | */ |
|---|
| 132 | protected function readThreadsByTag() { |
|---|
| 133 | $sql = "SELECT t.*, |
|---|
| 134 | tag, |
|---|
| 135 | SUM(weight) AS weight |
|---|
| 136 | FROM wcf".WCF_N."_taggingreloaded wcf |
|---|
| 137 | NATURAL JOIN wbb".WBB_N."_taggingreloaded wbb |
|---|
| 138 | NATURAL JOIN wbb".WBB_N."_post p |
|---|
| 139 | JOIN wbb".WBB_N."_thread t |
|---|
| 140 | ON p.threadID = t.threadID |
|---|
| 141 | WHERE tag = '".escapeString($this->tag)."' |
|---|
| 142 | GROUP BY p.threadID |
|---|
| 143 | ORDER BY weight DESC |
|---|
| 144 | LIMIT 15"; |
|---|
| 145 | |
|---|
| 146 | $result = WCF::getDB()->sendQuery($sql); |
|---|
| 147 | while ($row = WCF::getDB()->fetchArray($result)) { |
|---|
| 148 | $row['permission'] = Board::getBoard($row['boardID'])->getPermission('canReadThread'); |
|---|
| 149 | $this->threadList[] = new ViewableThreadTagging(null, $row); |
|---|
| 150 | } |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | /** |
|---|
| 154 | * @see Page::readData() |
|---|
| 155 | */ |
|---|
| 156 | protected function readData () { |
|---|
| 157 | switch($this->className) { |
|---|
| 158 | case 'ThreadPage': |
|---|
| 159 | $this->threadID = $this->eventObj->threadID; |
|---|
| 160 | |
|---|
| 161 | $sql = $this->queryTagsByThread(); |
|---|
| 162 | break; |
|---|
| 163 | case 'BoardPage': |
|---|
| 164 | $boardID = $this->eventObj->boardID; |
|---|
| 165 | $boardList = new BoardListTaggingReloaded($boardID); |
|---|
| 166 | $boardList->renderBoards(); |
|---|
| 167 | $this->readSubBoards($boardList->getSubBoards()); |
|---|
| 168 | $this->boards[] = $boardID; |
|---|
| 169 | |
|---|
| 170 | $sql = $this->queryTagsByBoard(); |
|---|
| 171 | break; |
|---|
| 172 | case 'TaggingPage': |
|---|
| 173 | if(isset($_GET['tag'])) { |
|---|
| 174 | $this->tag = $_GET['tag']; |
|---|
| 175 | $check = true; |
|---|
| 176 | $this->readThreadsByTag(); |
|---|
| 177 | $sql = $this->queryTagsByTag(); |
|---|
| 178 | } |
|---|
| 179 | break; |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | // break |
|---|
| 183 | if(!isset($sql)) return; |
|---|
| 184 | |
|---|
| 185 | // order by tag |
|---|
| 186 | $sql = "SELECT tag,weight FROM ($sql) A ORDER BY tag ASC"; |
|---|
| 187 | |
|---|
| 188 | // query |
|---|
| 189 | $result = WCF::getDB()->sendQuery($sql); |
|---|
| 190 | while ($row = WCF::getDB()->fetchArray($result)) { |
|---|
| 191 | $this->tags[$row['tag']] = array( |
|---|
| 192 | 'weight'=> $row['weight'], |
|---|
| 193 | 'color'=> 0, |
|---|
| 194 | 'size'=> 0 |
|---|
| 195 | ); |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | $this->tags = TaggingReloadedUtil::beautify($this->tags); |
|---|
| 199 | |
|---|
| 200 | if(isset($check) && count($this->tags) == 0) { |
|---|
| 201 | require_once(WCF_DIR.'lib/system/exception/IllegalLinkException.class.php'); |
|---|
| 202 | throw new IllegalLinkException(); |
|---|
| 203 | } |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | /** |
|---|
| 207 | * fetch recursive all child boardIDs |
|---|
| 208 | * @param arr |
|---|
| 209 | */ |
|---|
| 210 | protected function readSubBoards($arr) { |
|---|
| 211 | foreach($arr as $boardID => $next) { |
|---|
| 212 | $this->boards[] = $boardID; |
|---|
| 213 | $this->readSubBoards($next); |
|---|
| 214 | } |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | /** |
|---|
| 218 | * @see Page::assignVariables() |
|---|
| 219 | */ |
|---|
| 220 | protected function assignVariables () { |
|---|
| 221 | WCF::getTPL()->assign('tags', $this->tags); |
|---|
| 222 | WCF::getTPL()->append('specialStyles', '<link rel="stylesheet" type="text/css" href="'.RELATIVE_WCF_DIR.'style/taggingreloaded.css" />'); |
|---|
| 223 | |
|---|
| 224 | if($this->threadID || $this->postID || count($this->boards)>0) { |
|---|
| 225 | WCF::getTPL()->append('additionalBoxes', WCF::getTPL()->fetch('taggingCloudContainer')); |
|---|
| 226 | } else if($this->tag) { |
|---|
| 227 | WCF::getTPL()->assign('threads', $this->threadList); |
|---|
| 228 | WCF::getTPL()->append('additionalTaggingContents', WCF::getTPL()->fetch('taggingCloud')); |
|---|
| 229 | WCF::getTPL()->append('additionalTaggingContents', WCF::getTPL()->fetch('threadListTagging')); |
|---|
| 230 | } |
|---|
| 231 | } |
|---|
| 232 | } |
|---|
| 233 | ?> |
|---|