symfony - admin module filters accessible as links

妖精的绣舞 提交于 2019-12-02 07:29:00

I truly don't think you should use filters in your case. Filters are temporary conditions to your data list. Here's a more elegant solution. We will reuse the functionality of sfGuardUser index action, and set its table_method "on the fly" (based on url).

//exetent the configuration class to override getTable method
class sfGuardUserGeneratorConfiguration extends BaseSfGuardUserGeneratorConfiguration
{
  protected $tableMethod = null;

  public function setTableMethod($name)
  {
    $this->tableMethod = $name;
  }

  public function getTableMethod()
  {
    return null !== $this->tableMethod ? $this->tableMethod : parent::getTableMethod();
  }
}

//now we need to set the tableMethod based on a route param (list):
class sfGuardUserActions extends autoSfGuardUserActions
{
  public function executeIndex(sfWebRequest $request)
  {
    //create a mapping between an url and table method
    $map = array(
      'clients' => 'getClientsList',
      'suppliers' => 'getSuppliersList',
      'manufacturers' => 'getManufacturersList',
    );
    $list = $request->getParameter('list');
    $table_method = isset($map[$list]) ? $map[$list] : null;
    $this->configuration->setTableMethod($table_method);
    parent::executeIndex($request);
  }
}

//create a custom url for your lists:
sf_guard_user_list:
  url:   /guard/users/:list
  param: { module: sfGuardUser, action: index}
  requirements:
    list: clients|suppliers|manufacturers

//and model methods for each of your lists:
class sfGuardUserTable extends PluginsfGuardUserTable
{
  /**
   * List of clients query
   *
   */
  public function getClientsList()
  {
    $q = $this->createQuery('u')
      ->leftJoin('u.Groups g')
      ->where('g.name = ?', 'client');

    return $q;
  }
  //and others
}

That's it. Now you can add links to your dashboard like this:

<?php echo link_to('Clients', 'sf_guard_user_list', array('list'=>'clients')) ?>

P.S. this approach now allows you to use filters (for their true reasons) on top of these lists. But, you will also have to adjust the appropriate links.

Here is one issue: Admin generator remembers all filters data as user's attribute (read as "stores this information in session").

So if you dont care about all remembered data of filters, you may create one module that recieve GET parameters, set this parameters as user attribute (sfUser->setAttribute(...)) overriding filters data of needed module, and redirects to module.

Or

You may use GET parameters adding them to an module URL (example.com/users?filter[group_id]=123) that overrides filter params. At this case you should handle this information in each needed module.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!