forked from bitweaver/geoserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geoserver_lib.php
287 lines (228 loc) · 9.02 KB
/
geoserver_lib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<?php
/* Utilities for use in importing geoserver tilelayers into the bitweaver database
* used in geoserver/admin/csv_tilelayers_import.php
*/
function geoserverGetXML( $pPath ){
// create a new cURL resource
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $pPath);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
global $user_cookie_site;
$strCookie = $user_cookie_site . '=' . $_COOKIE[$user_cookie_site] . '; path=/';
curl_setopt($ch, CURLOPT_COOKIE, $strCookie);
$xmlFile = curl_exec($ch);
curl_close($ch);
$xml = new DOMDocument();
$xml->loadXML( $xmlFile );
return $xml;
}
function geoserverGetTilelayer( &$pParamHash ){
global $gBitSystem;
$ret = NULL;
$bindVars = array(); $selectSql = ''; $joinSql = ''; $whereSql = '';
// if a name is passed in we look up the tilelayer by name
if( !empty( $pParamHash['tiles_name'] ) ){
// make sure the name is not too long incase some dumbo uploads a layer with too long a name
array_push( $bindVars, substr( $pParamHash['tiles_name'], 0, 64 ) );
$whereSql .= " gtl.`tiles_name` = ?";
// otherwise we get the layer by id and join to get its meta data also
}else{
array_push( $bindVars, $pParamHash['tilelayer_id'] );
$whereSql .= " gtl.`tilelayer_id` = ? ";
}
$selectSql .= ", gtm.theme_id, gtm.datakey ";
$joinSql .= " LEFT JOIN `".BIT_DB_PREFIX."geoserver_tilelayers_meta` gtm ON( gtm.`tilelayer_id` = gtl.`tilelayer_id` ) ";
$query = "SELECT gtl.* $selectSql
FROM `".BIT_DB_PREFIX."gmaps_tilelayers` gtl
$joinSql
WHERE $whereSql";
$result = $gBitSystem->mDb->query( $query, $bindVars );
if( $result && $result->numRows() ) {
$ret = $result->fields;
}
return $ret;
}
function geoserverGetTilelayerList( &$pListHash ){
global $gBitSystem;
require_once( LIBERTY_PKG_PATH."LibertyContent.php" );
$ret = array();
$bindVars = array(); $selectSql = ''; $joinSql = ''; $whereSql = '';
if( empty( $pListHash['sort_mode'] )) {
// $pListHash['sort_mode'] = array( 'gtl.`tiles_name_asc`' );
$pListHash['sort_mode'] = array( 'gtt.`theme_title_asc`', 'gtl.`tiles_name_asc`' );
}
@LibertyContent::prepGetList( $pListHash );
$joinSql .= " INNER JOIN `".BIT_DB_PREFIX."geoserver_tilelayers_meta` gtm ON( gtm.`tilelayer_id` = gtl.`tilelayer_id` ) "
." LEFT JOIN `".BIT_DB_PREFIX."geoserver_tilelayers_themes` gtt ON( gtt.`theme_id` = gtm.`theme_id` ) ";
$selectSql .= ", gtm.*, gtt.theme_title";
if( @BitBase::verifyId( $pListHash['theme_id'] )) {
$whereSql = " WHERE gtm.`theme_id` = ? ";
$bindVars[] = $pListHash['theme_id'];
}
$sql = "SELECT gtl.* $selectSql
FROM `".BIT_DB_PREFIX."gmaps_tilelayers` gtl $joinSql
$whereSql
ORDER BY ".$gBitSystem->mDb->convertSortmode( $pListHash['sort_mode'] );
$result = $gBitSystem->mDb->query( $sql, $bindVars, $pListHash['max_records'], $pListHash['offset'] );
while( $aux = $result->fetchRow() ) {
$ret[$aux['tilelayer_id']] = $aux;
}
$pListHash['cant'] = $gBitSystem->mDb->getOne( "SELECT COUNT( gtl.`tilelayer_id` ) FROM `".BIT_DB_PREFIX."gmaps_tilelayers` gtl $joinSql $whereSql", $bindVars );
@LibertyContent::postGetList( $pListHash );
return $ret;
}
function geoserverGetTilelayerThemes( &$pParamHash ){
global $gBitSystem;
$selectSql = "";
if( !empty( $pParamHash['require_match'] ) ){
$selectSql .= " INNER JOIN `".BIT_DB_PREFIX."geoserver_tilelayers_meta` gtm ON( gtm.`theme_id` = gtt.`theme_id` ) ";
}
return( $gBitSystem->mDb->getAssoc( "
SELECT gtt.`theme_id`, gtt.`theme_title`
FROM `".BIT_DB_PREFIX."geoserver_tilelayers_themes` gtt
$selectSql
ORDER BY ".$gBitSystem->mDb->convertSortmode( 'theme_title_asc' )
));
}
function geoserverVerifyTilelayerMetaData( &$pParamHash ){
$pParamHash['meta_store'] = array();
if( isset( $pParamHash['tilelayer_id'] ) ) {
$pParamHash['meta_store']['tilelayer_id'] = $pParamHash['tilelayer_id'];
}
if( isset( $pParamHash['theme_id'] ) ) {
$pParamHash['meta_store']['theme_id'] = $pParamHash['theme_id'];
}
if( isset( $pParamHash['datakey'] ) ) {
$pParamHash['meta_store']['datakey'] = $pParamHash['datakey'];
}
return $pParamHash['meta_store'];
}
function geoserverStoreTilelayerMetaData( &$pParamHash ){
global $gBitSystem;
$ret = FALSE;
if( @BitBase::verifyId( $pParamHash['tilelayer_id'] ) && geoserverVerifyTilelayerMetaData( $pParamHash ) ) {
$gBitSystem->mDb->StartTrans();
// If metadata for this tilelayer was stored before delete it then store it new
$query = "DELETE FROM `".BIT_DB_PREFIX."geoserver_tilelayers_meta` WHERE `tilelayer_id` =?";
$result = $gBitSystem->mDb->query( $query, array( $pParamHash['tilelayer_id'] ) );
$gBitSystem->mDb->associateInsert( BIT_DB_PREFIX."geoserver_tilelayers_meta", $pParamHash['meta_store'] );
$gBitSystem->mDb->CompleteTrans();
$ret = TRUE;
}
return $ret;
}
function geoserverRewriteTilelayerCache() {
global $gBitSmarty;
$path = TEMP_PKG_PATH.GEOSERVER_PKG_NAME.'/templates';
// if the cache folder doesn't exist yet, create it
if( !is_dir( $path ) ) {
mkdir_p( $path );
}
if( is_dir( $path ) ) {
$handle = opendir( $path );
while( false!== ( $cache_file = readdir( $handle ) ) ) {
if( $cache_file != "." && $cache_file != ".." ) {
unlink( $path.'/'.$cache_file );
}
}
// get the tilelayers and rewrite the cache
$list = array( 'max_records' => 999999 );
$tilelayers = geoserverGetTilelayerList( $list );
$gBitSmarty->assign( 'geoserverTilelayers', $tilelayers );
$cacheTpls = array(
// 'mapdata.js.tpl' => $gBitSmarty->fetch( GEOSERVER_PKG_PATH.'templates/mapdata.js.tpl' ),
// 'tilelayers_menu_inc.tpl' => $gBitSmarty->fetch( GEOSERVER_PKG_PATH.'templates/tilelayers_menu_inc.tpl' ),
// 'edit_map_inc.tpl' => $gBitSmarty->fetch( GEOSERVER_PKG_PATH.'templates/edit_map_inc.tpl' ),
);
foreach( $cacheTpls as $cacheFile => $cacheString ){
$h = fopen( $path.'/'.$cacheFile, 'w' );
if( isset( $h ) ) {
fwrite( $h, $cacheString );
fclose( $h );
} else {
// $this->mErrors['write_module_cache'] = tra( "Unable to write to" ).': '.realpath( $cache_file );
}
}
} else {
// $this->mErrors['chache_rewrite'] = tra( "The cache directory for geoserver doesn't exist." );
}
// return( count( $this->mErrors ) == 0 );
}
/* make theme data is safe to store
*/
function geoserverVerifyThemes( &$pParamHash ) {
global $gBitUser, $gBitSystem;
$pParamHash['themes_store'] = array();
//need to break up this string
$themeMixed = isset($pParamHash['themes']) ? $pParamHash['themes'] : NULL;
if( !empty( $themeMixed )){
if (!is_array( $themeMixed ) && !is_numeric( $themeMixed ) ){
$themeIds = explode( ",", $themeMixed );
}else if ( is_array( $themeMixed ) ) {
$themeIds = $themeMixed;
}else if ( is_numeric( $themeMixed ) ) {
$themeIds = array( $themeMixed );
}
foreach( $themeIds as $value ) {
$value = trim($value);
/* Ignore empty themes like a trailing , generate */
if( !empty($value) ) {
// We always trim tags
$value = trim($value);
if ( !empty($value) ) {
array_push( $pParamHash['themes_store'], array(
'theme_title' => $value,
));
}
else {
// $this->mErrors[$value] = "Invalid theme.";
}
}
}
}
// return ( count( $this->mErrors ) == 0 );
return TRUE;
}
function geoserverStoreThemes( &$pParamHash ){
global $gBitSystem;
if( geoserverVerifyThemes( $pParamHash ) ) {
$themetable = BIT_DB_PREFIX."geoserver_tilelayers_themes";
$query = "SELECT gtt.theme_id
FROM `".BIT_DB_PREFIX."geoserver_tilelayers_themes` gtt
WHERE gtt.`theme_title` = ?";
foreach ( $pParamHash['themes_store'] as $hash) {
// if it doesnt exist already then store it
if( !$gBitSystem->mDb->getOne( $query, $hash ) ){
$gBitSystem->mDb->StartTrans();
$hash['theme_id'] = $gBitSystem->mDb->GenID( 'geoserver_tl_theme_id_seq' );
$gBitSystem->mDb->associateInsert( $themetable, $hash );
$gBitSystem->mDb->CompleteTrans();
}
}
}
}
/********* SERVICE FUNCTIONS *********/
/*
* universal service func
* for both content_display and content_edit
*/
function geoserver_content_gettilelayers( &$pObject ) {
global $gBitSystem, $gBitSmarty, $gBitUser;
if ( $gBitSystem->isPackageActive( 'gmap' ) && $gBitSystem->isPackageActive( 'geoserver' ) && $pObject->getContentType() == 'bitgmap' && $pObject->hasViewPermission() ){
$list = array( 'max_records' => 999999 );
$tilelayers = geoserverGetTilelayerList( $list );
$gBitSmarty->assign( 'geoserverTilelayers', $tilelayers );
if( $tilelayerPref = $pObject->getPreference( 'geoserver_tilelayer_id' ) ){
$gBitSmarty->assign( 'tilelayerPref', $tilelayerPref );
$gBitSmarty->assign( 'tilelayerPrefName', $tilelayers[$tilelayerPref]['tiles_name'] );
}
}
}
function geoserver_content_store( &$pObject ) {
global $gBitSystem, $gBitSmarty, $gBitUser;
if ( $gBitSystem->isPackageActive( 'gmap' ) && $gBitSystem->isPackageActive( 'geoserver' ) && $pObject->getContentType() == 'bitgmap' && $pObject->hasUpdatePermission() ){
$pObject->storePreference( 'geoserver_tilelayer_id', !empty( $_REQUEST['geoserver_tilelayer_id'] ) ? $_REQUEST['geoserver_tilelayer_id'] : NULL );
}
}
?>