135 lines
4.0 KiB
PHP
135 lines
4.0 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;
|
|
public $author_username;
|
|
public $is_in_favorite = 0;
|
|
public $in_usermenu = false;
|
|
|
|
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' => [
|
|
'us.username AS author_username'
|
|
],
|
|
'join_table' => 'users us ON us.id = obj.author_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'
|
|
];
|
|
protected static function get_additional_fields(){
|
|
$add_fields = parent::get_additional_fields();
|
|
|
|
// If user is authorized, we also check product in thw wishlist
|
|
if(CURRENT_USER) {
|
|
$add_fields = array_merge($add_fields, array(
|
|
[
|
|
"field" => [
|
|
"MAX(CASE WHEN fav.user_id = ". CURRENT_USER->get_id() ." THEN 1 ELSE 0 END) AS is_in_favorite"
|
|
],
|
|
"join_table" => "recipe_favorites fav ON fav.recipe_id = obj.id"
|
|
]
|
|
));
|
|
}
|
|
if(CURRENT_USER) {
|
|
$add_fields = array_merge($add_fields, array(
|
|
[
|
|
"field" => [
|
|
"MAX(m.dayofweek) AS in_usermenu"
|
|
],
|
|
"join_table" => "recipe_usermenu m ON m.recipe_id = obj.id AND m.user_id = " . CURRENT_USER->get_id()
|
|
]
|
|
));
|
|
}
|
|
|
|
return $add_fields;
|
|
}
|
|
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_absolute_url()
|
|
{
|
|
return get_permalink('recipes:single', [ $this->get_id() ]);
|
|
}
|
|
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";
|
|
}
|
|
} |