fridge_bites/apps/Recipes/Models/RecipeModel.php
2025-06-29 11:43:33 +02:00

95 lines
2.6 KiB
PHP

<?php
namespace Lycoreco\Apps\Recipes\Models;
use Lycoreco\Includes\Model\BaseModel;
class RecipeModel extends BaseModel
{
public $field_title;
public $field_instruction;
public $field_image_url;
public $field_estimated_time;
public $field_estimated_price;
public $field_category_id;
public $field_author_id;
public $field_status;
public $field_created_at;
public $category_name;
const STATUS = [['publish', 'Publish'], ['pending', 'Pending']];
static protected $search_fields = ['obj.title'];
static protected $table_name = 'recipes';
static protected $additional_fields = array(
[
'field' => [
'tb1.name AS category_name'
],
'join_table' => 'categories tb1 ON tb1.id = obj.category_id'
],
[
'field' => [
],
'join_table' => 'recipe_ingredients tb2 ON tb2.recipe_id = obj.id'
]
);
static protected $table_fields = [
'id' => 'int',
'title' => 'string',
'instruction' => 'string',
'image_url' => 'string',
'estimated_time' => 'int',
'estimated_price' => 'float',
'category_id' => 'int',
'author_id' => 'int',
'status' => 'string',
'created_at' => 'DateTime'
];
public static function init_table()
{
$result = db_query('CREATE TABLE ' . static::$table_name . ' (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
instruction TEXT NOT NULL,
image_url VARCHAR(255) NULL,
estimated_time INT NOT NULL,
estimated_price INT NOT NULL,
category_id INT NOT NULL,
author_id INT NOT NULL,
status VARCHAR(20) NOT NULL CHECK (status IN (\'publish\', \'pending\')),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (author_id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (category_id) REFERENCES categories(id) ON DELETE CASCADE
);');
return $result;
}
public function get_price()
{
return $this->field_estimated_price . '$';
}
public function get_status()
{
return ucfirst($this->field_status);
}
public function get_image_url()
{
if ($this->field_image_url)
return MEDIA_URL . $this->field_image_url;
return null;
}
public function get_html_instruction(): string
{
return nl2br(trim($this->field_instruction));
}
public function get_time(){
return $this->field_estimated_time . " minutes";
}
}