/** * @param ContentType $contentType * @param Dispatcher $dispatcher * @return CommandResult */ public function handle(ContentType $contentType, Dispatcher $dispatcher) { // begin before query $dispatcher->fire('contentType.beforeQuery', array($this->args)); // check if has permission if (!$this->disablePermissionChecking) { // if $type->type is not provided, the request referrer should be from // the admin UI Content Type Builder component. // so we will check if the user has permission (contentBuilder.manage) // on the other hand, // if $type->type is provided, we will check if user has permission to manage that type if (!is_null($this->type) && $this->type != '') { if (!$this->user->hasAnyPermission([$this->type . '.manage'])) { return new CommandResult(false, "Not enough permission.", null, 403); } } else { if (!$this->user->hasAnyPermission(['contentBuilder.manage'])) { return new CommandResult(false, "Not enough permission.", null, 403); } } } // begin query $results = $contentType->with(array('terms.taxonomy', 'taxonomies', 'taxonomies.terms', 'formGroups'))->ofType($this->type)->get(); // begin after query $dispatcher->fire('contentType.afterQuery', array($this->args)); // all good return new CommandResult(true, "Query content types successful.", $results, 200); }
/** * Query By content type * * @param ContentType $contentType * @param Content $content * @param $config * @return mixed */ protected function query($contentType, $content, $config) { // prepare content model used $content = $this->createContentModel($content, $config); $q = $content->with(array_merge(array('terms', 'author', 'metaData', 'type.formGroups', 'revisions', 'type'), $this->with)); // check if there is status provided if ($this->status && $this->status != 'any') { $q->where('status', $this->status); } // check if author ID is provided if ($this->authorId) { $q->where('author_id', $this->authorId); } // check if type is provided, we need to provide content type // we will not allow to query all of contents if (!is_null($this->type) && $this->type != '') { if (is_numeric($this->type)) { $cType = $contentType->find($this->type); } else { $cType = $contentType->with(array())->where('type', $this->type)->first(); } if ($cType) { $q->whereHas('type', function ($q) use($cType) { $q->where('type', $cType->type); }); // let's check first if the user querying has the permission to access this kind of content if (!$this->disablePermissionChecking) { $requiredPermission = $cType->type . '.manage'; if (!$this->user->hasAnyPermission([$requiredPermission])) { return new CommandResult(false, "Not enough permission.", null, 403); } } } } else { return new CommandResult(false, "Content Type should be provided.", null, 400); } // check if terms are provided so we can include it in query conditions if (!is_null($this->terms) && $this->terms != '') { $tax = $this->extractTerms($this->terms); if (count($tax) > 0) { foreach ($tax as $k => $v) { $q->whereHas('terms', function ($q) use($k, $v) { $q->whereHas('taxonomy', function ($q) use($k) { $q->where('taxonomy', $k); }); if (is_string($v)) { $q->where('slug', $v); } else { $q->whereIn('slug', $v); } }); } } } // setup date ranges if (!is_null($this->startDate) && $this->startDate != '') { $q->ofStartDate($this->startDate); } if (!is_null($this->endDate) && $this->endDate != '') { $q->ofEndDate($this->endDate); } // trigger query hook if provided if (!is_null($this->queryHook) && is_callable($this->queryHook)) { if ($res = call_user_func($this->queryHook, $q)) { $q = $res; } } // sort order $q->orderBy($this->sortBy, $this->sortOrder); // decide whether request wants paginated version or not if ($this->paginated) { $res = $q->paginate($this->perPage); } else { $res = $q->get(); } return $res; }