You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
6.8 KiB

  1. <?php
  2. // Project: Web Reference Database (refbase) <http://www.refbase.net>
  3. // Copyright: Matthias Steffens <mailto:refbase@extracts.de> and the file's
  4. // original author(s).
  5. //
  6. // This code is distributed in the hope that it will be useful,
  7. // but WITHOUT ANY WARRANTY. Please see the GNU General Public
  8. // License for more details.
  9. //
  10. // File: ./queries.php
  11. // Repository: $HeadURL: file:///svn/p/refbase/code/branches/bleeding-edge/queries.php $
  12. // Author(s): Matthias Steffens <mailto:refbase@extracts.de>
  13. //
  14. // Created: 16-May-04, 22:03
  15. // Modified: $Date: 2017-04-13 02:00:18 +0000 (Thu, 13 Apr 2017) $
  16. // $Author: karnesky $
  17. // $Revision: 1416 $
  18. // This script takes a user query name (which was passed to the script by use of the 'Recall My Query' form on the main page 'index.php')
  19. // and extracts all saved settings for this particular query from the 'queries' MySQL table. It will then build an appropriate query URL
  20. // and pass that to 'search.php' which will finally display all matching records in list view.
  21. // TODO: I18n
  22. // Incorporate some include files:
  23. include 'initialize/db.inc.php'; // 'db.inc.php' is included to hide username and password
  24. include 'includes/include.inc.php'; // include common functions
  25. include 'initialize/ini.inc.php'; // include common variables
  26. // --------------------------------------------------------------------
  27. // START A SESSION:
  28. // call the 'start_session()' function (from 'include.inc.php') which will also read out available session variables:
  29. start_session(true);
  30. // --------------------------------------------------------------------
  31. // Initialize preferred display language:
  32. // (note that 'locales.inc.php' has to be included *after* the call to the 'start_session()' function)
  33. include 'includes/locales.inc.php'; // include the locales
  34. // --------------------------------------------------------------------
  35. // Extract any parameters passed to the script:
  36. if (isset($_REQUEST['querySearchSelector']))
  37. $querySearchSelector = $_REQUEST['querySearchSelector']; // get the name of the saved query that was chosen by the user
  38. else
  39. $querySearchSelector = "";
  40. // Determine the button that was hit by the user (in English localization, either 'Go' or 'Edit'):
  41. $submitAction = $_REQUEST['submit'];
  42. // Check the correct parameters have been passed:
  43. if (empty($querySearchSelector)) // if 'queries.php' was called without any valid parameters:
  44. {
  45. // return an appropriate error message:
  46. $HeaderString = returnMsg($loc["Warning_IncorrectOrMissingParams"] . " '" . scriptURL() . "'!", "warning", "strong", "HeaderString"); // functions 'returnMsg()' and 'scriptURL()' are defined in 'include.inc.php'
  47. // Redirect the browser back to the calling page:
  48. header("Location: " . $referer); // variable '$referer' is globally defined in function 'start_session()' in 'include.inc.php'
  49. exit; // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> !EXIT! <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  50. }
  51. else // the script was called with required parameters
  52. {
  53. connectToMySQLDatabase(); // function 'connectToMySQLDatabase()' is defined in 'include.inc.php'
  54. // CONSTRUCT SQL QUERY:
  55. // Fetch all saved settings for the user's query from the 'queries' table:
  56. $query = "SELECT query_id, display_type, view_type, query, show_query, show_links, show_rows, cite_style_selector, cite_order FROM $tableQueries WHERE user_id = " . quote_smart($loginUserID) . " AND query_name = " . quote_smart($querySearchSelector); // the global variable '$loginUserID' gets set in function 'start_session()' within 'include.inc.php'
  57. $result = queryMySQLDatabase($query); // RUN the query on the database through the connection (function 'queryMySQLDatabase()' is defined in 'include.inc.php')
  58. $rowsFound = @ mysqli_num_rows($result);
  59. if ($rowsFound == 1) // if there was exactly one row found (normally, this should be the case) ...
  60. {
  61. $row = mysqli_fetch_array($result);
  62. // redirect the browser to 'query_manager.php':
  63. if (encodeHTML($submitAction) == $loc["ButtonTitle_Edit"]) // note that we need to HTML encode '$submitAction' for comparison with the HTML encoded locales (function 'encodeHTML()' is defined in 'include.inc.php')
  64. {
  65. header("Location: query_manager.php?queryAction=edit&queryID=" . $row['query_id']);
  66. exit; // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> !EXIT! <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  67. }
  68. }
  69. else // if ($rowsFound != 1) // if there was NOT exactly one row found (i.e., something went wrong) ...
  70. {
  71. if ($rowsFound > 1) // if there were more than one row found ...
  72. $HeaderString = "<b><span class=\"warning\">There's more than one saved query matching your query title!</span></b>";
  73. else // if ($rowsFound == 0) // nothing found
  74. $HeaderString = "<b><span class=\"warning\">Your saved query couldn't be found!</span></b>";
  75. // update the 'userQueries' session variable:
  76. getUserQueries($loginUserID); // function 'getUserQueries()' is defined in 'include.inc.php'
  77. // Write back session variable:
  78. saveSessionVariable("HeaderString", $HeaderString); // function 'saveSessionVariable()' is defined in 'include.inc.php'
  79. // Redirect the browser back to the calling page:
  80. header("Location: " . $referer);
  81. exit; // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> !EXIT! <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  82. }
  83. // We also update the time stamp for that query in the 'queries' table:
  84. $updateQuery = "UPDATE $tableQueries SET "
  85. . "last_execution = NOW() " // set 'last_execution' field to the current date & time in 'DATETIME' format (which is 'YYYY-MM-DD HH:MM:SS', e.g.: '2003-12-31 23:45:59')
  86. . "WHERE user_id = " . quote_smart($loginUserID) . " AND query_id = " . quote_smart($row['query_id']);
  87. $updateResult = queryMySQLDatabase($updateQuery); // RUN the query on the database through the connection (function 'queryMySQLDatabase()' is defined in 'include.inc.php')
  88. // update the 'userQueries' session variable:
  89. getUserQueries($loginUserID); // function 'getUserQueries()' is defined in 'include.inc.php'
  90. disconnectFromMySQLDatabase(); // function 'disconnectFromMySQLDatabase()' is defined in 'include.inc.php'
  91. // Build the correct query URL:
  92. // TODO: use function 'generateURL()'
  93. $queryURL = "sqlQuery=" . rawurlencode($row['query']) . "&formType=sqlSearch&submit=" . $row['display_type'] . "&viewType=" . $row['view_type'] . "&showQuery=" . $row['show_query'] . "&showLinks=" . $row['show_links'] . "&showRows=" . $row['show_rows'] . "&citeOrder=" . $row['cite_order'] . "&citeStyle=" . $row['cite_style_selector'];
  94. // call 'search.php' with the correct query URL in order to display all records matching the user's query:
  95. header("Location: search.php?$queryURL");
  96. }
  97. ?>