Changeset 56

Show
Ignore:
Timestamp:
04/14/10 08:50:14 (2 years ago)
Author:
brian
Message:

Added summary view to webtracker of all routes. Modified database tables to take a range in a where clause as a value argument to do a WHERE x IN (y1, y2, yx) clause.

Files:
7 modified

Legend:

Unmodified
Added
Removed
  • application/webtracker/controller/user.php

    r53 r56  
    4646         $trip = $tripModel->getById($this->_helper->getParam('trip', 0)); 
    4747          
    48          $mintime = $this->_helper->getParam('mintime', 0); 
    49          $maxtime = $this->_helper->getParam('maxtime', 0); 
    50  
    51          if ($trip === null || $trip->user != $user->id) { 
     48         if (count($trip) == 0) { 
    5249             nickelException ('No such trip!', 'bad-route'); die(); 
    5350         } 
    5451          
     52         $mintime = $this->_helper->getParam('mintime', 0); 
     53         $maxtime = $this->_helper->getParam('maxtime', 0); 
     54          
    5555         $this->_view->user = $user; 
    5656         $this->_view->trip = $trip; 
     57         $this->_view->tripraw = $this->_helper->getParam('trip', 0); 
    5758         $this->_view->mintime = $mintime; 
    5859         $this->_view->maxtime = $maxtime; 
     60         $this->_view->nospeed = $this->_helper->getParam('nospeed', 'false'); 
    5961    } 
     62     
     63     
     64    function summaryAction() { 
     65          
     66         $userModel = $this->_helper->model('Database_User'); 
     67         $tripModel = $this->_helper->model('Database_Trip'); 
     68          
     69         $username = $this->_helper->getParam('username', false); 
     70          
     71         if ($username === false) { 
     72             nickelException ('No such user!', 'bad-route'); die(); 
     73         } 
     74          
     75         $user = $userModel->getByUser($username); 
     76          
     77         if ($user === null) { 
     78             nickelException ('No such user!', 'bad-route'); die(); 
     79         } 
     80          
     81         $lowspeedTrips = $user->getLowspeedTrips(); 
     82 
     83         $url = array('module' => 'webtracker',  
     84                      'controller' => 'user',  
     85                      'action' => 'tripDetails',  
     86                      'username' => $username,  
     87                      'trip' => implode(',', $lowspeedTrips),  
     88                      'nospeed' => 'true'); 
     89          
     90         $this->redirect($url); 
     91    } 
     92 
    6093     
    6194    function tripDetailsAjaxAction() { 
     
    79112        } 
    80113 
    81         $trip = $tripModel->getById($this->_helper->getParam('trip', 0)); 
    82         $mintime = $this->_helper->getParam('mintime', 0); 
    83         $maxtime = $this->_helper->getParam('maxtime', 0); 
    84  
    85         if ($trip === null || $trip->user != $user->id) { 
    86              // TODO:  JSON error 
    87              nickelException ('No such trip!', 'bad-route'); die(); 
    88         } 
    89          
    90         $nelat = $this->_helper->getParam('nelat', false); 
    91         $nelng = $this->_helper->getParam('nelng', false); 
    92         $swlat = $this->_helper->getParam('swlat', false); 
    93         $swlng = $this->_helper->getParam('swlng', false); 
    94         $latdif = $nelat-$swlat; 
    95         $lngdif = $nelng-$swlng; 
    96         $latdif7 = abs($latdif / 7); 
    97         $lngdif7 = abs($lngdif / 7); 
    98          
    99         $data = $trip->getPointsByBounds($swlat - $latdif7, $swlng - $lngdif7, $nelat + $latdif7, $nelng + $lngdif7, 160, $mintime, $maxtime); 
    100          
    101         if(!isset($data[0])) { 
    102             $dataOutput['segments'] = array(); 
    103  
    104             ob_end_clean(); 
    105             echo(json_encode($dataOutput)); 
    106             die(); 
    107         } 
    108          
    109         $mintime = $data[0]['time']; 
    110         $maxtime = $data[count($data)-1]['time']; 
    111         $timeThatCountsAsAPause = (($maxtime - $mintime) / count($data)) * 4; 
    112         if ($timeThatCountsAsAPause < 60) $timeThatCountsAsAPause = 80; 
     114        $trips = explode(',', $this->_helper->getParam('trip', 0)); 
    113115         
    114116        $dataOutput = array(); 
     
    117119        $markers = array(); 
    118120         
    119         $pointsPerMarker = ((int)count($data))/5; 
    120          
    121         foreach ($data as $index => $point) { 
    122             if (count($segment) != 0 && $segment[count($segment)-1]['time'] < $point['time'] - $timeThatCountsAsAPause) { 
    123                 $segments[] = $segment; 
    124                 $segment = array(); 
     121        foreach ($trips as $tripid) { 
     122            
     123            $trip = $tripModel->getById($tripid); 
     124            $mintime = $this->_helper->getParam('mintime', 0); 
     125            $maxtime = $this->_helper->getParam('maxtime', 0); 
     126 
     127            if ($trip === null || $trip->user != $user->id) { 
     128                 // TODO:  JSON error 
     129                 nickelException ('No such trip!', 'bad-route'); die(); 
    125130            } 
    126             $segment[] = $point; 
    127             if ($index % $pointsPerMarker == 0 || $index == count($data) - 1) { 
    128                 $markers[] = array($point, $this->_helper->url(array('module'=>'webtracker','controller'=>'index','action'=>'generateMarkerSpeed','speed'=>$point['speed'],'time'=>$point['time']))); 
     131 
     132            $nelat = $this->_helper->getParam('nelat', false); 
     133            $nelng = $this->_helper->getParam('nelng', false); 
     134            $swlat = $this->_helper->getParam('swlat', false); 
     135            $swlng = $this->_helper->getParam('swlng', false); 
     136            $latdif = $nelat-$swlat; 
     137            $lngdif = $nelng-$swlng; 
     138            $latdif7 = abs($latdif / 7); 
     139            $lngdif7 = abs($lngdif / 7); 
     140 
     141            $data = $trip->getPointsByBounds($swlat - $latdif7, $swlng - $lngdif7, $nelat + $latdif7, $nelng + $lngdif7, 160, $mintime, $maxtime); 
     142 
     143            if(!isset($data[0])) { 
     144                continue; 
    129145            } 
     146 
     147            $mintime = $data[0]['time']; 
     148            $maxtime = $data[count($data)-1]['time']; 
     149            $timeThatCountsAsAPause = (($maxtime - $mintime) / count($data)) * 4; 
     150            if ($timeThatCountsAsAPause < 60) $timeThatCountsAsAPause = 80; 
     151 
     152            $pointsPerMarker = ((int)count($data))/5; 
     153 
     154            foreach ($data as $index => $point) { 
     155                if (count($segment) != 0 && $segment[count($segment)-1]['time'] < $point['time'] - $timeThatCountsAsAPause) { 
     156                    $segments[] = $segment; 
     157                    $segment = array(); 
     158                } 
     159                $segment[] = $point; 
     160                 
     161                if ($this->_helper->getParam('nospeed', 'false') == 'false') { 
     162                    if ($index % $pointsPerMarker == 0 || $index == count($data) - 1) { 
     163                        $markers[] = array($point, $this->_helper->url(array('module'=>'webtracker','controller'=>'index','action'=>'generateMarkerSpeed','speed'=>$point['speed'],'time'=>$point['time']))); 
     164                    } 
     165                } 
     166            } 
     167 
     168            $segments[] = $segment; 
    130169        } 
    131          
    132         $segments[] = $segment; 
    133          
     170 
    134171        $dataOutput['segments'] = $segments; 
    135172        $dataOutput['markers'] = $markers; 
    136          
     173 
    137174        ob_end_clean(); 
    138175        echo(json_encode($dataOutput)); 
     
    140177          
    141178    } 
     179     
     180     
    142181} 
  • application/webtracker/model/database/trip.php

    r53 r56  
    44 
    55    public function __construct() { 
    6         parent::__construct('webtracker_trip', 'id', 'Webtracker_Model_Database_Trip_Row'); 
     6        parent::__construct('webtracker_trip', 'id', 'Webtracker_Model_Database_Trip_Row', 'Webtracker_Model_Database_Trip_Resultset'); 
    77    } 
    88     
     
    1010        return $this->aggregate('distance', 'sum', array('date' => $day, 'user' => $user)); 
    1111    } 
     12} 
     13 
     14class Webtracker_Model_Database_Trip_Resultset extends Database_Resultset { 
    1215     
    13  
     16    public function getGoogleMapsBoundsString() { 
     17        $minlat = false; 
     18        $minlng = false; 
     19        $maxlat = false; 
     20        $maxlng = false; 
     21         
     22        foreach ($this as $row) { 
     23            $bounds = $row->getGoogleMapsBounds(); 
     24            if ($minlat === false || (float)$minlat > (float)$bounds['minlat']) $minlat = $bounds['minlat']; 
     25            if ($minlng === false || (float)$minlng > (float)$bounds['minlng']) $minlng = $bounds['minlng']; 
     26            if ($maxlat === false || (float)$maxlat < (float)$bounds['maxlat']) $maxlat = $bounds['maxlat']; 
     27            if ($maxlng === false || (float)$maxlng < (float)$bounds['maxlng']) $maxlng = $bounds['maxlng']; 
     28        } 
     29         
     30        return 'new GLatLngBounds(new GLatLng('.$minlat.', '.$minlng.'), new GLatLng('.$maxlat.', '.$maxlng.'))'; 
     31    } 
    1432} 
    1533 
    1634class Webtracker_Model_Database_Trip_Row extends Database_Row { 
    1735     
     36    public function getGoogleMapsBounds() { 
     37         // This code sucks, and only works when you're only on one specific quadrent of the world for the entire trip. 
     38            $pointModel = $this->_helper->model('Database_Point'); 
     39 
     40            $aggregateBounds = $this->_db->queryFetchRow($pointModel->select(array( 
     41                    'min(lat)' => 'minlat', 
     42                    'min(lon)' => 'minlng', 
     43                    'max(lat)' => 'maxlat', 
     44                    'max(lon)' => 'maxlng', 
     45                ))->where(array('trip' => $this->id))); 
     46         
     47            return $aggregateBounds; 
     48    } 
     49     
    1850    public function getGoogleMapsBoundsString() { 
    19         // This code sucks, and only works when you're only on one specific quadrent of the world for the entire trip. 
    20         $pointModel = $this->_helper->model('Database_Point'); 
    21          
    22         $aggregateBounds = $this->_db->queryFetchRow($pointModel->select(array( 
    23                 'min(lat)' => 'minlat', 
    24                 'min(lon)' => 'minlng', 
    25                 'max(lat)' => 'maxlat', 
    26                 'max(lon)' => 'maxlng', 
    27             ))->where(array('trip' => $this->id))); 
     51        
     52        $aggregateBounds = $this->getGoogleMapsBounds(); 
    2853         
    2954        return 'new GLatLngBounds(new GLatLng('.$aggregateBounds['minlat'].', '.$aggregateBounds['minlng'].'), new GLatLng('.$aggregateBounds['maxlat'].', '.$aggregateBounds['maxlng'].'))'; 
  • application/webtracker/model/database/user.php

    r42 r56  
    44 
    55    public function __construct() { 
    6         parent::__construct('webtracker_user', 'id'); 
     6        parent::__construct('webtracker_user', 'id', 'Webtracker_Model_Database_User_Row'); 
    77    } 
    88 
    99} 
     10 
     11 
     12class Webtracker_Model_Database_User_Row extends Database_Row { 
     13    
     14     
     15    public function getLowspeedTrips() { 
     16        $tripModel = $this->_helper->model('Database_Trip'); 
     17        $pointModel = $this->_helper->model('Database_Point'); 
     18 
     19        $tripsSlow = array(); 
     20         
     21        $trips = $tripModel->fetchAll(array('user = ?' => $this->id)); 
     22        foreach ($trips as $trip) { 
     23            if ($pointModel->aggregate('id', 'count', array('trip' => $trip->id, 'speed > 40')) == 0) { 
     24                $tripsSlow[] = $trip->id; 
     25            } 
     26        } 
     27         
     28        return $tripsSlow; 
     29    } 
     30  
     31     
     32    public function getGoogleMapsBoundsString() { 
     33        // This code sucks, and only works when you're only on one specific quadrent of the world for the entire trip. 
     34        $pointModel = $this->_helper->model('Database_Point'); 
     35 
     36        $aggregateBounds = $this->_db->queryFetchRow($pointModel->select(array( 
     37                'min(lat)' => 'minlat', 
     38                'min(lon)' => 'minlng', 
     39                'max(lat)' => 'maxlat', 
     40                'max(lon)' => 'maxlng', 
     41            ))->where(array('trip' => $this->id))); 
     42 
     43        return 'new GLatLngBounds(new GLatLng('.$aggregateBounds['minlat'].', '.$aggregateBounds['minlng'].'), new GLatLng('.$aggregateBounds['maxlat'].', '.$aggregateBounds['maxlng'].'))'; 
     44    } 
     45 
     46} 
     47 
  • application/webtracker/view/html/user/trip-details.phtml

    r53 r56  
    1010        <?php echo $this->addElement(new Element_AjaxGoogleMaps('map', '',  
    1111            array( 
    12                 'overlayService' => array('module'=>'webtracker','controller'=>'user','action'=>'tripDetailsAjax','username'=>$this->user->user,'trip'=>$this->trip->id,'mintime'=>$this->mintime,'maxtime'=>$this->maxtime), 
     12                'overlayService' => array('module'=>'webtracker','controller'=>'user','action'=>'tripDetailsAjax','username'=>$this->user->user,'trip'=>$this->tripraw,'mintime'=>$this->mintime,'maxtime'=>$this->maxtime,'nospeed'=>$this->nospeed), 
    1313                'initialLocation' => $this->trip->getGoogleMapsBoundsString(), 
    1414                'initialMode' => 'G_NORMAL_MAP' 
  • libraries/Nickel_Core/Controller.php

    r28 r56  
    6161     */ 
    6262    public function redirect($uri) { 
    63         header ('Location: '.$this->_helper->baseUrl().$uri); 
     63        header ('Location: '.$this->_helper->url($uri)); 
     64        die(); 
    6465    } 
    6566     
  • libraries/Nickel_Core/Database/Querybuilder/All.php

    r47 r56  
    112112                    $this->_clauses['WHERE'][] = ' '.$value.' '; 
    113113                } else { 
    114                     $id = ':pdoval'.count($this->_values); 
    115                     $this->_values[$id] = $value; 
    116114                    if (strpos($key, '?') === false) { 
    117115                        //array('column', 'value') 
    118                         $this->_clauses['WHERE'][] = ' '.$key.' = '.$id.' '; 
     116                        if (is_array($value)) { 
     117                            //array('column', array('value1', 'value2', 'value3')) 
     118                            $inclause = array(); 
     119                            foreach ($value as $v) { 
     120                                $id = ':pdoval'.count($this->_values); 
     121                                $this->_values[$id] = $v; 
     122                                $inclause[] = $id; 
     123                            } 
     124                            if (count($inclause) == 0) { 
     125                                $this->_clauses['WHERE'][] = ' FALSE '; 
     126                            } else { 
     127                                $this->_clauses['WHERE'][] = ' '.$key.' IN ('.implode(',', $inclause).') '; 
     128                            } 
     129                        } else { 
     130                            $id = ':pdoval'.count($this->_values); 
     131                            $this->_values[$id] = $value; 
     132                            $this->_clauses['WHERE'][] = ' '.$key.' = '.$id.' '; 
     133                        } 
    119134                    } else { 
    120135                        //array('column = ?', 'value') 
     136                        $id = ':pdoval'.count($this->_values); 
     137                        $this->_values[$id] = $value; 
    121138                        $this->_clauses['WHERE'][] = ' '.str_replace('?', $id, $key).' '; 
    122139                    } 
  • libraries/Nickel_Core/Database/Table.php

    r45 r56  
    66    public $_primaryKey; 
    77    public $_rowClass; 
     8    public $_resultSetClass; 
    89    public $_helper; 
    910    public $_db; 
    1011     
    11     public function __construct($table = '', $primaryKey = '', $rowClass = '') { 
     12    public function __construct($table = '', $primaryKey = '', $rowClass = '', $resultSetClass = '') { 
    1213        global $nickelGlobals; 
    1314         
     
    1617        $this->_primaryKey = $primaryKey; 
    1718        $this->_rowClass = $rowClass; 
     19        $this->_resultSetClass = $resultSetClass; 
    1820        $this->_db = new Database(); 
    1921    } 
     
    4749     */ 
    4850    public function _createResultSet($array) { 
    49         return new Database_Resultset($this, $array); 
     51        if ($this->_resultSetClass == '') { 
     52            return new Database_Resultset($this, $array); 
     53        } else { 
     54            return new $this->_resultSetClass($this, $array); 
     55        } 
    5056    } 
    5157     
     
    118124        $query = $this->_db->queryBuilder() 
    119125            ->select($this->_table) 
    120             ->where($column, $value); 
     126            ->where(array($column => $value)); 
    121127         
    122128        return $this->_createResultSet($this->_db->queryFetchAll($query));