Skip to content

Commit

Permalink
Merge pull request #1 from mugoweb/query-module-view
Browse files Browse the repository at this point in the history
Query module view
  • Loading branch information
pkamps authored Dec 12, 2016
2 parents 20f281e + b4ac1b8 commit ab713a9
Show file tree
Hide file tree
Showing 4 changed files with 230 additions and 1 deletion.
2 changes: 1 addition & 1 deletion design/standard/templates/ezfind/backoffice_left_menu.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<ul>
<li><div><a href={'/ezfind/elevate/'|ezurl}>{'Elevation'|i18n( 'extension/ezfind/backoffice_left_menu' )}</a></div></li>
{*
<li><div><a href={'/ezfind/query/'|ezurl}>{'Query'|i18n( 'extension/ezfind/backoffice_left_menu' )}</a></div></li> {*
TODO
<li><div><a href={'/ezfind/boost/'|ezurl}>{'Boost'|i18n( 'extension/ezfind/backoffice_left_menu' )}</a></div></li>
<li><div><a href={'/ezfind/facets/'|ezurl}>{'Facets'|i18n( 'extension/ezfind/backoffice_left_menu' )}</a></div></li>
Expand Down
103 changes: 103 additions & 0 deletions design/standard/templates/ezfind/query.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
{*
INPUT
data : form data
result : solr search result
*}
{def
$solr_fields_meta = array(
'meta_guid_ms',
'meta_installation_id_ms',
'meta_name_t',
'meta_owner_name_ms',
'meta_id_si',
'meta_current_version_si',
'meta_remote_id_ms',
'meta_class_identifier_ms',
'meta_main_node_id_si',
'meta_modified_dt',
'meta_published_dt',
'meta_main_url_alias_ms',
'meta_main_path_string_ms',
'meta_object_states_si',
)
$solr_fields_attr = array( 'attr_publish_date_dt' )
$solr_fields_other = array( 'timestamp', 'ezf_sp_words', 'ezf_df_text' )
$formats = array( 'php', 'json', 'csv' )
}

<div>
<form method="post" action={'ezfind/query'|ezurl()}>

<label>Query:(<a target="_blank" href="http://wiki.apache.org/solr/QueryParametersIndex">?</a>)</label>

<table>
<tr>
<td>q</td>
<td>
<textarea name="data[query]" type="text" style="width: 600px; height: 100px;">{$data[ 'query' ]}</textarea>
</td>
</tr>
<tr>
<td>format</td>
<td>
<select name="data[format]">
{foreach $formats as $format}
<option {if eq( $format, $data.format )}selected="selected"{/if}>{$format}</option>
{/foreach}
</select>
</td>
</tr>
<tr>
<td>offset</td>
<td><input type="text" name="data[offset]" value="{$data.offset}" /></td>
</tr>
<tr>
<td>limit</td>
<td><input type="text" name="data[limit]" value="{$data.limit}" /></td>
</tr>
<tr>
<td>sort</td>
<td><input type="text" name="data[sort]" value="{$data.sort}" /></td>
</tr>
<tr>
<td>Fields</td>
<td>
<select size="12" name="data[fields][]" multiple="multiple">
<optgroup label="Meta">
{foreach $solr_fields_meta as $solr_field}
<option value="{$solr_field}" {if $data.fields|contains( $solr_field )}selected="selected"{/if}>
{$solr_field}
</option>
{/foreach}
</optgroup>
<optgroup label="Attribute">
{foreach $solr_fields_attr as $solr_field}
<option value="{$solr_field}" {if $data.fields|contains( $solr_field )}selected="selected"{/if}>
{$solr_field}
</option>
{/foreach}
</optgroup>
<optgroup label="Others">
{foreach $solr_fields_other as $solr_field}
<option value="{$solr_field}" {if $data.fields|contains( $solr_field )}selected="selected"{/if}>
{$solr_field}
</option>
{/foreach}
</optgroup>
</select>
</td>
</tr>
</table>

<input type="submit" value="submit" name="submit" />
</form>
</div>

{if $result}
<div>
<pre style="max-width: 1200px; overflow-x: scroll">
{$result}
</pre>
</div>
{/if}
9 changes: 9 additions & 0 deletions modules/ezfind/module.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@
'params' => array( 'ObjectID', 'SearchQuery', 'LanguageCode' )
);

$ViewList[ 'query' ] = array(
'functions' => array( 'query_admin' ),
'default_navigation_part' => 'ezfindnavigationpart',
'ui_context' => 'administration',
'script' => 'query.php',
'params' => array(),
);

$FunctionList = array();
$FunctionList['elevate'] = array();
$FunctionList['query_admin'] = array();
?>
117 changes: 117 additions & 0 deletions modules/ezfind/query.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

$tpl = eZTemplate::factory();
$result = '';
$module = $Params[ 'Module' ];
$formats = array( 'php', 'json', 'csv' );
$field_list = array();
$data = array(
'limit' => 10,
'offset' => 0,
'query' => '',
'format' => 'php',
'fields' => array(),
);

if( isset( $_REQUEST[ 'submit' ] ) )
{
$solr = new eZSolrBase();

$data = array_replace_recursive( $data, $_REQUEST[ 'data' ] );

$format = 'php';

$params = array(
'q' => '*',
'fl' => '*',
);

if( $data[ 'query' ] )
{
$params[ 'q' ] = $data[ 'query' ];
}
if( in_array( $data[ 'format' ], $formats ) )
{
$format = $data[ 'format' ];
}
if( $format == 'csv' && $params[ 'q' ] && $data[ 'fields' ] )
{
$params[ 'fl' ] = $data[ 'fields' ];
}
if( $data[ 'limit' ] )
{
$params[ 'rows' ] = $data[ 'limit' ];
}
if( $data[ 'offset' ] )
{
$params[ 'start' ] = $data[ 'limit' ];
}
if( $data[ 'sort' ] )
{
$params[ 'sort' ] = $data[ 'sort' ];
}

$result = $solr->rawSearch( $params );

$result = print_r( formatData( $result, $format, $data[ 'fields' ] ), true );
}

$tpl->setVariable( 'data', $data );
$tpl->setVariable( 'result', $result );

$Result = array();
$Result['content'] = $tpl->fetch( "design:ezfind/query.tpl" );
$Result['left_menu'] = "design:ezfind/backoffice_left_menu.tpl";
$Result['path'] = array( array( 'url' => false,
'text' => ezpI18n::tr( 'extension/ezfind', 'eZFind' ) ),
array( 'url' => false,
'text' => ezpI18n::tr( 'extension/ezfind', 'Query' ) ) );

/*
* functions
*/

function formatData( $data, $format, $fields )
{
switch( $format )
{
case 'csv':
{
$resultData = $data[ 'response' ][ 'docs' ];

$csvData = fopen('php://temp/maxmemory:'. (5*1024*1024), 'r+');
fputcsv( $csvData, $fields );

foreach( $resultData as $row )
{
$csvRow = array();
foreach( $fields as $id )
{
$value = $row[ $id ];
// flatten arrays
if( is_array( $value ) )
{
$value = implode( '::', $value );
}
$csvRow[] = $value;
}

fputcsv( $csvData, $csvRow );
}

// reset pointer
rewind( $csvData );

$data = stream_get_contents( $csvData );
}
break;

case 'json':
{
$data = json_encode( $data );
}
break;
}

return $data;
}

0 comments on commit ab713a9

Please sign in to comment.