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
<?php

class TeamManagementAdmin extends LeftAndMain implements PermissionProvider {

    static $tree_class = "Team";

    static $url_segment = 'teammanagement';

	static $url_rule = '/$Action/$ID';

	static $menu_title = 'Teammangement';

    static $allowed_actions = array(
        'addteam',
        'shownews',
        'showplayers',
        'showteam',
        'saveTeam',
        'save',
        'test'
    );

    public function init() {
        parent::init();

        Requirements::javascript(THIRDPARTY_DIR . "/hover.js");
		Requirements::javascript(THIRDPARTY_DIR . "/scriptaculous/controls.js");
        Requirements::javascript("cms/javascript/LeftAndMain_left.js");
        Requirements::javascript("cms/javascript/LeftAndMain_right.js");
        Requirements::javascript("teammanagement/javascript/TeamManagementAdmin_left.js");
        Requirements::javascript("teammanagement/javascript/TeamManagementAdmin_right.js");
        Requirements::css("teammanagement/css/teammanagementadmin.css");

        LeftAndMain::setApplicationName("Team Management", "Team Management");
    }

    public function Link($action = null) {
        return "admin/teammanagement/$action";
    }

    public function save($data, $form) {
        FormResponse::status_message(_t('TeamManagementAdmin.SAVEDTEAM','Saved Team xXx'), 'good');
        return FormRepsonse::respond();
        //return "test";
    }

    public function addteam($params) {
        return $this->newTeam();
    }

    public function showteam($params) {
        if($params instanceof HTTPRequest) {
            $params = $params->allParams();
        }
        return $this->TeamEditForm($params['ID']);
    }

    public function TeamEditForm($id = -1) {
        if($id == -1) $id = $_REQUEST['ID'];
        $team = DataObject::get_by_id("Team", $id);
        $ret = "";
        $fields = $team->getCMSFields();
        if(Member::currentUser()->isAdmin()) {
            $fields->addFieldToTab("Root.Mannschaftsmanager", new HeaderField("ManagerHeader", "Manager dieser Mannschaft"));
            $fields->addFieldToTab("Root.Mannschaftsmanager",
                new CheckboxSetField("Editor", "Mannschaftsmanager auswählen: ", DataObject::get("Member"), $team->Editor())
            );
        }
        $actions = new FieldSet();
        $actions->push(new FormAction('save', _t("TeamManagementAdmin.SAVE", "Speichern")));
        $actions->push(new FormAction("test", "lala"));
        $form = new Form($this, "TeamEditForm", $fields, $actions);
        $form->loadDataFrom($team);
        $form->setHTMLID("Form_EditForm");
        $ret .= $form->formHtmlContent();
        return $ret;
    }

    public function EditForm() {
    	if(isset($_REQUEST['type']) && $_REQUEST['type'] == 'team' && isset($_REQUEST['ID'])) {
            $form = $this->TeamEditForm();
        }
        if(isset($form)) {
            $form->disableDefaultAction();
            return $form;
        }
        return null;
    }

    public function showplayers($params) {
        if($params instanceof HTTPRequest) {
            $params = $params->allParams();
        }
        $team = DataObject::get_by_id("Team", $params['ID']);
        $ret = "";
        foreach($team->Players() as $ID => $player) {
            $ret .= "<b>".$player->Name."</b><br />";
        }
        return $ret;
    }

    public function shownews($params) {
        return "News ist noch nicht implementiert.";
    }

    public function newteam() {

        $team = new Team();
        $team->Name = _t('TeamManagementAdmin.NEWTEAM','Neue Mannschaft');
        $team->write();

        return $team->ID;
    }

    function providePermissions() {
		return array(
			'EDIT_PERMISSIONS' => _t('TeamManagementAdmin.EDITPERMISSIONS', 'Edit teams. XXX'),
		);
	}

    public function AddingRights() {
        if(Member::currentUser()->isAdmin()) return true;
        return false;
    }

    public function Teams() {
        $member = Member::currentUser();
        // der Admin bekommt alle Teams angezeigt
        if($member->isAdmin()) {
            return DataObject::get("Team");
        }
        // für andere User werden die Teams angezeigt, die von den Usern verwaltet werden.
        else {
            return DataObject::get("Team", "Team_Editor.MemberID = ".$member->ID, "", "JOIN Team_Editor ON Team.ID = Team_Editor.TeamID");
        }
        return null;
    }
}