public function __invoke(Request $request, Admin $app) { $format = $request->getHeaderLine('Accept'); $format = isset($this->formats[$format]) ? $this->formats[$format] : 'html'; $entityName = $request->getAttribute('entity'); if ($app->hasEntity($entityName) && method_exists($this, $format)) { return $this->{$format}($request, $app, $entityName); } return Factory::createResponse(404); }
public function html(Request $request, Admin $app, $entityName) { $entity = $app->getEntity($entityName); $form = static::createForm($app, $entityName); $form->loadFromPsr7($request); if ($form->validate()) { return new RedirectResponse($app->getRoute('read', ['entity' => $entityName, 'id' => $entity->create($form['data']->val())])); } echo $app['templates']->render('pages/insert', ['entityName' => $entityName, 'form' => $form]); return Factory::createResponse(400); }
public function json(Request $request, Admin $app, $entityName) { $field = $request->getAttribute('field'); $data = $request->getParsedBody(); $form = static::createForm($app, $entityName); $form['data'][$field]->val($data['value']); if ($form->validate()) { $entity = $app->getEntity($entityName); $id = $entity->create($form['data']->val()); $data = $entity->read($id); return json_encode(['id' => $id, 'label' => $entity->getLabel($id, $data)]); } return Factory::createResponse(400); }
public function json(Request $request, Admin $app, $entityName) { $id = $request->getAttribute('id'); $field = $request->getAttribute('field'); $data = $request->getParsedBody(); $form = static::createForm($app, $entityName, $id); $form['data']->val($app->getEntity($entityName)->read($id)); $form['data'][$field]->val($data['value']); if ($form->validate()) { $app->getEntity($entityName)->update($id, $form['data']->val()); return json_encode(['value' => $form['data'][$field]->val(), 'htmlValue' => $form['data'][$field]->valToHtml()]); } return Factory::createResponse(400); }
private function thumbs(Request $request, Admin $app) { $query = $request->getQueryParams(); $thumbs = $app->getPath($query['thumbs']); $limit = empty($query['limit']) ? 100 : (int) $query['limit']; $offset = empty($query['offset']) ? 0 : (int) $query['offset']; $pattern = empty($query['pattern']) ? '/*' : $query['pattern']; $files = []; $baseLength = strlen($thumbs); if (is_dir($thumbs)) { foreach (glob($thumbs . $pattern, GLOB_NOSORT | GLOB_NOESCAPE | GLOB_BRACE) as $file) { if (is_file($file)) { $files[] = substr($file, $baseLength); } } } $files = array_reverse($files); $files = array_splice($files, $offset, $limit); echo json_encode($files); return Factory::createResponse()->withHeader('Content-Type', 'application/json'); }