Do a search on multiple TV

first — a small lyrical digression. Requested here recently with my work to make a small Basco apartments website for real estate Agency. Yes such that it was possible to find apartments as much as 5 parameters at once — the city, district, number of rooms, layout and price. Well, that all this was built into the admin area and easily edited, of course. And I must say that I have worked with different CMS — from expensive and heavy Bitrix to bespoke a simple Diachkov, and in the end I chose MODx for not consider for advertising/anti-advertising, but the fact that Bitrix was done for the week (namely, simple corporate saytets), MODx quietly done for the day.

What I am is, in fact, write. In almost any CMS for the implementation of this task would have to write additional code and otherwise subverted. In MODx it is solved using only the standard engine, and the only thing that you have to write "handles" is the actual search form. Let not obvious (had to dig through the documentation, until realized) — but all are useful.

So, the task is divided into the following stages:
    the
  1. New TV for the properties that will be searched.
    Ask the template and logic for the conclusions of the search results.
    To create the search form.
    I Think those who are familiar with MODx, there is no need to explain what this TV. The closest analogue that I have seen are the properties of the InfoBlock in the Bitrix. Though it's not quite the same, but the logic of construction in this case you can use similar.

    Semantically our database of apartments will be of a container of publications (in a separate document group, to avoid confusion). One apartment — one publication, and TV settings in this case, for each publication about the apartment will play the role of most of these properties. Like this:

    structure document
    publishing properties

    To edit a publication with the apartment we got here is such a beauty, we go to "resource Management" -> Settings "(TV)", and for each parameter set drop-down list like this:

    set the parameter

    Yes, the delimiter for the possible values is "| | " and the default value in any case make it empty.

    Now create a new document in the root of the site. Call it what you will, something like "search". And instead of having to write your own snippets and otherwise subverted, use a magic standard and wildly powerful Ditto. Is inserted into the body of the document as follows:

    [!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=`Sorry, the property at Your request not found.`!]

    Let us consider what makes this call.

    the &tpl=`Apartments` — defined as to be called a chunk with a template for displaying the found publications. The usual chunk for Ditto, I don't think you need to say, how to create it.

    the &startID=`52` — the ID of the container of publications in which selection. In our case, as seen in the screenshot, it 52.

    the &filter=`tvrooms, EVAL return $_POST['rooms'];,1... — the most magical thing. This, as you might guess, the filter, which eliminated the unwanted values and selected need. In salteranno logic is this: "to weed out all the documents, the values of TV-option rooms which is NOT EQUAL to the value of php variable rooms of the superglobal array $_POST".

    That is, we have three arguments, separated by commas. The first is the parameter on which to be filtered (if TV-setting — tv to add his name to indicate this). The second is the parameter value. In this case, we use the call EVAL to execute PHP code, with direct reference to a variable from $_POST. Well and the third parameter is a pointer to a Boolean operator relating the parameter and the value. One means "not equal to", (more you can read about it here here). Well, actually, these filtering rules can be combined using the " | " symbol, which means a logical "OR". What we do in order to unite in one filter all of our TV-parameters.
    By the way, please note that the names of the TV-parameters and variable names in $_POST are made the same. This is for convenience, to avoid any confusion.

    And now the finishing touch — create a chunk with a search form, a drop down list which is called — guess what? — Yes, it is the same as the TV-parameters. Option writing are the same as the settings (have not tried to find a way to make it happen automatically, but try to be sure). And in form tag's action attribute specified the URL of the search page with a call to Ditto. Called chunk as you want, for example, SearchForm and paste it on all pages from which we want to search, by calling {{SearchForm}}. Voila!

    So you can do bind and search for any advanced parameters, without writing a single line of PHP code. Enjoy!
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

Monitoring PostgreSQL + php-fpm + nginx + disk using Zabbix

Templates ESKD and GOST 7.32 for Lyx 1.6.x

Customize your Google