opensupports/server/controllers/system/delete-custom-field.php

61 lines
1.5 KiB
PHP
Raw Normal View History

2019-02-03 20:47:29 +01:00
<?php
use Respect\Validation\Validator as DataValidator;
/**
* @api {post} /system/delete-custom-field Delete custom field
* @apiVersion 4.10.0
2019-02-03 20:47:29 +01:00
*
* @apiName Delete a custom field
*
* @apiGroup System
*
* @apiDescription This path deletes a custom field and all its uses.
*
* @apiPermission staff2
*
* @apiParam {Number} id Id of the custom field to delete.
*
* @apiUse NO_PERMISSION
* @apiUse INVALID_CUSTOM_FIELD
*
* @apiSuccess {Object} data Empty object
*
*/
class DeleteCustomFieldController extends Controller {
const PATH = '/delete-custom-field';
const METHOD = 'POST';
public function validations() {
return [
'permission' => 'staff_2',
'requestData' => [
'id' => [
'validation' => DataValidator::dataStoreId('customfield'),
'error' => ERRORS::INVALID_CUSTOM_FIELD,
],
]
];
}
public function handler() {
$customField = Customfield::getDataStore(Controller::request('id'));
2019-02-13 03:37:27 +01:00
foreach(User::getAll() as $user) {
$customFieldValueList = $user->xownCustomfieldvalueList ? $user->xownCustomfieldvalueList : [];
2019-02-03 20:47:29 +01:00
foreach($customFieldValueList as $customFieldValue) {
if($customFieldValue->customfield->id == $customField->id) {
$user->xownCustomfieldvalueList->remove($customFieldValue);
}
}
2019-02-13 03:37:27 +01:00
$user->store();
2019-02-03 20:47:29 +01:00
}
$customField->delete();
2019-02-13 03:37:27 +01:00
Response::respondSuccess();
2019-02-03 20:47:29 +01:00
}
}