Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Filter Model Admin to only show certain records

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 397 Views
  • Optic Blaze
    Avatar
    Community Member
    162 Posts

    Filter Model Admin to only show certain records Link to this post

    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'
    );
    }
       
    };

  • swaiba
    Avatar
    Forum Moderator
    1792 Posts

    Re: Filter Model Admin to only show certain records Link to this post

    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;
       }

  • martimiz
    Avatar
    Forum Moderator
    1086 Posts

    Re: Filter Model Admin to only show certain records Link to this post

    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...

  • Optic Blaze
    Avatar
    Community Member
    162 Posts

    Re: Filter Model Admin to only show certain records Link to this post

    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.

    397 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.