forked from OpenStackweb/openstack-org
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemberListPage.php
More file actions
590 lines (443 loc) · 19.7 KB
/
Copy pathMemberListPage.php
File metadata and controls
590 lines (443 loc) · 19.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
<?php
/**
* Copyright 2014 Openstack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
class MemberListPage extends Page
{
static $db = array();
static $has_one = array();
static $has_many = array();
}
class MemberListPage_Controller extends Page_Controller
{
function init()
{
parent::init();
//CSS
Requirements::css("themes/openstack/css/jquery.autocomplete.css");
Requirements::javascript("themes/openstack/javascript/jquery.autocomplete.min.js");
Requirements::CustomScript("
jQuery(function(){
$('#SearchForm_MemberSearchForm_mq').autocomplete('" . $this->Link('results') . "', {
minChars: 3,
selectFirst: true,
autoFill: true,
});
$('#SearchForm_MemberSearchForm_mq').focus();
});
");
}
static $allowed_actions = array(
'confirmNomination',
'CurrentElection',
'saveNomination',
'profile',
'profileRedirect',
'results',
'MemberSearchForm',
'candidateStats',
'CandidateApplication',
'idList' => 'ADMIN',
'assignGroup' => 'ADMIN',
'ListExport',
'CSVExport' => 'ADMIN'
);
private static $url_handlers = array(
'profile/$MemberID!/$NameSlug!' => 'profile',
'profile/$MemberID!' => 'profileRedirect',
);
function MemberList()
{
if (isset($_GET['letter'])) {
$requestedLetter = Convert::raw2xml($_GET['letter']);
if ($requestedLetter == 'intl') {
$likeString = "NOT Surname REGEXP '[A-Za-z0-9]'";
} elseif (ctype_alpha($requestedLetter)) {
$likeString = "Surname LIKE '" . substr($requestedLetter, 0, 1) . "%'";
} else {
$likeString = "Surname LIKE 'a%'";
}
} else {
$likeString = "Surname LIKE 'a%'";
}
$list = Member::get()
->where("Group_Members.GroupID = 5 AND " . $likeString)
->leftJoin('Group_Members', 'Member.ID = Group_Members.MemberID')
->sort('Surname');
return GroupedList::create($list);
}
// Return the currently running election
// This simple function primarily exists to be used in the template
function CurrentElection()
{
// Load the election system
$Elections = ElectionSystem::get()->first();
if ($Elections && $Elections->CurrentElectionID != 0) {
return $Elections->CurrentElection();
}
}
function alreadyNominated($candidateID, $CurrentElection)
{
$memberID = Member::currentUserID();
$NominationsForThisCandidate = $CurrentElection->CandidateNominations("`MemberID` = " . $memberID . " AND `CandidateID` = " . $candidateID);
if ($NominationsForThisCandidate->Count() >= 1) {
return true;
}
}
function findMember($member_id)
{
$member = Member::get()->byID(intval($member_id));
if(!is_null($member))
{
// Check to make sure they are in the foundation membership group
If ($member->inGroup(5, true))
{
return $member;
}
}
}
function confirmNomination()
{
$results = array();
$results["Success"] = false;
// Grab candidate ID from the URL
$CandidateID = $this->request->param("ID");
// Check to see if the candidate ID is numeric and if the person is logged in
if ($this->validateNomation($CandidateID) == 'VALID') {
$Nominee = Member::get()->filter(array('ID' => $CandidateID))->first();
$results["Success"] = true;
$results["Candidate"] = $Nominee;
$results["NominateLink"] = $this->Link() . "saveNomination/" . $CandidateID;
$results["BackLink"] = $this->Link() . "profile/" . $CandidateID . '/' . $Nominee->getNameSlug();
} elseif ($this->validateNomation($CandidateID) == 'ALREADY NOMINATED') {
$Nominee = Member::get()->filter(array('ID' => $CandidateID))->first();
$CurrentElection = $this->CurrentElection();
$results["Election"] = $CurrentElection;
$results["Success"] = false;
$results["NominatedByMe"] = true;
$results["Candidate"] = $Nominee;
$results["BackLink"] = $this->Link() . "profile/" . $CandidateID . '/' . $Nominee->getNameSlug();
} elseif ($this->validateNomation($CandidateID) == 'LIMIT EXCEEDED') {
$Nominee = Member::get()->filter(array('ID' => $CandidateID))->first();
$results["Success"] = false;
$results["LimitExceeded"] = true;
$results["Candidate"] = $Nominee;
$results["BackLink"] = $this->Link() . "profile/" . $CandidateID . '/' . $Nominee->getNameSlug();
} else {
$results["Success"] = false;
$results["BackLink"] = $this->Link() . "profile/" . $CandidateID;
}
return $results;
}
// Checks whether a nomination is valid:
// Requires there to be a current election, the member to be logged in, and the ID of a member that hasn't been nominated yet.
function validateNomation($CandidateID)
{
$CurrentElection = $this->CurrentElection();
// Look for nominations for this candidate
$CandidateNominations = CandidateNomination::get()->where("CandidateID = " . $CandidateID . " AND ElectionID = " . $CurrentElection->ID);
$NumberOfNominations = 0;
if ($CandidateNominations) {
$NumberOfNominations = $CandidateNominations->Count();
}
// 1. Check to see if there's a current, active election
if (!$CurrentElection || !$CurrentElection->NominationsAreOpen()) {
return 'NO ACTIVE NOMINATIONS';
}
// 2. Check to see if a member is logged in
if (!Member::currentUserID()) {
return 'NOT LOGGED IN';
}
// 3. Check to make sure there's a valid Candidate ID
if (!is_numeric($CandidateID) || !$this->findMember($CandidateID)) {
return 'INVALID CANDIDATE';
}
// 4. Check to see if the member has already nominated this person
if ($this->alreadyNominated($CandidateID, $CurrentElection)) {
return 'ALREADY NOMINATED';
}
// 5. Check to see if this person already has 10 nominations
if ($NumberOfNominations >= 10) {
return 'LIMIT EXCEEDED';
}
// 6. Make sure that the person nominating is a foundation member
$CurrentMember = Member::currentUser();
If (!$CurrentMember->isFoundationMember()) {
return 'INVALID VOTER';
}
// If all of the above tests pass, this is a valid nomination
return 'VALID';
}
function saveNomination()
{
// Grab candidate ID from the URL
$CandidateID = $this->request->param("ID");
$NominationStatus = $this->validateNomation($CandidateID);
// Check to see if this is a valid nomination
if ($NominationStatus == 'VALID') {
// Grab the currently logged in member
$currentMember = Member::currentUser();
$CurrentElection = $this->CurrentElection();
// Record the nomination
$CandidateNomination = new CandidateNomination();
$CandidateNomination->MemberID = Member::currentUserID();
$CandidateNomination->CandidateID = $CandidateID;
$CandidateNomination->ElectionID = $CurrentElection->ID;
$CandidateNomination->write();
// Create a candidate record for the nominations page if one does not exist
$Candidate = Candidate::get()->filter(array(
'MemberID' => $CandidateID,
'ElectionID' => $CurrentElection->ID
))->first();
if (!$Candidate) {
// Create a new candidate entry
$Candidate = new Candidate();
$Candidate->MemberID = $CandidateID;
$Candidate->ElectionID = $CurrentElection->ID;
$Candidate->write();
}
// Log this new candidate
$logLine = $currentMember->FirstName . " " . $currentMember->Surname . " nominated " . $Candidate->Member()->FirstName . " " . $Candidate->Member()->Surname . " (ID " . $Candidate->Member()->ID . ") on " . $CandidateNomination->Created . " \n";
$file = fopen(ASSETS_PATH . '/candidate-nomination-log.txt', 'a');
fwrite($file, $logLine);
fclose($file);
// Email the member
// In dev and testing, send the nomination emails to the person who did the nomination
$To = $currentMember->Email;
// In live mode, send the email to the candidate
if (Director::isLive()) {
$To = $Candidate->Member()->Email;
}
$Subject = "You have been nominated in the " . $CurrentElection->Title;
$email = EmailFactory::getInstance()->buildEmail(CANDIDATE_NOMINATION_FROM_EMAIL, $To, $Subject);
$email->setTemplate('NominationEmail');
// Gather Data to send to template
$data["Candidate"] = $Candidate;
$data["Election"] = $CurrentElection;
$email->populateTemplate($data);
$email->send();
$this->setMessage('Success',
"You've just nominated " . $Candidate->Member()->FirstName . ' for the OpenStack Board.');
$this->redirect($this->Link('candidateStats/' . $Candidate->Member()->ID));
} elseif ($NominationStatus = 'ALREADY NOMINATED') {
$this->setMessage('Error', "Oops, you have already nominated this person.");
$this->redirect($this->Link());
} elseif ($NominationStatus = 'INVALID CANDIDATE') {
$this->setMessage('Error', "Oops, no candidate was selected.");
$this->redirect($this->Link());
} else {
$this->setMessage('Error', "There was an error logging your nomination.");
$this->redirect($this->Link());
}
}
function profileRedirect()
{
$member_id = Convert::raw2sql($this->request->param("MemberID"));
if (is_numeric($member_id)) {
// Check to make sure there's a member with the current id
if ($member = $this->findMember($member_id)) {
return $this->redirect($this->Link('profile/'.$member_id.'/'.$member->getNameSlug()), 301);
}
}
return $this->httpError(404, 'Sorry that member could not be found');
}
//Show the profile of the member using the MemberListPage_profile.ss template
function profile()
{
// Grab member ID from the URL
$MemberID = Convert::raw2sql($this->request->param("MemberID"));
// Check to see if the ID is numeric
if (is_numeric($MemberID)) {
// Check to make sure there's a member with the current id
if ($Profile = $this->findMember($MemberID)) {
$CurrentElection = $this->CurrentElection();
if ($CurrentElection) {
$Candidate = Candidate::get()->filter(array(
'MemberID' => $MemberID,
'ElectionID' => $CurrentElection->ID
))->first();
$data["Candidate"] = $Candidate;
$data["CurrentElection"] = $CurrentElection;
}
$data["Profile"] = $Profile;
// A member is looking at own profile
if (Member::currentUserID() == $MemberID) {
$data["OwnProfile"] = true;
}
//return our $Data to use on the page
return $this->getViewer('profile')->process
(
$this->customise($data)
);
}
}
return $this->httpError(404, 'Sorry that member could not be found');
}
function candidateStats()
{
// Grab candidate ID from the URL
$CandidateID = $this->request->param("ID");
// Check to see if the candidate is valid
if (is_numeric($CandidateID) && $this->findMember($CandidateID)) {
$CurrentElection = $this->CurrentElection();
$Candidate = $CurrentElection->Candidates("MemberID = " . $CandidateID);
$results["Success"] = true;
$results["Candidate"] = $Candidate;
return $results;
} else {
//Member not found
return $this->httpError(404, 'Sorry that candidate could not be found');
}
}
public function MemberSearchForm()
{
$searchField = new TextField('mq', 'Search Member', $this->getSearchQuery());
$searchField->setAttribute("placeholder", "first name, last name or irc nickname");
$fields = new FieldList($searchField);
$form = new SearchForm($this, 'MemberSearchForm', $fields);
$form->setFormAction($this->Link('results'));
return $form;
}
public function results()
{
if ($query = $this->getSearchQuery()) {
// Search for only foundation members (Group 5) against the query.
$filter = "(MATCH (FirstName, Surname) AGAINST ('{$query}')
OR FirstName LIKE '%{$query}%'
OR Surname LIKE '%{$query}%'
OR IRCHandle LIKE '%{$query}%') AND Group_Members.GroupID=5";
$Results = Member::get()
->where($filter)
->leftJoin("Group_Members", "Member.ID = Group_Members.MemberID");
// No Member was found
if (!isset($Results) || $Results->count() == 0) {
return $this->customise($Results);
}
// For AutoComplete
if (Director::is_ajax()) {
$Members = $Results->map('ID', 'Name');
$Suggestions = '';
foreach ($Members as $Member) {
$Suggestions = $Suggestions . $Member . '|' . '1' . "\n";
}
return $Suggestions;
} // For Results Template
else {
$filter = "(MATCH (FirstName, Surname) AGAINST ('{$query}')
OR FirstName LIKE '%{$query}%'
OR Surname LIKE '%{$query}%'
OR IRCHandle LIKE '%{$query}%') AND Group_Members.GroupID=5";
$OneMember = Member::get()
->where($filter)
->leftJoin("Group_Members", "Member.ID = Group_Members.MemberID");
// see if one member exactly matches the search term
if ($OneMember) {
$Results = $OneMember;
}
// If there is only one person with this name, go straight to the resulting profile page
if ($OneMember && $OneMember->Count() == 1) {
$this->redirect($this->Link() . 'profile/' . $OneMember->First()->ID .'/'. $OneMember->First()->getNameSlug());
}
$Output = new ArrayData(array(
'Title' => 'Results',
'Results' => $Results
));
if ($Results->count() == 0) {
$message = $this->getViewer('results')->process($this->customise($Output));
$this->response->setBody($message);
throw new SS_HTTPResponse_Exception($this->response, 404);
}
return $this->customise($Output);
}
}
$this->redirect($this->Link());
}
function getSearchQuery()
{
if ($this->request) {
$query = $this->request->getVar("mq");
if (!empty($query)) {
return Convert::raw2sql($query);
}
return false;
}
}
private function GetMembersInDateRange($StartTime = null, $EndTime = null)
{
$DateRange = "";
if ($StartTime && $EndTime) {
$DateRange = " AND (LastEdited BETWEEN '" . $StartTime . "' AND '" . $EndTime . "')";
} elseif ($EndTime) {
$DateRange = " AND (LastEdited <= '" . $EndTime . "')";
} elseif ($StartTime) {
$DateRange = " AND (LastEdited >= '" . $StartTime . "')";
} else {
$DateRange = "";
}
// Pull Members using a custom db query. This returns a MySQLQuery object
$MemberList = DB::query("
SELECT Member.ID, `FirstName`, `Surname`, `IRCHandle`, `TwitterName`, `Email`, `SecondEmail`, `ThirdEmail`
FROM `Member`
LEFT JOIN `Group_Members` ON `Member`.`ID` = `Group_Members`.`MemberID`
WHERE `Group_Members`.`GroupID`= 5" . $DateRange
);
return $MemberList;
}
function ListExport()
{
if (isset($_GET['token']) && $_GET['token'] == "fcv4x7Nl8v") {
// Check URL parameters for start and end times
$Start = isset($_GET['start']) ? $_GET['start'] : null;
$End = isset($_GET['end']) ? $_GET['end'] : null;
$StartTime = $Start ? date("Y-m-d H:i:s", strtotime($Start)) : null;
$EndTime = $End ? date("Y-m-d H:i:s", strtotime($End)) : null;
$MemberList = $this->GetMembersInDateRange($StartTime, $EndTime);
$results = array();
// Transform the MySQLQuery object created above into an ArrayData object
if ($MemberList) {
foreach ($MemberList as $Member) {
$dbMember = Member::get()->byID($Member['ID']);
if (!is_null($dbMember)) {
$AffiliationList = $dbMember->OrderedAffiliations();
// If there are Affiliation updates, push a new copy of the member to the results array filled in with the org and date from the update
if ($AffiliationList && $AffiliationList->Count() > 0) {
foreach ($AffiliationList as $a) {
$currentMemberOrg = $a->Organization();
$Member['OrgAffiliations'] = $currentMemberOrg->Name;
if ($a->Current) {
$Member['untilDate'] = null;
} else {
$Member['untilDate'] = $a->EndDate;
}
// Push the member to the results
array_push($results, $Member);
}
} else {
//no affiliations
$Member['OrgAffiliations'] = null;
$Member['untilDate'] = null;
array_push($results, $Member);
}
}
}
}
// Finally, convert the array from the ArrayData object to JSON
$json = Convert::Array2JSON($results);
return $json;
}
}
function FullMemberList()
{
return Member::get()->leftJoin('Group_Members',
'`Member`.`ID` = `Group_Members`.`MemberID` AND Group_Members.GroupID=5');
}
}