Skip to content

Instantly share code, notes, and snippets.

@ollietb
Created September 24, 2010 08:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ollietb/595077 to your computer and use it in GitHub Desktop.
Save ollietb/595077 to your computer and use it in GitHub Desktop.
<!--
The JCR repository - written in XML for easy reading. actual dumps will look more like this
http://willcode4beer.com/design.jsp?set=gettingStartedWithJCR&pageNum=4
-->
<root>
<!--
these pages are assigned layout and the hierarchy could represent the url structure
instead of the controller attribute, we might want to use custom nt:nodeType and map node types to controllers...
-->
<pages>
<Home_Page controller="Application:MyBlog:home" page_title="Welcome" body="Hello world">
<my_blog controller="Application:MyBlog:list" page_title="My Blog" intro="Welcome to my Blog" orderable_child_nodes="true">
<Other_Entry_2010-09-24 controller="Application:MyBlog:blog" date="2010-09-24T13:01:04.758+02:00">
<sv:property sv:name="blog_image" jcr:nodeType="nt:weak-reference"><sv:value>842e61c0-09ab-42a9-87c0-308ccc90e6f3</sv:value></sv:property>
</Other_Entry_2010-09-24>
<!--
uri for this page is /my_blog/Other_Entry_2010-09-24.html
uri for the image is probably resolved in output and /assets/blog_images/other.jpg
-->
<An_Entry_2010-09-23 controller="Application:MyBlog:blog" date="2010-09-23T13:01:04.758+02:00">
<illustration.png jcr:primaryType="nt:file" alt="Hello World!" jcr:created="2010-09-27T13:01:04.758+02:00">
<jcr:content jcr:primaryType="nt:unstructured" jcr:data="aDEuIENoYXB0ZXIgMSBUaXRsZQoKCiog..." jcr:lastModified="2009-04-27T13:01:07.472+02:00" jcr:mimeType="image/png"/>
</illustration.png>
<!--
user uploaded an image with this post that does not go into repository (well, this is a philosophical question independent of jcr or not)
uri would be /my_blog/An_Entry_2010-09-23/illustration.png
-->
</An_Entry_2010-09-23>
</my_blog>
<about controller="Application:Standardpage" page_title="About us" body="..." />
</Home_Page>
</pages>
<!--
this would be the place to store objects which don't have a specific page, but can appear on many pages eg. internal ads, quick links
we might want to put blog items here if they are aggregated dynamically in several places.
but even then, maybe there is a primary place for the blog and other things like "recent posts" can fetch info from there.
-->
<objects>
<blogs type="folder" name="Blog Entries">
<Blog-Entry-1 body="Hello World!" />
</blogs>
</objects>
<!--
these assets are represented in a folder structure in the cms
this is to create some basic images used everywhere
-->
<assets>
<blog_images jcr:primaryType="nt:folder" name="Blog Images">
<hello.png jcr:uuid="842e61c0-09ab-42a9-87c0-308ccc90e6f4" jcr:primaryType="nt:file" jcr:mixinTypes="mix:referenceable"
alt="Hello World!" jcr:created="2009-04-27T13:01:04.758+02:00">
<jcr:content jcr:primaryType="nt:unstructured" jcr:data="aDEuIENoYXB0ZXIgMSBUaXRsZQoKCiog..."
jcr:lastModified="2009-04-27T13:01:07.472+02:00" jcr:mimeType="image/png"/>
</hello.png>
<other.jpg jcr:uuid="842e61c0-09ab-42a9-87c0-308ccc90e6f3" jcr:primaryType="nt:file" jcr:mixinTypes="mix:referenceable"
alt="Hello World!" jcr:created="2009-04-27T13:01:04.758+02:00">
<jcr:content jcr:primaryType="nt:unstructured" jcr:data="aDEuIENoYXB0ZXIgMSBUaXRsZQoKCiog..."
jcr:lastModified="2009-04-27T13:01:07.472+02:00" jcr:mimeType="image/jpeg"/>
</other.jpg>
</blog_images>
<standard_company:logos jcr:primaryType="nt:folder" name="Standard Company Logos">
</standard_company_logos>
</assets>
</root>
<?php
$repository = $factory->getRepository($config['url']) or die('Failed to connect to server');
$session = $repository->login($config['credentials'],$config['workspace']) or die('Login credentials invalid or something...');
/** Node(versionable="true") */
class Blog
{
/** @Text */
protected $title;
/** @Text */
protected $body;
}
//loading objects
/* @var $firstBlogEntryNode Node */
$firstBlogEntryNode = $session->getNode('/pages/Home_Page/my_blog[0]', array('_version'=>1));
// maps the properties of the node to an object
/* @var $firstBlogEntry Blog */
$firstBlogEntry = $firstBlogEntryNode->getObject('Bundle\CMF\BlogBundle\Blog');
/* @var $firstBlogEntryVersion2 Blog */
$versions = $session->getVersionManager()->getVersionHistory($firstBlogEntryNode->getPath());
$firstBlogEntryVersion2 = $versions[2]->getObject('Bundle\CMF\BlogBundle\Blog');
// template/contoller logic is stored in the page object
/** Node(versionable="true") */
class Page
{
/** @Text */
protected $controller;
/** @Text */
protected $page_title;
public function getTitle()
{
return $this->page_title . ' - www.example.com';
}
}
class BlogListingPage extends Page
{
/** @Text */
protected $intro;
}
class BlogPage extends Page
{
}
//loading a page and overriding the controller to get a specific format
$blogListingPageNode = $session->getNode('/pages/Home_page/my_blog');
$blogListingPage = $cmfManager->getPage($blogListingPageNode, array(
'_controller'=>'Application:MyBlog:list',
'_format'=>'json'));
//this would inject the properties of the my_blog page into the listAction below
class MyBlogController extends Controller
{
/**
* The $node here is the page node at /Home_page/my_blog.html
*/
public function listAction(Node $node)
{
//get the list of blog items
$blogItemNodes = $node->getChildren();
// assign the listing page in case we want to use some methods on the BlogListingPage object (like getTitle)
$blogListingPage = $node->getObject('Bundle\CMF\BlogBundle\BlogListingPage');
foreach($blogItemNodes as $blogNode)
{
$blogItems[$blogNode->getPath()] = $blogNode->getObject('Bundle\CMF\BlogBundle\Blog');
}
return $this->render('Application:MyBlog:list', array('blogItems'=>$blogItems, 'page'=>$blogListingPage, 'intro'=>$node->getProperty('intro'), 'pageTitle'=>$blogListingPage->getTitle()));
}
/**
* The $node here is the page node at /Home_page/my_blog/Other_Entry_2010-09-24.html
*/
public function blogAction($node)
{
// loading the blog into a Blog object
$blogEntry = $node->getObject('Bundle\CMF\BlogBundle\Blog');
$blogChildNodes = $node->getChildren();
// this seems ugly to me - maybe there is a better way of doing it? or maybe using a utility method
// to iterate a NodeCollection, like $blogChildNodes->findByProperty('sv:name', 'blog_image');
foreach($blogChildNodes as $blogChildNode )
{
if($blogChildNode->getProperty('sv:name') == 'blog_image')
{
$blogImages[$blogChildNode->getPath()] = $blogChildNode->getObject('Bundle\CMF\Assets\Image');
}
}
return $this->render('Application:MyBlog:blog', array('blogEntry'=>$blogEntry, 'blogImages'=>$blogImages));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment