2018-10-25 16:51:20 +02:00
|
|
|
/* eslint-disable
|
|
|
|
handle-callback-err,
|
|
|
|
no-return-assign,
|
|
|
|
no-undef,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2018-10-25 16:50:16 +02:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
Application.Controllers.controller('GroupsController', ['$scope', 'groupsPromise', 'Group', 'growl', '_t', function ($scope, groupsPromise, Group, growl, _t) {
|
2018-11-21 09:42:26 +01:00
|
|
|
// List of users groups
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.groups = groupsPromise;
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2018-11-21 09:42:26 +01:00
|
|
|
// Default: we show only enabled groups
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.groupFiltering = 'enabled';
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2018-11-21 09:42:26 +01:00
|
|
|
// Available options for filtering groups by status
|
2017-10-05 17:12:22 +02:00
|
|
|
$scope.filterDisabled = [
|
|
|
|
'enabled',
|
|
|
|
'disabled',
|
2018-10-25 16:51:20 +02:00
|
|
|
'all'
|
2018-11-21 11:08:53 +01:00
|
|
|
];
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2018-11-21 09:42:26 +01:00
|
|
|
/**
|
|
|
|
* Removes the newly inserted but not saved group / Cancel the current group modification
|
|
|
|
* @param rowform {Object} see http://vitalets.github.io/angular-xeditable/
|
|
|
|
* @param index {number} group index in the $scope.groups array
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.cancelGroup = function (rowform, index) {
|
2018-10-25 16:50:16 +02:00
|
|
|
if ($scope.groups[index].id != null) {
|
2018-11-21 11:08:53 +01:00
|
|
|
return rowform.$cancel();
|
2018-10-25 16:50:16 +02:00
|
|
|
} else {
|
2018-11-21 11:08:53 +01:00
|
|
|
return $scope.groups.splice(index, 1);
|
2018-10-25 16:50:16 +02:00
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
};
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2018-11-21 09:42:26 +01:00
|
|
|
/**
|
|
|
|
* Creates a new empty entry in the $scope.groups array
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.addGroup = function () {
|
2016-03-23 18:39:41 +01:00
|
|
|
$scope.inserted =
|
2018-11-21 11:08:53 +01:00
|
|
|
{ name: '' };
|
|
|
|
return $scope.groups.push($scope.inserted);
|
|
|
|
};
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-21 09:42:26 +01:00
|
|
|
/**
|
|
|
|
* Saves a new group / Update an existing group to the server (form validation callback)
|
|
|
|
* @param data {Object} group name
|
|
|
|
* @param [id] {number} group id, in case of update
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.saveGroup = function (data, id) {
|
2018-10-25 16:50:16 +02:00
|
|
|
if (id != null) {
|
2019-12-18 13:04:38 +01:00
|
|
|
return Group.update({ id }, { group: data }, response => growl.success(_t('app.admin.members.group_form.changes_successfully_saved'))
|
|
|
|
, error => growl.error(_t('app.admin.members.group_form.an_error_occurred_while_saving_changes')));
|
2018-10-25 16:50:16 +02:00
|
|
|
} else {
|
2018-10-25 16:51:20 +02:00
|
|
|
return Group.save({ group: data }, function (resp) {
|
2019-12-18 13:04:38 +01:00
|
|
|
growl.success(_t('app.admin.members.group_form.new_group_successfully_saved'));
|
2018-11-21 11:08:53 +01:00
|
|
|
return $scope.groups[$scope.groups.length - 1].id = resp.id;
|
2018-10-25 16:50:16 +02:00
|
|
|
}
|
2018-10-25 16:51:20 +02:00
|
|
|
, function (error) {
|
2019-12-18 13:04:38 +01:00
|
|
|
growl.error(_t('app.admin.members.group_form.an_error_occurred_when_saving_the_new_group'));
|
2018-11-21 11:08:53 +01:00
|
|
|
return $scope.groups.splice($scope.groups.length - 1, 1);
|
|
|
|
});
|
2018-10-25 16:50:16 +02:00
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
};
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-21 09:42:26 +01:00
|
|
|
/**
|
|
|
|
* Deletes the group at the specified index
|
|
|
|
* @param index {number} group index in the $scope.groups array
|
|
|
|
*/
|
2018-10-25 16:50:16 +02:00
|
|
|
$scope.removeGroup = index =>
|
2018-10-25 16:51:20 +02:00
|
|
|
Group.delete({ id: $scope.groups[index].id }, function (resp) {
|
2019-12-18 13:04:38 +01:00
|
|
|
growl.success(_t('app.admin.members.group_form.group_successfully_deleted'));
|
2018-11-21 11:08:53 +01:00
|
|
|
return $scope.groups.splice(index, 1);
|
2018-10-25 16:50:16 +02:00
|
|
|
}
|
2019-12-18 13:04:38 +01:00
|
|
|
, error => growl.error(_t('app.admin.members.group_form.unable_to_delete_group_because_some_users_and_or_groups_are_still_linked_to_it')));
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-21 09:42:26 +01:00
|
|
|
/**
|
|
|
|
* Enable/disable the group at the specified index
|
|
|
|
* @param index {number} group index in the $scope.groups array
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
return $scope.toggleDisableGroup = function (index) {
|
2018-11-21 11:08:53 +01:00
|
|
|
const group = $scope.groups[index];
|
2018-10-25 16:50:16 +02:00
|
|
|
if (!group.disabled && (group.users > 0)) {
|
2019-12-18 13:04:38 +01:00
|
|
|
return growl.error(_t('app.admin.members.group_form.unable_to_disable_group_with_users', { USERS: group.users }));
|
2018-10-25 16:50:16 +02:00
|
|
|
} else {
|
2018-10-25 16:51:20 +02:00
|
|
|
return Group.update({ id: group.id }, { group: { disabled: !group.disabled } }, function (response) {
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.groups[index] = response;
|
2019-12-18 13:04:38 +01:00
|
|
|
return growl.success(_t('app.admin.members.group_form.group_successfully_enabled_disabled', { STATUS: response.disabled }));
|
2018-10-25 16:50:16 +02:00
|
|
|
}
|
2019-12-18 13:04:38 +01:00
|
|
|
, error => growl.error(_t('app.admin.members.group_form.unable_to_enable_disable_group', { STATUS: !group.disabled })));
|
2018-10-25 16:50:16 +02:00
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
};
|
2018-10-25 16:50:16 +02:00
|
|
|
}
|
|
|
|
|
2018-11-21 11:08:53 +01:00
|
|
|
]);
|