views:

909

answers:

3

Hi,

I am making use of the admin generator in Symfony 1.4, which is 99% working as I want it to simply using the options in the generator.yml config file.

The problem I have is that I wish to specify which columns are displayed in the list view using a filter (as opposed to configuring them in generator.yml).

I have successfully created a partial to display the additional column filter options, but I can't see a way of using the value of this filter to change which columns are displayed in the list. I can see how I would be able to use the value to filter the criteria of the database query, but I can't work out how I could use this method to restrict the display of certain columns.

If anyone has any ideas or could point me in the right direction it would be most appreciated, as I've been chasing my tail all day! Smile

Many thanks.

Regards,

Matt.

A: 

I suppose that you should create your own generator to implement such logic.

Darmen
+1  A: 

Hi, i did this last week and was also a bit lost at first. I can tell you what i did, and you adapt to your solution. In my solution i had a module called Notifications, with the following fields: id, content, state, sent_at, instance, created_at. In my problem notifications could be sent to a list of users, so the users are foreing data, not saved in the notification table (1 to n relation). So when you Edit o create a notification i only show a few fields of the notification (like it content, and the instance) the other fields like the date are not editable (my choice). So in the creation i show only 2 of the 6 fields of the class. And also i want to see a list of users to multi-select, so i use a partial located in the template folder called "_get_all_users_list.php" that has a criteria and returns a multi-select tag.

_get_all_users_list.php file

<?php

    echo select_tag('users', objects_for_select( UserPeer::doSelect(new        Criteria),"getId","getUsername")
          ,array( "multiple"=>1, "size"=>15)  );
?>

And to be able to show the list of users in the generator.yml i do this:

generator:

class: sfPropelAdminGenerator param: model_class: Notification theme: default

list:
  title:          Notificaciones
  max_per_page:   20
  display:        [ id, content, state, sent_at, sknow_instance, created_at]
  object_actions:
    _edit:         ~
    _delete:       ~
  batch_actions:
    _delete:       ~

edit:
  title:          Modificar notificacion:
  display:        [content, sknow_instance, _getAllUsersList ]
  fields:
    content:              { name: Notification content, type: textarea_tag, params: size=80x5 }
    get_all_users_list:   { name: Select one or more users to be notified}

as you see in the "display:" area of the generator.yml on edit, you can declare the fields you want to be editable/enabled on creation, and if you want to include a partial, you must declare it with a _ in the begining and in the camelCase format, and the name of the partial must start with a _ and the name must be separated by _ .

I found very useful this chapter of symfony's book Admin Generator

i hope it help, and sorry for my english.

Luciano
A: 

Thanks for your suggestions. The problem I was having was accessing the value of my custom filter so that I could use it to change the columns displayed. In the end, this is how I achieved it:

In my FormFilter class, I configured my custom widget and added a validator. This forced me to add a method (add%sColumnQuery) to process the value, in which I added the value to the user object in order to store it for retrieval elsewhere (usually this method would be used to alter the query). See below:

class ExpenditureFormFilter extends BaseExpenditureFormFilter
{
  public function configure()
  {
    parent::configure();

    $years = range((int)date("Y", strtotime(sfConfig::get('app_view_min_period'))), (int)date("Y", strtotime(sfConfig::get('app_view_max_period'))));

    $this->widgetSchema['start_period'] = new sfWidgetFormDate(array(
      'format'        => '%month%/%year%',
      'can_be_empty'  => false,
      'months'        => array_combine(range(1, 12), array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')),
      'years'         => array_combine($years, $years)
    ));

    $this->widgetSchema->setLabels(array(
      'start_period'  => 'Start period',
    ));

    $this->validatorSchema['start_period'] = new sfValidatorMonthYear(
      array(
        'min' => strtotime(sfConfig::get('app_view_min_period')),
        'max' => strtotime(sfConfig::get('app_view_main_period'))
      ),
      array(
        'required' => true
      )
    );
  }

  public function getFields()
  {
    $fields = parent::getFields();
    $fields['start_period'] = 'Date';
    return $fields;
  }

  protected function addStartPeriodColumnQuery($query, $field, $value)
  {
    sfContext::getInstance()->getUser()->setAttribute('start_period', $value);
  }
}

I then overrode the auto-generated _list_td_tabular.php partial (easily done just by creating a copy with the same name in the modules template directory), and within this I was able to access the user object, and therefore my filter value like so:

$start_period = sfContext::getInstance()->getUser()->getAttribute('start_period');

So now I am able to use this value to dynamically generate the columns to display in the list template - mission accomplished :)

I would love to know if there is a better way of course, but this works for me!

Regards,

Matt.

shiddot