diff --git a/lib/Alchemy/Phrasea/TaskManager/Editor/SubdefsEditor.php b/lib/Alchemy/Phrasea/TaskManager/Editor/SubdefsEditor.php index fb54cea097..644b9d2ddb 100644 --- a/lib/Alchemy/Phrasea/TaskManager/Editor/SubdefsEditor.php +++ b/lib/Alchemy/Phrasea/TaskManager/Editor/SubdefsEditor.php @@ -64,7 +64,7 @@ EOF; protected function getFormProperties() { return [ - 'sbas' => static::FORM_TYPE_INTEGER, + 'sbas[]' => static::FORM_TYPE_INTEGER, 'type_image' => static::FORM_TYPE_BOOLEAN, 'type_video' => static::FORM_TYPE_BOOLEAN, 'type_audio' => static::FORM_TYPE_BOOLEAN, @@ -84,35 +84,72 @@ EOF; public function updateXMLWithRequest(Request $request) { $dom = $this->createBlankDom(); + $dom->formatOutput = true; + $dom->preserveWhiteSpace = false; if (false === @$dom->loadXML($request->request->get('xml'))) { throw new BadRequestHttpException('Invalid XML data.'); } + + // delete empty /text-nodes so the output can be better formatted + $nodesToDel = array(); + for($node = $dom->documentElement->firstChild; $node; $node=$node->nextSibling) { + if($node->nodeType == XML_TEXT_NODE && trim($node->data)=="") { + $nodesToDel[] = $node; + } + } + foreach($nodesToDel as $node) { + $dom->documentElement->removeChild($node); + } + + $dom->normalizeDocument(); foreach ($this->getFormProperties() as $name => $type) { + $multi = false; + if(substr($name, -2)== "[]") { + $multi = true; + $name = substr($name, 0, strlen($name)-2); + } + $values = $request->request->get($name); - if($values === null) { - $values = array(); - } elseif(!is_array($values)) { - $values = array($values); + if($values === null && !$multi) { + switch($type) { + case static::FORM_TYPE_INTEGER: + case static::FORM_TYPE_BOOLEAN: + $values = "0"; + break; + case static::FORM_TYPE_STRING: + $values = ""; + break; + } } // erase the former setting but keep the node in place. // in case on multi-valued, keep only the first node (except if no value at all: erase all) + $nodesToDel = array(); foreach($dom->getElementsByTagName($name) as $i=>$node) { - if($i > 0 || count($values)==0) { - $node->parentNode->removeChild($node); - } else { - while ($child = $node->firstChild) { - $node->removeChild($child); - } + // empty + while ($child = $node->firstChild) { + $node->removeChild($child); + } + // keep the first for multi, only if there is something to write + if($i > 0 || ($multi && $values===null) ) { + $nodesToDel[] = $node; } } - // if no setting to write, no reason to create a node - if(count($values) == 0) { + foreach($nodesToDel as $node) { + $dom->documentElement->removeChild($node); + } + + // if no multiple-setting to write, no reason to create an empty node + if($values === null) { continue; } + if(!is_array($values)) { + $values = array($values); + } + // in case the node did not exist at all, create one if ( ($node = $dom->getElementsByTagName($name)->item(0)) === null) { $node = $dom->documentElement->appendChild($dom->createElement($name)); @@ -130,6 +167,7 @@ EOF; $node->appendChild($dom->createTextNode($this->toXMLValue($type, $value))); } } + $dom->normalizeDocument(); return new Response($dom->saveXML(), 200, ['Content-type' => 'text/xml']); } diff --git a/lib/Alchemy/Phrasea/TaskManager/Job/SubdefsJob.php b/lib/Alchemy/Phrasea/TaskManager/Job/SubdefsJob.php index 3f20dc1e60..2b2c0cd593 100644 --- a/lib/Alchemy/Phrasea/TaskManager/Job/SubdefsJob.php +++ b/lib/Alchemy/Phrasea/TaskManager/Job/SubdefsJob.php @@ -73,6 +73,9 @@ class SubdefsJob extends AbstractJob $sqlparms[] = $type; } } + if(count($sqlqmark) == 0) { + return; + } foreach ($app['phraseanet.appbox']->get_databoxes() as $databox) { if (!$this->isStarted()) { @@ -86,11 +89,9 @@ class SubdefsJob extends AbstractJob $conn = $databox->get_connection(); $sql = 'SELECT coll_id, record_id FROM record' - . ' WHERE jeton & ' . PhraseaTokens::MAKE_SUBDEF . ' > 0'; - if(count($sqlqmark) > 0) { - $sql .= ' AND type IN(' . implode(',', $sqlqmark) . ')'; - } - $sql .= ' ORDER BY record_id DESC LIMIT 0, 30'; + . ' WHERE jeton & ' . PhraseaTokens::MAKE_SUBDEF . ' > 0' + . ' AND type IN(' . implode(',', $sqlqmark) . ')' + . ' ORDER BY record_id DESC LIMIT 0, 30'; $stmt = $conn->prepare($sql); $stmt->execute($sqlparms); $rs = $stmt->fetchAll(\PDO::FETCH_ASSOC); diff --git a/templates/web/admin/task-manager/task-editor/subdefs.html.twig b/templates/web/admin/task-manager/task-editor/subdefs.html.twig index 26f9ff6666..4e5a077660 100644 --- a/templates/web/admin/task-manager/task-editor/subdefs.html.twig +++ b/templates/web/admin/task-manager/task-editor/subdefs.html.twig @@ -67,7 +67,7 @@ {% block javascript %} function minmax(v, _min, _max) { - if(v == NaN || v < _min) + if(isNaN(v) || v < _min) { v = _min; } diff --git a/tests/Alchemy/Tests/Phrasea/TaskManager/Editor/SubdefsEditorTest.php b/tests/Alchemy/Tests/Phrasea/TaskManager/Editor/SubdefsEditorTest.php index 4e034cc356..3729129155 100644 --- a/tests/Alchemy/Tests/Phrasea/TaskManager/Editor/SubdefsEditorTest.php +++ b/tests/Alchemy/Tests/Phrasea/TaskManager/Editor/SubdefsEditorTest.php @@ -12,7 +12,17 @@ class SubdefsEditorTest extends EditorTestCase [ ' -0000000 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + ', ' @@ -29,7 +39,7 @@ class SubdefsEditorTest extends EditorTestCase 0 0 0 - 0 + 1 12 1 1 @@ -67,16 +77,15 @@ class SubdefsEditorTest extends EditorTestCase ' value - 0 0 0 0 0 0 - - - + 0 + 0 + 0 0 ',