Custom icons and images for search result objects
authorAlexander Ebert <ebert@woltlab.com>
Fri, 22 Jun 2018 11:31:12 +0000 (13:31 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Fri, 22 Jun 2018 11:31:12 +0000 (13:31 +0200)
See #2581

com.woltlab.wcf/templates/searchResultList.tpl
wcfsetup/install/files/lib/data/search/ICustomIconSearchResultObject.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/page/SearchResultPage.class.php

index 291d73737b1072267ba41ac169d42ad71719295b..b9b62eee57aa197ace1eebbcd5ca120d3a97e827 100644 (file)
@@ -3,14 +3,22 @@
                {foreach from=$objects item=message}
                        <li>
                                <div class="box48">
-                                       {if $message->getUserProfile()}
-                                               {if $message->getUserProfile()->userID}
-                                                       <a href="{link controller='User' object=$message->getUserProfile()}{/link}" title="{$message->getUserProfile()->username}">{@$message->getUserProfile()->getAvatar()->getImageTag(48)}</a>
+                                       {assign var=_messageObjectHash value=$message|spl_object_hash}
+                                       {assign var=_messageCustomIcon value=$customIcons[$_messageObjectHash]}
+                                       {if $_messageCustomIcon === ''}
+                                               {if $message->getUserProfile()}
+                                                       {if $message->getUserProfile()->userID}
+                                                               <a href="{link controller='User' object=$message->getUserProfile()}{/link}" title="{$message->getUserProfile()->username}">{@$message->getUserProfile()->getAvatar()->getImageTag(48)}</a>
+                                                       {else}
+                                                               <p>{@$message->getUserProfile()->getAvatar()->getImageTag(48)}</p>
+                                                       {/if}
                                                {else}
-                                                       <p>{@$message->getUserProfile()->getAvatar()->getImageTag(48)}</p>
+                                                       <p><span class="icon icon48 fa-file-o"></span></p>
                                                {/if}
+                                       {elseif $_messageCustomIcon|strpos:'fa-' === 0}
+                                               <p><span class="icon icon48 {$_messageCustomIcon}"></span></p>
                                        {else}
-                                               <p><span class="icon icon48 fa-file-o"></span></p>
+                                               <p><img src="{$_messageCustomIcon}" style="width: 48px; height: 48px" alt=""></p>
                                        {/if}
                                        
                                        <div>
@@ -39,4 +47,4 @@
                        </li>
                {/foreach}
        </ul>
-</div>
\ No newline at end of file
+</div>
diff --git a/wcfsetup/install/files/lib/data/search/ICustomIconSearchResultObject.class.php b/wcfsetup/install/files/lib/data/search/ICustomIconSearchResultObject.class.php
new file mode 100644 (file)
index 0000000..846cf62
--- /dev/null
@@ -0,0 +1,24 @@
+<?php
+declare(strict_types=1);
+namespace wcf\data\search;
+
+/**
+ * Extends the base search result objects with the ability to provide a custom image or icon
+ * class name instead of the default avatar/icon. 
+ * 
+ * @author      Alexander Ebert
+ * @copyright   2001-2018 WoltLab GmbH
+ * @license     GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package     WoltLabSuite\Core\Data\Search
+ * @since       3.2
+ */
+interface ICustomIconSearchResultObject extends ISearchResultObject {
+       /**
+        * Returns either a FontAwesome icon name including the `fa-` prefix or
+        * a string that is interpreted as an URL to an image that can be scaled
+        * to 48x48. Returning an empty string will trigger the default behavior. 
+        * 
+        * @return      string
+        */
+       public function getCustomSearchResultIcon(): string;
+}
index ec42346d41937f3bfb4043e0c99ec9d1187641c9..00e8d5b84b0066e3849f2c295a30fd1bc9adfa63 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 declare(strict_types=1);
 namespace wcf\page;
+use wcf\data\search\ICustomIconSearchResultObject;
 use wcf\data\search\ISearchResultObject;
 use wcf\data\search\Search;
 use wcf\system\application\ApplicationHandler;
@@ -20,6 +21,12 @@ use wcf\system\WCF;
  * @package    WoltLabSuite\Core\Page
  */
 class SearchResultPage extends MultipleLinkPage {
+       /**
+        * list of custom icons per message
+        * @var string[]
+        */
+       public $customIcons = [];
+       
        /**
         * @inheritDoc
         */
@@ -144,7 +151,13 @@ class SearchResultPage extends MultipleLinkPage {
                                        throw new ImplementationException(get_class($message), ISearchResultObject::class);
                                }
                                
+                               $customIcon = '';
+                               if ($message instanceof ICustomIconSearchResultObject) {
+                                       $customIcon = $message->getCustomSearchResultIcon();
+                               }
+                               
                                $this->messages[] = $message;
+                               $this->customIcons[spl_object_hash($message)] = $customIcon;
                        }
                }
        }
@@ -171,7 +184,8 @@ class SearchResultPage extends MultipleLinkPage {
                        'resultListTemplateName' => $this->resultListTemplateName,
                        'resultListApplication' => $this->resultListApplication,
                        'application' => ApplicationHandler::getInstance()->getAbbreviation(ApplicationHandler::getInstance()->getActiveApplication()->packageID),
-                       'searchPreselectObjectType' => $searchPreselectObjectType
+                       'searchPreselectObjectType' => $searchPreselectObjectType,
+                       'customIcons' => $this->customIcons
                ]);
        }