Example #1
0
 /**
  * Controller Action
  * 	This will be called validating the form
  * @param string $action
  */
 public function controller($action)
 {
     $this->setAction($action);
     $repoMethod = $this->_v('repo.method', 'count');
     if ($repoMethod == 'update') {
         $ret = $this->entity()->widgetController(zbase_request_method(), $this->getAction(), [], $this);
         $actionMessages = $this->entity()->getActionMessages($this->getAction());
         if (!empty($actionMessages)) {
             foreach ($actionMessages as $alertType => $alertMessages) {
                 if (is_array($alertMessages)) {
                     foreach ($alertMessages as $alertMessage) {
                         zbase_alert($alertType, $alertMessage);
                     }
                 }
             }
         }
         if (!empty($ret)) {
             $url = zbase_url_previous();
             return zbase_redirect()->to($url);
         }
     }
     if (!$this->checkUrlRequest()) {
         return zbase_abort(404);
     }
     $this->_rows();
 }
Example #2
0
 public function controllerIndex()
 {
     if (!$this->getModule()->hasAccess()) {
         if (zbase_auth_has()) {
             return $this->unathorized(_zt('You don\'t have enough access to the resource.'));
         } else {
             return redirect()->to(zbase_url_from_route('login'));
         }
     }
     /**
      * Check for widgets
      */
     $widgetsAction = $action = str_replace('.', '-', $this->getRouteParameter('action', 'index'));
     $requestMethod = zbase_request_method();
     if (!empty($this->nodeName)) {
         $widgetsAction = $requestMethod . '-node-' . $this->nodeName . '-' . $action;
         $htmls = [];
     }
     $isAjax = zbase_request_is_ajax();
     if ($isAjax) {
         $widgetsAction = (!empty($this->nodeName) ? $requestMethod . '-node-' . $this->nodeName . '-' : '') . 'json-' . $action;
         $htmls = [];
     }
     if ($this->getModule()->hasAction($requestMethod . '-' . $action)) {
         $widgetsAction = $requestMethod . '-' . $action;
         $action = $widgetsAction;
         $htmls = [];
     }
     $widgets = $this->getModule()->pageProperties($action)->widgetsByControllerAction($widgetsAction);
     if (count($widgets) == 1) {
         $firstWidget = collect($widgets)->first();
         if ($firstWidget instanceof \Zbase\Widgets\WidgetInterface) {
             $firstWidget->pageProperties($widgetsAction);
         }
     }
     if (!is_array($widgets) && $widgets instanceof \Illuminate\Http\RedirectResponse) {
         return $widgets;
     }
     zbase()->json()->addVariable('_widget', $this->getModule()->id() . '_' . str_replace('-', '', $action));
     if (zbase_is_dev()) {
         zbase()->json()->addVariable(__METHOD__, $widgetsAction);
         if (zbase_request_is_post()) {
             zbase()->json()->addVariable('_POST_PARAMETERS', zbase_request_inputs());
         }
         zbase()->json()->addVariable('_ROUTE_PARAMETERS', zbase_route_inputs());
         zbase()->json()->addVariable('_GET_PARAMETERS', zbase_request_query_inputs());
     }
     // dd($this->getModule(), $widgetsAction, $widgets);
     if (empty($widgets)) {
         return zbase_abort(404);
     }
     foreach ($widgets as $widget) {
         if (!empty($this->nodeName)) {
             zbase()->json()->addVariable('node', ['prefix' => $this->getModule()->nodeNamespace(), 'name' => $this->nodeName, 'support' => 1]);
             $widget->setNodename($this->nodeName)->setNodeSupport(true);
         }
         if ($widget instanceof \Zbase\Widgets\ControllerInterface) {
             $v = $widget->validateWidget($action);
             if ($v instanceof \Illuminate\Contracts\Validation\Validator) {
                 if ($isAjax) {
                     zbase()->json()->addVariable('errors', $v->errors()->getMessages());
                     return new \Illuminate\Http\JsonResponse($v->errors()->getMessages(), 422);
                 } else {
                     return redirect()->to($this->getRedirectUrl())->withInput(zbase_request_inputs())->withErrors($v->errors()->getMessages());
                 }
             }
             $ret = $widget->controller($this->getRouteParameter('action', 'index'));
             if ($ret instanceof \Zbase\Exceptions\NotFoundHttpException) {
                 return $this->notFound();
             }
             if ($ret instanceof \Zbase\Exceptions\UnauthorizedException) {
                 return $this->unathorized();
             }
             if ($ret instanceof \Zbase\Exceptions\Exception) {
                 return $this->error();
             }
             if ($ret instanceof \Illuminate\Http\RedirectResponse) {
                 if ($isAjax) {
                     zbase()->json()->addVariable('redirect', $ret->getTargetUrl());
                 } else {
                     return $ret;
                 }
             }
             if (zbase_is_json()) {
                 zbase_response_format_set('json');
                 $jsonIndexName = $widget->getWidgetPrefix();
                 if (zbase_is_angular()) {
                     if ($widget instanceof \Zbase\Widgets\Type\Datatable) {
                         $angularTemplate = zbase_angular_widget_datatable($this->getModule(), $widget);
                         $jsonIndexName = $angularTemplate['serviceName'];
                     }
                 }
                 if (zbase_is_dev()) {
                     zbase()->json()->addVariable('$jsonIndexName', $jsonIndexName);
                 }
                 if (!$widget->isExporting()) {
                     zbase()->json()->addVariable($jsonIndexName, $widget->toArray());
                 }
             } else {
                 if ($isAjax) {
                     $htmls[str_replace('-', '_', $widget->id())] = $widget->render();
                 }
             }
             $widget->pageProperties($widgetsAction);
         }
     }
     if (!empty($isAjax)) {
         zbase()->json()->addVariable('_widgets', 1);
         zbase()->json()->addVariable('html', $htmls);
     } else {
         return $this->view(zbase_view_file('module.index'), array('module' => $this->getModule(), 'widgets' => $widgets));
     }
 }
Example #3
0
 /**
  * Return the Wrapper Attributes
  * @return array
  */
 public function wrapperAttributes()
 {
     $attr = parent::wrapperAttributes();
     if (!empty($this->_entity)) {
         if ($this->_action == 'delete' && strtolower(zbase_request_method()) != 'post' || $this->isNode() && $this->_entity->hasSoftDelete() && empty($this->_entityIsDefault) && $this->_entity->trashed()) {
             $attr['class'][] = 'action-delete';
             $attr['style'][] = 'border:2px solid red; padding:20px;';
         }
     }
     return $attr;
 }
Example #4
0
 public function checkAllowedMethod()
 {
     $requestMethod = zbase_request_method();
     $allowedMethods = !empty($this->apiConfiguration['requestMethod']) ? $this->apiConfiguration['requestMethod'] : ['get'];
     if (!empty($allowedMethods)) {
         if (is_array($allowedMethods) && !in_array($requestMethod, $allowedMethods)) {
             return false;
         }
         if (!is_array($allowedMethods) && $allowedMethods != $requestMethod) {
             return false;
         }
     }
     return true;
 }
Example #5
0
/**
 * Is Post?
 * @return boolean
 */
function zbase_request_is_post()
{
    return strtolower(zbase_request_method()) == 'post';
}
Example #6
0
/**
 * Create a route
 * @param string $name The Route Name
 * @param array $route The Route configuration
 * @return Response
 */
function zbase_route_response($name, $route)
{
    if (!empty(zbase_is_maintenance())) {
        return zbase_response(view(zbase_view_file('maintenance')));
    }
    $redirect = zbase_value_get($route, 'redirect', false);
    if (!empty($redirect)) {
        return redirect()->to($redirect);
    }
    $response = zbase_value_get($route, 'response', false);
    if (!empty($response)) {
        return $response;
    }
    /**
     * If we are using username in routes,
     * 	we have to check if the username exists in DB.
     * 	This is checked in zbase_route_username_get()
     * 	if the zbase_route_username_get() returns false, means
     * 	that the route is not a username or username didn't exists.
     * 	Here we check against all other Routes  if the prefix is in our
     * 	list of routes, if not found, throw NotFoundHttpException
     */
    $useUsernameRoute = zbase_route_username();
    $usernameRoute = zbase_route_username_get();
    $usernameRouteCheck = zbase_data_get($route, 'usernameRouteCheck', true);
    if (empty($usernameRouteCheck)) {
        /**
         * Will not check for username route
         */
        $useUsernameRoute = false;
    }
    //if($usernameRoute === false && !empty($useUsernameRoute))
    if ($name == 'index' && zbase_auth_has() && !empty($useUsernameRoute)) {
        return redirect()->to('/' . zbase_auth_real()->username);
    }
    if ($usernameRoute === false && !empty($useUsernameRoute)) {
        $uri = zbase_url_uri();
        $adminKey = zbase_admin_key();
        if (!empty($uri)) {
            $uriEx = explode('/', $uri);
            if (!empty($uriEx)) {
                foreach ($uriEx as $uriV) {
                    if (!empty($uriV)) {
                        /**
                         * If it isn't an admin key, check it against given Routes
                         */
                        if ($uriV !== $adminKey) {
                            $routes = zbase_config_get('routes', []);
                            if (!empty($routes)) {
                                foreach ($routes as $rName => $r) {
                                    if (!empty($r['enable']) && !empty($r['url'])) {
                                        $urlEx = explode('/', $r['url']);
                                        if (!empty($urlEx)) {
                                            foreach ($urlEx as $urlExV) {
                                                if (!empty($urlExV)) {
                                                    if ($uriV == $urlExV) {
                                                        /**
                                                         * Found it, valid URL
                                                         */
                                                        $validUrlPrefix = true;
                                                    }
                                                    /**
                                                     * Will deal only with the first not empty value so break it.
                                                     */
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                    if (!empty($validUrlPrefix)) {
                                        /**
                                         * Found it, break it
                                         */
                                        $name = $rName;
                                        $route = $r;
                                        break;
                                    }
                                }
                            }
                        } else {
                            return redirect(zbase_url_from_route('home'));
                        }
                        /**
                         * Will deal only with the first not empty value so break it.
                         */
                        break;
                    }
                }
                if (empty($validUrlPrefix)) {
                    /**
                     * Only if routeName is not the index
                     */
                    if ($name != 'index') {
                        // $response = new \Zbase\Exceptions\NotFoundHttpException();
                        // return $response->render(zbase_request(), $response);
                    }
                }
            }
        }
    }
    $usernameRoutePrefix = zbase_route_username_prefix();
    $originalRouteName = str_replace($usernameRoutePrefix, '', $name);
    zbase()->setCurrentRouteName($name);
    $guest = true;
    $authed = false;
    $guestOnly = false;
    $middleware = !empty($route['middleware']) ? $route['middleware'] : false;
    $backend = !empty($route['backend']) ? $route['backend'] : false;
    if ($name == 'password-reset' && zbase_auth_has()) {
        \Auth::guard()->logout();
        return redirect(zbase_url_from_current());
    }
    if (!empty($backend)) {
        //		zbase_in_back();
    }
    if (preg_match('/\\?usernameroute/', zbase_url_uri()) > 0 && !empty($useUsernameRoute) && zbase_auth_has()) {
        return redirect()->to('/' . zbase_auth_user()->username() . '/home');
    }
    if (!empty($useUsernameRoute) && zbase_auth_has() && $usernameRoute != zbase_auth_user()->username()) {
        return redirect(zbase_url_from_route($originalRouteName, [$usernameRoutePrefix => zbase_auth_user()->username()]));
    }
    if (!empty($middleware)) {
        if (is_array($middleware)) {
            $access = isset($middleware['access']) ? $middleware['access'] : false;
            if (!empty($access) && is_array($access)) {
                if (!zbase_auth_has()) {
                    zbase_session_set('__loginRedirect', zbase_url_from_current());
                    return redirect(zbase_url_from_route('login'));
                }
                if (zbase_auth_has() && !zbase_auth_is($access)) {
                    return zbase_abort(401, ucfirst($access) . ' is needed to access the page.');
                }
            } else {
                $guest = isset($middleware['guest']) ? $middleware['guest'] : false;
                $authed = isset($middleware['auth']) ? $middleware['auth'] : false;
                $adminAuthed = isset($middleware['admin']) ? $middleware['admin'] : false;
                if ($adminAuthed) {
                    $authed = true;
                }
                $guestOnly = isset($middleware['guestOnly']) ? $middleware['guestOnly'] : false;
            }
        }
    }
    if (empty($access)) {
        if (!empty($backend)) {
            if (!empty($usernameRoute)) {
                /**
                 * If user is loggedIn and this is admin side and this is not logIn page,
                 * redirect to users dashboard.
                 * User can only access his own dashboard via /{usernameroute?}/admin
                 */
                if (zbase_auth_has() && zbase_auth_is(zbase_route_username_minimum_access()) && zbase_is_back() && $usernameRoute != zbase_auth_user()->username()) {
                    return redirect(zbase_url_from_route('admin', [$usernameRoutePrefix => zbase_auth_user()->username]));
                }
                if ((empty(zbase_auth_has()) || !zbase_auth_is('user')) && $name != $usernameRoutePrefix . 'admin.login') {
                    zbase_session_set('__loginRedirect', zbase_url_from_current());
                    return redirect(zbase_url_from_route('admin.login'));
                }
            } else {
                if ((empty(zbase_auth_has()) || !zbase_auth_is('admin')) && $name != 'admin.login') {
                    zbase_session_set('__loginRedirect', zbase_url_from_current());
                    return redirect(zbase_url_from_route('admin.login'));
                }
            }
        } else {
            if (!empty($guestOnly) && zbase_auth_has()) {
                return redirect(zbase_url_from_route('home'));
            }
            if (!empty($usernameRoute)) {
                if (!empty($authed) && !zbase_auth_has() && $name != $usernameRoutePrefix . 'login') {
                    zbase_session_set('__loginRedirect', zbase_url_from_current());
                    return redirect(zbase_url_from_route('login'));
                }
            } else {
                if (!empty($authed) && !zbase_auth_has() && $name != 'login') {
                    zbase_session_set('__loginRedirect', zbase_url_from_current());
                    return redirect(zbase_url_from_route('login'));
                }
            }
        }
    }
    $params = zbase_route_inputs();
    $requestMethod = zbase_request_method();
    $controller = !empty($route['controller']) ? $route['controller'] : null;
    $command = !empty($route['command']) ? $route['command'] : false;
    if (!empty($command) && $command instanceof \Closure) {
        $command();
        exit;
    }
    if (!empty($controller) && !empty($controller['name']) && !empty($route['controller']['enable'])) {
        $controllerName = !empty($route['controller']['name']) ? $route['controller']['name'] : null;
        $controllerMethod = !empty($route['controller']['method'][$requestMethod]) ? $route['controller']['method'][$requestMethod] : (!empty($route['controller']['method']) ? $route['controller']['method'] : 'index');
        if (!empty($controllerName)) {
            $controllerConfig = zbase_config_get('controller.class.' . $controllerName, null);
            if (!empty($controllerConfig) && !empty($controllerConfig['enable'])) {
                $controllerClass = zbase_controller_create_name(zbase_config_get('controller.class.' . $controllerName . '.name', Zbase\Http\Controllers\__FRAMEWORK__\PageController::class));
                $controllerObject = zbase_object_factory($controllerClass, !empty($route['controller']['params']) ? $route['controller']['params'] : []);
                zbase()->setController($controllerObject->setName($controllerName)->setActionName($controllerMethod)->setRouteParameters($params));
                zbase_view_page_details($route);
                return zbase_response($controllerObject->{$controllerMethod}());
            }
        }
    }
    $view = !empty($route['view']) ? $route['view'] : null;
    if (!empty($view) && !empty($view['name']) && !empty($route['view']['enable'])) {
        zbase_view_page_details($route);
        if (!empty($route['view']['content'])) {
            $params['content'] = zbase_data_get($route['view']['content'], null);
        }
        if ($view['name'] == 'type.js') {
            zbase_response_format_set('javascript');
        }
        return zbase_response(zbase_view_render(zbase_view_file($view['name']), $params));
    }
}