Exemplo n.º 1
0
 /**
  *
  * @param array $view View Properties
  */
 public function setView($view)
 {
     if (!empty($view['file'])) {
         $this->_widgetView = zbase_value_get($view, 'file');
         $this->setViewFile($this->_widgetView);
     }
     if (!empty($view['layout'])) {
         $this->_viewLayout = $view['layout'];
     }
     if (!empty($view['params'])) {
         $this->_viewParams = $view['params'];
     }
 }
Exemplo n.º 2
0
 /**
  * @return void
  * @test
  */
 public function testValueGet()
 {
     /**
      * Check can return \Closure value
      */
     $closure = function () {
         return 8 * 4;
     };
     $this->assertEquals(8 * 4, zbase_value_get($closure));
     /**
      * Check can return value of dot-notated key
      */
     $config = ['key' => ['keyTwo' => ['keyThree' => ['keyFour' => 'keyFourValue', 'keyFive' => $closure]]]];
     $this->assertEquals('keyFourValue', zbase_value_get($config, 'key.keyTwo.keyThree.keyFour'));
     $this->assertEquals(8 * 4, zbase_value_get($config, 'key.keyTwo.keyThree.keyFive'));
     $this->assertSame(['keyThree' => ['keyFour' => 'keyFourValue', 'keyFive' => $closure]], zbase_value_get($config, 'key.keyTwo'));
 }
Exemplo n.º 3
0
/**
 * Return an instance of a Model
 * @param string $modelName
 * @param string $default
 * @return object
 */
function zbase_model($modelName, $default = null)
{
    $modelName = zbase_model_name($modelName);
    if (!empty($modelName)) {
        $enable = zbase_value_get($modelName, 'enable', false);
        $modelName = zbase_value_get($modelName, 'model', $default);
        if (!empty($modelName) && !empty($enable)) {
            $model = new $modelName();
            return $model;
        }
    }
    return null;
}
Exemplo n.º 4
0
 /**
  * Check if enabled by request
  * If true, this column is only for JSON request
  *
  * @return boolean
  */
 public function json()
 {
     return zbase_value_get($this->getAttributes(), 'json', false);
 }
Exemplo n.º 5
0
 /**
  * Event after Action
  * @param string $action
  * @param string $url The Default URL to redirect
  */
 protected function _postEvent($action)
 {
     if ($this->isPublic() && $this->isNode() && $this->isCreating()) {
         return zbase_redirect()->to($this->entity()->alphaUrl());
     }
     $isAjax = zbase_request_is_ajax();
     $requestMethod = strtolower(zbase_request_method());
     if ($isAjax) {
         if ($requestMethod == 'post') {
             $e = $this->_v('event.' . zbase_section() . '.' . $action . '.post-json.post', $this->_v('event.' . $action . '.post-json'));
         } else {
             $e = $this->_v('event.' . zbase_section() . '.' . $action . '.post-json', $this->_v('event.' . zbase_section() . '.' . $action . '.post'));
         }
     } else {
         if ($requestMethod == 'post') {
             $e = $this->_v('event.' . zbase_section() . '.' . $action . '.post.post', $this->_v('event.' . $action . '.post.post', null));
         } else {
             $e = $this->_v('event.' . zbase_section() . '.' . $action . '.post', $this->_v('event.' . $action . '.post', null));
         }
     }
     if (is_null($e)) {
         if (zbase_is_back()) {
             if ($this->isCreating()) {
                 $action = 'update';
             }
             $byAlphaId = $this->_v('entity.repo.byAlphaId.route', false);
             if ($this->entityIsPostInterface($this->entity())) {
                 if (!empty($byAlphaId)) {
                     $params = ['action' => $action, 'id' => $this->entity()->postAlphaId()];
                 } else {
                     $params = ['action' => $action, 'id' => $this->entity()->postId()];
                 }
             } else {
                 if (!empty($byAlphaId)) {
                     $params = ['action' => $action, 'id' => $this->entity()->alphaId()];
                 } else {
                     $params = ['action' => $action, 'id' => $this->entity()->id()];
                 }
             }
         } else {
             $params = ['action' => $action, 'id' => $this->entity()->alphaId()];
         }
         if ($action == 'delete') {
             $params = [];
         }
         $url = $this->getModule()->url(zbase_section(), $params);
         if ($action == 'restore' || $action == 'ddelete') {
             $url = zbase_url_previous();
         }
     }
     if (!empty($e)) {
         if (!empty($e['data'])) {
             if ($isAjax) {
                 zbase()->json()->addVariables($e['data']);
             }
         }
         if (!empty($e['route'])) {
             $params = zbase_route_inputs();
             if (!empty($e['route']['params'])) {
                 $params = array_merge($params, $e['route']['params']);
             }
             if (zbase_is_back()) {
                 $byAlphaId = $this->_v('entity.repo.byAlphaId.route', false);
                 if (!empty($byAlphaId)) {
                     $params['id'] = $this->entity()->alphaId();
                 } else {
                     $params['id'] = $this->entity()->id();
                 }
             }
             if ($action == 'ddelete') {
                 if (isset($params['id']) && isset($params['action'])) {
                     unset($params['id']);
                     unset($params['action']);
                 }
             }
             $e['route']['params'] = $params;
             $url = zbase_url_from_config($e);
         }
         $toUrl = zbase_value_get($e, 'url', false);
         if (!empty($toUrl)) {
             $url = $toUrl;
         }
     }
     $enableRedirect = $this->_v('event.' . zbase_section() . '.' . $action . '.post.redirect.enable', $this->_v('event.' . $action . '.post.redirect.enable', true));
     if (!empty($url) && !empty($enableRedirect)) {
         return zbase_redirect()->to($url);
     }
     return true;
 }
Exemplo n.º 6
0
    foreach ($breadcrumbs as $breadcrumb) {
        ?>
			<?php 
        $counter++;
        ?>
			<?php 
        $config = zbase_config_get('nav.front.main.' . $breadcrumb, []);
        ?>
			<?php 
        if (!empty($config)) {
            ?>
				<?php 
            $route = !empty($config['url']) ? $config['url'] : (!empty($config['link']) ? $config['link'] : null);
            $url = zbase_url_from_config($route);
            $label = zbase_value_get($config, 'label', null);
            $title = zbase_value_get($config, 'title', null);
            ?>
				<li class="<?php 
            echo $counter == count($breadcrumbs) ? 'last' : null;
            ?>
">
					<a href="<?php 
            echo $url;
            ?>
" title="<?php 
            echo $title;
            ?>
"><?php 
            echo $label;
            ?>
</a>
Exemplo n.º 7
0
 /**
  * Render the TAg Attiobute
  * @param string $tag HTML Tag
  *
  * @return string
  */
 public function renderTagAttribute($tag)
 {
     $attributes = [];
     if (method_exists($this, 'getAttributes')) {
         $attributes = $this->getAttributes();
     }
     return $this->renderHtmlAttributes(zbase_value_get($attributes, 'html.attributes.' . $tag, []));
 }
Exemplo n.º 8
0
 /**
  *
  * @param string $method The property or method to be called
  * @param string $name The name of the relationship index
  * @param array $config The config of the relationship
  * @return HasOne|BelongsTo
  */
 protected function _relationship($method, $name, $config)
 {
     $rType = zbase_value_get($config, 'type', false);
     if (!empty($rType)) {
         $rMethod = zbase_value_get($config, 'class.method', zbase_string_camel_case($name));
         $isFetching = preg_match('/fetch/', $method);
         if ($isFetching) {
             $method = zbase_string_camel_case(str_replace('fetch', '', $method));
         }
         if ($rMethod == $method) {
             $rEntity = zbase_value_get($config, 'entity', false);
             $rInverse = zbase_value_get($config, 'inverse', false);
             $model = zbase_entity($rEntity);
             $lKey = zbase_value_get($config, 'keys.local', null);
             $fKey = zbase_value_get($config, 'keys.foreign', null);
             if (!empty($rEntity)) {
                 switch (strtolower($rType)) {
                     case 'onetoone':
                         if (!empty($rInverse)) {
                             $relObj = new BelongsTo($model->newQuery(), $this, $model->getTable() . '.' . $fKey, $lKey, $name);
                         } else {
                             $relObj = new HasOne($model->newQuery(), $this, $model->getTable() . '.' . $fKey, $lKey);
                         }
                         break;
                     case 'onetomany':
                         if (!empty($rInverse)) {
                             $relObj = new BelongsTo($model->newQuery(), $this, $model->getTable() . '.' . $fKey, $lKey, $name);
                         } else {
                             return new HasMany($model->newQuery(), $this, $model->getTable() . '.' . $fKey, $lKey);
                         }
                         break;
                     case 'manytomany':
                         $pivot = zbase_value_get($config, 'pivot', null);
                         return new BelongsToMany($model->newQuery(), $this, $pivot, $fKey, $lKey, $name);
                     case 'hasmanythrough':
                         break;
                     case 'morphto':
                         break;
                     case 'morphtomany':
                         break;
                     case 'belongsto':
                         return new BelongsTo($model->newQuery(), $this, $model->getTable() . '.' . $fKey, $lKey, $name);
                     default:
                 }
                 if (!empty($relObj)) {
                     if ($this->relationshipMode == 'result') {
                         return zbase_cache($this->cacheKey($relObj), function () use($relObj) {
                             return $relObj->getResults();
                         }, [$this->getTable()]);
                     }
                     return $relObj;
                 }
             }
         }
     }
     return false;
 }
Exemplo n.º 9
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));
    }
}
Exemplo n.º 10
0
 public function setInputAppend($value)
 {
     $this->_inputAppend = zbase_value_get($value);
 }