/**
* Save the current value
*
* @param mixed $varValue
*
* @throws \Exception
*/
protected function save($varValue)
{
if (\Input::post('FORM_SUBMIT') != $this->strTable) {
return;
}
$arrData = $GLOBALS['TL_DCA'][$this->strTable]['fields'][$this->strField];
// File names
if ($this->strField == 'name') {
if (!file_exists(TL_ROOT . '/' . $this->strPath . '/' . $this->varValue . $this->strExtension) || !$this->isMounted($this->strPath . '/' . $this->varValue . $this->strExtension) || $this->varValue === $varValue) {
return;
}
$this->import('Files');
$varValue = Utf8::toAscii($varValue);
// Trigger the save_callback
if (is_array($arrData['save_callback'])) {
foreach ($arrData['save_callback'] as $callback) {
if (is_array($callback)) {
$this->import($callback[0]);
$varValue = $this->{$callback[0]}->{$callback[1]}($varValue, $this);
} elseif (is_callable($callback)) {
$varValue = $callback($varValue, $this);
}
}
}
// The target exists
if (strcasecmp($this->strPath . '/' . $this->varValue . $this->strExtension, $this->strPath . '/' . $varValue . $this->strExtension) !== 0 && file_exists(TL_ROOT . '/' . $this->strPath . '/' . $varValue . $this->strExtension)) {
throw new \Exception(sprintf($GLOBALS['TL_LANG']['ERR']['fileExists'], $varValue));
}
$arrImageTypes = trimsplit(',', strtolower(\Config::get('validImageTypes')));
// Remove potentially existing thumbnails (see #6641)
if (in_array(substr($this->strExtension, 1), $arrImageTypes)) {
foreach (glob(TL_ROOT . '/' . \System::getContainer()->getParameter('contao.image.target_path') . '/*/' . $this->varValue . '-*' . $this->strExtension) as $strThumbnail) {
$this->Files->delete(str_replace(TL_ROOT . '/', '', $strThumbnail));
}
}
// Rename the file
$this->Files->rename($this->strPath . '/' . $this->varValue . $this->strExtension, $this->strPath . '/' . $varValue . $this->strExtension);
// New folders
if (stristr($this->intId, '__new__') !== false) {
// Update the database
if ($this->blnIsDbAssisted && \Dbafs::shouldBeSynchronized($this->strPath . '/' . $varValue . $this->strExtension)) {
$this->objActiveRecord = \Dbafs::addResource($this->strPath . '/' . $varValue . $this->strExtension);
}
$this->log('Folder "' . $this->strPath . '/' . $varValue . $this->strExtension . '" has been created', __METHOD__, TL_FILES);
} else {
// Update the database
if ($this->blnIsDbAssisted) {
$syncSource = \Dbafs::shouldBeSynchronized($this->strPath . '/' . $this->varValue . $this->strExtension);
$syncTarget = \Dbafs::shouldBeSynchronized($this->strPath . '/' . $varValue . $this->strExtension);
if ($syncSource && $syncTarget) {
\Dbafs::moveResource($this->strPath . '/' . $this->varValue . $this->strExtension, $this->strPath . '/' . $varValue . $this->strExtension);
} elseif ($syncSource) {
\Dbafs::deleteResource($this->strPath . '/' . $this->varValue . $this->strExtension);
} elseif ($syncTarget) {
\Dbafs::addResource($this->strPath . '/' . $varValue . $this->strExtension);
}
}
$this->log('File or folder "' . $this->strPath . '/' . $this->varValue . $this->strExtension . '" has been renamed to "' . $this->strPath . '/' . $varValue . $this->strExtension . '"', __METHOD__, TL_FILES);
}
// Update the symlinks
if (is_link(TL_ROOT . '/web/' . $this->strPath . '/' . $this->varValue . $this->strExtension)) {
$this->Files->delete('web/' . $this->strPath . '/' . $this->varValue . $this->strExtension);
SymlinkUtil::symlink($this->strPath . '/' . $varValue . $this->strExtension, 'web/' . $this->strPath . '/' . $varValue . $this->strExtension, TL_ROOT);
}
// Set the new value so the input field can show it
if (\Input::get('act') == 'editAll') {
/** @var SessionInterface $objSession */
$objSession = \System::getContainer()->get('session');
$session = $objSession->all();
if (($index = array_search($this->strPath . '/' . $this->varValue . $this->strExtension, $session['CURRENT']['IDS'])) !== false) {
$session['CURRENT']['IDS'][$index] = $this->strPath . '/' . $varValue . $this->strExtension;
$objSession->replace($session);
}
}
$this->varValue = $varValue;
} elseif ($this->blnIsDbAssisted && $this->objActiveRecord !== null) {
// Convert date formats into timestamps
if ($varValue != '' && in_array($arrData['eval']['rgxp'], array('date', 'time', 'datim'))) {
$objDate = new \Date($varValue, \Date::getFormatFromRgxp($arrData['eval']['rgxp']));
$varValue = $objDate->tstamp;
}
// Make sure unique fields are unique
if ($arrData['eval']['unique'] && $varValue != '' && !$this->Database->isUniqueValue($this->strTable, $this->strField, $varValue, $this->objActiveRecord->id)) {
throw new \Exception(sprintf($GLOBALS['TL_LANG']['ERR']['unique'], $arrData['label'][0] ?: $this->strField));
}
// Handle multi-select fields in "override all" mode
if (\Input::get('act') == 'overrideAll' && ($arrData['inputType'] == 'checkbox' || $arrData['inputType'] == 'checkboxWizard') && $arrData['eval']['multiple']) {
if ($this->objActiveRecord !== null) {
$new = deserialize($varValue, true);
$old = deserialize($this->objActiveRecord->{$this->strField}, true);
switch (\Input::post($this->strInputName . '_update')) {
case 'add':
$varValue = array_values(array_unique(array_merge($old, $new)));
break;
case 'remove':
$varValue = array_values(array_diff($old, $new));
break;
case 'replace':
$varValue = $new;
break;
}
if (!is_array($varValue) || empty($varValue)) {
$varValue = '';
} elseif (isset($arrData['eval']['csv'])) {
$varValue = implode($arrData['eval']['csv'], $varValue);
// see #2890
} else {
$varValue = serialize($varValue);
}
}
}
// Convert arrays (see #2890)
if ($arrData['eval']['multiple'] && isset($arrData['eval']['csv'])) {
$varValue = implode($arrData['eval']['csv'], deserialize($varValue, true));
}
// Trigger the save_callback
if (is_array($arrData['save_callback'])) {
foreach ($arrData['save_callback'] as $callback) {
if (is_array($callback)) {
$this->import($callback[0]);
$varValue = $this->{$callback[0]}->{$callback[1]}($varValue, $this);
} elseif (is_callable($callback)) {
$varValue = $callback($varValue, $this);
}
}
}
// Save the value if there was no error
if (($varValue != '' || !$arrData['eval']['doNotSaveEmpty']) && ($this->varValue != $varValue || $arrData['eval']['alwaysSave'])) {
// If the field is a fallback field, empty all other columns
if ($arrData['eval']['fallback'] && $varValue != '') {
$this->Database->execute("UPDATE " . $this->strTable . " SET " . $this->strField . "=''");
}
// Set the correct empty value (see #6284, #6373)
if ($varValue === '') {
$varValue = \Widget::getEmptyValueByFieldType($GLOBALS['TL_DCA'][$this->strTable]['fields'][$this->strField]['sql']);
}
$this->objActiveRecord->{$this->strField} = $varValue;
$this->objActiveRecord->save();
$this->blnCreateNewVersion = true;
$this->varValue = deserialize($varValue);
}
}
}