2016-03-23 18:39:41 +01:00
|
|
|
Application.Controllers.controller "GroupsController", ["$scope", 'groupsPromise', 'Group', 'growl', '_t', ($scope, groupsPromise, Group, growl, _t) ->
|
|
|
|
|
|
|
|
## List of users groups
|
|
|
|
$scope.groups = groupsPromise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
# 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
|
|
|
|
##
|
|
|
|
$scope.cancelGroup = (rowform, index) ->
|
|
|
|
if $scope.groups[index].id?
|
|
|
|
rowform.$cancel()
|
|
|
|
else
|
|
|
|
$scope.groups.splice(index, 1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
# Creates a new empty entry in the $scope.groups array
|
|
|
|
##
|
|
|
|
$scope.addGroup = ->
|
|
|
|
$scope.inserted =
|
|
|
|
name: ''
|
|
|
|
$scope.groups.push($scope.inserted)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
# Saves a new group / Update an existing group to the server (form validation callback)
|
|
|
|
# @param data {Object} group name
|
2017-10-05 16:48:18 +02:00
|
|
|
# @param [id] {number} group id, in case of update
|
2016-03-23 18:39:41 +01:00
|
|
|
##
|
|
|
|
$scope.saveGroup = (data, id) ->
|
|
|
|
if id?
|
|
|
|
Group.update {id: id}, { group: data }, (response) ->
|
2017-10-05 16:48:18 +02:00
|
|
|
growl.success(_t('group_form.changes_successfully_saved'))
|
2016-03-23 18:39:41 +01:00
|
|
|
, (error) ->
|
2017-10-05 16:48:18 +02:00
|
|
|
growl.error(_t('group_form.an_error_occurred_while_saving_changes'))
|
2016-03-23 18:39:41 +01:00
|
|
|
else
|
|
|
|
Group.save { group: data }, (resp)->
|
2017-10-05 16:48:18 +02:00
|
|
|
growl.success(_t('group_form.new_group_successfully_saved'))
|
2016-03-23 18:39:41 +01:00
|
|
|
$scope.groups[$scope.groups.length-1].id = resp.id
|
|
|
|
, (error) ->
|
2017-10-05 16:48:18 +02:00
|
|
|
growl.error(_t('.group_forman_error_occurred_when_saving_the_new_group'))
|
2016-03-23 18:39:41 +01:00
|
|
|
$scope.groups.splice($scope.groups.length-1, 1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
# Deletes the group at the specified index
|
|
|
|
# @param index {number} group index in the $scope.groups array
|
|
|
|
##
|
|
|
|
$scope.removeGroup = (index) ->
|
|
|
|
Group.delete { id: $scope.groups[index].id }, (resp) ->
|
2017-10-05 16:48:18 +02:00
|
|
|
growl.success(_t('group_form.group_successfully_deleted'))
|
2016-03-23 18:39:41 +01:00
|
|
|
$scope.groups.splice(index, 1)
|
|
|
|
, (error) ->
|
2017-10-05 16:48:18 +02:00
|
|
|
growl.error(_t('group_form.unable_to_delete_group_because_some_users_and_or_groups_are_still_linked_to_it'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
# Enable/disable the group at the specified index
|
|
|
|
# @param index {number} group index in the $scope.groups array
|
|
|
|
##
|
|
|
|
$scope.toggleDisableGroup = (index) ->
|
|
|
|
group = $scope.groups[index]
|
|
|
|
Group.update {id: group.id}, { group: { disabled: !group.disabled } }, (response) ->
|
|
|
|
$scope.groups[index] = response
|
|
|
|
growl.success(_t('group_form.group_successfully_enabled_disabled', { STATUS: response.disabled }, 'messageformat'))
|
|
|
|
, (error) ->
|
|
|
|
growl.error(_t('group_form.unable_to_enable_disable_group', { STATUS: !group.disabled }, 'messageformat'))
|
2016-03-23 18:39:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
]
|