/** * Akce pro přidání pravidel do rulesetu * @param int $id * @param string|int $rules - ID pravidel oddělená čárkami či středníky * @param string $relation = 'positive' * @param string $result = "simple" (varianty "simple", "rules") */ public function actionAddRules($id, $rules, $relation = RuleSetRuleRelation::RELATION_POSITIVE, $result = "simple") { //najití RuleSetu a kontroly $ruleSet = $this->ruleSetsFacade->findRuleSet($id); $this->ruleSetsFacade->checkRuleSetAccess($ruleSet, $this->user->id); /** @var int[] $ruleIdsArr */ $ruleIdsArr = explode(',', str_replace(';', ',', $rules)); if (!empty($ruleIdsArr)) { foreach ($ruleIdsArr as $ruleId) { if (!$ruleId) { continue; } try { $rule = $this->rulesFacade->findRule($ruleId); $this->ruleSetsFacade->addRuleToRuleSet($rule, $ruleSet, $relation); } catch (\Exception $e) { continue; } } } if ($result == "rules") { $result = ['ruleset' => $ruleSet->getDataArr(), 'rules' => []]; $result['rules'] = $this->prepareRulesResult($ruleIdsArr, $ruleSet); $this->sendJsonResponse($result); } else { $this->sendJsonResponse(['state' => 'ok']); } }
/** * Akce pro přidání pravidel do rulesetu * @param int $id - ID rule setu * @param int|string $rules - ID pravidel oddělená čárkami či středníky * @param string $relation = 'positive' * @throws \Nette\Application\BadRequestException * @SWG\Post( * tags={"RuleSets"}, * path="/rule-sets/{id}/rules", * summary="Add rules into the selected rule set", * security={{"apiKey":{}},{"apiKeyHeader":{}}}, * produces={"application/json","application/xml"}, * @SWG\Parameter( * name="id", * description="RuleSet ID", * required=true, * type="integer", * in="path" * ), * @SWG\Parameter( * name="rules", * description="IDs of rules (optinally multiple - separated with , or ;)", * required=true, * type="array", * @SWG\items( * type="integer" * ), * collectionFormat="csv", * in="query" * ), * @SWG\Parameter( * name="relation", * description="positive|neutral|negative", * required=false, * type="string", * enum={"positive","negative","neutral"}, * in="query" * ), * @SWG\Response( * response=201, * description="Rules have been successfully added to the rule set.", * @SWG\Schema(ref="#/definitions/StatusResponse"), * examples={"code":201,"status":"OK"} * ), * @SWG\Response(response=404, description="Requested rule set was not found.") * ) */ public function actionCreateRules($id, $rules, $relation = RuleSetRuleRelation::RELATION_POSITIVE) { /** @var RuleSet $ruleSet */ $ruleSet = $this->findRuleSetWithCheckAccess($id); /** @var int[] $ruleIdsArr */ $ruleIdsArr = explode(',', str_replace(';', ',', $rules)); if (!empty($ruleIdsArr)) { foreach ($ruleIdsArr as $ruleId) { if (!$ruleId) { continue; } try { $rule = $this->rulesFacade->findRule($ruleId); $this->ruleSetsFacade->addRuleToRuleSet($rule, $ruleSet, $relation); } catch (\Exception $e) { continue; } } } $this->resource = ['code' => 200, 'status' => 'OK']; $this->sendResource(); }