diff --git a/apps/Admin/Controllers/Abstract/AdminSingleController.php b/apps/Admin/Controllers/Abstract/AdminSingleController.php index 04c08e3..5b4c9a2 100644 --- a/apps/Admin/Controllers/Abstract/AdminSingleController.php +++ b/apps/Admin/Controllers/Abstract/AdminSingleController.php @@ -38,6 +38,7 @@ abstract class AdminSingleController extends AdminBaseController protected $field_title = 'field_id'; protected $edit_title_template = 'Edit [:verbose] "[:field]"'; protected $can_save = true; + protected $is_new = false; /** * Function names with $object attribute. @@ -53,6 +54,7 @@ abstract class AdminSingleController extends AdminBaseController */ public function __construct($is_new = false) { + $this->is_new = $is_new; $this->context['is_new'] = $is_new; } diff --git a/apps/Admin/Controllers/AdminCategoryController.php b/apps/Admin/Controllers/AdminCategoryController.php new file mode 100644 index 0000000..a2638cd --- /dev/null +++ b/apps/Admin/Controllers/AdminCategoryController.php @@ -0,0 +1,21 @@ + 'name', + 'input_type' => 'text', + 'input_attrs' => ['required'] + ] + ); +} \ No newline at end of file diff --git a/apps/Admin/Controllers/AdminCategoryListController.php b/apps/Admin/Controllers/AdminCategoryListController.php new file mode 100644 index 0000000..aa824f2 --- /dev/null +++ b/apps/Admin/Controllers/AdminCategoryListController.php @@ -0,0 +1,17 @@ + 'field_name' + ); + protected $single_router_name = 'admin:category'; + protected $create_router_name = 'admin:category-new'; + protected $verbose_name = "category"; + protected $verbose_name_multiply = "categories"; +} \ No newline at end of file diff --git a/apps/Admin/Controllers/AdminDeleteController.php b/apps/Admin/Controllers/AdminDeleteController.php index ed9e085..17ac960 100644 --- a/apps/Admin/Controllers/AdminDeleteController.php +++ b/apps/Admin/Controllers/AdminDeleteController.php @@ -9,6 +9,49 @@ use Lycoreco\Apps\Users\Models\UserModel; */ class AdminDeleteController extends Abstract\AdminBaseController { + const DELETE_MODELS = [ + [ + 'url_name' => 'users', + 'model' => 'Lycoreco\Apps\Users\Models\UserModel', + 'field_display' => 'field_username', + 'back_to' => 'admin:user', + 'success_to' => 'admin:user-list' + ], + [ + 'url_name' => 'user-banlist', + 'model' => 'Lycoreco\Apps\Users\Models\BanlistModel', + 'field_display' => 'field_reason', + 'back_to' => 'admin:ban' + ], + [ + 'url_name' => 'recipes', + 'model' => 'Lycoreco\Apps\Recipes\Models\RecipeModel', + 'field_display' => 'field_title', + 'back_to' => 'admin:recipe', + 'success_to' => 'admin:recipe-list' + ], + [ + 'url_name' => 'ingredients', + 'model' => 'Lycoreco\Apps\Recipes\Models\IngredientModel', + 'field_display' => 'field_name', + 'back_to' => 'admin:ingredient', + 'success_to' => 'admin:ingredient-list' + ], + [ + 'url_name' => 'categories', + 'model' => 'Lycoreco\Apps\Recipes\Models\CategoryModel', + 'field_display' => 'field_name', + 'back_to' => 'admin:category', + 'success_to' => 'admin:category-list' + ], + [ + 'url_name' => 'recipe-ingredients', + 'model' => 'Lycoreco\Apps\Recipes\Models\IngredientInRecipeModel', + 'field_display' => 'ingredient_name', + 'back_to' => 'admin:ing-cat-rel', + ], + ]; + protected $template_name = APPS_PATH . '/Admin/Templates/delete.php'; /** @@ -22,17 +65,15 @@ class AdminDeleteController extends Abstract\AdminBaseController $id = $this->url_context['url_2']; $model_class = ''; - switch ($this->url_context['url_1']) { - case 'users': - $model_class = "Lycoreco\Apps\Users\Models\UserModel"; + foreach (self::DELETE_MODELS as $delete_model) { + if($this->url_context['url_1'] == $delete_model['url_name']) { + $model_class = $delete_model['model']; break; - case 'user-banlist': - $model_class = "Lycoreco\Apps\Users\Models\BanlistModel"; - break; - - default: - return null; + } } + if(empty($model_class)) + return null; + return $model_class::get(array( [ 'name' => 'obj.id', @@ -52,16 +93,12 @@ class AdminDeleteController extends Abstract\AdminBaseController // Display field to show what's model $field = ''; $back_url = ''; - switch ($this->url_context['url_1']) { - case 'users': - $back_url = get_permalink('admin:user', [$model->get_id()]); - $field = 'field_username'; - break; - case 'user-banlist': - $back_url = get_permalink('admin:ban', [$model->get_id()]); - $field = 'field_reason'; - break; + foreach (self::DELETE_MODELS as $delete_model) { + if($this->url_context['url_1'] == $delete_model['url_name']) { + $back_url = get_permalink($delete_model['back_to'], [$model->get_id()]); + $field = $delete_model['field_display']; + } } $context['back_url'] = $back_url; @@ -80,14 +117,14 @@ class AdminDeleteController extends Abstract\AdminBaseController $model->delete(); // Redirect after delete - $type_model = $this->url_context['url_1']; - switch ($type_model) { - case 'users': - $link = get_permalink('admin:user-list'); - break; - default: - $link = get_permalink('admin:home'); + $link = get_permalink('admin:home'); + foreach (self::DELETE_MODELS as $delete_model) { + if($this->url_context['url_1'] == $delete_model['url_name']) { + if(isset($delete_model['success_to'])) { + $link = get_permalink($delete_model['success_to']); + } break; + } } redirect_to($link); } diff --git a/apps/Admin/Controllers/AdminRecipeController.php b/apps/Admin/Controllers/AdminRecipeController.php new file mode 100644 index 0000000..b9c88c6 --- /dev/null +++ b/apps/Admin/Controllers/AdminRecipeController.php @@ -0,0 +1,78 @@ + 'title', + 'input_type' => 'text', + 'input_attrs' => ['required'] + ], + [ + 'model_field' => 'instruction', + 'input_type' => 'textarea', + 'input_attrs' => ['required'] + ], + [ + 'model_field' => 'image_url', + 'input_type' => 'image', + 'input_label' => 'Image', + ], + [ + 'model_field' => 'estimated_time', + 'input_type' => 'number', + 'input_attrs' => ['required'], + 'input_label' => 'Estimated time (min)' + ], + [ + 'model_field' => 'estimated_price', + 'input_type' => 'number', + 'input_attrs' => ['required'], + 'input_label' => 'Estimated price ($)' + ], + [ + 'model_field' => 'status', + 'input_type' => 'select', + 'input_attrs' => ['required'], + 'input_values' => RecipeModel::STATUS + ], + [ + 'model_field' => 'created_at', + 'input_type' => 'text', + 'dynamic_save' => false, + 'input_label' => 'Created at', + 'input_attrs' => ['disabled'] + ] + ); + protected function before_save(&$object) + { + if($this->is_new) + { + $object->field_author_id = CURRENT_USER->get_id(); + } + } + + public function __construct($is_new = false) { + parent::__construct($is_new); + $this->fields[] = [ + 'model_field' => 'category_id', + 'input_type' => 'select', + 'input_label' => 'Categories', + 'input_attrs' => ['required'], + 'input_values' => CategoryModel::get_cat_values() + ]; + } +} \ No newline at end of file diff --git a/apps/Admin/Controllers/AdminRecipeListController.php b/apps/Admin/Controllers/AdminRecipeListController.php new file mode 100644 index 0000000..cdcd88e --- /dev/null +++ b/apps/Admin/Controllers/AdminRecipeListController.php @@ -0,0 +1,20 @@ + 'field_title', + 'Price' => 'get_price()', + 'Status ' => 'get_status()', + 'Creaated at' => 'field_created_at', + ); + protected $single_router_name = 'admin:recipe'; + protected $create_router_name = 'admin:recipe-new'; + protected $verbose_name = "recipe"; + protected $verbose_name_multiply = "recipes"; +} \ No newline at end of file diff --git a/apps/Admin/Controllers/IngredientController.php b/apps/Admin/Controllers/IngredientController.php new file mode 100644 index 0000000..07681eb --- /dev/null +++ b/apps/Admin/Controllers/IngredientController.php @@ -0,0 +1,27 @@ + 'name', + 'input_type' => 'text', + 'input_attrs' => ['required'] + ], + [ + 'model_field' => 'unit_name', + 'input_type' => 'text', + 'input_attrs' => ['required'], + 'input_label' => 'Unit name' + ] + ); +} \ No newline at end of file diff --git a/apps/Admin/Controllers/IngredientListController.php b/apps/Admin/Controllers/IngredientListController.php new file mode 100644 index 0000000..f4795d7 --- /dev/null +++ b/apps/Admin/Controllers/IngredientListController.php @@ -0,0 +1,18 @@ + 'field_name', + 'Unit' => 'field_unit_name' + ); + protected $single_router_name = 'admin:ingredient'; + protected $create_router_name = 'admin:ingredient-new'; + protected $verbose_name = "ingredient"; + protected $verbose_name_multiply = "ingredients"; +} \ No newline at end of file diff --git a/apps/Admin/Controllers/IngredientRecipeRelController.php b/apps/Admin/Controllers/IngredientRecipeRelController.php new file mode 100644 index 0000000..4b7afe7 --- /dev/null +++ b/apps/Admin/Controllers/IngredientRecipeRelController.php @@ -0,0 +1,50 @@ + 'recipe_id', + 'input_type' => 'number', + 'dynamic_save' => false, + 'input_label' => 'Recipe id', + 'input_attrs' => ['required', 'disabled'] + ], + [ + 'model_field' => 'amount', + 'input_type' => 'text', + 'input_attrs' => ['required'] + ], + ); + protected function get_model() + { + $model = parent::get_model(); + + if($this->context['is_new']) + $model->field_recipe_id = (int)$this->url_context['url_1']; + + return $model; + } + public function __construct($is_new = false) { + parent::__construct($is_new); + + $this->fields[] = [ + 'model_field' => 'ingredient_id', + 'input_type' => 'select', + 'input_label' => 'Ingedient', + 'input_attrs' => ['required'], + 'input_values' => IngredientModel::get_ing_values() + ]; + } +} \ No newline at end of file diff --git a/apps/Admin/Controllers/IngredientRecipeRelListController.php b/apps/Admin/Controllers/IngredientRecipeRelListController.php new file mode 100644 index 0000000..1f319e3 --- /dev/null +++ b/apps/Admin/Controllers/IngredientRecipeRelListController.php @@ -0,0 +1,28 @@ + 'ingredient_name', + 'Amount' => 'get_count()' + ); + protected $single_router_name = 'admin:ing-cat-rel'; + protected $verbose_name = "ingredients in recipe"; + protected $verbose_name_multiply = "ingredients in recipe"; + + public function custom_filter_fields() + { + $recipe_id = $this->url_context['url_1']; + return array( + [ + 'name' => 'obj.recipe_id', + 'type' => '=', + 'value' => $recipe_id + ] + ); + } +} diff --git a/apps/Admin/Templates/components/admin-header.php b/apps/Admin/Templates/components/admin-header.php index d5b508a..71bb505 100644 --- a/apps/Admin/Templates/components/admin-header.php +++ b/apps/Admin/Templates/components/admin-header.php @@ -1,5 +1,33 @@ 'Dashboard', + 'icon' => 'fa-solid fa-house', + 'router_name' => 'admin:home', + ], + [ + 'name' => 'Users', + 'icon' => 'fa-solid fa-users', + 'router_name' => 'admin:user-list', + ], + [ + 'name' => 'Recipes', + 'icon' => 'fa-solid fa-bowl-food', + 'router_name' => 'admin:recipe-list' + ], + [ + 'name' => 'Categories', + 'icon' => 'fa-solid fa-tag', + 'router_name' => 'admin:category-list' + ], + [ + 'name' => 'Ingredients', + 'icon' => 'fa-solid fa-carrot', + 'router_name' => 'admin:ingredient-list' + ] +]; ?> @@ -45,19 +73,16 @@ use Lycoreco\Includes\Routing\Router;