Пример #1
0
 public function edit(Request $req, Response $res)
 {
     $name = $req->info->local->name;
     $entity = isset($req->id) ? $this->em($req->info)->id($req->id) : $this->em($req->info)->create();
     $req->view->{$name} = $wrap = $this->em->wrap($entity);
     if (!$req->isPost()) {
         return;
     }
     $wrap->graphFromArray($req->bodyParams());
     if (!$wrap->graphIsValid()) {
         $this->notify("{$req->info->singular} <strong>{$entity->name}</strong> could not be saved, please check the messages below", 'negative');
         return;
     }
     if (!$this->em->isPersisted($entity)) {
         $this->em->persist($entity);
     }
     try {
         $this->em->flush();
     } catch (UniqueConstraintViolationException $e) {
         $this->notify("{$req->info->singular} could not be saved because <strong>{$entity->name}</strong> already exists", 'negative');
         return;
     }
     $this->notify("{$req->info->singular} <strong>{$entity->name}</strong> was saved successfully", 'positive');
     return $res->redirect($this->url(array('action' => null, 'id' => null)));
 }
Пример #2
0
 public function reorder(Request $req, Response $res)
 {
     if (!$req->isPost()) {
         throw new \Chalk\Exception("Reorder action only accepts POST requests");
     }
     if (!$req->nodeData) {
         return $res->redirect($this->url(array('action' => 'index')));
     }
     $data = json_decode($req->nodeData);
     $structure = $this->em('Chalk\\Core\\Structure')->id($req->structure);
     $nodes = $this->em('Chalk\\Core\\Structure\\Node')->all(['structure' => $structure]);
     $map = [];
     foreach ($nodes as $node) {
         $map[$node->id] = $node;
     }
     $it = new \RecursiveIteratorIterator(new Iterator($data), \RecursiveIteratorIterator::SELF_FIRST);
     $stack = [];
     foreach ($it as $i => $value) {
         array_splice($stack, $it->getDepth(), count($stack), array($value));
         $depth = $it->getDepth();
         $parent = $depth > 0 ? $stack[$depth - 1] : $structure->root;
         $node = $map[$value->id];
         $node->parent->children->removeElement($node);
         $node->parent = $map[$parent->id];
         $node->sort = $i;
     }
     $this->em->flush();
     $this->notify("Content was moved successfully", 'positive');
     if (isset($req->redirect)) {
         return $res->redirect($req->redirect);
     } else {
         return $res->redirect($this->url(array('action' => 'index')));
     }
 }
Пример #3
0
 public function merge(Request $req, Response $res)
 {
     $merge = new Merge();
     $req->view->merge = $wrap = $this->em->wrap($merge);
     if (!$req->isPost()) {
         return;
     }
     $wrap->graphFromArray($req->bodyParams());
     if (!$wrap->graphIsValid()) {
         $this->notify("{$req->info->plural} could not be merged, please check the messages below", 'negative');
         return;
     }
     $sourceTag = $this->em('core_tag')->id($merge->sourceTag->id);
     $targetTag = $this->em('core_tag')->id($merge->targetTag->id);
     $entities = $this->em('core_content')->all(['tags' => [$sourceTag]]);
     foreach ($entities as $entity) {
         $entity->tags->removeElement($sourceTag);
         if (!$entity->tags->contains($targetTag)) {
             $entity->tags->add($targetTag);
         }
     }
     $this->em->remove($sourceTag);
     $this->em->flush();
     $this->notify("{$req->info->plural} were merged successfully", 'positive');
     return $res->redirect($this->url(array('action' => null, 'id' => null)));
 }
Пример #4
0
 public function edit(Request $req, Response $res)
 {
     $name = $req->info->local->name;
     $entity = isset($req->id) ? $this->em($req->info)->id($req->id) : $this->em($req->info)->create();
     $req->view->{$name} = $wrap = $this->em->wrap($entity);
     if (!$req->isPost()) {
         return;
     }
     $wrap->graphFromArray($req->bodyParams());
     if (!$wrap->graphIsValid()) {
         return;
     }
     if (!$this->em->isPersisted($entity)) {
         $this->em->persist($entity);
     }
     $this->em->flush();
     $this->notify("{$req->info->singular} <strong>{$entity->name}</strong> was saved successfully", 'positive');
     return $res->redirect($this->url([]));
 }
Пример #5
0
 public function passwordReset(Request $req, Response $res)
 {
     $user = $this->em('core_user')->one(['token' => $req->token]);
     if (!isset($user)) {
         $this->notify('Sorry, your password reset link has expired, please request a new one', 'negative');
         return $res->redirect($this->url([], 'core_passwordRequest', true));
     }
     $req->view->passwordReset = $wrap = $this->em->wrap($passwordReset = new \Chalk\Core\Model\PasswordReset());
     if (!$req->isPost()) {
         return;
     }
     $wrap->graphFromArray($req->bodyParams());
     if (!$wrap->graphIsValid()) {
         return;
     }
     $user->passwordPlain = $passwordReset->password;
     $user->token = null;
     $user->tokenDate = null;
     $this->em->flush();
     $this->notify('Your password has been reset successfully', 'positive');
     return $res->redirect($this->url([], 'core_login', true));
 }
Пример #6
0
 public function select(Request $req, Response $res)
 {
     $filters = $this->chalk->module('core')->contentList($req->filters);
     $info = isset($req->type) ? Chalk::info($req->type) : $filters->first();
     $req->queryParam('type', $info->name);
     $class = "\\{$info->module->class}\\Model\\{$info->local->class}\\Index";
     if (!class_exists($class)) {
         $class = "\\Chalk\\Core\\Model\\Content\\Index";
     }
     $index = new $class();
     $wrap = $this->em->wrap($index);
     $wrap->graphFromArray($req->queryParams());
     $req->view->index = $wrap;
     $req->view->filters = $filters;
     if (!$req->isPost() && !$index->contentNew) {
         return;
     }
     $wrap->graphFromArray($req->bodyParams());
     $contents = [];
     foreach ($index->contents as $content) {
         $contents[] = ['id' => $content->id, 'name' => $content->name, 'card' => $this->view->render('content/card', ['content' => $content], 'core')->toString()];
     }
     $req->data->contents = $contents;
 }
Пример #7
0
 public function edit(Request $req, Response $res)
 {
     if (isset($req->node)) {
         $node = $this->em('Chalk\\Core\\Structure\\Node')->id($req->node);
     } else {
         $node = $this->em('Chalk\\Core\\Structure\\Node')->create();
         $node->parent = $this->em('Chalk\\Core\\Structure\\Node')->id($req->parent);
         $node->content = $this->em($req->type)->create();
     }
     $req->view->node = $wrap = $this->em->wrap($node);
     if (!$req->isPost()) {
         return;
     }
     $wrap->graphFromArray($req->bodyParams());
     if (!$wrap->graphIsValid()) {
         return;
     }
     if (!$this->em->isPersisted($node)) {
         $this->em->persist($node);
     }
     $this->em->flush();
     $this->notify(Chalk::info($node->content)->singular . " <strong>{$node->content->name}</strong> was saved successfully", 'positive');
     return $res->redirect($this->url(['node' => $node->id]));
 }
Пример #8
0
 public function upload(Request $req, Response $res)
 {
     if (!$req->isPost()) {
         throw new \Chalk\Exception("Upload action only accepts POST requests");
     }
     $dir = $this->chalk->config->dataDir->dir('upload', true);
     $uploader = new FileUpload($_FILES['files'], $req->servers());
     $uploader->setPathResolver(new PathResolver\Simple($dir->name()));
     $uploader->setFileSystem(new FileSystem\Simple());
     list($uploads, $headers) = $uploader->processAll();
     foreach ($uploads as $upload) {
         if (isset($upload->path)) {
             $content = isset($req->route['params']['content']) ? $this->em($req->info)->id($req->route['params']['content']) : $this->em($req->info)->create();
             $view = $content->isNew() ? 'content/thumb' : 'content/card-upload';
             if (!$this->em->isPersisted($content)) {
                 $content->newFile = new \Coast\File($upload->path);
                 $this->em->persist($content);
             } else {
                 $content->move(new \Coast\File($upload->path));
             }
             $this->em->flush();
             unset($upload->path);
             $upload->html = $this->view->render($view, ['content' => $content, 'covered' => true, 'isEditAllowed' => (bool) $req->isEditAllowed] + (array) $req->view, 'core')->toString();
         }
     }
     return $res->headers($headers)->json(['files' => $uploads]);
 }