main/core/Listener/Resource/Types/DirectoryListener.php
public function onAdd(ResourceActionEvent $event)
    {
        $data = $event->getData();
        $parent = $event->getResourceNode();

        $add = $this->actionManager->get($parent, 'add');

        // checks if the current user can add
        $collection = new ResourceCollection([$parent], ['type' => $data['resourceNode']['meta']['type']]);
        if (!$this->actionManager->hasPermission($add, $collection)) {
            throw new ResourceAccessException($collection->getErrorsForDisplay(), $collection->getResources());
        }

        $options = $event->getOptions();
        $options[] = Options::IGNORE_CRUD_POST_EVENT;

        // create the resource node

        /** @var ResourceNode $resourceNode */
        $resourceNode = $this->crud->create(ResourceNode::class, $data['resourceNode'], $options);
        $resourceNode->setParent($parent);
        $resourceNode->setWorkspace($parent->getWorkspace());

        // initialize custom resource Entity
        $resourceClass = $resourceNode->getResourceType()->getClass();

        /** @var AbstractResource $resource */
        $resource = $this->crud->create($resourceClass, !empty($data['resource']) ? $data['resource'] : [], $options);
        $resource->setResourceNode($resourceNode);

        // maybe do it in the serializer (if it can be done without intermediate flush)
        if (!empty($data['resourceNode']['rights'])) {
            foreach ($data['resourceNode']['rights'] as $rights) {
                /** @var Role $role */
                $role = $this->om->getRepository('ClarolineCoreBundle:Role')->findOneBy(['name' => $rights['name']]);
                $this->rightsManager->editPerms($rights['permissions'], $role, $resourceNode);
            }
        } else {
            // todo : initialize default rights
        }

        $this->crud->dispatch('create', 'post', [$resource, $options]);
        $this->om->persist($resource);
        $this->om->persist($resourceNode);

        // todo : dispatch creation event

        $this->om->flush();

        // todo : dispatch get/load action instead
        $event->setResponse(new JsonResponse(
            [
                'resourceNode' => $this->serializer->serialize($resourceNode),
                'resource' => $this->serializer->serialize($resource),
            ],
            201
        ));
    }
public function onAdd(ResourceActionEvent $event)
    {
        $data = $event->getData();
        $parent = $event->getResourceNode();

        $add = $this->actionManager->get($parent, 'add');

        // checks if the current user can add
        $collection = new ResourceCollection([$parent], ['type' => $data['resourceNode']['meta']['type']]);
        if (!$this->actionManager->hasPermission($add, $collection)) {
            throw new ResourceAccessException($collection->getErrorsForDisplay(), $collection->getResources());
        }

        $options = $event->getOptions();
        $options[] = Options::IGNORE_CRUD_POST_EVENT;

        // create the resource node

        /** @var ResourceNode $resourceNode */
        $resourceNode = $this->crud->create(ResourceNode::class, $data['resourceNode'], $options);
        $resourceNode->setParent($parent);
        $resourceNode->setWorkspace($parent->getWorkspace());

        // initialize custom resource Entity
        $resourceClass = $resourceNode->getResourceType()->getClass();

        /** @var AbstractResource $resource */
        $resource = $this->crud->create($resourceClass, !empty($data['resource']) ? $data['resource'] : [], $options);
        $resource->setResourceNode($resourceNode);

        // maybe do it in the serializer (if it can be done without intermediate flush)
        if (!empty($data['resourceNode']['rights'])) {
            foreach ($data['resourceNode']['rights'] as $rights) {
                /** @var Role $role */
                $role = $this->om->getRepository('ClarolineCoreBundle:Role')->findOneBy(['name' => $rights['name']]);
                $this->rightsManager->editPerms($rights['permissions'], $role, $resourceNode);
            }
        } else {
            // todo : initialize default rights
        }

        $this->crud->dispatch('create', 'post', [$resource, $options]);
        $this->om->persist($resource);
        $this->om->persist($resourceNode);

        // todo : dispatch creation event

        $this->om->flush();

        // todo : dispatch get/load action instead
        $event->setResponse(new JsonResponse(
            [
                'resourceNode' => $this->serializer->serialize($resourceNode),
                'resource' => $this->serializer->serialize($resource),
            ],
            201
        ));
    }
public function onAdd ( ResourceActionEvent $ event ) { $ data = $ event -> getData ( ) ; $ parent = $ event -> getResourceNode ( ) ; $ add = $ this -> actionManager -> get ( $ parent , 'add' ) ; // checks if the current user can add $ collection = new ResourceCollection ( [ $ parent ] , [ 'type' => $ data [ 'resourceNode' ] [ 'meta' ] [ 'type' ] ] ) ; if ( ! $ this -> actionManager -> hasPermission ( $ add , $ collection ) ) { throw new ResourceAccessException ( $ collection -> getErrorsForDisplay ( ) , $ collection -> getResources ( ) ) ; } $ options = $ event -> getOptions ( ) ; $ options [ ] = Options :: IGNORE_CRUD_POST_EVENT ; // create the resource node /** @var ResourceNode $resourceNode */ $ resourceNode = $ this -> crud -> create ( ResourceNode :: class , $ data [ 'resourceNode' ] , $ options ) ; $ resourceNode -> setParent ( $ parent ) ; $ resourceNode -> setWorkspace ( $ parent -> getWorkspace ( ) ) ; // initialize custom resource Entity $ resourceClass = $ resourceNode -> getResourceType ( ) -> getClass ( ) ; /** @var AbstractResource $resource */ $ resource = $ this -> crud -> create ( $ resourceClass , ! empty ( $ data [ 'resource' ] ) ? $ data [ 'resource' ] : [ ] , $ options ) ; $ resource -> setResourceNode ( $ resourceNode ) ; // maybe do it in the serializer (if it can be done without intermediate flush) if ( ! empty ( $ data [ 'resourceNode' ] [ 'rights' ] ) ) { foreach ( $ data [ 'resourceNode' ] [ 'rights' ] as $ rights ) { /** @var Role $role */ $ role = $ this -> om -> getRepository ( 'ClarolineCoreBundle:Role' ) -> findOneBy ( [ 'name' => $ rights [ 'name' ] ] ) ; $ this -> rightsManager -> editPerms ( $ rights [ 'permissions' ] , $ role , $ resourceNode ) ; } } else { // todo : initialize default rights } $ this -> crud -> dispatch ( 'create' , 'post' , [ $ resource , $ options ] ) ; $ this -> om -> persist ( $ resource ) ; $ this -> om -> persist ( $ resourceNode ) ; // todo : dispatch creation event $ this -> om -> flush ( ) ; // todo : dispatch get/load action instead $ event -> setResponse ( new JsonResponse ( [ 'resourceNode' => $ this -> serializer -> serialize ( $ resourceNode ) , 'resource' => $ this -> serializer -> serialize ( $ resource ) , ] , 201 ) ) ; }
Adds a new resource inside a directory.

@DI\Observe("resource.directory.add")

@param ResourceActionEvent $event
Adds a new resource inside a directory .