PageRenderTime 63ms CodeModel.GetById 36ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/atom/index.php

http://github.com/ThomasWeinert/FluentDOM
PHP | 97 lines | 87 code | 3 blank | 7 comment | 4 complexity | f6c933cfa14364b228027cfb8599daa9 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * @version $Id$
  5. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  6. * @copyright Copyright (c) 2009 Bastian Feder, Thomas Weinert
  7. */
  8. ?>
  9. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  10. <html>
  11. <head>
  12. <title>Simple Atom Reader - Sample Script</title>
  13. <meta http-equiv="content-type" content="text/html; charset=utf-8">
  14. <style type="text/css">
  15. body {
  16. font-family: sans-serif;
  17. background-color: #FFF;
  18. color: #000;
  19. }
  20. a {
  21. color: #060;
  22. }
  23. div.entry {
  24. clear: both;
  25. padding-top: 2em;
  26. }
  27. ul.categories li {
  28. float: left;
  29. margin-left: 20px;
  30. }
  31. </style>
  32. </head>
  33. <body>
  34. <?php
  35. require_once('../../src/FluentDOM.php');
  36. $dom = FluentDOM('./atom-sample.xml');
  37. $categories = array_unique($dom->find('//_:category')->map('callbackCategoryTerm'));
  38. if (count($categories) > 0) {
  39. echo '<ul class="categories">'."\n";
  40. foreach ($categories as $category) {
  41. printf(
  42. '<li><a href="?label=%s">%s</a></li>'."\n",
  43. urlencode($category),
  44. htmlspecialchars($category)
  45. );
  46. }
  47. echo '</ul>'."\n";
  48. }
  49. if (empty($_GET['label'])) {
  50. $expr = '//_:entry';
  51. } else {
  52. $expr = '//_:entry[_:category[@term = "'.htmlspecialchars($_GET['label']).'"]]';
  53. }
  54. foreach ($dom->find($expr) as $entryNode) {
  55. echo '<div class="entry">'."\n";
  56. $entry = FluentDOM($entryNode);
  57. printf(
  58. '<h2><a href="%s">%s</a></h2>'."\n",
  59. htmlspecialchars(
  60. $entry->find('.//_:link[@rel = "alternate" and @type = "text/html"]')->attr('href')
  61. ),
  62. htmlspecialchars($entry->find('.//_:title')->text())
  63. );
  64. $summary = $entry->find('.//_:summary|.//_:content');
  65. switch ($summary->attr('type')) {
  66. case 'html' :
  67. case 'text/html' :
  68. //in real world you whould need to use a purifier
  69. printf(
  70. '<div class="summary">%s</div>'."\n",
  71. $summary->text()
  72. );
  73. break;
  74. case 'text' :
  75. case 'text/plain' :
  76. case '' :
  77. printf(
  78. '<div class="summary">%s</div>'."\n",
  79. htmlspecialchars($summary->text())
  80. );
  81. break;
  82. default :
  83. break;
  84. }
  85. echo '</div>'."\n";
  86. }
  87. ?>
  88. </body>
  89. </html>
  90. <?php
  91. function callbackCategoryTerm($node) {
  92. return $node->getAttribute('term');
  93. }
  94. ?>