Skip to content

Commit dc85eda

Browse files
committed
Merge pull request #16 from symfony-cmf/simplecms-based-fixtures
Simplecms based fixtures
2 parents 612f47d + a04d697 commit dc85eda

File tree

4 files changed

+151
-115
lines changed

4 files changed

+151
-115
lines changed

composer.lock

Lines changed: 39 additions & 36 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Acme/MainBundle/DataFixtures/PHPCR/LoadSimpleCmsData.php

Lines changed: 46 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -2,107 +2,74 @@
22

33
namespace Acme\MainBundle\DataFixtures\PHPCR;
44

5-
use Doctrine\Common\DataFixtures\FixtureInterface;
65
use Doctrine\Common\Persistence\ObjectManager;
76

8-
use PHPCR\Util\NodeHelper;
7+
use Symfony\Component\Yaml\Parser;
98

10-
use Symfony\Component\DependencyInjection\ContainerInterface;
11-
use Symfony\Component\DependencyInjection\ContainerAware;
12-
13-
use Symfony\Cmf\Bundle\SimpleCmsBundle\Document\Page;
14-
use Symfony\Cmf\Bundle\SimpleCmsBundle\Document\MultilangPage;
15-
use Symfony\Cmf\Bundle\SimpleCmsBundle\Document\MultilangRoute;
9+
use Symfony\Cmf\Bundle\SimpleCmsBundle\DataFixtures\LoadCmsData;
1610
use Symfony\Cmf\Bundle\SimpleCmsBundle\Document\MultilangRedirectRoute;
11+
use Symfony\Cmf\Bundle\SimpleCmsBundle\Document\MultilangRoute;
12+
1713
use Symfony\Cmf\Bundle\MenuBundle\Document\MultilangMenuItem;
1814

19-
class LoadSimpleCmsData extends ContainerAware implements FixtureInterface
15+
class LoadSimpleCmsData extends LoadCmsData
2016
{
21-
public function load(ObjectManager $dm)
22-
{
23-
$session = $dm->getPhpcrSession();
17+
private $yaml;
2418

25-
$basepath = explode('/', $this->container->getParameter('symfony_cmf_simple_cms.basepath'));
26-
$rootname = array_pop($basepath);
27-
$basepath = implode('/', $basepath);
28-
29-
if ($session->nodeExists($basepath)) {
30-
$session->removeItem($basepath);
31-
}
19+
public function getOrder()
20+
{
21+
return 5;
22+
}
3223

33-
NodeHelper::createPath($session, $basepath);
34-
$base = $dm->find(null, $basepath);
24+
protected function getData()
25+
{
26+
return $this->yaml->parse(file_get_contents(__DIR__.'/../../Resources/data/page.yml'));
27+
}
3528

36-
$root = $this->createPage($dm, $base, $rootname, 'Homepage', array('en' => array('Welcome to the CMF Standard Edition', 'This is should get you started with the Symfony CMF.'), 'de' => array('Willkommen zur CMF Standard Edition', 'Dies sollte Ihnen einen Einstieg in das Symfony CMF bieten.')));
37-
$this->createPage($dm, $root, 'about', 'About us', array('' => array('Some information about us', 'The about us page with some content')));
38-
$contact = $this->createPage($dm, $root, 'contact', 'Contact', array('' => array('A contact page', 'Please send an email to [email protected]')));
39-
$this->createPage($dm, $contact, 'map', 'Map', array('en' => array('A map of a location in the US', 'Have a look at the map to find us.'), 'de' => array('Eine Karte von einem Ort in Deutschland', 'Hier können Sie uns finden.')));
40-
$this->createPage($dm, $contact, 'team', 'Team', array('' => array('A team page', 'Our team consists of C, M and F.')));
29+
public function load(ObjectManager $dm)
30+
{
31+
$this->yaml = new Parser();
4132

42-
$this->createMenuItem($dm, $root, 'link', 'http://cmf.symfony.com', array('en' => 'Website', 'de' => 'Webseite'));
33+
parent::load($dm);
4334

44-
$route = new MultilangRedirectRoute();
45-
$route->setPosition($root, 'demo');
46-
$route->setUri('http://cmf.liip.ch');
47-
$dm->persist($route);
35+
$data = $this->yaml->parse(file_get_contents(__DIR__ . '/../../Resources/data/external.yml'));
4836

49-
$this->createMenuItem($dm, $root, 'demo_redirect', $route, array('en' => 'Demo', 'de' => 'Demo'));
37+
$basepath = $this->container->getParameter('symfony_cmf_simple_cms.basepath');
38+
$home = $dm->find(null, $basepath);
5039

5140
$route = new MultilangRoute();
52-
$route->setPosition($root, 'dynamic');
41+
$route->setPosition($home, 'dynamic');
5342
$route->setDefault('_controller', 'AcmeMainBundle:Demo:dynamic');
5443

5544
$dm->persist($route);
5645

57-
$this->createMenuItem($dm, $root, 'hardcoded_dynamic', $route, array('en' => 'Dynamic', 'de' => 'Dynamisch'));
58-
59-
$this->createMenuItem($dm, $root, 'hardcoded_static', 'static', array('en' => 'Static', 'de' => 'Statisch'));
60-
61-
$dm->flush();
62-
}
63-
64-
/**
65-
* @return Page instance with the specified information
66-
*/
67-
protected function createPage(ObjectManager $dm, $parent, $name, $label, array $content)
68-
{
69-
$page = new MultilangPage();
70-
$page->setPosition($parent, $name);
71-
$page->setLabel($label);
72-
73-
$dm->persist($page);
74-
foreach ($content as $locale => $data) {
75-
$page->setTitle($data[0]);
76-
$page->setBody($data[1]);
77-
if ($locale) {
78-
$dm->bindTranslation($page, $locale);
46+
foreach ($data['static'] as $name => $overview) {
47+
$menuItem = new MultilangMenuItem();
48+
$menuItem->setName($name);
49+
$menuItem->setParent($home);
50+
if (!empty($overview['route'])) {
51+
if (!empty($overview['uri'])) {
52+
$route = new MultilangRedirectRoute();
53+
$route->setPosition($home, $overview['route']);
54+
$route->setUri($overview['uri']);
55+
$dm->persist($route);
56+
} else {
57+
$route = $dm->find(null, $basepath.'/'.$overview['route']);
58+
}
59+
$menuItem->setRoute($route->getId());
60+
} else {
61+
$menuItem->setUri($overview['uri']);
7962
}
80-
}
81-
82-
return $page;
83-
}
84-
85-
/**
86-
* @return MenuItem instance with the specified information
87-
*/
88-
protected function createMenuItem(ObjectManager $dm, $parent, $name, $target, array $content)
89-
{
90-
$menuItem = new MultilangMenuItem();
91-
$menuItem->setPosition($parent, $name);
92-
if (is_object($target)) {
93-
$menuItem->setRoute($target->getId());
94-
} else {
95-
$menuItem->setUri($target);
96-
}
97-
98-
$dm->persist($menuItem);
99-
foreach ($content as $locale => $label) {
100-
$menuItem->setLabel($label);
101-
if ($locale) {
102-
$dm->bindTranslation($menuItem, $locale);
63+
64+
$dm->persist($menuItem);
65+
foreach ($overview['label'] as $locale => $label) {
66+
$menuItem->setLabel($label);
67+
if ($locale) {
68+
$dm->bindTranslation($menuItem, $locale);
69+
}
10370
}
10471
}
10572

106-
return $menuItem;
73+
$dm->flush();
10774
}
10875
}

0 commit comments

Comments
 (0)