diff --git a/index.php b/index.php index dabb0f7..b5bd296 100755 --- a/index.php +++ b/index.php @@ -3,6 +3,8 @@ ini_set("display_errors", 1); error_reporting(-1); const ROOT_PATH = __DIR__; + +const BASE_URL = "http://forms.local"; const VIEW_PATH = __DIR__ . "/views"; const VIEW_CACHE_PATH = __DIR__ . "/compilation_cache"; @@ -24,9 +26,10 @@ $router->get("/form-res/{id}", [\itguild\forms\app\controllers\FormResController $router->get("/input-type/{id}", [\itguild\forms\app\controllers\InputTypeController::class, "indexAction"]); $router->get("/input-value/{id}", [\itguild\forms\app\controllers\InputValueController::class, "indexAction"]); $router->post("/form-save/{id}", [\itguild\forms\app\controllers\FormController::class, "saveAction"]); -$router->get("/form-load/{id}", [\itguild\forms\app\controllers\FormController::class, "result"]); -$router->get("/form-load/{id}/{page}", [\itguild\forms\app\controllers\FormController::class, "result"]); +$router->get("/form-result/{id}", [\itguild\forms\app\controllers\FormController::class, "result"]); +$router->get("/form-result/{id}/{page}", [\itguild\forms\app\controllers\FormController::class, "result"]); $router->get("/form-item/{id}", [\itguild\forms\app\controllers\FormController::class, "getResultItem"]); +$router->get("/form-delete/{id}", [\itguild\forms\app\controllers\FormController::class, "deleteItem"]); $dispatcher = new Phroute\Phroute\Dispatcher($router->getData()); diff --git a/src/app/controllers/FormController.php b/src/app/controllers/FormController.php index 22355c2..5c984c4 100644 --- a/src/app/controllers/FormController.php +++ b/src/app/controllers/FormController.php @@ -2,21 +2,16 @@ namespace itguild\forms\app\controllers; -use Exception; use itguild\forms\app\core\BaseController; - -use itguild\forms\app\models\FormInputModel; use itguild\forms\app\models\FormModel; use itguild\forms\app\models\FormResModel; use itguild\forms\app\models\InputValueModel; -use itguild\forms\app\models\User; -use itguild\forms\debug\Debug; use itguild\forms\exceptions\FormNotFoundException; use itguild\forms\Form; use itguild\forms\JsonForm; -use itguild\forms\widgets\PaginationWidget; -use itguild\forms\widgets\ResultWidget; -use itguild\forms\widgets\TableJsonWidget; +use itguild\forms\table\ListJsonTable; +use itguild\forms\table\Pagination; +use itguild\forms\table\ViewJsonTable; use Twig\TwigFunction; @@ -105,21 +100,24 @@ class FormController extends BaseController $formParams = json_decode($form->params, true); $perPage = $formParams['meta']['perpage'] ?? "10"; $fillable = $formParams["meta"]["fillable"] ?? null; + $actions = $formParams["meta"]["actions"] ?? null; $offset = ($page - 1) * $perPage; $countItems = FormResModel::count(); - $table = FormResModel::where("form_id", $id)->limit(3)->offset($offset)->get(); + $table = FormResModel::where("form_id", $id)->where("status", FormResModel::STATUS_ACTIVE)->limit(3)->offset($offset)->get(); foreach ($table as $key => $item) { - $data[] = array_merge(json_decode($item->data, true), ["id" => $item->id]); + + $data[] = array_merge(json_decode($item->data, true), ["id" => $item->id]); } if ($form->fields) { foreach ($form->fields as $field) { - if ($field->input_type_id === 6){ - $columnsToMod[] = $field->name; + if ($field->input_type_id === 6) { + $fillable = $formParams["meta"]["fillable"] ?? null; + $columnsToMod[] = $field->name; - } + } $columns[$field->name] = $field->label; @@ -132,15 +130,16 @@ class FormController extends BaseController $meta["fillable"] = $fillable; $meta['params'] = ["class" => "table table-bordered", "border" => "1"]; $meta['currentPage'] = $page; - $meta['baseUrl'] = "http://forms.local/"; + $meta['actions'] = $actions; + $meta['baseUrl'] = BASE_URL; $tableArr["meta"] = $meta; $tableArr["data"] = $data; - //Debug::prn($tableArr); + $tableRes = json_encode($tableArr); $this->view->addFunction(new TwigFunction("create_table", function () use ($tableRes, $columnsToMod, $form) { - $table = new TableJsonWidget($tableRes); + $table = new ListJsonTable($tableRes); $table->setBeforePrintCell(function ($column, $data) use ($columnsToMod, $form){ @@ -160,7 +159,7 @@ class FormController extends BaseController $this->view->addFunction(new TwigFunction("create_pagination", function () use ($perPage, $page, $countItems, $form) { - $pagination = new PaginationWidget($countItems, $perPage, $page, "http://forms.local/form-load/$form->id"); + $pagination = new Pagination($countItems, $perPage, $page, BASE_URL . "/form-result/" . $form->id); $pagination->create(); $pagination->render(); @@ -169,11 +168,18 @@ class FormController extends BaseController } + public function deleteItem($id) + { + $item = FormResModel::find($id)->update(["status" => FormResModel::STATUS_ARCHIVE]); + header( 'Location: ' . '' ); + exit; + + } + public function getResultItem($id) { $resultForm = FormResModel::find($id); $formID = $resultForm->form_id; - $form = FormModel::find($formID); $meta = []; $columnsToMod = []; @@ -209,7 +215,7 @@ class FormController extends BaseController $this->view->addFunction(new TwigFunction("create_get_action", function () use ($tableRes, $columnsToMod, $form) { - $pagination = new ResultWidget($tableRes); + $pagination = new ViewJsonTable($tableRes); $pagination->setBeforePrintCell(function ($column, $data) use ($columnsToMod, $form) { if (in_array($column, $columnsToMod)) { $res = InputValueModel::find($data); diff --git a/src/app/models/FormResModel.php b/src/app/models/FormResModel.php index 1010043..f665a07 100644 --- a/src/app/models/FormResModel.php +++ b/src/app/models/FormResModel.php @@ -6,10 +6,12 @@ use Illuminate\Database\Eloquent\Model; class FormResModel extends Model { + const STATUS_ACTIVE = 1; + const STATUS_ARCHIVE = 2; protected $table = "form_res"; protected $fillable = [ - 'form_id', 'data' + 'form_id', 'data', 'status' ]; protected $hidden = [ diff --git a/src/table/ActionColumn/ActionColumn.php b/src/table/ActionColumn/ActionColumn.php new file mode 100644 index 0000000..e385990 --- /dev/null +++ b/src/table/ActionColumn/ActionColumn.php @@ -0,0 +1,19 @@ +baseUrl = $baseUrl; + $this->id = $id; + } + + abstract public function fetch(); + + +} \ No newline at end of file diff --git a/src/table/ActionColumn/DeleteActionColumn.php b/src/table/ActionColumn/DeleteActionColumn.php new file mode 100644 index 0000000..2e3589e --- /dev/null +++ b/src/table/ActionColumn/DeleteActionColumn.php @@ -0,0 +1,14 @@ +baseUrl . $this->prefix . $this->id; + return " Удалить "; + } +} \ No newline at end of file diff --git a/src/table/ActionColumn/ViewActionColumn.php b/src/table/ActionColumn/ViewActionColumn.php new file mode 100644 index 0000000..eff82b7 --- /dev/null +++ b/src/table/ActionColumn/ViewActionColumn.php @@ -0,0 +1,14 @@ +baseUrl . $this->prefix . $this->id; + return " Просмотр "; + } +} \ No newline at end of file diff --git a/src/widgets/TableJsonWidget.php b/src/table/ListJsonTable.php similarity index 71% rename from src/widgets/TableJsonWidget.php rename to src/table/ListJsonTable.php index e67bcf0..4557d5e 100644 --- a/src/widgets/TableJsonWidget.php +++ b/src/table/ListJsonTable.php @@ -1,13 +1,14 @@ beforePrintCell = false; $this->json = $json; $this->data = json_decode($this->json, true); $this->baseUrl = $this->data['meta']['baseUrl'] ?? null; + $this->setActions(); } public function beginTable(): void @@ -42,7 +46,7 @@ class TableJsonWidget extends BaseWidget $this->html .= ""; $this->html .= "" . "ID" . ""; foreach ($this->data['meta']['columns'] as $key => $column) { - if ($this->is_fillable($key)){ + if ($this->is_fillable($key)) { $this->html .= "" . $column . ""; } } @@ -57,7 +61,7 @@ class TableJsonWidget extends BaseWidget foreach ($this->data['data'] as $col) { $this->html .= ""; $this->count += 1; - $this->html .= '' . $this->count . ''; + $this->html .= '' . $this->count . ''; foreach ($col as $key => $row) { if ($this->issetColumn($key) and $this->is_fillable($key)) { if ($this->beforePrintCell) { @@ -68,7 +72,26 @@ class TableJsonWidget extends BaseWidget $this->html .= "" . $row . ""; } } - $this->html .= "ПросмотрУдалить"; + + $actions = $this->getActions($col["id"]); + + $this->html .= "$actions"; + } + } + } + + private function setActions(): void + { + if (isset($this->data['meta']['actions'])) { + foreach ($this->data['meta']['actions'] as $action) { + switch ($action) { + case 'view': + $this->actionsArray[] = ViewActionColumn::class; + break; + case 'delete': + $this->actionsArray[] = DeleteActionColumn::class; + break; + } } } } @@ -95,6 +118,17 @@ class TableJsonWidget extends BaseWidget return false; } + private function getActions(int $id): string + { + $actions = ""; + foreach ($this->actionsArray as $item) { + $objItem = new $item($this->baseUrl, $id); + $actions .= $objItem->fetch(); + } + + return $actions; + } + public function create() { $this->beginTable(); diff --git a/src/widgets/PaginationWidget.php b/src/table/Pagination.php similarity index 95% rename from src/widgets/PaginationWidget.php rename to src/table/Pagination.php index 7480de2..c6d51a4 100644 --- a/src/widgets/PaginationWidget.php +++ b/src/table/Pagination.php @@ -1,10 +1,10 @@ data = json_decode($this->json, true); $this->dataJson = json_decode($this->data['data']['data'], true); + } public function beginTable(): void { + $paramsStr = $this->createParams($this->data['meta']['params']); $this->html = ""; } diff --git a/src/widgets/BaseWidget.php b/src/widgets/BaseWidget.php deleted file mode 100644 index a7e2be5..0000000 --- a/src/widgets/BaseWidget.php +++ /dev/null @@ -1,8 +0,0 @@ -