/** * Request must be done via ajax * You can specify a fallback url OR 'route' with optional 'params' via 'fallback' key in route config: * Example: * Route::get('forgot_password/{param}', [ * 'middleware' => AjaxOnly::class, * 'fallback' => '/some/url' * // or * 'fallback' => '/some/url/{param}' * // or * 'fallback' => [ * 'route' => 'cmf_login', * 'params' => [] //< optional, can be array or boolean (by default === true: pass params from original url) * ], * ... * ] * If 'params' === true - all params retrieved from original URL will be passed to fallback route * If 'params' === false - all params retrieved from original URL will be passed to fallback route * * @param Request $request * @param \Closure $next * @return mixed * @throws \InvalidArgumentException */ public function handle(Request $request, Closure $next) { if (!$request->ajax()) { // maybe there is a fallback? $fallback = array_get($request->route()->getAction(), 'fallback', []); if (!empty($fallback) && is_string($fallback)) { return new RedirectResponse(StringUtils::insert($fallback, $request->route()->parameters(), ['before' => '{', 'after' => '}'])); } else { if (!empty($fallback['route'])) { $params = array_get($fallback, 'params', true); if ($params === true) { $params = $request->route()->parameters(); } else { if ($params instanceof \Closure) { $params = call_user_func($params, $request->route()->parameters()); } else { if ($params === false || !is_array($params)) { $params = []; } } } return new RedirectResponse(route($fallback['route'], $params)); } else { abort(HttpCode::FORBIDDEN, 'Only ajax requests'); } } } try { return $next($request); } catch (DbObjectValidationException $exc) { return new JsonResponse(['_message' => trans(CmfConfig::transBase('.error.invalid_data_received')), 'errors' => $exc->getValidationErrors()], HttpCode::INVALID); } catch (HttpException $exc) { if ($exc->getStatusCode() === HttpCode::INVALID) { $data = json_decode($exc->getMessage(), true); if (!empty($data) && !empty($data['_message'])) { return new JsonResponse(['_message' => $data['_message'], 'errors' => empty($data['errors']) ? [] : $data['errors']], HttpCode::INVALID); } } throw $exc; } }
/** * @param array $data * @param array $validators - supports inserts in format "{{id}}" where "id" can be any key from $data * @param array $messages * @param bool $isRevalidation * @param bool $isBulkEdit * @return array|string|bool * @throws \LogicException * * @throws ScaffoldActionException */ public function validateData(array $data, array $validators, array $messages = [], $isRevalidation = false, $isBulkEdit = false) { $success = $this->beforeValidate($data, $isRevalidation); if ($success !== true) { return $success; } if (!is_array($validators)) { throw new ScaffoldActionException($this, '$validators must be an array'); } if (empty($validators)) { return []; } if (empty($messages)) { $messages = CmfConfig::transCustom('.' . $this->getModel()->getTableName() . '.form.validation'); } if (!is_array($messages)) { $messages = []; } else { $messages = Set::flatten($messages); } $arrayFields = []; foreach ($validators as $key => &$value) { if (is_string($value)) { $value = StringUtils::insert($value, $data, ['before' => '{{', 'after' => '}}']); if (preg_match('%(^|\\|)array%i', $value)) { $arrayFields[] = $key; } } else { if (is_array($value)) { foreach ($value as &$validator) { if (is_string($validator)) { $validator = StringUtils::insert($value, $data, ['before' => '{{', 'after' => '}}']); } if ($validator === 'array') { $arrayFields[] = $key; } } unset($validator); } } } unset($value); $validator = \Validator::make($data, $validators, $messages); if ($validator->fails()) { $errors = $validator->getMessageBag()->toArray(); foreach ($errors as $field => $error) { if (in_array($field, $arrayFields, true)) { $errors[$field . '[]'] = $error; unset($errors[$field]); } } return $errors; } $success = $this->onValidationSuccess($data, $isRevalidation, $isBulkEdit); if ($success !== true) { return $success; } return []; }
/** * @param string $schema * @param string $queryTpl * @param array $tables - empty value: just execute $queryTpl * @param null|string $testQueryTpl * @throws \PeskyORM\Exception\DbException */ public function executeQueryOnSchema($schema, $queryTpl, array $tables = [], $testQueryTpl = null) { $this->out('DB Schema: ' . $schema); $ds = $this->getConnection(); if (!empty($tables)) { foreach ($tables as $tableName) { $this->out('Update table: ' . $tableName); $query = StringUtils::insert($queryTpl, ['table' => $tableName, 'schema' => $schema]); if (!empty($testQueryTpl)) { $test = StringUtils::insert($testQueryTpl, ['table' => $tableName, 'schema' => $schema]); $stmnt = $ds->query(DbExpr::create($test)); if ($stmnt) { $exists = Db::processRecords($stmnt, DB::FETCH_VALUE); if (!empty($exists)) { $this->out('- Object already exists'); } else { $ds->exec(DbExpr::create($query)); $this->out('+ Done'); } } else { $this->out('- Failed to test if Object already exists'); } } else { $ds->exec(DbExpr::create($query)); $this->out('+ Done'); } } } else { $query = $queryTpl; if (!empty($testQueryTpl)) { $test = StringUtils::insert($testQueryTpl, ['schema' => $schema]); $stmnt = $ds->query(DbExpr::create($test)); if ($stmnt) { $exists = Db::processRecords($stmnt, DB::FETCH_VALUE); if (!empty($exists)) { $this->out('- Object already exists'); } else { $ds->exec(DbExpr::create($query)); $this->out('+ Done'); } } else { $this->out('- Failed to test if Object already exists'); } } else { $ds->exec(DbExpr::create($query)); $this->out('+ Done'); } } $ds->disconnect(); }
/** * @param array $data * @param array $validators * @param array $messages * @param bool $isRevalidation * @return array * @throws ScaffoldActionException */ public function validateData(array $data, array $validators, array $messages = [], $isRevalidation = false) { $success = $this->beforeValidate($data, $isRevalidation); if ($success !== true) { return $success; } if (!is_array($validators)) { throw new ScaffoldActionException($this, '$validators must be an array'); } if (empty($validators)) { return []; } if (empty($messages)) { $messages = CmfConfig::transCustom('.' . $this->getModel()->getTableName() . '.form.validation'); } if (!is_array($messages)) { $messages = []; } else { $messages = Set::flatten($messages); } array_walk($validators, function (&$value, $key) use($data) { if (is_string($value)) { $value = StringUtils::insert($value, $data, ['before' => '{{', 'after' => '}}']); } }); $validator = \Validator::make($data, $validators, $messages); if ($validator->fails()) { return $validator->getMessageBag()->toArray(); } $success = $this->onValidationSuccess($data, $isRevalidation); if ($success !== true) { return $success; } return []; }