From 6e626f6a858e7cc58ce54761b7466a12824edae4 Mon Sep 17 00:00:00 2001 From: Romain Neutron Date: Wed, 15 Feb 2012 17:25:45 +0100 Subject: [PATCH] Add query controller --- lib/Alchemy/Phrasea/Application/Prod.php | 1 + lib/Alchemy/Phrasea/Controller/Prod/Query.php | 236 ++++++++++++++++++ 2 files changed, 237 insertions(+) create mode 100644 lib/Alchemy/Phrasea/Controller/Prod/Query.php diff --git a/lib/Alchemy/Phrasea/Application/Prod.php b/lib/Alchemy/Phrasea/Application/Prod.php index 81b9fbe68e..2d65ce3bf4 100644 --- a/lib/Alchemy/Phrasea/Application/Prod.php +++ b/lib/Alchemy/Phrasea/Application/Prod.php @@ -40,6 +40,7 @@ return call_user_func(function() }); $app->mount('/UserPreferences/', new Controller\UserPreferences()); + $app->mount('/query/', new Controller\Query()); $app->mount('/baskets', new Controller\Basket()); $app->mount('/story', new Controller\Story()); $app->mount('/WorkZone', new Controller\WorkZone()); diff --git a/lib/Alchemy/Phrasea/Controller/Prod/Query.php b/lib/Alchemy/Phrasea/Controller/Prod/Query.php new file mode 100644 index 0000000000..f7be9f31fc --- /dev/null +++ b/lib/Alchemy/Phrasea/Controller/Prod/Query.php @@ -0,0 +1,236 @@ +match('/', function(Application $app, Request $request) + { + + $appbox = \appbox::get_instance(); + $registry = $appbox->get_registry(); + + $user = $app['Core']->getAuthenticatedUser(); + + $query = (string) $request->get('qry'); + + $mod = $user->getPrefs('view'); + + $json = array(); + + $options = new \searchEngine_options(); + + $bas = is_array($request->get('bas')) ? $request->get('bas') : array(); + + $status = is_array($request->get('status')) ? $request->get('status') : array(); + $fields = is_array($request->get('fields')) ? $request->get('fields') : array(); + + $options->set_fields($fields); + $options->set_status($status); + $options->set_bases($bas, $user->ACL()); + + $options->set_search_type($request->get('search_type')); + $options->set_record_type($request->get('recordtype')); + $options->set_min_date($request->get('datemin')); + $options->set_max_date($request->get('datemax')); + $options->set_date_fields(explode('|', $request->get('datefield'))); + $options->set_sort($request->get('sort'), $request->get('ord', PHRASEA_ORDER_DESC)); + $options->set_use_stemming($request->get('stemme')); + + $form = serialize($options); + + $perPage = (int) $user->getPrefs('images_per_page'); + + $search_engine = new \searchEngine_adapter($registry); + $search_engine->set_options($options); + + $page = (int) $request->get('pag'); + + if ($page < 1) + { + $search_engine->set_is_first_page(true); + $search_engine->reset_cache(); + $page = 1; + } + + $result = $search_engine->query_per_page($query, $page, $perPage); + + $proposals = $search_engine->is_first_page() ? $result->get_propositions() : false; + + $npages = $result->get_total_pages(); + + $page = $result->get_current_page(); + + $string = ''; + + if ($npages > 1) + { + + $d2top = ($npages - $page); + $d2bottom = $page; + + if (min($d2top, $d2bottom) < 4) + { + if ($d2bottom < 4) + { + for ($i = 1; ($i <= 4 && (($i <= $npages) === true)); $i++) + { + if ($i == $page) + $string .= ''; + else + $string .= "" . $i . ""; + } + if ($npages > 4) + $string .= ">>"; + } + else + { + $start = $npages - 4; + if (($start) > 0) + $string .= "<<"; + else + $start = 1; + for ($i = ($start); $i <= $npages; $i++) + { + if ($i == $page) + $string .= ''; + else + $string .= "" . $i . ""; + } + } + } + else + { + $string .= "<<"; + + for ($i = ($page - 2); $i <= ($page + 2); $i++) + { + if ($i == $page) + $string .= ''; + else + $string .= "" . $i . ""; + } + + $string .= ">>"; + } + } + $string .= '
'; + + + $explain = "
"; + + $explain .= ""; + + if ($result->get_count_total_results() != $result->get_count_available_results()) + { + $explain .= sprintf(_('reponses:: %d Resultats rappatries sur un total de %d trouves'), $result->get_count_available_results(), $result->get_count_total_results()); + } + else + { + $explain .= sprintf(_('reponses:: %d Resultats'), $result->get_count_total_results()); + } + + $explain .= " "; + $explain .= '
' . $result->get_query_time() . ' s
dans index ' . $result->get_search_indexes(); + $explain .= "
"; + + + + $infoResult = '' . sprintf(_('reponses:: %d reponses'), $result->get_count_total_results()) . ' | ' . sprintf(_('reponses:: %s documents selectionnes'), ''); + + $json['infos'] = $infoResult; + $json['navigation'] = $string; + + $prop = null; + + if ($search_engine->is_first_page()) + { + $propals = $result->get_suggestions(); + if (count($propals) > 0) + { + foreach ($propals as $prop_array) + { + if ($prop_array['value'] !== $query && $prop_array['hits'] > $result->get_count_total_results()) + { + $prop = $prop_array['value']; + break; + } + } + } + } + + $core = \bootstrap::getCore(); + $twig = $core->getTwig(); + + if ($result->get_count_total_results() === 0) + { + $template = 'prod/results/help.twig'; + } + else + { + if ($mod == 'thumbs') + { + $template = 'prod/results/answergrid.html'; + } + else + { + $template = 'prod/results/answerlist.html'; + } + } + + + $json['results'] = $twig->render($template, array( + 'results' => $result, + 'GV_social_tools' => $registry->get('GV_social_tools'), + 'array_selected' => explode(';', $request->get('sel')), + 'highlight' => $search_engine->get_query(), + 'searchEngine' => $search_engine, + 'suggestions' => $prop + ) + ); + + + $json['query'] = $query; + $json['phrasea_props'] = $proposals; + $json['total_answers'] = (int) $result->get_count_available_results(); + $json['next_page'] = ($page < $npages && $result->get_count_available_results() > 0) ? ($page + 1) : false; + $json['prev_page'] = ($page > 1 && $result->get_count_available_results() > 0) ? ($page - 1) : false; + $json['form'] = $form; + + + $datas = $app['Core']['Serializer']->serialize($json, 'json'); + + return new Response($datas, 200, array('Content-Type' => 'application/json')); + }); + + return $controllers; + } + +}