Scalr\Model\Entity\EventDefinition::getUsed PHP Метод

getUsed() публичный Метод

Checks whether the Event exists
public getUsed ( integer $accountId = null, integer $envId = null ) : array | false
$accountId integer optional Identifier of the account
$envId integer optional Identifier of the environment
Результат array | false Returns FALSE if the Event does not exist on account or Array otherwise. Array looks like ['rolesCount' => N, 'farmRolesCount' => M, 'webhooksCount' => Z, 'accountScriptsCount' => K]
    public function getUsed($accountId = null, $envId = null)
    {
        if (!empty($this->accountId)) {
            $accountId = $this->accountId;
            if (!empty($this->envId)) {
                $envId = $this->envId;
            }
        }
        $used = [];
        // Find role scripts that use this event definition
        $query = "SELECT COUNT(*) FROM role_scripts rs";
        $where = " WHERE rs.event_name = ?";
        $params = [$this->name];
        if (!empty($accountId)) {
            $query .= " JOIN roles r ON r.id = rs.role_id";
            $where .= " AND r.client_id = ?";
            $params[] = $accountId;
            if (!empty($envId)) {
                $where .= " AND r.env_id = ?";
                $params[] = $envId;
            }
        }
        $query .= $where;
        $used['rolesCount'] = $this->db()->GetOne($query, $params);
        // Find farm role scripts that use this event definition
        $query = "SELECT COUNT(*) FROM farm_role_scripts frs";
        $where = " WHERE frs.event_name = ?";
        $params = [$this->name];
        if (!empty($accountId)) {
            $query .= " JOIN farms f ON f.id = frs.farmid";
            $where .= " AND f.clientid = ?";
            $params[] = $accountId;
            if (!empty($envId)) {
                $where .= " AND f.env_id = ?";
                $params[] = $envId;
            }
        }
        $query .= $where;
        $used['farmRolesCount'] = $this->db()->GetOne($query, $params);
        // Find webhook config events that use this event definition
        $query = "SELECT COUNT(*) FROM webhook_config_events wce";
        $where = " WHERE wce.event_type = ?";
        $params = [$this->name];
        if (!empty($accountId)) {
            $query .= " JOIN webhook_configs wh ON wh.webhook_id = wce.webhook_id";
            $where .= " AND wh.account_id = ?";
            $params[] = $accountId;
            if (!empty($envId)) {
                $where .= " AND wh.env_id = ?";
                $params[] = $envId;
            }
        }
        $query .= $where;
        $used['webhooksCount'] = $this->db()->GetOne($query, $params);
        if (empty($envId)) {
            // Find account scripts that use this event definition
            $query = "SELECT COUNT(*) FROM account_scripts acs WHERE acs.event_name = ?";
            $params = [$this->name];
            if (!empty($accountId)) {
                $query .= " AND acs.account_id = ?";
                $params[] = $accountId;
            }
            $used['accountScriptsCount'] = $this->db()->GetOne($query, $params);
        } else {
            $used['accountScriptsCount'] = 0;
        }
        return $used['rolesCount'] != 0 || $used['farmRolesCount'] != 0 || $used['webhooksCount'] != 0 || $used['accountScriptsCount'] != 0 ? $used : false;
    }

Usage Example

Пример #1
0
 /**
  * @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');
 }