mirror of
https://github.com/RSS-Bridge/rss-bridge.git
synced 2025-04-15 03:10:55 +00:00
[PanneauPocketBridge] use dynamic_list
This commit is contained in:
parent
2fe105e9b7
commit
31d972b50d
@ -4,24 +4,31 @@ class PanneauPocketBridge extends BridgeAbstract
|
|||||||
{
|
{
|
||||||
const NAME = 'Panneau Pocket';
|
const NAME = 'Panneau Pocket';
|
||||||
const URI = 'https://app.panneaupocket.com';
|
const URI = 'https://app.panneaupocket.com';
|
||||||
const DESCRIPTION = 'Fetches the latest infos from Panneau Pocket';
|
const DESCRIPTION = 'Fetches the latest infos from Panneau Pocket.
|
||||||
|
<br><br>To have completion on cities, you must click on the button "Request temporary access to the demo server" <a target="_blank" href="https://cors-anywhere.herokuapp.com/corsdemo">here</a>
|
||||||
|
<br><br>Or use your own proxy and change the proxy value in RSS-Bridge config.ini.php';
|
||||||
const MAINTAINER = 'floviolleau';
|
const MAINTAINER = 'floviolleau';
|
||||||
const CACHE_TIMEOUT = 7200; // 2h
|
const CACHE_TIMEOUT = 0;//7200; // 2h
|
||||||
|
const PARAMETERS = [[
|
||||||
private static ?array $CITIES = null;
|
'city' => [
|
||||||
|
'name' => 'Choisir une ville',
|
||||||
public function __construct($cache, $logger)
|
'type' => 'dynamic_list',
|
||||||
{
|
'ajax_route' => 'https://app.panneaupocket.com/public-api/city',
|
||||||
parent::__construct($cache, $logger);
|
'fields_name_used_as_value' => [
|
||||||
|
'id',
|
||||||
// Assign the dynamic value to the constant in the constructor
|
'name',
|
||||||
self::$CITIES = self::getCities();
|
'postCode'
|
||||||
}
|
],
|
||||||
|
'fields_name_used_for_display' => [
|
||||||
|
'name',
|
||||||
|
'postCode'
|
||||||
|
],
|
||||||
|
]
|
||||||
|
]];
|
||||||
|
|
||||||
public function collectData()
|
public function collectData()
|
||||||
{
|
{
|
||||||
$matchedCity = array_search($this->getInput('city'), self::$CITIES);
|
$city = $this->getInput('city');
|
||||||
$city = strtolower($this->getInput('city') . '-' . $matchedCity);
|
|
||||||
$url = sprintf('https://app.panneaupocket.com/ville/%s', urlencode($city));
|
$url = sprintf('https://app.panneaupocket.com/ville/%s', urlencode($city));
|
||||||
|
|
||||||
$html = getSimpleHTMLDOM($url);
|
$html = getSimpleHTMLDOM($url);
|
||||||
@ -42,157 +49,4 @@ class PanneauPocketBridge extends BridgeAbstract
|
|||||||
$this->items[] = $item;
|
$this->items[] = $item;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* /!\ Warning
|
|
||||||
* Display all cities in a select on the front-end can be time-consuming to render
|
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
* @throws CloudFlareException
|
|
||||||
* @throws HttpException
|
|
||||||
* @throws JsonException
|
|
||||||
*/
|
|
||||||
private static function getCities(): array
|
|
||||||
{
|
|
||||||
$cities = json_decode(getContents(self::URI . '/public-api/city'), true, 512, JSON_THROW_ON_ERROR);
|
|
||||||
|
|
||||||
$formattedCities = null;
|
|
||||||
$maxTextSize = 50;
|
|
||||||
foreach ($cities as $city) {
|
|
||||||
$value = $city['name'] . ' - ' . $city['postCode'];
|
|
||||||
|
|
||||||
// reduce length for very long cities' name else style page is broken
|
|
||||||
// because of a too long value in select option.
|
|
||||||
if (strlen($city['name']) > $maxTextSize) {
|
|
||||||
// remove 11 char:
|
|
||||||
// '...' + ' - ' + postcode (5)
|
|
||||||
$lastPos = ($maxTextSize - 11) - strlen($value);
|
|
||||||
$value = substr($value, 0, strrpos($value, ' ', $lastPos)) . '... - ' . $city['postCode'];
|
|
||||||
}
|
|
||||||
$formattedCities[$value] = $city['id'];
|
|
||||||
}
|
|
||||||
|
|
||||||
return $formattedCities;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* override base method
|
|
||||||
* @return array[]
|
|
||||||
*/
|
|
||||||
public function getParameters(): array
|
|
||||||
{
|
|
||||||
return [
|
|
||||||
[
|
|
||||||
'city' => [
|
|
||||||
'name' => 'Choisir une ville',
|
|
||||||
'type' => 'list',
|
|
||||||
'values' => self::$CITIES,
|
|
||||||
]
|
|
||||||
]
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Override base method because they use static::PARAMETERS instead of getParameters
|
|
||||||
* in function setInputWithContext
|
|
||||||
*
|
|
||||||
* else $this->getInput do not return any value
|
|
||||||
*
|
|
||||||
* @param array $input
|
|
||||||
* @return void
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function setInput(array $input)
|
|
||||||
{
|
|
||||||
parent::setInput($input);
|
|
||||||
$this->setInputWithContext($input, $this->queriedContext);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Override base method because they use static::PARAMETERS instead of getParameters
|
|
||||||
*
|
|
||||||
* else $this->getInput do not return any value
|
|
||||||
*
|
|
||||||
* @param array $input
|
|
||||||
* @param $queriedContext
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function setInputWithContext(array $input, $queriedContext)
|
|
||||||
{
|
|
||||||
// Import and assign all inputs to their context
|
|
||||||
foreach ($input as $name => $value) {
|
|
||||||
foreach ($this->getParameters() as $context => $set) {
|
|
||||||
if (array_key_exists($name, $this->getParameters()[$context])) {
|
|
||||||
$this->inputs[$context][$name]['value'] = $value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Apply default values to missing data
|
|
||||||
$contexts = [$queriedContext];
|
|
||||||
if (array_key_exists('global', $this->getParameters())) {
|
|
||||||
$contexts[] = 'global';
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($contexts as $context) {
|
|
||||||
if (!isset($this->getParameters()[$context])) {
|
|
||||||
// unknown context provided by client, throw exception here? or continue?
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($this->getParameters()[$context] as $name => $properties) {
|
|
||||||
if (isset($this->inputs[$context][$name]['value'])) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$type = $properties['type'] ?? 'text';
|
|
||||||
|
|
||||||
switch ($type) {
|
|
||||||
case 'checkbox':
|
|
||||||
$this->inputs[$context][$name]['value'] = $input[$context][$name]['value'] ?? false;
|
|
||||||
break;
|
|
||||||
case 'list':
|
|
||||||
if (!isset($properties['defaultValue'])) {
|
|
||||||
$firstItem = reset($properties['values']);
|
|
||||||
if (is_array($firstItem)) {
|
|
||||||
$firstItem = reset($firstItem);
|
|
||||||
}
|
|
||||||
$this->inputs[$context][$name]['value'] = $firstItem;
|
|
||||||
} else {
|
|
||||||
$this->inputs[$context][$name]['value'] = $properties['defaultValue'];
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
if (isset($properties['defaultValue'])) {
|
|
||||||
$this->inputs[$context][$name]['value'] = $properties['defaultValue'];
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Copy global parameter values to the guessed context
|
|
||||||
if (array_key_exists('global', $this->getParameters())) {
|
|
||||||
foreach ($this->getParameters()['global'] as $name => $properties) {
|
|
||||||
if (isset($input[$name])) {
|
|
||||||
$value = $input[$name];
|
|
||||||
} else {
|
|
||||||
if ($properties['type'] ?? null === 'checkbox') {
|
|
||||||
$value = false;
|
|
||||||
} elseif (isset($properties['defaultValue'])) {
|
|
||||||
$value = $properties['defaultValue'];
|
|
||||||
} else {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$this->inputs[$queriedContext][$name]['value'] = $value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Only keep guessed context parameters values
|
|
||||||
if (isset($this->inputs[$queriedContext])) {
|
|
||||||
$this->inputs = [$queriedContext => $this->inputs[$queriedContext]];
|
|
||||||
} else {
|
|
||||||
$this->inputs = [];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user