Passed
Push — master ( 6ef073...e381e3 )
by Julito
17:00
created

PageController   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 179
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 104
dl 0
loc 179
rs 10
c 0
b 0
f 0
wmc 23

2 Methods

Rating   Name   Duplication   Size   Complexity  
D createPage() 0 136 19
A getLatestPages() 0 27 4
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\PageBundle\Controller;
5
6
use Chamilo\CoreBundle\Controller\BaseController;
7
use Chamilo\PageBundle\Entity\Page;
8
use Chamilo\PageBundle\Entity\Snapshot;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
10
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
11
use Ivory\CKEditorBundle\Form\Type\CKEditorType;
12
use Symfony\Bundle\FrameworkBundle\Controller\ControllerTrait;
13
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\Response;
16
17
/**
18
 * Class PageController.
19
 *
20
 * @package Chamilo\PageBundle\Controller
21
 */
22
class PageController extends BaseController
23
{
24
    /**
25
     * @Route("/cms/page/latest/{number}")
26
     *
27
     * @param int $number
28
     */
29
    public function getLatestPages($number)
30
    {
31
        $site = $this->container->get('sonata.page.site.selector')->retrieve();
32
33
        $criteria = ['enabled' => 1, 'site' => $site, 'decorate' => 1, 'routeName' => 'page_slug'];
34
        $order = ['createdAt' => 'desc'];
35
        // Get latest pages
36
        $pages = $this->container->get('sonata.page.manager.page')->findBy($criteria, $order, $number);
37
        $pagesToShow = [];
38
        /** @var Page $page */
39
        foreach ($pages as $page) {
40
            // Skip homepage
41
            if ($page->getUrl() === '/') {
42
                continue;
43
            }
44
            $criteria = ['pageId' => $page];
45
            /** @var Snapshot $snapshot */
46
            // Check if page has a valid snapshot
47
            $snapshot = $this->container->get('sonata.page.manager.snapshot')->findEnableSnapshot($criteria);
48
            if ($snapshot) {
49
                $pagesToShow[] = $page;
50
            }
51
        }
52
53
        return $this->render(
54
            '@ChamiloPage/latest.html.twig',
55
            ['pages' => $pagesToShow]
56
        );
57
    }
58
59
    /**
60
     * @param string  $pageSlug
61
     * @param Request $request
62
     *
63
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response
64
     */
65
    public function createPage($pageSlug, $redirect, Request $request)
66
    {
67
        $siteSelector = $this->container->get('sonata.page.site.selector');
68
        $site = $siteSelector->retrieve();
69
        $em = $this->getDoctrine()->getManager();
70
        $page = null;
71
72
        $form = $this->createFormBuilder()
73
            ->add('content', CKEditorType::class)
74
            ->add('save', SubmitType::class, ['label' => 'Update'])
75
            ->getForm();
76
77
        $blockToEdit = null;
78
        if ($site) {
79
            $pageManager = $this->get('sonata.page.manager.page');
80
            // Parents only of homepage
81
            $criteria = ['site' => $site, 'enabled' => true, 'parent' => 1, 'slug' => $pageSlug];
82
            /** @var Page $page */
83
            $page = $pageManager->findOneBy($criteria);
84
            if ($page) {
0 ignored issues
show
introduced by
$page is of type Chamilo\PageBundle\Entity\Page, thus it always evaluated to true. If $page can have other possible types, add them to src/PageBundle/Controller/PageController.php:82
Loading history...
85
                $blocks = $page->getBlocks();
86
                /** @var Block $block */
87
                foreach ($blocks as $block) {
88
                    if ($block->getName() == 'Main content') {
89
                        $code = $block->getSetting('code');
90
                        if ($code == 'content') {
91
                            $children = $block->getChildren();
92
                            /** @var Block $child */
93
                            foreach ($children as $child) {
94
                                if ($child->getType() == 'sonata.formatter.block.formatter') {
95
                                    $blockToEdit = $child;
96
                                    break 2;
97
                                }
98
                            }
99
                        }
100
                    }
101
                }
102
            } else {
103
                $pageManager = $this->get('sonata.page.manager.page');
104
105
                $criteria = ['site' => $site, 'enabled' => true, 'parent' => null, 'slug' => 'homepage'];
106
                /** @var Page $page */
107
                $parent = $pageManager->findOneBy($criteria);
108
109
                $page = $pageManager->create();
110
                $page->setSlug($pageSlug);
111
                $page->setUrl('/'.$pageSlug);
112
                $page->setName($pageSlug);
113
                $page->setTitle($pageSlug);
114
                $page->setEnabled(true);
115
                $page->setDecorate(1);
116
                $page->setRequestMethod('GET');
117
                $page->setTemplateCode('default');
118
                $page->setRouteName($pageSlug);
119
                $page->setParent($parent);
120
                $page->setSite($site);
121
122
                $pageManager->save($page);
123
124
                $templateManager = $this->get('sonata.page.template_manager');
125
                $template = $templateManager->get('default');
126
                $templateContainers = $template->getContainers();
127
128
                $containers = [];
129
                foreach ($templateContainers as $id => $area) {
130
                    $containers[$id] = [
131
                        'area' => $area,
132
                        'block' => false,
133
                    ];
134
                }
135
136
                // Create blocks for this page
137
                $blockInteractor = $this->get('sonata.page.block_interactor');
138
                $parentBlock = null;
139
                foreach ($containers as $id => $area) {
140
                    if (false === $area['block'] && $templateContainers[$id]['shared'] === false) {
141
                        $block = $blockInteractor->createNewContainer(
142
                            [
143
                                'page' => $page,
144
                                'name' => $templateContainers[$id]['name'],
145
                                'code' => $id,
146
                            ]
147
                        );
148
149
                        if ($id === 'content' && $templateContainers[$id]['name'] == 'Main content') {
150
                            $parentBlock = $block;
151
                        }
152
                    }
153
                }
154
155
                // Create block in main content
156
                $block = $this->get('sonata.page.manager.block');
157
                /** @var \Sonata\BlockBundle\Model\Block $myBlock */
158
                $myBlock = $block->create();
159
                $myBlock->setType('sonata.formatter.block.formatter');
160
                $myBlock->setSetting('format', 'richhtml');
161
                $myBlock->setSetting('content', '');
162
                $myBlock->setSetting('rawContent', '');
163
                $myBlock->setSetting('template', '@SonataFormatter/Block/block_formatter.html.twig');
164
                $myBlock->setParent($parentBlock);
165
                $page->addBlocks($myBlock);
166
                $pageManager->save($page);
167
            }
168
        }
169
170
        if ($blockToEdit) {
171
            $form->setData(['content' => $blockToEdit->getSetting('content')]);
172
        }
173
174
        $form->handleRequest($request);
175
176
        if ($form->isSubmitted() && $form->isValid() && $blockToEdit) {
177
            $data = $form->getData();
178
            $content = $data['content'];
179
            /** @var Block $blockToEdit */
180
            $blockToEdit->setSetting('rawContent', $content);
181
            $blockToEdit->setSetting('content', $content);
182
            $em->merge($blockToEdit);
183
            $em->flush();
184
185
            $this->addFlash('success', $this->trans('Updated'));
186
187
            if (!empty($redirect)) {
188
                return $this->redirect($redirect);
189
            }
190
191
            return $this->redirectToRoute('home');
192
        }
193
194
        $template = $pageSlug.'_edit.html.twig';
195
196
        return $this->render(
197
            '@ChamiloCore/Index/'.$template,
198
            [
199
                'page' => $page,
200
                'form' => $form->createView(),
201
            ]
202
        );
203
    }
204
}
205