/**
  * Creates a new model.
  * @return \Leaps\Db\ActiveRecordInterface the model newly created
  * @throws ServerErrorHttpException if there is any error when creating the model
  */
 public function run()
 {
     if ($this->checkAccess) {
         call_user_func($this->checkAccess, $this->id);
     }
     /* @var $model \Leaps\Db\ActiveRecord */
     $model = new $this->modelClass(['scenario' => $this->scenario]);
     $model->load(Leaps::$app->getRequest()->getBodyParams(), '');
     if ($model->save()) {
         $response = Leaps::$app->getResponse();
         $response->setStatusCode(201);
         $id = implode(',', array_values($model->getPrimaryKey(true)));
         $response->getHeaders()->set('Location', Url::toRoute([$this->viewAction, 'id' => $id], true));
     } elseif (!$model->hasErrors()) {
         throw new ServerErrorHttpException('Failed to create the object for unknown reason.');
     }
     return $model;
 }
 /**
  *
  * @param AssetBundle $bundle
  * @param string $asset
  * @return string|boolean
  */
 protected function resolveAsset($bundle, $asset)
 {
     if (isset($this->assetMap[$asset])) {
         return $this->assetMap[$asset];
     }
     if ($bundle->sourcePath !== null && Url::isRelative($asset)) {
         $asset = $bundle->sourcePath . '/' . $asset;
     }
     $n = mb_strlen($asset);
     foreach ($this->assetMap as $from => $to) {
         $n2 = mb_strlen($from);
         if ($n2 <= $n && substr_compare($asset, $from, $n - $n2, $n2) === 0) {
             return $to;
         }
     }
     return false;
 }
Beispiel #3
0
 /**
  * Renders the content of a menu item.
  * Note that the container and the sub-menus are not rendered here.
  * @param array $item the menu item to be rendered. Please refer to [[items]] to see what data might be in the item.
  * @return string the rendering result
  */
 protected function renderItem($item)
 {
     if (isset($item['url'])) {
         $template = ArrayHelper::getValue($item, 'template', $this->linkTemplate);
         return strtr($template, ['{url}' => Html::encode(Url::to($item['url'])), '{label}' => $item['label']]);
     } else {
         $template = ArrayHelper::getValue($item, 'template', $this->labelTemplate);
         return strtr($template, ['{label}' => $item['label']]);
     }
 }
Beispiel #4
0
 /**
  * Redirects the browser to the specified URL.
  * This method is a shortcut to [[Response::redirect()]].
  *
  * You can use it in an action by returning the [[Response]] directly:
  *
  * ```php
  * // stop executing this action and redirect to login page
  * return $this->redirect(['login']);
  * ```
  *
  * @param string|array $url the URL to be redirected to. This can be in one of the following formats:
  *
  * - a string representing a URL (e.g. "http://example.com")
  * - a string representing a URL alias (e.g. "@example.com")
  * - an array in the format of `[$route, ...name-value pairs...]` (e.g. `['site/index', 'ref' => 1]`)
  *   [[Url::to()]] will be used to convert the array into a URL.
  *
  * Any relative URL will be converted into an absolute one by prepending it with the host info
  * of the current request.
  *
  * @param integer $statusCode the HTTP status code. Defaults to 302.
  * See <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html>
  * for details about HTTP status code
  * @return Response the current response object
  */
 public function redirect($url, $statusCode = 302)
 {
     return Leaps::$app->getResponse()->redirect(Url::to($url), $statusCode);
 }
 /**
  * Creates a URL for the given action and model.
  * This method is called for each button and each row.
  * @param string $action the button name (or action ID)
  * @param \Leaps\Db\ActiveRecord $model the data model
  * @param mixed $key the key associated with the data model
  * @param integer $index the current row index
  * @return string the created URL
  */
 public function createUrl($action, $model, $key, $index)
 {
     if ($this->urlCreator instanceof Closure) {
         return call_user_func($this->urlCreator, $action, $model, $key, $index);
     } else {
         $params = is_array($key) ? $key : ['id' => (string) $key];
         $params[0] = $this->controller ? $this->controller . '/' . $action : $action;
         return Url::toRoute($params);
     }
 }
Beispiel #6
0
 /**
  * Redirects the browser to the specified URL.
  *
  * This method adds a "Location" header to the current response. Note that it does not send out
  * the header until [[send()]] is called. In a controller action you may use this method as follows:
  *
  * ~~~
  * return Leaps::$app->getResponse()->redirect($url);
  * ~~~
  *
  * In other places, if you want to send out the "Location" header immediately, you should use
  * the following code:
  *
  * ~~~
  * Leaps::$app->getResponse()->redirect($url)->send();
  * return;
  * ~~~
  *
  * In AJAX mode, this normally will not work as expected unless there are some
  * client-side JavaScript code handling the redirection. To help achieve this goal,
  * this method will send out a "X-Redirect" header instead of "Location".
  *
  * If you use the "leaps" JavaScript module, it will handle the AJAX redirection as
  * described above. Otherwise, you should write the following JavaScript code to
  * handle the redirection:
  *
  * ~~~
  * $document.ajaxComplete(function (event, xhr, settings) {
  *     var url = xhr.getResponseHeader('X-Redirect');
  *     if (url) {
  *         window.location = url;
  *     }
  * });
  * ~~~
  *
  * @param string|array $url the URL to be redirected to. This can be in one of the following formats:
  *
  * - a string representing a URL (e.g. "http://example.com")
  * - a string representing a URL alias (e.g. "@example.com")
  * - an array in the format of `[$route, ...name-value pairs...]` (e.g. `['site/index', 'ref' => 1]`).
  *   Note that the route is with respect to the whole application, instead of relative to a controller or module.
  *   [[Url::to()]] will be used to convert the array into a URL.
  *
  * Any relative URL will be converted into an absolute one by prepending it with the host info
  * of the current request.
  *
  * @param integer $statusCode the HTTP status code. Defaults to 302.
  * See <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html>
  * for details about HTTP status code
  * @param boolean $checkAjax whether to specially handle AJAX (and PJAX) requests. Defaults to true,
  * meaning if the current request is an AJAX or PJAX request, then calling this method will cause the browser
  * to redirect to the given URL. If this is false, a `Location` header will be sent, which when received as
  * an AJAX/PJAX response, may NOT cause browser redirection.
  * @return $this the response object itself
  */
 public function redirect($url, $statusCode = 302, $checkAjax = true)
 {
     if (is_array($url) && isset($url[0])) {
         // ensure the route is absolute
         $url[0] = '/' . ltrim($url[0], '/');
     }
     $url = Url::to($url);
     if (strpos($url, '/') === 0 && strpos($url, '//') !== 0) {
         $url = Leaps::$app->getRequest()->getHostInfo() . $url;
     }
     if ($checkAjax) {
         if (Leaps::$app->getRequest()->getIsPjax()) {
             $this->getHeaders()->set('X-Pjax-Url', $url);
         } elseif (Leaps::$app->getRequest()->getIsAjax()) {
             $this->getHeaders()->set('X-Redirect', $url);
         } else {
             $this->getHeaders()->set('Location', $url);
         }
     } else {
         $this->getHeaders()->set('Location', $url);
     }
     $this->setStatusCode($statusCode);
     return $this;
 }
Beispiel #7
0
 /**
  * Returns the options for the grid view JS widget.
  * @return array the options
  */
 protected function getClientOptions()
 {
     $filterUrl = isset($this->filterUrl) ? $this->filterUrl : Leaps::$app->request->url;
     $id = $this->filterRowOptions['id'];
     $filterSelector = "#{$id} input, #{$id} select";
     if (isset($this->filterSelector)) {
         $filterSelector .= ', ' . $this->filterSelector;
     }
     return ['filterUrl' => Url::to($filterUrl), 'filterSelector' => $filterSelector];
 }
Beispiel #8
0
 /**
  * Returns the options for the captcha JS widget.
  * @return array the options
  */
 protected function getClientOptions()
 {
     $route = $this->captchaAction;
     if (is_array($route)) {
         $route[CaptchaAction::REFRESH_GET_VAR] = 1;
     } else {
         $route = [$route, CaptchaAction::REFRESH_GET_VAR => 1];
     }
     $options = ['refreshUrl' => Url::toRoute($route), 'hashKey' => "leapsCaptcha/{$route[0]}"];
     return $options;
 }
Beispiel #9
0
 /**
  * Returns the options for the form JS widget.
  * @return array the options
  */
 protected function getClientOptions()
 {
     $options = ['encodeErrorSummary' => $this->encodeErrorSummary, 'errorSummary' => '.' . implode('.', preg_split('/\\s+/', $this->errorSummaryCssClass, -1, PREG_SPLIT_NO_EMPTY)), 'validateOnSubmit' => $this->validateOnSubmit, 'errorCssClass' => $this->errorCssClass, 'successCssClass' => $this->successCssClass, 'validatingCssClass' => $this->validatingCssClass, 'ajaxParam' => $this->ajaxParam, 'ajaxDataType' => $this->ajaxDataType, 'scrollToError' => $this->scrollToError];
     if ($this->validationUrl !== null) {
         $options['validationUrl'] = Url::to($this->validationUrl);
     }
     // only get the options that are different from the default ones (set in leaps.activeForm.js)
     return array_diff_assoc($options, ['encodeErrorSummary' => true, 'errorSummary' => '.error-summary', 'validateOnSubmit' => true, 'errorCssClass' => 'has-error', 'successCssClass' => 'has-success', 'validatingCssClass' => 'validating', 'ajaxParam' => 'ajax', 'ajaxDataType' => 'json', 'scrollToError' => true]);
 }
 /**
  * Runs the action.
  */
 public function run()
 {
     if (Leaps::$app->request->getQueryParam(self::REFRESH_GET_VAR) !== null) {
         // AJAX request for regenerating code
         $code = $this->getVerifyCode(true);
         Leaps::$app->response->format = Response::FORMAT_JSON;
         return ['hash1' => $this->generateValidationHash($code), 'hash2' => $this->generateValidationHash(strtolower($code)), 'url' => Url::to([$this->id, 'v' => uniqid()])];
     } else {
         $this->setHttpHeaders();
         Leaps::$app->response->format = Response::FORMAT_RAW;
         return $this->renderImage($this->getVerifyCode());
     }
 }