Permalink
Browse files

Add ResultList and use it in doIndex()

  • Loading branch information...
1 parent 0bdf3c1 commit b66910c6aade040cc299eb3f0a5349c139319964 @andrerom committed Apr 1, 2012
@@ -18,26 +18,15 @@
* Encapsulates all data from a controller action to be able to generate view and
* for hmvc use be able to figgure out the overall expiry of the full page.
*
- * @todo: Support disposition and ResultList (Result with several models, aka list views)?
- * Aka: Make this abstract and cover all cases? (so impl can have 3 variants?)
- *
- * @property-read object $model
* @property-read string $module
* @property-read string $action
* @property-read string $view
* @property-read string $uri
* @property-read array $params
*/
-class Result extends ValueObject
+abstract class Result extends ValueObject
{
/**
- * The model object for the result
- *
- * @var object
- */
- protected $model;
-
- /**
* The module name of the controller
*
* Used for template name conventions in view handlers.
@@ -114,10 +103,9 @@ class Result extends ValueObject
*/
public function __construct( array $properties = array() )
{
- if ( !isset( $properties['model'] ) || !isset( $properties['module'] ) ||
- !isset( $properties['action'] ) || !isset( $properties['uri'] ) )
- throw new \Exception( 'Properties that must be present: model, module, action and uri' );
+ if ( !isset( $properties['module'] ) || !isset( $properties['action'] ) || !isset( $properties['uri'] ) )
+ throw new \Exception( 'Properties that must be present: module, action and uri' );
parent::__construct( $properties );
}
-}
+}
@@ -0,0 +1,45 @@
+<?php
+/**
+ * MVC\Result class
+ *
+ * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
+ * @copyright Copyright (C) 2009-2012 github.com/andrerom. All rights reserved.
+ * @license http://www.gnu.org/licenses/gpl-3.0.txt GNU General Public License v3
+ * @version //autogentag//
+ */
+
+namespace HiMVC\API\MVC\Values;
+
+use HiMVC\API\MVC\Values\Result;
+
+/**
+ * Result Item object
+ *
+ * @see \HiMVC\API\MVC\Values\Result
+ *
+ * @property-read object $model
+ */
+class ResultItem extends Result
+{
+ /**
+ * The model object for the result
+ *
+ * @var object
+ */
+ protected $model;
+
+ /**
+ * Constructor for Result
+ *
+ * Check presence of model, module, action and uri as they are minimum properties that needs to be set.
+ *
+ * @param array $properties
+ */
+ public function __construct( array $properties = array() )
+ {
+ if ( !isset( $properties['model'] ) )
+ throw new \Exception( 'Properties that must be present: model' );
+
+ parent::__construct( $properties );
+ }
+}
@@ -0,0 +1,58 @@
+<?php
+/**
+ * MVC\Result class
+ *
+ * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
+ * @copyright Copyright (C) 2009-2012 github.com/andrerom. All rights reserved.
+ * @license http://www.gnu.org/licenses/gpl-3.0.txt GNU General Public License v3
+ * @version //autogentag//
+ */
+
+namespace HiMVC\API\MVC\Values;
+
+use HiMVC\API\MVC\Values\Result;
+use HiMVC\API\MVC\Values\ResultItem;
+
+/**
+ * Result Item object
+ *
+ * @see \HiMVC\API\MVC\Values\Result
+ *
+ * @property-read \HiMVC\API\MVC\Values\ResultItem $items[]
+ * @property-read int $count
+ */
+class ResultList extends Result
+{
+ /**
+ * The model objects for the result
+ *
+ * @var \HiMVC\API\MVC\Values\ResultItem[]
+ */
+ protected $items;
+
+ /**
+ * Count of total count in the collection of items
+ *
+ * Given limit and offset use, the toatl amount of items using the attached url might be much larger
+ * then number of $modules on current instance, this property is need to be able to have paging.
+ * Limit and offset values will be part of $params.
+ *
+ * @var int
+ */
+ protected $count;
+
+ /**
+ * Constructor for Result
+ *
+ * Check presence of model, module, action and uri as they are minimum properties that needs to be set.
+ *
+ * @param array $properties
+ */
+ public function __construct( array $properties = array() )
+ {
+ if ( !isset( $properties['items'] ) || !isset( $properties['count'] ) )
+ throw new \Exception( 'Properties that must be present: items, count' );
+
+ parent::__construct( $properties );
+ }
+}
@@ -13,7 +13,10 @@
HiMVC\API\MVC\Values\Request,
HiMVC\Core\MVC\View\ViewDispatcher,
eZ\Publish\API\Repository\Repository,
- HiMVC\API\MVC\Values\Result;
+ HiMVC\API\MVC\Values\ResultItem,
+ HiMVC\API\MVC\Values\ResultList,
+ eZ\Publish\API\Repository\Values\Content\Query,
+ eZ\Publish\API\Repository\Values\Content\Query\Criterion\ParentLocationId;
/**
* Example controller, does no chnages to data atm
@@ -61,7 +64,7 @@ public function doRead( $id, $view = 'full' )
{
$model = $this->repository->getContentService()->loadContent( $id );
- return new Result( array(
+ return new ResultItem( array(
'model' => $model,
'module' => 'content',
'action' => 'read',
@@ -100,7 +103,30 @@ public function doDelete( $id )
*/
public function doIndex()
{
- return __METHOD__ . "()";
+ $query = new Query();
+ $query->criterion = new ParentLocationId( 1 );
+ $searchResult = $this->repository->getContentService()->findContent( $query, array() );
+
+ $resultHash = array(
+ 'items' => array(),
+ 'count' => $searchResult->count,
+ 'module' => 'content',
+ 'action' => 'index',
+ 'uri' => "content/",
+ );
+
+ foreach ( $searchResult->items as $model )
+ {
+ $resultHash['items'][] = new ResultItem( array(
+ 'model' => $model,
+ 'module' => 'content',
+ 'action' => 'read',
+ 'view' => 'line',
+ 'uri' => "content/{$model->contentId}",
+ ) );
+ }
+
+ return new ResultList( $resultHash );
}
}
@@ -0,0 +1,3 @@
+{% for resultItem in result.items %}
+ {{ view( request, resultItem ) }}
+{% endfor %}
@@ -0,0 +1,5 @@
+<h4><a href="{{ link( request, result ) }}">Name: {{ result.model.contentInfo.name }}</a></h4>
+
+
+Line view twig template got Content object with id: {{ result.model.contentId }}
+<a href="{{ link( request, result, true ) }}">Read more</a>
@@ -1,16 +1,23 @@
<!DOCTYPE html>
-<html>
+<html lang="en">
<head>
- <title>{{ result.model.contentId }}</title>
+ <meta charset="utf-8" />
+ <title>Site title</title>
+ <style>
+ section { width:786px; }
+ article { float: left; width: 69%; }
+ aside { float: right; width: 30%; }
+ </style>
</head>
<body>
-
+ <section>
<article>
{{ view( request, result ) }}
</article>
<aside>
{{ dispatch( request.createChild( 'content/4/embed' ) ) }}
</aside>
+ </section>
</body>
</html>
@@ -12,6 +12,8 @@
use HiMVC\API\MVC\Values\Request;
use HiMVC\API\MVC\Values\Result;
+use HiMVC\API\MVC\Values\ResultItem;
+use HiMVC\API\MVC\Values\ResultList;
/**
* ViewDispatcher
@@ -88,13 +90,17 @@ public function layout( Request $request, Result $result )
*/
protected function viewBySource( $source, Request $request, Result $result )
{
+ $params = array(
+ 'metaData' => $result->metaData,
+ 'params' => $result->params
+ );
+
+ if ( $result instanceof ResultItem )
+ $params['model'] = $result->model;
+
$target = $this->getMatchingConditionTarget(
$source,
- array(
- 'metaData' => $result->metaData,
- 'model' => $result->model,
- 'params' => $result->params,
- )
+ $params
);
if ( $target === null )

0 comments on commit b66910c

Please sign in to comment.