authlist.tpl 9.5 KB
Newer Older
1
<script type="application/javascript">
Naomi Guyer's avatar
Naomi Guyer committed
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

    function move_up(id) {
        instanceArray = document.getElementById('instancePriority').value.split(',');
        var outputArray = new Array();
        for(i = instanceArray.length - 1; i >= 0; i--) {
            if(instanceArray[i] == id) {
                outputArray[i] = instanceArray[i-1];
                outputArray[i-1] = instanceArray[i];
                --i;
            } else {
                outputArray[i] = instanceArray[i];
            }
        }
        rebuildInstanceList(outputArray);
        if (typeof formchangemanager !== 'undefined') {
            var form = jQuery('div#instanceList').closest('form')[0];
            formchangemanager.setFormState(form, FORM_CHANGED);
        }
    }

    function move_down(id) {
        instanceArray = document.getElementById('instancePriority').value.split(',');
        var outputArray = new Array();

        for(i = 0; i < instanceArray.length; i++) {
            if(instanceArray[i] == id) {
                outputArray[i+1] = instanceArray[i];
                outputArray[i] = instanceArray[i+1];
                ++i;
            } else {
                outputArray[i] = instanceArray[i];
            }
        }
        rebuildInstanceList(outputArray);
        if (typeof formchangemanager !== 'undefined') {
            var form = jQuery('div#instanceList').closest('form')[0];
            formchangemanager.setFormState(form, FORM_CHANGED);
        }
    }

    function rebuildInstanceList(outputArray) {
        var displayArray = new Array();
        var instanceListDiv = document.getElementById('instanceList');

        // Take each auth instance div, remove its span tag (containing arrow links) and clone it
        // adding the clone to the displayArray list
        for (i = 0; i < outputArray.length; i++) {
            var myDiv =  document.getElementById('instanceDiv' + outputArray[i]);
            replaceChildNodes(getFirstElementByTagAndClassName('span', 'authIcons', 'instanceDiv' + outputArray[i]));
            displayArray.push(myDiv.cloneNode(true));
        }

        emptyThisNode(instanceListDiv);

        for(i = 0; i < displayArray.length; i++) {
            if(displayArray.length > 1) {
                if (i + 1 != displayArray.length) {
59
                    getFirstElementByTagAndClassName('span', 'authIcons', displayArray[i]).innerHTML += '<a class="btn btn-link" href="" onclick="move_down('+outputArray[i]+'); return false;"><span class="icon icon-long-arrow-down"></span><span class="sr-only">'+get_string('moveitemdown')+'</span></a>'+"\n";
Naomi Guyer's avatar
Naomi Guyer committed
60
61
                }
                if(i != 0) {
62
                    getFirstElementByTagAndClassName('span', 'authIcons', displayArray[i]).innerHTML += '<a class="btn btn-link" href="" onclick="move_up('+outputArray[i]+'); return false;"><span class="icon icon-long-arrow-up"></span><span class="sr-only">'+get_string('moveitemup')+'</span></a>'+"\n";
Naomi Guyer's avatar
Naomi Guyer committed
63
64
65
                }
            }

66
            getFirstElementByTagAndClassName('span', 'authIcons', displayArray[i]).innerHTML += '<a class="btn btn-default btn-sm" href="" onclick="removeAuth('+outputArray[i]+'); return false;"><span class="icon icon-trash icon-lg text-danger"></span><span class="sr-only">'+get_string('deleteitem')+'</span></a>'+"\n";
Naomi Guyer's avatar
Naomi Guyer committed
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

            instanceListDiv.appendChild(displayArray[i]);
        }
        document.getElementById('instancePriority').value = outputArray.join(',');
    }

    function arrayIze(id) {
        var thing = document.getElementById(id).value;
        if(thing == '') {
            return new Array();
        }
        return thing.split(',');
    }

    function removeAuth(id) {
        instanceArray = arrayIze('instancePriority');
        deleteArray   = arrayIze('deleteList');
        inuseArray   = arrayIze('institution_inuse');

        if (instanceArray.length == 1) {
            alert({{$cannotremove|safe}});
            return false;
        }

        for(i = 0; i < inuseArray.length; i++) {
            if (id == inuseArray[i]) {
                alert({{$cannotremoveinuse|safe}});
                return false;
            }
        }

        for(i = 0; i < instanceArray.length; i++) {
            if(instanceArray[i] == id) {
                instanceArray.splice(i, 1);
                deleteArray.push(id);
                var instanceListDiv = document.getElementById('instanceList');
                instanceListDiv.removeChild(instanceListDiv.childNodes[i]);
            }
        }

        document.getElementById('deleteList').value = deleteArray.join(',');
        rebuildInstanceList(instanceArray);
        if (typeof formchangemanager !== 'undefined') {
            var form = jQuery('div#instanceList').closest('form')[0];
            formchangemanager.setFormState(form, FORM_CHANGED);
        }
    }

    function emptyThisNode(node) {
        while(node.hasChildNodes()) {
            node.removeChild(node.childNodes[0]);
        }
    }

    function requiresConfig(authname) {
        var requires_config = new Array();
        {{foreach $authtypes authtype}}
            requires_config['{{$authtype->name}}'] = {{$authtype->requires_config}};
        {{/foreach}}
        
        return requires_config[authname];
    }

    function addinstance() {
        var selectedPlugin = document.getElementById('dummySelect').value;
        var institution = '{{$institution}}';
        if (institution.length == 0) {
            alert({{$saveinstitutiondetailsfirst|safe}});
            return false;
        }

        if (requiresConfig(selectedPlugin) == 1) {
            window.open('addauthority.php?add=1&i={{$institution}}&p=' + selectedPlugin, 'addinstance', 'height=600,width=800,screenx=250,screenY=200,scrollbars=1');
            return;
        }

        var authSelect = document.getElementById('dummySelect');
        for (var i=0; i < authSelect.length; i++) {
            if (authSelect.options[i].value == selectedPlugin) {
                authSelect.remove(i);
            }
        }

        sendjsonrequest('{{$WWWROOT}}admin/users/addauthority.php', {'i': '{{$institution}}', 'p': selectedPlugin, 'add': 1, 'j': 1 }, 'GET', function (data) { addAuthority(data.id, data.name, data.authname); });
        return false;
    }

    function editinstance(id, plugin) {
        if (requiresConfig(plugin)) {
            window.open('addauthority.php?id='+id+'&edit=1&i={{$institution}}&p=' + plugin, 'editinstance', 'height=520,width=550,screenx=250,screenY=200,scrollbars=1');
        } else {
            alert({{$noauthpluginconfigoptions|safe}});
        }
    }

    function addAuthority(id, name, authname) {
        var newDiv = '<div class="authInstance" id="instanceDiv'+id+'"> '+
            '<label class="authLabel"><a href="" onclick="editinstance('+id+',\''+authname+'\'); return false;">'+name+'</a></label> '+
            '<span class="authIcons" id="arrows'+id+'"></span> </div>';
        document.getElementById('instanceList').innerHTML += newDiv;
        if(document.getElementById('instancePriority').value.length) {
            instanceArray = document.getElementById('instancePriority').value.split(',');
        } else {
            instanceArray = new Array();
        }
        instanceArray.push(id);
        rebuildInstanceList(instanceArray);
        if (typeof formchangemanager !== 'undefined') {
            var form = jQuery('div#instanceList').closest('form')[0];
            formchangemanager.setFormState(form, FORM_CHANGED);
        }
        replaceChildNodes('messages');
    }

</script>
{{*

IMPORTANT: do not introduce any new whitespace into the instanceList div.

*}}
187
188
189
<div id="instanceList" class="pbl">
    {{foreach $instancelist instance}}
    <div class="authInstance" id="instanceDiv{{$instance->id}}">
Naomi Guyer's avatar
Naomi Guyer committed
190
191
192
193
194
195
        <label class="authLabel">
            <a href="" onclick="editinstance({{$instance->id}},'{{$instance->authname}}'); return false;">
            {{str tag="title" section="auth.`$instance->authname`"}}</a>
        </label>
        <span class="authIcons" id="arrows{{$instance->id}}">
            {{if $instance->index + 1 < $instance->total}}
196
197
198
199
            <a href="" onclick="move_down({{$instance->id}}); return false;">
                <span class="icon icon-arrow-long-down"></span>
                <span class="sr-only">{{str tag=moveitemdown}}</span>
            </a>
Naomi Guyer's avatar
Naomi Guyer committed
200
201
            {{/if}}
            {{if $instance->index != 0 }}
202
203
204
205
            <a href="" onclick="move_up({{$instance->id}}); return false;">
                <span class="icon icon-arrow-long-up"></span>
                <span class="sr-only">{{str tag=moveitemup}}</span>
            </a>
Naomi Guyer's avatar
Naomi Guyer committed
206
            {{/if}}
207
208
209
210
            <a href="" class="btn btn-default btn-sm" onclick="removeAuth({{$instance->id}}); return false;">
                <span class="icon icon-trash icon-lg text-danger"></span>
                <span class="sr-only">{{str tag=deleteitem}}</span>
            </a>
Naomi Guyer's avatar
Naomi Guyer committed
211
        </span>
212
213
    </div>
    {{/foreach}}
Naomi Guyer's avatar
Naomi Guyer committed
214
</div>
215
216
217
218
219
220
221
222
223
<div class="select">
    <span class="picker">
        <select class="select form-control" name="dummy" id="dummySelect">
        {{foreach $authtypes authtype}}
            <option value="{{$authtype->name}}"{{if !$authtype->is_usable}} disabled="disabled"{{/if}}>{{$authtype->title}} - {{$authtype->description}}</option>
        {{/foreach}}
        </select>
    </span>
    <button class="btn btn-primary" type="button" onclick="addinstance(); return false;" name="button" value="foo">{{str tag=Add section=admin}}</button>
224
225
</div>

Naomi Guyer's avatar
Naomi Guyer committed
226
227
<input type="hidden" id="instancePriority" name="instancePriority" value="{{$instancestring}}" />
<input type="hidden" id="deleteList" name="deleteList" value="" />