Example #1
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)));
 }
Example #2
0
 public function postDispatch(Request $req, Response $res)
 {
     $config = $this->chalk->config->viewScripts;
     $path = "{$config[0]}/{$req->group}/{$req->controller}/{$req->action}";
     $params = (array) $req->view + ['req' => $req, 'res' => $res];
     return $res->html($this->view->render($path, $params, $config[1]));
 }
Example #3
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')));
     }
 }
Example #4
0
 public function index(Request $req, Response $res)
 {
     $items = $this->navList->children('core_setting');
     if (count($items)) {
         $item = current($items);
         return $res->redirect($this->url($item['url'][0], $item['url'][1], true));
     }
 }
Example #5
0
 public function testErrorHandler()
 {
     $app = new App(__DIR__);
     $app->errorHandler(function (Request $req, Response $res) {
         return $res->text('ERROR');
     });
     $app->execute($req = new Request(), $res = new Response($req));
     $this->assertEquals('ERROR', $res->body());
 }
Example #6
0
 public function delete(Request $req, Response $res)
 {
     $entity = $this->em($req->info)->find($req->id);
     $this->em->remove($entity);
     try {
         $this->em->flush();
     } catch (ForeignKeyConstraintViolationException $e) {
         $this->notify("{$req->info->singular} <strong>{$entity->name}</strong> could not be deleted because it is in use", 'negative');
         return $res->redirect($this->url(array('action' => 'edit')));
     }
     $this->notify("{$req->info->singular} <strong>{$entity->name}</strong> was deleted successfully", 'positive');
     return $res->redirect($this->url(array('action' => null, 'id' => null)));
 }
Example #7
0
 public function postDispatch(Request $req, Response $res)
 {
     $controller = strtolower(str_replace('_', '/', $req->controller));
     $action = strtolower(str_replace('_', '-', $req->action));
     $path = isset($req->view->path) ? $req->view->path : "{$controller}/{$action}";
     $isJson = $req->isAjax() && count((array) $req->data);
     $isNotify = $req->isAjax() && !isset($req->data->redirect);
     if ($isNotify) {
         $req->data->notifications = $this->notify->notifications();
     }
     if ($isJson) {
         return $res->json($req->data);
     }
     return $res->header('X-JSON', json_encode($req->data))->html($this->view->render($path, ['req' => $req, 'res' => $res] + (array) $req->view, $req->group));
 }
Example #8
0
 public function delete(Request $req, Response $res)
 {
     $node = $this->em('Chalk\\Core\\Structure\\Node')->id($req->node);
     $parent = $node->parent;
     $parent->id;
     foreach ($node->children as $child) {
         $node->children->removeElement($child);
         $child->parent = $parent;
         $child->sort = \Chalk\Core\Structure\Node::VALUE_MAX;
     }
     $this->em->remove($node);
     $this->em->flush();
     $this->notify(Chalk::info($node->content)->singular . " <strong>{$node->content->name}</strong> was removed successfully", 'positive');
     return $res->redirect($this->url(array('action' => 'index', 'structure' => $node->structure->id), 'core_structure', true));
 }
Example #9
0
 public function feed(Request $req, Response $res)
 {
     $articles = $this->em($this->module->name('article'))->all(['limit' => $this->module->option('feedLimit')]);
     $feed = new Feed("{$this->home['name']} {$req->content->name}", $this->url->route([], $this->module->name('main'), true), "{$this->chalk->config->name}", $articles[0]->publishDate);
     foreach ($articles as $article) {
         $feed->add($article->name, $this->url($article), $this->app->date($article->publishDate), $article->description($this->module->option('extractLength')), $this->parser->parse($article->body));
     }
     return $res->xml($feed->toXml(), 'atom');
 }
Example #10
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([]));
 }
Example #11
0
 public function logout(Request $req, Response $res)
 {
     // $this->session->regenerate();
     $session = $this->session->data('__Chalk\\Backend');
     $session->user = null;
     return $res->redirect($this->url([], 'core_login', true));
 }
Example #12
0
 public function postExecute(Request $req, Response $res)
 {
     $header = $this->_isReportOnly ? 'Content-Security-Policy-Report-Only' : 'Content-Security-Policy';
     $res->header($header, $this->toString());
 }
Example #13
0
 public function xml(Request $req, Response $res)
 {
     $sitemap = $this->hook->fire($this->module->name('xml'), new CoastSitemap());
     return $res->xml($sitemap->toXml());
 }
Example #14
0
 public function forbidden(Request $req, Response $res)
 {
     return $res->status(403)->html($this->view->render('error/forbidden', ['req' => $req, 'res' => $res], 'core'));
 }
Example #15
0
 public function robots(Request $req, Response $res)
 {
     $lines = isset($this->chalk->config->robotsTxt) ? $this->chalk->config->robotsTxt : ['User-agent: *', 'Disallow:'];
     $robots = $this->hook->fire('core_robots', (object) ['lines' => $lines]);
     return $res->text(implode("\n", $robots->lines));
 }
Example #16
0
 public function delete(Request $req, Response $res)
 {
     $content = $this->em($req->info)->find($req->content);
     try {
         $this->em->remove($content);
         $this->em->flush();
     } catch (ForeignKeyConstraintViolationException $e) {
         $this->notify("{$req->info->singular} <strong>{$content->name}</strong> cannot be deleted because it is in use", 'negative');
         if (isset($req->redirect)) {
             return $res->redirect($req->redirect);
         } else {
             return $res->redirect($this->url(array('action' => 'edit', 'content' => $content->id)));
         }
     }
     $this->notify("{$req->info->singular} <strong>{$content->name}</strong> was deleted successfully", 'positive');
     return $res->redirect($this->url(array('action' => 'index', 'content' => null)));
 }
Example #17
0
 public function execute(Request $req, Response $res)
 {
     $parts = explode('/', $req->path());
     if ($parts[0] != 'image') {
         return;
     }
     $file = new File("{$this->_baseDir}/{$req->file}");
     $file = $file->toReal();
     if (!$file->isWithin($this->_baseDir)) {
         throw new Image\Exception("File '{$file}' is not within base directory '{$this->_baseDir}'");
     } else {
         if (!$file->isReadable()) {
             throw new Image\Exception("File '{$file}' is not readable");
         }
     }
     $transforms = $req->transforms;
     $params = isset($req->params) ? $req->params : [];
     $output = $this->_generateOutput($file, $transforms, $params);
     $image = $this->_manager->make($file->name());
     foreach ($transforms as $name) {
         $this->run($name, $image, $params);
     }
     $image->save($output->name(), isset($image->quality) ? $image->quality : 90);
     return $res->redirect(isset($this->_outputUrlResolver) ? $this->_outputUrlResolver->file($output) : $this->_urlResolver->file($output));
 }
Example #18
0
 public function execute(\Coast\Request $req, \Coast\Response $res)
 {
     $path = $req->path();
     $path = '/' . (strlen($path) ? $path : 'index');
     $files = $this->files($this->script($path));
     if (!count($files)) {
         return;
     }
     return $res->html($this->render($path, ['req' => $req, 'res' => $res]));
 }
Example #19
0
 public function index(Request $req, Response $res)
 {
     return $res->redirect($this->url->file($req->file->file));
 }
Example #20
0
 public function index(Request $req, Response $res)
 {
     return $res->redirect($this->url($req->alias->content));
 }