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.

170 lines
9.6 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: ./cite/formats/cite_rtf.php
  11. // Repository: $HeadURL: file:///svn/p/refbase/code/branches/bleeding-edge/cite/formats/cite_rtf.php $
  12. // Author(s): Matthias Steffens <mailto:refbase@extracts.de>
  13. //
  14. // Created: 10-Jun-06, 02:04
  15. // Modified: $Date: 2017-04-13 02:00:18 +0000 (Thu, 13 Apr 2017) $
  16. // $Author: karnesky $
  17. // $Revision: 1416 $
  18. // This is a citation format file (which must reside within the 'cite/formats/' sub-directory of your refbase root directory). It contains a
  19. // version of the 'citeRecords()' function that outputs a reference list from selected records in RTF format.
  20. // RTF 1.0 format specification is available at <http://latex2rtf.sourceforge.net/RTF-Spec-1.0.txt>, more info at <http://en.wikipedia.org/wiki/RTF>
  21. // --------------------------------------------------------------------
  22. // Include the MINIMALRTF Package
  23. require_once 'includes/classes/org/bibliophile/MINIMALRTF.php';
  24. // --- BEGIN CITATION FORMAT ---
  25. // Requires the MINIMALRTF Package (by Mark Grimshaw), which is available
  26. // under the GPL from: <http://bibliophile.sourceforge.net>
  27. function citeRecords($result, $rowsFound, $query, $queryURL, $showQuery, $showLinks, $rowOffset, $showRows, $previousOffset, $nextOffset, $wrapResults, $citeStyle, $citeOrder, $citeType, $orderBy, $headerMsg, $userID, $viewType)
  28. {
  29. global $contentTypeCharset; // defined in 'ini.inc.php'
  30. global $client;
  31. // The array '$transtab_refbase_rtf' contains search & replace patterns for conversion from refbase markup to RTF markup & entities
  32. global $transtab_refbase_rtf; // defined in 'transtab_refbase_rtf.inc.php'
  33. // Initialize array variables:
  34. $yearsArray = array();
  35. $typeTitlesArray = array();
  36. // Define inline text markup to be used by the 'citeRecord()' function:
  37. $markupPatternsArray = array("bold-prefix" => "{\\b ",
  38. "bold-suffix" => "}",
  39. "italic-prefix" => "{\\i ",
  40. "italic-suffix" => "}",
  41. "underline-prefix" => "{\\ul ",
  42. "underline-suffix" => "}",
  43. "endash" => "\\endash ", // or use "\\uc0\\u8211 " or "\\u8211\\'2D" (the first two seem to work more reliably than the third one)
  44. "emdash" => "\\emdash ", // or use "\\uc0\\u8212 " or "\\u8212\\'2D"
  45. "ampersand" => "&",
  46. "double-quote" => '"',
  47. "double-quote-left" => "\\ldblquote ", // or use "\\uc0\\u8220 " or "\\u8220\\'22"
  48. "double-quote-right" => "\\rdblquote ", // or use "\\uc0\\u8221 " or "\\u8221\\'22"
  49. "single-quote" => "'",
  50. "single-quote-left" => "\\lquote ", // or use "\\uc0\\u8216 " or "\\u8216\\'27"
  51. "single-quote-right" => "\\rquote ", // or use "\\uc0\\u8217 " or "\\u8217\\'27"
  52. "less-than" => "<",
  53. "greater-than" => ">",
  54. "newline" => "\n{\\f1\\fs24 \par}\n"
  55. );
  56. // Defines search & replace 'actions' that will be applied upon RTF output to all those refbase fields that are listed
  57. // in the corresponding 'fields' element:
  58. $rtfSearchReplaceActionsArray = array(
  59. array(
  60. 'fields' => array("title", "publication", "abbrev_journal", "address", "keywords", "abstract", "orig_title", "series_title", "abbrev_series_title", "notes"),
  61. 'actions' => $transtab_refbase_rtf
  62. )
  63. );
  64. // For CLI queries, we'll allow paging thru the result set, i.e. we honour the values of the CLI options '-S|--start' ('$rowOffset')
  65. // and '-R|--rows' ('$showRows') ('$rowOffset' and '$showRows' are re-assigned in function 'seekInMySQLResultsToOffset()' in 'include.inc.php')
  66. if (preg_match("/^cli/i", $client)) // if the query originated from a command line client such as the "refbase" CLI client ("cli-refbase-1.0")
  67. $showMaxRows = $showRows; // show only rows up to the value given in '$showRows'
  68. else
  69. $showMaxRows = $rowsFound; // otherwise show all rows
  70. // Setup the basic RTF document structure (RTF functions defined in 'MINIMALRTF.php'):
  71. $rtf = new MINIMALRTF(); // initialize RTF object
  72. $rtfData = $rtf->openRtf(); // create RTF opening tag
  73. $rtf->createFontBlock(0, "Arial"); // create & set RTF font blocks
  74. $rtf->createFontBlock(1, "Times New Roman");
  75. $rtfData .= $rtf->setFontBlock();
  76. // Header:
  77. if (!empty($headerMsg))
  78. {
  79. // Remove any colon (":") from end of header message:
  80. $headerMsg = trimTextPattern($headerMsg, ":", false, true); // function 'trimTextPattern()' is defined in 'include.inc.php'
  81. // Decode any HTML entities:
  82. // (these may occur in the header message e.g. if the user's preferred display language is not English but German or French, etc)
  83. $headerMsg = decodeHTML($contentTypeCharset, $headerMsg); // function 'decodeHTML()' is defined in 'include.inc.php', and '$contentTypeCharset' is defined in 'ini.inc.php'
  84. // Convert refbase markup in the header message into appropriate RTF markup & entities:
  85. $headerMsg = searchReplaceText($transtab_refbase_rtf, $headerMsg, true); // function 'searchReplaceText()' is defined in 'include.inc.php'
  86. $rtfData .= "{\header\pard\qc $headerMsg\par}\n";
  87. }
  88. $rtfData .= $rtf->justify("full", 0.5, 0, -0.5); // by default, we'll justify text and set a hanging indent (left indent: 0.5, right indent: 0, first-line indent: -0.5)
  89. // LOOP OVER EACH RECORD:
  90. // Fetch one page of results (or less if on the last page)
  91. // (i.e., upto the limit specified in $showMaxRows) fetch a row into the $row array and ...
  92. for ($rowCounter=0; (($rowCounter < $showMaxRows) && ($row = @ mysqli_fetch_array($result))); $rowCounter++)
  93. {
  94. foreach ($row as $rowFieldName => $rowFieldValue)
  95. // Apply search & replace 'actions' to all fields that are listed in the 'fields' element of the arrays contained in '$rtfSearchReplaceActionsArray':
  96. foreach ($rtfSearchReplaceActionsArray as $fieldActionsArray)
  97. if (in_array($rowFieldName, $fieldActionsArray['fields']))
  98. $row[$rowFieldName] = searchReplaceText($fieldActionsArray['actions'], $row[$rowFieldName], true); // function 'searchReplaceText()' is defined in 'include.inc.php'
  99. // Order attributes according to the chosen output style & record type:
  100. $record = citeRecord($row, $citeStyle, $citeType, $markupPatternsArray, false); // function 'citeRecord()' is defined in the citation style file given in '$citeStyleFile' (which, in turn, must reside in the 'cite' directory of the refbase root directory), see function 'generateCitations()'
  101. // Print out the current record:
  102. if (!empty($record)) // unless the record buffer is empty...
  103. {
  104. // Print any section heading(s):
  105. if (preg_match("/year|type/i", $citeOrder))
  106. {
  107. $headingPrefix = $rtf->justify("left", 0, 0, 0) // left-align the current heading without any indents
  108. . $rtf->paragraph(0, 12); // create empty paragraph in front of heading using "Arial" (font block 0) and a font size of 12pt
  109. $headingSuffix = $rtf->justify("full", 0.5, 0, -0.5); // justify any following text and set a hanging indent (left indent: 0.5, right indent: 0, first-line indent: -0.5)
  110. if ($citeOrder == "type") // for 'citeOrder=type' we'll always print an empty paragraph after the heading
  111. $headingSuffix .= $rtf->paragraph(0, 12); // create empty paragraph using "Arial" (font block 0) and a font size of 12pt
  112. list($yearsArray, $typeTitlesArray, $sectionHeading) = generateSectionHeading($yearsArray, $typeTitlesArray, $row, $citeOrder, $headingPrefix, $headingSuffix, "{\\f0\\fs28 {\b ", "}\par}\n", "{\\f0\\fs24 {\b ", "}\par}\n"); // function 'generateSectionHeading()' is defined in 'cite.inc.php'
  113. // Note that we pass raw RTF commands to the above function instead of using the 'textBlock()' function from 'MINIMALRTF.php'. This is due to a current limitation of the 'generateSectionHeading()' function.
  114. // For 'citeOrder=year', the appropriate call to the 'textBlock()' function would look like this:
  115. // $rtfData .= $rtf->textBlock(0, 14, $rtf->bold($row['year'])); // create major heading with the current year using "Arial" (font block 0) and a font size of 14pt, printed in bold
  116. $rtfData .= $sectionHeading;
  117. }
  118. // If character encoding is not UTF-8 already, convert record text to UTF-8:
  119. if ($contentTypeCharset != "UTF-8")
  120. $record = convertToCharacterEncoding("UTF-8", "IGNORE", $record); // function 'convertToCharacterEncoding()' is defined in 'include.inc.php'
  121. // Encode characters with an ASCII value of >= 128 in RTF 1.16 unicode format:
  122. $recordUnicodeCharEncoded = $rtf->utf8_2_unicode($record); // converts UTF-8 chars to unicode character codes
  123. // Write RTF paragraph:
  124. $rtfData .= $rtf->textBlock(1, 12, $recordUnicodeCharEncoded); // create text block with encoded record text using "Times New Roman" (font block 1) and a font size of 12pt
  125. }
  126. }
  127. $rtfData .= $rtf->closeRtf(); // create RTF closing tag
  128. return $rtfData;
  129. }
  130. // --- END CITATION FORMAT ---
  131. ?>