<commit_before><?php

namespace Kunstmaan\NodeBundle\Controller;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\EntityManagerInterface;
use Kunstmaan\AdminBundle\Helper\DomainConfigurationInterface;
use Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionMap;
use Kunstmaan\NodeBundle\Entity\Node;
use Kunstmaan\NodeBundle\Entity\StructureNode;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

final class WidgetsController extends Controller
{
    /**
     * @Route("/ckselecturl", name="KunstmaanNodeBundle_ckselecturl")
     * @Template("@KunstmaanNode/Widgets/selectLink.html.twig")
     *
     * @return array
     */
    public function ckSelectLinkAction(Request $request)
    {
        $params = $this->getTemplateParameters($request);
        $params['cke'] = true;
        $params['multilanguage'] = $this->getParameter('kunstmaan_admin.multi_language');

        return $params;
    }

    /**
     * Select a link
     *
     * @Route("/selecturl", name="KunstmaanNodeBundle_selecturl")
     * @Template("@KunstmaanNode/Widgets/selectLink.html.twig")
     *
     * @return array
     */
    public function selectLinkAction(Request $request)
    {
        $params = $this->getTemplateParameters($request);
        $params['cke'] = false;
        $params['multilanguage'] = $this->getParameter('kunstmaan_admin.multi_language');

        return $params;
    }

    /**
     * Select a link
     *
     * @Route("/select-nodes-lazy", name="KunstmaanNodeBundle_nodes_lazy")
     *
     * @return JsonResponse
     */
    public function selectNodesLazy(Request $request)
    {
        /* @var EntityManagerInterface $em */
        $em = $this->getDoctrine()->getManager();
        $locale = $request->getLocale();
        $id = $request->query->get('id');
        $depth = $this->getParameter('kunstmaan_node.url_chooser.lazy_increment');

        if (!$id || $id == '#') {
            $domainConfig = $this->get('kunstmaan_admin.domain_configuration');

            if ($domainConfig->isMultiDomainHost()) {
                $switchedHost = $domainConfig->getHostSwitched();
                $rootItems = [$domainConfig->getRootNode($switchedHost['host'])];
            } else {
                $rootItems = $em->getRepository(Node::class)->getAllTopNodes();
            }
        } else {
            $rootNode = $em->getRepository(Node::class)->find($id);
            $rootItems = $this->getChildren($locale, $rootNode);
        }

        $results = $this->nodesToArray($locale, $rootItems, $depth);

        return new JsonResponse($results);
    }

    /**
     * Get the parameters needed in the template. This is common for the
     * default link chooser and the cke link chooser.
     *
     * @return array
     */
    private function getTemplateParameters(Request $request)
    {
        // When the media bundle is available, we show a link in the header to the media chooser
        $allBundles = $this->getParameter('kernel.bundles');
        $mediaChooserLink = null;

        if (\array_key_exists('KunstmaanMediaBundle', $allBundles)) {
            $params = ['linkChooser' => 1];
            $cKEditorFuncNum = $request->get('CKEditorFuncNum');
            if (!empty($cKEditorFuncNum)) {
                $params['CKEditorFuncNum'] = $cKEditorFuncNum;
            }
            $mediaChooserLink = $this->generateUrl(
                'KunstmaanMediaBundle_chooser',
                $params<commit_msg>[NodeBundle] Fix missing route bc-break in url chooser search<commit_after><?php

namespace Kunstmaan\NodeBundle\Controller;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\EntityManagerInterface;
use Kunstmaan\AdminBundle\Helper\DomainConfigurationInterface;
use Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionMap;
use Kunstmaan\NodeBundle\Entity\Node;
use Kunstmaan\NodeBundle\Entity\NodeTranslation;
use Kunstmaan\NodeBundle\Entity\StructureNode;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

final class WidgetsController extends Controller
{
    /**
     * @Route("/ckselecturl", name="KunstmaanNodeBundle_ckselecturl")
     * @Template("@KunstmaanNode/Widgets/selectLink.html.twig")
     *
     * @return array
     */
    public function ckSelectLinkAction(Request $request)
    {
        $params = $this->getTemplateParameters($request);
        $params['cke'] = true;
        $params['multilanguage'] = $this->getParameter('kunstmaan_admin.multi_language');

        return $params;
    }

    /**
     * Select a link
     *
     * @Route("/selecturl", name="KunstmaanNodeBundle_selecturl")
     * @Template("@KunstmaanNode/Widgets/selectLink.html.twig")
     *
     * @return array
     */
    public function selectLinkAction(Request $request)
    {
        $params = $this->getTemplateParameters($request);
        $params['cke'] = false;
        $params['multilanguage'] = $this->getParameter('kunstmaan_admin.multi_language');

        return $params;
    }

    /**
     * Select a link
     *
     * @Route("/select-nodes-lazy", name="KunstmaanNodeBundle_nodes_lazy")
     *
     * @return JsonResponse
     */
    public function selectNodesLazy(Request $request)
    {
        /* @var EntityManagerInterface $em */
        $em = $this->getDoctrine()->getManager();
        $locale = $request->getLocale();
        $id = $request->query->get('id');
        $depth = $this->getParameter('kunstmaan_node.url_chooser.lazy_increment');

        if (!$id || $id == '#') {
            $domainConfig = $this->get('kunstmaan_admin.domain_configuration');

            if ($domainConfig->isMultiDomainHost()) {
                $switchedHost = $domainConfig->getHostSwitched();
                $rootItems = [$domainConfig->getRootNode($switchedHost['host'])];
            } else {
                $rootItems = $em->getRepository(Node::class)->getAllTopNodes();
            }
        } else {
            $rootNode = $em->getRepository(Node::class)->find($id);
            $rootItems = $this->getChildren($locale, $rootNode);
        }

        $results = $this->nodesToArray($locale, $rootItems, $depth);

        return new JsonResponse($results);
    }

    /**
     * Search action in url chooser popup
     *
     * @Route("/select-nodes-lazy_search", name="KunstmaanNodeBundle_nodes_lazy_search")
     */
    public function selectNodesLazySearch(Request $request): JsonResponse
    {
        $locale = $request->getLocale();
        $search = $request->query->get('str');

        $results = [];
        if ($search) {
            $em = $this->getDoctrine()->getManager();
            $nts = $em->getRepository(NodeTranslation::class)->getNodeTranslationsLikeTitle($search, $locale);
            foreach ($nts as $nt) {
                $node = $nt->getNode();
                $results[] = $node->getId();
                while ($node->getParent()) {
                    $node = $node->getParent();
                    $results[] = $node->getId();
                }
            }
            $results = array_unique($results);
            sort($results);
        }

        return new JsonResponse($results);
    }

    /**
     * Get the parameters needed in the template. This is common for the
     * default link chooser and the cke link chooser.
     *
     * @return array
     */
    private function getTemplateParameters(Request $request)
    {
        // When the media bundle is available, we show a link in the header to the media chooser
        $allBundles = $this->getParameter('kernel.bundles');
        $mediaChooserLink = null;

        if (\array_key_exists('KunstmaanMediaBundle', $allBundles)) {
            $params = ['linkChooser' => 1];
            $cKEditorFuncNum = $request->get('CKEditorFuncNum');
            if (!empty($cKEditorFuncNum)) {
                $params['CKEditorFuncNum'] = $cKEditorFuncNum;
            }
            $mediaChooserLink = $this->generateUrl(
                'KunstmaanMediaBundle_chooser',
                $params
{'fraction_non_alphanumeric': 0.13272338403041825, 'fraction_numerical': 0.0002376425855513308, 'mean_word_length': 2.7408888888888887, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3105818', 'n_tokens_mistral': 2420, 'n_tokens_neox': 2412, 'n_words': 412}