PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/pages/DetailsPage.php

http://manx.codeplex.com
PHP | 398 lines | 367 code | 27 blank | 4 comment | 49 complexity | 9c5fbaa5b25802270594a9cf0d316829 MD5 | raw file
  1. <?php
  2. require_once 'PageBase.php';
  3. class DetailsPage extends PageBase
  4. {
  5. private $_details;
  6. public function __construct(IManx $manx)
  7. {
  8. parent::__construct($manx);
  9. $this->_details = $this->getDetailsForPathInfo($_SERVER['PATH_INFO']);
  10. }
  11. protected function getTitle()
  12. {
  13. return $this->_details[1]['ph_title'];
  14. }
  15. private static function formatPubDate($pubDate)
  16. {
  17. $pubDate = DetailsPage::replaceNullWithEmptyStringOrTrim($pubDate);
  18. if (strlen($pubDate) > 0)
  19. {
  20. return ' (' . htmlspecialchars($pubDate) . ')';
  21. }
  22. return '';
  23. }
  24. public static function replaceNullWithEmptyStringOrTrim($value)
  25. {
  26. return is_null($value) ? '' : trim($value);
  27. }
  28. public static function formatDocRef($row)
  29. {
  30. $out = sprintf('<a href="../details.php/%d,%d"><cite>%s</cite></a>', $row['ph_company'], $row['ph_pub'], htmlspecialchars($row['ph_title']));
  31. return DetailsPage::partPrefix($row['ph_part']) . $out;
  32. }
  33. private static function partPrefix($part)
  34. {
  35. $part = DetailsPage::replaceNullWithEmptyStringOrTrim($part);
  36. if (strlen($part) > 0)
  37. {
  38. return htmlspecialchars($part) . ', ';
  39. }
  40. return '';
  41. }
  42. protected function renderBodyContent()
  43. {
  44. $params = $this->_details[0];
  45. $row = $this->_details[1];
  46. $coverImage = $row['ph_cover_image'];
  47. if (!is_null($coverImage))
  48. {
  49. printf('<div style="float:right; margin: 10px"><img src="%s" alt="" /></div>', $coverImage);
  50. }
  51. echo '<div class="det"><h1>', $row['ph_title'], "</h1>\n";
  52. echo '<table><tbody>';
  53. $this->printTableRowNoEncode('Company',
  54. sprintf('<a href="../search.php?cp=%d&q=">%s</a>',
  55. $params['cp'],
  56. htmlspecialchars(trim($row['name']))
  57. ));
  58. $this->printTableRow('Part', $row['ph_part'] . ' ' . $row['ph_revision']);
  59. $this->printTableRowFromDatabaseRow($row, 'Date', 'ph_pub_date');
  60. $this->printTableRowFromDatabaseRow($row, 'Keywords', 'ph_keywords');
  61. $this->renderLanguage($row['ph_lang']);
  62. $pubId = $row['pub_id'];
  63. $this->renderAmendments($pubId);
  64. $this->renderOSTags($pubId);
  65. $this->renderLongDescription($pubId);
  66. $this->renderCitations($pubId);
  67. $this->renderSupersessions($pubId);
  68. $abstract = DetailsPage::replaceNullWithEmptyStringOrTrim($row['ph_abstract']);
  69. if (strlen($abstract) > 0)
  70. {
  71. $this->printTableRow('Text', $abstract);
  72. }
  73. echo "</tbody>\n</table>\n";
  74. $fullContents = array_key_exists('cn', $params) && ($params['cn'] == 1);
  75. $this->renderTableOfContents($pubId, $fullContents);
  76. $this->renderCopies($pubId);
  77. print "</div>\n";
  78. }
  79. private function printTableRowNoEncode($name, $value)
  80. {
  81. echo '<tr><td>', $name, ':</td><td>', $value, "</td></tr>\n";
  82. }
  83. private function printTableRow($name, $value)
  84. {
  85. $this->printTableRowNoEncode($name, htmlspecialchars(trim($value)));
  86. }
  87. private function printTableRowFromDatabaseRow($row, $name, $key)
  88. {
  89. $this->printTableRow($name, $row[$key]);
  90. }
  91. public function renderLanguage($lang)
  92. {
  93. if (!is_null($lang) && $lang != '+en')
  94. {
  95. $languages = array();
  96. foreach (array_slice(explode('+', $lang), 1) as $languageCode)
  97. {
  98. array_push($languages, $this->_manxDb->getDisplayLanguage($languageCode));
  99. }
  100. if (count($languages) > 0)
  101. {
  102. echo '<tr><td>Language', (count($languages) > 1) ? 's' : '', ':</td><td>', DetailsPage::neatListPlain($languages), "</td></tr>\n";
  103. }
  104. }
  105. }
  106. public function renderAmendments($pubId)
  107. {
  108. $amendments = array();
  109. foreach ($this->_manxDb->getAmendmentsForPub($pubId) as $row)
  110. {
  111. $amend = sprintf('<a href="../details.php/%d,%d"><cite>%s</cite></a>', $row['ph_company'], $row['ph_pub'], htmlspecialchars($row['ph_title']));
  112. $amend = DetailsPage::partPrefix($row['ph_part']) . $amend;
  113. $amend .= DetailsPage::formatPubDate($row['ph_pub_date']);
  114. $amend .= $this->renderOSTagsForPub($pubId);
  115. array_push($amendments, $amend);
  116. }
  117. if (count($amendments) > 0)
  118. {
  119. echo '<tr valign="top"><td>Amended&nbsp;by:</td><td><ul class="citelist"><li>', implode('</li><li>', $amendments), "</li></ul></td></tr>\n";
  120. }
  121. }
  122. private function renderOSTagsForPub($pubId)
  123. {
  124. $tags = $this->_manxDb->getOSTagsForPub($pubId);
  125. if (count($tags) > 0)
  126. {
  127. return ' <b>OS:</b> ' . htmlspecialchars(implode(', ', $tags));
  128. }
  129. return '';
  130. }
  131. public function renderOSTags($pubId)
  132. {
  133. $tags = $this->_manxDb->getOSTagsForPub($pubId);
  134. if (count($tags) > 0)
  135. {
  136. echo '<tr><td>Operating System:</td><td>', htmlspecialchars(implode(', ', $tags)), "</td></tr>\n";
  137. }
  138. }
  139. public function renderLongDescription($pubId)
  140. {
  141. $startedDesc = false;
  142. foreach ($this->_manxDb->getLongDescriptionForPub($pubId) as $html)
  143. {
  144. if (!$startedDesc)
  145. {
  146. echo '<tr valign="top"><td>Description:</td><td>';
  147. $startedDesc = true;
  148. }
  149. print $html;
  150. }
  151. if ($startedDesc)
  152. {
  153. echo '</td></tr>';
  154. }
  155. }
  156. public function renderCitations($pubId)
  157. {
  158. // Citations from other documents (only really important when there are no copies online)
  159. $citations = array();
  160. foreach ($this->_manxDb->getCitationsForPub($pubId) as $row)
  161. {
  162. array_push($citations, DetailsPage::formatDocRef($row));
  163. }
  164. if (count($citations) > 0)
  165. {
  166. echo '<tr valign="top"><td>Cited by:</td><td><ul class="citelist"><li>', implode('</li><li>', $citations), "</li></ul></td></tr>\n";
  167. }
  168. }
  169. public function renderSupersessions($pubId)
  170. {
  171. // Supersession information. Because documents can be merged in later revisions,
  172. // or expand to become more than one, there may be more than one document that
  173. // preceded or superseded this one.
  174. $supers = array();
  175. foreach ($this->_manxDb->getPublicationsSupersededByPub($pubId) as $pub)
  176. {
  177. array_push($supers, DetailsPage::formatDocRef($pub));
  178. }
  179. if (count($supers) > 0)
  180. {
  181. echo '<tr valign="top"><td>Supersedes:</td><td><ul class="citelist"><li>',
  182. implode('</li><li>', $supers), "</li></ul></td></tr>\n";
  183. }
  184. $supers = array();
  185. foreach ($this->_manxDb->getPublicationsSupersedingPub($pubId) as $pub)
  186. {
  187. array_push($supers, DetailsPage::formatDocRef($pub));
  188. }
  189. if (count($supers) > 0)
  190. {
  191. echo '<tr valign="top"><td>Superseded by:</td><td><ul class="citelist"><li>',
  192. implode('</li><li>', $supers), "</li></ul></td></tr>\n";
  193. }
  194. }
  195. public function renderTableOfContents($pubId, $fullContents)
  196. {
  197. $currentLevel = 0;
  198. $startedContents = false;
  199. foreach ($this->_manxDb->getTableOfContentsForPub($pubId, $fullContents) as $row)
  200. {
  201. if (!$startedContents)
  202. {
  203. print "<h2>Table of Contents</h2>\n";
  204. print '<div class="toc">';
  205. $startedContents = true;
  206. }
  207. $rowLevel = $row['level'];
  208. $rowLabel = $row['label'];
  209. $rowName = $row['name'];
  210. if ($rowLevel > $currentLevel)
  211. {
  212. ++$currentLevel;
  213. print "\n<ul>\n";
  214. }
  215. else if ($rowLevel < $currentLevel)
  216. {
  217. print "</li>\n";
  218. while ($rowLevel < $currentLevel)
  219. {
  220. print "</ul></li>\n";
  221. --$currentLevel;
  222. }
  223. }
  224. else
  225. {
  226. print "</li>\n";
  227. }
  228. if (is_null($rowLabel) && $currentLevel > 1)
  229. {
  230. $rowLabel = '&nbsp;';
  231. }
  232. printf('<li class="level%d"><span%s>%s</span> %s',
  233. $currentLevel, ($currentLevel == 1 ? ' class="level1"' : ''), $rowLabel, htmlspecialchars($rowName));
  234. }
  235. if ($startedContents)
  236. {
  237. while (0 < $currentLevel--)
  238. {
  239. print "</li>\n</ul>";
  240. }
  241. print '</div>';
  242. }
  243. }
  244. public function renderCopies($pubId)
  245. {
  246. print "<h2>Copies</h2>\n";
  247. $copyCount = 0;
  248. foreach ($this->_manxDb->getCopiesForPub($pubId) as $row)
  249. {
  250. if (++$copyCount == 1)
  251. {
  252. print "<table>\n<tbody>";
  253. }
  254. else
  255. {
  256. print "<tr>\n<td colspan=\"2\">&nbsp;</td>\n</tr>\n";
  257. }
  258. print "<tr>\n<td>Address:</td>\n<td>";
  259. $copyUrl = $row['url'];
  260. if (substr($copyUrl, 0, 1) == '+')
  261. {
  262. $copyUrl = $row['copy_base'] . substr($copyUrl, 1);
  263. }
  264. printf("<a href=\"%s\">%s</a></td>\n</tr>\n", $copyUrl, $copyUrl);
  265. printf("<tr>\n<td>Site:</td>\n<td><a href=\"%s\">%s</a>", htmlspecialchars($row['site_url']), htmlspecialchars($row['description']));
  266. if ($row['low'] != 'N')
  267. {
  268. print ' <span class="warning">(Low Bandwidth)</span>';
  269. }
  270. print "</td>\n</tr>\n";
  271. printf("<tr>\n<td>Format:</td>\n<td>%s</td>\n</tr>\n", htmlspecialchars($row['format']));
  272. $size = $row['size'];
  273. if ($size > 0)
  274. {
  275. printf("<tr>\n<td>Size:</td>\n<td>%d bytes", $size);
  276. $sizeMegabytes = $size/(1024*1024);
  277. $sizeKilobytes = $size/1024;
  278. if ($sizeMegabytes > 1.0)
  279. {
  280. printf(" (%.1f MiB)", $sizeMegabytes);
  281. }
  282. else if ($sizeKilobytes > 1.0)
  283. {
  284. printf(" (%.0f KiB)", $sizeKilobytes);
  285. }
  286. print "</td>\n</tr>\n";
  287. }
  288. $md5 = DetailsPage::replaceNullWithEmptyStringOrTrim($row['md5']);
  289. if (strlen($md5) > 0)
  290. {
  291. printf("<tr>\n<td>MD5:</td>\n<td>%s</td>\n</tr>\n", htmlspecialchars($md5));
  292. }
  293. $notes = DetailsPage::replaceNullWithEmptyStringOrTrim($row['notes']);
  294. if (strlen($notes) > 0)
  295. {
  296. printf("<tr>\n<td>Notes:</td>\n<td>%s</td>\n</tr>\n", htmlspecialchars($notes));
  297. }
  298. $credits = DetailsPage::replaceNullWithEmptyStringOrTrim($row['credits']);
  299. if (strlen($credits) > 0)
  300. {
  301. printf("<tr>\n<td>Credits:</td><td>%s</td>\n</tr>\n", htmlspecialchars($credits));
  302. }
  303. $amendSerial = $row['amend_serial'];
  304. if (!is_null($amendSerial))
  305. {
  306. $amendRow = $this->_manxDb->getAmendedPub($pubId, $amendSerial);
  307. $amend = sprintf("<a href=\"../details.php/%d,%d\"><cite>%s</cite></a>",
  308. $amendRow['ph_company'], $amendRow['pub_id'], htmlspecialchars($amendRow['ph_title']));
  309. $amend = DetailsPage::partPrefix($amendRow['ph_part']) . $amend;
  310. $amend .= DetailsPage::formatPubDate($amendRow['ph_pub_date']);
  311. $amend .= $this->renderOSTagsForPub($amendRow['pub_id']);
  312. printf("<tr>\n<td>Amended to:</td>\n<td>%s</td>\n</tr>\n", $amend);
  313. }
  314. $mirrorCount = 0;
  315. foreach ($this->_manxDb->getMirrorsForCopy($row['copy_id']) as $mirror)
  316. {
  317. if (++$mirrorCount == 1)
  318. {
  319. print '<tr valign="top"><td>Mirrors:</td><td><ul style="list-style-type: none; margin: 0; padding: 0">';
  320. }
  321. printf("<li style=\"margin: 0; padding: 0\"><a href=\"%s\">%s</a></li>", $mirror, htmlspecialchars($mirror));
  322. }
  323. if ($mirrorCount > 0)
  324. {
  325. print '</ul></td></tr>';
  326. }
  327. }
  328. if ($copyCount > 0)
  329. {
  330. print "</tbody>\n</table>\n";
  331. }
  332. else
  333. {
  334. print <<<EOH
  335. <p>No copies are known to be online. Feel
  336. free to <a href="http://manx.codeplex.com/WorkItem/Create">create a bug
  337. report</a> on our <a href="http://manx.codeplex.com">CodePlex project</a> if
  338. you know of an online copy of this publication.</p>
  339. EOH;
  340. }
  341. }
  342. public static function detailParamsForPathInfo($pathInfo)
  343. {
  344. $matches = array();
  345. $params = array();
  346. if (1 == preg_match_all('/^\\/(\\d+),(\\d+)$/', $pathInfo, $matches))
  347. {
  348. $params['cp'] = $matches[1][0];
  349. $params['id'] = $matches[2][0];
  350. $params['cn'] = 1;
  351. $params['pn'] = 0;
  352. }
  353. return $params;
  354. }
  355. private function getDetailsForPathInfo($pathInfo)
  356. {
  357. $params = DetailsPage::detailParamsForPathInfo($pathInfo);
  358. return array($params, $this->_manxDb->getDetailsForPub($params['id']));
  359. }
  360. public static function neatListPlain($values)
  361. {
  362. if (count($values) > 1)
  363. {
  364. return implode(', ', array_slice($values, 0, count($values) - 1)) . ' and ' . $values[count($values) - 1];
  365. }
  366. else
  367. {
  368. return $values[0];
  369. }
  370. }
  371. }