Skip to content
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions Controller/JsloaderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -48,6 +49,16 @@ class JsloaderController
*/
private $plainTextTypes;

/**
* @var array
*/
private $createRoutesForTypes;

/**
* @var ContainerInterface
*/
private $container;


/**
* Create the Controller
Expand All @@ -62,6 +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 array $createRoutesForTypes types for which it is needed to create a route
* @param string $requiredRole
* @param SecurityContextInterface $securityContext
*/
Expand All @@ -72,24 +84,27 @@ public function __construct(
$useCoffee = false,
$fixedToolbar = true,
$plainTextTypes = array(),
$createRoutesForTypes = array(),
$requiredRole = "IS_AUTHENTICATED_ANONYMOUSLY",
SecurityContextInterface $securityContext = null
SecurityContextInterface $securityContext = null,
ContainerInterface $container
) {
$this->viewHandler = $viewHandler;
$this->stanbolUrl = $stanbolUrl;
$this->imageClass = $imageClass;
$this->coffee = $useCoffee;
$this->fixedToolbar = $fixedToolbar;
$this->plainTextTypes = $plainTextTypes;

$this->createRoutesForTypes = $createRoutesForTypes;
$this->requiredRole = $requiredRole;
$this->securityContext = $securityContext;
$this->container = $container;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would prefer you inject the parameters explicitly .. if you want you can inject them all as an array

}

/**
* 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.
*
Expand Down Expand Up @@ -120,12 +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))
);
'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')
));

return $this->viewHandler->handle($view);
}
Expand Down
4 changes: 4 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public function getConfigTreeBuilder()
->useAttributeAsKey('name')
->prototype('scalar')->end()
->end()
->arrayNode('create_routes_types')
->useAttributeAsKey('name')
->prototype('scalar')->end()
->end()
->arrayNode('rdf_config_dirs')
->useAttributeAsKey('dir')
->prototype('scalar')->end()
Expand Down
2 changes: 2 additions & 0 deletions DependencyInjection/SymfonyCmfCreateExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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_types', $config['create_routes_types']);

if ($config['auto_mapping']) {
foreach ($container->getParameter('kernel.bundles') as $class) {
$bundle = new \ReflectionClass($class);
Expand Down
31 changes: 31 additions & 0 deletions Document/CmfRoute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Symfony\Cmf\Bundle\CreateBundle\Document;

use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCRODM;

use Symfony\Cmf\Bundle\RoutingExtraBundle\Document\Route;

/**
* @PHPCRODM\Document
*/
class CmfRoute extends Route
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we really need a new document here? this could become tricky.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i wonder if we can handle that generically. or otherwise move it into the RoutingExtraBundle as it is useful in general?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes this seems like just adding 2 convenience methods, think it will however cause more trouble than its worth.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well the problem will be the mapper that is guessing a method name from local and has no way of knowing how to correctly map a locale otherwise. but maybe we should extend the mapper for the route and handle that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i discussed with adou and we will make another PR on createphp and make it possible to configure specific mappers for some classes and provide a mapper that handles locale on the route. this solves a general shortcoming of createphp then and is more elegant.

{
/**
* Set the _locale requirement and the default _locale
* @param array $locale
*/
public function setLocale($locale) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is a CS violation here.

parent::setDefault('_locale', $locale);
parent::setRequirement('_locale', $locale);
}

/**
* Get the default _locale for this route
* @return string
*/
public function getLocale() {
return parent::getDefault('_locale');
}

}
2 changes: 2 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
<argument>%symfony_cmf_create.use_coffee%</argument>
<argument>%symfony_cmf_create.fixed_toolbar%</argument>
<argument>%symfony_cmf_create.plain_text_types%</argument>
<argument>%symfony_cmf_create.create_routes_types%</argument>
<argument>%symfony_cmf_create.role%</argument>
<argument type="service" id="security.context" on-invalid="ignore"/>
<argument type="service" id="service_container" />
</service>

<service id="symfony_cmf_create.rest.controller" class="%symfony_cmf_create.rest.controller.class%">
Expand Down
68 changes: 68 additions & 0 deletions Resources/public/js/create-routes-handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
jQuery(document).ready(function() {

(function(){

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"];
//remove the enclosing <>
createdType = createdType.substring(1, createdType.length - 1);

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;

/**
* 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["<http://purl.org/dc/terms/partOf>"].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 = "<http://purl.org/dc/terms/partOf>";

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) {
if (options.entity.isNew() &&
$.inArray(options.entity.attributes['@type'], cmfCreateCreateRoutesTypes)) {
createRouteForTypes.push(options.entity.attributes['@type']);
}
});
})()
});
20 changes: 20 additions & 0 deletions Resources/rdf-mappings/Cmf.CreateBundle.Document.CmfRoute.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<type
xmlns:dcterms="http://purl.org/dc/terms/"
xmlns:cmf="http://cmf.symfony.com/"
xmlns:route="http://cmf.symfony.com/CmfRoute/"
xmlns:skos="http://www.w3.org/2004/02/skos/core#"
typeof="cmf:CmfRoute"
itemtype="http://cmf.symfony.com/CmfRoute"
>
<rev>dcterms:partOf</rev>
<children>
<property property="route:Parent" identifier="parent">
<config key="doctrine:reference" value=""/>
</property>
<property property="route:RouteContent" identifier="routeContent">
<config key="doctrine:reference" value=""/>
</property>
<property property="route:Name" identifier="name" />
<property property="route:Locale" identifier="locale" />
</children>
</type>
9 changes: 8 additions & 1 deletion Resources/views/includejsfiles-create.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
var cmfCreateHalloParentElement = 'body';
{% endif %}
var cmfCreateHalloPlainTextTypes = {{ cmfCreateHalloPlainTextTypes|raw }};
var cmfCreateCreateRoutesTypes = {{ cmfCreateCreateRoutesTypes|raw }};
var cmfCreateLocales = {{ cmfCreateLocales|raw }};
var cmfCreateContentPrefix = '{{ cmfCreateContentPrefix }}';
var cmfCreateRoutesPrefix = '{{ cmfCreateRoutesPrefix }}';
var cmfCreateRouteRdfType = "http://cmf.symfony.com/CmfRoute";

</script>

{% javascripts output="js/create.js"
Expand All @@ -33,6 +39,7 @@
'@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'
'@SymfonyCmfCreateBundle/Resources/public/js/create-routes-handler.js'
%}
<script src="{{ asset_url }}"></script>
{% endjavascripts %}
Expand Down Expand Up @@ -61,4 +68,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'
#}
#}