Last active
April 18, 2017 09:22
-
-
Save andrealandonio/787763f888b05bea37b11ef446b43127 to your computer and use it in GitHub Desktop.
Custom CS search page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Includes | |
include_once('../wp-load.php'); | |
// Set timeout | |
set_time_limit(0); | |
// Set default timezone | |
date_default_timezone_set('Europe/Rome'); | |
// Read global parameters | |
$search_field = $_GET['search_field']; | |
$search_keyword = $_GET['search_keyword']; | |
$start = $_GET['start']; | |
$size = $_GET['size']; | |
$origin = $_GET['origin']; | |
// Prepare keyword | |
if (!empty($search_field) && !empty($search_keyword)) { | |
// Search in a specific field | |
$keyword = $search_field . ':' . (is_numeric($search_keyword) ? $search_keyword : '\'' . $search_keyword . '\''); | |
} | |
elseif (empty($search_field) && !empty($search_keyword)) { | |
// Search in a all fields | |
$keyword = $search_keyword; | |
} | |
else { | |
die('Provide a valid search parameters ("search_field" or "search_keyword")'); | |
} | |
// Prepare filter query (leave empty for no additional filters) | |
if (!empty($origin)) { | |
// Use provided origin | |
$filter_query = 'cf_cms_origin:' . '\'' . $origin . '\''; | |
} | |
else { | |
// Use custom origins (only "wp" and "legacy") | |
$filter_query = '(or cf_cms_origin:\'wp\' cf_cms_origin:\'legacy\')'; | |
} | |
// Check defaults | |
if (empty($size)) $size = 10; | |
if (empty($start)) { | |
$page = 0; | |
$start = 0; | |
} | |
else { | |
$page = (intval($start) - 1); | |
$start = (intval($start) - 1) * $size; | |
} | |
echo '<h1>Query CS</h1>'; | |
echo '<h3>Data</h3>'; | |
echo '<ul>'; | |
echo '<li><i>keyword</i>: ' . $keyword . '</li>'; | |
echo '<li><i>filter</i>: ' . $filter_query . '</li>'; | |
echo '<li><i>start</i>: ' . $start . '</li>'; | |
echo '<li><i>size</i>: ' . $size . '</li>'; | |
echo '</ul>'; | |
try { | |
// Search full data | |
$acs_result = acs_index_documents_search($keyword, ACS::TYPE_FIELD_DEFAULT, $start, $size, 'structured', $filter_query, true, ACS::TYPE_FIELD_DEFAULT, ACS::SORT_FIELD_DEFAULT, ACS::SORT_ORDER_DEFAULT, true); | |
$result_search = $acs_result->get_data(); | |
} | |
catch (Exception $e) { | |
$result_search = array(); | |
echo '<h3>Results</h3>'; | |
echo '<ul>'; | |
echo '<li>Error: ' . $e->getMessage() . '</li>'; | |
echo '</ul>'; | |
} | |
echo '<h3>Results (' . count($result_search['items']) . ')</h3>'; | |
echo '<ul>'; | |
if (!empty($result_search) && count($result_search['items']) > 0) { | |
// Loop over document founded | |
foreach ($result_search['items'] as $item) { | |
// Get post data | |
$post_id = $item['id'][0]; | |
$post_type = $item['post_type'][0]; | |
$post_title = $item['post_title'][0]; | |
$post_content = $item['post_content'][0]; | |
echo '<li><i>item</i>: ' . $post_id . ' - ' . $post_type . ' - ' . $post_title . ' - ' . $post_content . '</li>'; | |
} | |
// Manage pagination info | |
$found_items = $result_search['found']; | |
$total_pages = intval($result_search['found'] / $size) + 1; | |
$current_page = $page + 1; | |
// Calculate pagination | |
if (!empty($found_items) && !empty($current_page) && !empty($total_pages)) { | |
echo '<nav class="pagination">'; | |
echo '<ul class="pagination" style="list-style: none; padding: 20px 0;">'; | |
if ($current_page >= 4) echo '<li style="display: inline-block; margin: 0 5px;"><a href="' . decode_pagination_url(1) . '">1</a></li>'; | |
if ($current_page > 4) echo '<li style="display: inline-block; margin: 0 5px;"><span>…</span></li>'; | |
if ($current_page - 2 >= 1) echo '<li style="display: inline-block; margin: 0 5px;"><a href="' . decode_pagination_url($current_page - 2) . '">' . ($current_page - 2) . '</a></li>'; | |
if ($current_page - 1 >= 1) echo '<li style="display: inline-block; margin: 0 5px;"><a href="' . decode_pagination_url($current_page - 1) . '">' . ($current_page - 1) . '</a></li>'; | |
if ($current_page != $total_pages) echo '<li class="active" style="display: inline-block; margin: 0 5px;"><a href="' . decode_pagination_url($current_page) . '">' . $current_page . '</a></li>'; | |
if ($current_page + 1 < $total_pages) echo '<li style="display: inline-block; margin: 0 5px;"><a href="' . decode_pagination_url($current_page + 1) . '">' . ($current_page + 1) . '</a></li>'; | |
if ($current_page + 2 < $total_pages) echo '<li style="display: inline-block; margin: 0 5px;"><a href="' . decode_pagination_url($current_page + 2) . '">' . ($current_page + 2) . '</a></li>'; | |
if ($current_page + 4 <= $total_pages) echo '<li style="display: inline-block; margin: 0 5px;"><span>…</span></li>'; | |
echo '<li class="' . (($current_page == $total_pages) ? 'active' : '') . '" style="display: inline-block; margin: 0 5px;"><a href="' . decode_pagination_url($total_pages) . '">' . $total_pages . '</a></li>'; | |
echo '</ul>'; | |
echo '</nav>'; | |
} | |
} | |
else { | |
// No documents founded | |
echo '<li>No document founded</li>'; | |
} | |
echo '</ul>'; | |
/** | |
* Get pagination link | |
* | |
* @param int $page | |
* | |
* @return string | |
*/ | |
function decode_pagination_url($page = 1) { | |
// Get page URL | |
$page_url = (isset($_SERVER['HTTPS']) ? 'http' : 'http') . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; | |
// GEt page parameters | |
$page_parameters = $_SERVER['QUERY_STRING']; | |
if (!empty($page_parameters)) { | |
$parameters = explode('&', $page_parameters); | |
$page_url .= '?'; | |
$count = 0; | |
foreach ($parameters as $parameter) { | |
$parameter_values = explode('=', $parameter); | |
if ($parameter_values[0] == 'start') { | |
$page_url .= (($page == 1) ? '' : ((($count != 0 ) ? '&' : '') . 'start=' . $page)); | |
} | |
elseif (!empty($parameter_values[0])) { | |
$page_url .= ($count != 0 ) ? '&' : ''; | |
$page_url .= $parameter_values[0] . '=' . $parameter_values[1]; | |
} | |
$count = $count + 1; | |
} | |
if (strpos($page_parameters, 'start=') === false) { | |
// No start parameter found, add it | |
$page_url .= (($page == 1) ? '' : ((($count != 0 ) ? '&' : '') . 'start=' . $page)); | |
} | |
} | |
return $page_url; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment