task subdef

- better format of xml settings
- fix filter on document type
- fix filter on multiple databoxes
- fix unit tests
This commit is contained in:
Jean-Yves Gaulier
2015-05-13 19:38:39 +02:00
parent 76cf43bf51
commit eeed4d3120
4 changed files with 73 additions and 25 deletions

View File

@@ -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']);
}

View File

@@ -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);

View File

@@ -67,7 +67,7 @@
{% block javascript %}
function minmax(v, _min, _max)
{
if(v == NaN || v < _min)
if(isNaN(v) || v < _min)
{
v = _min;
}

View File

@@ -12,7 +12,17 @@ class SubdefsEditorTest extends EditorTestCase
[
'<?xml version="1.0" encoding="UTF-8"?>
<tasksettings>
<sbas></sbas><type_image>0</type_image><type_video>0</type_video><type_audio>0</type_audio><type_document>0</type_document><type_flash>0</type_flash><type_unknown>0</type_unknown><flush></flush><maxrecs></maxrecs><maxmegs></maxmegs><embedded>0</embedded></tasksettings>
<type_image>0</type_image>
<type_video>0</type_video>
<type_audio>0</type_audio>
<type_document>0</type_document>
<type_flash>0</type_flash>
<type_unknown>0</type_unknown>
<flush>0</flush>
<maxrecs>0</maxrecs>
<maxmegs>0</maxmegs>
<embedded>0</embedded>
</tasksettings>
',
'<?xml version="1.0" encoding="UTF-8"?>
<tasksettings>
@@ -29,7 +39,7 @@ class SubdefsEditorTest extends EditorTestCase
<type_document>0</type_document>
<type_flash>0</type_flash>
<type_unknown>0</type_unknown>
<flush>0</flush>
<flush>1</flush>
<maxrecs>12</maxrecs>
<maxmegs>1</maxmegs>
<embedded>1</embedded>
@@ -67,16 +77,15 @@ class SubdefsEditorTest extends EditorTestCase
'<?xml version="1.0" encoding="UTF-8"?>
<tasksettings>
<customtag>value</customtag>
<sbas></sbas>
<type_image>0</type_image>
<type_video>0</type_video>
<type_audio>0</type_audio>
<type_document>0</type_document>
<type_flash>0</type_flash>
<type_unknown>0</type_unknown>
<flush></flush>
<maxrecs></maxrecs>
<maxmegs></maxmegs>
<flush>0</flush>
<maxrecs>0</maxrecs>
<maxmegs>0</maxmegs>
<embedded>0</embedded>
</tasksettings>
',