Search code examples
phpsymfony1symfony-1.4

How to check if filters have been applyed in template?


I'm working on an admin-generator module. I'd like to hide the item-list until the user has used the filters. So I'd like to check, if the filter-form has been sent.

Is there any var I can check for this in the indexSuccess.php Template?


Solution

  • Filters are stored inside the session when the user submit them.

    So, in your template, you can access defined filter by calling the session. If you don't have defaults filter, it will return an empty array (might be sfOutputEscaperArrayDecorator if you use output protection).

    If you module name is car for example, you can get filters inside your template using:

    $filters = $sf_user->getAttribute('car.filters', null, 'admin_module');
    

    And if you use output protection, you can do:

    $filters = sfOutputEscaper::unescape($sf_user->getAttribute('car.filters', null, 'admin_module'));
    

    If you do not have filters, you will get something like this with a var_dump (with the first solution):

    object(sfOutputEscaperArrayDecorator)[181]
      private 'count' => int 0
      protected 'value' => 
        array
          empty
      protected 'escapingMethod' => string 'esc_specialchars' (length=16)
    

    And with the second:

    array
      empty
    

    For example, if you have some filters define, you will get :

    array
      'model' => 
        array
          'text' => string 'test' (length=4)
      'updated_at' => 
        array
          'from' => null
          'to' => null
      'created_at' => 
        array
          'from' => null
          'to' => null