Move to sane directory structure. Don't make 'cms' the top level of the silverstripe...
[silverstripe-elijah.git] / silverstripe-gsoc / cms / code / MemberList.php
blobe26f06093977d4975f601b326136676ac169dded
1 <?php
3 class MemberList extends FormField {
4 protected $members;
5 protected $hidePassword;
6 protected $pageSize;
8 function __construct($name, $group, $members = null, $hidePassword = null, $pageLimit = 10) {
9 if($group) {
10 if(is_object($group)) $this->group = $group;
11 else $this->group = DataObject::get_by_id('Group',$group);
14 $this->pageSize = $pageLimit;
16 if($members) $this->members = $this->memberListWithGroupID($members, $group);
18 $this->hidePassword = $hidePassword;
20 parent::__construct(null);
23 function memberListWithGroupID($members, $group) {
24 $newMembers = new DataObjectSet();
25 foreach($members as $member) {
26 $newMembers->push($member->customise(array("GroupID" => $group->ID)));
28 return $newMembers;
31 function FieldHolder() {
32 return $this->renderWith("MemberList");
34 function setGroup($group) {
35 $this->group = $group;
37 function setController($controller) {
38 $this->controller = $controller;
41 function GroupID() {
42 if( $this->group )
43 return $this->group->ID;
45 return '0';
48 function GetControllerName() {
49 return $this->controller->class;
51 function Members() {
52 /*if($this->members)
53 $members = $this->members;
54 else if($this->group) {
55 $members = $this->group->Members( $this->pageSize, 0 );
56 $allMembers = $this->group->Members();
58 if( $allMembers )
59 $total = $allMembers->Count();
60 } else
61 return null;*/
63 if( $this->members )
64 return $this->members;
66 if( !$baseGroup )
67 $baseGroup = $this->group->ID;
69 // Debug::message( $_REQUEST['MemberListOrderByField'] );
71 // construct the filter and sort
73 if( $_REQUEST['MemberListOrderByField'] )
74 $sort = "`" . $_REQUEST['MemberListOrderByField'] . "`" . addslashes( $_REQUEST['MemberListOrderByOrder'] );
76 $whereClauses = array();
78 $search = addslashes( $_REQUEST['MemberListSearch'] );
80 if( is_numeric( $_REQUEST['MemberListStart'] ) )
81 $limitClause = ( $_REQUEST['MemberListStart'] ) . ", {$this->pageSize}";
82 else
83 $limitClause = "0, {$this->pageSize}";
86 if( !empty($_REQUEST['MemberListSearch']) )
87 $whereClauses[] = "( `Email`='$search' OR `FirstName`='$search' OR `Surname`='$search' )";
89 if( is_numeric( $_REQUEST['MemberListBaseGroup'] ) )
90 $baseGroup = $_REQUEST['MemberListBaseGroup'];
92 $whereClauses[] = "`GroupID`='".$baseGroup."'";
93 $join = "INNER JOIN `Group_Members` ON `MemberID`=`Member`.`ID`";
95 // $_REQUEST['showqueries'] = 1;
97 $members = DataObject::get('Member', implode( ' AND ', $whereClauses ), $sort, $join, $limitClause );
99 // $_REQUEST['showqueries'] = 0;
101 if( is_numeric( $_REQUEST['MemberListGroup'] ) ) {
102 $baseMembers = new DataObjectSet();
104 if( $members )
105 foreach( $members as $member )
106 if( $member->inGroup( $_REQUEST['MemberListGroup'] ) )
107 $baseMembers->push( $member );
108 } else
109 $baseMembers = $members;
111 if($members){
112 $members->setPageLimits( $_REQUEST['MemberListStart'], $this->pageSize, $total );
114 $this->members = $this->memberListWithGroupID($members, $this->group);
116 return $this->members;
119 function FirstLink() {
120 if( !$_REQUEST['MemberListStart'] )
121 return null;
123 return "admin/security/listmembers?MemberListStart=0{$this->filterString()}";
126 function filterString() {
128 foreach( $_REQUEST as $key => $value ) {
129 if( strpos( $key, 'MemberList' ) === 0 && $key != 'MemberListStart' )
130 $filterString .= "&$key=$value";
133 if( !$_REQUEST['MemberListBaseGroup'] )
134 $filterString .= '&MemberListBaseGroup=' . $this->group->ID;
136 return $filterString;
139 function MemberListStart() {
140 return $_REQUEST['MemberListStart'];
143 function PrevLink() {
144 if( !$_REQUEST['MemberListStart'] )
145 return null;
147 $prevStart = $_REQUEST['MemberListStart'] - $this->pageSize;
149 if( $prevStart < 0 )
150 $prevStart = 0;
152 return "admin/security/listmembers?MemberListStart=$prevStart{$this->filterString()}";
155 function NextLink() {
156 $total = $this->TotalMembers();
158 $lastStart = $total - ( $total % $this->pageSize );
160 if( $_REQUEST['MemberListStart'] >= $lastStart )
161 return null;
163 return "admin/security/listmembers?MemberListStart={$this->pageSize}{$this->filterString()}";
166 function LastLink() {
168 $total = $this->TotalMembers();
170 $lastStart = $total - ( $total % $this->pageSize );
172 if( $_REQUEST['MemberListStart'] >= $lastStart )
173 return null;
175 return "admin/security/listmembers?MemberListStart=$lastStart{$this->filterString()}";
178 function PageSize() {
179 return $this->pageSize;
182 function FirstMember() {
183 return $_REQUEST['MemberListStart'] + 1;
186 function LastMember() {
187 return $_REQUEST['MemberListStart'] + min( $_REQUEST['MemberListStart'] + $this->pageSize, $this->TotalMembers() - $_REQUEST['MemberListStart'] );
190 function TotalMembers() {
191 if($this->group) $members = $this->group->Members();
193 if( !$members )
194 return null;
196 return $members->TotalItems();
199 function DontShowPassword(){
200 if( $this->hidePassword )
201 return true;
203 return $this->controller->class=='CMSMain'||$this->controller->class=='NewsletterAdmin';
206 function AddRecordForm() {
207 if($this->DontShowPassword())
209 return new TabularStyle(new Form($this->controller,'AddRecordForm',
210 new FieldSet(
211 new TextField("FirstName", "First Name"),
212 new TextField("Surname", "Surname"),
213 new TextField("Email", "Email"),
214 new HiddenField("GroupID", null, $this->group->ID)
216 new FieldSet(
217 new FormAction("addmember", "Add")
221 } else {
222 return new TabularStyle(new Form($this->controller,'AddRecordForm',
223 new FieldSet(
224 new TextField("FirstName", "First Name"),
225 new TextField("Surname", "Surname"),
226 new TextField("Email", "Email"),
227 new TextField("Password", "Password"),
228 new HiddenField("GroupID", null, $this->group->ID)
230 new FieldSet(
231 new FormAction("addmember", "Add")
237 function SearchField() {
238 $field = new TextField( 'MemberListSearch', 'Search' );
239 return $field->FieldHolder();
242 function OrderByField() {
243 $fields = new FieldGroup( new DropdownField('MemberListOrderByField','', array(
244 'FirstName' => 'FirstName',
245 'Surname' => 'Surname',
246 'Email' => 'Email'
248 new DropdownField('MemberListOrderByOrder','',array(
249 'ASC' => 'Ascending',
250 'DESC' => 'Descending'
251 )));
253 $field = new FieldGroup( new LabelField( 'Order by' ), $fields );
254 return $field->FieldHolder();
257 function GroupFilter() {
259 $groups = DataObject::get('Group');
261 $groupArray = array( '' => 'Any group' );
263 foreach( $groups as $group )
264 $groupArray[$group->ID] = $group->Title;
266 $field = new DropdownField('MemberListGroup','Filter by group',$groupArray );
267 return $field->FieldHolder();