';
$form .= '';
$form .= '| '.$i18n->get('s location').' | '.WebGUI::Form::radioList($self->session, {
name => "searchInTrash",
options => \%searchInTrashOptions,
value => $searchInTrash,
});
$form .= ' |
';
$form .= ''.$i18n->get('s search type').' | '.WebGUI::Form::radioList($self->session, {
name => "searchType",
options => {'or' => $i18n->get('or'), 'and' => $i18n->get('and')},
value => $searchType,
});
$form .= ' | ';
$self->session->scratch->set('SQLForm_'.$self->getId.'searchType', $searchType);
$self->session->scratch->set('SQLForm_'.$self->getId.'searchInTrash', $searchInTrash);
foreach (@$fieldList) {
if ($self->session->form->process("searchQueried")) {
$self->session->scratch->delete('SQLForm_'.$self->getId.'---'.$_.'v1');
$self->session->scratch->delete('SQLForm_'.$self->getId.'---'.$_.'v2');
$self->session->scratch->delete('SQLForm_'.$self->getId.'---'.$_.'c');
}
my $formValue1 = $self->session->form->process($_.'-1') || $self->session->scratch->get('SQLForm_'.$self->getId.'---'.$_.'v1');
my $formValue2 = $self->session->form->process($_.'-2') || $self->session->scratch->get('SQLForm_'.$self->getId.'---'.$_.'v2');
my $conditional = $self->session->form->process('_'.$_.'_conditional') || $self->session->scratch->get('SQLForm_'.$self->getId.'---'.$_.'c');
$self->session->scratch->set('SQLForm_'.$self->getId.'---'.$_.'v1', $formValue1);
$self->session->scratch->set('SQLForm_'.$self->getId.'---'.$_.'v2', $formValue2);
$self->session->scratch->set('SQLForm_'.$self->getId.'---'.$_.'c', $conditional);
if ($fieldProperties->{$_}->{type} eq 'list') {
if ($self->session->form->process($_.'-2')) {
$formValue2 = [ $self->session->request->param($_.'-2') ];
$self->session->scratch->set('SQLForm_'.$self->getId.'---'.$_.'v2', Storable::freeze($formValue2));
} else {
$formValue2 = eval('Storable::thaw($formValue2)');
}
}
$form .= '';
$form .= '| '.$fieldProperties->{$_}->{displayName}.' | ';
$form .= '';
if (exists $types->{$fieldProperties->{$_}->{type}}) {
$form .= WebGUI::Form::selectList($self->session, {
name => '_'.$_.'_conditional',
value => [ $conditional || '' ],
options => $types->{$fieldProperties->{$_}->{type}},
extras => 'onchange="'.$typeFunctions->{$fieldProperties->{$_}->{type}}.'(this.value, \''.$_.'\')"',
size => 1,
multiple=> 0,
});
$js .= $typeFunctions->{$fieldProperties->{$_}->{type}}."('".$conditional."', '$_');";
}
$form .= ' | ';
$form .= '';
my $parameters = {};
$parameters->{name} = $_.'-1';
$parameters->{value} = $formValue1;
$parameters->{options} = $fieldProperties->{$_}->{options} if ($fieldProperties->{$_}->{hasOptions});
$parameters->{id} = $_.'-1"';
my $searchElement = $fieldProperties->{$_}->{searchElement};
$searchElement = 'text' if ($searchElement eq 'selectList');
my $cmd = "WebGUI::Form::$searchElement".'($self->session, $parameters)';
$form .= eval($cmd);
unless ($fieldProperties->{$_}->{type} eq 'text') {
$searchElement = $fieldProperties->{$_}->{searchElement};
$parameters->{name} = $_.'-2';
$parameters->{value} = $formValue2;
$parameters->{size} = undef;
$parameters->{id} = $_.'-2"';
if ($fieldProperties->{$_}->{type} eq 'list') {
$parameters->{multiple} = 1;
$parameters->{size} = 5;
$parameters->{value} = $formValue2;
}
$cmd = "WebGUI::Form::$searchElement".'($self->session, $parameters)';
$form .= eval($cmd);
}
$form .= ' | ';
$form .= '
';
}
$form .= ''.WebGUI::Form::submit($self->session, {value => $i18n->get('s search button')}).' | ';
$form .= '
';
$form .= WebGUI::Form::formFooter($self->session);
$form .= '';
$form .= '';
return $form;
}
#-------------------------------------------------------------------
=head1 _constructSearchQuery ( searchInFields, showFields, fieldProperties )
Constructs an SQL query from the search query
=head2 searchInFields
Arrayref containing the field id's that should be included in the search.
=head2 showFields
List of field id's that should be shown in the results.
=head2 fieldProperties
Hashref containing the properties of the fields that are in the search.
=cut
sub _constructSearchQuery {
my (@tables, @joinConstraints, $tableCounter, @constraints, $currentField, $conditional, @joinSequence);
my $self = shift;
my $searchInFields = shift;
my $showFields = shift;
my $fieldProperties = shift;
my $passedQuery = shift;
# This variable should be set to value of the minimum word length for fulltext searches
# as it is set in your MySQL database. Normally this is 3.
my $minimumFulltextLength = 3;
# Include the table the form writes to.
$tableCounter = 2;
# Process search fields.
foreach $currentField (@$searchInFields) {
# Set conditional given for this field or to like or regexp mode if in normal search
my $searchMode = $self->session->form->process("searchMode") || $self->session->scratch->get('SQLForm_'.$self->getId.'searchMode');
if ($searchMode) {
$conditional = 100 if ($searchMode eq 'normal');
$conditional = 101 if ($searchMode eq 'regexp');
} else {
$conditional = $self->session->form->process('_'.$currentField.'_conditional') || $self->session->scratch->get('SQLForm_'.$self->getId.'---'.$currentField.'c');
}
$tableCounter++;
if ($conditional ne '') {
my $currentFieldProperties = $fieldProperties->{$currentField};
my $fieldName = $currentFieldProperties->{fieldName};
my $fieldType = $currentFieldProperties->{type};
my $fullFieldName = "t1.$fieldName";
my $constraint;
my $query = $passedQuery || $self->session->form->process("searchQuery") || $self->session->form->process($currentField.'-1') || $self->session->scratch->get('SQLForm_'.$self->getId.'query');
my $queryLike;
if ($conditional == 100 || $conditional == 101) {
$query =~ s/\\/\\\\/g;
$query =~ s/'/\\'/g;
# Search on 'like'
if ($conditional == 100) {
$queryLike = $query;
$queryLike =~ s/%/\\%/g;
$queryLike =~ s/\*/%/g;
$queryLike = "'%".$queryLike."%'";
}
$query = "'$query'";
}
my $formValue1 = $self->session->form->process($currentField.'-1') || $self->session->scratch->get('SQLForm_'.$self->getId.'---'.$currentField.'v1');
my $formValue2 = $self->session->form->process($currentField.'-2') || $self->session->scratch->get('SQLForm_'.$self->getId.'---'.$currentField.'v2');
if ($fieldType eq 'list') {
if ($self->session->form->process($currentField.'-2')) {
$formValue2 = [ $self->session->request->param($currentField.'-2') ];
} else {
$formValue2 = Storable::thaw($formValue2);
}
}
if ($conditional == 200 && $formValue2) {
#$constraint = "(".join(' or ', map {"$fullFieldName = ".$self->session->db->quote($_)} $self->session->request->param($currentField.'-2')).")";
$constraint = "(".join(' or ', map {"$fullFieldName = ".$self->session->db->quote($_)} @$formValue2).")";
} elsif ($conditional == 201 && $formValue2) {
#$constraint = "(".join(' and ', map {"$fullFieldName = ".$self->session->db->quote($_)} $self->session->request->param($currentField.'-2')).")";
$constraint = "(".join(' or ', map {"$fullFieldName = ".$self->session->db->quote($_)} @$formValue2).")";
# Match the joined columns only if type is a list and has joins.
# Else the regular like and regex will handle this.
} elsif ($fieldType eq 'list' && $currentFieldProperties->{numberOfJoins}) {
my $prepend = "t$tableCounter";
for my $joinCounter (1 .. $currentFieldProperties->{numberOfJoins}) {
my $joinStatement = $currentFieldProperties->{"database$joinCounter"}.'.'.
$currentFieldProperties->{"table$joinCounter"}." as ".$prepend."table$joinCounter";
if ($joinCounter > 1) {
$joinStatement .= " on ".
$prepend.$currentFieldProperties->{"joinOnA$joinCounter"}.'='.
$prepend.$currentFieldProperties->{"joinOnB$joinCounter"};
} else {
$joinStatement .= " on ".
$fullFieldName." = ".$prepend.$currentFieldProperties->{selectField1};
}
push(@joinSequence, $joinStatement);
}
if ($conditional == 100) {
$constraint .= $prepend.$currentFieldProperties->{selectField2}." like ".$queryLike;
} else {
$constraint .= $prepend.$currentFieldProperties->{selectField2}." regexp($query)";
}
# 10 = between
} elsif ($conditional == 10) {
$constraint =
"($fullFieldName > ".$self->session->db->quote($formValue1)." and ".
" $fullFieldName <".$self->session->db->quote($formValue2).")";
# 100 = like
} elsif ($conditional == 100) {
if ($currentFieldProperties->{useFulltext} && length($query) >= $minimumFulltextLength) {
$constraint = "match($fullFieldName) against($query in boolean mode)";
} else {
$constraint = "$fullFieldName like $queryLike";
}
# 101 = regexp
} elsif ($conditional == 101) {
$constraint = "$fullFieldName regexp($query)";
} else {
$constraint = "$fullFieldName ".$types->{$fieldType}->{$conditional}." ".$self->session->db->quote($formValue1);
}
push(@constraints, $constraint) if $constraint;
}
}
my $searchInTrash = $self->session->scratch->get('SQLForm_'.$self->getId.'searchInTrash') || $self->session->form->process("searchInTrash") || '0';
my $searchType = ($self->session->form->process("searchType") || $self->session->scratch->get('SQLForm_'.$self->getId.'searchType')) eq 'and' ? 'and' : 'or';
return undef if (!@constraints);
# Construct the search query
my $sql = " select t1.__recordId, t1.__deletionDate, t1.__deletedBy, t1.__initDate, t1.__userId, t1.__deleted, t1.__archived, t1.__revision ";
$sql .= ", ".join(", \n", map {"t1.".$fieldProperties->{$_}->{fieldName}} @$showFields)."\n";
$sql .= " from ".$self->get('tableName').' as t1 ';
$sql .= " left join ".join(" left join \n", @joinSequence)."\n" if (@joinSequence);
$sql .= " where ";
$sql .= "(".join(" $searchType \n", @constraints).")\n" if (@constraints);
$sql .= " and " if (@constraints);
$sql .= " t1.__archived=0 ";
$sql .= " and t1.__deleted=".$self->session->db->quote($searchInTrash) if ($searchInTrash < 2);
my $sortColumn = $self->session->form->process("sortColumn");
$sortColumn = $self->session->scratch->get('SQLForm_'.$self->getId.'sortColumn') unless ($sortColumn);
$self->session->scratch->set('SQLForm_'.$self->getId.'sortColumn', $sortColumn);
my $sortAscending = $self->session->form->process("sortAscending");
$sortAscending = $self->session->scratch->get('SQLForm_'.$self->getId.'sortAscending') unless (defined $self->session->form->process("sortAscending"));
$self->session->scratch->set('SQLForm_'.$self->getId.'sortAscending', $sortAscending);
if (isIn($sortColumn, @$showFields)) {
$sql .= " order by ".$fieldProperties->{$sortColumn}->{fieldName};
$sql .= " desc " unless ($sortAscending);
}
return $sql;
}
#-------------------------------------------------------------------
=head1 _processSearchQuery ( sth, showFields, fieldProperties )
Processes the results of a search query and returns an arrayref suitable for use as a template loop.
=head2 sth
Statement handle of the executed query.
=head2 showFields
List of field id's that should be shown in the results.
=head2 fieldProperties
Hashref containing the properties of the fields that are in the search.
=cut
sub _processSearchQuery {
my $self = shift;
my $sth = shift;
my $showFields = shift;
my $fieldProperties = shift;
my $i18n = WebGUI::International->new($self->session, 'Asset_SQLForm');
my $recordControls;
my $searchInTrash;
my @recordLoop;
while (my %row = $sth->hash) {
my %record;
my $fieldValues;
if ($self->_canEditRecord) {
if ($row{__deleted}) {
$recordControls = WebGUI::Form::checkbox($self->session, {name=>'rid', value=>$row{__recordId}});
$recordControls .= '