/[drupal]/contributions/modules/carto/carto.module
ViewVC logotype

Diff of /contributions/modules/carto/carto.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.18, Thu Oct 5 20:13:05 2006 UTC revision 1.19, Sat Oct 7 22:12:05 2006 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: carto.module,v 1.17 2006/09/29 14:34:22 openwereld Exp $  // $Id: carto.module,v 1.18 2006/10/05 20:13:05 openwereld Exp $
3    
4  define( 'DEFAULT_WMS_SEARCH_FIELDS', 'name,title,abstract' );  define( 'DEFAULT_WMS_SEARCH_FIELDS', 'name,title,abstract' );
5  define( 'DEFAULT_CARTO_WMS_TEASER_MAP', 1 );  define( 'DEFAULT_CARTO_WMS_TEASER_MAP', 1 );
# Line 66  function carto_help( $section ) Line 66  function carto_help( $section )
66      case 'node/add#carto-wfs':      case 'node/add#carto-wfs':
67          $output = t('Add a Opengis web feature service.');          $output = t('Add a Opengis web feature service.');
68          break;          break;
69        }
70    
71      default:      if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == 'layers') {
72  //print 'carto_help:' . $section . '<br />';          return t('Select the layers to be visible in the wms node. At leas one layer is required, and the selected layers must have a SRS in common.');
         return;  
73      }      }
74    
75      return $output;      return $output;
# Line 108  function _carto_is_bootstrap( ) Line 108  function _carto_is_bootstrap( )
108  /**  /**
109   * Implementation of hook_access().   * Implementation of hook_access().
110   */   */
111  function carto_access( $op, $node )  /*function carto_access( $op, $node )
112  {  {
113      global $user;      global $user;
114    
115      //print 'carto_access: ' . $op . '<br>';      drupal_set_message( 'carto_access: ' . $op );
   
116      if( $op == 'create' )      if( $op == 'create' )
117      {      {
118          return user_access( 'create wms nodes' ) && $user->uid;          return user_access( 'create wms nodes' ) && $user->uid;
# Line 121  function carto_access( $op, $node ) Line 120  function carto_access( $op, $node )
120    
121      if( $op == 'update' || $op == 'delete' )      if( $op == 'update' || $op == 'delete' )
122      {      {
123          if( user_access( 'select wms layers' ) && ( $user->uid == $node->uid ))          if( user_access( 'create wms nodes' ) && ( $user->uid == $node->uid ))
124          {          {
125              return TRUE;              return TRUE;
126          }          }
127      }      }
128    }*/
129    
130    function carto_wms_access( $op, $node )
131    {
132        global $user;
133    
134        drupal_set_message( 'carto_wms_access: ' . $op );
135        switch( $op )
136        {
137            case 'view':
138                return user_access( 'view wms nodes' );
139            case 'create':
140                return user_access( 'create wms nodes' ) && $user->uid;
141            case 'delete': // own nodes only!
142                return user_access( 'create wms nodes' ) && $user->uid == $node->uid;
143        }
144    
145        drupal_set_message( 'carto_wms_access: ' . var_export(func_get_args(),true) );
146  }  }
147    
148  /**  /**
# Line 137  function carto_perm( ) Line 154  function carto_perm( )
154      return array(      return array(
155          'administer carto'          'administer carto'
156          , 'create wms nodes' // create or edit wms nodes          , 'create wms nodes' // create or edit wms nodes
157          , 'access wms nodes' // view wms nodes          , 'view wms nodes' // view wms nodes
158            , 'query wms nodes' // query wms nodes
159          , 'select wms layers' // select wms layers (select layers)          , 'select wms layers' // select wms layers (select layers)
160  //        , 'create wfs nodes'  //        , 'create wfs nodes'
161  //        , 'access wfs nodes'  //        , 'access wfs nodes'
# Line 183  function carto_menu( $may_cache ) Line 201  function carto_menu( $may_cache )
201          $items[] = array( 'path' => 'carto/wms'          $items[] = array( 'path' => 'carto/wms'
202                  , 'title' => t('wms')                  , 'title' => t('wms')
203                  , 'callback' => 'carto_wms_all'                  , 'callback' => 'carto_wms_all'
204                  , 'access' => user_access('access wms nodes')                  , 'access' => user_access('view wms nodes')
205                  , 'type' => MENU_NORMAL_ITEM                  , 'type' => MENU_NORMAL_ITEM
206                  );                  );
207    
208            $items[] = array( 'path' => 'carto/wms/node'
209                    //, 'callback' => 'carto_wms_query'
210                    , 'access' => user_access('query wms nodes')
211                    , 'type' => MENU_CALLBACK
212                    );
213    
214          $items[] = array( 'path' => 'carto/wms/user/' . $user->uid          $items[] = array( 'path' => 'carto/wms/user/' . $user->uid
215                  , 'title' => t('my wms')                  , 'title' => t('my wms')
216                  , 'access' => $user->uid > 0 && user_access('access wms nodes')                  , 'access' => $user->uid > 0 && user_access('view wms nodes')
217                  , 'type' => MENU_NORMAL_ITEM                  , 'type' => MENU_NORMAL_ITEM
218                  );                  );
219    
# Line 236  function carto_menu( $may_cache ) Line 261  function carto_menu( $may_cache )
261              {              {
262                  $items[] = array( 'path' => 'node/' . arg(1) . '/layers'                  $items[] = array( 'path' => 'node/' . arg(1) . '/layers'
263                      , 'title' => t('select layers')                      , 'title' => t('select layers')
264                      , 'callback' => 'drupal_get_form', 'callback arguments' => array('carto_wms_select_layers', intval(arg(1)))                      , 'callback' => 'drupal_get_form'
265                        , 'callback arguments' => array('carto_wms_select_layers', intval(arg(1)))
266                      , 'access' => user_access('select wms layers')                      , 'access' => user_access('select wms layers')
267                      , 'type' => MENU_LOCAL_TASK                      , 'type' => MENU_LOCAL_TASK
268                      );                      );
269              }              }
270          }          }
271          //drupal_add_css(drupal_get_path('module', 'system') .'/system.css', 'core');          drupal_add_css(drupal_get_path('module', 'system') .'/system.css', 'core');
272      }      }
273      return $items;      return $items;
274  }  }
# Line 1184  function carto_wms_form( &$node ) Line 1210  function carto_wms_form( &$node )
1210          , '#size' => 100          , '#size' => 100
1211          , '#maxlength' => 1000          , '#maxlength' => 1000
1212          , '#required' => TRUE, '#default_value' => $node->service );          , '#required' => TRUE, '#default_value' => $node->service );
1213      $form['getcapabilities'] = array( '#type' => 'checkbox', '#title' => t('Get capabilities'), '#required' => FALSE );      $form['getcapabilities'] = array( '#type' => 'checkbox', '#title' => t('Get capabilities')
1214            , '#required' => FALSE
1215            );
1216      $form['capabilities'] = array( '#type' => 'textarea'      $form['capabilities'] = array( '#type' => 'textarea'
1217          , '#title' => t('Capabilities')          , '#title' => t('Capabilities')
1218          , '#required' => FALSE, '#rows' => 20          , '#required' => FALSE
1219          , '#default_value' => $node->capabilities          , '#rows' => 20
1220          );          , '#default_value' => $node->capabilities ? $node->capabilities : $_SESSION['capabilities']
1221            );
1222        if( isset($_SESSION['capabilities']) )
1223            $form['capabilities']['#value'] = $_SESSION['capabilities'];
1224    //    drupal_set_message( 'sessie: ' . var_export($_SESSION['capabilities'],true) );
1225    //    drupal_set_message( 'default: ' . var_export($form['capabilities']['#default_value'],true) );
1226    
1227        // set format to carto filter
1228        $results = db_query('SELECT ff.format FROM {filter_formats} ff JOIN {filters} f ON ff.format = f.format WHERE f.module = \'carto\' and f.delta = 0' );
1229        $rec = db_fetch_array( $results );
1230        $form['format'] = filter_form( $rec['format'] );
1231    
     $form['format'] = filter_form( $node->format );  
1232      return $form;      return $form;
1233  }  }
1234    
# Line 1199  function carto_wms_validate( &$node ) Line 1236  function carto_wms_validate( &$node )
1236  {  {
1237      //print 'carto_wms_validate: ' . var_export(array("POST" => $_POST, "NODE" => $node),true) . '<hr>';      //print 'carto_wms_validate: ' . var_export(array("POST" => $_POST, "NODE" => $node),true) . '<hr>';
1238    
1239        if( $node->getcapabilities == 0 && $node->capabilities == '' )
1240        {
1241            form_set_error( 'capabilities', t('Either the capabilities must be inserted, or the \'Get capabilities\' option must be checked.') );
1242        }
1243      if( $node->capabilities == '' && $node->getcapabilities == 1 )//&& $_POST['edit']['capabilities'] == '' )      if( $node->capabilities == '' && $node->getcapabilities == 1 )//&& $_POST['edit']['capabilities'] == '' )
1244      {      {
1245          $url = $node->service . "&service=wms&request=GetCapabilities";          $url = $node->service . "&service=wms&request=GetCapabilities";
# Line 1210  function carto_wms_validate( &$node ) Line 1251  function carto_wms_validate( &$node )
1251              // Note: form value is determined by 1) $_POST (if set) or else              // Note: form value is determined by 1) $_POST (if set) or else
1252              // by 2) #default_value. An alternative is to use #value?              // by 2) #default_value. An alternative is to use #value?
1253              $node->capabilities = $response->data;              $node->capabilities = $response->data;
1254              drupal_set_message( 'Capabilities were requested in carto_wms_validate' );              $_SESSION['capabilities'] = $response->data;
1255                //drupal_set_message( 'Capabilities were requested in carto_wms_validate' );
1256          }          }
1257          else          else
1258          {          {
# Line 1218  function carto_wms_validate( &$node ) Line 1260  function carto_wms_validate( &$node )
1260          }          }
1261      }      }
1262    
1263  //    form_set_error('capabilities','carto_wms_validate functie:' . var_export($node,true) );      //form_set_error('capabilities','carto_wms_validate functie:' . var_export($node,true) );
1264  }  }
1265    
1266  function carto_wms_submit( &$node )  function carto_wms_submit( &$node )
# Line 1228  function carto_wms_submit( &$node ) Line 1270  function carto_wms_submit( &$node )
1270    
1271  function carto_wms_insert( $node )  function carto_wms_insert( $node )
1272  {  {
1273      print 'carto_wms_insert:' . var_export(array("NODE" => $node,"EDIT" => $_POST['edit']),true) . '<hr>';      //print 'carto_wms_insert:' . var_export(array("NODE" => $node,"EDIT" => $_POST['edit']),true) . '<hr>';
1274      $caps = & new CapabilitiesParser( );      $caps = & new CapabilitiesParser( );
1275    
1276      // @todo: do this (get capabilities) in validate function      // @todo: do this (get capabilities) in validate function
1277      if( $node->getcapabilities && $node->capabilities == '')      if( $node->getcapabilities && $node->capabilities == '')
1278      {      {
1279  //        carto_wms_validate( $node );  //        carto_wms_validate( $node );
1280          $url = $node->service . "&service=wms&request=GetCapabilities";          if( isset($_SESSION['capabilities']) )
         $response = drupal_http_request( $url );  
         //drupal_set_message( var_export($response,true) );  
         if( $response->code == 200 )  
1281          {          {
1282              // Set capabilities value in $_POST              $node->capabilities = $_SESSION['capabilities'];
1283              // Note: form value is determined by 1) $_POST (if set) or else          }
1284              // by 2) #default_value. An alternative is to use #value?          else
1285              $node->capabilities = $response->data;          {
1286              drupal_set_message( 'Capabilities were requested in carto_wms_insert' );              $url = $node->service . "&service=wms&request=GetCapabilities";
1287                $response = drupal_http_request( $url );
1288                //drupal_set_message( var_export($response,true) );
1289                if( $response->code == 200 )
1290                {
1291                    // Set capabilities value in $_POST
1292                    // Note: form value is determined by 1) $_POST (if set) or else
1293                    // by 2) #default_value. An alternative is to use #value?
1294                    $node->capabilities = $response->data;
1295                    drupal_set_message( 'Capabilities were requested in carto_wms_insert' );
1296                }
1297          }          }
1298      }      }
1299    
1300      $caps->parse( $node->capabilities, "insert_capabilities", & $node );      $caps->parse( $node->capabilities, "insert_capabilities", & $node );
1301  //    print 'caps:' . var_export($caps,true) . '<br>';  //    print 'caps:' . var_export($caps,true) . '<br>';
1302        unset( $_SESSION['capabilities'] );
1303    
1304      $caps->free_parser( );      $caps->free_parser( );
1305  }  }
1306    
1307  function insert_capabilities( $layer, $node )  function insert_capabilities( $layer, $node )
1308  {  {
1309  //    print 'insert_capabilities ' . var_export($layer,true) . '<br>';      static $wms_version = NULL;
1310        static $first_layer = NULL;
1311    
1312        //print 'insert_capabilities ' . var_export($layer,true) . '<br>';
1313        if( $layer['layer_id'] == 0 )
1314        {
1315            $wms_version = $layer['wms_version'];
1316        }
1317      if( $layer['layer_id'] == 1 )      if( $layer['layer_id'] == 1 )
1318      {      {
1319          db_query( "INSERT INTO {carto_wms_node} ( nid, service, capabilities, bbox )"          $first_layer = $layer['Name'];
1320              . " VALUES( %d, '%s', '%s', '%s' )"  
1321              , $node->nid, $node->service, $node->capabilities          db_query( "INSERT INTO {carto_wms_node} ( nid, service, version, capabilities, bbox, layers )"
1322                . " VALUES( %d, '%s', '%s', '%s', '%s', '%s' )"
1323                , $node->nid, $node->service, $wms_version, $node->capabilities
1324              , $layer['LatLonBoundingBox']['minx'] . ',' . $layer['LatLonBoundingBox']['miny'] . ',' . $layer['LatLonBoundingBox']['maxx'] . ',' . $layer['LatLonBoundingBox']['maxy']              , $layer['LatLonBoundingBox']['minx'] . ',' . $layer['LatLonBoundingBox']['miny'] . ',' . $layer['LatLonBoundingBox']['maxx'] . ',' . $layer['LatLonBoundingBox']['maxy']
1325                , $first_layer
1326              );              );
1327      }      }
1328      if( $layer['Title'] != '' )      if( $layer['Title'] != '' )
1329      {      {
1330            if( ! $first_layer )
1331            {
1332                $first_layer = $layer['Name'];
1333                db_query( "UPDATE {carto_wms_node} SET layers = '%s' WHERE nid = %d", $first_layer, $node->nid );
1334            }
1335          if( FALSE == @db_query( "INSERT INTO {carto_wms_layer}( nid, lid, name, title, abstract, minx, miny, maxx, maxy, queryable, opaque, cascaded )"          if( FALSE == @db_query( "INSERT INTO {carto_wms_layer}( nid, lid, name, title, abstract, minx, miny, maxx, maxy, queryable, opaque, cascaded )"
1336              . " VALUES( %d, %d, '%s', '%s', '%s', %f, %f, %f, %f, %d, %d, %d )"              . " VALUES( %d, %d, '%s', '%s', '%s', %f, %f, %f, %f, %d, %d, %d )"
1337              , $node->nid, $layer['layer_id'], $layer['Name'], $layer['Title'], $layer['Abstract']              , $node->nid, $layer['layer_id'], $layer['Name'], $layer['Title'], $layer['Abstract']
# Line 1367  function carto_wms_delete( &$node ) Line 1433  function carto_wms_delete( &$node )
1433    
1434  function carto_wms_load( $node )  function carto_wms_load( $node )
1435  {  {
1436      $additions = db_fetch_object(db_query('SELECT service, capabilities, layers, bbox FROM {carto_wms_node} WHERE nid = %d', $node->nid));      $additions = db_fetch_object(db_query('SELECT service, version, capabilities, layers, bbox, srs FROM {carto_wms_node} WHERE nid = %d', $node->nid));
1437      $additions->srs = 'EPSG:4326';  
1438      if( ! $additions->layers )      //if( ! $additions->layers ) $additions->layers = 'Countries';
1439          $additions->layers = 'Countries';      //if( ! $additions->bbox ) $additions->bbox = '-180,-90,180,90';
1440      if( ! $additions->bbox )  
         $additions->bbox = '-180,-90,180,90';  
1441      //print "carto_wms_load({$node->nid}): " . var_export($additions,true) . '<br>';      //print "carto_wms_load({$node->nid}): " . var_export($additions,true) . '<br>';
1442      return $additions;      return $additions;
1443  }  }
1444    
1445  function carto_wms_view( &$node, $teaser = FALSE, $page = FALSE )  function carto_wms_view( &$node, $teaser = FALSE, $page = FALSE )
1446  {  {
1447        // if( ! user_access('view wms nodes') ) return;
1448    
1449  //    print 'carto_wms_view: ' . var_export(array("NODE" => $node,"EDIT" => $_POST['edit']),true) . '<hr>';  //    print 'carto_wms_view: ' . var_export(array("NODE" => $node,"EDIT" => $_POST['edit']),true) . '<hr>';
1450      if( $page )      if( $page )
1451      {      {
1452          // @todo: breadcrumb navigation          // @todo: breadcrumb navigation
1453          $breadcrumb = array();          $breadcrumb = array();
1454          $breadcrumb[] = array( 'path' => 'carto', 'title' => t('carto') );          $breadcrumb[] = array( 'path' => 'carto', 'title' => t('carto') );
1455          $breadcrumb[] = array( 'path' => 'carto/'. $node->uid, 'title' => t("%name's web mapping services", array('%name' => $node->name)) );          $breadcrumb[] = array( 'path' => 'carto/user/'. $node->uid, 'title' => t("%name's web mapping services", array('%name' => $node->name)) );
1456          $breadcrumb[] = array( 'path' => 'node/'. $node->nid );          $breadcrumb[] = array( 'path' => 'node/'. $node->nid );
1457          menu_set_location( $breadcrumb );          menu_set_location( $breadcrumb );
1458      }      }
# Line 1433  function carto_wms_content($node, $tease Line 1500  function carto_wms_content($node, $tease
1500      }      }
1501    
1502      $content .= '<p>Web service: ' . $node->service . "</p>\n";      $content .= '<p>Web service: ' . $node->service . "</p>\n";
1503      $content .= '<p>Capabilities: <a href="' . $node->service . '&service=wms&request=GetCapabilities">click here</a>' . "</p>\n";      $content .= '<p>Capabilities: <a href="' . $node->service . '&service=wms&request=GetCapabilities">click there</a>'
1504        . " or <a href=\"" . base_path() . "carto/wms/node/{$node->nid}?request=GetCapabilities\">here</a>"
1505            . "</p>\n";
1506      $content .= '<p>Layers: ' . $node->layers . "</p>\n";      $content .= '<p>Layers: ' . $node->layers . "</p>\n";
1507    
1508      if( variable_get('carto_wms_teaser_map',1) )//! $teaser )      if( variable_get('carto_wms_teaser_map',1) )//! $teaser )
# Line 1468  function carto_wms_content($node, $tease Line 1537  function carto_wms_content($node, $tease
1537    
1538          $imageformat = "image/png";          $imageformat = "image/png";
1539    
1540          $widget_text = "{mappingwidget type=\"SimpleMap\" service=\"{$node->service}&version=1.1.1\"          $widget_text = "{mappingwidget type=\"SimpleMap\" service=\"{$node->service}&version={$node->version}\"
1541      width=\"$width\" height=\"$height\"      width=\"$width\" height=\"$height\"
1542      layers=\"{$node->layers}\"      layers=\"{$node->layers}\"
1543      extent=\"{$extent}\" srs=\"{$node->srs}\"      extent=\"{$extent}\" srs=\"{$node->srs}\"
# Line 1585  function carto_wms_all( $type = '', $id Line 1654  function carto_wms_all( $type = '', $id
1654      }      }
1655      else if( $type == 'node' && $id )      else if( $type == 'node' && $id )
1656      {      {
1657          carto_wms_service( $id );          carto_wms_query( $id );
1658      }      }
1659      else      else
1660      {      {
# Line 1593  function carto_wms_all( $type = '', $id Line 1662  function carto_wms_all( $type = '', $id
1662      }      }
1663  }  }
1664    
1665  function carto_wms_service( $nid )  function carto_wms_query( $nid )
1666  {  {
1667      $request = strtolower( $_GET['request'] );      $request = strtolower( $_GET['request'] );
1668      switch( $request )      switch( $request )
# Line 1605  function carto_wms_service( $nid ) Line 1674  function carto_wms_service( $nid )
1674              break;              break;
1675    
1676          case 'getmap':          case 'getmap':
1677              echo 'todo: handle wms service request to wms node';              // pass arguments to wms service
1678              // todo: pass arguments to wms service              $node = node_load( $nid );
1679                $url = $node->service;
1680                $mimetype = null;
1681                foreach( $_GET as $param => $value )
1682                {
1683                    if( $param != 'q' )
1684                    {
1685                        $url .= '&' . $param . '=' . $value;
1686                        if( $param == 'format' )
1687                            $requestedFormat = $value;
1688                    }
1689                }
1690                //print $url;
1691                $response = drupal_http_request( $url );
1692                // response may contain (request, code, data, headers) or (error)
1693                if( $response->code == 200 )
1694                {
1695                    //$response->data = 'xx';
1696                    foreach( $response->headers as $key => $value )
1697                    {
1698                        if( $key == 'Content-Type' )
1699                        {
1700                            $requestedFormat = NULL;
1701                            $mimeType = explode( '/', $value );
1702                            if( $mimeType[0] != 'image' )
1703                            {
1704                                // show response data as plain text
1705                                $value = 'text/plain';
1706                            }
1707                        }
1708                        drupal_set_header( $key . ': ' . $value );
1709                    }
1710                    //echo var_export($response,true); return;
1711                    if( $requestedFormat )
1712                        drupal_set_header( "Content-Type: " . $requestedFormat );
1713                    echo $response->data;
1714                }
1715                else
1716                {
1717                    //$response->data = 'xx';
1718                    //foreach( $response->headers as $key => $value )
1719                    //    drupal_set_header( $key . ': ' . $value );
1720                    //echo var_export($response,true); return;
1721    
1722                    echo t('Error: ') . $response->error;
1723                    //echo $url . '<br/>';
1724                    //echo var_export($response,true);
1725                }
1726              break;              break;
1727    
1728          default:          default:
1729              $output = 'Not a valid request';              $output = t('Not a valid request or not supported');
1730                // todo: explain usage
1731              echo theme( 'page', $output );              echo theme( 'page', $output );
1732      }      }
1733  }  }
# Line 1786  function carto_wms_select_layers( $nid ) Line 1903  function carto_wms_select_layers( $nid )
1903      $layers = explode(',',$results['layers']);      $layers = explode(',',$results['layers']);
1904    
1905      // add checkboxes for layers      // add checkboxes for layers
1906      $results = db_query( "SELECT * FROM {carto_wms_layer} WHERE nid = %d", $nid );      //$results = db_query( "SELECT l.nid,l.lid,l.name,l.abstract FROM {carto_wms_layer} l WHERE l.nid = %d", $nid );
1907      $headers = array('nid','lid','name','title','abstract','minx','miny','maxx','maxy','queryable','opaque','cascaded');      $results = db_query( "SELECT l.nid,l.lid,l.name,l.abstract,p.value as srs"
1908            . " FROM {carto_wms_layer} l LEFT JOIN {carto_wms_layer_properties} p ON l.nid = p.nid AND l.lid = p.lid"
1909            . " WHERE l.nid = %d AND p.property = 'SRS'", $nid );
1910    
1911        //$headers = array('nid','lid','name','title','abstract','minx','miny','maxx','maxy','queryable','opaque','cascaded');
1912    
1913      $i = 0;      $i = 0;
1914      $rows = array();      $rows = array();
1915        //print db_num_rows($results);
1916    
1917        $form['#redirect'] = 'node/' . $nid;
1918        // the srs values must be hierarchical (for each layer)
1919        $form['srs'] = array( '#tree' => TRUE );
1920        $common_srs = array();
1921        $prev_srs = NULL;
1922      while( ($row = db_fetch_array( $results )) != NULL )      while( ($row = db_fetch_array( $results )) != NULL )
1923      {      {
1924          if( $row['name'] )          if( $row['name'] )
1925          {          {
1926              $form[ $row['name'] ]['name'] = array( '#type' => 'item'              $key = $row['name'];
1927                  , '#title' => $row['name']  
1928                  , '#value' => $row['abstract']              $form['name'][ $key ] = array( '#value' => $row['name'] );
1929                  );              $form['abstract'][ $key ] = array( '#value' => $row['abstract'] );
1930              $options[ $row['name'] ] = '';              $form['srs-item'][ $key ] = array( '#value' => $prev_srs == $row['srs'] ? 'id.' : $row['srs'] );
1931              if( in_array($row['name'],$layers) )              // note: 'value'-type fields are an alternative to hidden fields and
1932                  $status[] = $row['name'];              // do not need to be sent (=rendered) to the browser
1933                $form['srs'][ $key ] = array( '#type' => 'value', '#value' => $row['srs'] );
1934    
1935                $options[ $key ] = '';
1936                if( in_array( $key, $layers ) )
1937                {
1938                    $status[] = $key;
1939    
1940                    // determine common srs
1941                    $common_srs = get_common_srs( $row['srs'] );
1942                }
1943                $prev_srs = $row['srs'];
1944          }          }
1945      }      }
1946      $form['status'] = array( '#type' => 'checkboxes', '#options' => $options, '#default_value' => $status );      $form['status'] = array( '#type' => 'checkboxes', '#options' => $options, '#default_value' => $status );
1947      $form['buttons']['submit'] = array('#type' => 'submit', '#value' => t('Save configuration') );      $form['buttons']['submit'] = array('#type' => 'submit', '#value' => t('Save configuration') );
1948      $form['nid'] = array( '#type' => 'hidden', '#value' => $nid );  
1949        $form['nid'] = array( '#type' => 'value', '#value' => $nid );
1950        $form['common_srs'] = array( '#type' => 'value', '#value' => implode(' ',$common_srs) );
1951    
1952        //drupal_set_message( 'build: common_srs:' . implode(' ', get_common_srs()));
1953      return $form;      return $form;
1954  }  }
1955    
1956  function theme_carto_wms_select_layers( $form )  function theme_carto_wms_select_layers( $form )
1957  {  {
1958      foreach (element_children($form) as $key)      foreach (element_children($form['name']) as $key)
1959      {      {
1960          $row = array();          $row = array();
1961          if (is_array($form[$key]['name']))          $row[] = array( 'data' => drupal_render( $form['status'][$key]), 'valign' => 'top' );
1962          {          $row[] = array( 'data' => drupal_render( $form['name'][$key] ), 'valign' => 'top' );
1963              $row[] = array('data' => drupal_render($form['status'][$key]), 'align' => 'center');          $row[] = array( 'data' => drupal_render( $form['abstract'][$key] ), 'valign' => 'top' );
1964              $row[] = drupal_render($form[$key]['name']);          $row[] = array( 'data' => drupal_render( $form['srs-item'][$key] ), 'valign' => 'top' );
         }  
1965          $rows[] = $row;          $rows[] = $row;
1966      }      }
1967        $header = array( t('Enabled'), t('Name'), t('Abstract'), t('SRS') );
1968      $header = array( t('Enabled'), t('Name') );  
1969      $output = theme('table', $header, $rows);      $output = theme( 'table', $header, $rows );
1970      $output .= drupal_render($form);  
1971        // render the hidden fields containing srs values here (not as a table column!)
1972        //$output .= drupal_render( $form['srs'] );
1973    
1974        // Render the remainder of the form.
1975        $output .= drupal_render( $form );
1976      return $output;      return $output;
1977  }  }
1978    
1979  function carto_wms_select_layers_validate( $form_id, $form_values )  function carto_wms_select_layers_validate( $form_id, $form_values )
1980  {  {
1981      $layers = implode(',',array_diff(array_values($form_values['status']),array(0)));      $layers = array_diff(array_values($form_values['status']),array(0));
1982      if( $layers == '' )      if( count($layers) == 0 )
1983      {      {
1984          form_set_error( 'status', t('At least one layer must be selected') );          form_set_error( 'status', t('At least one layer must be selected') );
1985            return;
1986        }
1987    
1988        // the selected layers must have a common srs and bbox!!
1989        get_common_srs( array() ); // reset common srs
1990        for( $i = 0; $i < count($layers); $i++ )
1991        {
1992            get_common_srs( $form_values['srs'][ $layers[$i] ] );
1993        }
1994        $common_srs = get_common_srs();
1995        if( count($common_srs) == 0 )
1996        {
1997            form_set_error( 'srs', t('The selected layers don\'t share a common SRS') );
1998        }
1999        else
2000        {
2001            // keep common_srs for submit
2002            $_SESSION[$form_id]['common_srs'] = $common_srs;
2003            //drupal_set_message( 'validate: common_srs:' . implode(' ', get_common_srs()));
2004      }      }
2005  }  }
2006    
2007  function carto_wms_select_layers_submit( $form_id, $form_values )  function carto_wms_select_layers_submit( $form_id, $form_values )
2008  {  {
2009      $layers = implode(',',array_diff(array_values($form_values['status']),array(0)));      $layers = implode(',',array_diff(array_values($form_values['status']),array(0)));
2010    
2011      $nid = $form_values['nid'];      $nid = $form_values['nid'];
2012      db_query( "UPDATE {carto_wms_node} SET layers = '%s' WHERE nid = %d", $layers, $nid );  
2013        // find a common srs and bbox for the selected layers!
2014        $common_srs = $_SESSION[$form_id]['common_srs'];
2015        unset( $_SESSION[$form_id]['common_srs'] );
2016    
2017        // find the common bbox
2018        // todo: at present all layers of wms for the common srs are used
2019        $srs = $common_srs[0];
2020        if( $srs == 'EPSG:4326' )
2021        {
2022            // get LatLonBoundingBox
2023            $qry = "SELECT value FROM {carto_wms_layer} l"
2024                . " JOIN {carto_wms_layer_properties} p ON l.nid = p.nid AND l.lid = p.lid"
2025                . " WHERE l.nid = %d AND property = 'LatLonBoundingBox'";
2026            $results = db_query( $qry, $nid );
2027        }
2028        else
2029        {
2030            // get BoundingBox for SRS
2031            $qry = "SELECT value FROM {carto_wms_layer} l"
2032                . " JOIN {carto_wms_layer_properties} p ON l.nid = p.nid AND l.lid = p.lid"
2033                . " WHERE l.nid = %d AND property = 'BoundingBox' AND p.name = '%s'";
2034            $results = db_query( $qry, $nid, $srs );
2035        }
2036    
2037        $bbox = NULL;
2038        while( ($rec = db_fetch_array( $results )) != NULL )
2039        {
2040            $rect = explode( ' ', $rec['value'] );
2041            $rect = array( floatval($rect[0]), floatval($rect[1]), floatval($rect[2]), floatval($rect[3]) );
2042            if( is_null( $bbox ) )
2043            {
2044                $bbox = $rect;
2045            }
2046            else
2047            {
2048                if( $rect[0] < $bbox[0] ) $bbox[0] = $rect[0];
2049                if( $rect[1] < $bbox[1] ) $bbox[1] = $rect[1];
2050                if( $rect[2] > $bbox[2] ) $bbox[2] = $rect[2];
2051                if( $rect[3] > $bbox[3] ) $bbox[3] = $rect[3];
2052            }
2053        }
2054    
2055        $bbox = implode( ',', $bbox );
2056        db_query( "UPDATE {carto_wms_node} SET layers = '%s', srs = '%s', bbox = '%s' WHERE nid = %d", $layers, $srs, $bbox, $nid );
2057  }  }
2058    
2059    function get_common_srs( $other = NULL )
2060    {
2061        static $common_srs = NULL;
2062    
2063        //if( $common_srs == NULL ) drupal_set_message( 'common_srs was NULL' );
2064        if( $other != NULL )
2065        {
2066            $srs = explode( ' ', $other );
2067            if( $common_srs == NULL )
2068            {
2069                $common_srs = $srs;
2070            }
2071            else
2072            {
2073                $common_srs = array_intersect( $srs, $common_srs );
2074            }
2075        }
2076        return $common_srs;
2077    }
2078  ?>  ?>

Legend:
Removed from v.1.18  
changed lines
  Added in v.1.19

  ViewVC Help
Powered by ViewVC 1.1.2