Skip to content
Snippets Groups Projects

explo new storm with more volatile

Merged Sebastien ANDRE requested to merge explo_new_storm into master
Compare and Show latest version
8 files
+ 88
3615
Preferences
Compare changes
Files
8
+ 0
570
<?php
/**
* Copyright (c) 2012, Agence Française Informatique (AFI). All rights reserved.
*
* BOKEH is free software; you can redistribute it and/or modify
* it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
* the Free Software Foundation.
*
* There are special exceptions to the terms and conditions of the AGPL as it
* is applied to this software (see README file).
*
* BOKEH is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
* along with BOKEH; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/** [[file:~/public_html/afi-opac3/tests/library/Class/AgendaSQYImportTest.php::class%20AgendaSQYImportTest%20extends%20Storm_Test_ModelTestCase%20{][tests]] */
Trait Trait_Agenda_SQY_ItemWrapper {
protected static $_instances = [];
protected $_wrapped_instance;
protected $_attributes;
public static function resetInstances() {
static::$_instances = [];
}
public static function newInstance($attributes) {
$id = $attributes['INDEX'];
return static::$_instances[$id]= new static($attributes);
}
public static function getInstances() {
return static::$_instances;
}
public static function getWrappedInstance($id) {
if (!isset(static::$_instances[$id]))
return null;
$wrapper = static::$_instances[$id];
return $wrapper->_wrapped_instance;
}
public static function saveInstances() {
foreach(static::$_instances as $instance) {
$instance->save();
}
}
public function __construct($attributes) {
$this->_wrapped_instance = new static::$_item_class();
$this->_attributes = $attributes;
$this->initialize();
}
public function initialize() {}
public function __call($method, $args) {
return isset(static::$_method_map[$method])
? call_user_func_array([$this->_wrapped_instance, static::$_method_map[$method]],
$args)
: null;
}
}
class Class_Agenda_SQY_EventWrapper {
use Trait_Agenda_SQY_ItemWrapper;
protected static $_item_class = 'Class_Article';
protected static $_method_map = ['setTitle' => 'setTitre',
'getTitre' => 'getTitre',
'setAbstract' => 'setDescription',
'getDescription' => 'getDescription',
'setDescription' => 'setContenu',
'getContenu' => 'getContenu',
'getEventsDebut' => 'getEventsDebut',
'getEventsFin' => 'getEventsFin',
'getLieu' => 'getLieu',
'getCategorie' => 'getCategorie',
'getTags' => 'getTags',
'getStatus' => 'getStatus',
'getDomaineIds' => 'getDomaineIds',
'getIdOrigine' => 'getIdOrigine',
'setIdOrigine' => 'setIdOrigine',
'isStatusValidated' => 'isStatusValidated'];
protected $_infos = [];
public static function mapLocationsAndCategories() {
$instances = static::getInstances();
foreach($instances as $event) {
$event
->findLibellesCategories()
->mapLocation()
->mapCategory()
->mapTags()
->mapDomaines();
}
}
public function initialize() {
$id_origine = $this->_attributes['INDEX'];
if ($article = Class_Article::findFirstBy(['id_origine' => $id_origine])) {
$this->_wrapped_instance = $article;
} else {
$this->_wrapped_instance->beValidated();
$this->_wrapped_instance->setIdOrigine($this->_attributes['INDEX']);
}
}
public function save() {
if ($this->_wrapped_instance->hasDescription())
$this->_wrapped_instance->setContenu($this->_wrapped_instance->getDescription().$this->_wrapped_instance->getContenu());
if ($this->_infos)
$this->_injectInfosInContenu();
if($this->_wrapped_instance->hasCategorie() && $this->_wrapped_instance->getCategorie()->getLibelle()!='Portail')
$this->_wrapped_instance->setContenu(
'<span class="categorie">'.
'<h2>'.
$this->_wrapped_instance->getCategorie()->getLibelle().
'</h2>'.
'</span>'.
$this->_wrapped_instance->getContenu());
return $this->_wrapped_instance->save();
}
public function _injectInfosInContenu() {
$dl = '<dl class="infos"><h2>Infos pratiques</h2>';
foreach ($this->_infos as $name => $value)
$dl .= '<dd>'.$name.'</dd><dt>'.$value.'</dt>';
$dl .= '</dl>';
$this->_wrapped_instance->setContenu($this->_wrapped_instance->getContenu().$dl);
}
public function findLibellesCategories() {
$categorie1 = explode(',', trim($this->_attributes['CATEGORY']));
$categorie2 = explode(',', trim($this->_attributes['CATEGORY2']));
$categorie3 = explode(',', trim($this->_attributes['CATEGORY3']));
$this->_categorie1_libelles = $this->getLibellesCategories($categorie1);
$this->_categorie2_libelles = $this->getLibellesCategories($categorie2);
$this->_categorie3_libelles = $this->getLibellesCategories($categorie3);
return $this;
}
public function setImage($url) {
$img = '<img style="margin:5px; float:left" src="'.$url.'" alt="'.$this->_wrapped_instance->getTitre().'"/>';
if ($this->_wrapped_instance->hasDescription())
$this->_wrapped_instance->setDescription($img.'<span class="description">'.$this->_wrapped_instance->getDescription().'</span>');
else
$this->_wrapped_instance->setContenu($img.$this->_wrapped_instance->getContenu());
}
public function addInfo($name, $value) {
$this->_infos[$name] = $value;
}
public function setDateStart($date) {
$this->_wrapped_instance->setEventsDebut(Class_Date::frToIso($date));
}
public function setDateEnd($date) {
$this->_wrapped_instance->setEventsFin(Class_Date::frToIso($date));
}
public function mapLocation() {
if (!$location_id = $this->_attributes['LOCATION'])
return $this;
$lieu = Class_Agenda_SQY_LocationWrapper::getWrappedInstance($location_id);
$lieu->addArticle($this->_wrapped_instance);
return $this;
}
public function mapCategory() {
$article_categories = $this->getIdsForCategories(Class_ArticleCategorie::findTopCategories(),
$this->_categorie1_libelles,
$this->_categorie2_libelles,
$this->_categorie3_libelles);
if ($article_categories) {
$this->_wrapped_instance->setIdCat($article_categories[0]->getId());
return $this;
}
$category_id = $this->_attributes['CATEGORY'] ? $this->_attributes['CATEGORY'] : 0;
$category_id = explode(',', $category_id)[0];
$category = Class_Agenda_SQY_CategoryWrapper::getWrappedInstance($category_id);
$category->addArticle($this->_wrapped_instance);
return $this;
}
public function mapDomaines() {
$domaines = $this->getIdsForCategories(Class_Catalogue::findTopCatalogues(),
$this->_categorie1_libelles,
$this->_categorie2_libelles,
$this->_categorie3_libelles);
$this->_wrapped_instance->setDomaines($domaines);
return $this;
}
public function mapTags() {
$tags = array_merge($this->_categorie1_libelles,
$this->_categorie2_libelles,
$this->_categorie3_libelles);
$this->_wrapped_instance->setTags(implode(';', $tags));
return $this;
}
public function getLibellesCategories($categorie_ids) {
$libelles = [];
foreach ($categorie_ids as $categorie_id) {
if ($categorie = Class_Agenda_SQY_CategoryWrapper::getWrappedInstance($categorie_id))
$libelles []= trim($categorie->getLibelle());
}
return $libelles;
}
public function getIdsForCategories($items, $categories1, $categories2, $categories3) {
$items_for_category1 = $this->getChildrenForCategoryLabels($items,
$categories1);
if (!$items_for_category2 = $this->getChildrenOfItemsForCategoryLabels($items_for_category1,
$categories2))
return $items_for_category1;
if (!$items_for_category3 = $this->getChildrenOfItemsForCategoryLabels($items_for_category2,
$categories3))
return $items_for_category2;
return $items_for_category3;
}
public function getChildrenOfItemsForCategoryLabels($domaines, $labels) {
$sous_domaines = [];
foreach($domaines as $domaine) {
$sous_domaines = array_merge($sous_domaines,
$this->getChildrenForCategoryLabels($domaine->getChildren(), $labels));
}
return $sous_domaines;
}
public function getChildrenForCategoryLabels($items, $labels) {
$labels = array_map('strtolower', $labels);
$found_children = [];
foreach($items as $item) {
if (in_array(strtolower(trim($item->getLibelle())), $labels))
$found_children []= $item;
}
return $found_children;
}
}
class Class_Agenda_SQY_CategoryWrapper {
use Trait_Agenda_SQY_ItemWrapper {
resetInstances as originalResetInstances;
}
protected static $_import_categorie;
protected static $_item_class = 'Class_ArticleCategorie';
protected static $_method_map = ['setTitle' => 'setLibelle',
'getLibelle' => 'getLibelle',
'getParentCategorie' => 'getParentCategorie'];
public static function resetInstances() {
static::originalResetInstances();
static::newInstance(['INDEX' => 0])->setTitle('Portail');
if ($import_categorie = Class_ArticleCategorie::findFirstBy(['libelle' => Class_Agenda_SQY::IMPORT_CATEGORIE_LIBELLE])) {
$sous_categories = $import_categorie->getSousCategories();
foreach($sous_categories as $cat) $cat->delete();
$articles = $import_categorie->getArticles();
foreach($articles as $article) $article->delete();
} else {
$import_categorie = (new Class_ArticleCategorie())->setLibelle(Class_Agenda_SQY::IMPORT_CATEGORIE_LIBELLE);
$import_categorie->save();
}
static::$_import_categorie = $import_categorie;
}
public function save() {
return $this->_wrapped_instance->setParentCategorie(static::$_import_categorie)
->save();
}
}
class Class_Agenda_SQY_LocationWrapper {
use Trait_Agenda_SQY_ItemWrapper;
protected static $_item_class = 'Class_Lieu';
protected static $_method_map = ['setTitle' => 'setLibelle',
'getLibelle' => 'getLibelle',
'setZip' => 'setCodePostal',
'getCodePostal' => 'getCodePostal',
'setCity' => 'setVille',
'getVille' => 'getVille',
'getPays' => 'getPays',
'setTelephone' => 'setTelephone',
'setLatitude' => 'setLatitude',
'setLongitude' => 'setLongitude',
'setAdresse' => 'setAdresse',
'setMail' => 'setMail',
'setUrl' => 'setUrl'];
public function initialize() {
$this->_wrapped_instance->setPays('France');
}
public function setZip($value) {
if (!$value) $value = '';
$this->_wrapped_instance->setCodePostal($value);
}
public function setCity($value) {
if (!$value) $value = '';
$this->_wrapped_instance->setVille($value);
}
public function save() {
if (!$this->_wrapped_instance->hasArticles())
return true;
if ($lieu = Class_Lieu::findFirstBy(['libelle' => $this->_wrapped_instance->getLibelle()])) {
$this->_wrapped_instance->setId($lieu->getId());
$lieu->updateAttributes($this->_wrapped_instance->toArray());
$this->_wrapped_instance = $lieu;
}
return $this->_wrapped_instance->save();
}
}
class Class_Agenda_SQY_OrganizerWrapper {
use Trait_Agenda_SQY_ItemWrapper;
protected static $_item_class = 'StdClass';
protected static $_method_map = [];
}
class Class_Agenda_SQY extends Class_WebService_Abstract {
const IMPORT_CATEGORIE_LIBELLE = 'import_casqy_typo3';
const URL_TYP03 = 'http://www.saint-quentin-en-yvelines.fr/index.php?id=5357';
/** @var Class_WebService_XMLParser */
protected $_xml_parser;
protected $_item_class;
protected $_item;
public function importFromURL() {
$xml = $this->httpGet(Class_AdminVar::get('URL_TYPO3'));
return $this->importFromXML($xml);
}
public function importFromXML($xml) {
$this->_xml_parser = (new Class_WebService_XMLParser())->setElementHandler($this);
$this->_xml_parser->parse($xml);
Class_Agenda_SQY_EventWrapper::mapLocationsAndCategories();
Class_Agenda_SQY_CategoryWrapper::saveInstances();
Class_Agenda_SQY_LocationWrapper::saveInstances();
Class_Agenda_SQY_EventWrapper::saveInstances();
return $this;
}
public function getCategories() {
return Class_Agenda_SQY_CategoryWrapper::getInstances();
}
public function getEvents() {
return Class_Agenda_SQY_EventWrapper::getInstances();
}
public function getLocations() {
return Class_Agenda_SQY_LocationWrapper::getInstances();
}
public function startItem($attributes) {
$this->_item = call_user_func_array([$this->_item_class, 'newInstance'],
[$attributes]);
}
public function startEvent() {
$this->_item_class = $this->wrapperClassForTag('event');
}
public function startCategory() {
$this->_item_class = $this->wrapperClassForTag('category');
}
public function startLocation() {
$this->_item_class = $this->wrapperClassForTag('location');
}
public function startOrganizer() {
$this->_item_class = $this->wrapperClassForTag('organizer');
}
public function wrapperClassForTag($tag) {
$class_name = 'Class_Agenda_SQY_'.ucfirst($tag).'Wrapper';
call_user_func([$class_name, 'resetInstances']);
return $class_name;
}
public function endTitle($data) {
$this->_item->setTitle($data);
}
public function endZip($data) {
$this->_item->setZip($data);
}
public function endCity($data) {
$this->_item->setCity($data);
}
public function endAbstract($data) {
$this->_item->setAbstract($data);
}
public function endDescription($data) {
$this->_item->setDescription($data);
}
public function endDate_Start($data) {
$this->_item->setDateStart($data);
}
public function endDate_End($data) {
$this->_item->setDateEnd($data);
}
public function endImage($data) {
$this->_item->setImage($data);
}
public function endTariffs($data) {
$this->_item->addInfo('Tarif', $data);
}
public function endAge($data) {
if ($data)
$this->_item->addInfo('Age', $data.' ans');
}
public function endPhone($data) {
$this->_item->setTelephone($data);
}
public function endEmail($data) {
$this->_item->setMail($data);
}
public function endGeo_Latitude($data) {
if ($data)
$this->_item->setLatitude($data);
}
public function endGeo_Longitude($data) {
if ($data)
$this->_item->setLongitude($data);
}
public function endUrl($data) {
$this->_item->setUrl($data);
}
public function endAddress($data) {
$this->_item->setAdresse($data);
}
public function endGeo_Map($data) {
$parts = parse_url($data);
if (!isset($parts['query']))
return;
$queries = [];
parse_str($parts['query'], $queries);
if (!isset($queries['ll']))
return;
$lat_long = explode(',', $queries['ll']);
if (count($lat_long) < 2)
return;
$this->_item->setLatitude($lat_long[0]);
$this->_item->setLongitude($lat_long[1]);
}
}
?>
\ No newline at end of file