X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FModule%2FBaseSearchModule.php;h=ed39f071ca61c92c898aa1a702ccd638f04cfbf2;hb=4d8ca5c9c212153c4f892d7b67a2e52a741d7f4d;hp=d11f7e98609728a867ad6c3d55a4b2ab2bd40284;hpb=5b44fdc2021e09a8e29081d46e5707d25a25a189;p=friendica.git diff --git a/src/Module/BaseSearchModule.php b/src/Module/BaseSearchModule.php index d11f7e9860..ed39f071ca 100644 --- a/src/Module/BaseSearchModule.php +++ b/src/Module/BaseSearchModule.php @@ -7,15 +7,74 @@ use Friendica\Content\ContactSelector; use Friendica\Content\Pager; use Friendica\Core\L10n; use Friendica\Core\Renderer; +use Friendica\Core\Search; +use Friendica\Model; +use Friendica\Network\HTTPException; +use Friendica\Object\Search\ContactResult; use Friendica\Object\Search\ResultList; use Friendica\Util\Proxy as ProxyUtils; -use Friendica\Model; +use Friendica\Util\Strings; /** * Base class for search modules */ -abstract class BaseSearchModule extends BaseModule +class BaseSearchModule extends BaseModule { + /** + * Performs a search with an optional prefix + * + * @param string $prefix A optional prefix (e.g. @ or !) for searching + * + * @return string + * @throws HTTPException\InternalServerErrorException + * @throws \ImagickException + */ + public static function performSearch($prefix = '') + { + $a = self::getApp(); + $config = $a->getConfig(); + + $type = Search::TYPE_ALL; + + $localSearch = $config->get('system', 'poco_local_search'); + + $search = $prefix . Strings::escapeTags(trim(defaults($_REQUEST, 'search', ''))); + + if (!$search) { + return ''; + } + + $header = ''; + + if (strpos($search, '@') === 0) { + $search = substr($search, 1); + $type = Search::TYPE_PEOPLE; + $header = L10n::t('People Search - %s', $search); + + if (strrpos($search, '@') > 0) { + $results = Search::getContactsFromProbe($search); + } + } + + if (strpos($search, '!') === 0) { + $search = substr($search, 1); + $type = Search::TYPE_FORUM; + $header = L10n::t('Forum Search - %s', $search); + } + + $pager = new Pager($a->query_string); + + if ($localSearch && empty($results)) { + $pager->setItemsPerPage(80); + $results = Search::getContactsFromLocalDirectory($search, $type, $pager->getStart(), $pager->getItemsPerPage()); + } elseif (strlen($config->get('system', 'directory')) && empty($results)) { + $results = Search::getContactsFromGlobalDirectory($search, $type, $pager->getPage()); + $pager->setItemsPerPage($results->getItemsPage()); + } + + return self::printResult($results, $pager, $header); + } + /** * Prints a human readable search result * @@ -24,74 +83,78 @@ abstract class BaseSearchModule extends BaseModule * @param string $header * * @return string The result - * @throws \Friendica\Network\HTTPException\InternalServerErrorException + * @throws HTTPException\InternalServerErrorException * @throws \ImagickException */ protected static function printResult(ResultList $results, Pager $pager, $header = '') { - if (empty($results) || empty($results->getResults())) { - info(L10n::t('No matches') . EOL); + if ($results->getTotal() == 0) { + info(L10n::t('No matches')); return ''; } $a = self::getApp(); - $id = 0; + $id = 0; $entries = []; foreach ($results->getResults() as $result) { - $alt_text = ''; - $location = ''; - $about = ''; - $accountType = ''; - $photo_menu = []; - - // If We already know this contact then don't show the "connect" button - if ($result->getCid() > 0 || $result->getPcid() > 0) { - $connLink = ""; - $connTxt = ""; - $contact = Model\Contact::getById( - ($result->getCid() > 0) ? $result->getCid() : $result->getPcid() - ); - - if (!empty($contact)) { - $photo_menu = Model\Contact::photoMenu($contact); - $details = Contact::getContactTemplateVars($contact); - $alt_text = $details['alt_text']; - $location = $contact['location']; - $about = $contact['about']; - $accountType = Model\Contact::getAccountType($contact); + // in case the result is a contact result, add a contact-specific entry + if ($result instanceof ContactResult) { + + $alt_text = ''; + $location = ''; + $about = ''; + $accountType = ''; + $photo_menu = []; + + // If We already know this contact then don't show the "connect" button + if ($result->getCid() > 0 || $result->getPCid() > 0) { + $connLink = ""; + $connTxt = ""; + $contact = Model\Contact::getById( + ($result->getCid() > 0) ? $result->getCid() : $result->getPCid() + ); + + if (!empty($contact)) { + $photo_menu = Model\Contact::photoMenu($contact); + $details = Contact::getContactTemplateVars($contact); + $alt_text = $details['alt_text']; + $location = $contact['location']; + $about = $contact['about']; + $accountType = Model\Contact::getAccountType($contact); + } else { + $photo_menu = []; + } } else { - $photo_menu = []; + $connLink = $a->getBaseURL() . '/follow/?url=' . $result->getUrl(); + $connTxt = L10n::t('Connect'); + + $photo_menu['profile'] = [L10n::t("View Profile"), Model\Contact::magicLink($result->getUrl())]; + $photo_menu['follow'] = [L10n::t("Connect/Follow"), $connLink]; } - } else { - $connLink = $a->getBaseURL() . '/follow/?url=' . $result->getUrl(); - $connTxt = L10n::t('Connect'); - $photo_menu['profile'] = [L10n::t("View Profile"), Model\Contact::magicLink($result->getUrl())]; - $photo_menu['follow'] = [L10n::t("Connect/Follow"), $connLink]; - } + $photo = str_replace("http:///photo/", get_server() . "/photo/", $result->getPhoto()); - $photo = str_replace("http:///photo/", get_server() . "/photo/", $result->getPhoto()); - - $entry = [ - 'alt_text' => $alt_text, - 'url' => Model\Contact::magicLink($result->getUrl()), - 'itemurl' => $result->getItem(), - 'name' => $result->getName(), - 'thumb' => ProxyUtils::proxifyUrl($photo, false, ProxyUtils::SIZE_THUMB), - 'img_hover' => $result->getTags(), - 'conntxt' => $connTxt, - 'connlnk' => $connLink, - 'photo_menu' => $photo_menu, - 'details' => $location, - 'tags' => $result->getTags(), - 'about' => $about, - 'account_type' => $accountType, - 'network' => ContactSelector::networkToName($result->getNetwork(), $result->getUrl()), - 'id' => ++$id, - ]; - $entries[] = $entry; + $entry = [ + 'alt_text' => $alt_text, + 'url' => Model\Contact::magicLink($result->getUrl()), + 'itemurl' => $result->getItem(), + 'name' => $result->getName(), + 'thumb' => ProxyUtils::proxifyUrl($photo, false, ProxyUtils::SIZE_THUMB), + 'img_hover' => $result->getTags(), + 'conntxt' => $connTxt, + 'connlnk' => $connLink, + 'photo_menu' => $photo_menu, + 'details' => $location, + 'tags' => $result->getTags(), + 'about' => $about, + 'account_type' => $accountType, + 'network' => ContactSelector::networkToName($result->getNetwork(), $result->getUrl()), + 'id' => ++$id, + ]; + $entries[] = $entry; + } } $tpl = Renderer::getMarkupTemplate('viewcontact_template.tpl');