/**
  * List children of a category.
  * The parent category is provided via its id ('id' request parameter).
  * The node category to select is given by 'select_node_id' request parameter.
  *
  * If the node to select is not a direct child of the parent category, the tree
  * is expanded until the selected node is found amongs the children
  *
  * @param Request $request
  *
  * @Template
  * @AclAncestor("pim_enrich_category_list")
  *
  * @return array
  */
 public function childrenAction(Request $request)
 {
     try {
         $parent = $this->findCategory($request->get('id'));
     } catch (NotFoundHttpException $e) {
         return ['categories' => []];
     }
     $selectNodeId = $this->getRequest()->get('select_node_id', -1);
     $withProductsCount = (bool) $this->getRequest()->get('with_products_count', false);
     $includeParent = (bool) $this->getRequest()->get('include_parent', false);
     $includeSub = (bool) $this->getRequest()->get('include_sub', false);
     $relatedEntity = $this->getRequest()->get('related_entity', 'product');
     try {
         $selectNode = $this->findCategory($selectNodeId);
         if (!$this->categoryManager->isAncestor($parent, $selectNode)) {
             $selectNode = null;
         }
     } catch (NotFoundHttpException $e) {
         $selectNode = null;
     }
     if ($selectNode !== null) {
         $categories = $this->getChildren($parent->getId(), $selectNode->getId());
         $view = 'PimEnrichBundle:CategoryTree:children-tree.json.twig';
     } else {
         $categories = $this->getChildren($parent->getId());
         $view = 'PimEnrichBundle:CategoryTree:children.json.twig';
     }
     return $this->render($view, ['categories' => $categories, 'parent' => $includeParent ? $parent : null, 'include_sub' => $includeSub, 'product_count' => $withProductsCount, 'select_node' => $selectNode, 'related_entity' => $relatedEntity], new JsonResponse());
 }