]> git.mxchange.org Git - friendica.git/commitdiff
Split Directory Search as a base for further changes
authorPhilipp Holzer <admin+github@philipp.info>
Sun, 19 May 2019 03:01:46 +0000 (05:01 +0200)
committerPhilipp Holzer <admin+github@philipp.info>
Sun, 19 May 2019 03:01:46 +0000 (05:01 +0200)
mod/search.php
src/App/Router.php
src/Module/BaseSearchModule.php [new file with mode: 0644]
src/Module/DirectorySearch.php [deleted file]
src/Module/Search/Directory.php [new file with mode: 0644]

index 21b6918b1ced35a341d2c688b92b68faf8c94cbb..816ebf18a96a559e25548e46f7c9574a068993c6 100644 (file)
@@ -14,7 +14,7 @@ use Friendica\Core\Logger;
 use Friendica\Core\Renderer;
 use Friendica\Database\DBA;
 use Friendica\Model\Item;
-use Friendica\Module\DirectorySearch;
+use Friendica\Module\Directory;
 use Friendica\Util\Strings;
 
 function search_saved_searches() {
@@ -148,10 +148,10 @@ function search_content(App $a) {
                $search = substr($search,1);
        }
        if (strpos($search,'@') === 0) {
-               return DirectorySearch::performSearch();
+               return Directory::performSearch();
        }
        if (strpos($search,'!') === 0) {
-               return DirectorySearch::performSearch();
+               return Directory::performSearch();
        }
 
        if (!empty($_GET['search-option']))
@@ -162,9 +162,9 @@ function search_content(App $a) {
                                $tag = true;
                                break;
                        case 'contacts':
-                               return DirectorySearch::performSearch('@');
+                               return Directory::performSearch('@');
                        case 'forums':
-                               return DirectorySearch::performSearch('!');
+                               return Directory::performSearch('!');
                }
 
        if (!$search)
index 4a8807fa7025ca90fc36edade579b5ffd0da31aa..02072594cdd5ca51da1b40e3480ee1f3feddcb74 100644 (file)
@@ -113,7 +113,7 @@ class Router
                        $collector->addRoute(['GET'], '/ignored',                            Module\Contact::class);
                });
                $this->routeCollector->addRoute(['GET'],         '/credits',             Module\Credits::class);
-               $this->routeCollector->addRoute(['GET'],         '/dirfind',             Module\DirectorySearch::class);
+               $this->routeCollector->addRoute(['GET'],         '/dirfind',             Module\Search\Directory::class);
                $this->routeCollector->addRoute(['GET'],         '/directory',           Module\Directory::class);
                $this->routeCollector->addGroup('/feed', function (RouteCollector $collector) {
                        $collector->addRoute(['GET'], '/{nickname}',                         Module\Feed::class);
diff --git a/src/Module/BaseSearchModule.php b/src/Module/BaseSearchModule.php
new file mode 100644 (file)
index 0000000..d11f7e9
--- /dev/null
@@ -0,0 +1,104 @@
+<?php
+
+namespace Friendica\Module;
+
+use Friendica\BaseModule;
+use Friendica\Content\ContactSelector;
+use Friendica\Content\Pager;
+use Friendica\Core\L10n;
+use Friendica\Core\Renderer;
+use Friendica\Object\Search\ResultList;
+use Friendica\Util\Proxy as ProxyUtils;
+use Friendica\Model;
+
+/**
+ * Base class for search modules
+ */
+abstract class BaseSearchModule extends BaseModule
+{
+       /**
+        * Prints a human readable search result
+        *
+        * @param ResultList $results
+        * @param Pager      $pager
+        * @param string     $header
+        *
+        * @return string The result
+        * @throws \Friendica\Network\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);
+                       return '';
+               }
+
+               $a = self::getApp();
+
+               $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);
+                               } else {
+                                       $photo_menu = [];
+                               }
+                       } 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());
+
+                       $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');
+               return Renderer::replaceMacros($tpl, [
+                       'title'     => $header,
+                       '$contacts' => $entries,
+                       '$paginate' => $pager->renderFull($results->getTotal()),
+               ]);
+       }
+}
diff --git a/src/Module/DirectorySearch.php b/src/Module/DirectorySearch.php
deleted file mode 100644 (file)
index 7e3e3c2..0000000
+++ /dev/null
@@ -1,149 +0,0 @@
-<?php
-
-namespace Friendica\Module;
-
-use Friendica\BaseModule;
-use Friendica\Content\ContactSelector;
-use Friendica\Content\Pager;
-use Friendica\Content\Widget;
-use Friendica\Core\L10n;
-use Friendica\Core\Renderer;
-use Friendica\Util\Proxy as ProxyUtils;
-use Friendica\Util\Strings;
-use Friendica\Model;
-
-/**
- * Multi search module, which is needed for further search operations
- */
-class DirectorySearch extends BaseModule
-{
-       public static function content()
-       {
-               if (!local_user()) {
-                       notice(L10n::t('Permission denied.'));
-                       return Login::form();
-               }
-
-               $a = self::getApp();
-
-               if (empty($a->page['aside'])) {
-                       $a->page['aside'] = '';
-               }
-
-               $a->page['aside'] .= Widget::findPeople();
-               $a->page['aside'] .= Widget::follow();
-
-               return self::performSearch();
-       }
-
-       public static function performSearch($prefix = '')
-       {
-               $a      = self::getApp();
-               $config = $a->getConfig();
-
-               $community = false;
-
-               $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);
-                       $header  = L10n::t('People Search - %s', $search);
-                       $results = Model\Search::searchUser($search);
-               }
-
-               if (strpos($search, '!') === 0) {
-                       $search    = substr($search, 1);
-                       $community = true;
-                       $header    = L10n::t('Forum Search - %s', $search);
-               }
-
-               $pager = new Pager($a->query_string);
-
-               if ($localSearch && empty($results)) {
-                       $pager->setItemsPerPage(80);
-                       $results = Model\Search::searchLocal($search, $pager->getStart(), $pager->getItemsPerPage(), $community);
-
-               } elseif (strlen($config->get('system', 'directory')) && empty($results)) {
-                       $results = Model\Search::searchDirectory($search, $pager->getPage());
-                       $pager->setItemsPerPage($results->getItemsPage());
-               }
-
-               if (empty($results) || empty($results->getResults())) {
-                       info(L10n::t('No matches') . EOL);
-                       return '';
-               }
-               $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);
-                               } else {
-                                       $photo_menu = [];
-                               }
-                       } 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());
-
-                       $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');
-               return Renderer::replaceMacros($tpl, [
-                       'title'     => $header,
-                       '$contacts' => $entries,
-                       '$paginate' => $pager->renderFull($results->getTotal()),
-               ]);
-       }
-}
diff --git a/src/Module/Search/Directory.php b/src/Module/Search/Directory.php
new file mode 100644 (file)
index 0000000..900bf81
--- /dev/null
@@ -0,0 +1,82 @@
+<?php
+
+namespace Friendica\Module\Search;
+
+use Friendica\BaseModule;
+use Friendica\Content\ContactSelector;
+use Friendica\Content\Pager;
+use Friendica\Content\Widget;
+use Friendica\Core\L10n;
+use Friendica\Core\Renderer;
+use Friendica\Module\BaseSearchModule;
+use Friendica\Util\Proxy as ProxyUtils;
+use Friendica\Util\Strings;
+use Friendica\Model;
+
+/**
+ * Multi search module, which is needed for further search operations
+ */
+class Directory extends BaseSearchModule
+{
+       public static function content()
+       {
+               if (!local_user()) {
+                       notice(L10n::t('Permission denied.'));
+                       return Login::form();
+               }
+
+               $a = self::getApp();
+
+               if (empty($a->page['aside'])) {
+                       $a->page['aside'] = '';
+               }
+
+               $a->page['aside'] .= Widget::findPeople();
+               $a->page['aside'] .= Widget::follow();
+
+               return self::performSearch();
+       }
+
+       public static function performSearch($prefix = '')
+       {
+               $a      = self::getApp();
+               $config = $a->getConfig();
+
+               $community = false;
+
+               $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);
+                       $header  = L10n::t('People Search - %s', $search);
+                       $results = Model\Search::searchUser($search);
+               }
+
+               if (strpos($search, '!') === 0) {
+                       $search    = substr($search, 1);
+                       $community = true;
+                       $header    = L10n::t('Forum Search - %s', $search);
+               }
+
+               $pager = new Pager($a->query_string);
+
+               if ($localSearch && empty($results)) {
+                       $pager->setItemsPerPage(80);
+                       $results = Model\Search::searchLocal($search, $pager->getStart(), $pager->getItemsPerPage(), $community);
+
+               } elseif (strlen($config->get('system', 'directory')) && empty($results)) {
+                       $results = Model\Search::searchDirectory($search, $pager->getPage());
+                       $pager->setItemsPerPage($results->getItemsPage());
+               }
+
+               return self::printResult($results, $pager, $header);
+       }
+}