archivalProfileAccess.html 11.9 KB
Newer Older
1
<?merge hideAccess.bool().not() ?>
2
3
4
<div class="panel-group" id="profile_accordion" role="tablist" aria-multiselectable="true">
    <div class="row">
        <br/>
5
6
7
8
9
        <div class="col-md-12">
            <div class="col-md-12" >
                <div class="form-group" id="originatorAgencyHolder" style="display: none;">
                    <input type="checkbox" id="isOriginatorAgency" name="isOriginatorAgency" data-toggle="toggle" data-width="150" checked>
                </div>
10
11
                <div class="form-group">
                    <label>Archival profile</label>
Dylan's avatar
Dylan committed
12
13
                    <select class="form-control" id="profileReference">
                        <option value=""></option>
14
15
                        <option value="*">Accept archives without profile</option>
                        <option disabled="disabled">----</option>
Dylan's avatar
Dylan committed
16
17
18
                        <?merge archivalProfile ?>
                        <option value="[?merge .reference ?]"><?merge .name ?></option>
                    </select>
19
                </div>
Dylan's avatar
Dylan committed
20
21
22
                <div class="form-group">
                    <label>Service level</label>
                    <select class="form-control" id="serviceLevelReference">
23
                        <option value="">Default service level</option>
Dylan's avatar
Dylan committed
24
25
26
27
                        <?merge serviceLevel ?>
                        <option value="[?merge .reference ?]"><?merge .reference ?></option>
                    </select>
                </div>
28
29
30
                <div>
                <div class="form-group" id="extensions"></div>
                </div>
31
                <div class="form-group text-right">
32
33
34
35
                    <button class="btn btn-success" id="addArchivalProfile" type="button" style="display: none;"><i
                        class="fa fa-plus">&nbsp;</i>Add</button>
                    <button class="btn btn-warning" id="updateArchivalProfile" type="button" style="display: none;"><i
                        class="fa fa-edit">&nbsp;</i>Save</button>
36
37
38
                </div>
            </div>
            <div class="col-md-12">
39
                <p data-translate-catalog="organization/archivalProfileAccess"><strong>Archival profiles list</strong></p>
40
                <ul class="list-group" id="accessEntries"></ul>
41
            </div>
42
43
44
        </div>
    </div>
</div>
45
<span class="hidden" id="errorEmptyArchivalProfileField" data-translate-catalog="organization/archivalProfileAccess">Archival Profile field cannot be empty</span>
46

47
<script src="/public/js/bootstrap-toggle/bootstrap-toggle.js"></script>
48
49
<script>

50
51
52
53
    var ArchivalProfileAccessForm = {
        load: function(profileAccess) {
            if (profileAccess) {
                $.each(profileAccess, function() {
54
                    ArchivalProfileAccessForm.addToList(this);
55
56
57
58
                });

                $.each($('#accessEntries li'), function() {
                    $(this).removeClass('active');
59
60
61
                })
            }
        },
62

63
        save: function (type) {
64
            var archivalProfileAccess = this.serialize();
65

66
            if (!archivalProfileAccess.archivalProfileReference) {
67
                gritter.show($('#errorEmptyArchivalProfileField').text(), false);
68
69
                return;
            }
70

71
            $('#checkboxes_holder').find('li').each(function () {
72
73
74
                if (typeof $(this).data('json') !== "undefined") {
                    archivalProfileAccess.push($(this).data('json'));
                }
75
            });
76
            json_archival = JSON.stringify({'archivalProfileAccess': archivalProfileAccess});
Dylan's avatar
Dylan committed
77

78
79
80
81
82
83
84
85
86
            $.ajax({
                type: type,
                url: '/archivalprofileaccess',
                data: json_archival,
                dataType: 'json',
                contentType: 'application/json',
                async: true,
                success: function (response) {
                    if (response.status === true) {
87
                        $.each($('#accessEntries li'), function() {
88
                            if($(this).data('json').archivalProfileReference === archivalProfileAccess.archivalProfileReference) {
89
                                ArchivalProfileAccessForm.removeFromList($(this).closest('li'), false);
90
91
                            }
                        });
92
                        ArchivalProfileAccessForm.addToList(archivalProfileAccess);
93
                        window.scrollTo(0, 0);
94
95
                    }
                    gritter.show(response.message, response.status, response.errors);
96
                    $('#addArchivalProfile').addClass('hide');
97
                    $('#actionsListStatus').addClass("hide");
98
                    $('#subProfile').addClass("hide");
99
                    $('#updateArchivalProfile').addClass('hide');
Alexandre Goldstein's avatar
Alexandre Goldstein committed
100
101
102
103

                    $('#profileReference').val("");
                    $('#serviceLevelReference').val("");

104
105
106
107
108
109
                },
                error: function (response) {
                    gritter.show(response.responseJSON.message, response.responseJSON.status, response.responseJSON.errors);
                }
            });
        },
Dylan's avatar
Dylan committed
110

111
112
113
114
        addToList: function(profileAccess) {
            var selectedOption = $('#profileReference').find('option[value="' + profileAccess.archivalProfileReference + '"]');
            var selectedOptionServiceLevel = $('#serviceLevelReference').find('option[value="' + profileAccess.serviceLevelReference + '"]');

115
            var selectedOptions = selectedOption.text();
116
117
118

            if(!selectedOptionServiceLevel.text()){
                selectedOptions = selectedOption.text();
119
120
            }

121
122
            var li = $('<li/>').addClass('list-group-item clearfix')
            .data('json', profileAccess)
123
124
            .html(selectedOptions)
            .prop('title', selectedOptionServiceLevel.text());
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
            var span = $('<span/>').addClass('pull-right').appendTo(li);

            $('<a/>').attr('href','#')
            .addClass('text-danger removeProfileAccess')
            .html(
                $('<i/>').addClass("fa fa-times fa-fw")
                )
            .appendTo(span);

            li.appendTo($("#accessEntries"));

            selectedOption.addClass('hide');
        },

        delete: function (orgId, archivalProfileReference, li) {
            $.ajax({
                type: 'DELETE',
                url: '/archivalprofileaccess',
                data: JSON.stringify({orgId: orgId, archivalProfileReference: archivalProfileReference}),
                dataType: 'json',
                contentType: 'application/json',
                success: function (response) {
                    gritter.show(response.message, response.status, response.errors);
                    ArchivalProfileAccessForm.removeFromList(li);
                    $('#checkboxes_holder').empty();
151
152
                    $('#addArchivalProfile').addClass("hide");
                    $('#updateArchivalProfile').addClass("hide");
153
                    $('#profileReference').val("");
154
155
                    $('#actionsListStatus').addClass("hide");
                    $('#subProfile').addClass("hide");
156
157
158
159
160
161
162
163
164
165
166
167
168
                },
                error: function (response) {
                    gritter.show(response.responseJSON.message, response.responseJSON.status, response.responseJSON.errors);
                }
            });
        },

        close: function () {
            $('#profileReference').val('').find('.hide').removeClass('hide');
            $('#accessEntries').find('li').remove();
        },

        retrieveUserAccess: function () {
169
170
171
            var userAccesses = {processingStatuses: {}, subProfile: {}};
            var status_inputs = $('#actionsListStatus').find('#checkboxes_holder').find("input[type='checkbox']");
            $.merge(status_inputs, $('#subProfile').find('#checkboxes_holder').find("input[type='checkbox']"));
172
173
174

            status_inputs.each(function (key, status_input) {
                if (status_input.checked === true) {
175
176
177
178
179
180
181
182
183
184
                    switch (this.name) {
                        case 'processingStatus' :
                            var statusId = Object.keys($(this).data('json'))[0];
                            var action = {};
                            var action_inputs = $(this).siblings('ul').find("input[type='checkbox']");
                            action_inputs.each(function (action_key, action_input) {
                                if (action_input.checked === true) {
                                    if (action_input.name === 'action') {
                                        action[Object.keys($(this).data('json'))[0]] = {};
                                    }
185
                                }
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
                            });

                            userAccesses.processingStatuses[statusId] = {actions: action };
                            break;
                        case 'history' :
                            userAccesses.history = {};
                            break;
                        case 'subProfile' :
                            var subProfileId = $(this).data('json').reference;
                            userAccesses.subProfile[subProfileId] = {};
                            var subProfileChildren_inputs = $(this).siblings('ul').find("input[type='checkbox']");
                            subProfileChildren_inputs.each(function (subProfileChildren_key, subProfileChildren_input) {
                                if (subProfileChildren_input.checked === true) {
                                    var subProfileChildren_reference = $(this).data('json').reference;
                                    userAccesses.subProfile[subProfileId][subProfileChildren_reference] = {};
                                }
                            });
                            break;
204
205
206
207
208
209
210
                    }
                }
            });

            return userAccesses
        },

211
212
213
214
215
        removeFromList: function(li, removeFromSelect) {
            // to inititate default value and avoid bug on IE 11
            if (removeFromSelect === undefined) {
                removeFromSelect = true;
            }
216
217
218
            if (removeFromSelect) {
                $('#profileReference').val('').find('option[value="'+li.data('json').archivalProfileReference+'"]').removeClass('hide');
            }
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
            li.remove();
        },

        serialize: function () {
            var archivalProfileReference = $('#profileReference').val();
            var object = {
                orgId: $("#modifyOrganization").data('organization').orgId,
                archivalProfileReference: archivalProfileReference,
                originatorAccess: true,
                serviceLevelReference: null,
                userAccess: null
            };

            if (archivalProfileReference !== "*") {
                object.originatorAccess = $('#isOriginatorAgency').prop('checked');
                object.serviceLevelReference = $('#serviceLevelReference').val();
                object.userAccess = ArchivalProfileAccessForm.retrieveUserAccess();
236
            }
237
238
239

            return object;
        }
240
241
    }

242
243
244
    $('#addArchivalProfile').on('click', function () {
        ArchivalProfileAccessForm.save('POST');
    });
245

246
247
248
249
250
251
252
    $('#updateArchivalProfile').on('click', function () {
        ArchivalProfileAccessForm.save('PUT');
    });

    $('#accessEntries').on('click', '.removeProfileAccess', function (e) {
        var archivalProfileAccess = $(this).parents('li').data('json');
        ArchivalProfileAccessForm.delete(archivalProfileAccess.orgId, archivalProfileAccess.archivalProfileReference, $(this).closest('li'));
253
        $('#serviceLevelReference').parent().show();
254
255
        e.stopPropagation();
    });
256

257
258
259
    $('#organizationInfo_profileTab').on('click', function() {
        $('#profileReference').val('');
        $('#accessEntries li').removeClass('active');
260
        $('#serviceLevelReference').val('');
261
262
    });

263
264
265
266
    $('#organizationInfo_profileTab').on('click', function() {
        $('#updateArchivalProfile').addClass('hide');
        $('#addArchivalProfile').addClass('hide');
    });
267
</script>