Example #1
0
function map_show_types($map, $id, $types)
{
    $html = '';
    foreach ($types as $ptype => $rec) {
        $html .= clb_tag('option', clb_val($ptype, $rec, 'label'), '', array('value' => $ptype));
    }
    return clb_tag('select', '', $html, array('id' => $id, 'name' => $id, 'multiple' => 'multiple', 'size' => min(20, count($types)) + 1, 'onchange' => 'map_refresh(' . $map . ', \'purge\');'));
}
Example #2
0
function pfind_service($prms, $pfind_defs, $type_names, $p2p_paths, $node_types = FALSE)
{
    clb_timing(__LINE__);
    pfind_def_tables($pfind_defs);
    $result = array('error' => 200, 'error_str' => 'no errors');
    //get the query or way points from the request
    $entries = array();
    if (isset($prms['q']) && $prms['q']) {
        $entries[] = $prms['q'];
    } else {
        $w = 0;
        while (isset($prms['w' . $w])) {
            $entries[] = $prms['w' . $w];
            $w++;
        }
    }
    $prop = array();
    $prop['service'] = strtolower(clb_val('json', $prms, 's'));
    //json, xml
    $prop['mode'] = strtolower(clb_val('', $prms, 'm'));
    $prop['units'] = strtolower(clb_val('mi', $prms, 'u'));
    //km, mi
    $prop['opto'] = strtolower(clb_val('best', $prms, 'o'));
    //dist, change, best
    $prop['opacity'] = strtolower(clb_val(0.5, $prms, 't'));
    $prop['color'] = strtolower(clb_val('#0000FF', $prms, 'c'));
    $prop['stroke'] = strtolower(clb_val(5, $prms, 'k'));
    $dist_only = FALSE != clb_val(0, $prms, 'gd');
    //get result from precalculated distances
    $prop['dist_only'] = $dist_only;
    $prop['getSteps'] = !$dist_only && FALSE != clb_val(0, $prms, 'gs');
    $prop['getPolyline'] = !$dist_only && FALSE != clb_val(0, $prms, 'gp');
    $path = FALSE;
    $mode = $prop['mode'];
    if (is_string($p2p_paths)) {
        $path = $p2p_paths;
    } else {
        if (!is_array($p2p_paths)) {
            $result = array('error' => 500, 'error_str' => 'server configuration error: route tables not specified');
        } else {
            if ($mode && isset($p2p_paths[$mode])) {
                $path = $p2p_paths[$mode];
            } else {
                if (isset($p2p_paths['rtm'])) {
                    $node_types = array('rail', 'tube', 'tram');
                    $path = $p2p_paths['rtm'];
                } else {
                    $path = reset($p2p_paths);
                }
            }
        }
    }
    if (is_array($path)) {
        if (!$node_types) {
            $node_types = clb_val(FALSE, $path, 'types');
        }
        //get the types
        $path = clb_val(FALSE, $path, 'path');
    }
    //if types not given as param or with paths, but the mode name is a node type use it as default.
    if (!$node_types && clb_val(FALSE, $type_names, $prop['mode'])) {
        $node_types = $prop['mode'];
    }
    if ($node_types && !is_array($node_types)) {
        $node_types = array($node_types);
    }
    //make types an array if just a single
    if (!$path || !is_string($path) || !is_file($path)) {
        $result = array('error' => 500, 'error_str' => 'server configuration error: route tables could not be loaded');
    } else {
        $waypoints = array();
        $result = pfind_interpret($entries, $mode, $waypoints, $path, $node_types);
        qlog(__LINE__, $mode, $entries, $result);
        if (IS_LOCAL) {
            foreach ($waypoints as $i => $stage) {
                foreach ($stage as $see) {
                    qlog(__LINE__, $i, join(', ', $see));
                }
            }
        }
        clb_timing('interpret');
    }
    if ($result['error'] == 200) {
        $links = FALSE;
        if (!$prop['dist_only'] && is_file($path)) {
            $data = file_get_contents($path);
            //, FILE_BINARY);	//need different p2p files for different combinations of modes
            $links = clb_blob_dec($data);
        }
        clb_timing('load array');
        if (!$links && !$prop['dist_only']) {
            $result = array('error' => 500, 'error_str' => 'point to point data file could not be found/loaded ' . $mode);
            qlog(__LINE__, $result, $mode, $path, $p2p_paths);
        } else {
            $result = pfind_routes($waypoints, $links, $prop, $type_names);
        }
        qlog(__LINE__, clb_timing('find path'));
    }
    //qlog(__LINE__,clb_xml($result, 'RouteFinder'));
    // qlog(__LINE__,clb_json($result, "'"));
    switch ($prop['service']) {
        case 'json':
            $return_data = json_encode($result);
            rs_response('application/json', $return_data, 'UTF-8');
            break;
        case 'javascript':
            $sid = clb_val('', $prms, 'sid');
            $func = clb_val('mfw_dir_result', $prms, 'callback');
            $return_data = clb_json($result, "'");
            $callback = $func . '(\'' . $sid . '\',' . $return_data . ');';
            clb_response('application/javascript', $callback, 'UTF-8');
            break;
        case 'xml':
            $return_data = clb_xml($result, 'RouteFinder');
            clb_response('xml', $return_data, 'UTF-8');
            break;
        case 'php':
            return $result;
            break;
    }
}
Example #3
0
	normally pfind_service() sends the result and exists, but if the web service mode is "php" the structure is returned.
*/
$result = pfind_service($_REQUEST, $pfind_defs, $editor_types, $p2p_paths);
if ($result['error'] == 604) {
    $_REQUEST['gd'] = 0;
    //turn off distance only
    $_REQUEST['gp'] = 0;
    //turn off polyline
    $_REQUEST['gs'] = 0;
    //turn off steps
    $result = pfind_service($_REQUEST, $pfind_defs, $editor_types, $p2p_paths);
}
switch ($service) {
    case 'javascript':
        $sid = clb_val('', $_REQUEST, 'sid');
        $func = clb_val('mfw_dir_result', $_REQUEST, 'callback');
        $return_data = clb_json($result, "'");
        $callback = $func . '(\'' . $sid . '\',' . $return_data . ');';
        rs_response('application/javascript', $callback, 'UTF-8');
        break;
    case 'json':
        $return_data = json_encode($result);
        rs_response('application/json', $return_data, 'UTF-8');
        break;
    case 'xml':
        $return_data = clb_xml($result, 'RouteFinder');
        rs_response('xml', clb_tag('xml?', '', '', array('version' => '1.0', 'encoding' => 'utf-8')) . clb_tag('responses', '', $return_data), 'UTF-8');
        break;
    default:
        header('x', TRUE, 400);
        //Deliver a 'bad request' response
Example #4
0
function pbuild_make_link(&$p2p_links, $ends, $dist = FALSE)
{
    global $wpdb;
    $rec = reset($ends);
    $name = clb_val('', $rec, RF_NODES_TYPE) . ': ' . clb_val('', $rec, RF_NODES_NAME);
    $lat1 = clb_val(0, $rec, 'lat');
    $lng1 = clb_val(0, $rec, 'lng');
    $pnum1 = clb_val(0, $rec, RF_LINKS_KEY);
    $rec = next($ends);
    $name .= clb_val('', $rec, RF_NODES_TYPE) . ': ' . clb_val('', $rec, RF_NODES_NAME);
    $lat2 = clb_val(0, $rec, 'lat');
    $lng2 = clb_val(0, $rec, 'lng');
    $pnum2 = clb_val(0, $rec, RF_LINKS_KEY);
    //checking for existing links, and specifically any existing walk link
    $linkpnum = '';
    $makelink = TRUE;
    if (isset($p2p_links['nodes'][$pnum1]) && isset($p2p_links['nodes'][$pnum2])) {
        //get the list of link_pnums for both nodes and intersect to see if they are already connected
        $connections = array_intersect(explode(',', $p2p_links['nodes'][$pnum1]), explode(',', $p2p_links['nodes'][$pnum2]));
        $makelink = count($connections) <= 0;
        //dont make a link if there is already a direct link
        //if there are links see if one of them is a walk link in which case we may want to update it if the ends have moved.
        foreach ($connections as $ref) {
            if (isset($p2p_links['links'][$ref]['ptype']) && $p2p_links['links'][$ref]['ptype'] == 'walk') {
                $linkpnum = $ref;
                $makelink = TRUE;
                //comment this out if dont want to update walk links
                if (isset($p2p_links['links'][$ref]['pt_count']) && $p2p_links['links'][$ref]['pt_count'] > 2) {
                    $makelink = FALSE;
                }
                //if the walk link has more than 2 points then
                break;
            }
        }
        if (count($connections) > 1 && $linkpnum) {
            qlog(__LINE__, 'walk link and other link types', $pnum1, $pnum2, $connections);
        }
    }
    /*
    	$p2p_links may not include existing "walk" interchanges if there were no routes involving the stops
    	but to prevent creating duplicates do a search and do nothing if a walk exists
    */
    if ($makelink && !$linkpnum) {
        $pair = clb_join(array($pnum1, $pnum2), TRUE);
        $query = 'SELECT ' . RF_LINKS_KEY . ' FROM ' . RF_LINKS_FROM . ' WHERE ' . RF_LINKS_TYPE . '="walk" AND ' . RF_LINKS_END1 . ' IN ' . $pair . ' AND ' . RF_LINKS_END2 . ' IN ' . $pair;
        $check = $wpdb->get_results($query, ARRAY_A);
        if (is_array($check) && clb_val('', reset($check), RF_LINKS_KEY)) {
            $makelink = FALSE;
        }
    }
    if ($makelink) {
        if ($dist === FALSE) {
            $dist = pline_surface_dist($lat1, $lng1, $lat2, $lng2);
        }
        $processed = clb_now_utc();
        $query = '';
        $query .= ', ' . RF_LINKS_DIST . '=' . $dist * 2;
        //double the distance so this is not chosen over actual tracks
        $query .= ', ' . RF_LINKS_TIME . '=' . ceil($dist / 80);
        //80 meters per minute is about 3 miles per hour
        $query .= ', ' . RF_LINKS_MODIFIED . '=' . clb_escape($processed);
        $query .= ', ' . RF_LINKS_NAME . '=' . clb_escape($name);
        $query .= ', lat=' . round(($lat1 + $lat2) / 2, PT_PRECISION);
        $query .= ', lng=' . round(($lng1 + $lng2) / 2, PT_PRECISION);
        $points = array();
        $points[] = array($lat1, $lng1, 0);
        $points[] = array($lat2, $lng2, 0);
        if ($polyline = pline_make($points, array('color' => '#00FF00'))) {
            $query .= ', ' . RF_LINKS_LINE . '=' . clb_escape(clb_join($polyline, '', '&', '='));
            $query .= ', ' . RF_LINKS_POINTS . '=' . clb_escape(clb_blob_enc($points));
        }
        if ($linkpnum) {
            $query = 'UPDATE ' . RF_LINKS_FROM . ' SET ' . trim($query, ', ') . ' WHERE ' . RF_LINKS_KEY . '=' . clb_escape($linkpnum);
        } else {
            $linkpnum = pbuild_new_pnum(RF_LINKS_FROM, RF_LINKS_KEY);
            $query .= ', ' . RF_LINKS_KEY . '=' . clb_escape($linkpnum);
            $query .= ', ' . RF_LINKS_CREATED . '=' . clb_escape($processed);
            $query .= ', ' . RF_LINKS_END1 . '=' . clb_escape($pnum1);
            $query .= ', ' . RF_LINKS_END2 . '=' . clb_escape($pnum2);
            $query .= ', ' . RF_LINKS_TYPE . '=' . clb_escape('walk');
            $query = 'INSERT INTO ' . RF_LINKS_FROM . ' SET ' . trim($query, ', ');
        }
        // qlog(__LINE__, $query);
        $wpdb->query($query);
    }
    return $makelink;
}
Example #5
0
 function clb_get_prm($dflt, $array, $key)
 {
     $val = clb_val($dflt, $array, $key);
     if (get_magic_quotes_gpc() || ini_get('magic_quotes_sybase') || function_exists('add_magic_quotes')) {
         $val = stripslashes($val);
     }
     return $val;
 }
Example #6
0
function pline_splice($db, $route, $pnum1 = FALSE, $pnum2 = FALSE, $stopsegs = FALSE)
{
    //$route is an array of link pnums
    if (!is_array($route) || count($route) == 0) {
        qlog(__LINE__, __FUNCTION__, 'bad route', $route, $pnum1, $pnum2);
        //exit();
        return '';
    }
    $raw = is_array($stopsegs);
    //TRUE => segment end points are points not pnums
    if ($raw) {
        //in the raw case we have to trim the ends so prepare information we will need
        $key1 = pline_arr_match($stopsegs['stoppnums'], $pnum1, $stopsegs['segpnums'], reset($route));
        $key1 = reset($key1);
        //should only be one match but still need to turn array into single value
        $cut1 = clb_val(FALSE, $stopsegs['data'], $key1);
        $key2 = pline_arr_match($stopsegs['stoppnums'], $pnum2, $stopsegs['segpnums'], end($route));
        $key2 = reset($key2);
        //should only be one match but still need to turn array into single value
        $cut2 = clb_val(FALSE, $stopsegs['data'], $key2);
    }
    //load all segs in one hit
    $query = 'SELECT ' . RF_LINKS_SELECT . ',' . RF_LINKS_POINTS . ' FROM ' . RF_LINKS_FROM . ' WHERE ' . RF_LINKS_KEY . ' IN ' . clb_join($route, TRUE);
    $segs = $db->get_results($query, ARRAY_A);
    $segs = clb_rekey($segs, 'pnum');
    $last_end1 = $last_end2 = $flip = FALSE;
    $flipfirst = FALSE;
    //if the first seg is flipped this holds the nubmer of points so we can work out how many to trim
    $points = array();
    $count = 0;
    $max = count($route);
    foreach ($route as $seg_pnum) {
        $count++;
        //so will be 1 on first loop, 2 on second etc
        $rec = clb_val(FALSE, $segs, $seg_pnum);
        $aoe_text = clb_val('', $rec, RF_LINKS_POINTS);
        $aoe_data = pline_pts_arr($aoe_text);
        if (!is_array($aoe_data)) {
            qlog(__FUNCTION__, __LINE__, 'failed to get points list for route segment', $seg_pnum, $count, $aoe_data);
            continue;
        }
        $this_end1 = clb_val('', $rec, RF_LINKS_END1);
        $this_end2 = clb_val('', $rec, RF_LINKS_END2);
        $flip = FALSE;
        if ($max == 1) {
            if ($raw) {
                //if the position within the segment of the second point is before the first, then we need to reverse
                $flip = isset($cut1['pos']) && isset($cut2['pos']) && $cut2['pos'] < $cut1['pos'];
            } else {
                if ($pnum1) {
                    $flip = $this_end2 == $pnum1;
                    //on a stop to stop link, need to reverse if start pnum is at tail end of link
                }
            }
            if ($flip) {
                $flipfirst = count($aoe_data);
            }
        } else {
            if ($count <= 1 || $this_end1 == $last_end2) {
                //no action
            } else {
                if ($count == 2 && $this_end1 == $last_end1) {
                    $points = array_reverse($points, TRUE);
                    //TRUE=preserve_keys
                    $flipfirst = count($points);
                } else {
                    if ($count == 2 && $this_end2 == $last_end1) {
                        $points = array_reverse($points, TRUE);
                        //TRUE=preserve_keys
                        $flipfirst = count($points);
                        $flip = TRUE;
                    } else {
                        $flip = $this_end2 == $last_end2;
                    }
                }
            }
        }
        if ($flip) {
            $aoe_data = array_reverse($aoe_data, TRUE);
            //TRUE=preserve_keys
            list($this_end1, $this_end2) = array($this_end2, $this_end1);
            //reverse end points too
        }
        //check if first point on this seg is same as last point on previous seg and if so remove it
        if (count($points) && count($aoe_data)) {
            $last = end($points);
            $first = reset($aoe_data);
            //do an array_pop on the existing points rather than removing the duplicate from $aoe_data since that alters the cutting math on raw segs
            if (round($last[0], 5) == round($first[0], 5) && round($last[1], 5) == round($first[1], 5)) {
                array_pop($points);
            }
        }
        $points = array_merge($points, $aoe_data);
        //$seg_idx[$this_end2] = count($points) - 1;
        $last_end1 = $this_end1;
        $last_end2 = $this_end2;
    }
    if ($raw) {
        //if the first seg was reversed then we want to keep the number of points on the other side
        $pos = is_int($flipfirst) ? $flipfirst - $cut1['pos'] : $cut1['pos'];
        //cut off end points and insert the stops own point.  If this is duplicate it will be cleaned by polyline later
        array_splice($points, 0, $pos, array(array($cut1['lat'], $cut1['lng'], 0)));
        //if the last seg was reversed then we want to keep the number of points on the other side
        $pos = $flip ? $cut2['pos'] : count($aoe_data) - $cut2['pos'];
        //aoe_data still around after last itteration of loop
        //cut off end points and insert the stops own point.  If this is duplicate it will be cleaned by polyline later
        array_splice($points, -$pos, $pos, array(array($cut2['lat'], $cut2['lng'], 0)));
        //qlog(__FUNCTION__, __LINE__, $pnum1, $cut1, $pnum2, $cut2);
    }
    //return array($points, $seg_idx);	//not using this because the positions may change when making a polyline and makes returning the points list more cumbersome
    return $points;
}
Example #7
0
function ajax_request()
{
    global $editor_types, $editor_tables;
    $xml = '';
    $msg = '';
    $rec_count = 0;
    $db = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
    $pnum = clb_val(FALSE, $_REQUEST, 'pnum');
    if ($new_mark = preg_match('/^b(\\w{7})(\\w{7})/', $pnum, $m)) {
        $_REQUEST['lat'] = hexdec($m[1]) / 100000 - 180;
        $_REQUEST['lng'] = hexdec($m[2]) / 100000 - 180;
        $_REQUEST['coords'] = clb_b64e($_REQUEST['lat']) . clb_b64e($_REQUEST['lng']);
    }
    $ptype = clb_val(FALSE, $_REQUEST, 'ptype');
    //if the ptype is cust, default to the first table and type in the definitions that does or does not have a polyline
    if ($ptype == 'cust' && $new_mark) {
        foreach ($editor_types as $ptype => $spec) {
            if (clb_val(FALSE, $spec, 'polyline') == isset($_REQUEST['polyline'])) {
                break;
            }
        }
    }
    $table = FALSE;
    $prefixes = clb_column($editor_tables, 'prefix');
    if ($pnum && preg_match('/^(\\w+)_\\w+$/', $pnum, $m)) {
        $table = array_search($m[1], $prefixes);
    } else {
        if ($ptype) {
            $table = clb_val(FALSE, $editor_types, $ptype, 'table');
        }
    }
    $spec = clb_val(FALSE, $editor_tables, $table);
    $ajax = clb_val(FALSE, $_REQUEST, 'ajax');
    qlog(__LINE__, '>>>>', $ajax, $pnum, $ptype, $table, $new_mark ? 'new' : 'old');
    //, $spec);
    switch ($ajax) {
        case 'show':
            $sel = FALSE;
            if ($spec) {
                $select = clb_val(FALSE, $spec, 'select');
                $query = 'SELECT ' . clb_join($select, '`') . ' FROM ' . $table . ' WHERE pnum=' . clb_escape($pnum);
                $sel = $db->get_results($query, ARRAY_A);
            }
            //if not found via pnum, try title in each table.
            if (!$sel) {
                foreach ($editor_tables as $table => $spec) {
                    if (($select = clb_val(FALSE, $spec, 'select')) && in_array('title', $select)) {
                        $query = 'SELECT ' . clb_join($select, '`') . ' FROM ' . $table . ' WHERE title like ' . clb_escape($pnum . '%');
                        $sel = $db->get_results($query, ARRAY_A);
                        if ($sel) {
                            break;
                        }
                    }
                }
            }
            if (!$sel) {
                $msg = 'No matches found for: ' . $pnum;
            } else {
                $titles = clb_column($sel, 'title');
                $msg = count($sel) . ' matches found: ' . join(', ', $titles);
                $rec = reset($sel);
                $pnum = clb_val(FALSE, $rec, 'pnum');
                $xml .= clb_tag('response', '', '', array_merge(array('type' => 'centre'), array_intersect_key($rec, array('lat' => 0, 'lng' => 0)))) . "\n";
                $xml .= refesh_marker($db, $table, $pnum);
                $xml .= marker_bubble($db, $spec, $table, $pnum);
            }
            break;
        case 'route_list':
            //shows bubble on station showing list of routes
            $html = '';
            //find the route table and which field(s) hold the stop list
            $route_flds = '';
            foreach ($editor_tables as $route_tab => $info) {
                if ($route_flds = clb_val(FALSE, $info, 'routes')) {
                    break;
                }
            }
            if (!$route_flds) {
                break;
            }
            $route_flds = explode('/', $route_flds);
            //can be more than one field with '/' as a separator
            //build query to get route list
            $select = clb_val(FALSE, $editor_tables, $route_tab, 'select');
            $where = array();
            foreach ($route_flds as $fld) {
                $where[] = ' (`' . $fld . '` LIKE ' . clb_escape('%' . $pnum . '%') . ')';
            }
            $query = 'SELECT ' . clb_join(array_merge($select, $route_flds), '`') . ' FROM ' . $route_tab . ' WHERE ' . join(' OR ', $where);
            $sel = $db->get_results($query, ARRAY_A);
            if (clb_count($sel) == 0) {
                $msg = 'There are no routes for node: ' . $pnum;
            } else {
                $list = '';
                $alt = 0;
                foreach ($sel as $rec) {
                    foreach ($route_flds as $pos => $fld) {
                        $rnum = reset($rec);
                        $route_key = key($rec);
                        $script = 'map_ajax(the_map, \'show_route\', {\'pnum\':\'' . $rnum . '\',\'fld\':\'' . $fld . '\', \'stop\':\'' . $pnum . '\'});';
                        $name = $rnum . ':' . $pos . ': ' . clb_val(FALSE, $rec, 'title');
                        $list .= clb_tag('li', '', clb_tag('a', $name, '', array('onclick' => $script, 'href' => 'javascript:void(0);')), array('class' => 'alt' . $alt++ % 2));
                    }
                }
                $html .= clb_tag('ul', '', $list, array('class' => 'route_list')) . "\n";
                $form = clb_tag('form', '', $html, array('id' => 'info_win', 'action' => '', 'method' => 'get', 'onsubmit' => 'return false;')) . "\n";
                $xml .= clb_tag('response', '', htmlspecialchars($form), array('type' => 'bubble', 'pnum' => $pnum)) . "\n";
            }
            break;
        case 'new_route':
            //create new route
        //create new route
        case 'route_stop':
            //add stop to route
        //add stop to route
        case 'route_unstop':
            //remove stop from route
        //remove stop from route
        case 'show_route':
            //show route
            //		qlog(__LINE__, $table, $pnum, $spec);
            require_once CODE_DIR . 'pline_lib.php';
            //scan tables to find routes
            foreach ($editor_tables as $table => $info) {
                if ($routes = clb_val(FALSE, $info, 'routes')) {
                    break;
                }
            }
            if (!$routes) {
                break;
            }
            //did not find route table
            $routes = explode('/', $routes);
            $route_tab = $table;
            $select = clb_val(FALSE, $editor_tables, $route_tab, 'select');
            $route_key = reset($select);
            $stops = FALSE;
            $new_route = 'new_route' == $ajax;
            if ($new_route) {
                $fld = reset($routes);
                $newstop = $pnum;
                $stopindex = 0;
                $list = '';
                $title = clb_val(FALSE, $_REQUEST, 'title');
                $pnum = new_pnum($db, $table, $route_key);
                $ajax = 'route_stop';
            } else {
                $stopindex = clb_val(FALSE, $_REQUEST, 'stopindex');
                //this only come with route_stop and route_unstop
                $newstop = clb_val(FALSE, $_REQUEST, 'newstop');
                //this only come with route_stop
                $fld = clb_val(FALSE, $_REQUEST, 'fld');
                $select = array_merge($select, $routes);
                //select route record
                $query = 'SELECT ' . clb_join($select, '`') . ' FROM ' . $route_tab . ' WHERE ' . $route_key . '=' . clb_escape($pnum);
                $sel = $db->get_results($query, ARRAY_A);
                $rec = clb_count($sel) ? reset($sel) : FALSE;
                $list = clb_val(FALSE, $rec, $fld);
                $ptype = clb_val(FALSE, $rec, 'ptype');
                $title = clb_val(FALSE, $rec, 'title');
                //if (empty($list) && (reset($routes) == $fld) && ($fld
                if (empty($list)) {
                    $newstop = clb_val(FALSE, $_REQUEST, 'stop');
                    $stopindex = 0;
                    $ajax = 'route_stop';
                }
            }
            if (preg_match_all('/^(\\w+)\\s+(.*)/m', $list, $m)) {
                $stops = $m[1];
            }
            if (in_array($ajax, array('route_stop', 'route_unstop')) && is_numeric($stopindex)) {
                if (!$stops) {
                    $stops = array(FALSE);
                }
                //ensure loop runs at least once
                $new = '';
                foreach ($stops as $i => $stop_pnum) {
                    if ('route_unstop' == $ajax && $i == $stopindex) {
                        continue;
                    }
                    //skip the stop that is to be deleted
                    //add the stop on this line
                    if ($stop_pnum) {
                        $new .= $m[1][$i] . ' ' . trim($m[2][$i]) . "\n";
                    }
                    //add new station if this is the right position
                    if ('route_stop' == $ajax && $i == $stopindex && $newstop) {
                        //find maker table via ptype
                        $table = clb_val(FALSE, $editor_types, $ptype, 'table');
                        $query = 'SELECT lat, lng, pnum, ptype, title FROM ' . $table . ' WHERE pnum=' . clb_escape($newstop);
                        $sel = $db->get_results($query, ARRAY_A);
                        if ($sel) {
                            $new .= $newstop . ' ' . clb_val('', reset($sel), 'title') . "\n";
                        }
                    }
                }
                if (preg_match_all('/^(\\w+)\\s+(.*)/m', $new, $m)) {
                    $stops = $m[1];
                }
                //if forward direction and changing last item update orig, dest and title
                $rename = '';
                if (reset($routes) == $fld && clb_count($stops) <= $stopindex + 2) {
                    $orig = reset($m[2]);
                    $dest = end($m[2]);
                    $rename .= ', title=' . clb_escape($orig . ' - ' . $dest);
                    $rename .= ', orig=' . clb_escape($orig);
                    $rename .= ', dest=' . clb_escape($dest);
                }
                if ($new_route) {
                    $query = 'SELECT area, count(*) AS c FROM ' . $route_tab . ' WHERE ptype=' . clb_escape($ptype) . ' GROUP BY area';
                    $sel = $db->get_results($query, ARRAY_A);
                    $area = is_array($sel) ? clb_val('', reset($sel), 'area') : '';
                    $query = '';
                    $query .= ', ' . $route_key . '=' . clb_escape($pnum);
                    $query .= ', area=' . clb_escape($area);
                    $query .= ', ptype=' . clb_escape($ptype);
                    $query .= ', ' . $fld . '=' . clb_escape($new);
                    $query .= ', created=' . clb_escape(clb_now_utc());
                    $query = 'INSERT INTO ' . $route_tab . ' SET ' . trim($query, ', ') . $rename;
                } else {
                    $query = 'UPDATE ' . $route_tab . ' SET ' . $fld . '=' . clb_escape($new) . $rename . ' WHERE ' . $route_key . '=' . clb_escape($pnum);
                }
                $db->query($query);
            }
            if ($stops) {
                $index = array_search(clb_val(FALSE, $_REQUEST, 'stop'), $stops);
                //index of the stop we opened route from
                if (is_numeric($stopindex)) {
                    $index = Min($stopindex + ($newstop ? 1 : 0), clb_count($stops) - 1);
                }
                //use new index if provided and add one if new stop
                if (preg_match('/^(\\w+)_\\w+$/', reset($stops), $m)) {
                    $table = array_search($m[1], $prefixes);
                    $spec = clb_val(FALSE, $editor_tables, $table);
                    $points = array();
                    $names = array();
                    $pnums = array();
                    $query = 'SELECT lat, lng, pnum, ptype, title FROM ' . $table . ' WHERE pnum IN ' . clb_join($stops, TRUE);
                    $sel = $db->get_results($query, ARRAY_A);
                    //in theory could be multiple stop instances and need to give points in order so loop on $stops and look up record via xref
                    $xref = clb_column($sel, 'pnum');
                    if (clb_count($sel)) {
                        foreach ($stops as $stop_pnum) {
                            $rec = clb_val(FALSE, $sel, array_search($stop_pnum, $xref));
                            $points[] = array($rec['lat'], $rec['lng'], 0);
                            $names[] = str_replace('|', ',', $rec['title']);
                            $pnums[] = $rec['pnum'];
                            $rec['type'] = 'marker';
                            $xml .= clb_tag('response', '', '', $rec) . "\n";
                            $rec_count++;
                        }
                    }
                    foreach ($points as $i => $pt) {
                        if (!is_array($pt)) {
                            unset($points[$i]);
                        }
                    }
                    //remove non points if there are any
                    $line = pline_make($points, array('color' => '#FF0000'));
                    $line['names'] = join('|', $names);
                    $line['pnums'] = join('|', $pnums);
                    if ($line) {
                        $line = clb_join($line, '', '&', '=');
                    }
                    $attr = array('type' => 'route_pline', 'pnum' => $pnum, 'title' => $title, 'fld' => $fld, 'ptype' => $ptype, 'index' => $index);
                    if ($line) {
                        $xml .= clb_tag('response', '', htmlspecialchars($line), $attr) . "\n";
                    }
                }
            } else {
                $msg = 'This route (in this direction) has no stops. ';
            }
            break;
        case 'overlays':
            if (clb_val(FALSE, $_REQUEST, 'zm') < 9) {
                $msg = 'Zoom in to get markers to download.';
            } else {
                $rect = array();
                $rect[] = 'lat >= ' . clb_val(FALSE, $_REQUEST, 'bot');
                $rect[] = 'lng >= ' . clb_val(FALSE, $_REQUEST, 'lft');
                $rect[] = 'lat <= ' . clb_val(FALSE, $_REQUEST, 'top');
                $rect[] = 'lng <= ' . clb_val(FALSE, $_REQUEST, 'rgt');
                $qtypes = preg_split('/;/', clb_val(FALSE, $_REQUEST, 'types'), -1, PREG_SPLIT_NO_EMPTY);
                $tables = array();
                foreach ($qtypes as $ptype) {
                    $tables[clb_val('bad', $editor_types, $ptype, 'table')][] = $ptype;
                }
                if (!count($qtypes)) {
                    $msg = 'No marker types selected.';
                } else {
                    foreach ($tables as $table => $list) {
                        if ($table != 'bad' && count($list)) {
                            $prefix = clb_val(FALSE, $editor_tables, $table, 'prefix');
                            $types = preg_replace('/\\w+_/', '', clb_join($list, TRUE));
                            $query = '';
                            $query .= 'SELECT lat, lng, pnum, ptype, title FROM ' . $table . ' WHERE ';
                            $query .= ' ptype IN ' . $types . ' AND (' . join(' AND ', $rect) . ') ';
                            $sel = $db->get_results($query, ARRAY_A);
                            if (clb_count($sel)) {
                                foreach ($sel as $rec) {
                                    $rec['type'] = 'marker';
                                    if (!in_array($rec['ptype'], $list)) {
                                        $rec['ptype'] = $prefix . '_' . $rec['ptype'];
                                    }
                                    $xml .= clb_tag('response', '', '', $rec) . "\n";
                                    $rec_count++;
                                }
                            }
                        }
                    }
                }
                $msg = $rec_count ? count($sel) . ' markers loaded' : 'No markers found.';
                if ('purge' == clb_val(FALSE, $_REQUEST, 'purge')) {
                    $xml .= clb_tag('response', '', '', array('type' => 'purge')) . "\n";
                }
            }
            break;
        case 'saveline':
            require_once CODE_DIR . 'pline_lib.php';
            //fall through
        //fall through
        case 'save':
            //no real validation so just save and close
            if (!$spec) {
                $msg = 'could not identify the marker type.';
                break;
            }
            //('polyline'=>'line', 'select'=>' lat, lng, pnum, ptype, title, line, aoe_data')
            if ($new_mark) {
                $pnum = new_pnum($db, $table);
            }
            //get field names and types
            $def = array();
            $sel = $db->get_results('describe ' . $table, ARRAY_A);
            foreach ($sel as $rec) {
                $def[$rec['Field']] = $rec['Type'];
            }
            /*
            	normally save line is handled by the normal save 
            	but if we are saving a cut line, we need to save off the first part and 
            	then use the normal save to create the latter part as a new record
            	dont cut on first or last point of line
            */
            $points = FALSE;
            if ('saveline' == $ajax) {
                $points = pline_pts_arr(clb_val(FALSE, $_REQUEST, 'polyline'));
                //the rest of this is only for cutting a line
                if (($cut = clb_val(FALSE, $_REQUEST, 'cut')) && $cut + 1 < clb_count($points)) {
                    $first = array_slice($points, 0, $cut + 1);
                    //keep points up to and including cut
                    $points = array_slice($points, $cut);
                    //reduce points on and after cut for new record
                    $query = save_line_query($first, $spec);
                    $query .= '`' . 'lat' . '`=' . clb_escape($_REQUEST['lat']) . ',';
                    $query .= '`' . 'lng' . '`=' . clb_escape($_REQUEST['lng']) . ',';
                    $query .= '`' . 'end1' . '`=' . clb_escape($_REQUEST['end1']) . ',';
                    //adjusts mid point
                    $query .= '`' . 'end2' . '`=' . clb_escape($_REQUEST['end2']) . ',';
                    if ($new_mark) {
                        $query .= '`' . 'pnum' . '`=' . clb_escape($pnum) . ',';
                        $query .= '`' . 'ptype' . '`=' . clb_escape($ptype) . ',';
                        if (isset($def['created'])) {
                            $query .= '`' . 'created' . '`=' . clb_escape(clb_now_utc()) . ',';
                        }
                        $query = 'INSERT INTO ' . $table . ' SET ' . trim($query, ', ');
                    } else {
                        $query = 'UPDATE ' . $table . ' SET ' . trim($query, ', ') . ' WHERE pnum=' . clb_escape($pnum);
                    }
                    $res = $db->query($query);
                    $xml .= refesh_marker($db, $table, $pnum, $pnum);
                    unset($_REQUEST['pnum']);
                    //clear this so that the new marker does not remove the repositioned marker
                    $pnum = new_pnum($db, $table);
                    //new pnum for marker
                    $new_mark = TRUE;
                }
            }
            $query = '';
            if ('saveline' == $ajax && is_array($points)) {
                $query .= save_line_query($points, $spec);
            }
            foreach ($def as $fld => $type) {
                switch ($fld) {
                    case 'pnum':
                        if ($new_mark) {
                            $query .= '`' . $fld . '`=' . clb_escape($pnum) . ',';
                        }
                        break;
                    case 'created':
                        if ($new_mark) {
                            $query .= '`' . $fld . '`=' . clb_escape(clb_now_utc()) . ',';
                        }
                        break;
                    case 'ptype':
                        $query .= '`' . $fld . '`=' . clb_escape($ptype) . ',';
                        break;
                    default:
                        if (isset($_REQUEST[$fld])) {
                            $query .= '`' . $fld . '`=' . clb_escape($_REQUEST[$fld]) . ',';
                        }
                        break;
                }
            }
            qlog(__LINE__, $query);
            if ($new_mark) {
                $query = 'INSERT INTO ' . $table . ' SET ' . trim($query, ', ');
            }
            if (!$new_mark) {
                $query = 'UPDATE ' . $table . ' SET ' . trim($query, ', ') . ' WHERE pnum=' . clb_escape($pnum);
            }
            $res = $db->query($query);
            //when saving a marker remember the type to be used as the default for next new marker.
            $name = clb_val(FALSE, $spec, 'polyline') ? 'type_line' : 'type_mark';
            $xml .= clb_tag('response', '', $ptype, array('type' => 'state', 'pnum' => $name)) . "\n";
            //pnum is the "id" name is the "id" of the state we are setting
            $xml .= refesh_marker($db, $table, $pnum, clb_val(FALSE, $_REQUEST, 'pnum'));
            //want to fall through to show a bubble if saving a line
            //so break when either condition fails
            if ('saveline' == $ajax) {
                $xml .= marker_bubble($db, $spec, $table, $pnum);
            }
            break;
        case 'bubble':
            if (!$spec || !$pnum) {
                $msg = 'could not identify the marker type.';
                break;
            }
            $xml .= marker_bubble($db, $spec, $table, $pnum);
            break;
        case 'delete':
            $query = 'DELETE FROM ' . $table . ' WHERE pnum=' . clb_escape($pnum);
            $sel = $db->query($query);
            //remove old marker, which closes info box
            $xml .= clb_tag('response', '', '', array('type' => 'remove', 'pnum' => $_REQUEST['pnum'])) . "\n";
            break;
    }
    if ($ajax) {
        if ($msg) {
            $xml .= clb_tag('response', $msg, '', array('type' => 'info')) . "\n";
        }
        if (!$xml) {
            $xml .= clb_tag('response', '', '', array('type' => 'null')) . "\n";
        }
        // qlog(__LINE__, $xml);
        clb_response('xml', $xml);
    }
}
Example #8
0
 $stop_pnum = isset($p2p_links['plat2stat'][$node1]) ? $p2p_links['plat2stat'][$node1] : $node1;
 //0=pnum, 1=dist from last station to this one
 $stoplist['pnum'][] = $stop_pnum;
 $stoplist['dist'][] = 0;
 //loop to build stop list with real stations not platforms and with distances
 foreach ($details as $linkpnum => $info) {
     if (!isset($p2p_links['links'][$linkpnum])) {
         qpre('flatten - link record not found', $linkpnum, $pnum1, $pnum2);
         break;
     } else {
         parse_str($p2p_links['links'][$linkpnum], $linkrec);
         //get link rec by linkpnum
         $ptype = clb_val(0, $linkrec, 'ptype');
         $dist = 0;
         if ($ptype != 'walk') {
             $dist = clb_val(0, $linkrec, 'dist');
         }
         $next = isset($linkrec['end1']) && $node1 == $linkrec['end1'] ? $linkrec['end2'] : $linkrec['end1'];
         $stop_pnum = isset($p2p_links['plat2stat'][$next]) ? $p2p_links['plat2stat'][$next] : $next;
         $stoplist['pnum'][] = $stop_pnum;
         $stoplist['dist'][] = $dist;
         $node1 = $next;
     }
 }
 // now scan all nodes along path adding AB pairs where B>A and B still in $targets
 $dist = 0;
 $query = '';
 for ($combo1 = 1; $combo1 < count($stoplist['pnum']); $combo1++) {
     $B = $stoplist['pnum'][$combo1];
     //these are stations not platforms
     $dist += $stoplist['dist'][$combo1];
Example #9
0
            foreach ($editor_types as $type => $info) {
                if (strlen($type) == 4 && clb_val(FALSE, $info, 'table') == RF_LINKS_FROM) {
                    $types[] = $type;
                }
            }
            $query = 'SELECT ' . RF_LINKS_KEY . ',' . RF_LINKS_POINTS . ' FROM ' . RF_LINKS_FROM . ' WHERE ' . RF_LINKS_TYPE . ' IN ' . clb_join($types, TRUE);
            $sel = $wpdb->get_results($query, ARRAY_A);
            $max = clb_count($sel);
            if ($max) {
                foreach ($sel as $i => $rec) {
                    if ($i % round($max / 10) == 0) {
                        echo round(100 * ($i / $max)) . '% complete' . "\n";
                    }
                    $pnum = clb_val('', $rec, RF_LINKS_KEY);
                    $query = '';
                    $points = pline_pts_arr(clb_val('', $rec, RF_LINKS_POINTS));
                    $pt = reset($points);
                    $query .= ',' . RF_LINKS_END1 . '=' . clb_escape(clb_b64e($pt[0], 10000.0) . clb_b64e($pt[1], 10000.0));
                    $pt = end($points);
                    $query .= ',' . RF_LINKS_END2 . '=' . clb_escape(clb_b64e($pt[0], 10000.0) . clb_b64e($pt[1], 10000.0));
                    $query = 'UPDATE ' . RF_LINKS_FROM . ' SET ' . trim($query, ', ') . ' WHERE pnum=' . clb_escape($pnum);
                    $wpdb->query($query);
                }
            }
            break;
        case 'blowup':
            $path = dirname(__FILE__);
            require_once $path . '/blowup.php';
            break;
    }
} else {