Changeset 57418

Show
Ignore:
Timestamp:
04/07/08 16:40:13 (5 months ago)
Author:
ischommer
Message:

adapted to DataFormatter? and RestfulServer? API changes

Location:
modules/marketdemandmap/trunk
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • modules/marketdemandmap/trunk/code/controller/DemandPointRestfulServer.php

    r56979 r57418  
    1111        } 
    1212         
    13         protected function search($className, $params = null, $sort = null, $limit = null, $existingQuery = null) { 
    14                  
    15                 if(singleton($className)->hasMethod('getRestfulSearchContext')) { 
    16                         $searchContext = singleton($className)->{'getRestfulSearchContext'}(); 
    17                 } else { 
    18                         $searchContext = singleton($className)->getDefaultSearchContext(); 
    19                 } 
    20  
    21                 if(isset($params['clustered']) && $params['clustered']) { 
    22                         // we need bounds to cluster onto, otherwise its kinda pointless 
    23                         if(!isset($params['Bounds']) || !singleton('LatLngBoundsFilter')->isValid($params['Bounds'])) return false; 
    24                         // don't apply limit clause, we'll do this later in clustering 
    25                         $query = $searchContext->getQuery($params, $sort, null, $existingQuery); 
    26                          
    27                         return $this->cluster($query, $params['Bounds']); 
    28                 } else { 
    29                         $query = $searchContext->getQuery($params, $sort, $limit, $existingQuery); 
    30                         if( 
    31                                 (isset($limit['limit']) && $limit['limit'] > DemandPoint::$default_cluster_markercount) 
    32                                 || (!isset($limit['limit']) && $query->unlimitedRowCount() > DemandPoint::$default_cluster_markercount) 
    33                                 && isset($params['Bounds']) 
    34                         ) { 
    35                                 $query = $searchContext->getQuery($params, $sort, null, $existingQuery); 
    36                                 return $this->cluster($query, $params['Bounds']); 
    37                         } else { 
    38                                 return singleton($className)->buildDataObjectSet($query->execute()); 
    39                         } 
    40                 } 
     13        protected function needsCluster($query, $params, $sort, $limit) { 
     14                return ( 
     15                        (isset($limit['limit']) && $limit['limit'] > DemandPoint::$default_cluster_markercount) 
     16                        || (!isset($limit['limit']) && $query->unlimitedRowCount() > DemandPoint::$default_cluster_markercount) 
     17                        && isset($params['Bounds']) 
     18                ); 
    4119        } 
    4220         
     
    204182        } 
    205183         
    206         /** 
    207          * HACK to set custom fields if clustering is necessary 
    208          * as we're requiring different fields outside of the normal $db 
    209          * array - and can't anticipate in the RESTful request 
    210          * if those cluster fields are needed. 
    211          *  
    212          * @todo Need a way to determine custom fields on a by-result and by-class basis. 
    213          */ 
    214         protected function getHandler($className, $id, $relation) { 
     184        protected function getHandler($className, $id, $relationName) { 
    215185                $sort = array( 
    216186                        'sort' => $this->request->getVar('sort'), 
     
    222192                ); 
    223193                 
     194                $params = $this->request->getVars(); 
     195                 
    224196                $responseFormatter = $this->getResponseDataFormatter(); 
    225197                if(!$responseFormatter) return $this->unsupportedMediaType(); 
    226198                 
     199                // $obj can be either a DataObject or a DataObjectSet, 
     200                // depending on the request 
    227201                if($id) { 
    228                         $obj = DataObject::get_by_id($className, $id); 
     202                        // Format: /api/v1/<MyClass>/<ID> 
     203                        $query = $this->getObjectQuery($className, $id, $params); 
     204                        $obj = singleton($className)->buildDataObjectSet($query->execute()); 
    229205                        if(!$obj) return $this->notFound(); 
     206                        $obj = $obj->First(); 
    230207                        if(!$obj->canView()) return $this->permissionFailure(); 
    231                          
    232                         if($relation) { 
    233                                 if($relationClass = $obj->many_many($relation)) { 
    234                                         $query = $obj->getManyManyComponentsQuery($relation); 
    235                                 } elseif($relationClass = $obj->has_many($relation)) { 
    236                                         $query = $obj->getComponentsQuery($relation); 
    237                                 } elseif($relationClass = $obj->has_one($relation)) { 
    238                                         $query = null; 
    239                                 } elseif($obj->hasMethod("{$relation}Query")) { 
    240                                         // @todo HACK Switch to ComponentSet->getQuery() once we implement it (and lazy loading) 
    241                                         $query = $obj->{"{$relation}Query"}(null, $sort, null, $limit); 
    242                                         $relationClass = $obj->{"{$relation}Class"}(); 
     208 
     209                        // Format: /api/v1/<MyClass>/<ID>/<Relation> 
     210                        if($relationName) { 
     211                                $query = $this->getObjectRelationQuery($obj, $params, $sort, $limit, $relationName); 
     212                                if($query === false) return $this->notFound(); 
     213                                if($this->needsCluster($query, $params, $sort, $limit)) { 
     214                                        $obj = $this->cluster($query, $params['Bounds']); 
    243215                                } else { 
    244                                         return $this->notFound(); 
     216                                        $searchQuery = $this->getSearchQuery($className, $params, $sort, $limit); 
     217                                        $obj = singleton($className)->buildDataObjectSet($searchQuery->execute()); 
    245218                                } 
    246                                  
    247                                 // get all results 
    248                                 $obj = $this->search($relationClass, $this->request->getVars(), $sort, $limit, $query); 
    249                                 if(!$obj) $obj = new DataObjectSet(); 
    250                         } 
     219                        }  
     220                         
    251221                } else { 
    252                         $obj = $this->search($className, $this->request->getVars(), $sort, $limit); 
     222                        // Format: /api/v1/<MyClass> 
     223                        $query = $this->getObjectsQuery($className, $params, $sort, $limit); 
     224                        if($this->needsCluster($query, $params, $sort, $limit)) { 
     225                                $obj = $this->cluster($query, $params['Bounds']); 
     226                        } else { 
     227                                $searchQuery = $this->getSearchQuery($className, $params, $sort, $limit); 
     228                                $obj = singleton($className)->buildDataObjectSet($searchQuery->execute()); 
     229                        } 
     230 
    253231                        // show empty serialized result when no records are present 
    254232                        if(!$obj) $obj = new DataObjectSet(); 
     233                } 
     234                 
     235                $this->getResponse()->addHeader('Content-Type', $responseFormatter->getOutputContentType()); 
     236 
     237                if($obj instanceof DataObjectSet) { 
     238                        $responseFormatter->setTotalSize($query->unlimitedRowCount()); 
    255239                         
    256240                        // HACK Customization (see method comment) 
     
    264248                                ));  
    265249                        } 
    266                 } 
    267                  
    268                 $this->getResponse()->addHeader('Content-Type', $responseFormatter->getOutputContentType()); 
    269                  
    270                 if($obj instanceof DataObjectSet) return $responseFormatter->convertDataObjectSet($obj); 
    271                 else return $responseFormatter->convertDataObject($obj); 
    272         } 
    273          
     250                         
     251                        return $responseFormatter->convertDataObjectSet($obj); 
     252                } else { 
     253                        return $responseFormatter->convertDataObject($obj); 
     254                } 
     255        } 
     256                         
    274257        protected function getResponseDataFormatter() { 
    275258                $responseFormatter = $this->getDataFormatter(true); 
  • modules/marketdemandmap/trunk/javascript/demandmap.DemandCategoryControl.js

    r57365 r57418  
    8383                                                        _setLoading(false, i); 
    8484                                                }); 
    85                                                 $map.fn('addPoints', markersJson); 
     85                                                $map.fn('addPoints', markersJson.items); 
    8686                                                 
    87                                                 $this.trigger('DemandCategoryControl:loadcategories', [categoryIDs, markersJson]); 
     87                                                $this.trigger('DemandCategoryControl:loadcategories', [categoryIDs, markersJson.items]); 
    8888                                        } 
    8989                                ); 
  • modules/marketdemandmap/trunk/javascript/demandmap.DemandPointList.js

    r57410 r57418  
    1616                // config 
    1717                var defaults = { 
    18                          
     18                        'pageSize': 20 
    1919                }; 
    2020                 
     
    5555                        // ######## public methods ######### 
    5656                        $this.fn({ 
    57                                 'refresh' : function(force) { 
     57                                'refresh' : function(force, url) { 
    5858                                        if(!$this.is(':visible') && !force) return false; 
    5959                                         
    60                                         var url = demandControl.fn('getDataURL'); 
     60                                        if(!url) url = demandControl.fn('getDataURL'); 
    6161 
    6262                                        // don't set this above the cluster threshold, 
    6363                                        // or the clustering will kick in 
    64                                         url += '&limit=40'; 
     64                                        url += '&limit=' + options.pageSize; 
    6565 
    6666                                        $.getJSON( 
     
    8989                                // body definition 
    9090                                html += '<tbody>'; 
    91                                 for(var i=0; i<markers.length; i++) { 
    92                                         var m = markers[i]; 
    93 console.debug(markers[i]); 
     91                                for(var i=0; i<markers.items.length; i++) { 
     92                                        var m = markers.items[i]; 
    9493                                        html +=  
    9594                                                '<tr class="vcard">' 
     
    115114                                html += '</table>'; 
    116115                                 
     116                                // pagination 
     117                                html += '<ul class="pagination">'; 
     118                                html += '<li>'; 
     119                                html += '<a href="#"></a>'; 
     120                                html += '</li>'; 
     121                                html += '</ul>'; 
     122                                 
    117123                                // insert html 
    118124                                $this.html(html);