libreenfete-squelettes/lef_fonctions.php

109 lines
2.6 KiB
PHP
Raw Permalink Normal View History

2021-11-26 15:05:38 +01:00
<?php
class LefPays {
private $pays_id;
private $name = "";
private $code = "";
private $regions = array();
private $total_pays = 0;
private $lieuxnationaux;
private $key_array = array();
public function __construct($json_region) {
$this->pays_id = $json_region["id"];
$this->code = $json_region["code"];
$this->name = $json_region["name"];
}
public function populate($json_regions, $evenementParRegionMap) {
$pays_key = "id_".$this->pays_id;
$this->key_array[] = $pays_key;
if (array_key_exists($pays_key, $evenementParRegionMap)) {
$evenementNationalArray = $evenementParRegionMap[$pays_key];
$total_national = count($evenementNationalArray);
$this->total_pays += $total_national;
$this->lieuxnationaux = new LefLieux($evenementNationalArray);
}
foreach($json_regions as $region) {
if ($region["region_id"] == $this->pays_id) {
$region_key = "id_".$region["id"];
$this->key_array[] = $region_key;
if (array_key_exists($region_key, $evenementParRegionMap)) {
$evenementParRegionArray = $evenementParRegionMap[$region_key];
$total_region = count($evenementParRegionArray);
$lieux = new LefLieux($evenementParRegionArray);
$region["evenements"] = $lieux->toArray();
$this->total_pays += $total_region;
$this->regions[] = $region;
}
}
}
}
public function getTotal() {
return $this->total_pays;
}
public function getKeyArray() {
return $this->key_array;
}
public function toArray() {
$evenementsnationaux;
if ($this->lieuxnationaux) {
$evenementsnationaux = $this->lieuxnationaux->toArray();
} else {
$evenementsnationaux = array();
}
return array("name" => $this->name, "code" => $this->code, "sousregions" => $this->regions, "evenementsnationaux" => $evenementsnationaux);
}
}
class LefLieux {
public $lieux = array();
public function __construct($evenementArray) {
foreach($evenementArray as $evenement) {
$city = $evenement["city"];
if (!$city) {
$city = "";
}
$lieu = null;
if (array_key_exists($city, $this->lieux)) {
$lieu = $this->lieux[$city];
} else {
$lieu = new LefLieu($city);
$this->lieux[$city] = $lieu;
}
$lieu->addEvenement($evenement);
}
ksort($this->lieux);
}
public function toArray() {
$result = array();
foreach($this->lieux as $key => $lieu) {
$result[$lieu->name] = $lieu->evenementArray;
}
return $result;
}
}
class LefLieu {
public $name = "";
public $evenementArray = array();
public function __construct($name) {
$this->name = $name;
}
public function addEvenement($evenement) {
$this->evenementArray[] = $evenement;
}
}