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.

129 lines
6.9 KiB

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