From e96df2bcc6955b76ca4a66dd324f7fb53852c287 Mon Sep 17 00:00:00 2001 From: Matthieu Leproux Date: Fri, 20 Aug 2021 15:11:48 +0200 Subject: [PATCH] related to #1465 Search action done in ajax, still need to construct response properly --- admin/history.php | 9 +- admin/themes/default/js/history.js | 42 +++++++ admin/themes/default/template/history.tpl | 10 +- include/ws_functions/pwg.php | 129 ++++++++++++++++++++++ ws.php | 8 ++ 5 files changed, 195 insertions(+), 3 deletions(-) create mode 100644 admin/themes/default/js/history.js diff --git a/admin/history.php b/admin/history.php index 6ed3a872d..436a4953f 100644 --- a/admin/history.php +++ b/admin/history.php @@ -161,7 +161,8 @@ history_tabsheet(); $template->assign( array( 'U_HELP' => get_root_url().'admin/popuphelp.php?page=history', - 'F_ACTION' => get_root_url().'admin.php?page=history' + 'F_ACTION' => get_root_url().'admin.php?page=history', + 'API_METHOD' => 'ws.php?format=json&method=pwg.history.search' ) ); @@ -182,6 +183,7 @@ SELECT rules $page['search'] = unserialize($serialized_rules); + // Used when filtering on a specific user from displayed lines if (isset($_GET['user_id'])) { if (!is_numeric($_GET['user_id'])) @@ -212,6 +214,7 @@ INSERT INTO '.SEARCH_TABLE.' $page['nb_lines'] = count($data); + //Number of ids of each kind $history_lines = array(); $user_ids = array(); $username_of = array(); @@ -435,7 +438,8 @@ SELECT } } } - + + // Put in ajax response $template->append( 'search_results', array( @@ -480,6 +484,7 @@ SELECT $member_strings[] = $member_string; } + // Put in ajax response $template->assign( 'search_summary', array( diff --git a/admin/themes/default/js/history.js b/admin/themes/default/js/history.js new file mode 100644 index 000000000..e62f540dd --- /dev/null +++ b/admin/themes/default/js/history.js @@ -0,0 +1,42 @@ +$(document).ready(() => { + + $(".filter").submit(function (e) { + e.preventDefault(); + + var dataArray = $(this).serializeArray() + console.log(dataArray); + dataObj = {}; + + dataObj["types"] = []; + $(dataArray).each(function(i, field){ + if (field.name == "types[]") { + dataObj["types"].push(field.value); + } else { + dataObj[field.name] = field.value; + } + }); + + $.ajax({ + url: API_METHOD, + method: "POST", + data: { + start: dataObj['start'], + end: dataObj['end'], + types: dataObj['types'], + user: dataObj['user'], + image_id: dataObj['image_id'], + filename: dataObj['filename'], + ip: dataObj['ip'], + display_thumbnail: dataObj['display_thumbnail'], + }, + success: function (data) { + console.log(data); + }, + error: function (e) { + console.log("Something went wrong: " + e); + } + }) + + console.log(dataObj); + }) +}) \ No newline at end of file diff --git a/admin/themes/default/template/history.tpl b/admin/themes/default/template/history.tpl index a9a9fc6ea..f3edaf998 100644 --- a/admin/themes/default/template/history.tpl +++ b/admin/themes/default/template/history.tpl @@ -5,11 +5,19 @@ jQuery(function(){ {* *} jQuery('[data-datepicker]').pwgDatepicker(); }); + +const API_METHOD = "{$API_METHOD}"; {/footer_script} +{combine_script id='common' load='footer' path='admin/themes/default/js/common.js'} +{combine_script id='history' load='footer' path='admin/themes/default/js/history.js'} + +{combine_script id='jquery.confirm' load='footer' require='jquery' path='themes/default/js/plugins/jquery-confirm.min.js'} +{combine_css path="admin/themes/default/fontello/css/animation.css" order=10} {* order 10 is required, see issue 1080 *} +

{'History'|@translate} {$TABSHEET_TITLE}

-
+
{'Filter'|@translate}
    diff --git a/include/ws_functions/pwg.php b/include/ws_functions/pwg.php index 4a1a9e476..b2f7b881c 100644 --- a/include/ws_functions/pwg.php +++ b/include/ws_functions/pwg.php @@ -578,4 +578,133 @@ SELECT ); } +function ws_history_search($param, &$service) +{ + +if (isset($_GET['start']) and is_numeric($_GET['start'])) +{ + $page['start'] = $_GET['start']; +} +else +{ + $page['start'] = 0; +} + +$types = array_merge(array('none'), get_enums(HISTORY_TABLE, 'image_type')); + +$display_thumbnails = array('no_display_thumbnail' => l10n('No display'), + 'display_thumbnail_classic' => l10n('Classic display'), + 'display_thumbnail_hoverbox' => l10n('Hoverbox display') + ); + +// +-----------------------------------------------------------------------+ +// | Build search criteria and redirect to results | +// +-----------------------------------------------------------------------+ + +$page['errors'] = array(); +$search = array(); + +// date start +if (!empty($param['start'])) +{ + check_input_parameter('start', $param, false, '/^\d{4}-\d{2}-\d{2}$/'); + $search['fields']['date-after'] = $param['start']; +} + +// date end +if (!empty($param['end'])) +{ + check_input_parameter('end', $param, false, '/^\d{4}-\d{2}-\d{2}$/'); + $search['fields']['date-before'] = $param['end']; +} + +// types +if (empty($param['types'])) +{ + $search['fields']['types'] = $types; +} +else +{ + check_input_parameter('types', $param, true, '/^('.implode('|', $types).')$/'); + $search['fields']['types'] = $param['types']; +} + +// user +$search['fields']['user'] = intval($param['user']); + +// image +if (!empty($param['image_id'])) +{ + $search['fields']['image_id'] = intval($param['image_id']); +} + +// filename +if (!empty($param['filename'])) +{ + $search['fields']['filename'] = str_replace( + '*', + '%', + pwg_db_real_escape_string($param['filename']) + ); +} + +// ip +if (!empty($param['ip'])) +{ + $search['fields']['ip'] = str_replace( + '*', + '%', + pwg_db_real_escape_string($param['ip']) + ); +} + +// thumbnails +check_input_parameter('display_thumbnail', $param, false, '/^('.implode('|', array_keys($display_thumbnails)).')$/'); + +$search['fields']['display_thumbnail'] = $param['display_thumbnail']; +// Display choise are also save to one cookie +if (!empty($param['display_thumbnail']) + and isset($display_thumbnails[$param['display_thumbnail']])) +{ + $cookie_val = $param['display_thumbnail']; +} +else +{ + $cookie_val = null; +} + +pwg_set_cookie_var('display_thumbnail', $cookie_val, strtotime('+1 month') ); + +// TODO manage inconsistency of having $_POST['image_id'] and +// $_POST['filename'] simultaneously + +// store seach in database +if (!empty($search)) +{ + // register search rules in database, then they will be available on + // thumbnails page and picture page. + $query =' +INSERT INTO '.SEARCH_TABLE.' +(rules) +VALUES +(\''.pwg_db_real_escape_string(serialize($search)).'\') +;'; + + pwg_query($query); + + $search_id = pwg_db_insert_id(SEARCH_TABLE); + + // Remove redirect for ajax // + // redirect( + // PHPWG_ROOT_PATH.'admin.php?page=history&search_id='.$search_id + // ); +} +else +{ + $page['errors'][] = l10n('Empty query. No criteria has been entered.'); +} + + return $param; +} + ?> \ No newline at end of file diff --git a/ws.php b/ws.php index 0a346b0c9..de5dd6177 100644 --- a/ws.php +++ b/ws.php @@ -1244,6 +1244,14 @@ enabled_high, registration_date, registration_date_string, registration_date_sin 'Returns the favorite images of the current user.', $ws_functions_root . 'pwg.users.php' ); + + $service->addMethod( + 'pwg.history.search', + 'ws_history_search', + null, + 'Gives an history of who has visited the galery and the actions done in it. Receives parameter.', + $ws_functions_root . 'pwg.php' + ); } ?>