Hallo.
Ich habe mir ein eigenes Backend Widget gebaut / zusammenkopiert
Nun habe ich versucht aus dem ListWidget das Kopieren von Elementen nachzubauen, doch leider wird mit eifach nur die Seite neu geladet und das Input ist leer + das Select auf den ersten Wert gestellt.
Eventuell sieht jemand was ich falsch habe. Denke es lieg an der ID die übergeben wird.
PHP-Code:
<?php
/**
* Contao Open Source CMS
*
* Copyright (c) 2017 Matthias Gmeiner
*
* @license LGPL-3.0+
*/
namespace MatthiasGmeiner;
/**
* Provide methods to handle list items.
*
* @property integer $maxlength
*
*/
class ConditionsList extends \Widget
{
/**
* Submit user input
* @var boolean
*/
protected $blnSubmitInput = true;
/**
* Template
* @var string
*/
protected $strTemplate = 'be_widget';
/**
* Units
* @var array
*/
protected $arrUnits = array();
/**
* Add specific attributes
*
* @param string $strKey
* @param mixed $varValue
*/
public function __set($strKey, $varValue)
{
switch ($strKey)
{
case 'maxlength':
if ($varValue > 0)
{
$this->arrAttributes['maxlength'] = $varValue;
}
break;
case 'options':
$this->arrUnits = deserialize($varValue);
break;
default:
parent::__set($strKey, $varValue);
break;
}
}
/**
* Do not validate unit fields
*
* @param mixed $varInput
*
* @return mixed
*/
protected function validator($varInput)
{
foreach ($varInput as $k=>$v)
{
if ($k != 'unit')
{
$varInput[$k] = parent::validator($v);
}
}
return $varInput;
}
/**
* Only check against the unit values (see #7246)
*
* @param array $arrOption The options array
*
* @return string The "selected" attribute or an empty string
*/
protected function isSelected($arrOption)
{
if (empty($this->varValue) && empty($_POST) && $arrOption['default'])
{
return parent::optionSelected(1, 1);
}
if (empty($this->varValue) || !is_array($this->varValue))
{
return '';
}
return parent::optionSelected($arrOption['value'], $this->varValue['unit']);
}
/**
* Generate the widget and return it as string
*
* @return string
*/
public function generate()
{
$arrButtons = array('copy', 'drag', 'up', 'down', 'delete');
$strCommand = 'cmd_' . $this->strField;
// Change the order
if (\Input::get($strCommand) && is_numeric(\Input::get('cid')) && \Input::get('id') == $this->currentRecord)
{
$this->import('Database');
switch (\Input::get($strCommand))
{
case 'copy':
$this->varValue = array_duplicate($this->varValue, \Input::get('cid'));
break;
case 'up':
$this->varValue = array_move_up($this->varValue, \Input::get('cid'));
break;
case 'down':
$this->varValue = array_move_down($this->varValue, \Input::get('cid'));
break;
case 'delete':
$this->varValue = array_delete($this->varValue, \Input::get('cid'));
break;
}
$this->Database->prepare("UPDATE " . $this->strTable . " SET " . $this->strField . "=? WHERE id=?")
->execute(serialize($this->varValue), $this->currentRecord);
$this->redirect(preg_replace('/&(amp;)?cid=[^&]*/i', '', preg_replace('/&(amp;)?' . preg_quote($strCommand, '/') . '=[^&]*/i', '', \Environment::get('request'))));
}
// Make sure there is at least an empty array
if (!is_array($this->varValue) || empty($this->varValue))
{
$this->varValue = array('');
}
$arrUnits = array();
foreach ($this->arrUnits as $arrUnit)
{
$arrUnits[] = sprintf('<option value="%s"%s>%s</option>',
specialchars($arrUnit['value']),
$this->isSelected($arrUnit),
$arrUnit['label']);
}
$arrFields = array();
$strKey = 'conditions';
if (!is_array($this->varValue))
{
$this->varValue = array();
}
for ($i=0, $c=count($this->varValue); $i<$c; $i++)
{
$arrFields[] = sprintf('<input type="text" name="%s[%s]" id="ctrl_%s" class="tl_text_trbl trbl_%s%s" value="%s"%s onfocus="Backend.getScrollOffset()">',
$this->strName,
$strKey,
$this->strId.'_'.$strKey,
$strKey,
(($this->strClass != '') ? ' ' . $this->strClass : ''),
specialchars(@$this->varValue[$strKey]), // see #4979
$this->getAttributes());
foreach ($arrButtons as $button)
{
$class = ($button == 'up' || $button == 'down') ? ' class="button-move"' : '';
if ($button == 'drag')
{
$return .= \Image::getHtml('drag.gif', '', 'class="drag-handle" title="' . sprintf($GLOBALS['TL_LANG']['MSC']['move']) . '"');
}
else
{
$return .= '<a href="'.$this->addToUrl('&'.$strCommand.'='.$button.'&cid='.$i.'&id='.$this->currentRecord).'"' . $class . ' title="'.specialchars($GLOBALS['TL_LANG']['MSC']['lw_'.$button]).'" onclick="Backend.listWizard(this,\''.$button.'\',\'ctrl_'.$this->strId.'\');return false">'.\Image::getHtml($button.'.gif', $GLOBALS['TL_LANG']['MSC']['lw_'.$button], 'class="tl_listwizard_img"').'</a> ';
}
}
return sprintf('%s<select name="%s[unit]" class="tl_select_unit" onfocus="Backend.getScrollOffset()"%s>%s</select>%s%s',
implode(' ', $arrFields),
$this->strName,
$this->getAttribute('disabled'),
implode('', $arrUnits),
$this->wizard,
$return);
}
}
}
Als Link wird das hier generiert.
Code:
cmd_db_conditions=copy&cid=0&id=9&ref=776eafc4
In der Backend URL steht folgendes.
Code:
main.php?do=form&table=tl_form_field&act=edit&rt=d9e0f1c74889e32b9fb3e09f309d1658&id=9&ref=1f7a5e06
lg Matthias
Lesezeichen