/** * Helper to build the entity form. * * @param Admin $app * @param string $entityName * @param mixed|null $id * * return \Folk\Formats\Form */ protected static function createForm(Admin $app, $entityName, $id = null) { $entity = $app->getEntity($entityName); $form = F::form()->method('post')->enctype('multipart/form-data')->add(['entity' => F::hidden()->val($entityName)->class('field-data-entity'), 'data' => $entity->getScheme($app['builder'])]); if ($id !== null) { $form['id'] = F::hidden()->val($id)->class('field-data-id'); } return $form; }
public function json(Request $request, Admin $app, $entityName) { $entity = $app->getEntity($entityName); $search = new SearchQuery($request->getQueryParams()); $json = []; foreach ($this->search($entity, $search) as $id => $item) { $json[$id] = $entity->getLabel($id, $item); } return json_encode($json); }
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); }
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'); }
public function html(Request $request, Admin $app, $entityName) { $app->getEntity($entityName)->delete($request->getAttribute('id')); return new RedirectResponse($app->getRoute('search', ['entity' => $entityName])); }