Manipulate total_match_count in search

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
User avatar
kinerity
Registered User
Posts: 1128
Joined: Mon Sep 01, 2014 1:00 am
Name: Kailey Truscott
Contact:

Manipulate total_match_count in search

Post by kinerity » Mon Dec 01, 2014 9:58 am

Hi everyone!

I'm hoping someone can help me figure this one out (I've been at it for a while and can't figure out how to accomplish this one).

I have a piece of code that modifies the search parameters and check whether the user has the appropriate permissions or is the original poster. If neither, the search result is removed. This part works great. The problem is that I need to update the $total_match_count variable so that I don't get results like this:
search.png
search.png (13.1 KiB) Viewed 708 times
Here is my relevant code:

Code: Select all

public function search_get_topic_data($event)
{
    $sql_where = $event['sql_where'];

    $sql = 'SELECT *
        FROM ' . FORUMS_TABLE;
    $result = $this->db->sql_query($sql);
    $fid_ary = array();
    while ($row = $this->db->sql_fetchrow($result))
    {
        if ($row['forum_op_only_view'] && !$this->auth->acl_get('f_op_only_view', $row['forum_id']))
        {
            $fid_ary[] = $row['forum_id'];
        }
    }
    $this->db->sql_freeresult($result);

    $sql_where .= ' AND (f.forum_id NOT IN (' . (empty($fid_ary) ? 0 : implode($fid_ary)) . ') OR t.topic_poster = ' . $this->user->data['user_id'] . ')';

    $event['sql_where'] = $sql_where;
} 
I need to count the number of times a search term appears within posts/topics in the $fid_ary and then subtract that number from $total_match_count. Can someone help me? Thanks!
phpBB3 Refugees - Unofficial phpBB 3.0.x support

Image

User avatar
Wolfsblvt
Registered User
Posts: 632
Joined: Sun Oct 26, 2014 9:12 pm
Location: Solingen, Germany
Name: Clemens H.
Contact:

Re: Manipulate total_match_count in search

Post by Wolfsblvt » Mon Dec 01, 2014 10:15 am

I haven't looked deeply in the code, as I've thought you have done that already, but a quick idea:
Use a dirty trick and overwrite the template var.
There is an event after assigning the template vars. Just reassign it.

Code: Select all

    $total_matches_with_substracted = $total_match_count - $my_hidden_topics_count;
    $template->assign_vars(array(
        'TOTAL_MATCHES'        => $total_matches_with_substracted,
    ));
 
Event is core.search_results_modify_search_title
Maybe there is a smarter solution for this, but I don't have the time to look deeply in here.
If you have a specific extension request and you are willing to pay for, you can write me a PM.
My extensions (Trending: @Mention SystemAdvanced PollsUser Online Time)

»Du kamst zu uns. Deine Stimme kam. Du zeigtest uns die Sterne. Sie funkelten. Wir konnten sehen.«

User avatar
kinerity
Registered User
Posts: 1128
Joined: Mon Sep 01, 2014 1:00 am
Name: Kailey Truscott
Contact:

Re: Manipulate total_match_count in search

Post by kinerity » Mon Dec 01, 2014 12:11 pm

The problem isn't the wrong event or needing a different event. I also tried the code below, but the SQL query is ignoring the $ex_fid_ary value I pass by by the event.

Code: Select all

$ex_fid_ary = $event['ex_fid_ary'];

$sql = 'SELECT forum_id, forum_op_only_view
    FROM ' . FORUMS_TABLE;
$result = $this->db->sql_query($sql);
while ($row = $this->db->sql_fetchrow($result))
{
    if ($row['forum_op_only_view'] && !$this->auth->acl_get('f_op_only_view', $row['forum_id']))
    {
        $ex_fid_ary[] = (int) $row['forum_id'];
    }
}
$this->db->sql_freeresult($result);

$event['ex_fid_ary'] = $ex_fid_ary; 
If I var_dump the variable, it shows array(1) { [0]=> int(3) } (3 is the forum_id I'm checking against), so the variable definitely gets passed. Having this code work is more desirable because then I won't have to worry about manipulating $total_match_count.
phpBB3 Refugees - Unofficial phpBB 3.0.x support

Image

User avatar
Wolfsblvt
Registered User
Posts: 632
Joined: Sun Oct 26, 2014 9:12 pm
Location: Solingen, Germany
Name: Clemens H.
Contact:

Re: Manipulate total_match_count in search

Post by Wolfsblvt » Mon Dec 01, 2014 12:20 pm

Are you searching for posts or topics? This definitely relevant, cause the queries are totally different.
Also it would help if you would give me the event you are modifient, not just the code you are using there :P
I think you mean core.search_get_posts_data?

If this is the event you are using, there is a problem. 'ex_fid_ary' is a variable passed to the event just for reading access.
The query was already build in 'sql_array', and this is the query wich is run. So you need to modify $sql_array['WHERE']

Wich would be a bit more difficult.
If you have a specific extension request and you are willing to pay for, you can write me a PM.
My extensions (Trending: @Mention SystemAdvanced PollsUser Online Time)

»Du kamst zu uns. Deine Stimme kam. Du zeigtest uns die Sterne. Sie funkelten. Wir konnten sehen.«

User avatar
kinerity
Registered User
Posts: 1128
Joined: Mon Sep 01, 2014 1:00 am
Name: Kailey Truscott
Contact:

Re: Manipulate total_match_count in search

Post by kinerity » Mon Dec 01, 2014 5:10 pm

Looks like I need to request another event. Not being able to modify $ex_fid_ary is a major drawback for this extension. Thanks!
phpBB3 Refugees - Unofficial phpBB 3.0.x support

Image

Post Reply

Return to “Extension Writers Discussion”

Who is online

Users browsing this forum: No registered users and 10 guests