| 1 | <?php |
|---|
| 2 | // wcf imports |
|---|
| 3 | require_once(WCF_DIR.'lib/page/AbstractPage.class.php'); |
|---|
| 4 | |
|---|
| 5 | // map imports |
|---|
| 6 | require_once(WCF_DIR.'lib/data/gmap/GmapCluster.class.php'); |
|---|
| 7 | |
|---|
| 8 | /** |
|---|
| 9 | * Returns the AJAX Content for the Gooogle Map |
|---|
| 10 | * |
|---|
| 11 | * @package de.gmap.wcf.data.page |
|---|
| 12 | * @author Torben Brodt |
|---|
| 13 | * @license GNU General Public License <http://opensource.org/licenses/gpl-3.0.html> |
|---|
| 14 | */ |
|---|
| 15 | class MapAjaxPage extends AbstractPage { |
|---|
| 16 | public $action = ''; |
|---|
| 17 | protected $zoom = 0; |
|---|
| 18 | protected $distance = 35; |
|---|
| 19 | protected $bounds= array(); |
|---|
| 20 | |
|---|
| 21 | protected $datapoints = array(); |
|---|
| 22 | |
|---|
| 23 | /** |
|---|
| 24 | * @see Page::readData() |
|---|
| 25 | */ |
|---|
| 26 | public function readParameters() { |
|---|
| 27 | parent::readParameters(); |
|---|
| 28 | |
|---|
| 29 | $this->zoom = max(min(21, isset($_GET['zoom']) ? $_GET['zoom'] : 0), 0); |
|---|
| 30 | |
|---|
| 31 | // ((50.08930948264218, 10.298652648925781), (50.14434619645057, 10.506362915039062)) |
|---|
| 32 | if(isset($_GET['bounds'])) { |
|---|
| 33 | if(preg_match('/^\(\((-?\d+\.?\d*), (-?\d+\.?\d*)\), \((-?\d+\.?\d*), (-?\d+\.?\d*)\)\)$/', $_GET['bounds'], $match)) { |
|---|
| 34 | $this->bounds = array( |
|---|
| 35 | array( |
|---|
| 36 | 'lat' => $match[1], |
|---|
| 37 | 'lon' => $match[2] |
|---|
| 38 | ), |
|---|
| 39 | array( |
|---|
| 40 | 'lat' => $match[3], |
|---|
| 41 | 'lon' => $match[4] |
|---|
| 42 | ), |
|---|
| 43 | ); |
|---|
| 44 | } |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | // just get bounds |
|---|
| 48 | $this->action = $_GET['action']; |
|---|
| 49 | |
|---|
| 50 | // load content |
|---|
| 51 | $this->content = isset($_GET['content']) && $_GET['content']; |
|---|
| 52 | |
|---|
| 53 | // coordinates |
|---|
| 54 | $this->lat = isset($_GET['lat']) ? floatval($_GET['lat']) : 0; |
|---|
| 55 | $this->lon = isset($_GET['lon']) ? floatval($_GET['lon']) : 0; |
|---|
| 56 | |
|---|
| 57 | // pick action |
|---|
| 58 | $this->idx = isset($_GET['idx']) ? intval($_GET['idx']) : 0; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | /** |
|---|
| 62 | * @see Page::readData() |
|---|
| 63 | */ |
|---|
| 64 | public function readData() { |
|---|
| 65 | parent::readData(); |
|---|
| 66 | |
|---|
| 67 | $markers = array(); |
|---|
| 68 | |
|---|
| 69 | $sql = 'SELECT '.($this->action == 'pick' ? 'userID AS id,' : '').' |
|---|
| 70 | X(pt) AS lon, |
|---|
| 71 | Y(pt) AS lat |
|---|
| 72 | FROM wcf'.WCF_N.'_gmap_user |
|---|
| 73 | WHERE 1'; |
|---|
| 74 | |
|---|
| 75 | if($this->bounds) { |
|---|
| 76 | $sql .= ' AND X(pt) BETWEEN '.floatval($this->bounds[0]['lon']).' AND '.floatval($this->bounds[1]['lon']).' '; |
|---|
| 77 | $sql .= ' AND Y(pt) BETWEEN '.floatval($this->bounds[0]['lat']).' AND '.floatval($this->bounds[1]['lat']).' '; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | if(!$this->action == 'initialize') { |
|---|
| 81 | $sql = 'SELECT AVG(lon) AS lon, |
|---|
| 82 | AVG(lat) AS lat |
|---|
| 83 | FROM ( |
|---|
| 84 | '.$sql.' |
|---|
| 85 | ) x'; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | $result = WCF::getDB()->sendQuery($sql); |
|---|
| 89 | while ($row = WCF::getDB()->fetchArray($result)) { |
|---|
| 90 | $markers[] = $row; |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | $cluster = new GmapCluster($this->distance, $this->zoom); |
|---|
| 94 | if($this->action == 'pick') { |
|---|
| 95 | $ids = $cluster->getIDs($markers, $this->idx); |
|---|
| 96 | if($ids === null) { |
|---|
| 97 | $this->datapoints = array(); |
|---|
| 98 | $this->datapoints[] = array( |
|---|
| 99 | 0, |
|---|
| 100 | 'data n/a', |
|---|
| 101 | '' |
|---|
| 102 | ); |
|---|
| 103 | } else { |
|---|
| 104 | $this->datapoints = $this->getUsers($ids); |
|---|
| 105 | } |
|---|
| 106 | } else { |
|---|
| 107 | $this->datapoints = $cluster->getMarkers($markers); |
|---|
| 108 | } |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | /** |
|---|
| 112 | * Returns a list of users. |
|---|
| 113 | * |
|---|
| 114 | * @param string $userIDs |
|---|
| 115 | * @return array<UserProfile> users |
|---|
| 116 | */ |
|---|
| 117 | public function getUsers(array $userIDs) { |
|---|
| 118 | require_once(WCF_DIR.'lib/data/user/UserProfile.class.php'); |
|---|
| 119 | |
|---|
| 120 | $users = array(); |
|---|
| 121 | $sql = "SELECT avatar_table.*, |
|---|
| 122 | user_table.* |
|---|
| 123 | FROM wcf".WCF_N."_user user_table |
|---|
| 124 | LEFT JOIN wcf".WCF_N."_avatar avatar_table |
|---|
| 125 | ON (avatar_table.avatarID = user_table.avatarID) |
|---|
| 126 | WHERE user_table.userID IN (".implode(',', $userIDs).") |
|---|
| 127 | ORDER BY username"; |
|---|
| 128 | $result = WCF::getDB()->sendQuery($sql); |
|---|
| 129 | while ($row = WCF::getDB()->fetchArray($result)) { |
|---|
| 130 | |
|---|
| 131 | $avatar = ''; |
|---|
| 132 | $user = new UserProfile(null, $row); |
|---|
| 133 | if($user->getAvatar()) { |
|---|
| 134 | $user->getAvatar()->setMaxSize(24, 24); |
|---|
| 135 | $avatar = $user->getAvatar()->getURL(); |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | $users[] = array( |
|---|
| 139 | intval($user->userID), |
|---|
| 140 | $user->username, |
|---|
| 141 | $avatar |
|---|
| 142 | ); |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | return $users; |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | /** |
|---|
| 149 | * @see Page::show() |
|---|
| 150 | */ |
|---|
| 151 | public function show() { |
|---|
| 152 | parent::show(); |
|---|
| 153 | |
|---|
| 154 | // send header for corrent charset |
|---|
| 155 | @header('Content-Type: application/json; charset='.CHARSET); |
|---|
| 156 | echo json_encode($this->datapoints); |
|---|
| 157 | } |
|---|
| 158 | } |
|---|
| 159 | ?> |
|---|