1<?php
2/**********************************************************************************
3WIKINDX: Bibliographic Management system.
4Copyright (C)
5
6This program is free software; you can redistribute it and/or modify it under the terms
7of the GNU General Public License as published by the Free Software Foundation; either
8version 2 of the License, or (at your option) any later version.
9
10This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12See the GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License along with this program;
15if not, write to the
16Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18The WIKINDX Team 2004
19sirfragalot@users.sourceforge.net
20**********************************************************************************/
21/*****
22*	BIBLIOGRAPHY STYLE class
23*	Format a resource for a bibliographic style.
24*
25*	$Header: /cvsroot/bibliophile/OSBib/format/BIBSTYLE.php,v 1.1 2005/06/20 22:26:51 sirfragalot Exp $
26*****/
27class BIBSTYLE
28{
29// Constructor
30	function BIBSTYLE($db, $output)
31	{
32		$this->db = $db;
33		include_once("core/session/SESSION.php");
34		$this->session = new SESSION();
35		include_once("core/styles/BIBFORMAT.php");
36		$this->bibformat = new BIBFORMAT("core/styles/");
37/**
38* CSS class for highlighting search terms
39*/
40		$this->bibformat->patternHighlight = "highlight";
41		include_once("core/html/MISC.php");
42// get the bibliographic style
43		if($output == 'rtf')
44			$this->setupStyle = $this->session->getVar("exportRtf_style");
45		else
46			$this->setupStyle = $this->session->getVar("setup_style");
47/**
48* If our style arrays do not exist in session, parse the style file and write to session.  Loading and
49* parsing the XML file takes about 0.1 second (P4 system) and so is a significant slowdown.
50* Try to do this only once every time we use a style.  NB.  These are saved in session with 'cite_' and 'style_'
51* prefixes - creating/copying or editing a bibliographic style clears these arrays from the session which will
52* force a reload of the style here.
53*/
54		$styleInfo = $this->session->getVar("style_name");
55		$styleCommon = unserialize(base64_decode($this->session->getVar("style_common")));
56		$styleTypes = unserialize(base64_decode($this->session->getVar("style_types")));
57// File not yet parsed or user's choice of style has changed so need to
58// load, parse and store to session
59		if((!$styleInfo || !$styleCommon || !$styleTypes)
60			|| ($styleInfo != $this->setupStyle))
61		{
62			list($info, $citation, $styleCommon, $styleTypes) =
63				$this->bibformat->loadStyle("styles/bibliography/", $this->setupStyle);
64			$this->session->setVar("style_name", $info['name']);
65			$this->session->setVar("cite_citation", base64_encode(serialize($citation)));
66			$this->session->setVar("style_common", base64_encode(serialize($styleCommon)));
67			$this->session->setVar("style_types", base64_encode(serialize($styleTypes)));
68			$this->session->delVar("style_edited");
69		}
70		$this->bibformat->getStyle($styleCommon, $styleTypes);
71		$this->output = $output;
72		$this->bibformat->output = $output;
73	}
74// Accept a SQL result row of raw bibliographic data and process it.
75// We build up the $bibformat->item array with formatted parts from the raw $row
76	function process($row)
77	{
78		$this->row = $row;
79		$type = $row['type']; // WIKINDX type
80// For WIKINDX, if type == book or book article and there exists both 'year1' and 'year2' in $row (entered as
81// publication year and reprint year respectively), then switch these around as 'year1' is
82// entered in the style template as 'originalPublicationYear' and 'year2' should be 'publicationYear'.
83		if(($type == 'book') || ($type == 'book_article'))
84		{
85			$year2 = stripslashes($this->row['year2']);
86			if($year2 && !$this->row['year1'])
87			{
88				$this->row['year1'] = $year2;
89				unset($this->row['year2']);
90			}
91			else if($year2 && $this->row['year1'])
92			{
93				$this->row['year2'] = stripslashes($this->row['year1']);
94				$this->row['year1'] = $year2;
95			}
96		}
97		$this->row = $this->bibformat->preProcess($type, $this->row);
98// Return $type is the OSBib resource type ($this->book, $this->web_article etc.) as used in STYLEMAP
99		$type = $this->bibformat->type;
100// Various types of creator
101		for($index = 1; $index <= 5; $index++)
102		{
103			if(!$this->row['creator' . $index] ||
104				!array_key_exists('creator' . $index, $this->bibformat->styleMap->$type))
105				continue;
106			if(array_key_exists('creator' . $index, $this->bibformat->styleMap->$type))
107				$this->grabNames('creator' . $index);
108		}
109// The title of the resource
110		$this->createTitle();
111// edition
112		if($editionKey = array_search('edition', $this->bibformat->styleMap->$type))
113			$this->createEdition($editionKey);
114// pageStart and pageEnd
115		$this->pages = FALSE; // indicates not yet created pages for articles
116		if(array_key_exists('pages', $this->bibformat->styleMap->$type))
117			$this->createPages();
118// Date
119		if(array_key_exists('date', $this->bibformat->styleMap->$type))
120			$this->createDate();
121// runningTime for film/broadcast
122		if(array_key_exists('runningTime', $this->bibformat->styleMap->$type))
123			$this->createRunningTime();
124// web_article URL
125		if(array_key_exists('URL', $this->bibformat->styleMap->$type) &&
126			($itemElement = $this->createUrl()))
127			$this->bibformat->addItem($itemElement, 'URL', FALSE);
128// the rest...  All other database resource fields that do not require special formatting/conversion.
129		$this->bibformat->addAllOtherItems($this->row);
130// We now have an array for this item where the keys match the key names of $this->styleMap->$type
131// where $type is book, journal_article, thesis etc. and are now ready to map this against the defined
132// bibliographic style for each resource ($this->book, $this->book_article etc.).
133// This bibliographic style array not only provides the formatting and punctuation for each field but also
134// provides the order. If a field name does not exist in this style array, we print nothing.
135		$pString = $this->bibformat->map();
136// ordinals such as 5$^{th}$
137		$pString = preg_replace_callback("/(\d+)\\$\^\{(.*)\}\\$/", array($this, "ordinals"), $pString);
138// remove extraneous {...}
139		return preg_replace("/{(.*)}/U", "$1", $pString);
140	}
141// callback for ordinals above
142	function ordinals($matches)
143	{
144		if($this->output == 'html')
145			return $matches[1] . "<sup>" . $matches[2] . "</sup>";
146		else if($this->output == 'rtf')
147			return $matches[1] . "{{\up5 " . $matches[2] . "}}";
148		else
149			return $matches[1] . $matches[2];
150	}
151// Create the resource title
152	function createTitle()
153	{
154		$pString = stripslashes($this->row['noSort']) . ' ' .
155			stripslashes($this->row['title']);
156		if($this->row['subtitle'])
157			$pString .= ': ' . stripslashes($this->row['subtitle']);
158// anything enclosed in {...} is to be left as is
159		$this->bibformat->formatTitle($pString, "{", "}");
160	}
161// Create the URL
162	function createUrl()
163	{
164		if(!$this->row['url'])
165			return FALSE;
166		$url = ($this->output == 'html') ? htmlspecialchars(stripslashes($this->row['url'])) :
167			stripslashes($this->row['url']);
168		unset($this->row['url']);
169		if($this->output == 'html')
170			return MISC::a('rLink', $url, $url, "_blank");
171		else
172			return $url;
173	}
174// Create date
175	function createDate()
176	{
177		$startDay = isset($this->row['miscField2']) ? stripslashes($this->row['miscField2']) : FALSE;
178		$startMonth = isset($this->row['miscField3']) ? stripslashes($this->row['miscField3']) : FALSE;
179		unset($this->row['miscField2']);
180		unset($this->row['miscField3']);
181		$endDay = isset($this->row['miscField5']) ? stripslashes($this->row['miscField5']) : FALSE;
182		$endMonth = isset($this->row['miscField6']) ? stripslashes($this->row['miscField6']) : FALSE;
183		unset($this->row['miscField5']);
184		unset($this->row['miscField6']);
185		$startDay = ($startDay == 0) ? FALSE : $startDay;
186		$startMonth = ($startMonth == 0) ? FALSE : $startMonth;
187		if(!$startMonth)
188			return;
189		$endDay = ($endDay == 0) ? FALSE : $endDay;
190		$endMonth = ($endMonth == 0) ? FALSE : $endMonth;
191		$this->bibformat->formatDate($startDay, $startMonth, $endDay, $endMonth);
192	}
193// Create runningTime for film/broadcast
194	function createRunningTime()
195	{
196		$minutes = stripslashes($this->row['miscField1']);
197		$hours = stripslashes($this->row['miscField4']);
198		if(!$hours && !$minutes)
199			return;
200		if(!$hours)
201			$hours = 0;
202		$this->bibformat->formatRunningTime($minutes, $hours);
203	}
204// Create the edition number
205	function createEdition($editionKey)
206	{
207		if(!$this->row[$editionKey])
208			return FALSE;
209		$edition = stripslashes($this->row[$editionKey]);
210		$this->bibformat->formatEdition($edition);
211	}
212// Create page start and page end
213	function createPages()
214	{
215		if(!$this->row['pageStart'] || $this->pages) // empty field or page format already done
216		{
217			$this->pages = TRUE;
218			return;
219		}
220		$this->pages = TRUE;
221		$start = trim(stripslashes($this->row['pageStart']));
222		$end = $this->row['pageEnd'] ? trim(stripslashes($this->row['pageEnd'])) : FALSE;
223		$this->bibformat->formatPages($start, $end);
224	}
225// get names from database for creator, editor, translator etc.
226	function grabNames($nameType)
227	{
228		$recordset = $this->db->select(array("WKX_creator"), array("surname", "firstname",
229			"initials", "prefix", "id"),
230			" WHERE FIND_IN_SET(" . $this->db->formatField("id") . ", " .
231			$this->db->tidyInput($this->row[$nameType]) . ")");
232		$numNames = $this->db->numRows($recordset);
233		$nameArray = array("surname", "firstname", "initials", "prefix");
234// Reorder $row so that creator order is correct and not that returned by SQL
235		$ids = explode(",", $this->row[$nameType]);
236		while($row = $this->db->loopRecordSet($recordset))
237			$rowSql[$row['id']] = $row;
238		if(!isset($rowSql))
239			return FALSE;
240		foreach($ids as $id)
241			$rowTemp[] = $rowSql[$id];
242		$this->bibformat->formatNames($rowTemp, $nameType);
243	}
244// bad Input function
245	function badInput($error)
246	{
247		include_once("core/html/CLOSE.php");
248		new CLOSE($this->db, $error);
249	}
250}
251?>
252