/search.php

https://bitbucket.org/welz01/webgroup · PHP · 174 lines · 144 code · 30 blank · 0 comment · 12 complexity · 073f97695d8021b68012eba2f8b1c6b6 MD5 · raw file

  1. <?php
  2. include('_common.php');
  3. class SearchResultData extends Entity {
  4. function __construct() {
  5. parent::__construct();
  6. $this->defineField('Title');
  7. $this->defineField('Users');
  8. $this->defineField('Categories');
  9. $this->defineField('Images');
  10. $this->defineGetter('HasUsers', 'hasUsers');
  11. $this->defineGetter('HasCategories', 'hasCategories');
  12. $this->defineGetter('HasImages', 'hasImages');
  13. }
  14. function hasUsers() {
  15. return count($this->Users) > 0;
  16. }
  17. function hasCategories() {
  18. return count($this->Categories) > 0;
  19. }
  20. function hasImages() {
  21. return count($this->Images) > 0;
  22. }
  23. }
  24. class UserSearchResultData extends Entity {
  25. function __construct($user = null) {
  26. parent::__construct();
  27. $this->defineField('ProfileLink');
  28. $this->defineField('Name');
  29. if ($user != null) {
  30. $this->Name = htmlspecialchars($user->Name);
  31. $this->ProfileLink = 'profile.php?id='.$user->Id;
  32. }
  33. }
  34. }
  35. class CategorySearchResultData extends Entity {
  36. function __construct($category = null) {
  37. parent::__construct();
  38. $this->defineField('Name');
  39. $this->defineField('Link');
  40. if ($category != null) {
  41. $this->Name = htmlspecialchars($category->Name);
  42. $this->Link = 'bycategory.php?id='.$category->Id;
  43. }
  44. }
  45. }
  46. class ImageSearchResultData extends Entity {
  47. function __construct($image = null) {
  48. parent::__construct();
  49. $this->defineField('Title');
  50. $this->defineField('PictureLink');
  51. $this->defineField('Link');
  52. if ($image != null) {
  53. $this->Title = htmlspecialchars($image->Title);
  54. $this->Link = 'image.php?id='.$image->Id;
  55. $this->PictureLink = 'picture.php?id='.$image->PictureId;
  56. }
  57. }
  58. }
  59. function mapUser($user) {
  60. return new UserSearchResultData($user);
  61. }
  62. function mapCategory($category) {
  63. return new CategorySearchResultData($category);
  64. }
  65. function mapImage($image) {
  66. return new ImageSearchResultData($image);
  67. }
  68. class Search extends Template {
  69. private $data;
  70. function __construct() {
  71. parent::__construct();
  72. $searchText = $this->context->getParameter('searchText', NULL);
  73. logging('Searched for '.$searchText);
  74. $this->data = new SearchResultData();
  75. $this->data->Title = 'Search results: \''.htmlspecialchars($searchText).'\'';
  76. $searchText = '%'.$searchText.'%';
  77. $this->data->Users = $this->searchUsers($searchText);
  78. $this->data->Categories = $this->searchCategories($searchText);
  79. $this->data->Images = $this->searchImages($searchText);
  80. $this->setTitle($this->data->Title);
  81. }
  82. function searchUsers($term) {
  83. $users = $this->context->repository->findUsers($term);
  84. return array_map('mapUser', $users);
  85. }
  86. function searchCategories($term) {
  87. $users = $this->context->repository->findCategories($term);
  88. return array_map('mapCategory', $users);
  89. }
  90. function searchImages($term) {
  91. $images = $this->context->repository->findImages($term);
  92. return array_map('mapImage', $images);
  93. }
  94. function help() {
  95. ?>
  96. <p>
  97. This page displays the search results.
  98. </p>
  99. <?php
  100. }
  101. function content() { ?>
  102. <h2>Users</h2>
  103. <?php if ($this->data->HasUsers) { ?>
  104. <ul>
  105. <?php foreach ($this->data->Users as $user) { ?>
  106. <li><a href="<?=$user->ProfileLink ?>"><?=$user->Name ?></a></li>
  107. <?php } ?>
  108. </ul>
  109. <?php }
  110. else { ?>
  111. <p>No users found</p>
  112. <?php } ?>
  113. <h2>Categories</h2>
  114. <?php if ($this->data->HasCategories) { ?>
  115. <ul>
  116. <?php foreach ($this->data->Categories as $category) { ?>
  117. <li><a href="<?=$category->Link ?>"><?=$category->Name ?></a></li>
  118. <?php } ?>
  119. </ul>
  120. <?php }
  121. else { ?>
  122. <p>No categories found</p>
  123. <?php } ?>
  124. <h2>Images</h2>
  125. <?php if ($this->data->HasImages) { ?>
  126. <ul id="photos">
  127. <?php foreach ($this->data->Images as $image) { ?>
  128. <li>
  129. <a href="<?=$image->Link ?>">
  130. <img class="thumbnailImage" alt="preview" src="<?=$image->PictureLink ?>" />
  131. <span class="imageTitle"><?=$image->Title ?></span>
  132. </a>
  133. </li>
  134. <?php } ?>
  135. </ul>
  136. <?php }
  137. else { ?>
  138. <p>No images found</p>
  139. <?php }
  140. }
  141. }
  142. $page = new Search();
  143. $page->build();
  144. ?>