Пример #1
0
 /**
  * Handles GET requests for this adapter. Overridable. Does nothing if not overridden.
  */
 public function HandleGet($single)
 {
     $reader = null;
     $related = new MgRelatedFeaturesSet();
     $smarty = new Smarty();
     $smarty->setCompileDir($this->app->config("Cache.RootDir") . "/templates_c");
     //$smarty->setCaching(false);
     try {
         $output = "";
         $query = $this->CreateQueryOptions($single);
         $reader = $this->featSvc->SelectFeatures($this->featureSourceId, $this->className, $query);
         if ($single === true) {
             //Have to advance the read to initialize the record
             if ($reader->ReadNext()) {
                 $this->LoadRelatedFeatures($reader, $related);
                 $smarty->assign("model", new MgFeatureModel(new MgFormatterSet($this->app), $reader, $this->transform));
                 $smarty->assign("related", $related);
                 $smarty->assign("helper", new MgTemplateHelper($this->app));
                 $output = $smarty->fetch($this->singleViewPath);
             } else {
                 $this->app->response->setStatus(404);
                 $smarty->assign("single", $single);
                 $smarty->assign("ID", $this->featureId);
                 $smarty->assign("helper", new MgTemplateHelper($this->app));
                 $output = $smarty->fetch($this->noneViewPath);
             }
         } else {
             $start = -1;
             $end = -1;
             $read = 0;
             $limit = $this->limit;
             $pageNo = $this->app->request->get("page");
             if ($pageNo == null) {
                 $pageNo = 1;
             } else {
                 $pageNo = intval($pageNo);
             }
             $bNoResults = false;
             $firstRecord = null;
             $bEndOfReader = false;
             if ($this->pageSize > 0) {
                 if ($pageNo > 1) {
                     $skipThisMany = ($pageNo - 1) * $this->pageSize - 1;
                     //echo "skip this many: $skipThisMany<br/>";
                     $bEndOfReader = true;
                     while ($reader->ReadNext()) {
                         if ($read == $skipThisMany) {
                             $bEndOfReader = false;
                             $limit = min($skipThisMany + $this->pageSize, $this->limit - 1);
                             break;
                         }
                         $read++;
                     }
                     $model = new MgFeatureReaderModel(new MgFormatterSet($this->app), $reader, $limit, $read, $this->transform);
                 } else {
                     //first page, set limit to page size
                     $limit = $this->pageSize;
                     $model = new MgFeatureReaderModel(new MgFormatterSet($this->app), $reader, $limit, $read, $this->transform);
                     //See if this only has one result. If so, then re-route to the single view
                     if ($pageNo == 1) {
                         //Peek() lets us advance the reader for previewing purposes without
                         //compromising the behaviour of Next()
                         if ($model->Peek()) {
                             $firstRecord = $model->Current();
                             //There's actually more, so this isn't a single result
                             if ($model->Peek()) {
                                 $firstRecord = null;
                             }
                         } else {
                             $bNoResults = true;
                         }
                     }
                 }
             }
             if ($firstRecord != null) {
                 $this->LoadRelatedFeatures($firstRecord->ReaderFacade(), $related);
                 $smarty->assign("model", $firstRecord);
                 $smarty->assign("related", $related);
                 $smarty->assign("helper", new MgTemplateHelper($this->app));
                 $output = $smarty->fetch($this->singleViewPath);
             } else {
                 if ($bNoResults) {
                     //Query produced 0 results
                     $this->app->response->setStatus(404);
                     $smarty->assign("single", $single);
                     $smarty->assign("helper", new MgTemplateHelper($this->app));
                     $output = $smarty->fetch($this->noneViewPath);
                 } else {
                     //echo "read: $read, limit: $limit, pageSize: ".$this->pageSize." result limit: ".$this->limit;
                     //die;
                     $smarty->assign("model", $model);
                     $smarty->assign("currentPage", $pageNo);
                     $smarty->assign("endOfReader", $bEndOfReader ? "true" : "false");
                     if ($this->limit > 0) {
                         if ($bEndOfReader) {
                             $smarty->assign("maxPages", $pageNo);
                         } else {
                             $smarty->assign("maxPages", ceil($this->limit / $this->pageSize));
                         }
                     } else {
                         if ($bEndOfReader) {
                             $smarty->assign("maxPages", $pageNo);
                         } else {
                             if ($this->pageSize > 0) {
                                 $smarty->assign("maxPages", -1);
                             } else {
                                 $smarty->assign("maxPages", 1);
                             }
                         }
                     }
                     $smarty->assign("helper", new MgTemplateHelper($this->app));
                     $output = $smarty->fetch($this->manyViewPath);
                 }
             }
         }
         $this->WriteOutput($output);
     } catch (MgException $ex) {
         $err = new stdClass();
         $err->code = get_class($ex);
         $err->message = $ex->GetExceptionMessage();
         $err->stack = sprintf("%s\n======== Native <-> PHP boundary ========\n\n%s", $ex->GetStackTrace(), $ex->getTraceAsString());
         $smarty->assign("error", $err);
         $smarty->assign("helper", new MgTemplateHelper($this->app));
         $this->app->response->write($smarty->fetch($this->errorViewPath));
     } catch (Exception $e) {
         $err = new stdClass();
         $err->code = get_class($e);
         $err->message = $e->getMessage();
         $err->stack = $e->getTraceAsString();
         $smarty->assign("error", $err);
         $smarty->assign("helper", new MgTemplateHelper($this->app));
         $this->app->response->write($smarty->fetch($this->errorViewPath));
     }
     $related->Cleanup();
     if ($reader != null) {
         $reader->Close();
     }
 }
 public function testModelMultiPeekIterationMixture()
 {
     $rdr = new MockReader();
     $model = new MgFeatureReaderModel(new MockFormatterSet(), $rdr, -1, 0);
     //Peek 1st and 2nd record
     $this->assertTrue($model->Peek());
     $this->assertEquals(0, $model->Current()->ID);
     $this->assertTrue($model->Peek());
     $this->assertEquals(1, $model->Current()->ID);
     $this->assertTrue($model->Next());
     $this->assertEquals(0, $model->Current()->ID);
     $this->assertTrue($model->Next());
     $this->assertEquals(1, $model->Current()->ID);
     $this->assertTrue($model->Next());
     $this->assertEquals(2, $model->Current()->ID);
     //Peek 4th record
     $this->assertTrue($model->Peek());
     $this->assertEquals(3, $model->Current()->ID);
     $this->assertTrue($model->Next());
     $this->assertEquals(3, $model->Current()->ID);
     $this->assertTrue($model->Next());
     $this->assertEquals(4, $model->Current()->ID);
     //Peek end of reader
     $this->assertFalse($model->Peek());
     $this->assertFalse($model->Next());
 }