diff --git a/src/GeocodeWidget.php b/src/GeocodeWidget.php index e0da0d8..f63f99d 100644 --- a/src/GeocodeWidget.php +++ b/src/GeocodeWidget.php @@ -5,130 +5,38 @@ * * @package netzmacht * @author David Molineus - * @copyright 2016-2017 netzmacht David Molineus. All rights reserved. + * @copyright 2016-2018 netzmacht David Molineus. All rights reserved. * @license LGPL-3.0 https://github.com/netzmacht/contao-leaflet-geocode-widget/blob/master/LICENSE * @filesource */ + namespace Netzmacht\Contao\Leaflet\GeocodeWidget; +use Netzmacht\Contao\Leaflet\GeocodeWidget\Widget\GeocodeWidget as BaseWidget; + /** * Class GeocodeWidget * - * @property int size - * @property bool multiple + * @package Netzmacht\Contao\Leaflet\GeocodeWidget + * @deprecated */ -class GeocodeWidget extends \Widget +class GeocodeWidget extends BaseWidget { /** - * Submit user input. - * - * @var boolean + * {@inheritdoc} */ - protected $blnSubmitInput = true; - - /** - * Add a for attribute. - * - * @var boolean - */ - protected $blnForAttribute = true; - - /** - * Template. - * - * @var string - */ - protected $strTemplate = 'be_widget'; - - /** - * Template name. - * - * @var string - */ - protected $widgetTemplate = 'be_widget_leaflet_geocode'; - - /** - * Validate the input. - * - * @param mixed $value Given value. - * - * @return mixed - * - * @SuppressWarnings(PHPMD.Superglobals) - */ - protected function validator($value) + public function __construct(array $arrAttributes = null) { - $value = parent::validator($value); + parent::__construct($arrAttributes); - if (!$value) { - return $value; - } - - if (is_array($value)) { - foreach ($value as $key => $val) { - $value[$key] = $this->validator($val); - } - - return $value; - } - - // See: http://stackoverflow.com/a/18690202 - if (!preg_match( - '#^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?),[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)(,[-+]?\d+)?$#', - $value - )) { - $this->addError( - sprintf( - $GLOBALS['TL_LANG']['ERR']['leafletInvalidCoordinate'], - $value - ) - ); - } - - return $value; - } - - /** - * Generate the widget. - * - * @return string - */ - public function generate() - { - $wrapperClass = 'wizard'; - - if (!$this->multiple || !$this->size) { - $this->size = 1; - } else { - $wrapperClass .= ' wizard_' . $this->size; - } - - if (!is_array($this->value)) { - $this->value = [$this->value]; - } - - $buffer = ''; - - for ($index = 0; $index < $this->size; $index++) { - $template = new \BackendTemplate($this->widgetTemplate); - $template->setData( - [ - 'wrapperClass' => $wrapperClass, - 'widget' => $this, - 'value' => \StringUtil::specialchars($this->value[$index]), - 'class' => $this->strClass ? ' ' . $this->strClass : '', - 'id' => $this->strId . (($this->size > 1) ? '_' . $index : ''), - 'name' => $this->strName . (($this->size > 1) ? '[]' : ''), - 'attributes' => $this->getAttributes(), - 'wizard' => $this->wizard, - 'label' => $this->strLabel, - ] - ); - - $buffer .= $template->parse(); - } - - return $buffer; + @trigger_error( + sprintf( + '"%s" is deprecated and will be removed in version 2.0.0. Use "%s" instead.', + GeocodeWidget::class, + BaseWidget::class + ), + E_USER_DEPRECATED + ); } } diff --git a/src/Resources/contao/config/config.php b/src/Resources/contao/config/config.php index 3f03f84..881f079 100644 --- a/src/Resources/contao/config/config.php +++ b/src/Resources/contao/config/config.php @@ -5,9 +5,9 @@ * * @package netzmacht * @author David Molineus - * @copyright 2016-2017 netzmacht David Molineus. All rights reserved. + * @copyright 2016-2018 netzmacht David Molineus. All rights reserved. * @license LGPL-3.0 https://github.com/netzmacht/contao-leaflet-geocode-widget/blob/master/LICENSE * @filesource */ -$GLOBALS['BE_FFL']['leaflet_geocode'] = 'Netzmacht\Contao\Leaflet\GeocodeWidget\GeocodeWidget'; +$GLOBALS['BE_FFL']['leaflet_geocode'] = 'Netzmacht\Contao\Leaflet\GeocodeWidget\Widget\GeocodeWidget'; diff --git a/src/Widget/GeocodeWidget.php b/src/Widget/GeocodeWidget.php new file mode 100644 index 0000000..798db5b --- /dev/null +++ b/src/Widget/GeocodeWidget.php @@ -0,0 +1,134 @@ + + * @copyright 2016-2018 netzmacht David Molineus. All rights reserved. + * @license LGPL-3.0 https://github.com/netzmacht/contao-leaflet-geocode-widget/blob/master/LICENSE + * @filesource + */ + +namespace Netzmacht\Contao\Leaflet\GeocodeWidget\Widget; + +/** + * Class GeocodeWidget + * + * @property int size + * @property bool multiple + */ +class GeocodeWidget extends \Widget +{ + /** + * Submit user input. + * + * @var boolean + */ + protected $blnSubmitInput = true; + + /** + * Add a for attribute. + * + * @var boolean + */ + protected $blnForAttribute = true; + + /** + * Template. + * + * @var string + */ + protected $strTemplate = 'be_widget'; + + /** + * Template name. + * + * @var string + */ + protected $widgetTemplate = 'be_widget_leaflet_geocode'; + + /** + * Validate the input. + * + * @param mixed $value Given value. + * + * @return mixed + * + * @SuppressWarnings(PHPMD.Superglobals) + */ + protected function validator($value) + { + $value = parent::validator($value); + + if (!$value) { + return $value; + } + + if (is_array($value)) { + foreach ($value as $key => $val) { + $value[$key] = $this->validator($val); + } + + return $value; + } + + // See: http://stackoverflow.com/a/18690202 + if (!preg_match( + '#^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?),[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)(,[-+]?\d+)?$#', + $value + )) { + $this->addError( + sprintf( + $GLOBALS['TL_LANG']['ERR']['leafletInvalidCoordinate'], + $value + ) + ); + } + + return $value; + } + + /** + * Generate the widget. + * + * @return string + */ + public function generate() + { + $wrapperClass = 'wizard'; + + if (!$this->multiple || !$this->size) { + $this->size = 1; + } else { + $wrapperClass .= ' wizard_' . $this->size; + } + + if (!is_array($this->value)) { + $this->value = [$this->value]; + } + + $buffer = ''; + + for ($index = 0; $index < $this->size; $index++) { + $template = new \BackendTemplate($this->widgetTemplate); + $template->setData( + [ + 'wrapperClass' => $wrapperClass, + 'widget' => $this, + 'value' => \StringUtil::specialchars($this->value[$index]), + 'class' => $this->strClass ? ' ' . $this->strClass : '', + 'id' => $this->strId . (($this->size > 1) ? '_' . $index : ''), + 'name' => $this->strName . (($this->size > 1) ? '[]' : ''), + 'attributes' => $this->getAttributes(), + 'wizard' => $this->wizard, + 'label' => $this->strLabel, + ] + ); + + $buffer .= $template->parse(); + } + + return $buffer; + } +}