TIST-42: Add id or url to item for search action on ajax #29

Merged
steve_dekart merged 2 commits from TIST-42 into develop 2025-07-03 22:27:52 +02:00
4 changed files with 20 additions and 4 deletions

View File

@ -31,10 +31,17 @@ function ajax_search()
}
$result = array();
$recipes = RecipeModel::filter(
$recipes = array();
$recipe_models = RecipeModel::filter(
count: 5,
search: $search_query
);
foreach ($recipe_models as $recipe_model) {
$recipe = $recipe_model->getAssocArr();
$recipe['image_url'] = $recipe_model->get_image_url();
$recipe['url'] = $recipe_model->get_absolute_url();
$recipes[] = $recipe;
}
$result['count'] = count($recipes);
$result['result'] = $recipes;
@ -132,7 +139,9 @@ function ajax_search_ingredient()
search: $search_query
);
$result['count'] = count($ingredients);
$result['result'] = $ingredients;
$result['result'] = array_map(function($ing) {
return $ing->getAssocArr();
}, $ingredients);
return $result;
}

View File

@ -86,6 +86,10 @@ class RecipeModel extends BaseModel
);');
return $result;
}
public function get_absolute_url()
{
return get_permalink('recipes:single', [ $this->get_id() ]);
}
public function get_price()
{
return $this->field_estimated_price . '$';

View File

@ -1,4 +1,4 @@
<a href="<?php the_permalink('recipes:single', [$recipe->get_id()]) ?>" class="catalog-recipe hover-anim">
<a href="<?= $recipe->get_absolute_url() ?>" class="catalog-recipe hover-anim">
<div class="catalog-recipe__image">
<img src="<?= $recipe->get_image_url() ?>" alt="<?= $recipe->field_title ?>">
</div>

View File

@ -364,6 +364,9 @@ abstract class BaseModel
else
return $filter_result[0]->func_total_count;
}
public function getAssocArr() {
return get_object_vars($this);
}
public function delete()
{
@ -436,7 +439,7 @@ abstract class BaseModel
/**
* Return model from Mysql result
* @param array $pdo_result pdo resut FETCH_MODE = FETCH_ASSOC
* @return array
* @return array(self)
*/
protected static function createObjectsFromQuery(array $pdo_result)
{