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'))); } }
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))); }
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))); }
public function postExecute(Request $req, Response $res) { if (!preg_match($this->_regex, $req->path(), $path)) { return; } if ($this->_executable instanceof Executable) { $this->_executable->postExecute($req, $res); } }
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)); }
public function execute(Request $req, Response $res) { if (!in_array($req->method(), $this->_methods)) { return; } $this->isValid($req->param($this->_name), true); $req->param($this->_name, null); }
public function edit(Request $req, Response $res) { $class = $req->info->class; $widget = new $class(); $req->view->widget = $wrap = $this->em->wrap($widget); $wrap->graphFromArray($req->bodyParams()); if (!$req->post) { return; } if (!$wrap->isValid()) { return; } $req->data->entity = $req->info->name; $req->data->params = array_map(function ($value) { return is_object($value) ? $value->id : $value; }, $widget->toArray()); $req->data->html = $this->view->render('widget/card', ['widget' => $widget], 'core')->toString(); }
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([])); }
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)); }
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; }
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])); }
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])); }
public function execute(\Coast\Request $req, \Coast\Response $res) { $route = $this->match($req->method(), $req->path()); if (!$route) { return; } $req->params(array_merge(['route' => $route], $route['params'])); if (isset($route['target'])) { return $route['target']($req, $res, $this->app); } else { if (isset($this->_target)) { return $this->_target->route($req, $res); } else { throw new Router\Exception("There's nothing to route '{$route['name']}' to"); } } }
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)); }
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]); }