/**
  * @param Factory $validator
  * @param Dispatcher $dispatcher
  * @param ContentType $contentType
  * @param ContentTypeFormGroup $contentTypeFormGroup
  * @return CommandResult
  */
 public function handle(Factory $validator, Dispatcher $dispatcher, ContentType $contentType, ContentTypeFormGroup $contentTypeFormGroup)
 {
     // check if user has permission
     if (!$this->disablePermissionChecking) {
         if (!$this->user->hasAnyPermission(['contentBuilder.manage'])) {
             return new CommandResult(false, "Not enough permission.", null, 403);
         }
     }
     // validate data
     $validationResult = $validator->make(array('name' => $this->name, 'form_name' => $this->formName, 'fields' => $this->fields, 'content_type_id' => $this->contentTypeId), ContentTypeFormGroup::$rules);
     if ($validationResult->fails()) {
         return new CommandResult(false, $validationResult->getMessageBag()->first(), null, 400);
     }
     // fire event creating
     $dispatcher->fire('formGroup.creating', array($this->args));
     // begin create
     if (!($cType = $contentType->find($this->contentTypeId))) {
         return new CommandResult(false, "Content Type Not Found.", null, 400);
     }
     $createdFormGroup = $cType->formGroups()->create(array('name' => $this->name, 'form_name' => $this->formName, 'conditions' => $this->conditions, 'fields' => $this->fields, 'content_type_id' => $this->contentTypeId));
     // fire event creating
     $dispatcher->fire('formGroup.created', array($createdFormGroup));
     // all good
     return new CommandResult(true, "Form group successfully created.", $createdFormGroup, 201);
 }
 /**
  * @param ContentType $contentType
  * @param ContentTypeFormGroup $contentTypeFormGroup
  * @param Dispatcher $dispatcher
  * @return CommandResult
  */
 protected function queryByContentType($contentType, $contentTypeFormGroup, $dispatcher)
 {
     if (!($cType = $contentType->find($this->contentTypeId))) {
         return new CommandResult(false, 'Content Type not found.', null, 400);
     }
     // build needed permissions
     // if the user has no permissions to manage or create under this Content Type,
     // the query should be forbidden
     $cTypeManage = $cType->type . '.manage';
     if (!$this->disablePermissionChecking) {
         if (!$this->user->hasAnyPermission([$cTypeManage, 'contentBuilder.manage'])) {
             return new CommandResult(false, CommandResult::$responseForbiddenMessage, null, 403);
         }
     }
     // fire before query
     $dispatcher->fire('formGroup.beforeQuery', array($this->args));
     if ($this->paginated) {
         $res = $contentTypeFormGroup->with(array('contentType'))->where('content_type_id', $this->contentTypeId)->paginate($this->perPage);
     } else {
         $res = $contentTypeFormGroup->with(array('contentType'))->where('content_type_id', $this->contentTypeId)->get();
     }
     // fire after query
     $dispatcher->fire('formGroup.afterQuery', array($res));
     // all good
     return new CommandResult(true, "Query form groups command successful.", $res, 200);
 }
 public function testShouldDeleteContentType()
 {
     // create user and logged in (the user who will perform the action)
     $user = User::create(array('first_name' => 'dianne', 'email' => '*****@*****.**', 'password' => 'pass$dianne', 'permissions' => array('superuser' => 1)));
     // logged in the user
     $this->application['auth']->loginUsingId($user->id);
     // create dummy content type
     $blog = ContentType::create(array('type' => 'Blog', 'enable_revisions' => true));
     // begin
     $result = $this->commandDispatcher->dispatchFromArray('Darryldecode\\Backend\\Components\\ContentBuilder\\Commands\\DeleteContentTypeCommand', array('contentTypeId' => $blog->id));
     $this->assertTrue($result->isSuccessful());
     $this->assertEquals('Content type successfully deleted.', $result->getMessage());
     $this->assertEquals(200, $result->getStatusCode(), 'Status code should be ok');
     // prove content type has been deleted
     $this->assertInternalType('null', ContentType::find($blog->id));
 }
 /**
  * @param Dispatcher $dispatcher
  * @param ContentType $contentType
  * @return CommandResult
  */
 public function handle(Dispatcher $dispatcher, ContentType $contentType)
 {
     // check if user has permission
     if (!$this->disablePermissionChecking) {
         if (!$this->user->hasAnyPermission(['contentBuilder.delete'])) {
             return new CommandResult(false, "Not enough permission.", null, 403);
         }
     }
     if (!($cType = $contentType->find($this->contentTypeId))) {
         return new CommandResult(false, "Content type not found.", null, 400);
     }
     // prevent deletion if content Type as contents
     if ($this->contentTypeHasContents($cType)) {
         return new CommandResult(false, "Content type has contents. Delete Contents first.", null, 400);
     }
     // fire before delete
     $dispatcher->fire('contentType.deleting', array($this->args));
     $cType->delete();
     // fire before delete
     $dispatcher->fire('contentType.deleted', array($cType));
     // all good
     return new CommandResult(true, "Content type successfully deleted.", null, 200);
 }
 public function testCreateContentTypeWithDisabledRevisions()
 {
     // this is a super user
     $user = User::create(array('first_name' => 'darryl', 'email' => '*****@*****.**', 'password' => 'pass$darryl', 'permissions' => array('superuser' => 1)));
     $this->application['auth']->loginUsingId($user->id);
     $result = $this->commandDispatcher->dispatchFromArray('Darryldecode\\Backend\\Components\\ContentBuilder\\Commands\\CreateContentTypeCommand', array('type' => 'Products', 'enableRevision' => 'no'));
     $this->assertTrue($result->isSuccessful());
     $this->assertEquals(201, $result->getStatusCode());
     $this->assertEquals('Content type successfully created.', $result->getMessage());
     // prove
     $cType = ContentType::find($result->getData()->id);
     $this->assertFalse((bool) $cType->enable_revisions);
 }
 /**
  * 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;
 }