Skip to content

Commit

Permalink
#954 Add the possibility to delete a custom field from the admin section
Browse files Browse the repository at this point in the history
  • Loading branch information
nadouani committed Sep 4, 2019
1 parent e00f4c5 commit d219cb0
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 6 deletions.
1 change: 1 addition & 0 deletions ui/app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@
<script src="scripts/services/Constants.js"></script>
<script src="scripts/services/CortexSrv.js"></script>
<script src="scripts/services/CustomFieldsCacheSrv.js"></script>
<script src="scripts/services/CustomFieldsSrv.js"></script>
<script src="scripts/services/DashboardSrv.js"></script>
<script src="scripts/services/EntitySrv.js"></script>
<script src="scripts/services/FileResource.js"></script>
Expand Down
55 changes: 54 additions & 1 deletion ui/app/scripts/controllers/admin/AdminCustomFieldsCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
'use strict';

angular.module('theHiveControllers').controller('AdminCustomFieldsCtrl',
function($scope, $uibModal, ListSrv, CustomFieldsCacheSrv, NotificationSrv) {
function($scope, $uibModal, ListSrv, CustomFieldsCacheSrv, NotificationSrv, ModalUtilsSrv, CustomFieldsSrv) {
var self = this;

self.reference = {
Expand Down Expand Up @@ -66,6 +66,59 @@
});
};

self.deleteField = function(customField) {
CustomFieldsSrv.usage(customField)
.then(function(response) {
var usage = response.data,
message,
isHtml = false;


if (usage.total === 0) {
message = 'Are you sure you want to delete this custom field?';
} else {
var segs = [
'Are you sure you want to delete this custom field?',
'<br />',
'<br />',
'This custom field is used by:',
'<ul>'
];

if(usage.case) {
segs.push('<li>' + usage.case + ' cases</li>');
}

if(usage.alert) {
segs.push('<li>' + usage.alert + ' alerts</li>');
}

if(usage.caseTemplate) {
segs.push('<li>' + usage.caseTemplate + ' case templates</li>');
}

segs.push('</ul>');

message = segs.join('');
isHtml = true;
}

return ModalUtilsSrv.confirm('Remove custom field', message, {
okText: 'Yes, remove it',
flavor: 'danger',
isHtml: isHtml
});
})
.then(function(response) {
return CustomFieldsSrv.removeField(customField);
})
.then(function() {
self.initCustomfields();
CustomFieldsCacheSrv.clearCache();
$scope.$emit('custom-fields:refresh');
});
};

self.initCustomfields();
});
})();
9 changes: 7 additions & 2 deletions ui/app/scripts/controllers/case/CaseCloseModalCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,20 @@
}), 'name');

return result;
}
};

$scope.initialize = function() {
CustomFieldsCacheSrv.all().then(function(fields) {
$scope.orderedFields = getTemplateCustomFields($scope.caze.customFields);
$scope.allCustomFields = fields;
$scope.allCustomFields = fields;

$scope.mandatoryFields = _.without(_.map($scope.orderedFields, function(cf) {
var fieldDef = fields[cf];

if(!fieldDef) {
return;
}

var fieldValue = $scope.caze.customFields[cf][cf.type];

if((fieldValue === undefined || fieldValue === null) && fieldDef.mandatory === true) {
Expand Down
17 changes: 17 additions & 0 deletions ui/app/scripts/services/CustomFieldsSrv.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
(function () {
'use strict';
angular.module('theHiveServices')
.factory('CustomFieldsSrv', function ($http) {

var factory = {
removeField: function (field) {
return $http.delete('./api/list/' + field.id);
},
usage: function(field) {
return $http.get('./api/customFields/' + field.reference);
}
};

return factory;
});
})();
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ <h4 class="vpad10 text-primary">
<a href ng-click="$vm.removeCustomField(cf)"><span class="pull-right text-danger"><i class="fa fa-trash"></i> Delete</span></a>
</div>
<div class="col-sm-6">
<select class="form-control" ng-model="cf.name" ng-options="item.reference as item.name for (key, item) in $vm.fields" required></select>
<select class="form-control" ng-model="cf.name" ng-options="item.reference as item.name for (key, item) in $vm.fields | orderObjectBy:'name'" required></select>
</div>
<div class="col-sm-6" ng-if="$vm.fields[cf.name].options.length > 0">
<select class="form-control" ng-model="cf.value" ng-options="v for v in $vm.fields[cf.name].options">
Expand Down
3 changes: 2 additions & 1 deletion ui/app/views/partials/admin/custom-fields.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ <h3 class="box-title">Case custom fields ({{$vm.customFields.length}})</h3>
<th>Description</th>
<th width="100" class="text-center">Mandatory</th>
<th width="200">Options</th>
<th width="80" class="text-center">Actions</th>
<th width="160" class="text-center">Actions</th>
</tr>
</thead>
<tbody>
Expand All @@ -42,6 +42,7 @@ <h3 class="box-title">Case custom fields ({{$vm.customFields.length}})</h3>
</td>
<td align="center">
<button class="btn btn-xs btn-primary" ng-click="$vm.showFieldDialog(field)"><i class="fa fa-pencil"></i> Edit</button>
<button class="btn btn-xs btn-danger" ng-click="$vm.deleteField(field)"><i class="fa fa-trash"></i> Delete</button>
</td>
</tr>
</tbody>
Expand Down
3 changes: 2 additions & 1 deletion ui/app/views/partials/utils/confirm.modal.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
<h3 class="modal-title">{{$modal.title}}</h3>
</div>
<div class="modal-body">
<p>{{$modal.message}}</p>
<p ng-if="!$modal.config.isHtml">{{$modal.message}}</p>
<p ng-if="$modal.config.isHtml" ng-bind-html="$modal.message"></p>
</div>
<div class="modal-footer text-left">
<button class="btn btn-default" ng-click="$modal.cancel()" type="button">Cancel</button>
Expand Down

0 comments on commit d219cb0

Please sign in to comment.