Jump to content

Compare fields in two templates


blad
 Share

Recommended Posts

Hello everyone!


I have properties and clients pages and want to make a statement showing what may be of interest to customers who are looking for.
I have two different templates with fields that are repeated in both. I want to do a relationship in my module. My code works fine but looking for a more simple and efficient way and learn for the next time I do it.

My site:

- Clientes (template=clientes)
           - cliente 1 (template=cliente) ->fields (prop_precio, prop_zona, prop_tipo_inmueble, prop_tipo_transaccion)
           - cliente 2 (template=cliente)
           ... It also has a checkbox field (cliente_busca) because not all clients seeking properties.

- Inmuebles (template=inmuebles)
            - inmueble1 (template=inmueble) -> fields (prop_precio, prop_zona, prop_tipo_inmueble, prop_tipo_transaccion)


My code:
 

	$clientes = wire('pages')->find("template=cliente, cliente_busca=1, include=all");
	$inmuebles = wire('pages')->find("template=Inmueble");
	
	foreach($clientes as $cliente) {

	
	$out .= "<ul><li>Al cliente {$cliente->title} le pueden interesar los siguientes inmuebles...</li></li>";
	
		foreach($inmuebles as $inmueble) {
		
		if ($cliente->prop_precio >= $inmueble->prop_precio && $cliente->prop_habitaciones <= $inmueble->prop_habitaciones && $cliente->prop_tipo_inmueble == $inmueble->prop_tipo_inmueble && $cliente->prop_tipo_transaccion == $inmueble->prop_tipo_transaccion && $cliente->prop_zona == $inmueble->prop_zona) {
						$out .= "<li>{$inmueble->title}</li>";
						}	
						
		}
	}; 

Any help is welcome.

Link to comment
Share on other sites

Hello,

Wow ok, there is so many ways to do what you are trying to do. I would have to understand your site better to be able to say what would be the most efficent way.

But just to give you an idea, you could create a module that hooks the matching inmuebles as a property for the cliente-pages. I have done this by taking the big if(...) you created and turning it into a selector.

public function init() {
  $this->addHookProperty("Page::inmuebles", function (HookEvent $e) {
    $page = $e->object;

    // We only execute for cliente-templates
    if($page->template->name != "cliente")
      return;

    $sanitizer = wire('sanitizer');

    // Create the selector "template"
    $selector = "template=%s";
    $selector.= ",prop_precio<=%s";
    $selector.= ",prop_habitaciones>=%s";
    $selector.= ",prop_tipo_inmueble=%s";
    $selector.= ",prop_tipo_transaccion=%s";
    $selector.= ",prop_zona=%s";

    // The values
    $values = Array(
      "Inmueble",
      $page->prop_precio,
      $page->prop_habitaciones,
      $page->prop_tipo_inmueble,
      $page->prop_tipo_transaccion,
      $page->prop_zona,
    );

    // Make the selector safe
    $selector = vsprintf(
      $selector, 
      array_map(Array($sanitizer, "selectorValue"), $values)
    );

    // Make the property hold the pages found by the selector
    $e->return = wire('pages')->find($selector);
  )); 
}

Then you can do

$clientes = wire('pages')->find("template=cliente, cliente_busca=1, include=all");
	
foreach($clientes as $cliente) {
  $out.= "...";
  foreach($cliente->inmuebles as $inmueble) {
    $out.= "...";
  }
}

This only makes sense if you always want to fetch the inmuebles in real time. There are other ways to achieve the same results too.

Of course if you don't want to create a module, then you could just make a function that returns the selector for the cliente and use that with a find().

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...