Merge pull request #1213 from jygaulier/PHRAS-354

fix PHRAS-354
This commit is contained in:
Nicolas Le Goff
2015-01-14 19:23:48 +01:00
2 changed files with 62 additions and 30 deletions

View File

@@ -383,10 +383,6 @@ class PhraseaEngine implements SearchEngineInterface
$query = "all"; $query = "all";
} }
if ($this->options->getRecordType()) {
$query .= ' AND recordtype=' . $this->options->getRecordType();
}
$sql = 'SELECT query, query_time, duration, total FROM cache WHERE session_id = :ses_id'; $sql = 'SELECT query, query_time, duration, total FROM cache WHERE session_id = :ses_id';
$stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql); $stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute(array(':ses_id' => $this->app['session']->get('phrasea_session_id'))); $stmt->execute(array(':ses_id' => $this->app['session']->get('phrasea_session_id')));
@@ -683,51 +679,73 @@ class PhraseaEngine implements SearchEngineInterface
*/ */
private function addQuery($query) private function addQuery($query)
{ {
if ($this->options->getFields()) {
$fields = array_map(function (\databox_field $field) {
return $field->get_name();
}, $this->options->getFields());
if(count($fields) == 1) {
$query .= ' IN ' . $fields[0];
}
else if(count($fields) > 1) {
$query .= ' IN (' . implode(' OR ', $fields) . ')';
}
}
foreach ($this->options->getDataboxes() as $databox) { foreach ($this->options->getDataboxes() as $databox) {
$this->queries[$databox->get_sbas_id()] = $query; $this->queries[$databox->get_sbas_id()] = '';
} }
$status = $this->options->getStatus(); $status = $this->options->getStatus();
foreach ($this->queries as $sbas => $qs) { foreach ($this->queries as $sbas => $qs) {
if ($status) { $clauses = array();
$requestStat = 'xxxx';
for ($i = 4; ($i <= 32); $i++) { // the main query
if (!isset($status[$i])) { $clauses[] = $query;
$requestStat = 'x' . $requestStat;
continue; // status
} if ($status) {
$requestStat = '';
for ($i = 31; $i >= 4; $i--) {
$val = 'x'; $val = 'x';
if (isset($status[$i][$sbas])) { if (isset($status[$i]) && isset($status[$i][$sbas])) {
if ($status[$i][$sbas] == '0') { if ($status[$i][$sbas] == '0') {
$val = '0'; $val = '0';
} elseif ($status[$i][$sbas] == '1') { } elseif ($status[$i][$sbas] == '1') {
$val = '1'; $val = '1';
} }
} }
$requestStat = $val . $requestStat; $requestStat .= $val;
} }
$requestStat = ltrim($requestStat.'xxxx', 'x');
$requestStat = ltrim($requestStat, 'x');
if ($requestStat !== '') { if ($requestStat !== '') {
$this->queries[$sbas] .= ' AND (recordstatus=' . $requestStat . ')'; $clauses[] = 'recordstatus=' . $requestStat . '';
} }
} }
if ($this->options->getFields()) {
$this->queries[$sbas] .= ' IN (' . implode(' OR ', array_map(function (\databox_field $field) { // date
return $field->get_name();
}, $this->options->getFields())) . ')';
}
if (($this->options->getMinDate() || $this->options->getMaxDate()) && $this->options->getDateFields()) { if (($this->options->getMinDate() || $this->options->getMaxDate()) && $this->options->getDateFields()) {
if ($this->options->getMinDate()) { if ($this->options->getMinDate()) {
$this->queries[$sbas] .= ' AND ( ' . implode(' >= ' . $this->options->getMinDate()->format('Y-m-d') . ' OR ', array_map(function (\databox_field $field) { return $field->get_name(); }, $this->options->getDateFields())) . ' >= ' . $this->options->getMinDate()->format('Y-m-d') . ' ) '; $clauses[] = implode(' >= ' . $this->options->getMinDate()->format('Y-m-d') . ' OR ', array_map(function (\databox_field $field) { return $field->get_name(); }, $this->options->getDateFields())) . ' >= ' . $this->options->getMinDate()->format('Y-m-d');
} }
if ($this->options->getMaxDate()) { if ($this->options->getMaxDate()) {
$this->queries[$sbas] .= ' AND ( ' . implode(' <= ' . $this->options->getMaxDate()->format('Y-m-d') . ' OR ', array_map(function (\databox_field $field) { return $field->get_name(); }, $this->options->getDateFields())) . ' <= ' . $this->options->getMaxDate()->format('Y-m-d') . ' ) '; $clauses[] = implode(' <= ' . $this->options->getMaxDate()->format('Y-m-d') . ' OR ', array_map(function (\databox_field $field) { return $field->get_name(); }, $this->options->getDateFields())) . ' <= ' . $this->options->getMaxDate()->format('Y-m-d');
} }
} }
// record type
if ($this->options->getRecordType()) {
$clauses[] = 'recordtype=' . $this->options->getRecordType();
}
// join clauses to build a query
if(count($clauses) == 1) {
$this->queries[$sbas] = $clauses[0];
}
else{
$this->queries[$sbas] = '(' . join(') AND (', $clauses) . ')';
}
} }
$this->singleParse('main', $query); $this->singleParse('main', $query);

View File

@@ -323,21 +323,35 @@ class SearchEngineOptions
{ {
$tmp = array(); $tmp = array();
foreach ($status as $n => $options) { foreach ($status as $n => $options) {
if (count($options) > 1) {
continue;
}
if (isset($options['on'])) { if (isset($options['on'])) {
foreach ($options['on'] as $sbas_id) { foreach ($options['on'] as $sbas_id) {
$tmp[$n][$sbas_id] = 1; if(!isset($tmp[$n][$sbas_id])) {
$tmp[$n][$sbas_id] = array();
}
$tmp[$n][$sbas_id][] = 1;
} }
} }
if (isset($options['off'])) { if (isset($options['off'])) {
foreach ($options['off'] as $sbas_id) { foreach ($options['off'] as $sbas_id) {
$tmp[$n][$sbas_id] = 0; if(!isset($tmp[$n][$sbas_id])) {
$tmp[$n][$sbas_id] = array();
}
$tmp[$n][$sbas_id][] = 0;
} }
} }
} }
foreach($tmp as $n=>$bas) {
foreach($bas as $sbas_id=>$values) {
if(count($values) > 1) {
unset($tmp[$n][$sbas_id]);
} else {
$tmp[$n][$sbas_id] = $values[0];
}
}
if(count($tmp[$n]) == 0) {
unset($tmp[$n]);
}
}
$this->status = $tmp; $this->status = $tmp;
return $this; return $this;