/** * Serve a Post File * @return Response */ public function file() { $table = zbase_route_input('table', null); $action = zbase_route_input('action', null); $id = zbase_route_input('id', null); $file = zbase_route_input('file', null); if (!empty($table) && !empty($action) && !empty($id) && !empty($file)) { $entityObject = zbase_entity($table); if ($entityObject instanceof \Zbase\Post\PostInterface) { $entity = $entityObject->postById($id); if (!empty($entity)) { if ($entity->postUserHasAccess()) { if ($action == 'delete' && zbase_is_post()) { $file = $entity->postFileByFilename($file); if (!empty($file) && $entity->postFileCanBeDeleted($file)) { return $entity->postFileDelete($file); } } if ($action == 'view') { $width = null; $height = null; if (preg_match('/_/', $file) > 0) { $filex = explode('_', $file); if (count($filex) == 1 && !empty($filex[0])) { $filename = $filex[0]; } if (count($filex) == 2 && !empty($filex[1])) { $sizeX = explode('x', $filex[0]); $width = $sizeX[0]; $height = $sizeX[1]; $filename = $filex[1]; } } else { $filename = $file; } if (!empty($filename)) { $file = $entity->postFileByFilename($filename); if (!empty($file)) { return $entity->postFileServe($file, $width, $height); } } } } } } } return $this->notfound(); }
/** * Return search queries * @return string|array */ public function getSearchKeyword() { if (zbase_is_post()) { return zbase_request_input($this->getWidgetPrefix('search_query')); } }
/** * true if HTTP Accept header is application/json * * @return boolean */ function zbase_is_json() { if (zbase_request_query_input('jsonp', false)) { return true; } if (zbase_request_query_input('json', false)) { return true; } if (zbase_is_post() && zbase_request_input('json', false)) { return true; } return \Request::wantsJson(); }
/** * Return the entity * @return Zbase\Widget\EntityInterface */ public function entity() { if (empty($this->hasEntity())) { return false; } if (is_null($this->_entity)) { $entityName = $this->_v('entity.name', null); if (!empty($this->_nodeSupport)) { $entityName = $this->getNodeNamespace() . '_' . strtolower($this->_nodeName); } if (!is_null($entityName)) { $entity = $this->_v('entity.entity', null); if ($entity instanceof \Zbase\Entity\Laravel\Entity) { $this->_entityObject = zbase()->entity($entityName, [], true); $this->_entity = $entity; return $this->_entity; } if ($entity instanceof \Zbase\Post\PostInterface) { $this->_entityObject = $entity; $this->_entity = $entity; return $this->_entity; } $this->_entity = $this->_entityObject = $entity = zbase()->entity($entityName, [], true); $repoById = $this->_v('entity.repo.byId', null); $repoByFilter = $this->_v('entity.repo.byFilter', null); if (is_null($repoById)) { $repoById = $this->_v('entity.repo.byAlphaId', null); if (!empty($repoById)) { $byAlpha = true; } else { $repoById = $this->_v('entity.repo.bySlug', null); if (!empty($repoById)) { $bySlug = true; } } } if (is_array($repoById)) { if (!empty($repoById['route'])) { $id = zbase_route_input($repoById['route']); } if ($this->isNodeCategoryBrowsing()) { $repoItemBySlug = $this->_v('entity.repo.item.bySlug', null); $repoItemByAlpha = $this->_v('entity.repo.item.byAlpha', null); $repoItemById = $this->_v('entity.repo.item.byId', null); /** * Browse by category * /CategorySlug/ - should show all category items * /CategorySlug/ItemName - show item * * Module should have a "default" entry as the wildcard catchAll action */ if (!empty($repoItemByAlpha)) { $itemRouteParameterName = $this->_v('entity.repo.item.byAlpha.route', null); $childAlphaId = zbase_route_input($itemRouteParameterName); if (!empty($childAlphaId)) { $this->_childEntity = zbase()->entity($this->nodePrefix(), [], true)->repository()->byAlphaId($childAlphaId); if (!$this->_childEntity instanceof \Zbase\Entity\Laravel\Node\Node) { $this->setViewFile(zbase_view_file_contents('errors.404')); return zbase_abort(404); } } } if (!empty($repoItemBySlug)) { $itemRouteParameterName = $this->_v('entity.repo.item.bySlug.route', null); $childAlphaId = zbase_route_input($itemRouteParameterName); if (!empty($childAlphaId)) { $this->_childEntity = zbase()->entity($this->nodePrefix(), [], true)->repository()->bySlug($childAlphaId); if (!$this->_childEntity instanceof \Zbase\Entity\Laravel\Node\Node) { $this->setViewFile(zbase_view_file_contents('errors.404')); return zbase_abort(404); } } } if (!empty($repoItemById)) { $itemRouteParameterName = $this->_v('entity.repo.item.byId.route', null); $childAlphaId = zbase_route_input($itemRouteParameterName); if (!empty($childAlphaId)) { $this->_childEntity = zbase()->entity($this->nodePrefix(), [], true)->repository()->byId($childAlphaId); if (!$this->_childEntity instanceof \Zbase\Entity\Laravel\Node\Node) { $this->setViewFile(zbase_view_file_contents('errors.404')); return zbase_abort(404); } } } } if (!empty($repoById['request']) && zbase_is_post() == 'post') { $id = zbase_request_input($repoById['request']); } if (!empty($id)) { $filters = $this->_v('entity.filter.query', []); $sorting = $this->_v('entity.sorting.query', []); $selects = ['*']; $joins = []; $this->_urlHasRequest = true; if ($this->isNode()) { zbase()->json()->addVariable('id', $id); if (!empty($repoById) && !empty($id) && empty($byAlpha) && empty($bySlug)) { $filters['id'] = ['eq' => ['field' => $entity->getKeyName(), 'value' => $id]]; } if ($this->isCurrentUser()) { $filters['user'] = ['eq' => ['field' => 'user_id', 'value' => zbase_auth_user()->id()]]; } if ($this->isPublic()) { $filters['status'] = ['eq' => ['field' => 'status', 'value' => 2]]; } if (!empty($byAlpha)) { $filters['alpha'] = ['eq' => ['field' => 'alpha_id', 'value' => $id]]; } if (!empty($bySlug)) { $filters['slug'] = ['eq' => ['field' => 'slug', 'value' => $id]]; } if (method_exists($entity, 'querySelects')) { $selects = $entity->querySelects($filters, ['widget' => $this]); } if (method_exists($entity, 'queryJoins')) { $joins = $entity->queryJoins($filters, $this->getRequestSorting(), ['widget' => $this]); } if (method_exists($entity, 'querySorting')) { $sorting = $entity->querySorting($sorting, $filters, ['widget' => $this]); } if (method_exists($entity, 'queryFilters')) { $filters = $entity->queryFilters($filters, $sorting, ['widget' => $this]); } /** * Merge filters from widget configuration * entity.filter.query */ $filters = array_merge($filters, $this->_v('entity.filter.query', [])); $sorting = array_merge($sorting, $this->_v('entity.sorting.query', [])); $action = $this->getAction(); $debug = zbase_request_query_input('__widgetEntityDebug', false); if ($this->isAdmin()) { if ($action == 'restore' || $action == 'ddelete') { return $this->_entity = $entity->repository()->onlyTrashed()->all($selects, $filters, $sorting, $joins)->first(); } } else { if ($entity->hasSoftDelete() && $this->isCurrentUser()) { if ($action == 'restore' || $action == 'ddelete') { return $this->_entity = $entity->repository()->onlyTrashed()->all($selects, $filters, $sorting, $joins)->first(); } return $this->_entity = $entity->repository()->setDebug($debug)->withTrashed()->all($selects, $filters, $sorting, $joins)->first(); } } return $this->_entity = $entity->repository()->setDebug($debug)->all($selects, $filters, $sorting, $joins)->first(); } } } else { if (!empty($repoByFilter)) { $filters = []; $sorting = []; $selects = ['*']; $joins = []; $singleRow = $this->_v('entity.singlerow', true); if ($this->isCurrentUser()) { $filters['user'] = ['eq' => ['field' => 'user_id', 'value' => zbase_auth_user()->id()]]; } if ($this->isPublic()) { $filters['status'] = ['eq' => ['field' => 'status', 'value' => 2]]; } $filters = array_merge($filters, $this->_v('entity.filter.query', [])); $sorting = array_merge($sorting, $this->_v('entity.sorting.query', [])); if (!empty($singleRow)) { return $this->_entity = $entity->repository()->all($selects, $filters, $sorting, $joins)->first(); } else { return $this->_entity = $entity->repository()->all($selects, $filters, $sorting, $joins); } } } $repoMethod = $this->_v('entity.method', null); if (!is_null($repoMethod)) { return $this->_entity = $this->_entityObject->{$repoMethod}(); } $this->_entityIsDefault = true; return $this->_entity = $this->_entityObject; } } return $this->_entity; }
/** * Check if POSTing * * @return boolean */ public function isPost() { return zbase_is_post(); }