Skip to content

Commit

Permalink
Featured story and article
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Apr 9, 2011
1 parent 0cfdeac commit 0b9cbea
Show file tree
Hide file tree
Showing 24 changed files with 331 additions and 138 deletions.
10 changes: 10 additions & 0 deletions app/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,16 @@ ltc_config:
model: Ltc\ConfigBundle\Document\Photo
form: Ltc\ConfigBundle\Form\PhotoForm
title: Ailleurs
featured_story:
model: Ltc\ConfigBundle\Document\FeaturedStory
form: Ltc\ConfigBundle\Form\FeaturedStoryForm
title: Actu Accueil
form_factory: ltc_config.form_factory.featured_story
featured_article:
model: Ltc\ConfigBundle\Document\FeaturedArticle
form: Ltc\ConfigBundle\Form\FeaturedArticleForm
title: A propos de...
form_factory: ltc_config.form_factory.featured_article

zend_cache:
templates:
Expand Down
5 changes: 3 additions & 2 deletions src/Ltc/ArticleBundle/Controller/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ public function viewAction($categorySlug, $slug)

public function featuredAction()
{
$article = $this->get('ltc_article.repository.article')->findOneFeatured();
$config = $this->get('ltc_config.manager')->getConfig('featured_article')->getDocument();

return $this->render('LtcArticle:Article:featured.html.twig', array(
'doc' => $article
'doc' => $config->getArticle(),
'title' => $config->getTitle()
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,6 @@ public function load($manager)
}

$manager->flush();

// set the latest article as featured
$featured = $this->articleRepository->findPublished(1)->toArray();
$this->articleRepository->feature(reset($featured));
$manager->flush();
}

protected function prepareArticleTags()
Expand Down
31 changes: 5 additions & 26 deletions src/Ltc/ArticleBundle/Document/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
* )
* @mongodb:Index(keys={"category.$id"="asc"})
* @mongodb:Index(keys={"slug"="asc"})
* @mongodb:Index(keys={"isFeatured"="desc"})
*/
class Article extends Doc
{
Expand Down Expand Up @@ -39,31 +38,6 @@ class Article extends Doc
*/
protected $publicationDate;

/**
* Whether the article is featured or not
*
* @var bool
* @mongodb:Field(type="boolean")
*/
protected $isFeatured = false;

/**
* @return bool
*/
public function getIsFeatured()
{
return $this->isFeatured;
}

/**
* @param bool
* @return null
*/
public function setIsFeatured($isFeatured)
{
$this->isFeatured = (bool) $isFeatured;
}

/**
* Tells whether the doc has a manually set publication date in string format
*
Expand Down Expand Up @@ -117,4 +91,9 @@ public function getCommentIdentifier()
{
return $this->getCategory()->getSlug().'-'.$this->getSlug();
}

public function __toString()
{
return $this->getCategory()->getTitle().' - '.$this->getTitle();
}
}
29 changes: 8 additions & 21 deletions src/Ltc/ArticleBundle/Document/ArticleRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function findByCategory(Category $category)
**/
public function findPublishedByCategory(Category $category)
{
return $this->createPublishedQueryBuilder()
return $this->createPublishedSortedQueryBuilder()
->field('category.$id')->equals(new MongoId($category->getId()))
->getQuery()
->execute();
Expand All @@ -41,7 +41,7 @@ public function findPublishedByCategory(Category $category)
**/
public function findLatestByCategory(Category $category, $limit)
{
return $this->createPublishedQueryBuilder()
return $this->createPublishedSortedQueryBuilder()
->field('category.$id')->equals(new MongoId($category->getId()))
->limit($limit)
->getQuery()
Expand All @@ -63,28 +63,15 @@ public function findOneByCategoryAndSlug(Category $category, $slug)
}

/**
* Find one featured article
* Gets all published article sorted by their category
*
* @return Article
* @return array
**/
public function findOneFeatured()
public function findAllPublishedSortByCategory()
{
return $this->createQueryBuilder()
->sort('isFeatured', 'desc')
return $this->createPublishedQueryBuilder()
->sort('category.$id', 'asc')
->getQuery()
->getSingleResult();
}

/**
* Sets this article as featured, and unset the other ones
*
* @return null
**/
public function feature(Article $article)
{
foreach ($this->findAll() as $other) {
$other->setIsFeatured(false);
}
$article->setIsFeatured(true);
->execute();
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
{% extends "LtcArticle:Article:preview.html.twig" %}
<span class="title">A propos de {{ title }}</span>
{% include "LtcArticle:Article:preview.html.twig" with {"doc": doc} %}
21 changes: 20 additions & 1 deletion src/Ltc/ConfigBundle/DataFixtures/MongoDB/LoadConfigData.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,20 @@ class LoadConfigData extends AbstractFixture implements OrderedFixtureInterface,
{
protected $configs;
protected $documentRoot;
protected $storyRepository;
protected $articleRepository;

public function getOrder()
{
return 1;
return 70;
}

public function setContainer(ContainerInterface $container = null)
{
$this->configs = $container->get('ltc_config.manager')->getConfigs();
$this->documentRoot = $container->getParameter('document_root');
$this->storyRepository = $container->get('ltc_story.repository.story');
$this->articleRepository = $container->get('ltc_article.repository.article');
}

public function load($manager)
Expand All @@ -35,11 +39,26 @@ public function load($manager)
$this->loadAuthor($document);
} elseif ('photo' == $name) {
$this->loadPhoto($document);
} elseif ('featured_story' == $name) {
$this->loadFeaturedStory($document);
} elseif ('featured_article' == $name) {
$this->loadFeaturedArticle($document);
}
}
$manager->flush();
}

protected function loadFeaturedStory($featuredStory)
{
$featuredStory->setStory($this->storyRepository->findOneBy(array()));
}

protected function loadFeaturedArticle($featuredArticle)
{
$featuredArticle->setArticle($this->articleRepository->findOneBy(array()));
$featuredArticle->setTitle('didactique');
}

protected function loadPhoto($photo)
{
$photo->setTitle('l\'Antenora');
Expand Down
2 changes: 1 addition & 1 deletion src/Ltc/ConfigBundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public function getConfigTree()
->children()
->scalarNode('model')->end()
->scalarNode('form')->end()
->scalarNode('route')->end()
->scalarNode('title')->end()
->scalarNode('form_factory')->end()
->end()
->end()
;
Expand Down
11 changes: 9 additions & 2 deletions src/Ltc/ConfigBundle/DependencyInjection/LtcConfigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,17 @@ public function load(array $configs, ContainerBuilder $container)
$repoId = 'ltc_config.repository.'.$name;
$repoDef = $container->setDefinition($repoId, new DefinitionDecorator('ltc_config.repository'));
$repoDef->setArgument(0, $options['model']);
$formFactoryId = 'ltc_config.form_factory.'.$name;
$formFactoryDef = $container->setDefinition($formFactoryId, new DefinitionDecorator('ltc_config.form_factory'));

if (isset($options['form_factory'])) {
$formFactoryId = $options['form_factory'];
$formFactoryDef = $container->getDefinition($formFactoryId);
} else {
$formFactoryId = 'ltc_config.form_factory.'.$name;
$formFactoryDef = $container->setDefinition($formFactoryId, new DefinitionDecorator('ltc_config.form_factory'));
}
$formFactoryDef->setArgument(1, $name);
$formFactoryDef->setArgument(2, $options['form']);

$configId = 'ltc_config.'.$name;
$configDef = $container->setDefinition($configId, new DefinitionDecorator('ltc_config.config'));
$configDef->setArgument(0, new Reference($repoId));
Expand Down
69 changes: 69 additions & 0 deletions src/Ltc/ConfigBundle/Document/FeaturedArticle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Ltc\ConfigBundle\Document;

use Ltc\ArticleBundle\Document\Article;

/**
* @mongodb:Document
*/
class FeaturedArticle
{
/**
* Unique ID
*
* @var string
* @mongodb:Id()
*/
protected $id;

/**
* Article
*
* @var Article
* @mongodb:ReferenceOne(targetDocument="Ltc\ArticleBundle\Document\Article")
*/
protected $article;

/**
* Title
*
* @var string
* @mongodb:Field(type="string")
*/
protected $title;

/**
* @return string
*/
public function getTitle()
{
return $this->title;
}

/**
* @param string
* @return null
*/
public function setTitle($title)
{
$this->title = $title;
}

/**
* @return Article
*/
public function getArticle()
{
return $this->article;
}

/**
* @param Article
* @return null
*/
public function setArticle(Article $article)
{
$this->article = $article;
}
}
44 changes: 44 additions & 0 deletions src/Ltc/ConfigBundle/Document/FeaturedStory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Ltc\ConfigBundle\Document;

use Ltc\StoryBundle\Document\Story;

/**
* @mongodb:Document
*/
class FeaturedStory
{
/**
* Unique ID
*
* @var string
* @mongodb:Id()
*/
protected $id;

/**
* Story
*
* @var Story
* @mongodb:ReferenceOne(targetDocument="Ltc\StoryBundle\Document\Story")
*/
protected $story;

/**
* @return Story
*/
public function getStory()
{
return $this->story;
}

/**
* @param Story
* @return null
*/
public function setStory(Story $story)
{
$this->story = $story;
}
}
26 changes: 26 additions & 0 deletions src/Ltc/ConfigBundle/Form/FeaturedArticleForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Ltc\ConfigBundle\Form;

use Symfony\Component\Form\Form;
use Symfony\Component\Form\ChoiceField;
use Symfony\Component\Form\TextField;
use Ltc\ArticleBundle\Document\ArticleRepository;
use Ltc\CoreBundle\Form\ValueTransformer\DoctrineObjectTransformer;

class FeaturedArticleForm extends Form
{
public function configure()
{
$this->add(new TextField('title'));
}

public function setArticleRepository(ArticleRepository $articleRepository)
{
$field = new ChoiceField('article', array(
'choices' => $articleRepository->findAllPublishedSortByCategory()->toArray()
));
$field->setValueTransformer(new DoctrineObjectTransformer($articleRepository));
$this->add($field);
}
}
24 changes: 24 additions & 0 deletions src/Ltc/ConfigBundle/Form/FeaturedStoryForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Ltc\ConfigBundle\Form;

use Symfony\Component\Form\Form;
use Symfony\Component\Form\ChoiceField;
use Ltc\StoryBundle\Document\StoryRepository;
use Ltc\CoreBundle\Form\ValueTransformer\DoctrineObjectTransformer;

class FeaturedStoryForm extends Form
{
public function configure()
{
}

public function setStoryRepository(StoryRepository $storyRepository)
{
$field = new ChoiceField('story', array(
'choices' => $storyRepository->findAll()->toArray()
));
$field->setValueTransformer(new DoctrineObjectTransformer($storyRepository));
$this->add($field);
}
}
Loading

0 comments on commit 0b9cbea

Please sign in to comment.