PHPfox-Official / phpfox-v4-feature-requests

phpFox Feature Request Tracker https://phpfox.com
6 stars 12 forks source link

New Callback to global Search #950

Open Bjoern3003 opened 4 years ago

Bjoern3003 commented 4 years ago

Hi,

can you please add a new Callback Event to your global Search.

So we can set own SearchFilter, that are not using the SQL Union.

We have tested it successfull.

Here are the Method of Search_Service_Search()

public function query($sQuery, $iPage, $iTotalShow, $sView = null)
    {
        $aRows = array();

        if ($sView !== null) {
            if(Phpfox::isModule($sView) && Phpfox::hasCallback($sView, 'CustomSearchQuery')) {
                $aRows = Phpfox::callback($sView . '.CustomSearchQuery', $this->preParse()->clean($sQuery));
            }
        } else {
            foreach(Phpfox::massCallback('CustomSearchQuery', $this->preParse()->clean($sQuery)) AS $aRow)
            {
                $aRows = array_merge($aRows, $aRow);
            }
        }

        if($sView === null && !Phpfox::hasCallback($sView, 'CustomSearchQuery'))
        {
            Phpfox::massCallback('globalUnionSearch', $this->preParse()->clean($sQuery));

            $this->database()->select('
                item.feed_id AS item_id,
                \'\' AS item_title,
                item.time_stamp AS item_time_stamp,
                item.user_id AS item_user_id,
                item.type_id AS item_type_id,
                item.content AS item_photo,
                0 AS item_photo_server')->from(Phpfox::getT('feed'), 'item')->where('' . $this->database()->searchKeywords('item.content', $sQuery))->union();
        }
        else
        {
            if(Phpfox::hasCallback($sView, 'globalUnionSearch'))
            {
                Phpfox::callback($sView . '.globalUnionSearch', $this->preParse()->clean($sQuery));
            }
            else
            {
                $this->database()->select('
                item.feed_id AS item_id,
                \'\' AS item_title,
                item.time_stamp AS item_time_stamp,
                item.user_id AS item_user_id,
                item.type_id AS item_type_id,
                item.content AS item_photo,
                \'\' AS item_photo_server')
                    ->from(Phpfox::getT('feed'), 'item')
                    ->where('' . $this->database()->searchKeywords('item.content', $sQuery) . ' AND ' . $this->database()->searchKeywords('item.type_id', $sView))
                    ->union();
            }
        }

        $aRows = array_merge($aRows, $this->database()->select('item.*, ' . Phpfox::getUserField())
            ->unionFrom('item', true)
            ->join(Phpfox::getT('user'), 'u', 'u.user_id = item.item_user_id')
            ->limit($iPage, $iTotalShow)
            ->order('item_time_stamp DESC')
            ->execute('getSlaveRows'));

        $aResults = array();
        foreach ($aRows as $iKey => $aRow) {
            if (app()->exists($aRow['item_type_id'])) {
                $app = app($aRow['item_type_id']);
                if ($app->map_search) {
                    $data = json_decode($aRow['item_photo']);
                    if (isset($data->{$app->map_search->title})) {
                        $aRow['item_title'] = $data->{$app->map_search->title};
                        $aRow['item_link'] = url(str_replace(':id', $aRow['item_id'], $app->map_search->link));
                        $aRow['item_name'] = _p($app->map_search->info);

                        $aResults[] = $aRow;
                    }
                }

                continue;
            }
            if (Phpfox::hasCallback($aRow['item_type_id'], 'getSearchInfo')) {
                $aResults[] = array_merge($aRow, (array)Phpfox::callback($aRow['item_type_id'] . '.getSearchInfo', $aRow));
            }
        }

        if (Phpfox::getParam('core.section_privacy_item_browsing') && !empty($aResults)) {
            // Check for special filters
            $aToParse = array();
            // Group results by their module
            foreach ($aResults as $aResult) {
                $aToParse[$aResult['item_type_id']][] = $aResult['item_id'];
            }

            foreach ($aToParse as $sModule => $aItems) {
                if (Phpfox::hasCallback($sModule, 'filterSearchResults')) {
                    $aNotAllowed = Phpfox::callback($sModule . '.filterSearchResults', $aItems);
                    if (!empty($aNotAllowed)) {
                        foreach ($aNotAllowed as $aItem) {
                            foreach ($aResults as $iKey => $aResult) {
                                if ($aResult['item_type_id'] == $aItem['item_type_id'] && $aResult['item_id'] == $aItem['item_id']) {
                                    unset($aResults[$iKey]);
                                }
                            }
                        }
                    }
                }
            }
        }
        return $aResults;
    }
Bjoern3003 commented 4 years ago

Any news there? Is there any plan for a target version?