Skip to content
Snippets Groups Projects
Commit 4c010db9 authored by Patrick Barroca's avatar Patrick Barroca :grin:
Browse files

dev #48349 : date range picker with specific decorator

parent f5da5949
Branches
Tags
2 merge requests!2334Master,!2104Dev#48349 ux versionning
Pipeline #920 passed with stage
in 11 minutes and 53 seconds
......@@ -5,4 +5,4 @@ echo $this->tag('h2',
$this->version->getDate()),
($user = $this->version->getUser()) ? $user->getNomAff() : $this->_('Inconnu')));
echo $this->renderVersionForm($this->form, $this->version->getData());
echo $this->renderVersionForm($this->form);
......@@ -227,22 +227,24 @@ class ZendAfi_Form extends Zend_Form {
public function beVersionCompare($datas) {
foreach($this->getElements() as $element)
$this->_injectVersionCompare($element, $datas);
$element->setDecorators([$this->_versionCompareDecoratorFor($element, $datas)]);
return $this;
}
protected function _injectVersionCompare($element, $datas) {
$decorators = [new ZendAfi_Form_Decorator_VersionCompare(['datas' => $datas])];
protected function _versionCompareDecoratorFor($element, $datas) {
$class_name = 'ZendAfi_Form_Decorator_VersionCompare';
if ('DateRangePicker' == $this->_getElementType($element))
$class_name .= '_DateRangePicker';
return new $class_name(['datas' => $datas]);
}
foreach($element->getDecorators() as $k => $v) {
$parts = explode('_', $k);
if ('Label' == end($parts))
$decorators[$k] = $v;
}
$element->setDecorators($decorators);
protected function _getElementType($element) {
$parts = explode('_', get_class($element));
return end($parts);
}
......
......@@ -24,40 +24,44 @@ class ZendAfi_Form_Decorator_VersionCompare extends Zend_Form_Decorator_Abstract
use Trait_Translator;
public function render($content) {
$attribs = [];
if ($this->_isModified())
$attribs['class'] = 'modified';
return $this
->_tag('table',
$this->_tag('tr',
$this->_renderCurrentValue()
. $this->_renderVersionValue()),
['style' => 'width:100%']);
->_tag('tr',
$this->_renderLabel() . $this->_renderValues(),
$attribs);
}
protected function _renderCurrentValue() {
if ('DateRangePicker' == $this->_getElementType())
return $this->_renderValue($this->_(' du ')
. $this->getElement()->getStart()->getValue()
. $this->_tag('br')
. $this->_(' au ')
. $this->getElement()->getEnd()->getValue());
protected function _renderLabel() {
return $this->_tag('td',
$this->_tag('label', $this->getElement()->getLabel()),
['class' => 'droite']);
}
protected function _renderValues() {
return $this
->_tag('td',
$this->_tag('table',
$this->_tag('tr',
$this->_renderCurrentValue()
. $this->_renderVersionValue()),
['style' => 'width:100%']),
['class' => 'gauche']);
}
protected function _renderCurrentValue() {
return $this->_renderValue($this->getElement()->getValue());
}
protected function _renderVersionValue() {
$datas = $this->getOption('datas');
if ('DateRangePicker' == $this->_getElementType()) {
return $this->_renderValue($this->_(' du ')
. $datas[$this->getElement()->getStart()->getName()]
. $this->_tag('br')
. $this->_(' au ')
. $datas[$this->getElement()->getEnd()->getName()]);
}
$value = $this->_('Non renseigné');
if ($datas
&& array_key_exists($name = $this->getElement()->getName(), $datas))
$value = $datas[$name];
......@@ -138,4 +142,19 @@ class ZendAfi_Form_Decorator_VersionCompare extends Zend_Form_Decorator_Abstract
protected function _tag($name, $content=null, $attribs=[]) {
return $this->getElement()->getView()->tag($name, $content, $attribs);
}
protected function _isModified() {
return $this->_isSimpleModified($this->getElement()->getName(),
$this->getElement()->getValue());
}
protected function _isSimpleModified($name, $value) {
$datas = $this->getOption('datas');
return $datas
&& array_key_exists($name, $datas)
&& $value != $datas[$name];
}
}
<?php
/**
* Copyright (c) 2012-2017, Agence Française Informatique (AFI). All rights reserved.
*
* BOKEH is free software; you can redistribute it and/or modify
* it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
* the Free Software Foundation.
*
* There are special exceptions to the terms and conditions of the AGPL as it
* is applied to this software (see README file).
*
* BOKEH is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
* along with BOKEH; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class ZendAfi_Form_Decorator_VersionCompare_DateRangePicker
extends ZendAfi_Form_Decorator_VersionCompare {
protected function _renderCurrentValue() {
return $this->_renderValue($this->_(' du ')
. $this->getElement()->getStart()->getValue()
. $this->_tag('br')
. $this->_(' au ')
. $this->getElement()->getEnd()->getValue());
}
protected function _renderVersionValue() {
$datas = $this->getOption('datas');
return $this->_renderValue($this->_(' du ')
. $datas[$this->getElement()->getStart()->getName()]
. $this->_tag('br')
. $this->_(' au ')
. $datas[$this->getElement()->getEnd()->getName()]);
}
protected function _isModified() {
return $this->_isDateModified($this->getElement()->getStart())
|| $this->_isDateModified($this->getElement()->getEnd());
}
protected function _isDateModified($element) {
$name = $element->getName();
$value = $element->getValue();
$datas = $this->getOption('datas');
if (!$datas || !array_key_exists($name, $datas))
return false;
$value = (new ZendAfi_View_Helper_DatePicker())->formatDate($value, 'dd/MM/yyyy'. ($element->getDateOnly() ? '' : ' HH:mm'));
return $value != $datas[$name];
}
}
......@@ -23,35 +23,11 @@
class ZendAfi_View_Helper_Admin_RenderVersionForm
extends ZendAfi_View_Helper_Admin_RenderForm {
protected $_version_data;
public function renderVersionForm($form, $data) {
$this->_version_data = $data;
public function renderVersionForm($form) {
return parent::renderForm($form);
}
protected function _labelForTableRendering($element, $decorator, $newDecorators, $name) {
$newDecorators = parent::_labelForTableRendering($element, $decorator, $newDecorators, $name);
if (!$this->_isModified($element))
return $newDecorators;
$tr = array_pop($newDecorators);
$newDecorators[] = ['HtmlTag', ['tag' => 'tr',
'class' => 'modified']];
return $newDecorators;
}
protected function _isModified($element) {
return $this->_version_data
&& array_key_exists($name = $element->getName(), $this->_version_data)
&& $element->getValue() != $this->_version_data[$name];
}
protected function _buttonsFor($form, $buttons) {
return $this->_renderDefaultButtons($form,
[$this->view->Button_Back($this->_getBackUrl($form))]);
......
......@@ -43,7 +43,9 @@ abstract class VersionningAdminTestCase extends Admin_AbstractControllerTestCase
$this->fixture('Class_Article', ['id' => 27,
'titre' => 'An article',
'contenu' => 'A content',
'id_cat' => 33]);
'id_cat' => 33,
'debut' => '2017-03-23',
'events_debut' => '2017-03-23 16:00:00']);
}
......@@ -148,9 +150,9 @@ class VersionningArticleVersionTest extends VersionningAdminTestCase {
->whenCalled('file_get_contents')
->answers(json_encode(['titre' => 'An different title',
'debut' => '',
'debut' => '2016-12-23',
'fin' => '',
'events_debut' => '',
'events_debut' => '23/03/2017 16:00',
'events_fin' => '']));
$this->dispatch('/admin/cms/version/id/27/key/2017-03-20_112408_33', true);
......@@ -187,6 +189,18 @@ class VersionningArticleVersionTest extends VersionningAdminTestCase {
$this->assertXPathContentContains('//tr[1]//td[@class="gauche"]//td',
'An different title');
}
/** @test */
public function articlePublicationShouldBeMarkedAsModified() {
$this->assertXPath('//tr[5][@class="modified"]');
}
/** @test */
public function articleAgendaShouldNotBeMarkedAsModified() {
$this->assertNotXPath('//tr[6][@class="modified"]');
}
}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment