Completed
Push — 6.0 ( eb641c...92cb73 )
by Ruud
148:12 queued 131:31
created

WidgetsController::selectNodesLazyAction()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 24
rs 8.6845
cc 4
eloc 16
nc 3
nop 1
1
<?php
2
3
namespace Kunstmaan\NodeBundle\Controller;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\ORM\EntityManagerInterface;
7
use Kunstmaan\MultiDomainBundle\Helper\DomainConfiguration;
8
use Kunstmaan\NodeBundle\Entity\Node;
9
use Kunstmaan\NodeBundle\Entity\NodeTranslation;
10
use Kunstmaan\NodeBundle\Entity\StructureNode;
11
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
12
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
13
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\JsonResponse;
16
17
/**
18
 * WidgetsController
19
 */
20
class WidgetsController extends Controller
21
{
22
    /**
23
     * @Route("/ckselecturl", name="KunstmaanNodeBundle_ckselecturl")
24
     * @Template("KunstmaanNodeBundle:Widgets:selectLink.html.twig")
25
     *
26
     * @param \Symfony\Component\HttpFoundation\Request $request
27
     * @return array
28
     */
29 View Code Duplication
    public function ckSelectLinkAction(Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
30
    {
31
        $params = $this->getTemplateParameters($request);
32
        $params['cke'] = true;
33
        $params['multilanguage'] = $this->getParameter('multilanguage');
34
35
        return $params;
36
    }
37
38
    /**
39
     * Select a link
40
     *
41
     * @Route("/selecturl", name="KunstmaanNodeBundle_selecturl")
42
     * @Template("KunstmaanNodeBundle:Widgets:selectLink.html.twig")
43
     *
44
     * @param \Symfony\Component\HttpFoundation\Request $request
45
     * @return array
46
     */
47 View Code Duplication
    public function selectLinkAction(Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48
    {
49
        $params = $this->getTemplateParameters($request);
50
        $params['cke'] = false;
51
        $params['multilanguage'] = $this->getParameter('multilanguage');
52
53
        return $params;
54
    }
55
56
    /**
57
     * Select a link
58
     *
59
     * @Route("/select-nodes-lazy", name="KunstmaanNodeBundle_nodes_lazy")
60
     *
61
     * @param \Symfony\Component\HttpFoundation\Request $request
62
     * @return JsonResponse
63
     */
64
    public function selectNodesLazyAction(Request $request)
65
    {
66
        /* @var EntityManagerInterface $em */
67
        $em = $this->getDoctrine()->getManager();
68
        $locale = $request->getLocale();
69
        $id = $request->query->get('id');
70
        $depth = $this->getParameter('kunstmaan_node.url_chooser.lazy_increment');
71
72
        if (!$id || $id == '#') {
73
            $domainConfig = $this->get('kunstmaan_admin.domain_configuration');
74
75
            if ($domainConfig->isMultiDomainHost()) {
76
                $switchedHost = $domainConfig->getHostSwitched();
77
                $rootItems = [$domainConfig->getRootNode($switchedHost['host'])];
78
            } else {
79
                $rootItems = $em->getRepository('KunstmaanNodeBundle:Node')->getAllTopNodes();
80
            }
81
        } else {
82
            $rootItems = $em->getRepository('KunstmaanNodeBundle:Node')->find($id)->getChildren();
83
        }
84
85
        $results = $this->nodesToArray($locale, $rootItems, $depth);
86
        return new JsonResponse($results);
87
    }
88
89
    /**
90
     * Get the parameters needed in the template. This is common for the
91
     * default link chooser and the cke link chooser.
92
     *
93
     * @param \Symfony\Component\HttpFoundation\Request $request
94
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,string|null>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
95
     */
96
    private function getTemplateParameters(Request $request)
97
    {
98
        // When the media bundle is available, we show a link in the header to the media chooser
99
        $allBundles = $this->getParameter('kernel.bundles');
100
        $mediaChooserLink = null;
101
102
        if (array_key_exists('KunstmaanMediaBundle', $allBundles)) {
103
            $params = ['linkChooser' => 1];
104
            $cKEditorFuncNum = $request->get('CKEditorFuncNum');
105
            if (!empty($cKEditorFuncNum)) {
106
                $params['CKEditorFuncNum'] = $cKEditorFuncNum;
107
            }
108
            $mediaChooserLink = $this->generateUrl(
109
                'KunstmaanMediaBundle_chooser',
110
                $params
111
            );
112
        }
113
114
        return [
115
            'mediaChooserLink' => $mediaChooserLink
116
        ];
117
    }
118
119
    /**
120
     * Determine if current node is a structure node.
121
     *
122
     * @param string $refEntityName
123
     *
124
     * @return bool
125
     */
126
    protected function isStructureNode($refEntityName)
127
    {
128
        $structureNode = false;
129
        if (class_exists($refEntityName)) {
130
            $page = new $refEntityName();
131
            $structureNode = ($page instanceof StructureNode);
132
            unset($page);
133
        }
134
135
        return $structureNode;
136
    }
137
138
    /**
139
     * Determine if current node is a structure node.
140
     *
141
     * @param string $locale
142
     * @param Node[]|ArrayCollection $rootNodes
143
     * @param integer $depth
144
     *
145
     * @return array
146
     */
147
    protected function nodesToArray($locale, $rootNodes, $depth = 2)
148
    {
149
        /** @var DomainConfiguration $domainconfig */
150
        $domainconfig = $this->get('kunstmaan_admin.domain_configuration');
151
        $isMultiDomain = $domainconfig->isMultiDomainHost();
152
        $switchedHost = $domainconfig->getHostSwitched();
153
        $switched = $domainconfig->getHost() == $switchedHost['host'];
154
155
        $results = [];
156
157
        /** @var Node $rootNode */
158
        foreach ($rootNodes as $rootNode) {
159
            if ($nodeTranslation = $rootNode->getNodeTranslation($locale, true)) {
160
                if ($isMultiDomain && !$switched) {
161
                    $slug = sprintf("[%s:%s]", $switchedHost['id'], "NT" . $nodeTranslation->getId());
162
                } else {
163
                    $slug = sprintf("[%s]", "NT" . $nodeTranslation->getId());
164
                }
165
166
                $root = [
167
                    'id' => $rootNode->getId(),
168
                    'type' => $nodeTranslation->isOnline() ? "default" : "offline",
169
                    'text' => $nodeTranslation->getTitle(),
170
                    'li_attr' => ['class' => 'js-url-chooser-link-select', 'data-slug' => $slug, 'data-id' => $rootNode->getId()]
171
                ];
172
173
                if ($rootNode->getChildren()->count()) {
174
                    if ($depth - 1) {
175
                        $root['children'] = $this->nodesToArray($locale, $rootNode->getChildren(), --$depth);
176
                    } else {
177
                        $root['children'] = true;
178
                    }
179
                }
180
                $results[] = $root;
181
            }
182
        }
183
184
        return $results;
185
    }
186
}
187