Code snippets for symfony 1.x

Navigation

Refine Tags

Snippets tagged "filter adminchecklist"

Filter item in admin_check_list using admin generator.

Hi, this is my first snippet (in my first Symfony project), so I hope this is useful and not boring for you. I show you my code to solve a problem: how to filter an admin_check_list using a criteria.

In my project I have in the schema also this tables:

  role:
    type:                { primaryKey: true, type: char }
    descritpion:         { type: varchar(50), required: true }
  user:
    id:
    username:            { type: varchar(100), index: unique, required: true }
    role_type:           { type: char, foreignTable: role, foreignReference: type, required: true }
  course:
    id:
    name:                { type: varchar(255), required: true }
    research_proposal:   { type: longvarchar }
  course_teacher:
    course_id:
    user_id:
 

Little explanation: Each user have a role. Each course can have more teacher; each teacher is an user with "T" role_type.

In the generator.yml I can write

edit:
  fields:
    course_teachers:         { type: admin_check_list, params: through_class=CourseTeacher }
 

then in the course creation page I have a list of all users (and not a list of the teacher).

To reach my scope I need to override the ObjectAdminHelper. I create a file named ObjectAdminHelper.php and put it into the /lib/helper folder of my Symfony folder. To override successfully an helper it is necessary to overwrite (or write) all its method. So just copy all from the original ObjectAdminHelper.php file and paste it into this new file. The only method we need to override is "_get_object_list($object, $method, $options, $callback)": it use by default the method "_get_propel_object_list" to get the list to show... But if we would like to modify that list how can we do?

function _get_object_list($object, $method, $options, $callback){
  $object = get_class($object) == 'sfOutputEscaperObjectDecorator' ? $object->getRawValue() : $object;
  // the default callback is the propel one
  if (!$callback) {
    $callback = _get_option($options, 'callback');
    if (!$callback) {
        $callback = '_get_propel_object_list';
    }
  }
  return call_user_func($callback, $object, $method, $options);
}
 

This method get an option called "callback" to get the name of the callback method. If a callback method is not present it use the standard "_get_propel_object_list".

So if I would like to get only the teacher (role_type="T") i can write this function (very similar than _get_propel_object_list):

function _get_teacher_from_users($object, $method, $options){
    $criteria = new Criteria();
    $criteria->add(UserPeer::ROLE_TYPE, "T");
    $through_class = _get_option($options, 'through_class');
    $objects = sfPropelManyToMany::getAllObjects($object, $through_class, $criteria);
    $objects_associated = sfPropelManyToMany::getRelatedObjects($object, $through_class, $criteria);
    $ids = array_map(create_function('$o', 'return $o->getPrimaryKey();'), $objects_associated);
    return array($objects, $objects_associated, $ids);
}
 

Now you can write all the function you need to filter the lists.

Last step: how can my generator.yml say to the helper which method it must use? Simply modify the course_teacher row:

edit:
  fields:
    course_teachers:         { type: admin_check_list, params: through_class=CourseTeacher callback=_get_teacher_from_users }
 

I hope this is useful for you (and sorry for my english).

Pierpaolo Cira

by Pierpaolo Cira on 2008-01-21, tagged adminchecklist  filter  helper