/**
* @param integer $id
* @param string $name
* @param string $description
* @param bool $replaceEvent
* @throws Exception
* @throws Scalr_Exception_Core
*/
public function xSaveAction($id = 0, $name, $description, $replaceEvent = false)
{
$this->request->restrictAccess(Acl::RESOURCE_GENERAL_CUSTOM_EVENTS, Acl::PERM_GENERAL_CUSTOM_EVENTS_MANAGE);
$validator = new \Scalr\UI\Request\Validator();
$validator->addErrorIf(!preg_match("/^[A-Za-z0-9]+\$/si", $name), 'name', "Name should contain only alphanumeric characters");
$validator->addErrorIf(strlen($name) > 25, 'name', "Name should be less than 25 characters");
$validator->addErrorIf(in_array($name, array_keys(EVENT_TYPE::getScriptingEvents())), 'name', sprintf("'%' is reserved name for event. Please select another one.", $name));
$scope = $this->request->getScope();
if (!$id) {
$criteria = [['name' => $name]];
if ($this->user->isScalrAdmin()) {
$criteria[] = ['accountId' => NULL];
} else {
$criteria[] = ['$or' => [['accountId' => $this->user->getAccountId()], ['accountId' => NULL]]];
if ($scope == 'account') {
$criteria[] = ['envId' => NULL];
} else {
$criteria[] = ['$or' => [['envId' => NULL], ['envId' => $this->getEnvironmentId(true)]]];
}
}
$validator->addErrorIf(EventDefinition::find($criteria)->count(), 'name', 'This name is already in use. Note that Event names are case-insensitive.');
// check replacements
$replacements = NULL;
if ($this->user->isScalrAdmin()) {
$replacements = EventDefinition::find([['name' => $name], ['accountId' => ['$ne' => NULL]]]);
} else {
if ($scope == 'account') {
$replacements = EventDefinition::find([['name' => $name], ['accountId' => $this->user->getAccountId()], ['envId' => ['$ne' => NULL]]]);
}
}
}
if (!$validator->isValid($this->response)) {
return;
}
if ($replacements && $replacements->count() && !$replaceEvent) {
$this->response->data(['replaceEvent' => true]);
$this->response->failure();
return;
}
if ($id) {
$event = EventDefinition::findPk($id);
/* @var $event EventDefinition */
if (!$event) {
throw new Exception('Event not found');
}
if ($this->user->isScalrAdmin() && $event->accountId == NULL && $event->envId == NULL || $this->user->isUser() && $event->accountId == $this->user->getAccountId() && ($event->envId == NULL || $event->envId == $this->getEnvironmentId())) {
$event->description = $description;
} else {
throw new Scalr_Exception_InsufficientPermissions();
}
$event->save();
} else {
$event = new EventDefinition();
if ($this->user->isScalrAdmin()) {
$event->accountId = NULL;
$event->envId = NULL;
} else {
$event->accountId = $this->user->getAccountId();
$event->envId = $scope == 'account' ? NULL : $this->getEnvironmentId();
}
$event->name = $name;
$event->description = $description;
$event->save();
if ($replacements) {
foreach ($replacements as $e) {
$e->delete();
}
}
}
$used = $event->getUsed($this->user->getAccountId(), $this->getEnvironmentId(true));
$this->response->data(['event' => ['id' => $event->id, 'name' => $event->name, 'description' => $event->description, 'used' => $used, 'scope' => $scope, 'status' => $used ? 'In use' : 'Not used']]);
$this->response->success('Custom event definition successfully saved');
}