⬆️ ⬇️

Do a search on multiple TV

To begin with - a small lyrical digression. I was recently instructed here at work to make a small apartment base for a real estate agency site. Yes, such that it was possible to search for apartments by as many as 5 parameters at once - the city, district, number of rooms, layout and price. Well, so that all this was built into the admin area and easily edited, of course. And I must say that I worked with different CMS - from expensive and heavy Beatrix to self-written unpretentious engines, and in the end I opted for MODx - for, do not consider advertising / anti-advertising, but that on the same Bitrix was done for a week (namely, an uncomplicated corporate website), MODx is quietly done in a day.



Why am I actually writing this? In almost any CMS for the implementation of this task would have to write additional code and in every possible way pervert. In MODx, this is solved with the help of exclusively standard engine tools, and the only thing that you have to write with “pens” is the search form itself. Suppose it is not obvious (I had to dig through the documentation, while I realized), but everything is useful.



So, the task is divided into the following stages:

  1. Create a TV for the properties that will be searched.

    Set a template and logic for finding search results.

    Create the search form itself.

    I think that those who are familiar with MODx, it makes no sense to explain what TV is. The closest analogue I have ever seen is the properties of the information block in Bitrix. Suppose this is not exactly the same thing, but in this case the logic of construction can be used similar.

    ')

    Semantically, our base of apartments will be a container with publications (selected in a separate group of documents so that there is no confusion). One apartment - one publication, and the TV parameters in this case for each publication about the apartment will play the role of these very properties. Like this:



    document structure

    publication properties



    So that when editing a publication with a flat, we get such a beauty, we go to “Resource Management” -> “Parameters (TV)”, and for each parameter we set a drop-down list like this:



    set the parameter



    Yes, the separator for possible values ​​is "||", and the default value is empty just in case.



    Now create a new document in the root of the site. Call it what we want, something like "search." And instead of writing our snippets and in every possible way perverted, we use the magic of the standard and wildly powerful Ditto. Paste the following into the body of the document:



    [!Ditto? &tpl=`Apartments` &startID=`52` &filter=`tvrooms,@EVAL return $_POST['rooms'];,1|tvcity,@EVAL return $_POST['city'];,1|tvregion,@EVAL return $_POST['region'];,1|tvplan,@EVAL return $_POST['plan'];,1|tvcost,@EVAL return $_POST['cost'];,1` &noResults=`, .`!]



    We will analyze what makes this call.



    & tpl = `Apartments` - we define what will be the name of the chunk with the template for displaying the found publications. The usual chunk for Ditto, I think, is not necessary to say separately how to create it.



    & startID = `52` - ID of the container with publications for which the selection is made. In our case, as seen in the screenshot, this is 52.



    & filter = `tvrooms, EVAL return $ _POST ['rooms'] ;, 1 ... is the most magical thing. This, as you can guess, is a filter by which unnecessary values ​​are eliminated and the necessary ones are selected. In the quoted logic, this is: “weed out all documents whose TV-parameter values ​​are NOT EQUAL to the php-variable of rooms from the $ _POST superglobal array” .



    That is, we have three arguments, separated by commas. The first is the parameter by which we filter (if this is a TV parameter, then add tv to its name to indicate this). The second is the value of the parameter. In this case, we use the EVAL call to execute the PHP code with a direct reference to the variable from $ _POST. Well, the third parameter is a pointer to a logical operator linking the parameter and the value. The unit means "not equal", yes (you can read more about this here ). Well, actually, such filtering rules can be combined using the "|" symbol, which corresponds to the logical "OR". What we are doing in order to combine all our TV-parameters into one filter.



    By the way, note that the names of the TV parameters and the names of the variables in $ _POST are made the same. This is for convenience so that there is no confusion.



    And now the final touch - create a chunk with a search form, drop-down lists in which we call - guess how? - yes, exactly the same as TV-parameters. The option values ​​are written the same as when setting the parameters (I have not tried to look for a way for this to happen automatically, but I will try for sure). And in the action attribute of the form tag we indicate the URL of the search page with a call to Ditto. We call the chunk as we want, for example, SearchForm - and paste it into all the pages from which we want to search, by calling {{SearchForm}}. Voila!



    So you can make bindings and search forms for any advanced parameters, without writing a single line of PHP code. Enjoy!

Source: https://habr.com/ru/post/16117/



All Articles