/**
  * 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()
 {
     /* @var EntityManager $em */
     $em = $this->getDoctrine()->getManager();
     $locale = $this->getRequest()->getLocale();
     $qb = $em->getConnection()->createQueryBuilder();
     $qb->select('n.id, n.parent_id, t.weight, t.title, t.online, t.url, n.ref_entity_name')->from('kuma_nodes', 'n')->leftJoin('n', 'kuma_node_translations', 't', "(t.node_id = n.id AND t.lang = ?)")->where('n.deleted = 0')->andWhere('t.online IN (0, 1)')->addOrderBy('parent_id', 'ASC')->addOrderBy('weight', 'ASC')->addOrderBy('title', 'ASC');
     $permissionDef = new PermissionDefinition(array(PermissionMap::PERMISSION_VIEW));
     $permissionDef->setEntity('Kunstmaan\\NodeBundle\\Entity\\Node');
     $permissionDef->setAlias('n');
     $qb = $this->get('kunstmaan_admin.acl.native.helper')->apply($qb, $permissionDef);
     $stmt = $em->getConnection()->prepare($qb->getSQL());
     $stmt->bindValue(1, $locale);
     $stmt->execute();
     $result = $stmt->fetchAll();
     $simpleTreeView = new SimpleTreeView();
     foreach ($result as $data) {
         if ($this->isStructureNode($data['ref_entity_name'])) {
             $data['online'] = true;
         }
         $simpleTreeView->addItem($data['parent_id'], $data);
     }
     // When the media bundle is available, we show a link in the header to the media chooser
     $allBundles = $this->container->getParameter('kernel.bundles');
     if (array_key_exists('KunstmaanMediaBundle', $allBundles)) {
         $params = array('linkChooser' => 1);
         $cKEditorFuncNum = $this->getRequest()->get('CKEditorFuncNum');
         if (!empty($cKEditorFuncNum)) {
             $params['CKEditorFuncNum'] = $cKEditorFuncNum;
         }
         $mediaChooserLink = $this->generateUrl('KunstmaanMediaBundle_chooser', $params);
     }
     return array('tree' => $simpleTreeView, 'mediaChooserLink' => $mediaChooserLink);
 }
    /**
     * Get all the information needed to build a menu tree with one query.
     * We only fetch the fields we need, instead of fetching full objects to limit the memory usage.
     *
     * @param string          $lang                 The locale
     * @param string          $permission           The permission (read, write, ...)
     * @param AclNativeHelper $aclNativeHelper      The acl helper
     * @param bool            $includeHiddenFromNav Include nodes hidden from navigation or not
     *
     * @return array
     */
    public function getAllMenuNodes($lang, $permission, AclNativeHelper $aclNativeHelper, $includeHiddenFromNav = false)
    {
        $connection = $this->_em->getConnection();
        $qb = $connection->createQueryBuilder();
        $databasePlatformName = $connection->getDatabasePlatform()->getName();
        $createIfStatement = function ($expression, $trueValue, $falseValue) use($databasePlatformName) {
            switch ($databasePlatformName) {
                case 'sqlite':
                    $statement = 'CASE WHEN %s THEN %s ELSE %s END';
                    break;
                default:
                    $statement = 'IF(%s, %s, %s)';
            }
            return sprintf($statement, $expression, $trueValue, $falseValue);
        };
        $sql = <<<SQL
n.id, n.parent_id AS parent, t.url,
{$createIfStatement('t.weight IS NULL', 'v.weight', 't.weight')} AS weight,
{$createIfStatement('t.title IS NULL', 'v.title', 't.title')} AS title,
{$createIfStatement('t.online IS NULL', '0', 't.online')} AS online,
n.hidden_from_nav AS hidden,
n.ref_entity_name AS ref_entity_name
SQL;
        $qb->select($sql)->from('kuma_nodes', 'n')->leftJoin('n', 'kuma_node_translations', 't', '(t.node_id = n.id AND t.lang = ?)')->leftJoin('n', '(SELECT lang, title, weight, node_id, url FROM kuma_node_translations GROUP BY node_id ORDER BY id ASC)', 'v', '(v.node_id = n.id AND v.lang <> ?)')->where('n.deleted = 0')->addGroupBy('n.id')->addOrderBy('t.weight', 'ASC')->addOrderBy('t.title', 'ASC');
        if (!$includeHiddenFromNav) {
            $qb->andWhere('n.hidden_from_nav <> 0');
        }
        $permissionDef = new PermissionDefinition(array($permission));
        $permissionDef->setEntity('Kunstmaan\\NodeBundle\\Entity\\Node');
        $permissionDef->setAlias('n');
        $qb = $aclNativeHelper->apply($qb, $permissionDef);
        $stmt = $this->_em->getConnection()->prepare($qb->getSQL());
        $stmt->bindValue(1, $lang);
        $stmt->bindValue(2, $lang);
        $stmt->execute();
        return $stmt->fetchAll();
    }
 /**
  * @covers Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionDefinition::setEntity
  * @covers Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionDefinition::getEntity
  */
 public function testSetGetEntity()
 {
     $this->object->setEntity('entity');
     $this->assertEquals('entity', $this->object->getEntity());
 }