We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 16429
    • 254 Posts
    *** The krequest extender with alternative filter syntax and the maxFilter param description are here ***

    Hi all

    (Disclaimer: if you don’t understand the base of Ditto filtering && the anatomy of a request call, it’s difficult you’ll be able to understand this.
    In that case, RTFM about filtering and then RTFM about the request extender.
    Thx!)

    I was playing with a form and noticed that it’s impossible to send filters to Ditto with request extender using fields not fixed.
    For example, I can send a filter with a select, like
    <label for="ditto_filter_1">News about:</label>
    <select name="ditto_filter_1" id="ditto_filter_1">
    	<option value="topic,sport,1">Sport</option>
    	<option value="topic,economy,1">Economy</option>
    	<option value="topic,travel,1">Travel</option>
    	<option value="topic,science,1">Science</option>
    </select>
    

    Same for checkboxes
    <input name="ditto_filter_1" id="ditto_filter_1" value="hasColor,red,7" type="checkbox" />Red
    <input name="ditto_filter_2" id="ditto_filter_2" value="hasColor,blue,7" type="checkbox" />Blue
    <input name="ditto_filter_3" id="ditto_filter_3" value="hasColor,green,7" type="checkbox" />Green
    

    And, basically, with every type of fixed single-or-multi choice field.
    That’s because you can hardcode the filtering syntax. With a user-input field (like <input type="text" />) you can receive a value but not a complete filter (unless you teach the syntax to your users and have it directly inputed in the field).
    This can be a problem if you (like me, obviously grin) need to use a form with text fields to complex-filter a Ditto call with $_GETs.

    So why don’t spice up a little bit the "filter on request" syntax? Yes! Who-o!

    I’ve thought one solution: we can write part of the filter, the piece to describe the comparison (read: the TV and the numeric comparator), in the variable name and let the variable old the value we want to filter by.
    Maybe an example is better than 10k words:
    We have the old way to send filters (this is an piece of a $_GET url, just to highlight how the filters are passed: you can use $_POST too, it’s the same):
    &ditto_id_filter_1=tvToCompare,value,3&ditto_id_filter_2=anotherTvToCompare,value,2
    

    (the number after the value is just for the sake of the example)

    But we can use an alternative way, where you can write the filter in the variable name and let the user send only the value to compare against: let’s see the same call in the alternative syntax
    &ditto_id_filter_1_tvToCompare3=value&ditto_id_filter_2_anotherTvToCompare2=value
    

    As you can see, after the filter numbering (filter_1, filter_2) I’ve added an underscore and then the tv name (tvToCompare, anotherTvToCompare) AND, at the end, the numeric comparator (as told, in this case the numbers are for example).

    Obviously the request extender can’t match this syntax with the regular request filter parser so I’ve created a modified request extender that includes the maxFilter param use: you’ll find informations on maxFilter and its use in this post, along with the krequest.extender.inc.php

    PAY ATTENTION

    1) The filter numbering must be preserved between normal and alternative syntax: the resulting of my piece of code is a normal filter, so if you use the same numbering for two filters written in different ways, the alternative syntax filter will replace the same-numbered normal filter.
    Write every filter with a different increasing number!!! And respect the request filtering requirements (start with filter_1, then filter_2, and so on).

    2) Under this code you can’t change the "localFilterDelimiter", because for the rush I didn’t check if that param is yet set at the time Ditto includes the extender so I optioned to create the filter inserting commas (",") as local delimiters as Ditto default behaviour.


    Conclusions
    Obviously this is a "fix" for a very remote situation, it works for me without problems but I suggest not to use it if you don’t work with complex forms as source for Ditto request filtering.

    Obviously, it works for me. I’ve tested it but I don’t give warranty for anyone. Test it if you want to use it.

    Mark, if it’s good for you, please put it in Ditto so I don’t have to modify the code every time you release a new version grin
      kudo
      www.kudolink.com - webdesign (surprised?)

      [img]http://www.kudolink.com/kudolinkcom.png[/img] [sup]proudly uses[/sup] [img]http://www.kudolink.com/modx.png[/img]
      • 11975
      • 2,542 Posts
      Hi Kudo,

      by curiosity, why not use tvexplorer for this ?

      :-)

        Made with MODx : [url=http://www.copadel.com]copadel, fruits et l
        • 16429
        • 254 Posts
        Quote from: heliotrope at Apr 17, 2008, 01:00 PM

        Hi Kudo,

        by curiosity, why not use tvexplorer for this ?

        :-)

        Well... because I don’t have the time now to understand how to build a form with tve and I don’t need a snippet that does that kind of work for me (in this case, I mean).
        My problem was how to pass certain data to Ditto, resolved that, all works now.
        Aside, I used other logic to retrieve other parameter, because the search is in a booking system and the final Ditto call is the result of many snippets, a reservations module, etc... I used Ditto as a base, using tve would mean having a driven form->search flow in a script and you can understand the work to modify it for my needs.
        Aside, I think tve is a fantastic idea and I’ll surely use it in the future when it will suit my needs. But for now the solution was to tailor various scripts for the party, and not use tve.

        Specifically, this work involved not complex tvs that need a logic to present them to the user, but many many tvs that I need to pass at the same time.
          kudo
          www.kudolink.com - webdesign (surprised?)

          [img]http://www.kudolink.com/kudolinkcom.png[/img] [sup]proudly uses[/sup] [img]http://www.kudolink.com/modx.png[/img]
          • 11975
          • 2,542 Posts
          I get your point but what I mean is tve is a form builder as well.

          You still have to code your html

          <label for="ditto_filter_1">News about:</label>
          <select name="ditto_filter_1" id="ditto_filter_1">
          <option value="topic,sport,1">Sport</option>
          <option value="topic,economy,1">Economy</option>
          <option value="topic,travel,1">Travel</option>
          <option value="topic,science,1">Science</option>
          </select>

          You could get the same output in one tag

          [+tve.topic:tvtype->select:label->news about:filter->1+]

          without writing any options values.

          It also support @BINDINGS
          [+tve.topic:tvtype->select:label->news about:filter->1:elements->@SELECT value,id FROM my_table+]
          In case you want to get options values from another table

          :-)
            Made with MODx : [url=http://www.copadel.com]copadel, fruits et l
            • 16429
            • 254 Posts
            I’ve noticed, don’t think I didn’t take tve into account when thinking about this thing smiley

            But, for example, some filter doesn’t respect the exact tv but they are used only as a switch so I had to use the filtering and comparison capabilities of the Ditto filter class.
            Maybe one day I’ll crawl in tve use and fnd a way to achieve the same result, but for now my hurry didn’t leave me the time to experiment with a new snippet, the method flashed in my mind and I started to follow it smiley
              kudo
              www.kudolink.com - webdesign (surprised?)

              [img]http://www.kudolink.com/kudolinkcom.png[/img] [sup]proudly uses[/sup] [img]http://www.kudolink.com/modx.png[/img]
              • 16429
              • 254 Posts
              Ok, another bonus.
              Obviously the situation is the same: a form to create a Ditto call, the request extender, filters.

              The problem
              Using checkboxes as filters can cause problems, because we have to use sequential filter numbering but with checkboxes a filter is sent only when it’s checked.
              This can cause the form to send the list of filters with numbering holes, and the request extender stops to add filters received from the GET or POST when it can’t find a sequential number.
              Let’s see the example (a piece of a form):
              <li><input name="ditto_id_filter_1" id="ditto_ra_filter_16" value="TvFishing,true,1" type="checkbox" />Fishing</li>
              <li><input name="ditto_id_filter_2" id="ditto_ra_filter_17" value="TvHunting,true,1" type="checkbox" />Hunting</li>
              <li><input name="ditto_id_filter_3" id="ditto_ra_filter_18" value="TvSkiing,true,1" type="checkbox" />Skiing</li>
              <li><input name="ditto_id_filter_4" id="ditto_ra_filter_19" value="TvWellness,true,1" type="checkbox" />Wellness center</li>
              

              If the user selects "Fishing", "Hunting" and "Wellness center" assuming we use GET method for simplicity the url will contain:
              &ditto_id_filter_1=TvFishing,true,1&ditto_id_filter_2=TvHunting,true,1&ditto_id_filter_4=TvWellness,true,1
              

              But looking at the output we can see the filter_4 has not been taken into account and the results are filtered only by the first two filters.
              This is because the request extender creates a mega-filter param cycling through the filters numbers (filter_1, filter_2, filter_3 and so on) until it finds the next number. When it fails at finding the next number (in the example, we lack the filter_3) the filter parser stops and all the filters with number greater than the one we lack are not added to the mega filter.

              The solution
              This time the solution was to hack the filter parser to find if a maxFilter variable is set and use it to tell the parser how many ditto_filter_N there are, and search for them.
              Luckily Mark set the request base parser to create variables for ALL the params that start with ditto_ in the request extender. This means we can send new params (like maxFilter) prefixing them with ditto_ and have them working in the request extender logic.
              So, simply, if you can have filter numbering holes (as in the previous example) set a hidden input with name "ditto_id_maxFilter" and a numeric value that matches your greater filter number.
              If you are interested, mine was 56 !

              Now I $_POST the solution, so you can $_GET it
              This time a little light bulb has lit on my head. So I’ve dumped the request.extender.inc.php, added my hacks, and krequest.extender.inc.php has born smiley
              Ok, ok... Nothing special in it, I admit... simply put it in your /extenders/ dir and choose if you want to include the normal request extender or my modified version (which includes the alternative filter search and the maxFilter solution).

              Take it (even if I’m very curious to know if someone will front a similar situation!)
                kudo
                www.kudolink.com - webdesign (surprised?)

                [img]http://www.kudolink.com/kudolinkcom.png[/img] [sup]proudly uses[/sup] [img]http://www.kudolink.com/modx.png[/img]