Commit 3477d1b6 authored by O'Reilly Media, Inc.'s avatar O'Reilly Media, Inc.
Browse files

Initial commit

parents
## Example files for the title:
# Zukunftssichere TYPO3-Extensions mit Extbase und Fluid, by Jochen Rau
[![Zukunftssichere TYPO3-Extensions mit Extbase und Fluid, by Jochen Rau](http://akamaicovers.oreilly.com/images/9783897219656/cat.gif)](https://www.safaribooksonline.com/library/view/title/9783868993073//)
The following applies to example files from material published by O’Reilly Media, Inc. Content from other publishers may include different rules of usage. Please refer to any additional usage rights explained in the actual example files or refer to the publisher’s website.
O'Reilly books are here to help you get your job done. In general, you may use the code in O'Reilly books in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from our books does not require permission. Answering a question by citing our books and quoting example code does not require permission. On the other hand, selling or distributing a CD-ROM of examples from O'Reilly books does require permission. Incorporating a significant amount of example code from our books into your product's documentation does require permission.
We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN.
If you think your use of code examples falls outside fair use or the permission given here, feel free to contact us at <permissions@oreilly.com>.
Please note that the examples are not production code and have not been carefully testing. They are provided "as-is" and come with no warranty of any kind.
<?php
/***************************************************************
* Copyright notice
*
* (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
* The blog controller for the Blog package
*
* @version $Id:$
* @license http://opensource.org/licenses/gpl-license.php GNU Public License, version 2
*/
class Tx_BlogExample_Controller_BlogController extends Tx_Extbase_MVC_Controller_ActionController {
/**
* @var Tx_BlogExample_Domain_Model_BlogRepository
*/
protected $blogRepository;
/**
* @var Tx_BlogExample_Domain_Model_AdministratorRepository
*/
protected $administratorRepository;
/**
* Initializes the current action
*
* @return void
*/
public function initializeAction() {
$this->blogRepository = t3lib_div::makeInstance('Tx_BlogExample_Domain_Repository_BlogRepository');
$this->administratorRepository = t3lib_div::makeInstance('Tx_BlogExample_Domain_Repository_AdministratorRepository');
}
/**
* Index action for this controller. Displays a list of blogs.
*
* @return string The rendered view
*/
public function indexAction() {
$this->view->assign('blogs', $this->blogRepository->findAll());
}
/**
* Displays a form for creating a new blog
*
* @param Tx_BlogExample_Domain_Model_Blog $newBlog A fresh blog object taken as a basis for the rendering
* @return string An HTML form for creating a new blog
* @dontvalidate $newBlog
*/
public function newAction(Tx_BlogExample_Domain_Model_Blog $newBlog = NULL) {
$this->view->assign('newBlog', $newBlog);
$this->view->assign('administrators', $this->administratorRepository->findAll());
}
/**
* Creates a new blog
*
* @param Tx_BlogExample_Domain_Model_Blog $newBlog A fresh Blog object which has not yet been added to the repository
* @return void
*/
public function createAction(Tx_BlogExample_Domain_Model_Blog $newBlog) {
$this->blogRepository->add($newBlog);
$this->flashMessages->add('Your new blog was created.');
$this->redirect('index');
}
/**
* Edits an existing blog
*
* @param Tx_BlogExample_Domain_Model_Blog $blog The blog to be edited. This might also be a clone of the original blog already containing modifications if the edit form has been submitted, contained errors and therefore ended up in this action again.
* @return string Form for editing the existing blog
* @dontvalidate $blog
*/
public function editAction(Tx_BlogExample_Domain_Model_Blog $blog) {
$this->view->assign('blog', $blog);
$this->view->assign('administrators', $this->administratorRepository->findAll());
}
/**
* Updates an existing blog
*
* @param Tx_BlogExample_Domain_Model_Blog $blog A not yet persisted clone of the original blog containing the modifications
* @return void
*/
public function updateAction(Tx_BlogExample_Domain_Model_Blog $blog) {
$this->blogRepository->update($blog);
$this->flashMessages->add('Your blog has been updated.');
$this->redirect('index');
}
/**
* Deletes an existing blog
*
* @param Tx_BlogExample_Domain_Model_Blog $blog The blog to delete
* @return void
*/
public function deleteAction(Tx_BlogExample_Domain_Model_Blog $blog) {
$this->blogRepository->remove($blog);
$this->flashMessages->add('Your blog has been removed.');
$this->redirect('index');
}
/**
* Deletes an existing blog
*
* @return void
*/
public function deleteAllAction() {
$this->blogRepository->removeAll();
$this->redirect('index');
}
/**
* Creates a several new blogs
*
* @return void
*/
public function populateAction() {
$author = t3lib_div::makeInstance('Tx_BlogExample_Domain_Model_Person', 'Stephen', 'Smith', 'foo.bar@example.com');
for ($blogNumber = 1; $blogNumber < 4; $blogNumber++) {
$blog = $this->getBlog($blogNumber, $author);
$this->blogRepository->add($blog);
}
$this->redirect('index');
}
/**
* Returns a sample blog populated with generic data. It is also an example how to handle objects and repositories in general.
*
* @param int $blogNumber The number of the blog
* @param Tx_BlogExample_Domain_Model_Person $author The author of posts
* @return Tx_BlogExample_Domain_Model_Blog The blog object
*/
private function getBlog($blogNumber, $author) {
$blog = new Tx_BlogExample_Domain_Model_Blog;
$blog->setTitle('Blog #' . $blogNumber);
$blog->setDescription('A blog about TYPO3 extension development.');
$administrator = new Tx_BlogExample_Domain_Model_Administrator();
$administrator->setName('John Doe');
$administrator->setEmail('john.doe@example.com');
$blog->setAdministrator($administrator);
for ($postNumber = 1; $postNumber < 6; $postNumber++) {
$post = new Tx_BlogExample_Domain_Model_Post;
$post->setTitle('The Post #' . $postNumber);
$post->setAuthor($author);
$post->setContent('Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.');
$blog->addPost($post);
$comment = new Tx_BlogExample_Domain_Model_Comment;
$comment->setDate(new DateTime);
$comment->setAuthor('Peter Pan');
$comment->setEmail('peter.pan@example.com');
$comment->setContent('Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.');
$post->addComment($comment);
$comment = new Tx_BlogExample_Domain_Model_Comment;
$comment->setDate(new DateTime('2009-03-19 23:44'));
$comment->setAuthor('John Smith');
$comment->setEmail('john@matrix.org');
$comment->setContent('Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.');
$post->addComment($comment);
$tag = new Tx_BlogExample_Domain_Model_Tag('MVC');
$post->addTag($tag);
$tag = new Tx_BlogExample_Domain_Model_Tag('Domain Driven Design');
$post->addTag($tag);
$post->setBlog($blog);
}
return $blog;
}
/**
* Override getErrorFlashMessage to present
* nice flash error messages.
*
* @return string
*/
protected function getErrorFlashMessage() {
switch ($this->actionMethodName) {
case 'updateAction' :
return 'Could not update the blog:';
case 'createAction' :
return 'Could not create the new blog:';
default :
return parent::getErrorFlashMessage();
}
}
}
?>
\ No newline at end of file
<?php
/***************************************************************
* Copyright notice
*
* (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
* Comments controller for the Blog package
*
* @version $Id:$
* @license http://opensource.org/licenses/gpl-license.php GNU Public License, version 2
*/
class Tx_BlogExample_Controller_CommentController extends Tx_Extbase_MVC_Controller_ActionController {
/**
* Action that adds a comment to a blog post and redirects to single view
*
* @param Tx_BlogExample_Domain_Model_Post $post The post the comment is related to
* @param Tx_BlogExample_Domain_Model_Comment $newComment The comment to create
* @return void
*/
public function createAction(Tx_BlogExample_Domain_Model_Post $post, Tx_BlogExample_Domain_Model_Comment $newComment) {
$post->addComment($newComment);
$this->flashMessages->add('Your new comment was created.');
$this->redirect('show', 'Post', NULL, array('post' => $post));
}
/**
* Deletes an existing comment
*
* @param Tx_BlogExample_Domain_Model_Post $post The post the comment is related to
* @param Tx_BlogExample_Domain_Model_Comment $comment The comment to be deleted
* @return void
*/
public function deleteAction(Tx_BlogExample_Domain_Model_Post $post, Tx_BlogExample_Domain_Model_Comment $comment) {
$post->removeComment($comment);
$this->flashMessages->add('The comment was removed.');
$this->redirect('edit', 'Post', NULL, array('post' => $post, 'blog' => $post->getBlog()));
}
/**
* Deletes all comments of the given post
*
* @param Tx_BlogExample_Domain_Model_Post $post The post the comment is related to
* @return void
*/
public function deleteAllAction(Tx_BlogExample_Domain_Model_Post $post) {
$post->removeAllComments();
$this->flashMessages->add('Comments have been removed.');
$this->redirect('edit', 'Post', NULL, array('post' => $post, 'blog' => $post->getBlog()));
}
/**
* Override getErrorFlashMessage to present
* nice flash error messages.
*
* @return string
*/
protected function getErrorFlashMessage() {
switch ($this->actionMethodName) {
case 'createAction' :
return 'Could not create the new comment:';
case 'deleteAction' :
return 'Could not delete comment:';
case 'createAction' :
return 'Could not delete comments:';
default :
return parent::getErrorFlashMessage();
}
}
}
?>
\ No newline at end of file
<?php
/***************************************************************
* Copyright notice
*
* (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
* The posts controller for the Blog package
*
* @version $Id:$
* @license http://opensource.org/licenses/gpl-license.php GNU Public License, version 2
*/
class Tx_BlogExample_Controller_PostController extends Tx_Extbase_MVC_Controller_ActionController {
/**
* @var Tx_BlogExample_Domain_Model_PostRepository
*/
protected $postRepository;
/**
* Initializes the current action
*
* @return void
*/
public function initializeAction() {
$this->postRepository = t3lib_div::makeInstance('Tx_BlogExample_Domain_Repository_PostRepository');
$this->personRepository = t3lib_div::makeInstance('Tx_BlogExample_Domain_Repository_PersonRepository');
}
/**
* List action for this controller. Displays latest posts
*
* @param Tx_BlogExample_Domain_Model_Blog $blog The blog to show the posts of
* @param string $tag The name of the tag to show the posts for
* @return string
*/
public function indexAction(Tx_BlogExample_Domain_Model_Blog $blog, $tag = NULL) {
if (empty($tag)) {
$posts = $this->postRepository->findRecentByBlog($blog, $this->settings['maxPosts']);
} else {
$tag = urldecode($tag);
$posts = $this->postRepository->findByTagAndBlog($tag, $blog, $this->settings['maxPosts']);
$this->view->assign('tag', $tag);
}
$this->view->assign('blog', $blog);
$this->view->assign('posts', $posts);
}
/**
* Action that displays one single post
*
* @param Tx_BlogExample_Domain_Model_Post $post The post to display
* @param Tx_BlogExample_Domain_Model_Comment $newComment A new comment
* @dontvalidate $newComment
* @return string The rendered view
*/
public function showAction(Tx_BlogExample_Domain_Model_Post $post, Tx_BlogExample_Domain_Model_Comment $newComment = NULL) {
$this->view->assign('post', $post);
$this->view->assign('newComment', $newComment);
}
/**
* Displays a form for creating a new post
*
* @param Tx_BlogExample_Domain_Model_Blog $blog The blog the post belogs to
* @param Tx_BlogExample_Domain_Model_Post $newPost A fresh post object taken as a basis for the rendering
* @return string An HTML form for creating a new post
* @dontvalidate $newPost
*/
public function newAction(Tx_BlogExample_Domain_Model_Blog $blog, Tx_BlogExample_Domain_Model_Post $newPost = NULL) {
$this->view->assign('authors', $this->personRepository->findAll());
$this->view->assign('blog', $blog);
$this->view->assign('newPost', $newPost);
}
/**
* Creates a new post
*
* @param Tx_BlogExample_Domain_Model_Blog $blog The blog the post belogns to
* @param Tx_BlogExample_Domain_Model_Post $newBlog A fresh Blog object which has not yet been added to the repository
* @return void
*/
public function createAction(Tx_BlogExample_Domain_Model_Blog $blog, Tx_BlogExample_Domain_Model_Post $newPost) {
$blog->addPost($newPost);
$newPost->setBlog($blog);
$this->flashMessages->add('Your new post was created.');
$this->redirect('index', NULL, NULL, array('blog' => $blog));
}
/**
* Displays a form to edit an existing post
*
* @param Tx_BlogExample_Domain_Model_Blog $blog The blog the post belogs to
* @param Tx_BlogExample_Domain_Model_Post $post The original post
* @return string Form for editing the existing blog
* @dontvalidate $post
*/
public function editAction(Tx_BlogExample_Domain_Model_Blog $blog, Tx_BlogExample_Domain_Model_Post $post) {
$this->view->assign('authors', $this->personRepository->findAll());
$this->view->assign('blog', $blog);
$this->view->assign('post', $post);
}
/**
* Updates an existing post
*
* @param Tx_BlogExample_Domain_Model_Blog $blog The blog the post belongs to
* @param Tx_BlogExample_Domain_Model_Post $post The existing, unmodified post
* @param Tx_BlogExample_Domain_Model_Post $updatedPost A clone of the original post with the updated values already applied
* @return void
*/
public function updateAction(Tx_BlogExample_Domain_Model_Blog $blog, Tx_BlogExample_Domain_Model_Post $post) {
$this->postRepository->update($post);
$this->flashMessages->add('Your post was updated.');
$this->redirect('index', NULL, NULL, array('blog' => $blog));
}
/**
* Deletes an existing post
*
* @param Tx_BlogExample_Domain_Model_Blog $blog The blog the post belongs to
* @param Tx_BlogExample_Domain_Model_Post $post The post to be deleted
* @return void
*/
public function deleteAction(Tx_BlogExample_Domain_Model_Blog $blog, Tx_BlogExample_Domain_Model_Post $post) {
$this->postRepository->remove($post);
$this->flashMessages->add('Your post was removed.');
$this->redirect('index', NULL, NULL, array('blog' => $blog));
}
}
?>
\ No newline at end of file
<?php
/***************************************************************
* Copyright notice
*
* (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
* An Administrator of a Blog
*
* @version $Id:$
*/
class Tx_BlogExample_Domain_Model_Administrator extends Tx_Extbase_Domain_Model_FrontendUser {
}
?>
\ No newline at end of file
<?php
/***************************************************************
* Copyright notice
*
* (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
* A blog
*
* @version $Id:$
* @copyright Copyright belongs to the respective authors
* @license http://opensource.org/licenses/gpl-license.php GNU Public License, version 2
*/
class Tx_BlogExample_Domain_Model_Blog extends Tx_Extbase_DomainObject_AbstractEntity {
/**
* The blog's title.
*
* @var string
* @validate StringLength(minimum = 1, maximum = 80)
*/
protected $title = '';
/**
* A short description of the blog
*
* @var string
* @validate StringLength(maximum = 150)
*/
protected $description = '';
/**
* The posts of this blog
*
* @var Tx_Extbase_Persistence_ObjectStorage<Tx_BlogExample_Domain_Model_Post>
* @lazy
* @cascade remove
*/
protected $posts;
/**
* The blog's administrator
*
* @var Tx_BlogExample_Domain_Model_Administrator
* @lazy
*/
protected $administrator;
/**
* Constructs a new Blog
*
*/
public function __construct() {
$this->posts = new Tx_Extbase_Persistence_ObjectStorage();
}
/**
* Sets this blog's title
*
* @param string $title The blog's title
* @return void
*/
public function setTitle($title) {
$this->title = $title;
}