In the last article, we added some checkboxes to the form and some new Tpl chunks. Those won't work without our new controller code, so in this article, we'll look at a bigger, but still fairly simple, controller.
See the previous articles for the Tpl chunks, menu, and namespace information.
New Code in the Controller
Remember that the controller file (
home.class.php) is in the
core/components/mysearch/controllers" directory. It's that file that we'll be changing.
core components mysearch controllers home.class.php
New Controller Code
To get ready for this, we've created a
processor() method (not to be confused with the original
process() method that's part of any MODX controller). The goal is to create a
processor() function that receives the same information as a real processor would, and returns what a real processor would return. That way, the processor's code (and any functions that support it) can easily be moved out of the controller, and into a real processor.
We've also made some changes to get the code ready for searches for other object like chunks and templates. The theory is that to add a search for chunks, for example, the only thing that would need to change is the form itself, because the controller and processor code would already be set up to process any additional elements added to the form.
In addition, the code that produces the output for a single section — the results of a search for a single object type — has been moved out of the
find() function to a function of its own (
processCollection()). This is in keeping with the coding principle that a function should have only one job.
Here's new code for the
We've created a new method,
process() method now just hands off the
$_POST array to the
processor() code sanitizes the fields (
$_POST), gets the search term, extracts the
['objects'] member, then loops through the objects, calling
find() with the appropriate arguments for each object type.
find() method builds the query criteria dynamically, based on the fields to search passed in to it. Then it queries the database, and passes the results (if any) back to the processor function. If there are results, the processor calls
processCollection(), which creates a section of output for each object searched, calling
addToResults() for each list item to be added to the results display.
To prepare for the addition of other objects to search, we've added the
getNameField() function to get the field that holds the name of the object, since it won't always be
pagetitle. We've also added the
getAction() function to get the correct action that needs to be called to update (edit) the object because it won't always be
find() method will only be called once given our current form, and the
$collection variable will only hold results from the search for resources, the code is now designed to process additional objects like chunks and templates as soon as they are added to the form. In fact, if you've been building this as we go along and you've got it working, you should be able to just add a section to the form for chunks with input names like this for the checkboxes:
Here's the code for a chunks section. Add it just below the checkbox div for resources:
<h3 style="margin-top:30px;">Chunks</h3> <div id="checkbox_div2" class="x-form-check-wrap"> <input style="position:static;" type="checkbox" id="modChunk_name" autocomplete="off" class="x-form-checkbox x-form-field" name="search_objects[modChunk]" value="name"> <label for="modChunk_name" class="x-form-cb-label"> name</label> <br/> <input style="position:static;" type="checkbox" id="modChunk_description" autocomplete="off" class="x-form-checkbox x-form-field" name="search_objects[modChunk]" value="description"> <label for="modChunk_description" class="x-form-cb-label"> description</label> <br/> <input style="position:static;" type="checkbox" id="modChunk_content" autocomplete="off" class="x-form-checkbox x-form-field" name="search_objects[modChunk]" value="content"> <label for="modChunk_content" class="x-form-cb-label"> content</label> <br/> <input style="position:static;" type="checkbox" id="modChunk_id" autocomplete="off" class="x-form-checkbox x-form-field" name="search_objects[modChunk]" value="id"> <label for="modChunk_id" class="x-form-cb-label"> id</label> <br/> </div>
Chunks, templates, plugins, and snippets should all work if you add them to the form (and remember to use the appropriate fields). If you add a chunks section, for example, be sure to use chunk fields, not resource fields for the values.
In the next article, we'll move the
Looking for high-quality, MODX-friendly hosting? As of May 2016, Bob's Guides is hosted at A2 hosting. (More information in the box below.)