Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/1.5 load widget entities form in ajax #385

Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
namespace Victoire\Bundle\BusinessEntityBundle\Exception;

/**
* Triggered when at least one Business entity instance is needed
* Triggered when at least one Business entity instance is needed.
*/
class MissingBusinessEntityInstanceException extends \Exception
{

/**
* MissingBusinessEntityException constructor.
*
Expand All @@ -20,4 +19,4 @@ public function __construct($className)
$className
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ private static function postProcessText($text, $separator, $excludeTwig)
$text = strtolower($text);
}
if (!$excludeTwig) {
;
$text = preg_replace('/\W/', ' ', $text);
}
$text = preg_replace('/::/', '/', $text);
Expand Down
2 changes: 1 addition & 1 deletion Bundle/CoreBundle/Entity/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ public function removeWidgetMap(WidgetMap $widgetMap)
public function getWidgetsIds()
{
$widgetIds = [];
foreach($this->getBuiltWidgetMap() as $slot => $_widgetMaps) {
foreach ($this->getBuiltWidgetMap() as $slot => $_widgetMaps) {
foreach ($_widgetMaps as $widgetMap) {
$widgetIds[] = $widgetMap->getWidget()->getId();
}
Expand Down
15 changes: 15 additions & 0 deletions Bundle/CoreBundle/Resources/public/js/edit/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,21 @@ function generateNewWidgetUrl(select){
);
}


$vic(document).on('click', '.load-business-form', function(e){
e.preventDefault();
var self = $vic(this);
var target = self.attr('href');
$vic.ajax({
url : self.data('business-entity-form-url'),
success: function(jsonResponse)
{
self.removeClass('load-business-form');
$(target).append(jsonResponse.html);
}
});

});
//Update View css file if hash is returned
function updateViewCssHash(response) {
if(response.viewCssHash) {
Expand Down
13 changes: 13 additions & 0 deletions Bundle/CoreBundle/Resources/views/Widget/Form/_entity.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<ul class="vic-nav vic-nav-tabs vic-modal-nav-tabs">
<li class="{% if widget.mode == constant('MODE_ENTITY', widget) or widget.mode == constant('MODE_STATIC', widget) %}vic-active {% endif %}vic-seperate"><a href="#picker-{{ class|lower }}-entity" data-toggle="vic-tab">{{ ('form.widget.tab.picker.label')|trans({}, 'victoire') }}</a></li>
<li{% if widget.mode == constant('MODE_QUERY', widget) %} class="vic-active"{% endif %}><a href="#picker-{{ class|lower }}-query" data-toggle="vic-tab">{{ ('form.widget.tab.query.label')|trans({}, 'victoire') }}</a></li>

{% if is_business_entity_allowed(class|lower, view) %}
<li{% if widget.mode == constant('MODE_BUSINESS_ENTITY', widget) %} class="vic-active"{% endif %}><a href="#picker-{{ class|lower }}-businessEntity" data-toggle="vic-tab">{{ ('form.widget.tab.entity_instance.label')|trans({}, 'victoire') }}</a></li>
{% endif %}
</ul>
<div class="vic-tab-content vic-modal-tab-content">
{% for subForm in entityForms %}
{{ subForm|raw }}
{% endfor %}
</div>
54 changes: 26 additions & 28 deletions Bundle/CoreBundle/Resources/views/Widget/Form/new.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,43 @@

{% if classes|length %}
<ul class="vic-nav vic-nav-tabs vic-modal-nav-tabs">
<li class="{% if constant('MODE_STATIC', widget) == widget.mode %}vic-active {% endif %}vic-seperate"><a href="#picker-static" data-toggle="vic-tab">{{ ('form.widget.tab.manual.label')|trans({}, 'victoire') }}</a></li>
{% for class, namespace in classes %}
{% if namespace.disable %}
<li class="{% if constant('MODE_STATIC', widget) == widget.mode or not entityForms %}vic-active {% endif %}vic-seperate"><a href="#picker-static" data-toggle="vic-tab">{{ ('form.widget.tab.manual.label')|trans({}, 'victoire') }}</a></li>
{% for businessEntity in classes %}
{% set class = businessEntity.id %}
{% if businessEntity.disable %}
<li class="vic-disable"><span title="{{ ('form.widget.tab.entity.disable.label')|trans({}, 'victoire') }}">{{ ('form.widget.tab.' ~ class|lower ~ '.label')|trans({}, 'victoire') }}</span></li>
{% else %}
<li class="{% if not loop.last %}vic-seperate{% endif %}{% if widget.businessEntityId == class|lower %} vic-active{% endif %}"><a href="#picker-{{ class|lower }}" data-toggle="vic-tab">{{ ('form.widget.tab.' ~ class|lower ~ '.label')|trans({}, 'victoire') }}</a></li>

<li class="{% if not loop.last %}vic-seperate{% endif %}{% if widget.businessEntityId == class|lower and entityForms %} vic-active{% endif %}">
{% set keyToEntity = widget.id ? 'victoire_core_widget_entity_edit' : 'victoire_core_widget_entity_create' %}
Copy link
Contributor

Choose a reason for hiding this comment

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

please rename into routeName

{% set position = position == '' ? null : position %}
{% set urlToload = path( keyToEntity, {'viewReference':view.reference.id, 'slot': slot, 'position': position, 'parentWidgetMap': parentWidgetMap, 'businessEntityId': class, 'type' : type, 'widget_id': widget.id }) %}
<a href="#picker-{{ class|lower }}" {% if not widget.businessEntityId or (widget.businessEntityId != class|lower or not entityForms) %} class="load-business-form" data-business-entity-form-url="{{ urlToload }}" {% endif %} data-toggle="vic-tab">
{{ ('form.widget.tab.' ~ class|lower ~ '.label')|trans({}, 'victoire') }}
</a>
</li>
{% endif %}
{% endfor %}
</ul>
{% endif %}

<div class="vic-tab-content vic-modal-tab-content">
{% for class, form in forms %}
{% set isActive = class == widget.mode or widget.businessEntityId == class|lower and widget.mode == constant('MODE_BUSINESS_ENTITY', widget) %}
<div class="vic-tab-pane{% if isActive %} vic-active{% endif %}" id="picker-{{ class|lower }}">

{% if class != constant('MODE_STATIC', widget) and is_granted('ROLE_VICTOIRE_DEVELOPER') %}
<ul class="vic-nav vic-nav-tabs vic-modal-nav-tabs">
<li class="{% if widget.mode == constant('MODE_ENTITY', widget) %}vic-active {% endif %}vic-seperate"><a href="#picker-{{ class|lower }}-entity" data-toggle="vic-tab">{{ ('form.widget.tab.picker.label')|trans({}, 'victoire') }}</a></li>
<li{% if widget.mode == constant('MODE_QUERY', widget) %} class="vic-active"{% endif %}><a href="#picker-{{ class|lower }}-query" data-toggle="vic-tab">{{ ('form.widget.tab.query.label')|trans({}, 'victoire') }}</a></li>

{% if is_business_entity_allowed(class|lower, view) %}
<li{% if widget.mode == constant('MODE_BUSINESS_ENTITY', widget) %} class="vic-active"{% endif %}><a href="#picker-{{ class|lower }}-businessEntity" data-toggle="vic-tab">{{ ('form.widget.tab.entity_instance.label')|trans({}, 'victoire') }}</a></li>
{% endif %}
</ul>
<div class="vic-tab-content vic-modal-tab-content">
{% for subForm in form %}
{{ subForm|raw }}
{% endfor %}
</div>
{% else %}
{% for subForm in form %}
{{ subForm|raw }}
{% endfor %}
{% endif %}
{% if is_granted('ROLE_VICTOIRE_DEVELOPER') %}
<div class="vic-tab-pane{% if widget.mode == constant('MODE_STATIC', widget) or not entityForms %} vic-active{% endif %}" id="picker-{{ constant('MODE_STATIC', widget)|lower }}">
{{ staticForm|raw }}
</div>
{% endfor %}
{% endif %}
{% for businessEntity in classes %}
{% set class = businessEntity.id %}
{% set isActive = class == widget.mode or widget.businessEntityId == class|lower and widget.mode == constant('MODE_BUSINESS_ENTITY', widget) %}
<div class="vic-tab-pane{% if widget.businessEntityId and widget.businessEntityId == class and entityForms %} vic-active{% endif %}" id="picker-{{ class|lower }}">
{% if widget.businessEntityId and widget.businessEntityId == class and entityForms %}
{% include 'VictoireCoreBundle:Widget:Form/_entity.html.twig' %}
{% endif %}
</div>
{% endfor %}
</div>

{% endblock modal_body_content %}

{% block action %}create{% endblock action %}
59 changes: 14 additions & 45 deletions Bundle/WidgetBundle/Builder/WidgetFormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Doctrine\Common\Util\ClassUtils;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\Form\Form;
use Victoire\Bundle\BusinessEntityBundle\Entity\BusinessEntity;
use Victoire\Bundle\CoreBundle\Entity\View;
use Victoire\Bundle\CoreBundle\Event\WidgetBuildFormEvent;
use Victoire\Bundle\CoreBundle\VictoireCmsEvents;
Expand Down Expand Up @@ -60,7 +59,7 @@ public function renderNewForm($form, $widget, $slot, View $view, $entity = null)
*
* @return form
*/
public function renderForm(Form $form, Widget $widget, $entity = null)
public function renderForm(Form $form, Widget $widget, $slot = null, View $view = null, $entity = null)
{
//the template displayed is in the widget bundle
$templateName = $this->container->get('victoire_widget.widget_helper')->getTemplateName('edit', $widget);
Expand All @@ -77,43 +76,6 @@ public function renderForm(Form $form, Widget $widget, $entity = null)
);
}

/**
* Generates new forms for each available business entities.
*
* @param string $slot
* @param View $view
* @param Widget $widget
* @param BusinessEntity[] $classes
* @param int $position
*
* @throws \Exception
*
* @return Form[]
*/
public function renderNewWidgetForms($slot, View $view, Widget $widget, $classes, $position = null, $parentWidgetMap = null)
{
//the static form
$forms['static'] = [];
$forms['static']['main'] = $this->renderNewForm($this->buildForm($widget, $view, null, null, Widget::MODE_STATIC, $slot, $position, $parentWidgetMap), $widget, $slot, $view, null);

// Build each form relative to business entities
foreach ($classes as $businessEntity) {
//get the forms for the business entity (entity/query/businessEntity)
$entityForms = $this->buildEntityForms($widget, $view, $businessEntity->getId(), $businessEntity->getClass(), $position, $parentWidgetMap, $slot);

//the list of forms
$forms[$businessEntity->getId()] = [];

//foreach of the entity form
foreach ($entityForms as $formMode => $entityForm) {
//we add the form
$forms[$businessEntity->getId()][$formMode] = $this->renderNewForm($entityForm, $widget, $slot, $view, $businessEntity->getId());
}
}

return $forms;
}

/**
* @param Widget $widget
* @param View $view
Expand All @@ -124,20 +86,26 @@ public function renderNewWidgetForms($slot, View $view, Widget $widget, $classes
*
* @return array
*/
protected function buildEntityForms($widget, View $view, $businessEntityId = null, $namespace = null, $position = null, $parentWidgetMap = null, $slot = null)
public function buildEntityForms($widget, View $view, $businessEntityId = null, $namespace = null, $position = null, $parentWidgetMap = null, $slot = null, $new = true)
{
$forms = [];

//get the entity form
$entityForm = $this->buildForm($widget, $view, $businessEntityId, $namespace, Widget::MODE_ENTITY, $slot, $position, $parentWidgetMap);
$renderMethod = $new ? 'renderNewForm' : 'renderForm';
$entityForm = $this->$renderMethod(
$this->buildForm($widget, $view, $businessEntityId, $namespace, Widget::MODE_ENTITY, $slot, $position, $parentWidgetMap), $widget, $slot, $view, $businessEntityId
);
$forms[Widget::MODE_ENTITY] = $entityForm;

//get the query form
$queryForm = $this->buildForm($widget, $view, $businessEntityId, $namespace, Widget::MODE_QUERY, $slot, $position, $parentWidgetMap);
$queryForm = $this->$renderMethod(
$this->buildForm($widget, $view, $businessEntityId, $namespace, Widget::MODE_QUERY, $slot, $position, $parentWidgetMap), $widget, $slot, $view, $businessEntityId
);
$forms[Widget::MODE_QUERY] = $queryForm;

//get the query form
$businessEntityForm = $this->buildForm($widget, $view, $businessEntityId, $namespace, Widget::MODE_BUSINESS_ENTITY, $slot, $position, $parentWidgetMap);
$businessEntityForm = $this->$renderMethod(
$this->buildForm($widget, $view, $businessEntityId, $namespace, Widget::MODE_BUSINESS_ENTITY, $slot, $position, $parentWidgetMap), $widget, $slot, $view, $businessEntityId
);
$forms[Widget::MODE_BUSINESS_ENTITY] = $businessEntityForm;

return $forms;
Expand Down Expand Up @@ -193,7 +161,8 @@ public function buildWidgetForm(Widget $widget, View $view, $businessEntityId =
}
$formUrl = $router->generate($action, $actionParams);
} else {
$viewReference = $widget->getCurrentView()->getReference();
$view = $view ? $view : $widget->getCurrentView();
$viewReference = $view->getReference();
$formUrl = $router->generate('victoire_core_widget_update',
[
'id' => $widget->getId(),
Expand Down
50 changes: 49 additions & 1 deletion Bundle/WidgetBundle/Controller/WidgetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,48 @@ public function newAction($type, $viewReference, $slot = null, $position = null,
return $response;
}

/**
* @param $widget
* @param View $view
* @param null $businessEntityId
* @param null $namespace
* @param null $position
* @param null $parentWidgetMap
* @param null $slot
*
* @return string
*
* @Route("/victoire-dcms/widget/entity/create/{viewReference}/{businessEntityId}/{type}/{slot}/{parentWidgetMap}/{position}", name="victoire_core_widget_entity_create", defaults={"widget_id":null, "slot":null, "businessEntityId":null, "position":null, "parentWidgetMap":null, "_format": "json"})
* @Route("/victoire-dcms/widget/entity/update/{viewReference}/{businessEntityId}/{type}/{widget_id}/{slot}/{parentWidgetMap}", name="victoire_core_widget_entity_edit", defaults={"widget_id":null, "slot":null, "businessEntityId":null, "position":null, "parentWidgetMap":null, "_format": "json"})
Copy link
Contributor

@lenybernard lenybernard May 3, 2016

Choose a reason for hiding this comment

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

rename into victoire_widget_partialForm_create|update

* @ParamConverter("widget", class="VictoireWidgetBundle:Widget", options={"id" = "widget_id"})
*/
public function entityAction($viewReference, $slot, $position, $parentWidgetMap, $businessEntityId, $type, Widget $widget = null)
Copy link
Contributor

Choose a reason for hiding this comment

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

@Charlie-Lucas I don't understand this name, please try to find a better and more explicit name

{
try {
$view = $this->getViewByReferenceId($viewReference);

if (!$reference = $this->get('victoire_view_reference.repository')
->getOneReferenceByParameters(['id' => $viewReference])) {
$reference = new ViewReference($viewReference);
}
$view->setReference($reference);
$businessEntity = null;
if ($businessEntityId) {
$businessEntity = $this->get('victoire_core.helper.business_entity_helper')->findById($businessEntityId);
}
$new = $widget ? false : true;
$response = new JsonResponse(
$this->get('victoire_widget.widget_manager')->newEntityForms(
$widget, $businessEntity, $type, $slot, $view, $position, $parentWidgetMap, $new
)
);
} catch (Exception $ex) {
$response = $this->getJsonReponseFromException($ex);
}

return $response;
}

/**
* Create a widget.
* This action needs 2 routes to handle the presence or not of "businessEntityId" and 'parentWidgetMap'
Expand Down Expand Up @@ -197,13 +239,19 @@ public function editAction(Widget $widget, $viewReference, $mode = Widget::MODE_
}
$widget->setCurrentView($widgetView);
$this->get('victoire_core.current_view')->setCurrentView($view);
$businessEntity = null;
$businessEntityId = $businessEntityId == null ? $widget->getBusinessEntityId() : $businessEntityId;
if ($businessEntityId) {
$businessEntity = $this->get('victoire_core.helper.business_entity_helper')->findById($businessEntityId);
}

try {
$response = new JsonResponse(
$this->get('widget_manager')->editWidget(
$this->get('request'),
$widget,
$view,
$businessEntityId,
$businessEntity,
$mode
)
);
Expand Down
Loading