Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
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.

169 lines
4.8KB

  1. <?php
  2. namespace Friendica\Module;
  3. use Friendica\BaseModule;
  4. use Friendica\Content\ContactSelector;
  5. use Friendica\Content\Pager;
  6. use Friendica\Core\L10n;
  7. use Friendica\Core\Renderer;
  8. use Friendica\Core\Search;
  9. use Friendica\Model;
  10. use Friendica\Network\HTTPException;
  11. use Friendica\Object\Search\ContactResult;
  12. use Friendica\Object\Search\ResultList;
  13. use Friendica\Util\Proxy as ProxyUtils;
  14. use Friendica\Util\Strings;
  15. /**
  16. * Base class for search modules
  17. */
  18. class BaseSearchModule extends BaseModule
  19. {
  20. /**
  21. * Performs a search with an optional prefix
  22. *
  23. * @param string $search Search query
  24. * @param string $prefix A optional prefix (e.g. @ or !) for searching
  25. *
  26. * @return string
  27. * @throws HTTPException\InternalServerErrorException
  28. * @throws \ImagickException
  29. */
  30. public static function performSearch($search, $prefix = '')
  31. {
  32. $a = self::getApp();
  33. $config = $a->getConfig();
  34. $type = Search::TYPE_ALL;
  35. $localSearch = $config->get('system', 'poco_local_search');
  36. $search = $prefix . $search;
  37. if (!$search) {
  38. return '';
  39. }
  40. $header = '';
  41. if (strpos($search, '@') === 0) {
  42. $search = substr($search, 1);
  43. $type = Search::TYPE_PEOPLE;
  44. $header = L10n::t('People Search - %s', $search);
  45. if (strrpos($search, '@') > 0) {
  46. $results = Search::getContactsFromProbe($search);
  47. }
  48. }
  49. if (strpos($search, '!') === 0) {
  50. $search = substr($search, 1);
  51. $type = Search::TYPE_FORUM;
  52. $header = L10n::t('Forum Search - %s', $search);
  53. }
  54. $pager = new Pager(self::getArgs()->getQueryString());
  55. if ($localSearch && empty($results)) {
  56. $pager->setItemsPerPage(80);
  57. $results = Search::getContactsFromLocalDirectory($search, $type, $pager->getStart(), $pager->getItemsPerPage());
  58. } elseif (strlen($config->get('system', 'directory')) && empty($results)) {
  59. $results = Search::getContactsFromGlobalDirectory($search, $type, $pager->getPage());
  60. $pager->setItemsPerPage($results->getItemsPage());
  61. }
  62. return self::printResult($results, $pager, $header);
  63. }
  64. /**
  65. * Prints a human readable search result
  66. *
  67. * @param ResultList $results
  68. * @param Pager $pager
  69. * @param string $header
  70. *
  71. * @return string The result
  72. * @throws HTTPException\InternalServerErrorException
  73. * @throws \ImagickException
  74. */
  75. protected static function printResult(ResultList $results, Pager $pager, $header = '')
  76. {
  77. if ($results->getTotal() == 0) {
  78. info(L10n::t('No matches'));
  79. return '';
  80. }
  81. $a = self::getApp();
  82. $id = 0;
  83. $entries = [];
  84. foreach ($results->getResults() as $result) {
  85. // in case the result is a contact result, add a contact-specific entry
  86. if ($result instanceof ContactResult) {
  87. $alt_text = '';
  88. $location = '';
  89. $about = '';
  90. $accountType = '';
  91. $photo_menu = [];
  92. // If We already know this contact then don't show the "connect" button
  93. if ($result->getCid() > 0 || $result->getPCid() > 0) {
  94. $connLink = "";
  95. $connTxt = "";
  96. $contact = Model\Contact::getById(
  97. ($result->getCid() > 0) ? $result->getCid() : $result->getPCid()
  98. );
  99. if (!empty($contact)) {
  100. $photo_menu = Model\Contact::photoMenu($contact);
  101. $details = Contact::getContactTemplateVars($contact);
  102. $alt_text = $details['alt_text'];
  103. $location = $contact['location'];
  104. $about = $contact['about'];
  105. $accountType = Model\Contact::getAccountType($contact);
  106. } else {
  107. $photo_menu = [];
  108. }
  109. } else {
  110. $connLink = $a->getBaseURL() . '/follow/?url=' . $result->getUrl();
  111. $connTxt = L10n::t('Connect');
  112. $photo_menu['profile'] = [L10n::t("View Profile"), Model\Contact::magicLink($result->getUrl())];
  113. $photo_menu['follow'] = [L10n::t("Connect/Follow"), $connLink];
  114. }
  115. $photo = str_replace("http:///photo/", get_server() . "/photo/", $result->getPhoto());
  116. $entry = [
  117. 'alt_text' => $alt_text,
  118. 'url' => Model\Contact::magicLink($result->getUrl()),
  119. 'itemurl' => $result->getItem(),
  120. 'name' => $result->getName(),
  121. 'thumb' => ProxyUtils::proxifyUrl($photo, false, ProxyUtils::SIZE_THUMB),
  122. 'img_hover' => $result->getTags(),
  123. 'conntxt' => $connTxt,
  124. 'connlnk' => $connLink,
  125. 'photo_menu' => $photo_menu,
  126. 'details' => $location,
  127. 'tags' => $result->getTags(),
  128. 'about' => $about,
  129. 'account_type' => $accountType,
  130. 'network' => ContactSelector::networkToName($result->getNetwork(), $result->getUrl()),
  131. 'id' => ++$id,
  132. ];
  133. $entries[] = $entry;
  134. }
  135. }
  136. $tpl = Renderer::getMarkupTemplate('viewcontact_template.tpl');
  137. return Renderer::replaceMacros($tpl, [
  138. 'title' => $header,
  139. '$contacts' => $entries,
  140. '$paginate' => $pager->renderFull($results->getTotal()),
  141. ]);
  142. }
  143. }