Compare commits

..

3 Commits

Author SHA1 Message Date
34df639446 Merge pull request 'TIST-42: Add id or url to item for search action on ajax' (#29) from TIST-42 into develop
Reviewed-on: #29
Reviewed-by: greendavid004 <davidkatrinka1995@gmail.com>
2025-07-03 22:27:51 +02:00
2651014595 Added id fields for search ingredients ajax action 2025-07-03 22:23:42 +02:00
572b7cf3f4 Added url, id, image_url fields for search ajax action 2025-07-03 22:21:24 +02:00
4 changed files with 20 additions and 4 deletions

View File

@ -31,10 +31,17 @@ function ajax_search()
} }
$result = array(); $result = array();
$recipes = RecipeModel::filter( $recipes = array();
$recipe_models = RecipeModel::filter(
count: 5, count: 5,
search: $search_query 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['count'] = count($recipes);
$result['result'] = $recipes; $result['result'] = $recipes;
@ -132,7 +139,9 @@ function ajax_search_ingredient()
search: $search_query search: $search_query
); );
$result['count'] = count($ingredients); $result['count'] = count($ingredients);
$result['result'] = $ingredients; $result['result'] = array_map(function($ing) {
return $ing->getAssocArr();
}, $ingredients);
return $result; return $result;
} }

View File

@ -86,6 +86,10 @@ class RecipeModel extends BaseModel
);'); );');
return $result; return $result;
} }
public function get_absolute_url()
{
return get_permalink('recipes:single', [ $this->get_id() ]);
}
public function get_price() public function get_price()
{ {
return $this->field_estimated_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"> <div class="catalog-recipe__image">
<img src="<?= $recipe->get_image_url() ?>" alt="<?= $recipe->field_title ?>"> <img src="<?= $recipe->get_image_url() ?>" alt="<?= $recipe->field_title ?>">
</div> </div>

View File

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