-
Notifications
You must be signed in to change notification settings - Fork 189
/
ArticleFixtures.php
158 lines (135 loc) · 5.37 KB
/
ArticleFixtures.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
namespace {{ namespace }}\DataFixtures\ORM\ArticleGenerator;
use Doctrine\Bundle\FixturesBundle\ORMFixtureInterface;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Faker\Factory;
use Faker\Provider\DateTime;
use Faker\Provider\Lorem;
use Kunstmaan\NodeBundle\Helper\Services\PageCreatorService;
use Kunstmaan\UtilitiesBundle\Helper\Slugifier;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use {{ namespace }}\Entity\Pages\{{ entity_class }}OverviewPage;
use {{ namespace }}\Entity\Pages\{{ entity_class }}Page;
use {{ namespace }}\Entity\{{ entity_class }}Author;
/**
* {{ entity_class }}ArticleFixtures
*/
class {{ entity_class }}ArticleFixtures extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface, ORMFixtureInterface
{
/**
* @var ContainerInterface
*/
private $container = null;
/**
* Load data fixtures with the passed EntityManager.
*
* @param ObjectManager $manager
*/
public function load(ObjectManager $manager)
{
if ($this->container->getParameter('kunstmaan_admin.multi_language')) {
$languages = explode('|', $this->container->getParameter('kunstmaan_admin.required_locales'));
}
if (!is_array($languages) || count($languages) < 1) {
$languages = array('en');
}
$em = $this->container->get('doctrine.orm.entity_manager');
$pageCreator = $this->container->get('kunstmaan_node.page_creator_service');
$ppCreatorService = $this->container->get('kunstmaan_pageparts.pagepart_creator_service');
// Create article overview page
$nodeRepo = $em->getRepository('KunstmaanNodeBundle:Node');
$homePage = $nodeRepo->findOneBy(array('internalName' => 'homepage'));
$overviewPage = new {{ entity_class }}OverviewPage();
$overviewPage->setTitle('{{ entity_class }}');
$translations = array();
foreach ($languages as $lang) {
$title = '{{ entity_class }}';
$translations[] = array('language' => $lang, 'callback' => function($page, $translation, $seo) use ($title) {
$translation->setTitle($title);
$translation->setWeight(30);
$slugifier = $this->container->get('kunstmaan_utilities.slugifier');
$translation->setSlug($slugifier->slugify($title));
});
}
$options = array(
'parent' => $homePage,
'page_internal_name' => '{{ entity_class|lower }}_overview_page',
'set_online' => true,
'creator' => 'admin'
);
$pageCreator->createPage($overviewPage, $translations, $options);
$fakerNL = Factory::create('nl_BE');
$fakerEN = Factory::create('en_US');
// Create articles
for ($i=1; $i<=6; $i++) {
{% if uses_author %}
// Create author
$author = new {{ entity_class }}Author();
$author->setName($fakerNL->name);
$manager->persist($author);
$manager->flush();
{% endif %}
$articlePage = new {{ entity_class }}Page();
$articlePage->setTitle(Lorem::sentence(6));
{% if uses_author %}
$articlePage->setAuthor($author);
{% endif %}
$articlePage->setDate(DateTime::dateTimeBetween('-'.($i+1).' days', '-'.$i.' days'));
$articlePage->setSummary(Lorem::paragraph(5));
$translations = array();
foreach ($languages as $lang) {
if ($lang == 'nl') {
$title = $fakerNL->sentence;
} else {
$title = $fakerEN->sentence;
}
$translations[] = array('language' => $lang, 'callback' => function($page, $translation, $seo) use ($title, $i) {
$translation->setTitle($title);
$translation->setWeight(100 + $i);
$slugifier = $this->container->get('kunstmaan_utilities.slugifier');
$translation->setSlug($slugifier->slugify($title));
});
}
$options = array(
'parent' => $overviewPage,
'set_online' => true,
'hidden_from_nav' => true,
'creator' => 'admin'
);
$articlePage = $pageCreator->createPage($articlePage, $translations, $options);
foreach ($languages as $lang) {
$pageparts = array(
'main' => array(
$ppCreatorService->getCreatorArgumentsForPagePartAndProperties('{{ namespace }}\Entity\PageParts\TextPagePart',
array('setContent' => '<p>'.Lorem::paragraph(15).'</p>' . '<p>'.Lorem::paragraph(25).'</p>' .'<p>'.Lorem::paragraph(10).'</p>')
)
)
);
$ppCreatorService->addPagePartsToPage($articlePage, $pageparts, $lang);
}
}
}
/**
* Get the order of this fixture
*
* @return int
*/
public function getOrder()
{
return 60;
}
/**
* Sets the Container.
*
* @param ContainerInterface $container A ContainerInterface instance
*
* @api
*/
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}
}