public function preDispatch(Request $req, Response $res) { $this->app->module = $this->chalk->module($req->group); $this->contentList = $this->hook->fire('core_contentList', new InfoList('core_main')); $this->widgetList = $this->hook->fire('core_widgetList', new InfoList()); $this->navList = $this->hook->fire('core_navList', new NavList()); $session = $this->session->data('__Chalk\\Backend'); if (!isset($session->user) && $req->controller !== 'auth') { $redirect = (string) $req->url()->toPart(Url::PART_PATH, true); return $res->redirect($this->url([], 'core_login', true) . $this->url->query(['redirect' => $redirect], true)); } $req->data = (object) []; $req->view = (object) []; if ($req->controller == 'auth') { return; } $req->user = $this->em('Chalk\\Core\\User')->id($session->user); if (!isset($req->user)) { $session->user = null; return $res->redirect($this->url([], 'core_login', true)); } $this->em->listener('core_trackable')->setUser($req->user); $saves = []; if (in_array("{$req->controller}__{$req->action}", $saves)) { $name = "query_" . md5(serialize($req->route['params'])); $params = $req->user->pref($name); unset($params['contentIds']); unset($params['batch']); if (isset($params)) { $req->queryParams($req->queryParams() + $params); } $params = $req->queryParams(); unset($params['contentIds']); unset($params['batch']); $req->user->pref($name, $params); $this->em->flush(); } }
public function edit(Request $req, Response $res) { $content = isset($req->route['params']['content']) ? $this->em($req->info)->id($req->route['params']['content']) : $this->em($req->info)->create(); $req->view->content = $wrap = $this->em->wrap($content); if ($content->isNew()) { $wrap->graphFromArray($req->queryParams()); } if (!$req->isPost()) { return; } $wrap->graphFromArray($req->bodyParams()); if (!$wrap->graphIsValid()) { $this->notify("{$req->info->singular} could not be saved, please check the messages below", 'negative'); return; } if (!$this->em->isPersisted($content)) { $this->em->persist($content); } $this->em->flush(); $this->notify("{$req->info->singular} <strong>{$content->name}</strong> was saved successfully", 'positive'); return $res->redirect($this->url(array('action' => 'edit', 'content' => $content->id))); }
public function add(Request $req, Response $res) { $filters = $this->chalk->module('core')->contentList('core_node'); $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()); if (isset($req->node)) { $parent = $this->em('Chalk\\Core\\Structure\\Node')->id($req->node); } else { $parent = $this->em('Chalk\\Core\\Structure')->id($req->structure)->root; } foreach ($index->contents as $content) { $node = new \Chalk\Core\Structure\Node(); $node->parent = $parent; $node->content = $content; $this->em->persist($node); } $this->em->flush(); $this->notify("Content was added successfully", 'positive'); if (isset($req->node)) { $req->data->redirect = $this->url(['action' => 'edit'])->toString(); } else { $req->data->redirect = $this->url(['action' => 'index', 'node' => null], 'core_structure')->toString(); } }
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; }