/** * Execute the command. * * @param ContentType $contentType * @param ContentTypeTaxonomy $contentTypeTaxonomy * @param Validator $validator * @param Dispatcher $dispatcher * @return CommandResult */ public function handle(ContentType $contentType, ContentTypeTaxonomy $contentTypeTaxonomy, Validator $validator, Dispatcher $dispatcher) { // in order to determine what permissions are needed to create // a taxonomy terms, we will get first what taxonomy the term is for // after we can get the taxonomy, we will then get what type the taxonomy // belong so we can verify if the user has permission for that type try { $taxonomy = $contentTypeTaxonomy->findOrFail($this->contentTypeTaxonomyId); } catch (\Exception $e) { return new CommandResult(false, "Invalid Taxonomy.", null, 400); } try { $type = $contentType->findOrFail($taxonomy->content_type_id); } catch (\Exception $e) { return new CommandResult(false, "Invalid Content Type.", null, 400); } // build the permissions needed $canManageOnThisType = $type->type . '.manage'; // check if user has permission if (!$this->disablePermissionChecking) { if (!$this->user->hasAnyPermission([$canManageOnThisType])) { return new CommandResult(false, "Not enough permission.", null, 403); } } // validate data $validationResult = $validator->make(array('term' => $this->term, 'slug' => $this->slug, 'content_type_taxonomy_id' => $this->contentTypeTaxonomyId), ContentTypeTaxonomyTerm::$rules); if ($validationResult->fails()) { return new CommandResult(false, $validationResult->getMessageBag()->first(), null, 400); } // prepare term to be created $termToBeCreated = array('term' => $this->term, 'slug' => $this->slug, 'content_type_taxonomy_id' => $this->contentTypeTaxonomyId); // fire creating event $dispatcher->fire('contentTypeTaxonomyTerm.creating', array($termToBeCreated)); // store $createdTerm = $taxonomy->terms()->create($termToBeCreated); // fire creating event $dispatcher->fire('contentTypeTaxonomyTerm.created', array($createdTerm)); // all good return new CommandResult(true, "Taxonomy term successfully created.", $createdTerm, 201); }
/** * @param ContentType $contentType * @param ContentTypeTaxonomy $contentTypeTaxonomy * @param ContentTypeTaxonomyTerm $contentTypeTaxonomyTerm * @param Dispatcher $dispatcher * @return CommandResult */ public function handle(ContentType $contentType, ContentTypeTaxonomy $contentTypeTaxonomy, ContentTypeTaxonomyTerm $contentTypeTaxonomyTerm, Dispatcher $dispatcher) { // in order to determine what permissions are needed to create // a taxonomy terms, we will get first what taxonomy the term is for // after we can get the taxonomy, we will then get what type the taxonomy // belong so we can verify if the user has permission for that type try { $taxonomy = $contentTypeTaxonomy->findOrFail($this->taxonomyId); } catch (\Exception $e) { return new CommandResult(false, "Invalid Taxonomy.", null, 400); } try { $type = $contentType->findOrFail($taxonomy->content_type_id); } catch (\Exception $e) { return new CommandResult(false, "Invalid Content Type.", null, 400); } try { $term = $contentTypeTaxonomyTerm->findOrFail($this->termId); } catch (\Exception $e) { return new CommandResult(false, "Invalid Taxonomy Term.", null, 400); } // build the permissions needed $canManageOnThisType = $type->type . '.manage'; // check if user has permission if (!$this->disablePermissionChecking) { if (!$this->user->hasAnyPermission([$canManageOnThisType])) { return new CommandResult(false, "Not enough permission.", null, 403); } } // fire creating event $dispatcher->fire('taxonomyTerm.deleting', array($this->args)); // detach all posts type related to this term first $term->contents()->detach(); $term->delete(); // fire creating event $dispatcher->fire('taxonomyTerm.deleted', array($taxonomy)); // all good return new CommandResult(true, "Taxonomy Term successfully deleted.", null, 200); }
/** * @param Content $content * @param ContentType $contentType * @param Dispatcher $dispatcher * @return CommandResult */ public function handle(Content $content, ContentType $contentType, Dispatcher $dispatcher) { // get content available permissions try { $cType = $contentType->findOrFail($this->contentTypeId); $cTypeManage = $cType->type . '.manage'; } catch (\Exception $e) { return new CommandResult(false, "Invalid Content Type.", null, 400); } // check if user has permission if (!$this->disablePermissionChecking) { if (!$this->user->hasAnyPermission([$cTypeManage])) { return new CommandResult(false, "Not enough permission.", null, 403); } } if (!($c = $content->find($this->id))) { return new CommandResult(false, "Content not found.", null, 404); } // fire event updating $dispatcher->fire($cType->type . '.updating', array($c, $this->args)); // hold the current content so we can use it later if revisions is enabled $oldBody = $c->body; $c->title = $this->title ? $this->title : $c->title; $c->body = $this->body ? $this->body : $c->body; $c->slug = $this->slug ? $this->slug : $c->slug; $c->status = $this->status ? $this->status : $c->status; $c->permission_requirements = $this->permissionRequirements ? $this->permissionRequirements : $c->permission_requirements; $c->misc_data = $this->miscData ? $this->miscData : $c->misc_data; // taxonomy if ($this->taxonomies) { // detach all taxonomies first $c->terms()->detach(); foreach ($this->taxonomies as $termId => $value) { if ($value == true) { $c->terms()->attach(array('content_type_taxonomy_term_id' => $termId)); } } } // meta data if ($this->metaData) { // clear all meta data first $c->metaData()->delete(); foreach ($this->metaData as $formGroup => $formGroupData) { foreach ($formGroupData as $metaKey => $metaValue) { $c->metaData()->create(array('key' => $metaKey, 'value' => $metaValue, 'form_group_name' => $formGroup)); } } } // save $c->save(); // check if revisions is enabled so we can deal with it if ($cType->enable_revisions == ContentType::REVISIONS_ENABLED) { if ($oldBody != $c->body) { $c->revisions()->create(array('old_content' => $oldBody, 'new_content' => $c->body, 'author_id' => $this->user->id)); } } // fire event updated $dispatcher->fire($cType->type . '.updated', array($c)); // return response return new CommandResult(true, "Content successfully updated.", $c, 200); }
/** * Execute the command. * * @param Content $content * @param Factory $validator * @param ContentType $contentType * @param Dispatcher $dispatcher * @param Repository $config * @return CommandResult */ public function handle(Content $content, Factory $validator, ContentType $contentType, Dispatcher $dispatcher, Repository $config) { $content = $this->createContentModel($content, $config); // get content available permissions try { $cType = $contentType->findOrFail($this->contentTypeId); $cTypeManage = $cType->type . '.manage'; } catch (\Exception $e) { return new CommandResult(false, "Invalid Content Type.", null, 400); } // check if user has permission if (!$this->disablePermissionChecking) { if (!$this->user->hasAnyPermission([$cTypeManage])) { return new CommandResult(false, "Not enough permission.", null, 403); } } // validate data $validationResult = $validator->make(array('title' => $this->title, 'body' => $this->body, 'slug' => $this->slug, 'author_id' => $this->authorId, 'content_type_id' => $this->contentTypeId), $content::$rules); if ($validationResult->fails()) { return new CommandResult(false, $validationResult->getMessageBag()->first(), null, 400); } // prepare data to be store $contentToBeCreated = array('title' => $this->title, 'body' => $this->body, 'slug' => $this->slug, 'status' => Helpers::issetAndHasValueOrAssignDefault($this->status, Content::CONTENT_PUBLISHED), 'author_id' => $this->authorId, 'content_type_id' => $this->contentTypeId, 'meta' => $this->metaData, 'misc_data' => $this->miscData, 'taxonomies' => $this->taxonomies); // fire event creating $dispatcher->fire($cType->type . '.creating', array($contentToBeCreated)); $createdContent = $content->create($contentToBeCreated); // taxonomy foreach ($contentToBeCreated['taxonomies'] as $termId => $value) { if ($value == true) { $createdContent->terms()->attach(array('content_type_taxonomy_term_id' => $termId)); } } // meta foreach ($contentToBeCreated['meta'] as $formGroup => $formGroupData) { foreach ($formGroupData as $metaKey => $metaValue) { $createdContent->metaData()->create(array('key' => $metaKey, 'value' => $metaValue, 'form_group_name' => $formGroup)); } } // fire event created $dispatcher->fire($cType->type . '.created', array($createdContent)); // return response return new CommandResult(true, "Content successfully created.", $createdContent, 201); }