Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LIMS-1025: Fix lab contact search #681

Merged
merged 3 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions api/src/Database/DatabaseQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,16 @@ private function addBoundVariable($value): int
array_push($this->query_bound_values, $value);
return sizeof($this->query_bound_values);
}

public static function getWhereSearch($searchValue, $fields, &$query_bound_values)
{
$where = " AND (0=1";

foreach ($fields as $field) {
array_push($query_bound_values, $searchValue);
$where .= " OR lower(" . $field . ") LIKE lower(CONCAT('%', :" . sizeof($query_bound_values) . ", '%'))";
}
$where .= ")";
return $where;
}
}
13 changes: 11 additions & 2 deletions api/src/Page/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace SynchWeb\Page;

use SynchWeb\Page;
use SynchWeb\Database\DatabaseQueryBuilder;

class Contact extends Page
{
Expand Down Expand Up @@ -46,9 +47,17 @@ function _get_contacts() {
$where .= ' AND c.labcontactid=:'.(sizeof($args)+1);
array_push($args, $this->arg('cid'));
}


$tot = $this->db->pq("SELECT count(c.labcontactid) as tot FROM labcontact c $where", $args);
if ($this->has_arg('s')) {
$fields = array('pe.givenname', 'pe.familyname', 'c.cardname',
'l.name', 'l.address', 'l.city', 'l.country', 'pe.phonenumber', 'l.postcode');
$where = $where . DatabaseQueryBuilder::getWhereSearch($this->arg('s'), $fields, $args);
}

$tot = $this->db->pq("SELECT count(c.labcontactid) as tot FROM labcontact c
INNER JOIN person pe ON c.personid = pe.personid
INNER JOIN laboratory l ON l.laboratoryid = pe.laboratoryid
$where", $args);
$tot = intval($tot[0]['TOT']);

$pp = $this->has_arg('per_page') ? $this->arg('per_page') : 15;
Expand Down
20 changes: 18 additions & 2 deletions api/tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
use SynchWeb\Database\DatabaseParent;
use SynchWeb\Database\DatabaseQueryBuilder;

final class DatabaseHelperTest extends TestCase
use function PHPUnit\Framework\assertEquals;

final class DatabaseQueryBuilderTest extends TestCase
{
use \phpmock\phpunit\PHPMock;

private function create_database_helper($fields, $expected_sql, $expected_id_value = null)
private function create_database_helper($fields, $expected_sql=null, $expected_id_value = null)
{
$expected_values = [];
foreach ($fields as $field)
Expand Down Expand Up @@ -168,4 +170,18 @@ public function testWithTwoValuesWhenInsertingDBInsertIsCorrect()
}
$dbh->insert($expected_table);
}

public function testWithSingleIdWhereClauseGetWhereClauseReturnIt() {
$fields = ["a.field1", "b.field2"];
$expected_sql = " AND (0=1 OR lower({$fields[0]}) LIKE lower(CONCAT('%', :2, '%')) OR lower({$fields[1]}) LIKE lower(CONCAT('%', :3, '%')))";
$expectedSearchValue = "searchVal";

$element1 = "first";
$args=[$element1];
$sql = DatabaseQueryBuilder::getWhereSearch($expectedSearchValue, $fields, $args);

assertEquals($expected_sql, $sql);
assertEquals([$element1, $expectedSearchValue, $expectedSearchValue], $args);
}

}
Loading