0

I am building a notification system which should email a user whenever a new entry is added that matches their saved results. The way I plan to make this work is using a Postmaster Notification which will run on CRON job once a day.

I have already built the system that saves searches using Solspace's Super Search (which we are already using the on site anyway - works well).

My notification code gets all the saved searches, and then should run them and email out any new results. What's the best way to run a search? I just need back the entry_ids I think.

Here's what I have so far:

    // Get all the saved searches that are active.
    $searches = ee()->db->select('screen_name, email, search_name, query')
      ->from('super_search_history h')
      ->join('members m', 'h.member_id = m.member_id')
      ->where(array(
        'h.saved' => 'y'
      ))
      ->get();

    // For each search, unhash the query, and pass it to Super Search.
    if ($searches->num_rows() > 0)
    {
        foreach($searches->result_array() as $row)
        {
            $q = unserialize(base64_decode($row['query']));
            // Add information to the query that indicates the timeframe for the search - LATER

            // What are the entry IDs?
            require_once(PATH_THIRD.'super_search/mod.super_search.php');
            $search = Super_search::do_search($q);
        }
    }

I think the main issue is me not knowing the best way to call one module's function from within another. I get the error Call to a member function fetch_param() on a non-object in mod.super_search.php on line 644.

Do I need to create a version of the "do_search" function that doesn't use the TMPL class (since we're not calling this function from within a template).

  • I found that if I initialise the template class, I don't get the PHP errors, but the do_search function still returns FALSE rather than the entry IDs as I would expect. – boilerroomdigital Jun 23 '14 at 16:25
1

My guess is that if you instantiate the class and it's not working, you need to also dive into their code to see why you aren't getting any results. If I had to guess, it's because there are no template parameters set. You can programmatically set parameters like so:

ee()->TMPL->tagparams['param_name'] = 'some value';

Or in versions of EE prior to 2.7 (I think)

$this->EE->TMPL->tagparams['param_name'] = 'some value';

ExpressionEngine is largely based on the singleton programming pattern, so you should be able to update the instance object with whatever you need. The main challenge will be finding out what you need and figuring out how to modify the singleton to fit your needs. Just going to point this out for the rest of the people to benefit, but you can access the main singleton like so:

ee();

Or in older versions prior to 2.7

$ee =& get_instance();

And in the case of the Template object, it's also a singleton. The class name would be EE_Template but EE instantiates it as TMPL, and is assigned to the main singleton which is:

ee()->TMPL;

Unfortunately, this is the best answer I can give without specifically writing the code myself.

| improve this answer | |
  • Thanks, Justin - that's very helpful. I've been diving into their code and trying to work out why the function returns false rather than some IDs. I think you could be right with regard to template parameters, but it's also looking at the session class, and variables set there including the URI. Is it possible to set these programmatically as well? – boilerroomdigital Jun 24 '14 at 8:52
  • You should be able to. Every instantiate object that assigned to the core singleton essentially becomes a singleton itself. So for example, you could update the ee()->session->userdata object if you wanted. – Justin Kimbrell Jun 24 '14 at 21:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.