From 97ddd4a6a713eeb07aef1bdd4ed0489a7cdcfa03 Mon Sep 17 00:00:00 2001 From: Adrien Nicolet Date: Fri, 21 Dec 2012 14:20:48 +0100 Subject: [PATCH 01/13] create_routes parameter in the config --- Controller/JsloaderController.php | 12 ++++++++++-- DependencyInjection/Configuration.php | 1 + .../SymfonyCmfCreateExtension.php | 2 ++ Resources/config/services.xml | 1 + .../views/includejsfiles-create.html.twig | 19 ++++++++++++++++++- 5 files changed, 32 insertions(+), 3 deletions(-) diff --git a/Controller/JsloaderController.php b/Controller/JsloaderController.php index b99ab81..3363e23 100644 --- a/Controller/JsloaderController.php +++ b/Controller/JsloaderController.php @@ -48,6 +48,11 @@ class JsloaderController */ private $plainTextTypes; + /** + * @var Boolean + */ + private $createRoutes; + /** * Create the Controller @@ -62,6 +67,7 @@ class JsloaderController * @param Boolean $useCoffee whether assetic is set up to use coffee script * @param Boolean $fixedToolbar whether the hallo toolbar is fixed or floating * @param array $plainTextTypes RDFa types to edit in raw text only + * @param Boolean $createRoutes whether the routes need to be created after content creation * @param string $requiredRole * @param SecurityContextInterface $securityContext */ @@ -72,6 +78,7 @@ public function __construct( $useCoffee = false, $fixedToolbar = true, $plainTextTypes = array(), + $createRoutes = false, $requiredRole = "IS_AUTHENTICATED_ANONYMOUSLY", SecurityContextInterface $securityContext = null ) { @@ -81,7 +88,7 @@ public function __construct( $this->coffee = $useCoffee; $this->fixedToolbar = $fixedToolbar; $this->plainTextTypes = $plainTextTypes; - + $this->createRoutes = $createRoutes; $this->requiredRole = $requiredRole; $this->securityContext = $securityContext; } @@ -124,7 +131,8 @@ public function includeJSFilesAction($editor = 'hallo') 'cmfCreateStanbolUrl' => $this->stanbolUrl, 'cmfCreateImageUploadEnabled' => (boolean) $this->imageClass, 'cmfCreateHalloFixedToolbar' => (boolean) $this->fixedToolbar, - 'cmfCreateHalloPlainTextTypes' => json_encode($this->plainTextTypes)) + 'cmfCreateHalloPlainTextTypes' => json_encode($this->plainTextTypes), + 'cmfCreateCreateRoutes' => (boolean) $this->createRoutes) ); return $this->viewHandler->handle($view); diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index d4d08e3..1e0977e 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -43,6 +43,7 @@ public function getConfigTreeBuilder() ->useAttributeAsKey('name') ->prototype('scalar')->end() ->end() + ->scalarNode('create_routes')->defaultFalse()->end() ->arrayNode('rdf_config_dirs') ->useAttributeAsKey('dir') ->prototype('scalar')->end() diff --git a/DependencyInjection/SymfonyCmfCreateExtension.php b/DependencyInjection/SymfonyCmfCreateExtension.php index 5da8fea..5c65a90 100644 --- a/DependencyInjection/SymfonyCmfCreateExtension.php +++ b/DependencyInjection/SymfonyCmfCreateExtension.php @@ -54,6 +54,8 @@ public function load(array $configs, ContainerBuilder $container) } $container->setParameter($this->getAlias().'.plain_text_types', $config['plain_text_types']); + $container->setParameter($this->getAlias().'.create_routes', $config['create_routes']); + if ($config['auto_mapping']) { foreach ($container->getParameter('kernel.bundles') as $class) { $bundle = new \ReflectionClass($class); diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 23f17f0..99a2332 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -19,6 +19,7 @@ %symfony_cmf_create.use_coffee% %symfony_cmf_create.fixed_toolbar% %symfony_cmf_create.plain_text_types% + %symfony_cmf_create.create_routes% %symfony_cmf_create.role% diff --git a/Resources/views/includejsfiles-create.html.twig b/Resources/views/includejsfiles-create.html.twig index 872670b..6043e9f 100644 --- a/Resources/views/includejsfiles-create.html.twig +++ b/Resources/views/includejsfiles-create.html.twig @@ -20,6 +20,15 @@ var cmfCreateHalloParentElement = 'body'; {% endif %} var cmfCreateHalloPlainTextTypes = {{ cmfCreateHalloPlainTextTypes|raw }}; + + {% if cmfCreateCreateRoutes %} + //TODO: get all these parameters from the config + var cmfCreateLocales = ['en', 'fr', 'de']; + var cmfCreateContentPrefix = '/cms/content'; + var cmfCreateRoutesPrefix = '/cms/routes'; + var cmfCreateRouteRdfType = "http://cmf.symfony.com/CmfRoute"; + {% endif %} + {% javascripts output="js/create.js" @@ -37,6 +46,14 @@ {% endjavascripts %} +{% if cmfCreateCreateRoutes %} +{% javascripts output="js/create.js" + '@SymfonyCmfCreateBundle/Resources/public/js/create-routes-handler.js' +%} + +{% endjavascripts %} +{% endif %} + {# to develop create use this instead of create-min '@SymfonyCmfCreateBundle/Resources/public/vendor/create/src/*.js' @@ -61,4 +78,4 @@ and set up assetic to handle coffee script files '@SymfonyCmfCreateBundle/Resources/public/js/init-vie-hallo.js' '@SymfonyCmfCreateBundle/Resources/public/vendor/hallo/hallo.coffee' '@SymfonyCmfCreateBundle/Resources/public/vendor/hallo/plugins/*.coffee' -#} \ No newline at end of file +#} From bf1da4a28d3238fb235f7f8e6938f2b01360245f Mon Sep 17 00:00:00 2001 From: Adrien Nicolet Date: Fri, 21 Dec 2012 14:21:58 +0100 Subject: [PATCH 02/13] WIP 1st functional version of create routes JS handler --- Resources/public/js/create-routes-handler.js | 61 ++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 Resources/public/js/create-routes-handler.js diff --git a/Resources/public/js/create-routes-handler.js b/Resources/public/js/create-routes-handler.js new file mode 100644 index 0000000..0bcfcf2 --- /dev/null +++ b/Resources/public/js/create-routes-handler.js @@ -0,0 +1,61 @@ +jQuery(document).ready(function() { + + (function(){ + + var needRouteCreation = false; //internal trigger for savedentity event + + //an entity has been saved and the response of the backend received + $('body').bind('midgardstoragesavedentity', function (event, options) { + + //create the routes only when new content has been saved + if (!needRouteCreation || options.entity.id.indexOf(cmfCreateRoutesPrefix, 1) == 1) { + return; + } + + var vie = options.entity.vie; + + /** + * Common request content + */ + var trimmedSubject = options.entity.id.substr(1, options.entity.id.length - 2); + var lastSlashPos = trimmedSubject.lastIndexOf("/") + 1; + var contentName = trimmedSubject.substr(lastSlashPos, trimmedSubject.length - lastSlashPos); + var partOf = options.entity.attributes[""].models[0]["@subject"]; + var trimmedPartOf = partOf.substr(1, partOf.length - 2); // "/cms/content/news" + var lastSlashPos = trimmedPartOf.lastIndexOf("/") + 1; + var parentName = trimmedPartOf.substr(lastSlashPos, trimmedPartOf.length - lastSlashPos); + + /** + * Request types + */ + var parentType = "<" + cmfCreateRouteRdfType + "/Parent" + ">"; + var nameType = "<" + cmfCreateRouteRdfType + "/Name" + ">"; + var routeContentType = "<" + cmfCreateRouteRdfType + "/RouteContent" + ">"; + var localeType = "<" + cmfCreateRouteRdfType + "/Locale" + ">"; + var partOfType = ""; + + for(var i in cmfCreateLocales) { + var parentPath = cmfCreateRoutesPrefix + "/" + cmfCreateLocales[i] + "/" + parentName; + + var routeRequest = {}; + routeRequest["@type"] = "<" + cmfCreateRouteRdfType + ">"; + routeRequest[nameType] = contentName; + routeRequest[routeContentType] = trimmedSubject; + routeRequest[partOfType] = [parentPath]; + routeRequest[localeType] = cmfCreateLocales[i]; + routeRequest[parentType] = parentPath; + + var routeEntity = new vie.Entity(); + routeEntity.set(routeRequest); + vie.entities.add(routeEntity); + jQuery('body').midgardStorage('saveRemote', routeEntity, options); + } + }); + + //an entity will be saved and sent to the backend + $('body').bind('midgardstoragesaveentity', function (event, options) { + //TODO: handle the case where new content and updated content is saved at the same time + needRouteCreation = options.entity.isNew(); + }); + })() +}); From 7cb10a0cc369918a722162dac1d0426e42d33759 Mon Sep 17 00:00:00 2001 From: Adrien Nicolet Date: Fri, 28 Dec 2012 10:16:02 +0100 Subject: [PATCH 03/13] automatic js config for locales, routes and content prefix --- Controller/JsloaderController.php | 21 +++++++++++++++---- Resources/config/services.xml | 1 + .../views/includejsfiles-create.html.twig | 7 +++---- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/Controller/JsloaderController.php b/Controller/JsloaderController.php index 3363e23..47372c5 100644 --- a/Controller/JsloaderController.php +++ b/Controller/JsloaderController.php @@ -6,6 +6,7 @@ FOS\RestBundle\View\View; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Security\Core\SecurityContextInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * This controller includes the correct twig file to bootstrap the javascript @@ -53,6 +54,11 @@ class JsloaderController */ private $createRoutes; + /** + * @var ContainerInterface + */ + private $container; + /** * Create the Controller @@ -80,7 +86,8 @@ public function __construct( $plainTextTypes = array(), $createRoutes = false, $requiredRole = "IS_AUTHENTICATED_ANONYMOUSLY", - SecurityContextInterface $securityContext = null + SecurityContextInterface $securityContext = null, + ContainerInterface $container ) { $this->viewHandler = $viewHandler; $this->stanbolUrl = $stanbolUrl; @@ -91,12 +98,13 @@ public function __construct( $this->createRoutes = $createRoutes; $this->requiredRole = $requiredRole; $this->securityContext = $securityContext; + $this->container = $container; } /** * Render js inclusion for create.js and dependencies and bootstrap code. * - * THe hallo editor is bundled with create.js and available automatically. + * The hallo editor is bundled with create.js and available automatically. * To use aloha, you need to download the zip, as explained in step 8 of * the README. * @@ -127,13 +135,18 @@ public function includeJSFilesAction($editor = 'hallo') throw new \InvalidArgumentException("Unknown editor '$editor' requested"); } + + $view->setData(array( 'cmfCreateStanbolUrl' => $this->stanbolUrl, 'cmfCreateImageUploadEnabled' => (boolean) $this->imageClass, 'cmfCreateHalloFixedToolbar' => (boolean) $this->fixedToolbar, 'cmfCreateHalloPlainTextTypes' => json_encode($this->plainTextTypes), - 'cmfCreateCreateRoutes' => (boolean) $this->createRoutes) - ); + 'cmfCreateCreateRoutes' => (boolean) $this->createRoutes, + 'cmfCreateLocales' => json_encode($this->container->getParameter('locales')), + 'cmfCreateContentPrefix' => $this->container->getParameter('symfony_cmf_content.content_basepath'), + 'cmfCreateRoutesPrefix' => $this->container->getParameter('symfony_cmf_routing_extra.routing_repositoryroot') + )); return $this->viewHandler->handle($view); } diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 99a2332..964c11f 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -22,6 +22,7 @@ %symfony_cmf_create.create_routes% %symfony_cmf_create.role% + diff --git a/Resources/views/includejsfiles-create.html.twig b/Resources/views/includejsfiles-create.html.twig index 6043e9f..cbac942 100644 --- a/Resources/views/includejsfiles-create.html.twig +++ b/Resources/views/includejsfiles-create.html.twig @@ -22,10 +22,9 @@ var cmfCreateHalloPlainTextTypes = {{ cmfCreateHalloPlainTextTypes|raw }}; {% if cmfCreateCreateRoutes %} - //TODO: get all these parameters from the config - var cmfCreateLocales = ['en', 'fr', 'de']; - var cmfCreateContentPrefix = '/cms/content'; - var cmfCreateRoutesPrefix = '/cms/routes'; + var cmfCreateLocales = {{ cmfCreateLocales|raw }}; + var cmfCreateContentPrefix = '{{ cmfCreateContentPrefix }}'; + var cmfCreateRoutesPrefix = '{{ cmfCreateRoutesPrefix }}'; var cmfCreateRouteRdfType = "http://cmf.symfony.com/CmfRoute"; {% endif %} From c3bfd1598037ff30e4fbbb4716b7b85137a423e4 Mon Sep 17 00:00:00 2001 From: Adrien Nicolet Date: Fri, 28 Dec 2012 10:37:40 +0100 Subject: [PATCH 04/13] move CmfRoute to the CreateBundle --- Document/CmfRoute.php | 31 +++++++++++++++++++ .../Cmf.CreateBundle.Document.CmfRoute.xml | 20 ++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 Document/CmfRoute.php create mode 100644 Resources/rdf-mappings/Cmf.CreateBundle.Document.CmfRoute.xml diff --git a/Document/CmfRoute.php b/Document/CmfRoute.php new file mode 100644 index 0000000..dd638bf --- /dev/null +++ b/Document/CmfRoute.php @@ -0,0 +1,31 @@ + + dcterms:partOfSymfony\Cmf\Bundle\CreateBundle\Document + + + + + + + + + + + From 96c7324acf524fcf943a3edd117ed09b96920c85 Mon Sep 17 00:00:00 2001 From: Adrien Nicolet Date: Fri, 28 Dec 2012 12:10:48 +0100 Subject: [PATCH 05/13] list of rdf types for which to create routes --- Controller/JsloaderController.php | 12 ++++++------ DependencyInjection/Configuration.php | 5 ++++- DependencyInjection/SymfonyCmfCreateExtension.php | 2 +- Resources/config/services.xml | 2 +- Resources/public/js/create-routes-handler.js | 12 ++++++++++-- Resources/views/includejsfiles-create.html.twig | 13 ++----------- 6 files changed, 24 insertions(+), 22 deletions(-) diff --git a/Controller/JsloaderController.php b/Controller/JsloaderController.php index 47372c5..a0b45d9 100644 --- a/Controller/JsloaderController.php +++ b/Controller/JsloaderController.php @@ -50,9 +50,9 @@ class JsloaderController private $plainTextTypes; /** - * @var Boolean + * @var array */ - private $createRoutes; + private $createRoutesForTypes; /** * @var ContainerInterface @@ -73,7 +73,7 @@ class JsloaderController * @param Boolean $useCoffee whether assetic is set up to use coffee script * @param Boolean $fixedToolbar whether the hallo toolbar is fixed or floating * @param array $plainTextTypes RDFa types to edit in raw text only - * @param Boolean $createRoutes whether the routes need to be created after content creation + * @param array $createRoutesForTypes types for which it is needed to create a route * @param string $requiredRole * @param SecurityContextInterface $securityContext */ @@ -84,7 +84,7 @@ public function __construct( $useCoffee = false, $fixedToolbar = true, $plainTextTypes = array(), - $createRoutes = false, + $createRoutesForTypes = array(), $requiredRole = "IS_AUTHENTICATED_ANONYMOUSLY", SecurityContextInterface $securityContext = null, ContainerInterface $container @@ -95,7 +95,7 @@ public function __construct( $this->coffee = $useCoffee; $this->fixedToolbar = $fixedToolbar; $this->plainTextTypes = $plainTextTypes; - $this->createRoutes = $createRoutes; + $this->createRoutesForTypes = $createRoutesForTypes; $this->requiredRole = $requiredRole; $this->securityContext = $securityContext; $this->container = $container; @@ -142,7 +142,7 @@ public function includeJSFilesAction($editor = 'hallo') 'cmfCreateImageUploadEnabled' => (boolean) $this->imageClass, 'cmfCreateHalloFixedToolbar' => (boolean) $this->fixedToolbar, 'cmfCreateHalloPlainTextTypes' => json_encode($this->plainTextTypes), - 'cmfCreateCreateRoutes' => (boolean) $this->createRoutes, + 'cmfCreateCreateRoutesTypes' => json_encode($this->createRoutesForTypes), 'cmfCreateLocales' => json_encode($this->container->getParameter('locales')), 'cmfCreateContentPrefix' => $this->container->getParameter('symfony_cmf_content.content_basepath'), 'cmfCreateRoutesPrefix' => $this->container->getParameter('symfony_cmf_routing_extra.routing_repositoryroot') diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 1e0977e..4699b6d 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -43,7 +43,10 @@ public function getConfigTreeBuilder() ->useAttributeAsKey('name') ->prototype('scalar')->end() ->end() - ->scalarNode('create_routes')->defaultFalse()->end() + ->arrayNode('create_routes_types') + ->useAttributeAsKey('name') + ->prototype('scalar')->end() + ->end() ->arrayNode('rdf_config_dirs') ->useAttributeAsKey('dir') ->prototype('scalar')->end() diff --git a/DependencyInjection/SymfonyCmfCreateExtension.php b/DependencyInjection/SymfonyCmfCreateExtension.php index 5c65a90..1e545a8 100644 --- a/DependencyInjection/SymfonyCmfCreateExtension.php +++ b/DependencyInjection/SymfonyCmfCreateExtension.php @@ -54,7 +54,7 @@ public function load(array $configs, ContainerBuilder $container) } $container->setParameter($this->getAlias().'.plain_text_types', $config['plain_text_types']); - $container->setParameter($this->getAlias().'.create_routes', $config['create_routes']); + $container->setParameter($this->getAlias().'.create_routes_types', $config['create_routes_types']); if ($config['auto_mapping']) { foreach ($container->getParameter('kernel.bundles') as $class) { diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 964c11f..76317ff 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -19,7 +19,7 @@ %symfony_cmf_create.use_coffee% %symfony_cmf_create.fixed_toolbar% %symfony_cmf_create.plain_text_types% - %symfony_cmf_create.create_routes% + %symfony_cmf_create.create_routes_types% %symfony_cmf_create.role% diff --git a/Resources/public/js/create-routes-handler.js b/Resources/public/js/create-routes-handler.js index 0bcfcf2..c5edec4 100644 --- a/Resources/public/js/create-routes-handler.js +++ b/Resources/public/js/create-routes-handler.js @@ -7,8 +7,16 @@ jQuery(document).ready(function() { //an entity has been saved and the response of the backend received $('body').bind('midgardstoragesavedentity', function (event, options) { - //create the routes only when new content has been saved - if (!needRouteCreation || options.entity.id.indexOf(cmfCreateRoutesPrefix, 1) == 1) { + var createdType = options.entity.attributes["@type"]; + var createRoutes = false; + for(var i in cmfCreateCreateRoutesTypes) { + if (createdType == "<" + cmfCreateCreateRoutesTypes[i] + ">") { + createRoutes = true; + break; + } + } + + if (!createRoutes || !needRouteCreation) { return; } diff --git a/Resources/views/includejsfiles-create.html.twig b/Resources/views/includejsfiles-create.html.twig index cbac942..01fc0a0 100644 --- a/Resources/views/includejsfiles-create.html.twig +++ b/Resources/views/includejsfiles-create.html.twig @@ -20,13 +20,11 @@ var cmfCreateHalloParentElement = 'body'; {% endif %} var cmfCreateHalloPlainTextTypes = {{ cmfCreateHalloPlainTextTypes|raw }}; - - {% if cmfCreateCreateRoutes %} + var cmfCreateCreateRoutesTypes = {{ cmfCreateCreateRoutesTypes|raw }}; var cmfCreateLocales = {{ cmfCreateLocales|raw }}; var cmfCreateContentPrefix = '{{ cmfCreateContentPrefix }}'; var cmfCreateRoutesPrefix = '{{ cmfCreateRoutesPrefix }}'; var cmfCreateRouteRdfType = "http://cmf.symfony.com/CmfRoute"; - {% endif %} @@ -41,17 +39,10 @@ '@SymfonyCmfCreateBundle/Resources/public/vendor/create/deps/jquery.tagsinput.min.js' '@SymfonyCmfCreateBundle/Resources/public/vendor/create/deps/annotate-min.js' '@SymfonyCmfCreateBundle/Resources/public/vendor/create/examples/create-min.js' -%} - -{% endjavascripts %} - -{% if cmfCreateCreateRoutes %} -{% javascripts output="js/create.js" '@SymfonyCmfCreateBundle/Resources/public/js/create-routes-handler.js' %} - + {% endjavascripts %} -{% endif %} {# to develop create use this instead of create-min From 3dfb241a19ff3c67e49092191bdc894f91cba7b8 Mon Sep 17 00:00:00 2001 From: Adrien Nicolet Date: Thu, 17 Jan 2013 13:00:22 +0100 Subject: [PATCH 06/13] fix typos --- Resources/rdf-mappings/Cmf.CreateBundle.Document.CmfRoute.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Resources/rdf-mappings/Cmf.CreateBundle.Document.CmfRoute.xml b/Resources/rdf-mappings/Cmf.CreateBundle.Document.CmfRoute.xml index 2437b8a..26fb36e 100644 --- a/Resources/rdf-mappings/Cmf.CreateBundle.Document.CmfRoute.xml +++ b/Resources/rdf-mappings/Cmf.CreateBundle.Document.CmfRoute.xml @@ -6,7 +6,7 @@ typeof="cmf:CmfRoute" itemtype="http://cmf.symfony.com/CmfRoute" > - dcterms:partOfSymfony\Cmf\Bundle\CreateBundle\Document + dcterms:partOf From 59aa2c531a8a2c0e5854a24fc0f712e3557ab93e Mon Sep 17 00:00:00 2001 From: Adrien Nicolet Date: Thu, 17 Jan 2013 17:23:30 +0100 Subject: [PATCH 07/13] handle the case where new content and updated content is saved at the same time --- Resources/public/js/create-routes-handler.js | 21 ++++++++++---------- Resources/public/vendor/create | 2 +- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/Resources/public/js/create-routes-handler.js b/Resources/public/js/create-routes-handler.js index c5edec4..b40ad64 100644 --- a/Resources/public/js/create-routes-handler.js +++ b/Resources/public/js/create-routes-handler.js @@ -2,23 +2,20 @@ jQuery(document).ready(function() { (function(){ - var needRouteCreation = false; //internal trigger for savedentity event + var createRouteForTypes = []; //types currently needing a route creation //an entity has been saved and the response of the backend received $('body').bind('midgardstoragesavedentity', function (event, options) { var createdType = options.entity.attributes["@type"]; - var createRoutes = false; - for(var i in cmfCreateCreateRoutesTypes) { - if (createdType == "<" + cmfCreateCreateRoutesTypes[i] + ">") { - createRoutes = true; - break; - } - } + //remove the enclosing <> + createdType = createdType.substring(1, createdType.length - 1); - if (!createRoutes || !needRouteCreation) { + if (!$.inArray(createdType, createRouteForTypes)) { return; } + //reset the types for which route creation is currently needed + createRouteForTypes.splice($.inArray(createdType, createRouteForTypes),1); var vie = options.entity.vie; @@ -62,8 +59,10 @@ jQuery(document).ready(function() { //an entity will be saved and sent to the backend $('body').bind('midgardstoragesaveentity', function (event, options) { - //TODO: handle the case where new content and updated content is saved at the same time - needRouteCreation = options.entity.isNew(); + if (options.entity.isNew() && + $.inArray(options.entity.attributes['@type'], cmfCreateCreateRoutesTypes)) { + createRouteForTypes.push(options.entity.attributes['@type']); + } }); })() }); diff --git a/Resources/public/vendor/create b/Resources/public/vendor/create index 271e011..0011a6f 160000 --- a/Resources/public/vendor/create +++ b/Resources/public/vendor/create @@ -1 +1 @@ -Subproject commit 271e0114a039ab256ffcceacdf7f361803995e05 +Subproject commit 0011a6faecbb9421d3bf19e7c85d147f8698da47 From 3ca869096863ec26b6fa21ea59b1d568e471655c Mon Sep 17 00:00:00 2001 From: Adrien Nicolet Date: Fri, 18 Jan 2013 17:40:58 +0100 Subject: [PATCH 08/13] WIP: use configurable mapper instead of CmfRoute document to set locale of route documents --- Controller/RestController.php | 2 +- DependencyInjection/Configuration.php | 4 ++ .../SymfonyCmfCreateExtension.php | 2 + Document/CmfRoute.php | 31 --------- Mapper/RouteDoctrinePhpcrOdmMapper.php | 33 +++++++++ Metadata/ContainerRdfTypeFactory.php | 67 +++++++++++++++++++ Resources/config/phpcr_odm.xml | 7 ++ Resources/config/services.xml | 16 ++++- ...Cmf.RoutingExtraBundle.Document.Route.xml} | 0 9 files changed, 128 insertions(+), 34 deletions(-) delete mode 100644 Document/CmfRoute.php create mode 100644 Mapper/RouteDoctrinePhpcrOdmMapper.php create mode 100644 Metadata/ContainerRdfTypeFactory.php rename Resources/rdf-mappings/{Cmf.CreateBundle.Document.CmfRoute.xml => Cmf.RoutingExtraBundle.Document.Route.xml} (100%) diff --git a/Controller/RestController.php b/Controller/RestController.php index bb52f76..e9daf20 100755 --- a/Controller/RestController.php +++ b/Controller/RestController.php @@ -115,8 +115,8 @@ public function postDocumentAction(Request $request) $this->performSecurityChecks(); $rdfType = trim($request->request->get('@type'), '<>'); - $type = $this->typeFactory->getTypeByRdf($rdfType); + $type = $this->typeFactory->getTypeByRdf($rdfType); $result = $this->restHandler->run($request->request->all(), $type, null, RestService::HTTP_POST); if (!is_null($result)) { diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 4699b6d..99378c1 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -27,6 +27,10 @@ public function getConfigTreeBuilder() ->useAttributeAsKey('name') ->prototype('scalar')->end() ->end() + ->arrayNode('rdfmapper') + ->useAttributeAsKey('name') + ->prototype('scalar')->end() + ->end() ->scalarNode('role')->defaultValue('IS_AUTHENTICATED_ANONYMOUSLY')->end() ->arrayNode('image') ->canBeUnset() diff --git a/DependencyInjection/SymfonyCmfCreateExtension.php b/DependencyInjection/SymfonyCmfCreateExtension.php index 1e545a8..c345ddc 100644 --- a/DependencyInjection/SymfonyCmfCreateExtension.php +++ b/DependencyInjection/SymfonyCmfCreateExtension.php @@ -41,6 +41,8 @@ public function load(array $configs, ContainerBuilder $container) $container->setParameter($this->getAlias().'.map', $config['map']); + $container->setParameter($this->getAlias().'.rdfmapper', $config['rdfmapper']); + $container->setParameter($this->getAlias().'.stanbol_url', $config['stanbol_url']); $container->setParameter($this->getAlias().'.role', $config['role']); diff --git a/Document/CmfRoute.php b/Document/CmfRoute.php deleted file mode 100644 index dd638bf..0000000 --- a/Document/CmfRoute.php +++ /dev/null @@ -1,31 +0,0 @@ -getIdentifier() === 'locale') { + $object->setDefault('_locale', $value); + $object->setRequirement('_locale', $value); + return $object; + } + return parent::setPropertyValue($object, $property, $value); + } + + public function getPropertyValue($object, PropertyInterface $property) + { + if ($object instanceof Route && $property->getIdentifier() === 'locale') { + return $object->getDefault('_locale'); + } + return parent::getPropertyValue($object, $property); + } +} diff --git a/Metadata/ContainerRdfTypeFactory.php b/Metadata/ContainerRdfTypeFactory.php new file mode 100644 index 0000000..06aaf5e --- /dev/null +++ b/Metadata/ContainerRdfTypeFactory.php @@ -0,0 +1,67 @@ +mapperServices = $mapperServices; + parent::__construct($defaultMapper, $driver); + } + + /** + * TODO: update the comment + * Get the mapper for type $name, or the defaultMapper if there is no specific mapper. + * + * @param string $name the type name for which to get the mapper + * + * @return RdfMapperInterface + */ + protected function getMapper($name) + { + if (isset($this->mapperServices[$name])) { + return $this->container->get($this->mapperServices[$name]); + } + + return parent::getMapper($name); + } + + /** + * @api + */ + public function setContainer(ContainerInterface $container = null) + { + $this->container = $container; + } +} diff --git a/Resources/config/phpcr_odm.xml b/Resources/config/phpcr_odm.xml index ef12ed4..cf5bc58 100755 --- a/Resources/config/phpcr_odm.xml +++ b/Resources/config/phpcr_odm.xml @@ -6,6 +6,7 @@ Midgard\CreatePHP\Mapper\DoctrinePhpcrOdmMapper + Symfony\Cmf\Bundle\CreateBundle\Mapper\RouteDoctrinePhpcrOdmMapper @@ -16,5 +17,11 @@ null + + %symfony_cmf_create.map% + + null + + diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 76317ff..23f95ad 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -8,6 +8,7 @@ Symfony\Cmf\Bundle\CreateBundle\Controller\JsloaderController Symfony\Cmf\Bundle\CreateBundle\Controller\RestController Midgard\CreatePHP\Metadata\RdfTypeFactory + Symfony\Cmf\Bundle\CreateBundle\Metadata\ContainerRdfTypeFactory Midgard\CreatePHP\RestService @@ -28,14 +29,14 @@ - + %symfony_cmf_create.role% - + @@ -43,11 +44,22 @@ %symfony_cmf_create.rdf_config_dirs% + + + + + + %symfony_cmf_create.rdfmapper% + + + + diff --git a/Resources/rdf-mappings/Cmf.CreateBundle.Document.CmfRoute.xml b/Resources/rdf-mappings/Cmf.RoutingExtraBundle.Document.Route.xml similarity index 100% rename from Resources/rdf-mappings/Cmf.CreateBundle.Document.CmfRoute.xml rename to Resources/rdf-mappings/Cmf.RoutingExtraBundle.Document.Route.xml From 544d7e01b0af5e4650771441e9cca873fca901e0 Mon Sep 17 00:00:00 2001 From: Adrien Nicolet Date: Sat, 19 Jan 2013 11:18:17 +0100 Subject: [PATCH 09/13] cleaning and comments after createphp RdfTypeFactory refactoring --- Metadata/ContainerRdfTypeFactory.php | 11 ++++------- Resources/config/services.xml | 8 -------- ....Cmf.Bundle.RoutingExtraBundle.Document.Route.xml} | 0 3 files changed, 4 insertions(+), 15 deletions(-) rename Resources/rdf-mappings/{Cmf.RoutingExtraBundle.Document.Route.xml => Symfony.Cmf.Bundle.RoutingExtraBundle.Document.Route.xml} (100%) diff --git a/Metadata/ContainerRdfTypeFactory.php b/Metadata/ContainerRdfTypeFactory.php index 06aaf5e..0c75feb 100644 --- a/Metadata/ContainerRdfTypeFactory.php +++ b/Metadata/ContainerRdfTypeFactory.php @@ -12,8 +12,8 @@ use Midgard\CreatePHP\Metadata\RdfDriverInterface; /** - * TODO: comment ... - * Use container to get instances of mappers instead of class names + * Factory for createphp types based on class names. If available, the mappers + * for the requested class are loaded from the Symfony service container. */ class ContainerRdfTypeFactory extends RdfTypeFactory implements ContainerAwareInterface { @@ -24,8 +24,6 @@ class ContainerRdfTypeFactory extends RdfTypeFactory implements ContainerAwareIn /** * @var ContainerInterface - * - * @api */ protected $container; @@ -41,8 +39,7 @@ public function __construct(RdfMapperInterface $defaultMapper, RdfDriverInterfac } /** - * TODO: update the comment - * Get the mapper for type $name, or the defaultMapper if there is no specific mapper. + * Get the mapper for type $name in the $symfony container, or the defaultMapper if there is no specific one * * @param string $name the type name for which to get the mapper * @@ -58,7 +55,7 @@ protected function getMapper($name) } /** - * @api + * @see ContainerAwareInterface::setContainer() */ public function setContainer(ContainerInterface $container = null) { diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 23f95ad..5825edf 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -44,14 +44,6 @@ %symfony_cmf_create.rdf_config_dirs% - - diff --git a/Resources/rdf-mappings/Cmf.RoutingExtraBundle.Document.Route.xml b/Resources/rdf-mappings/Symfony.Cmf.Bundle.RoutingExtraBundle.Document.Route.xml similarity index 100% rename from Resources/rdf-mappings/Cmf.RoutingExtraBundle.Document.Route.xml rename to Resources/rdf-mappings/Symfony.Cmf.Bundle.RoutingExtraBundle.Document.Route.xml From b154d18b78ee41566a112b86b0cd794315f6c633 Mon Sep 17 00:00:00 2001 From: Adrien Nicolet Date: Sat, 19 Jan 2013 15:17:12 +0100 Subject: [PATCH 10/13] improve error handling for frontend route creation --- Controller/RestController.php | 10 +++++-- Resources/public/js/create-routes-handler.js | 30 +++++++++++++++----- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/Controller/RestController.php b/Controller/RestController.php index e9daf20..5893cdd 100755 --- a/Controller/RestController.php +++ b/Controller/RestController.php @@ -117,14 +117,20 @@ public function postDocumentAction(Request $request) $rdfType = trim($request->request->get('@type'), '<>'); $type = $this->typeFactory->getTypeByRdf($rdfType); - $result = $this->restHandler->run($request->request->all(), $type, null, RestService::HTTP_POST); + + $result = null; + try { + $result = $this->restHandler->run($request->request->all(), $type, null, RestService::HTTP_POST); + } catch (\Exception $e) { + return Response::create("The document of type \"$rdfType\" could not be created: " . $e->getMessage(), 500); + } if (!is_null($result)) { $view = View::create($result)->setFormat('json'); return $this->viewHandler->handle($view, $request); } - return Response::create('The document could not be created', 500); + return Response::create("The document \"$rdfType\" could not be created", 500); } /** diff --git a/Resources/public/js/create-routes-handler.js b/Resources/public/js/create-routes-handler.js index b40ad64..427f57b 100644 --- a/Resources/public/js/create-routes-handler.js +++ b/Resources/public/js/create-routes-handler.js @@ -4,14 +4,17 @@ jQuery(document).ready(function() { var createRouteForTypes = []; //types currently needing a route creation + //remove the enclosing <> + function trimAttribute(value) { + return value.substring(1, value.length - 1); + } + //an entity has been saved and the response of the backend received $('body').bind('midgardstoragesavedentity', function (event, options) { - var createdType = options.entity.attributes["@type"]; - //remove the enclosing <> - createdType = createdType.substring(1, createdType.length - 1); + var createdType = trimAttribute(options.entity.attributes["@type"]); - if (!$.inArray(createdType, createRouteForTypes)) { + if ($.inArray(createdType, createRouteForTypes) == -1) { return; } //reset the types for which route creation is currently needed @@ -53,15 +56,28 @@ jQuery(document).ready(function() { var routeEntity = new vie.Entity(); routeEntity.set(routeRequest); vie.entities.add(routeEntity); - jQuery('body').midgardStorage('saveRemote', routeEntity, options); + jQuery('body').midgardStorage('saveRemote', routeEntity, { + success: function (m, err) { + jQuery('body').midgardNotifications('create', { + body: 'Route ' + m.attributes[routeContentType] + ' created successfully' + }); + }, + error: function (m, err) { + jQuery('body').midgardNotifications('create', { + body: 'Error during creation of route ' + m.attributes[partOfType] + '/' + m.attributes[nameType] + '. ' + err.responseText, + timeout: 0 + }); + } + }); } }); //an entity will be saved and sent to the backend $('body').bind('midgardstoragesaveentity', function (event, options) { + var type = trimAttribute(options.entity.attributes['@type']); if (options.entity.isNew() && - $.inArray(options.entity.attributes['@type'], cmfCreateCreateRoutesTypes)) { - createRouteForTypes.push(options.entity.attributes['@type']); + $.inArray(type, cmfCreateCreateRoutesTypes) != -1) { + createRouteForTypes.push(type); } }); })() From 34115e09ab1da42e45a0f043b8fe61ec8b75d959 Mon Sep 17 00:00:00 2001 From: Adrien Nicolet Date: Wed, 23 Jan 2013 14:58:54 +0100 Subject: [PATCH 11/13] various fixes after lsmith77 review --- Controller/JsloaderController.php | 37 ++++++++++++++++++++-------- Controller/RestController.php | 2 +- Metadata/ContainerRdfTypeFactory.php | 6 ++--- Resources/config/services.xml | 4 ++- 4 files changed, 34 insertions(+), 15 deletions(-) diff --git a/Controller/JsloaderController.php b/Controller/JsloaderController.php index a0b45d9..1c46837 100644 --- a/Controller/JsloaderController.php +++ b/Controller/JsloaderController.php @@ -55,9 +55,19 @@ class JsloaderController private $createRoutesForTypes; /** - * @var ContainerInterface + * @var array locales of the application */ - private $container; + private $locales; + + /** + * @var string content prefix in the repository path, like /cms/content + */ + private $contentPrefix; + + /** + * @var string routes prefix in the repository path, like /cms/routes + */ + private $routesPrefix; /** @@ -74,8 +84,11 @@ class JsloaderController * @param Boolean $fixedToolbar whether the hallo toolbar is fixed or floating * @param array $plainTextTypes RDFa types to edit in raw text only * @param array $createRoutesForTypes types for which it is needed to create a route - * @param string $requiredRole + * @param string $requiredRole the role name for the security check * @param SecurityContextInterface $securityContext + * @param array $locales the locales of the application + * @param string $contentPrefix content prefix in the repository path + * @param string $routesPrefix routes prefix in the repository path */ public function __construct( ViewHandlerInterface $viewHandler, @@ -83,11 +96,13 @@ public function __construct( $imageClass, $useCoffee = false, $fixedToolbar = true, - $plainTextTypes = array(), - $createRoutesForTypes = array(), + array $plainTextTypes = array(), + array $createRoutesForTypes = array(), $requiredRole = "IS_AUTHENTICATED_ANONYMOUSLY", SecurityContextInterface $securityContext = null, - ContainerInterface $container + array $locales, + $contentPrefix, + $routesPrefix ) { $this->viewHandler = $viewHandler; $this->stanbolUrl = $stanbolUrl; @@ -98,7 +113,9 @@ public function __construct( $this->createRoutesForTypes = $createRoutesForTypes; $this->requiredRole = $requiredRole; $this->securityContext = $securityContext; - $this->container = $container; + $this->locales = $locales; + $this->contentPrefix = $contentPrefix; + $this->routesPrefix = $routesPrefix; } /** @@ -143,9 +160,9 @@ public function includeJSFilesAction($editor = 'hallo') 'cmfCreateHalloFixedToolbar' => (boolean) $this->fixedToolbar, 'cmfCreateHalloPlainTextTypes' => json_encode($this->plainTextTypes), 'cmfCreateCreateRoutesTypes' => json_encode($this->createRoutesForTypes), - 'cmfCreateLocales' => json_encode($this->container->getParameter('locales')), - 'cmfCreateContentPrefix' => $this->container->getParameter('symfony_cmf_content.content_basepath'), - 'cmfCreateRoutesPrefix' => $this->container->getParameter('symfony_cmf_routing_extra.routing_repositoryroot') + 'cmfCreateLocales' => json_encode($this->locales), + 'cmfCreateContentPrefix' => $this->contentPrefix, + 'cmfCreateRoutesPrefix' => $this->routesPrefix )); return $this->viewHandler->handle($view); diff --git a/Controller/RestController.php b/Controller/RestController.php index 5893cdd..9ba5535 100755 --- a/Controller/RestController.php +++ b/Controller/RestController.php @@ -130,7 +130,7 @@ public function postDocumentAction(Request $request) return $this->viewHandler->handle($view, $request); } - return Response::create("The document \"$rdfType\" could not be created", 500); + return Response::create("The document '$rdfType' could not be created", 500); } /** diff --git a/Metadata/ContainerRdfTypeFactory.php b/Metadata/ContainerRdfTypeFactory.php index 0c75feb..d7ca433 100644 --- a/Metadata/ContainerRdfTypeFactory.php +++ b/Metadata/ContainerRdfTypeFactory.php @@ -18,7 +18,7 @@ class ContainerRdfTypeFactory extends RdfTypeFactory implements ContainerAwareInterface { /** - * @var array service names of the mappers + * @var array service names of the mappers per type */ private $mapperServices; @@ -30,9 +30,9 @@ class ContainerRdfTypeFactory extends RdfTypeFactory implements ContainerAwareIn /** * @param RdfMapperInterface $defaultMapper the default mapper to use if there is no specific one * @param RdfDriverInterface $driver the driver to load types from - * @param array $mappers rdf mappers per service name + * @param array $mapperServices rdf mappers service names per type */ - public function __construct(RdfMapperInterface $defaultMapper, RdfDriverInterface $driver, $mapperServices = array()) + public function __construct(RdfMapperInterface $defaultMapper, RdfDriverInterface $driver, array $mapperServices = array()) { $this->mapperServices = $mapperServices; parent::__construct($defaultMapper, $driver); diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 5825edf..ee96064 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -23,7 +23,9 @@ %symfony_cmf_create.create_routes_types% %symfony_cmf_create.role% - + %locales% + %symfony_cmf_content.content_basepath% + %symfony_cmf_routing_extra.routing_repositoryroot% From d436362d40acc5967a818dc78b4d000cb39f5d0f Mon Sep 17 00:00:00 2001 From: Adrien Nicolet Date: Mon, 28 Jan 2013 12:29:03 +0100 Subject: [PATCH 12/13] tmp fix for create.js bug --- Resources/public/js/create-routes-handler.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Resources/public/js/create-routes-handler.js b/Resources/public/js/create-routes-handler.js index 427f57b..1ff24d5 100644 --- a/Resources/public/js/create-routes-handler.js +++ b/Resources/public/js/create-routes-handler.js @@ -6,6 +6,10 @@ jQuery(document).ready(function() { //remove the enclosing <> function trimAttribute(value) { + if (value instanceof Array) { + //Create.js sometimes adds the owl#Thing type without reasons + value = value[value.length - 1]; + } return value.substring(1, value.length - 1); } From 2468b8a85af3261eaacbd615a26a6109bf93d978 Mon Sep 17 00:00:00 2001 From: Adrien Nicolet Date: Tue, 29 Jan 2013 17:42:27 +0100 Subject: [PATCH 13/13] typos on error messages --- Controller/RestController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Controller/RestController.php b/Controller/RestController.php index 9ba5535..2114ac8 100755 --- a/Controller/RestController.php +++ b/Controller/RestController.php @@ -122,7 +122,7 @@ public function postDocumentAction(Request $request) try { $result = $this->restHandler->run($request->request->all(), $type, null, RestService::HTTP_POST); } catch (\Exception $e) { - return Response::create("The document of type \"$rdfType\" could not be created: " . $e->getMessage(), 500); + return Response::create("The document '$rdfType' could not be created: " . $e->getMessage(), 500); } if (!is_null($result)) {