forked from Zuluru/Zuluru
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_controller.php
executable file
·1767 lines (1575 loc) · 78 KB
/
app_controller.php
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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Short description for file.
*
* This file is application-wide controller file. You can put all
* application-wide controller-related methods here.
*
* PHP versions 4 and 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package cake
* @subpackage cake.cake.libs.controller
* @since CakePHP(tm) v 0.2.9
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
/**
* Add your application-wide methods in the class below, your controllers
* will inherit them.
*
* @package cake
* @subpackage cake.cake.libs.controller
*/
class AppController extends Controller {
var $components = array('Session', 'Auth', 'Cookie', 'RequestHandler', 'UserCache');
var $uses = array('Person', 'Configuration');
var $helpers = array(
'Session', 'UserCache',
'Html', 'ZuluruHtml', 'Form', 'ZuluruForm', 'Js' => array('ZuluruJquery'),
'Time', 'ZuluruTime', 'Number', 'Text');
var $view = 'Theme';
var $is_admin = false;
var $is_manager = false;
var $is_official = false;
var $is_volunteer = false;
var $is_coach = false;
var $is_player = false;
var $is_child = false;
var $is_logged_in = false;
var $is_visitor = true;
var $menu_items = array();
function beforeFilter() {
// Set up our caches for rarely-changed data
Cache::config('file', array('engine' => 'File', 'path' => CACHE . DS . 'queries'));
Cache::config('long_term', array('engine' => 'File', 'path' => CACHE . DS . 'queries', 'duration' => YEAR));
parent::beforeFilter();
$this->_setLanguage();
// Use the configured model for handling hashing of passwords, and configure
// the Auth field names using it
$this->Auth->userModel = Configure::read('security.auth_model');
$this->Auth->authenticate = ClassRegistry::init($this->Auth->userModel);
$this->Auth->sessionKey = 'Auth.User';
$this->Auth->fields = array(
'username' => $this->Auth->authenticate->userField,
'password' => $this->Auth->authenticate->pwdField,
);
// Save a couple of bits of information from the selected auth model in the
// configuration, so that it's accessible from anywhere instead of just places
// that can access the model
Configure::write('feature.manage_accounts', $this->Auth->authenticate->manageAccounts);
Configure::write('feature.manage_name', $this->Auth->authenticate->manageName);
$this->_setPermissions();
// Load configuration from database
if (isset($this->Configuration) && !empty($this->Configuration->table)) {
$this->Configuration->load($this->UserCache->currentId());
if (Configure::read('feature.affiliates')) {
$affiliates = $this->_applicableAffiliateIDs();
if (count($affiliates) == 1) {
$this->Configuration->loadAffiliate(reset($affiliates));
}
}
}
if (Configure::read('feature.items_per_page')) {
$this->paginate['limit'] = Configure::read('feature.items_per_page');
}
// Set the theme, if any. Must be done before processing, in order for the theme to affect emails.
$this->theme = Configure::read('theme');
// Requests made through requestAction don't need any of the rest of this
if (!empty($this->params['requested'])) {
return;
}
// Set up various URLs to use
// TODO: Read these from site configuration
if (! $this->Session->read('Zuluru.external_login')) {
$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
} else {
$this->Auth->loginAction = array('controller' => 'leagues', 'action' => 'index');
}
$this->Auth->logoutAction = array('controller' => 'users', 'action' => 'logout');
$this->Auth->autoRedirect = false;
$this->Auth->loginRedirect = '/';
$this->Auth->logoutRedirect = '/';
if ($this->is_logged_in) {
$this->Auth->authError = __('You do not have permission to access that page.', true);
} else {
$this->Auth->authError = __('You must login to access full site functionality.', true);
}
if (!$this->RequestHandler->isAjax()) {
if ($this->_arg('return') && !$this->Session->check('Navigation.redirect')) {
// If there's a return requested, and nothing already saved to return to, remember the referrer
$url = $this->referer(null, true);
$matched = preg_match('#(.*)/act_as:[0-9]*(.*)#', $url, $matches);
if ($matched) {
$url = "{$matches[1]}{$matches[3]}";
}
$this->Session->write('Navigation.redirect', $url);
} else if (!$this->_arg('return') && $this->Session->check('Navigation.redirect') && empty($this->data)) {
// If there's no return requested, and something saved, and this is not a POST, then the operation was aborted and we
// don't want to remember this any more
$this->Session->delete('Navigation.redirect');
}
}
// Check if we need to redirect logged-in users for some required step first
// We will allow them to see help or logout. Or get the leagues list, as that's where some things redirect to.
$free = $this->freeActions();
if ($this->is_logged_in && !in_array($this->action, $free)) {
if (($this->name != 'People' || $this->action != 'edit') && $this->UserCache->read('Person.user_id')) {
$email = $this->UserCache->read('Person.email');
if (empty ($email)) {
$this->Session->setFlash(__('Last time we tried to contact you, your email bounced. We require a valid email address as part of your profile. You must update it before proceeding.', true), 'default', array('class' => 'warning'));
$this->redirect (array('controller' => 'people', 'action' => 'edit'));
}
}
if (($this->name != 'People' || $this->action != 'edit') && $this->UserCache->read('Person.complete') == 0) {
$this->Session->setFlash(__('Your player profile is incomplete. You must update it before proceeding.', true), 'default', array('class' => 'warning'));
$this->redirect (array('controller' => 'people', 'action' => 'edit'));
}
// Force response to roster requests, if enabled
if (Configure::read('feature.force_roster_request')) {
$teams = Set::extract ('/TeamsPerson[status=' . ROSTER_INVITED . ']/..', $this->UserCache->read('Teams'));
$response_required = array();
foreach ($teams as $team) {
// Only force responses to leagues that have started play, but the roster deadline hasn't passed
if ($team['Division']['open'] < date('Y-m-d') && !Division::rosterDeadlinePassed($team['Division'])) {
$response_required[] = $team['Team']['id'];
}
}
if (!empty ($response_required) && $this->name != 'Settings' &&
// We will let people look at information about teams that they've been invited to
($this->name != 'Teams' || !in_array ($this->_arg('team'), $response_required)))
{
$this->Session->setFlash(__('You have been invited to join a team, and must either accept or decline this invitation before proceeding. Before deciding, you have the ability to look at this team\'s roster, schedule, etc.', true), 'default', array('class' => 'info'));
$this->redirect (array('controller' => 'teams', 'action' => 'view', 'team' => reset($response_required)));
}
}
}
$this->_initMenu();
}
function _setLanguage() {
$this->_findLanguage();
$i18n =& I18n::getInstance();
$this->Session->write('Config.language', $i18n->l10n->lang);
Configure::write('Config.language', $i18n->l10n->lang);
Configure::write('Config.language_name', $i18n->l10n->language);
Configure::Load('features');
Configure::Load('options');
}
function _findLanguage() {
$i18n =& I18n::getInstance();
$translations = Cache::read('available_translations');
$translation_strings = Cache::read('available_translation_strings');
if (!$translations || !$translation_strings) {
$translations = array('en' => 'English');
$translation_strings = array("en: 'English'");
$dir = opendir(APP . 'locale');
if ($dir) {
while (false !== ($entry = readdir($dir))) {
if (array_key_exists($entry, $i18n->l10n->__l10nMap) && file_exists(APP . 'locale' . DS . $entry . DS . 'LC_MESSAGES' . DS . 'default.po')) {
$code = $i18n->l10n->__l10nMap[$entry];
$translations[$code] = $i18n->l10n->__l10nCatalog[$code]['language'];
$translation_strings[] = "$code: '{$i18n->l10n->__l10nCatalog[$code]['language']}'";
}
}
}
Cache::write('available_translations', $translations);
Cache::write('available_translation_strings', $translation_strings);
}
Configure::write('available_translations', $translations);
Configure::write('available_translation_strings', implode(', ', $translation_strings));
$language = Configure::read('personal.language');
if (!empty($language)) {
$i18n->l10n->__setLanguage($language);
return;
}
if ($this->Session->check('Config.language')) {
$i18n->l10n->__setLanguage($this->Session->read('Config.language'));
return;
}
$langs = array();
// From http://www.thefutureoftheweb.com/blog/use-accept-language-header
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
// break up string into pieces (languages and q factors)
preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_parse);
if (count($lang_parse[1])) {
// create a list like "en" => 0.8
$langs = array_combine($lang_parse[1], $lang_parse[4]);
// set default to 1 for any without q factor
foreach ($langs as $lang => $val) {
if ($val === '') $langs[$lang] = 1;
}
// sort list based on value
arsort($langs, SORT_NUMERIC);
}
}
// See if we have a file that matches something the user wants
foreach (array_keys($langs) as $lang) {
$i18n->l10n->__setLanguage($lang);
foreach ($i18n->l10n->languagePath as $path) {
if ($path == 'eng' || file_exists(APP . 'locale' . DS . low(Inflector::slug($path)) . DS . 'LC_MESSAGES' . DS . 'default.po')) {
return;
}
}
}
// Use the site's default language, if there is one
if (Configure::read('site.default_language')) {
$i18n->l10n->__setLanguage(Configure::read('site.default_language'));
return;
}
// Last ditch default to English
$i18n->l10n->__setLanguage('eng');
}
function beforeRender() {
parent::beforeRender();
// Set the theme, if any
$this->theme = Configure::read('theme');
// Seems that beforeFilter is not called for error pages
if (!isset ($this->is_logged_in)) {
$this->is_logged_in = false;
$this->set('is_logged_in', $this->is_logged_in);
}
if (isset($this->RequestHandler)) {
$this->set('is_mobile', $this->RequestHandler->isMobile());
} else {
$this->set('is_mobile', false);
}
// Set view variables for the menu
// TODO: Get the menu element name from some configuration, maybe per-user?
$this->set('menu_element', 'flyout');
$this->set('menu_items', $this->menu_items);
}
function redirect($url = null, $next = null) {
// If there's a referer saved, we always go back there
if ($this->Session->check('Navigation.redirect')) {
$saved = Router::normalize($this->Session->read('Navigation.redirect'));
$this->Session->delete('Navigation.redirect');
}
if ($next && !is_numeric($next)) {
$this->Session->write('Navigation.redirect', $next);
}
if (isset($saved)) {
parent::redirect($saved);
}
// If there was no referer saved, we might not want to redirect
if ($url !== null) {
if ($next && is_numeric($next)) {
parent::redirect(Router::normalize($url), $next);
} else {
parent::redirect(Router::normalize($url));
}
}
}
/**
* Read and set variables for the database-based address options.
*/
function _loadAddressOptions() {
if (!isset ($this->Province)) {
$this->Province = ClassRegistry::init('Province');
}
$provinces = $this->Province->find('all');
$provinces = Set::combine($provinces, '{n}.Province.name', '{n}.Province.name');
$this->set('provinces', $provinces);
if (!isset ($this->Country)) {
$this->Country = ClassRegistry::init('Country');
}
$countries = $this->Country->find('all');
$countries = Set::combine($countries, '{n}.Country.name', '{n}.Country.name');
$this->set('countries', $countries);
}
/**
* Read and set variables for the database-based group options.
*/
function _loadGroupOptions($force_players = false) {
if ($force_players) {
$conditions = array('OR' => array(
// We always want to include players, even if they aren't a valid "create account" group.
'id' => GROUP_PLAYER,
'active' => true,
));
} else {
$conditions = array('active' => true);
}
if (!$this->is_admin) {
if ($this->is_manager) {
$conditions['level <='] = 5;
} else {
$conditions['level <'] = 5;
}
}
$groups = $this->Group->find('all', array(
'conditions' => $conditions,
'contain' => array(),
'order' => array('Group.level', 'Group.id'),
));
$group_list = array();
foreach ($groups as $group) {
if (!empty($group['Group']['description'])) {
$group_list[$group['Group']['id']] = "{$group['Group']['name']}: {$group['Group']['description']}";
} else {
$group_list[$group['Group']['id']] = $group['Group']['name'];
}
}
$this->set('groups', $group_list);
return $group_list;
}
/**
* Read and set variables for the database-based affiliate options.
*/
function _loadAffiliateOptions() {
$affiliates = $this->Person->Affiliate->find('all', array(
'conditions' => array(
'active' => true,
'NOT' => array('id' => $this->UserCache->read('ManagedAffiliateIDs')),
),
'contain' => array(),
));
$affiliates = Set::combine($affiliates, '{n}.Affiliate.id', '{n}.Affiliate.name');
$this->set('affiliates', $affiliates);
}
/**
* Basic check for authorization, based solely on the person's login group.
* Set some "is_" variables for the views to use (is_admin, is_player, etc.).
*
* @access public
*/
function _setPermissions() {
$this->is_admin = $this->is_manager = $this->is_official = $this->is_volunteer = $this->is_coach = $this->is_player = $this->is_child = $this->is_logged_in = false;
$this->is_visitor = true;
$auth =& $this->Auth->authenticate;
$user = $this->Auth->user();
// If the user has no session but does have "remember me" login information
// saved in a cookie, we want to redirect to the login page, which will
// just log them in automatically and send them right back here. If we
// don't do that, then their permissions aren't set up correctly, and menus
// and views will be wrong.
if (!$user && $this->Cookie->read('Auth.User')) {
$login = array('controller' => 'users', 'action' => 'login');
if ($this->here != Router::url($login)) {
$this->Session->write('Auth.redirect', $this->here);
$this->redirect ($login);
}
}
// Perform any additional login processing that may be required;
// the Auth user information may be updated by this process
$login = $this->_getComponent ('Login', $auth->loginComponent, $this);
if (empty($this->params['requested']) && (!$user || $login->expired())) {
// If there is a redirect requested, logout will erase it, so remember it!
if ($this->Session->check('Auth.redirect')) {
$redirect = $this->Session->read('Auth.redirect');
}
$this->Session->delete('Zuluru');
$this->Auth->logout();
$this->UserCache->initializeData();
$login->login();
$user = $this->Auth->user();
if (isset($redirect)) {
$this->Session->write('Auth.redirect', $redirect);
}
}
// Do we already have the corresponding person record?
// If not read it; if it doesn't even exist, create it.
if ($user && !$this->UserCache->currentId()) {
$person = $auth->Person->find('first', array(
'contain' => false,
'conditions' => array(
'user_id' => $user[$auth->alias][$auth->primaryKey],
),
));
if ($person) {
$this->Session->write("{$this->Auth->sessionKey}.zuluru_person_id", $person['Person']['id']);
} else {
if ($auth->Person->create_person_record($user[$auth->alias], $auth->nameField)) {
$this->Session->write("{$this->Auth->sessionKey}.zuluru_person_id", $auth->Person->id);
} else {
// TODO: What to do when writing this record fails?
}
}
}
$groups = $this->UserCache->read('Groups');
$real_groups = $this->UserCache->read('Groups', $this->UserCache->realId());
$real_group_levels = Set::extract('/level', $real_groups);
if ($this->UserCache->read('Person.status', $this->UserCache->realId()) == 'active') {
// Approved accounts are granted permissions up to level 1,
// since they can just add that group to themselves anyway.
$real_group_levels[] = 1;
}
if (empty($real_group_levels)) {
$max_level = 0;
} else {
$max_level = max($real_group_levels);
}
foreach ($groups as $group) {
if ($this->UserCache->currentId() != $this->UserCache->realId()) {
// Don't give people enhanced access just because the person they are acting as has it
if ($group['level'] > $max_level) {
continue;
}
}
// TODO: Eliminate all these is_ variables and use database-driven permissions
switch ($group['name']) {
case 'Administrator':
$this->is_admin = $this->is_manager = true;
break;
case 'Manager':
$this->is_manager = true;
break;
case 'Official':
$this->is_official = true;
break;
case 'Volunteer':
$this->is_volunteer = true;
break;
case 'Coach':
$this->is_coach = true;
break;
case 'Player':
$this->is_player = true;
break;
}
}
if ($this->UserCache->currentId()) {
$this->is_logged_in = true;
$this->is_visitor = false;
$this->is_child = $this->_isChild($this->UserCache->read('Person.birthdate'));
}
// Set these in convenient locations for views to use
foreach (array('is_admin', 'is_manager', 'is_official', 'is_volunteer', 'is_coach', 'is_player', 'is_child', 'is_logged_in', 'is_visitor') as $role) {
$this->set($role, $this->$role);
}
$this->set('my_id', $this->UserCache->currentId());
if ($this->is_admin) {
// Admins have permission to do anything.
$this->Auth->allow('*');
} else {
// Check what actions anyone (logged on or not) is allowed in this controller.
$allowed = $this->publicActions();
// An empty array here means the controller has *no* public actions, but an empty
// array passed to Auth->allow means *everything* is public.
if (!empty($allowed)) {
$this->Auth->allow($allowed);
}
}
// Other authentication is handled through the isAuthorized function of
// the individual controllers.
$this->Auth->authorize = 'controller';
}
// By default, nothing is public. Any controller with special permissions
// must override this function.
function publicActions() {
if ($this->name == 'Pages') {
return array('display');
}
return null;
}
// Some actions must always be allowed regardless of any redirect that we might want.
function freeActions() {
return array();
}
// By default, we allow the actions listed above (in the Auth->allow calls) and
// nothing else. Any controller with special permissions must override this function.
function isAuthorized() {
return false;
}
function _arg($key) {
if (array_key_exists ($key, $this->passedArgs)) {
return $this->passedArgs[$key];
} else {
return null;
}
}
function _limitOverride($team_id) {
$on_team = in_array($team_id, $this->UserCache->read('TeamIDs'));
if ($on_team) {
$this->effective_admin = $this->effective_coordinator = false;
} else {
$this->effective_admin = $this->is_admin;
if ($this->is_manager) {
if (!isset ($this->Team)) {
$this->Team = ClassRegistry::init ('Team');
}
$division = $this->Team->field('division_id', array('id' => $team_id));
$league = $this->Team->Division->field('league_id', array('id' => $division));
$affiliate = $this->Team->Division->League->field('affiliate_id', array('id' => $league));
$this->effective_coordinator = in_array($affiliate, $this->UserCache->read('ManagedAffiliateIDs'));
} else {
$divisions = $this->UserCache->read('Divisions');
$teams = Set::extract ('/Team/id', $divisions);
$this->effective_coordinator = in_array($team_id, $teams);
}
}
$this->set('is_effective_admin', $this->effective_admin);
$this->set('is_effective_coordinator', $this->effective_coordinator);
}
// Various ways to get the list of affiliates to show
function _applicableAffiliates($admin_only = false) {
if (!Configure::read('feature.affiliates')) {
return array(1 => Configure::read('organization.name'));
}
$affiliate_model = ClassRegistry::init ('Affiliate');
// If there's something in the URL, perhaps only use that
$affiliate = $this->_arg('affiliate');
if ($affiliate === null) {
// If the user has selected a specific affiliate to view, perhaps only use that
$affiliate = $this->Session->read('Zuluru.CurrentAffiliate');
}
if ($affiliate !== null) {
// We only allow overrides through the URL or session if:
// - this is not an admin-only page OR
// - the current user is an admin OR
// - the current user is a manager of that affiliate
if (!$admin_only || $this->is_admin ||
($this->is_manager && in_array($affiliate, $this->UserCache->read('ManagedAffiliateIDs')))
)
{
return $affiliate_model->find('list', array(
'conditions' => array('Affiliate.id' => $affiliate),
));
}
}
// Managers may get only their list of managed affiliates
if (!$this->is_admin && $this->is_manager && $admin_only) {
$affiliates = $this->UserCache->read('ManagedAffiliates');
$affiliates = Set::combine($affiliates, '{n}.Affiliate.id', '{n}.Affiliate.name');
ksort($affiliates);
return $affiliates;
}
// Non-admins get their current list of "subscribed" affiliates
if ($this->is_logged_in && !$this->is_admin) {
$affiliates = $this->UserCache->read('Affiliates');
if (!empty($affiliates)) {
$affiliates = Set::combine($affiliates, '{n}.Affiliate.id', '{n}.Affiliate.name');
ksort($affiliates);
return $affiliates;
}
}
// Anyone not logged in, and admins, get the full list
return $affiliate_model->find('list', array(
'conditions' => array('active' => true),
'order' => 'name',
));
}
// Various ways to get the list of affiliates to query
function _applicableAffiliateIDs($admin_only = false) {
if (!Configure::read('feature.affiliates')) {
return array(1);
}
// If there's something in the URL, perhaps only use that
$affiliate = $this->_arg('affiliate');
if ($affiliate === null) {
// If the user has selected a specific affiliate to view, perhaps only use that
$affiliate = $this->Session->read('Zuluru.CurrentAffiliate');
}
if ($affiliate !== null) {
// We only allow overrides through the URL or session if:
// - this is not an admin-only page OR
// - the current user is an admin OR
// - the current user is a manager of that affiliate
if (!$admin_only || $this->is_admin ||
($this->is_manager && in_array($affiliate, $this->UserCache->read('ManagedAffiliateIDs')))
)
{
return array($affiliate);
}
}
// Managers may get only their list of managed affiliates
if (!$this->is_admin && $this->is_manager && $admin_only) {
return $this->UserCache->read('ManagedAffiliateIDs');
}
// Non-admins get their current list of selected affiliates
if ($this->is_logged_in && !$this->is_admin) {
$affiliates = $this->UserCache->read('AffiliateIDs');
if (!empty($affiliates)) {
return $affiliates;
}
}
// Anyone not logged in, and admins, get the full list
$affiliate_model = ClassRegistry::init ('Affiliate');
return array_keys($affiliate_model->find('list', array(
'conditions' => array('active' => true),
)));
}
/**
* Put basic items on the menu, some based on configuration settings.
* Other items like specific teams and divisions are added elsewhere.
*/
function _initMenu() {
// Initialize the menu
$this->menu_items = array();
$groups = $this->UserCache->read('GroupIDs');
if ($this->is_logged_in) {
$this->_addMenuItem(__('Home', true), array('controller' => 'all', 'action' => 'splash'));
$this->_addMenuItem(__('My Profile', true), array('controller' => 'people', 'action' => 'view'));
$this->_addMenuItem(__('View', true), array('controller' => 'people', 'action' => 'view'), __('My Profile', true));
$this->_addMenuItem(__('Edit', true), array('controller' => 'people', 'action' => 'edit'), __('My Profile', true));
$this->_addMenuItem(__('Preferences', true), array('controller' => 'people', 'action' => 'preferences'), __('My Profile', true));
if (in_array(GROUP_PARENT, $groups)) {
$this->_addMenuItem(__('Add new child', true), array('controller' => 'people', 'action' => 'add_relative'), __('My Profile', true));
}
$this->_addMenuItem(__('Link to relative', true), array('controller' => 'people', 'action' => 'link_relative'), __('My Profile', true));
$this->_addMenuItem(__('Waiver history', true), array('controller' => 'people', 'action' => 'waivers'), __('My Profile', true));
$this->_addMenuItem(__('Change password', true), array('controller' => 'users', 'action' => 'change_password'), __('My Profile', true));
if (Configure::read('feature.photos')) {
$this->_addMenuItem(__('Upload photo', true), array('controller' => 'people', 'action' => 'photo_upload'), __('My Profile', true));
}
if (Configure::read('feature.documents')) {
$this->_addMenuItem(__('Upload document', true), array('controller' => 'people', 'action' => 'document_upload'), __('My Profile', true));
}
}
// Depending on the account type, and the available registrations, this may not be available
// Admins and managers, anyone not logged in, and anyone with any registration history always get it
if (Configure::read('feature.registration')) {
// Parents always get the registration menu items
if (in_array(GROUP_PARENT, $groups) || $this->_showRegistration(null, $groups)) {
$this->_addMenuItem(__('Registration', true), array('controller' => 'events', 'action' => 'wizard'));
$this->_addMenuItem(__('Wizard', true), array('controller' => 'events', 'action' => 'wizard'), __('Registration', true));
$this->_addMenuItem(__('All events', true), array('controller' => 'events', 'action' => 'index'), __('Registration', true));
if ($this->is_logged_in && !empty($registrations)) {
$this->_addMenuItem(__('My history', true), array('controller' => 'people', 'action' => 'registrations'), __('Registration', true));
}
$unpaid = $this->UserCache->read('RegistrationsCanPay');
if (!empty ($unpaid)) {
$this->_addMenuItem(__('Checkout', true), array('controller' => 'registrations', 'action' => 'checkout'), __('Registration', true));
}
}
if ($this->is_admin || $this->is_manager) {
$this->_addMenuItem(__('Preregistrations', true), array('controller' => 'preregistrations', 'action' => 'index'), __('Registration', true));
$this->_addMenuItem(__('List', true), array('controller' => 'preregistrations', 'action' => 'index'), array(__('Registration', true), __('Preregistrations', true)));
$this->_addMenuItem(__('Add', true), array('controller' => 'preregistrations', 'action' => 'add'), array(__('Registration', true), __('Preregistrations', true)));
$this->_addMenuItem(__('Unpaid', true), array('controller' => 'registrations', 'action' => 'unpaid'), __('Registration', true));
$this->_addMenuItem(__('Credits', true), array('controller' => 'registrations', 'action' => 'credits'), __('Registration', true));
$this->_addMenuItem(__('Report', true), array('controller' => 'registrations', 'action' => 'report'), __('Registration', true));
$this->_addMenuItem(sprintf(__('Create %s', true), __('event', true)), array('controller' => 'events', 'action' => 'add'), __('Registration', true));
$this->_addMenuItem(__('Questionnaires', true), array('controller' => 'questionnaires', 'action' => 'index'), __('Registration', true));
$this->_addMenuItem(__('List', true), array('controller' => 'questionnaires', 'action' => 'index'), array(__('Registration', true), __('Questionnaires', true)));
$this->_addMenuItem(__('Questions', true), array('controller' => 'questions', 'action' => 'index'), array(__('Registration', true), __('Questionnaires', true)));
$this->_addMenuItem(__('Deactivated', true), array('controller' => 'questionnaires', 'action' => 'deactivated'), array(__('Registration', true), __('Questionnaires', true)));
$this->_addMenuItem(__('List', true), array('controller' => 'questions', 'action' => 'index'), array(__('Registration', true), __('Questionnaires', true), __('Questions', true)));
$this->_addMenuItem(__('Deactivated', true), array('controller' => 'questions', 'action' => 'deactivated'), array(__('Registration', true), __('Questionnaires', true), __('Questions', true)));
}
}
// Add the personal menu items next, so that specific teams and divisions
// are the first sub-menus in the Teams and Leagues menus, rather than
// the generic operations.
if ($this->is_logged_in) {
$this->_initPersonalMenu();
$relatives = $this->UserCache->allActAs(true, 'first_name');
foreach ($relatives as $id => $name) {
$this->_initPersonalMenu($id, $name);
}
}
if ($this->is_logged_in) {
$this->_addMenuItem(__('Teams', true), array('controller' => 'teams', 'action' => 'index'));
$this->_addMenuItem(__('List', true), array('controller' => 'teams', 'action' => 'index'), __('Teams', true));
// If registrations are enabled, it takes care of team creation
if ($this->is_admin || $this->is_manager || !Configure::read('feature.registration')) {
$this->_addMenuItem(sprintf(__('Create %s', true), __('team', true)), array('controller' => 'teams', 'action' => 'add'), __('Teams', true));
}
if ($this->is_admin || $this->is_manager) {
$this->_addMenuItem(__('Unassigned teams', true), array('controller' => 'teams', 'action' => 'unassigned'), __('Teams', true));
}
}
if ($this->is_logged_in && Configure::read('feature.franchises')) {
$this->_addMenuItem(__('Franchises', true), array('controller' => 'franchises', 'action' => 'index'), __('Teams', true));
$this->_addMenuItem(__('List', true), array('controller' => 'franchises', 'action' => 'index'), array(__('Teams', true), __('Franchises', true)));
$this->_addMenuItem(sprintf(__('Create %s', true), __('franchise', true)), array('controller' => 'franchises', 'action' => 'add'), array(__('Teams', true), __('Franchises', true)));
}
$this->_addMenuItem(__('Leagues', true), array('controller' => 'leagues', 'action' => 'index'));
$this->_addMenuItem(__('List', true), array('controller' => 'leagues', 'action' => 'index'), __('Leagues', true));
if ($this->is_admin || $this->is_manager) {
$this->_addMenuItem(__('League summary', true), array('controller' => 'leagues', 'action' => 'summary'), __('Leagues', true));
$this->_addMenuItem(sprintf(__('Create %s', true), __('league', true)), array('controller' => 'leagues', 'action' => 'add'), __('Leagues', true));
}
$this->_addMenuItem(__(Configure::read('ui.fields_cap'), true), array('controller' => 'facilities', 'action' => 'index'));
$this->_addMenuItem(__('List', true), array('controller' => 'facilities', 'action' => 'index'), __(Configure::read('ui.fields_cap'), true));
$this->_addMenuItem(sprintf(__('Map of all %s', true), __(Configure::read('ui.fields'), true)), array('controller' => 'maps', 'action' => 'index'), __(Configure::read('ui.fields_cap'), true), null, array('target' => 'map'));
if ($this->is_admin || $this->is_manager) {
$affiliates = $this->_applicableAffiliates(true);
$this->_addMenuItem(__('Regions', true), array('controller' => 'regions', 'action' => 'index'), __(Configure::read('ui.fields_cap'), true));
$this->_addMenuItem(__('List', true), array('controller' => 'regions', 'action' => 'index'), array(__(Configure::read('ui.fields_cap'), true), __('Regions', true)));
$this->_addMenuItem(sprintf(__('Create %s', true), __('region', true)), array('controller' => 'regions', 'action' => 'add'), array(__(Configure::read('ui.fields_cap'), true), __('Regions', true)));
$this->_addMenuItem(__('Closed facilities', true), array('controller' => 'facilities', 'action' => 'closed'), __(Configure::read('ui.fields_cap'), true));
$this->_addMenuItem(sprintf(__('Create %s', true), __('facility', true)), array('controller' => 'facilities', 'action' => 'add'), __(Configure::read('ui.fields_cap'), true));
if (!Configure::read('feature.affiliates')) {
$this->_addMenuItem(__('Add bulk gameslots', true), array('controller' => 'game_slots', 'action' => 'add'), __(Configure::read('ui.fields_cap'), true));
} else if (count($affiliates) == 1) {
$this->_addMenuItem(__('Add bulk gameslots', true), array('controller' => 'game_slots', 'action' => 'add', 'affiliate' => reset(array_keys($affiliates))), __(Configure::read('ui.fields_cap'), true));
} else {
foreach ($affiliates as $affiliate => $name) {
$this->_addMenuItem(__($name, true), array('controller' => 'game_slots', 'action' => 'add', 'affiliate' => $affiliate), array(__(Configure::read('ui.fields_cap'), true), __('Add bulk gameslots', true)));
}
}
}
if ($this->is_logged_in) {
$this->_addMenuItem(__('Search', true), array('controller' => 'people', 'action' => 'search'), __('People', true));
if (Configure::read('feature.badges')) {
$this->_addMenuItem(__('Badges', true), array('controller' => 'badges', 'action' => 'index'), __('People', true));
$this->_addMenuItem(__('Nominate', true), array('controller' => 'people', 'action' => 'nominate'), array(__('People', true), __('Badges', true)));
if ($this->is_admin || $this->is_manager) {
$new = $this->Person->Badge->find ('count', array(
'joins' => array(
array(
'table' => "{$this->Person->tablePrefix}badges_people",
'alias' => 'BadgesPerson',
'type' => 'LEFT',
'foreignKey' => false,
'conditions' => 'BadgesPerson.badge_id = Badge.id',
),
),
'conditions' => array(
'BadgesPerson.approved' => false,
'Badge.affiliate_id' => array_keys($affiliates),
),
));
if ($new > 0) {
$this->set('new_nominations', $new);
$this->_addMenuItem(sprintf(__('Approve nominations (%d pending)', true), $new), array('controller' => 'people', 'action' => 'approve_badges'), array(__('People', true), __('Badges', true)));
}
$this->_addMenuItem(__('Deactivated', true), array('controller' => 'badges', 'action' => 'deactivated'), array(__('People', true), __('Badges', true)));
}
}
}
if ($this->is_admin || $this->is_manager) {
$this->_addMenuItem(__('By name', true), array('controller' => 'people', 'action' => 'search'), array(__('People', true), __('Search', true)));
$this->_addMenuItem(__('By rule', true), array('controller' => 'people', 'action' => 'rule_search'), array(__('People', true), __('Search', true)));
$this->_addMenuItem(__('By league', true), array('controller' => 'people', 'action' => 'league_search'), array(__('People', true), __('Search', true)));
$this->_addMenuItem(__('Inactive', true), array('controller' => 'people', 'action' => 'inactive_search'), array(__('People', true), __('Search', true)));
if (!isset ($this->Person)) {
$this->Person = ClassRegistry::init ('Person');
}
$new = $this->Person->find ('count', array(
'contain' => array(),
'joins' => array(
array(
'table' => "{$this->Person->tablePrefix}affiliates_people",
'alias' => 'AffiliatePerson',
'type' => 'LEFT',
'foreignKey' => false,
'conditions' => 'AffiliatePerson.person_id = Person.id',
),
),
'conditions' => array(
'Person.status' => 'new',
'Person.complete' => 1,
'AffiliatePerson.affiliate_id' => array_keys($affiliates),
),
));
if ($new > 0) {
$this->set('new_accounts', $new);
$this->_addMenuItem(sprintf(__('Approve new accounts (%d pending)', true), $new), array('controller' => 'people', 'action' => 'list_new'), __('People', true));
}
if (!isset ($this->Person->Upload) &&
(Configure::read('feature.photos') || Configure::read('feature.documents')))
{
$this->Person->Upload = ClassRegistry::init ('Upload');
}
if (Configure::read('feature.photos') && Configure::read('feature.approve_photos')) {
$new = $this->Person->Upload->find ('count', array(
'conditions' => array(
'approved' => 0,
'type_id' => null,
),
));
if ($new > 0) {
$this->set('new_photos', $new);
$this->_addMenuItem(sprintf(__('Approve new photos (%d pending)', true), $new), array('controller' => 'people', 'action' => 'approve_photos'), __('People', true));
}
}
if (Configure::read('feature.documents')) {
$new = $this->Person->Upload->find ('count', array(
'conditions' => array(
'approved' => 0,
'type_id !=' => null,
),
));
if ($new > 0) {
$this->set('new_documents', $new);
$this->_addMenuItem(sprintf(__('Approve new documents (%d pending)', true), $new), array('controller' => 'people', 'action' => 'approve_documents'), __('People', true));
}
}
$this->_addMenuItem(__('List all', true), array('controller' => 'people', 'action' => 'index'), __('People', true));
$groups = $this->Person->Group->find('list', array(
'conditions' => array('OR' => array(
// We always want to include players, even if they aren't a valid "create account" group.
'id' => GROUP_PLAYER,
'active' => true,
)),
'order' => array('Group.level', 'Group.id'),
));
foreach ($groups as $group => $name) {
$this->_addMenuItem(__(Inflector::pluralize($name), true), array('controller' => 'people', 'action' => 'index', 'group' => $group), array(__('People', true), 'List all'));
}
$this->_addMenuItem(__('Bulk import', true), array('controller' => 'users', 'action' => 'import'), __('People', true));
$this->_addMenuItem(__('Newsletters', true), array('controller' => 'newsletters', 'action' => 'index'));
$this->_addMenuItem(__('Upcoming', true), array('controller' => 'newsletters', 'action' => 'index'), __('Newsletters', true));
$this->_addMenuItem(__('Mailing lists', true), array('controller' => 'mailing_lists', 'action' => 'index'), __('Newsletters', true));
$this->_addMenuItem(__('List', true), array('controller' => 'mailing_lists', 'action' => 'index'), array(__('Newsletters', true), __('Mailing lists', true)));
$this->_addMenuItem(sprintf(__('Create %s', true), __('mailing list', true)), array('controller' => 'mailing_lists', 'action' => 'add'), array(__('Newsletters', true), __('Mailing lists', true)));
$this->_addMenuItem(sprintf(__('Create %s', true), __('newsletter', true)), array('controller' => 'newsletters', 'action' => 'add'), __('Newsletters', true));
$this->_addMenuItem(__('All newsletters', true), array('controller' => 'newsletters', 'action' => 'past'), __('Newsletters', true));
}
if ($this->is_admin) {
$this->_addMenuItem(__('Organization', true), array('controller' => 'settings', 'action' => 'organization'), array(__('Configuration', true), __('Settings', true)));
$this->_addMenuItem(__('Features', true), array('controller' => 'settings', 'action' => 'feature'), array(__('Configuration', true), __('Settings', true)));
$this->_addMenuItem(__('Email', true), array('controller' => 'settings', 'action' => 'email'), array(__('Configuration', true), __('Settings', true)));
$this->_addMenuItem(__('Team', true), array('controller' => 'settings', 'action' => 'team'), array(__('Configuration', true), __('Settings', true)));
$this->_addMenuItem(__('User', true), array('controller' => 'settings', 'action' => 'user'), array(__('Configuration', true), __('Settings', true)));
$this->_addMenuItem(__('Profile', true), array('controller' => 'settings', 'action' => 'profile'), array(__('Configuration', true), __('Settings', true)));
$this->_addMenuItem(__('Scoring', true), array('controller' => 'settings', 'action' => 'scoring'), array(__('Configuration', true), __('Settings', true)));
if (Configure::read('feature.registration')) {
$this->_addMenuItem(__('Registration', true), array('controller' => 'settings', 'action' => 'registration'), array(__('Configuration', true), __('Settings', true)));
if (Configure::read('registration.online_payments')) {
$this->_addMenuItem(__('Payment', true), array('controller' => 'settings', 'action' => 'payment'), array(__('Configuration', true), __('Settings', true)));
}
}
if (Configure::read('feature.affiliates')) {
$this->_addMenuItem(__('Affiliates', true), array('controller' => 'affiliates', 'action' => 'index'), __('Configuration', true));
}
}
if (Configure::read('feature.affiliates') && $this->is_manager) {
if (count($affiliates) == 1 && !$this->is_admin) {
$affiliate = reset(array_keys($affiliates));
$this->_addMenuItem(__('Organization', true), array('controller' => 'settings', 'action' => 'organization', 'affiliate' => $affiliate), array(__('Configuration', true), __('Settings', true)));
$this->_addMenuItem(__('Features', true), array('controller' => 'settings', 'action' => 'feature', 'affiliate' => $affiliate), array(__('Configuration', true), __('Settings', true)));
$this->_addMenuItem(__('Email', true), array('controller' => 'settings', 'action' => 'email', 'affiliate' => $affiliate), array(__('Configuration', true), __('Settings', true)));
$this->_addMenuItem(__('Team', true), array('controller' => 'settings', 'action' => 'team', 'affiliate' => $affiliate), array(__('Configuration', true), __('Settings', true)));
$this->_addMenuItem(__('User', true), array('controller' => 'settings', 'action' => 'user', 'affiliate' => $affiliate), array(__('Configuration', true), __('Settings', true)));
$this->_addMenuItem(__('Profile', true), array('controller' => 'settings', 'action' => 'profile', 'affiliate' => $affiliate), array(__('Configuration', true), __('Settings', true)));
$this->_addMenuItem(__('Scoring', true), array('controller' => 'settings', 'action' => 'scoring', 'affiliate' => $affiliate), array(__('Configuration', true), __('Settings', true)));
if (Configure::read('feature.registration')) {
$this->_addMenuItem(__('Registration', true), array('controller' => 'settings', 'action' => 'registration', 'affiliate' => $affiliate), array(__('Configuration', true), __('Settings', true)));
if (Configure::read('registration.online_payments')) {
$this->_addMenuItem(__('Payment', true), array('controller' => 'settings', 'action' => 'payment', 'affiliate' => $affiliate), array(__('Configuration', true), __('Settings', true)));
}
}
} else {
foreach ($affiliates as $affiliate => $name) {
$this->_addMenuItem(__($name, true), array('controller' => 'settings', 'action' => 'organization', 'affiliate' => $affiliate), array(__('Configuration', true), __('Settings', true), __('Organization', true)));
$this->_addMenuItem(__($name, true), array('controller' => 'settings', 'action' => 'feature', 'affiliate' => $affiliate), array(__('Configuration', true), __('Settings', true), __('Features', true)));
$this->_addMenuItem(__($name, true), array('controller' => 'settings', 'action' => 'email', 'affiliate' => $affiliate), array(__('Configuration', true), __('Settings', true), __('Email', true)));
$this->_addMenuItem(__($name, true), array('controller' => 'settings', 'action' => 'team', 'affiliate' => $affiliate), array(__('Configuration', true), __('Settings', true), __('Team', true)));
$this->_addMenuItem(__($name, true), array('controller' => 'settings', 'action' => 'user', 'affiliate' => $affiliate), array(__('Configuration', true), __('Settings', true), __('User', true)));
$this->_addMenuItem(__($name, true), array('controller' => 'settings', 'action' => 'scoring', 'affiliate' => $affiliate), array(__('Configuration', true), __('Settings', true), __('Scoring', true)));
$this->_addMenuItem(__($name, true), array('controller' => 'settings', 'action' => 'profile', 'affiliate' => $affiliate), array(__('Configuration', true), __('Settings', true), __('Profile', true)));
if (Configure::read('feature.registration')) {
$this->_addMenuItem(__($name, true), array('controller' => 'settings', 'action' => 'registration', 'affiliate' => $affiliate), array(__('Configuration', true), __('Settings', true), __('Registration', true)));
if (Configure::read('registration.online_payments')) {
$this->_addMenuItem(__($name, true), array('controller' => 'settings', 'action' => 'payment', 'affiliate' => $affiliate), array(__('Configuration', true), __('Settings', true), __('Payment', true)));
}
}
}
}
}
if ($this->is_admin || $this->is_manager || $this->is_official || $this->is_volunteer) {
if (Configure::read('feature.tasks')) {
$this->_addMenuItem(__('Tasks', true), array('controller' => 'tasks', 'action' => 'index'));
$this->_addMenuItem(__('List', true), array('controller' => 'tasks', 'action' => 'index'), __('Tasks', true));
}
}
if ($this->is_admin) {
$this->_addMenuItem(__('Permissions', true), array('controller' => 'groups', 'action' => 'index'), __('Configuration', true));
}
if ($this->is_admin || $this->is_manager) {
$this->_addMenuItem(__('Holidays', true), array('controller' => 'holidays', 'action' => 'index'), __('Configuration', true));
if (Configure::read('feature.documents')) {
$this->_addMenuItem(__('Upload types', true), array('controller' => 'upload_types', 'action' => 'index'), __('Configuration', true));
}
$this->_addMenuItem(__('Waivers', true), array('controller' => 'waivers', 'action' => 'index'), __('Configuration', true));
if (Configure::read('feature.tasks')) {
$this->_addMenuItem(__('Categories', true), array('controller' => 'categories', 'action' => 'index'), __('Tasks', true));
$this->_addMenuItem(__('Download All', true), array('controller' => 'tasks', 'action' => 'index', 'download' => true), __('Tasks', true));
}
if (Configure::read('feature.contacts')) {
$this->_addMenuItem(__('Contacts', true), array('controller' => 'contacts', 'action' => 'index'), __('Configuration', true));