Development

#6731 (Admin Generator - add parameter to allow any displayed field to be sortable)

You must first sign up to be able to contribute.

Ticket #6731 (closed enhancement: wontfix)

Opened 7 months ago

Last modified 3 months ago

Admin Generator - add parameter to allow any displayed field to be sortable

Reported by: caponica Assigned to: fabien
Priority: minor Milestone:
Component: generator Version: 1.2.8
Keywords: admin generator field sort Cc:
Qualification: Unreviewed

Description

The problem:

Admin generator fields that are not part of the main object are not sortable.

Example: if I have a translated Entity with a [Doctrine] schema like this:

Entity:
  actAs:
    I18n:
      fields: [longname]
  columns:
    id:
      type:                    integer(10)
      primary:                 true
      autoincrement:           true
    longname:                  string(60)

I can create a table_method that efficiently pulls in the longname field, but I cannot easily sort the results based on the longname. The table_method would have a line looking like this:

  public function retrieveBackendEntityList(Doctrine_Query $q)
  {
    $rootAlias = $q->getRootAlias();
    $q->leftJoin($rootAlias . '.Translation ct WITH (ct.lang = ?)', 'en_GB');
    return $q;
  }

Which means that we know the table alias of the joined table (ct) and we know the field we want to sort by (longname), which leads me to...

The solution

I propose adding a "sort_field" parameter at the "fields" level in the generator.yml files. This would allow you to say "If I click this column header, I want to sort the results by %sort_field%".

This would give you a generator.yml that looks something like this:

...
    config:
      fields:
        longname:
          sort_field:  ct.longname
...

This would then be simply implemented by changing how the headers in _list_th_tabular are rendered

In this example the longname header would change FROM:

<th class="sf_admin_text sf_admin_list_th_longname">
  <?php echo __('Longname', array(), 'messages') ?>
</th>

TO:

<th class="sf_admin_text sf_admin_list_th_longname">
  <?php if ('ct.longname' == $sort[0]): ?>
    <?php echo link_to(__('Longname', array(), 'messages'), '@category_category?sort=ct.longname&sort_type='.($sort[1] == 'asc' ? 'desc' : 'asc')) ?>
    <?php echo image_tag(sfConfig::get('sf_admin_module_web_dir').'/images/'.$sort[1].'.png', array('alt' => __($sort[1], array(), 'sf_admin'), 'title' => __($sort[1], array(), 'sf_admin'))) ?>
  <?php else: ?>
    <?php echo link_to(__('Longname', array(), 'messages'), '@category_category?sort=ct.longname&sort_type=asc') ?>
  <?php endif; ?>
</th>

Which is effectively the same as a "normal" column with native sort-ability (but using the sort_field value instead of the field's own name:

<th class="sf_admin_text sf_admin_list_th_id">
  <?php if ('id' == $sort[0]): ?>
    <?php echo link_to(__('Id', array(), 'messages'), '@category_category?sort=id&sort_type='.($sort[1] == 'asc' ? 'desc' : 'asc')) ?>
    <?php echo image_tag(sfConfig::get('sf_admin_module_web_dir').'/images/'.$sort[1].'.png', array('alt' => __($sort[1], array(), 'sf_admin'), 'title' => __($sort[1], array(), 'sf_admin'))) ?>
  <?php else: ?>
    <?php echo link_to(__('Id', array(), 'messages'), '@category_category?sort=id&sort_type=asc') ?>
  <?php endif; ?>
</th>

I have manually changed the code in _list_th_tabular and it all seems to work as expected. (Although I have not yet fully tested my 'solution' to the problem yet)

However, changing the admin generator to do this automatically is beyond my symfony knowledge, so I leave it to you who are wiser and more experienced to decide if this is a worthy need and then code it in accordingly.

I eagerly anticipate the fruits of your labours! (Largely because I don't want to manage lots of manually created list headers....)

Yours,

C

Attachments

_list_th_tabular.php (1.4 kB) - added by caponica on 07/24/09 15:27:29.
New version of _list_th_tabular.php to allow user-defined sorting

Change History

07/24/09 15:27:29 changed by caponica

  • attachment _list_th_tabular.php added.

New version of _list_th_tabular.php to allow user-defined sorting

07/24/09 15:31:38 changed by caponica

  • version changed from 1.2.7 to 1.2.8.

OK, I've taken matters into my own hands and I (think that!) I have coded a solution for this. Please review the attached file and then comment here or incorporate it into the core as appropriate.

This file should replace the ones at:

symfony-1.2.8\lib\plugins\sfDoctrinePlugin\data\generator\sfDoctrineModule\admin\template\templates

and

symfony-1.2.8\lib\plugins\sfPropelPlugin\data\generator\sfPropelModule\admin\template\templates

Could this be my first contribution to the symfony core?!? I think it's a very useful and simple functionality to add!

C

09/01/09 16:20:53 changed by kingpin393

Nice patch - works great!

+ for adding to core

09/24/09 17:43:50 changed by simone.fumagalli

I've tried your patch and to make it works I had to change the method "isValidSortColumn".

Normally the method check if the column exists in the table and of course in your case longname doesn't exists in table Entity

If you change the method and make it always return true sorting works. I don't like my workaround that but I just wanted to let you know for the moment.

Here the example for Entity table


class entityActions extends autoEntityActions
{

  protected function isValidSortColumn($column)
  {
    return true;
  }

}

11/24/09 10:43:26 changed by tirengarfio

Hi,

after replacing _list_th_tabular.php, i get this error:

"Couldn't find class ct"

Any idea?

Javi

11/24/09 11:44:27 changed by caponica

Hi Javier,

Definitely a question for the forum and not the trac ticket!

Anyway, the answer is that you need to ensure that your "sort_field" (as defined in generator.yml) is one which is referenced by your table_method (also in generator.yml, this defines the method that builds the query and dataset for your admin page). In your case you are using "ct" as a table name, but this is not present in your table query - so you are getting an error.

If you have any more questions about this, just send me an email.

C

11/24/09 13:49:55 changed by Kris.Wallsmith

  • status changed from new to closed.
  • resolution set to wontfix.

I'm afraid this enhancement won't make it into either 1.3 or 1.4.

The Sensio Labs Network

Since 1998, Sensio Labs has been promoting the Open-Source software movement by providing quality web application development, training, consulting.
Sensio Labs also supports several large Open-Source projects.