From cc3af69b8458a844a249a702875ed07b2981d168 Mon Sep 17 00:00:00 2001 From: Ingolf Steinhardt Date: Wed, 17 May 2017 12:12:08 +0200 Subject: [PATCH] Hotfix #18 delete empty rows --- .../TranslatedTableText.php | 100 +++++++++--------- 1 file changed, 49 insertions(+), 51 deletions(-) diff --git a/src/MetaModels/Attribute/TranslatedTableText/TranslatedTableText.php b/src/MetaModels/Attribute/TranslatedTableText/TranslatedTableText.php index 1d88c1e..4a32de1 100644 --- a/src/MetaModels/Attribute/TranslatedTableText/TranslatedTableText.php +++ b/src/MetaModels/Attribute/TranslatedTableText/TranslatedTableText.php @@ -1,10 +1,14 @@ * @author Andreas Isaak * @author David Greminger - * @copyright 2012-2016 The MetaModels team. + * @author Ingolf Steinhardt + * @copyright 2012-2017 The MetaModels team. * @license https://github.com/MetaModels/attribute_translatedtabletext/blob/master/LICENSE LGPL-3.0 * @filesource */ @@ -26,11 +31,6 @@ /** * This is the MetaModelAttribute class for handling translated table text fields. - * - * @package MetaModels - * @subpackage AttributeTranslatedTableText - * @author David Maack - * @author Stefan Heimes */ class TranslatedTableText extends Base implements ITranslated, IComplex { @@ -77,8 +77,8 @@ public function getFieldDefinition($arrOverrides = array()) $arrFieldDef['inputType'] = 'multiColumnWizard'; $arrFieldDef['eval']['columnFields'] = array(); - $count = count($arrColLabels); - for ($i = 0; $i < $count; $i++) { + $countCol = count($arrColLabels); + for ($i = 0; $i < $countCol; $i++) { $arrFieldDef['eval']['columnFields']['col_' . $i] = array( 'label' => $arrColLabels[$i]['rowLabel'], 'inputType' => 'text', @@ -147,10 +147,15 @@ public function valueToWidget($varValue) return array(); } - $widgetValue = array(); - foreach ($varValue as $row) { - foreach ($row as $key => $col) { - $widgetValue[$col['row']]['col_' . $key] = $col['value']; + $arrColLabels = deserialize($this->get('translatedtabletext_cols'), true); + $countCol = count($arrColLabels); + $widgetValue = array(); + + foreach ($varValue as $k => $row) { + for ($kk = 0; $kk < $countCol; $kk++) { + $i = array_search($kk, array_column($row, 'col')); + + $widgetValue[$k]['col_' . $kk] = ($i !== false) ? $row[$i]['value'] : ''; } } @@ -167,14 +172,17 @@ public function widgetToValue($varValue, $itemId) } $newValue = array(); + // Start row numerator at 0. + $intRow = 0; foreach ($varValue as $k => $row) { foreach ($row as $kk => $col) { $kk = str_replace('col_', '', $kk); $newValue[$k][$kk]['value'] = $col; $newValue[$k][$kk]['col'] = $kk; - $newValue[$k][$kk]['row'] = $k; + $newValue[$k][$kk]['row'] = $intRow; } + $intRow++; } return $newValue; @@ -248,47 +256,37 @@ public function setTranslatedDataFor($arrValues, $strLangCode) $objDB = $this->getMetaModel()->getServiceContainer()->getDatabase(); // Get the ids. - $arrIds = array_keys($arrValues); + $arrIds = array_keys($arrValues); + + // Reset all data for the ids in language. + $this->unsetValueFor($arrIds, $strLangCode); + + // Insert or update the cells. $strQueryUpdate = 'UPDATE %s'; + $strQueryInsert = 'INSERT INTO ' . $this->getValueTable() . ' %s'; - // Insert or Update the cells. - $strQuery = 'INSERT INTO ' . $this->getValueTable() . ' %s'; foreach ($arrIds as $intId) { - // No values give, delete all values. - if (empty($arrValues[$intId])) { - $strDelQuery = 'DELETE FROM ' . $this->getValueTable() . ' WHERE att_id=? AND item_id=? AND langcode=?'; - - $objDB - ->prepare($strDelQuery) - ->execute(intval($this->get('id')), $intId, $strLangCode); - - continue; - } - - // Delete missing rows. - $rowIds = array_keys($arrValues[$intId]); - $strDelQuery = sprintf( - 'DELETE FROM %s WHERE att_id=? AND item_id=? AND langcode=? AND row NOT IN (%s)', - $this->getValueTable(), - implode(',', $rowIds) - ); - - $objDB - ->prepare($strDelQuery) - ->execute(intval($this->get('id')), $intId, $strLangCode); - // Walk every row. foreach ($arrValues[$intId] as $row) { - // Walk every column and update/insert the value. + // Walk every column and update / insert the value. foreach ($row as $col) { - $values = $this->getSetValues($col, $intId, $strLangCode); - $subQuery = $objDB - ->prepare($strQueryUpdate) - ->set($values) - ->query; - + $values = $this->getSetValues($col, $intId, $strLangCode); + if (empty($values['value'])) { + continue; + } $objDB - ->prepare($strQuery . ' ON DUPLICATE KEY ' . str_replace('SET ', '', $subQuery)) + ->prepare( + $strQueryInsert . + ' ON DUPLICATE KEY ' . + str_replace( + 'SET ', + '', + $objDB + ->prepare($strQueryUpdate) + ->set($values) + ->query + ) + ) ->set($values) ->execute(); }