views:

635

answers:

3

When using sfDoctrineGuard plugin, it automatically generates the backend administration functionality where I can edit users of the system and assign them permissions.

So I visit http://.../backend_dev.php/sf_guard_user/:id/edit where I am presented with the user's information including the available permissions to select.

By default the permissions are shown as a multiple select box, HTML follows:

<select name="sf_guard_user[permissions_list][]" multiple="multiple" id="sf_guard_user_permissions_list">
  <option value="1">Permission1</option>
  <option value="2">Permission2</option>
  <option value="3">Permission3</option>
  <option value="4">Permission4</option>
</select>

What I would prefer is a list of checkboxes. So I searched around and found that if I add the option "expanded" set to true to the following code:

'permissions_list' => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'sfGuardPermission', 'expanded' => true,)),

The code is part of this file: lib/form/doctrine/sfDoctrineGuardPlugin/base/BasesfGuardUserForm.class.php. I don't think I should've edited this file (potential for changes to be overwritten should sfDoctrineGuard ever be re-installed) but couldn't think of another way to make it work.

The generated HTML is as follows:

<ul class="checkbox_list">
 <li><input name="sf_guard_user[permissions_list][]" type="checkbox" value="1" id="sf_guard_user_permissions_list_1" />&nbsp;<label for="sf_guard_user_permissions_list_1">Permission1</label></li>
 <li><input name="sf_guard_user[permissions_list][]" type="checkbox" value="2" id="sf_guard_user_permissions_list_2" />&nbsp;<label for="sf_guard_user_permissions_list_2">Permission2</label></li>
 <li><input name="sf_guard_user[permissions_list][]" type="checkbox" value="3" id="sf_guard_user_permissions_list_3" />&nbsp;<label for="sf_guard_user_permissions_list_3">Permission3</label></li>
 <li><input name="sf_guard_user[permissions_list][]" type="checkbox" value="4" id="sf_guard_user_permissions_list_4" />&nbsp;<label for="sf_guard_user_permissions_list_4">Permission4</label></li>
</ul>

What I need to do now is split up the permissions based on their prefix. For example if I had permissions named user_action1, user_action2, file_action1, file_action2, they would display like:

User
checkbox  (custom label) Action One
checkbox                 Action Two

File
checkbox  (custom label) Action One
checkbox                 Action Two

but have no idea where to start with this. It would be easy if there was a template to edit but since I'm dealing with the Forms framework it is my understanding that the templates are generated on the fly - I can see them in my symonfy cache folder.

How would I go about this?

I started writing my own sfWidgetFormDoctrineChoicePermission class that extends the same class as sfWidgetFormDoctrineChoice but am struggling to edit the rendering functions correctly for the desired output. Is this the correct way to go about this work?

I also need to integrate my sfGuardUserProfile model into the edit user page (same as above), I read somwhere that editing the generator.yml file for the sfGuardUser plugin module and simply adding the field names from the sfGuardUserProfile table would make it work, but sadly it doesn't.

Any help with any of my queries would be appreciated.

Thanks

A: 

First of all don't ever edit base classes. The one you are wanting to edit is:

lib/form/doctrine/sfDoctrineGuardPlugin/sfGuardUserForm.class.php

From here you can override the default widget by calling:

$this->setWidget('permissions_list', new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'sfGuardPermission', 'expanded' => true)));

You should be creating your own widget and exdending it from sfWidgetFormDoctrineChoice is the best start. Basically it will return html as php strings which will get echoed. It is simple enough to get your head around eventually.

To include your profile form when editing your main user form it is very simple, in the same sfGuardUserForm class:

$this->embedForm('profile', new sfGuardUserProfileForm($this->getObject->NAME_OF_PROFILE_RELATIONSHIP));

That last call is off the top of my head so the syntax might be wrong so check it out, also replace the relationship with the name of your profile relationship of course.

johnwards
A: 

My changes to lib/form/doctrine/sfDoctrineGuardPlugin/sfGuardUserForm.class.php below.

class sfGuardUserForm extends PluginsfGuardUserForm
{
  public function configure()
  {
    $this->setWidget('permissions_list', new sfWidgetFormDoctrineChoicePermission());
  }
}

This isn't working as expected (permissions list showing as a select option menu), I thought perhaps I need to make the changes as below:

class sfGuardUserForm extends PluginsfGuardUserForm
{
  public function configure()
  {

  }

  public function setup()
  {
    parent::setup();
    $this->setWidget('permissions_list', new sfWidgetFormDoctrineChoicePermission());
  }
}

but this doesn't seem to be working either.

I think the problem is to do with the fact that sfGuardUserForm extends PluginsfGuardUserForm instead of extending BasesfGuardUserForm

It works when I edit BaseProfileForm but as you correctly said I shouldn't be editing the Base classes.

Any help would be appreciated, thanks.

Steve Mac
A: 

First off, you have to put the new configure() in UserAdmin form (backend), not the regular User from (frontend).

Then, your configure() method will have the new widget declaration:

$this->setWidget('permission_list', new sfWidgetFormDoctrineChoice(array( 'muliple' => true, // this makes checkboxes (default=false=radio) 'model' => sfGuardPermission, 'expanded' => true // changes from select menu -> radio or check (based on 'multiple' setting) )));

Good luck:)

Emre