public function handle() { $action = $this->url->get("action", "default"); $whoops = new \Whoops\Run(); if ($this->url->get("ajax", false)) { $whoops->pushHandler(new \Whoops\Handler\JsonResponseHandler()); } else { $whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler()); } $whoops->register(); $result = null; if (method_exists($this, "handle_" . $action)) { $result = call_user_func(array($this, "handle_" . $action)); } else { throw new Exception("Unknown action"); } $output = ""; if (is_string($result)) { $newResult = array('type' => 'transclude', 'content' => $result); $result = $newResult; } switch ($result['type']) { case "template": $output = $this->twig->renderTemplateToString($result['template'], $result['data']); break; case "json": $this->app->setJsonResponse(true); $data = $result['data']; $data['flashbag'] = FlashBag::getFlashes(); $output = json_encode($data); break; case "redirect": $this->app->_requestRedirect($result['url']); return; break; case "transclude": $pageMap = []; /** @var BasePage $pageItem */ foreach ($this->app->getPages() as $pageItem) { $pageMap[] = array('id' => $pageItem->getId(), 'name' => $pageItem->getName()); } ValueBag::set("flashbag", FlashBag::getFlashes()); $data = array('valueBag' => json_encode(ValueBag::getValues()), 'staticRoot' => $this->app->getStaticRoot(), 'pageMap' => $pageMap, 'defaultUrl' => $this->routeGen->defaultRoute(), 'title' => $this->app->getAppName(), 'userParams' => $this->app->getUserParams(), 'pageTitle' => ''); if ($this->page !== null) { $data['page'] = $this->page; $data['currentId'] = $this->page->getId(); $data['pageTitle'] = $this->page->getName(); } else { $data['currentId'] = -1; } $data['page_content'] = $result['content']; $data['dev'] = false; // change to true to load unminified js $output = $this->twig->renderTemplateToString("main_page.twig", $data); break; default: throw new Exception("Unknown result type"); } return $output; }
public function handle_set_form_values() { $form = new FormHelper(array(), $this->dataProvider->getEditFormConfig()); $url = new UrlHelper(); // let's not worry about validation right now. $values = json_decode($url->get("values_json", "{}"), true); if ($form->validate($values)) { $this->dataProvider->setRow($url->get("item_id", null), $values); FlashBag::add("alert", "Item has been updated", "success"); return array('type' => 'json', 'data' => array('success' => true)); } else { // TODO show errors on validation fail throw new \Exception("Cannot validate values"); } }
public function handle_set_form_values() { $form = new FormHelper(array(), $this->dataProvider->getEditFormConfig()); $url = new UrlHelper(); $values = json_decode($url->get("values_json", "{}"), true); if (empty($values)) { return array('type' => 'json', 'data' => array('success' => true)); } //validate $failedValues = $this->dataProvider->validateRow($values); if (empty($failedValues)) { $this->dataProvider->setRow($url->get("item_id", null), $values); FlashBag::add("alert", "Item has been updated", "success"); return array('type' => 'json', 'data' => array('success' => true, 'dataValid' => true)); } else { FlashBag::add("alert", "Could not update certain fields", "error"); return array('type' => 'json', 'data' => array('success' => true, 'dataValid' => false, 'failedValues' => $failedValues)); //throw new \Exception("Cannot validate values"); } }
public function handle_set_form_values() { if ($this->app->isReadOnly()) { throw new Exception("Read Only"); } $form = new FormHelper(array(), $this->dataProvider->getEditFormConfig()); $url = new UrlHelper(); $values = json_decode($url->get("values_json", "{}"), true); if (empty($values)) { return array('type' => 'json', 'data' => array('success' => true)); } //validate $failedValues = $this->dataProvider->validateRow($values); if (empty($failedValues)) { $url = new UrlHelper(); $rowId = $url->get("item_id", null); $summaryKey = $this->dataProvider->getSummaryColumns()[0]['key']; $rowData = $this->dataProvider->getRow($rowId); $rowName = $rowData[$summaryKey]; $this->dataProvider->setRow($url->get("item_id", null), $values); FlashBag::add("alert", "Item {$rowName} has been updated", "success"); return array('type' => 'json', 'data' => array('success' => true, 'dataValid' => true)); } else { FlashBag::add("alert", "Could not update certain fields", "error"); return array('type' => 'json', 'data' => array('success' => true, 'dataValid' => false, 'failedValues' => $failedValues)); //throw new \Exception("Cannot validate values"); } }