Skip to content

Commit c6d8498

Browse files
committed
Readded data fixtures class
1 parent 4eb849a commit c6d8498

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Bundle\SimpleCmsBundle\DataFixtures\Phpcr;
4+
5+
use Doctrine\Common\DataFixtures\FixtureInterface;
6+
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
7+
use Doctrine\Common\Persistence\ObjectManager;
8+
9+
use PHPCR\Util\NodeHelper;
10+
11+
use Symfony\Component\DependencyInjection\ContainerAware;
12+
13+
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
14+
15+
/**
16+
* @deprecated: To be removed in 1.1
17+
*/
18+
abstract class AbstractLoadPageData extends ContainerAware implements FixtureInterface, OrderedFixtureInterface
19+
{
20+
protected $defaultClass = array(
21+
'page' => 'Symfony\Cmf\Bundle\SimpleCmsBundle\Doctrine\Phpcr\Page'
22+
);
23+
24+
abstract protected function getData();
25+
26+
/**
27+
* @param $className
28+
* @return \Symfony\Cmf\Bundle\SimpleCmsBundle\Document\Page
29+
*/
30+
protected function createPageInstance($className)
31+
{
32+
return new $className(true);
33+
}
34+
35+
protected function getBasePath()
36+
{
37+
return $this->container->getParameter('cmf_simple_cms.basepath');
38+
}
39+
40+
protected function getDefaultClass()
41+
{
42+
return $this->container->getParameter('cmf_simple_cms.locales')
43+
? $this->defaultClass['multilangpage'] : $this->defaultClass['page'];
44+
}
45+
46+
public function load(ObjectManager $dm)
47+
{
48+
$session = $dm->getPhpcrSession();
49+
50+
$basepath = $this->getBasePath();
51+
NodeHelper::createPath($session, preg_replace('#/[^/]*$#', '', $basepath));
52+
53+
$data = $this->getData();
54+
55+
$defaultClass = $this->getDefaultClass();
56+
57+
foreach ($data['static'] as $overview) {
58+
$class = isset($overview['class']) ? $overview['class'] : $defaultClass;
59+
60+
$parent = (isset($overview['parent']) ? trim($overview['parent'], '/') : '');
61+
$name = (isset($overview['name']) ? trim($overview['name'], '/') : '');
62+
63+
$path = $basepath
64+
.(empty($parent) ? '' : '/' . $parent)
65+
.(empty($name) ? '' : '/' . $name);
66+
67+
$page = $dm->find($class, $path);
68+
if (!$page) {
69+
$page = $this->createPageInstance($class);
70+
$page->setId($path);
71+
}
72+
73+
if (isset($overview['formats'])) {
74+
$page->setDefault('_format', reset($overview['formats']));
75+
$page->setRequirement('_format', implode('|', $overview['formats']));
76+
}
77+
78+
if (!empty($overview['template'])) {
79+
$page->setDefault(RouteObjectInterface::TEMPLATE_NAME, $overview['template']);
80+
}
81+
82+
if (!empty($overview['controller'])) {
83+
$page->setDefault(RouteObjectInterface::CONTROLLER_NAME, $overview['controller']);
84+
}
85+
86+
if (!empty($overview['options'])) {
87+
$page->setOptions($overview['options']);
88+
}
89+
90+
$dm->persist($page);
91+
92+
if (is_array($overview['title'])) {
93+
foreach ($overview['title'] as $locale => $title) {
94+
$page->setTitle($title);
95+
if (isset($overview['label'][$locale]) && $overview['label'][$locale]) {
96+
$page->setLabel($overview['label'][$locale]);
97+
} elseif (!isset($overview['label'][$locale])) {
98+
$page->setLabel($title);
99+
}
100+
$page->setBody($overview['body'][$locale]);
101+
$dm->bindTranslation($page, $locale);
102+
}
103+
} else {
104+
$page->setTitle($overview['title']);
105+
if (isset($overview['label'])) {
106+
if ($overview['label']) {
107+
$page->setLabel($overview['label']);
108+
}
109+
} elseif (!isset($overview['label'])) {
110+
$page->setLabel($overview['title']);
111+
}
112+
$page->setBody($overview['body']);
113+
}
114+
115+
if (isset($overview['create_date'])) {
116+
$page->setCreateDate(date_create_from_format('U', strtotime($overview['create_date'])));
117+
}
118+
119+
if (isset($overview['publish_start_date'])) {
120+
$page->setPublishStartDate(date_create_from_format('U', strtotime($overview['publish_start_date'])));
121+
}
122+
123+
if (isset($overview['publish_end_date'])) {
124+
$page->setPublishEndDate(date_create_from_format('U', strtotime($overview['publish_end_date'])));
125+
}
126+
}
127+
128+
$dm->flush();
129+
}
130+
}

0 commit comments

Comments
 (0)