Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Data Model Questions /

Filter Model Admin to only show certain records


Reply


4 Posts   532 Views

Avatar
Optic Blaze

Community Member, 165 Posts

10 January 2014 at 4:06am

Hi,

I have extended the Member Class so that i can have a group called mymembers.
I am making use of Model Admin and setting up permissions so that non-admins can view the info, but cant change it.
How do i filter the Model Admin to display on the group called 'mymembers' and not show all other members and all other users?

At the moment i have this:

<?php
class MemberAdmin extends ModelAdmin {
static $managed_models = array('Member');
static $url_segment = 'memberadmin';
static $menu_title = 'Member Admin';

// Set Fields for export
public function getExportFields() {
return array(
'FirstName' => 'FirstName',
'Surname' => 'Surname',
'CompanyName' => 'CompanyName',
         'CompanyTel' => 'CompanyTel',
         'CompanyFax'=> 'CompanyFax',
         'CompanyAddress'=> 'CompanyAddress',
         'CompanyEmail'=> 'CompanyEmail',
         'CompanyWebsite'=> 'CompanyWebsite',
         'CompanyCEO'=> 'CompanyCEO',
         'IndustryGroup'=> 'IndustryGroup',
         'NumberEmployees'=> 'NumberEmployees',
         'Newsletter'=> 'Newsletter',
         'Sectors'=> 'Sectors'
);
}
   
};

Avatar
swaiba

Forum Moderator, 1805 Posts

10 January 2014 at 6:45am

Hi,

Probably not the best and certainly this code isn't tested, but hope this helps...

class MemberAdmin extends ModelAdmin {
   ...
   static $collection_controller_class = "MemberAdmin_CollectionController";
   ...
}

class MemberAdmin_CollectionController extends ModelAdmin_CollectionController {

   function modifySearchQuery($query,$searchCriteria){

      if ($this->modelClass == 'Member'){
         //do some complex joinging sql here
         $query->where[] = '
            EXISTS (
               SELECT 1
               FROM Group_Members g_m, Group g
               WHERE g_m.MemberID = Member.ID
                  AND g.ID = g_m.GroupID
                  AND g.Title = 'mymembers'
            )
         ';
      }

      return $query;
   }

Avatar
martimiz

Forum Moderator, 1132 Posts

10 January 2014 at 7:04am

I'm not sure if one should use the SearchContext in some way - because the actua DataList before filtering is retreived in SearchContext::getResult()... But it looks awfully complex. II suppose you could filter the list afterwards by doing something like this in your modeladmin:

   public function getList() {
      $list = parent::getList(); //<-- takes care of custom filters, but gets all members
      if ($this->modelClass == 'Member') {
         $list = $list
            ->innerjoin('Group_Members', 'Member.ID = Group_Members.MemberID')
            ->innerjoin('Group', 'Group_Members.GroupID = Group.ID')
            ->where("Group.Code = 'content-authors'");
      }
      return $list;
   }

You'd still have to make very very sure that an administrator isn't also a mymember, because that would be exposed as well... Tricky...

Avatar
Optic Blaze

Community Member, 165 Posts

10 January 2014 at 9:14pm

Thanks swabia & martimiz that worked really well.
I am not too concerned regarding the admins because they are not going to be part of the group.