diff --git a/library/Class/Import/Typo3.php b/library/Class/Import/Typo3.php index b4eef36fe1c550dbf15566b02b12ed95032e4bb8..96bd8421123824b9bfbf522d8c762c5c8c1869ef 100644 --- a/library/Class/Import/Typo3.php +++ b/library/Class/Import/Typo3.php @@ -21,15 +21,15 @@ class Class_Import_Typo3 { use Trait_TimeSource; const DEFAULT_CAT_ID = 30000; - const BOKEH_IMG_URL="http://www.mediathequeouestprovence.fr/uploads/"; + const BOKEH_IMG_URL = "http://www.mediathequeouestprovence.fr/uploads/"; // const BOKEH_IMG_URL="http://web.afi-sa.net/miop-test.net/userfiles/image/uploads/"; - const BOKEH_ATTACHMENT_URL="http://www.mediathequeouestprovence.fr/fileadmin/fichiers/"; + const BOKEH_ATTACHMENT_URL = "http://www.mediathequeouestprovence.fr/fileadmin/fichiers/"; // const BOKEH_ATTACHMENT_URL="http://web.afi-sa.net/miop-test.net/userfiles/file/"; - const BOKEH_FILEADMIN_URL= "http://www.mediathequeouestprovence.fr/fileadmin/"; + const BOKEH_FILEADMIN_URL = "http://www.mediathequeouestprovence.fr/fileadmin/"; // const BOKEH_FILEADMIN_URL= "http://web.afi-sa.net/miop_test.net/userfiles/image/"; - const FOREIGN_UID_DATETIME=[867,36]; - const UID_TYPO3_CF='uid_typo3'; + const FOREIGN_UID_DATETIME = [867,36]; + const UID_TYPO3_CF = 'uid_typo3'; protected $user_mapper, @@ -59,73 +59,85 @@ class Class_Import_Typo3 { public function createCustomField() { - Class_Article::addCustomField(self::UID_TYPO3_CF, Class_CustomField_Meta::TEXT_INPUT); - Class_ArticleCategorie::addCustomField(self::UID_TYPO3_CF, Class_CustomField_Meta::TEXT_INPUT); - Class_Catalogue::addCustomField(self::UID_TYPO3_CF, Class_CustomField_Meta::TEXT_INPUT); - Class_Sitotheque::addCustomField(self::UID_TYPO3_CF, Class_CustomField_Meta::TEXT_INPUT); - Class_SitothequeCategorie::addCustomField(self::UID_TYPO3_CF, Class_CustomField_Meta::TEXT_INPUT); + $to_customize = ['Class_Article', 'Class_ArticleCategorie', + 'Class_Catalogue', 'Class_Sitotheque', 'Class_SitothequeCategorie']; + foreach($to_customize as $class_name) + call_user_func_array([$class_name, 'addCustomField'], + [self::UID_TYPO3_CF, Class_CustomField_Meta::TEXT_INPUT]); } public function setTypo3DB($t3db) { - $this->t3db=$t3db; + $this->t3db = $t3db; return $this; } - public function run($what = 'all',$last_update=null) { + public function run($what='all', $last_update=null) { $_SERVER['HTTP_HOST']='localhost'; + $logger = Class_Import_Typo3_Logs::getInstance(); + + if ('update' == $what && !$last_update) { + $logger->addErrorRow("Mode mise à jour demandé sans date de référence"); + return $logger->report(); + } + if ($what == 'update') { - if (!$last_update) - return false; list($day, $month, $year, $hour, $minute) = split('[/ : -]', $last_update); $last_update_timestamp = mktime($hour, $minute, 0, $month, $day, $year); - Class_Import_Typo3_Logs::getInstance()->addLogRow("\n\n ******** Mise a jour uniquement"); + $logger->addLogRow("\n\n ******** Mise a jour uniquement"); $this->updateCategories($last_update_timestamp); - Class_Import_Typo3_Logs::getInstance()->addLogRow("\n\n ******** Mise a jour Articles"); + + $logger->addLogRow("\n\n ******** Mise a jour Articles"); $this->updateArticles($last_update_timestamp); - Class_Import_Typo3_Logs::getInstance()->addLogRow("\n\n ******** Mise a jour Articles Pages"); + + $logger->addLogRow("\n\n ******** Mise a jour Articles Pages"); $this->updateArticlePages($last_update_timestamp); - $this->updateCategoriesForDossiersDocumentaires(); + $logger->addLogRow("\n\n ******** Détection des dossiers documentaires"); + $this->updateCategoriesForDossiersDocumentaires(); } if ($what == 'all' || $what == 'users') { - Class_Import_Typo3_Logs::getInstance()->addLogRow("\n\n ******** Importing users"); + $logger->addLogRow("\n\n ******** Importing users"); $this->import_user(); } if ($what == 'docu') { + $logger->addLogRow("\n\n ******** Détection des dossiers documentaires"); $this->updateCategoriesForDossiersDocumentaires(); } if ($what == 'all' || $what == 'articles') { - Class_Import_Typo3_Logs::getInstance()->addLogRow("\n\n ******** Importing categories"); + $logger->addLogRow("\n\n ******** Importing categories"); $this->import_categories(); - Class_Import_Typo3_Logs::getInstance()->addLogRow("\n\n ******** Importing articles pages"); + $logger->addLogRow("\n\n ******** Importing articles pages"); $this->importArticlesPages(); - Class_Import_Typo3_Logs::getInstance()->addLogRow("\n\n ******** Importing articles"); + $logger->addLogRow("\n\n ******** Importing articles"); $this->importArticles(); - Class_Import_Typo3_Logs::getInstance()->addLogRow("\n\n ******** Importing events"); + + $logger->addLogRow("\n\n ******** Importing events"); $this->importCalendar(); - Class_Import_Typo3_Logs::getInstance()->addLogRow("\n\n ******** Importing sites"); + $logger->addLogRow("\n\n ******** Importing sites"); $this->importSites(); + + $logger->addLogRow("\n\n ******** Détection des dossiers documentaires"); $this->updateCategoriesForDossiersDocumentaires(); } - return Class_Import_Typo3_Logs::getInstance()->report(); + return $logger->report(); } public function import_user() { + $this->userMap->init(); $t3users = $this->t3db->findAllUsers(); - foreach($t3users as $t3user) { $this->userMap->newUser($t3user); } @@ -133,25 +145,30 @@ class Class_Import_Typo3 { Class_Import_Typo3_Logs::getInstance()->setUsersLog(); } + public function updateCategory($category,$t3_category) { $category->setLibelle($t3_category['title']); $category->save(); } + public function updateCategories($update_date) { $this->news_categories_map = new ArticleCategoriesMap('Non classé', $this->domaine_map); $t3categories = $this->t3db->findAllNewsCatSince($update_date); foreach ($t3categories as $t3category) { - if ($category=ArticleCategoriesMap::getCategory($t3category['uid'])){ + if ($category = ArticleCategoriesMap::getCategory($t3category['uid'])) { $this->updateCategory($category,$t3category); - continue;} - $this->news_categories_map->newCategory($t3category['title'],$t3category['uid'],$t3category['parent_category']); - - $this->news_categories_map->fixParentCategory($t3category['uid'],$t3category['parent_category']); + continue; + } + $this->news_categories_map->newCategory($t3category['title'], + $t3category['uid'], + $t3category['parent_category']); + $this->news_categories_map->fixParentCategory($t3category['uid'], + $t3category['parent_category']); } } @@ -184,45 +201,51 @@ class Class_Import_Typo3 { public function importSites() { $t3news = $this->t3db->findAllSites(); - - foreach($t3news as $new) { + foreach($t3news as $new) $element = $this->traceUnknownKohaUrls( - function() use ($new){ - return $this->createSito($new); - }, - $new['uid']); - - } - + function() use ($new) { + return $this->createSito($new); + }, + $new['uid']); } + public function createSito($new) { - $date_creation = date("Y-m-d H:i:s", $new['crdate']); - $element = Class_Sitotheque::newInstance(['id_cat' => $this->sites_categories_map->find($new['category'])->getId(), - 'date_maj' => $date_creation, + $date_maj = date("Y-m-d H:i:s", $new['crdate']); + $id_cat = $this->sites_categories_map->find($new['category'])->getId(); + $url = $this->convertKohaToBokehUrl($this->format_url($new['ext_url'])); + $tags = str_replace(', ', ';', $new['tx_danpextendnews_tags']); + + $element = Class_Sitotheque::newInstance(['id_cat' => $id_cat, + 'date_maj' => $date_maj, 'titre' => $new['title'], - 'url' => $this->convertKohaToBokehUrl($this->format_url($new['ext_url'])), - 'tags' => str_replace(', ', ';', $new['tx_danpextendnews_tags']), + 'url' => $url, + 'tags' => $tags, 'description' => $new['short']]); - $element->setCustomField(self::UID_TYPO3_CF,$new['uid']); + $element->setCustomField(self::UID_TYPO3_CF, $new['uid']); if (!$element->saveWithCustomFields()) { $this->report['sitotheque_errors']++; - Class_Import_Typo3_Logs::getInstance()->addErrorRow("site with uid : " . $new['uid'] . ' (' . $new['title'] . ') - ' . implode(', ', $element->getErrors())); + Class_Import_Typo3_Logs::getInstance() + ->addErrorRow("site with uid : " . $new['uid'] . ' (' . $new['title'] . ') - ' . implode(', ', $element->getErrors())); return $element; } - $foreign_uids = $this->t3db->findAllForeignUidForNewsCat($new['uid']); $this->sites_categories_map->setDomainsFor($element, $foreign_uids); - if (in_array(43, array_map(function($row){return $row['uid_foreign'];}, $foreign_uids))) { + if ($this->isInTop5($foreign_uids)) $this->putSiteInTop5($element, $new); - } + return $element; } + protected function isInTop5($foreign_uids) { + return in_array(43, array_map(function($item) { return $item['uid_foreign']; }, + $foreign_uids)); + } + public function putSiteInTop5($element, $t3record) { $parts = $element->getCategorie()->getPathParts(); @@ -238,14 +261,14 @@ class Class_Import_Typo3 { } - public function manipulate_body($body,$image='') { + public function manipulate_body($body, $image='') { $body = $this->wrapWithHtml($body); $body = $this->addImage($image).$body; $body = $this->translateLinksToAnchor($body); $body = $this->fixImageLinks($body); $body = $this->fixAttachmentsLinks($body); $body = $this->fixFileAdminLinks($body); - return str_replace(["\r\n", "\n\r","\n","\r"],"<br />",$body); + return str_replace(["\r\n", "\n\r", "\n","\r"], "<br />", $body); } @@ -271,6 +294,7 @@ class Class_Import_Typo3 { return $this->replaceUserfilesLinkPath($body,'fileadmin\/user_upload\/',self::BOKEH_FILEADMIN_URL); } + protected function replaceUserfilesLinkPath($body,$source, $dest) { $regs= '/(< *(a|img)[^>]*(href|src) *= *["\'])(http:\/\/[^"\']*\/)?'.$source.'([^"\']*)/i'; return preg_replace($regs, '$1'.$dest.'$5', $body); @@ -278,11 +302,14 @@ class Class_Import_Typo3 { protected function translateLinksToAnchor($content) { - while((false !== ($start = strpos($content, '<link '))) && (false !== ($end = strpos($content, '</link>')))) { + while((false !== ($start = strpos($content, '<link '))) + && (false !== ($end = strpos($content, '</link>')))) { if(false === ($link = substr($content, $start, $end - $start + 7))) return $content; + $content = str_replace($link, $this->linkToAnchor($link), $content); } + return $content; } @@ -307,8 +334,9 @@ class Class_Import_Typo3 { return '<a href="' . $url . '">' . $this->getContentFromLink($link) . '</a>'; } + protected function convertKohaToBokehUrl($url) { - if (false === strpos($url,'http://koha.mediathequeouestprovence.fr/')) + if (false === strpos($url, 'http://koha.mediathequeouestprovence.fr/')) return $url; if (preg_match('/biblionumber=(\d+)$/', $url, $matches)) @@ -325,6 +353,7 @@ class Class_Import_Typo3 { return $url; } + protected function urlSearchAuthors($url, $search_expression) { return '/miop-test.net/recherche/simple/rech_auteurs/'.$search_expression; } @@ -334,6 +363,7 @@ class Class_Import_Typo3 { return '/miop-test.net/recherche/simple/expressionRecherche/'.$search_expression; } + protected function urlViewNotice($url, $id_origine) { if (!$exemplaire = Class_Exemplaire::findFirstBy(['id_origine' => $id_origine])) return $url; @@ -350,117 +380,123 @@ class Class_Import_Typo3 { protected function wrapWithHtml($content) { - - if('<' === substr($content,0,1)) - return $content; - - return '<p>' . $content . '</p>'; + return ('<' === substr($content,0,1)) ? + $content : '<p>' . $content . '</p>'; } + public function getCatDossierDoc() { return $this->createCategory("Nos dossiers documentaires"); } public function updateCategoriesForDossiersDocumentaires() { - $t3_pages = $this->t3db->findAllPagesWithPid(309); - foreach ($t3_pages as $page) { - $category = $this->createCategory($page['title'],$this->getCatDossierDoc()->getId()); - if (!$category) - continue; + $dossiers_doc_id = $this->getCatDossierDoc()->getId(); + foreach ($this->t3db->findAllPagesWithPid(309) as $page) + $this->_addPageInDossiersDocumentaires($page, $dossiers_doc_id); + } - $contents = $this->t3db->findAllContents($page['uid']); - foreach ($contents as $content) { - $article = $this->_getArticle($content['uid']); - if (!$article) { - Class_Import_Typo3_Logs::getInstance()->addErrorRow('Article with uid: ' . $content['uid'] . ' missing'); - continue; - } - $article->setCategorie($category); - $article->save(); - } + + protected function _addPageInDossiersDocumentaires($page, $dossiers_doc_id) { + if (!$category = $this->createCategory($page['title'], $dossiers_doc_id)) + return; + + foreach ($this->t3db->findAllContents($page['uid']) as $content) + $this->_addContentInDossiersDocumentaires($content, $category); + } + + + protected function _addContentInDossiersDocumentaires($content, $category) { + if (!$article = $this->_getArticle($content['uid'])) { + Class_Import_Typo3_Logs::getInstance() + ->addErrorRow('Article with uid: ' . $content['uid'] . ' missing'); + return; } + $article->setCategorie($category) + ->save(); } + protected function _getArticle($value) { return Class_Article::findFirstByCustomFieldValue(self::UID_TYPO3_CF, $value); } - public function updateArticles($last_import_date) { - $t3news = $this->t3db->findAllArticlesSince($last_import_date); - foreach($t3news as $new) { - if ($new['deleted']) { - if ($article = $this->_getArticle($new['uid'])) - $article->deleteWithCustomFields(); - continue; - } + public function updateArticles($last_import_date) { + foreach($this->t3db->findAllArticlesSince($last_import_date) as $new) + $this->_updateArticle($new); - $id_cat=0; - if ($cat=ArticleCategoriesMap::getCategoryOrDefaultCategory($new['category'])) - $id_cat=$cat->getId(); + Class_Import_Typo3_Logs::getInstance()->setArticlesLog(); + return $this; + } - $block = function() use ($new, $id_cat){ - if ($article = $this->_getArticle($new['uid'])) - return $this->updateArticle($article,$new,$id_cat); - return $this->createArticle($new, $id_cat); - }; - $element = $this->traceUnknownKohaUrls($block, $new['uid']); + protected function _updateArticle($new) { + if ($new['deleted']) { + if ($article = $this->_getArticle($new['uid'])) + $article->deleteWithCustomFields(); + return; + } + $id_cat = ArticleCategoriesMap::getCategoryOrDefaultCategory($new['category']) + ->getId(); -// $this->news_categories_map->setDomainsFor($element, - // $this->t3db->findAllForeignUidForNewsCat($new['uid'])); - } + $block = function() use ($new, $id_cat){ + if ($article = $this->_getArticle($new['uid'])) + return $this->updateArticle($article, $new, $id_cat); + return $this->createArticle($new, $id_cat); + }; - Class_Import_Typo3_Logs::getInstance()->setArticlesLog(); - return $this; + $this->traceUnknownKohaUrls($block, $new['uid']); } public function importArticles() { - $t3news = $this->t3db->findAllArticles(); - foreach($t3news as $new) { - $id_cat = $this->news_categories_map->find($new['category'])->getId(); - $element = $this->traceUnknownKohaUrls( - function() use ($new, $id_cat){ - return $this->createArticle($new, $id_cat); - }, - $new['uid']); - - - $this->news_categories_map->setDomainsFor($element, - $this->t3db->findAllForeignUidForNewsCat($new['uid'])); - } + foreach($this->t3db->findAllArticles() as $new) + $this->_importArticle($new); Class_Import_Typo3_Logs::getInstance()->setArticlesLog(); return $this; } - public function setMiopDateEvent($article,$data) { - if ($this->existCategoriesAsForeign($data,self::FOREIGN_UID_DATETIME)) { + protected function _importArticle($new) { + $id_cat = $this->news_categories_map->find($new['category'])->getId(); + $element = $this->traceUnknownKohaUrls( + function() use ($new, $id_cat){ + return $this->createArticle($new, $id_cat); + }, + $new['uid']); + + + $this->news_categories_map->setDomainsFor($element, + $this->t3db->findAllForeignUidForNewsCat($new['uid'])); + } + + + public function setMiopDateEvent($article, $data) { + if ($this->existCategoriesAsForeign($data, self::FOREIGN_UID_DATETIME)) { $converted_date = $this->formatDate($data['datetime']); $article->setEventsDebut($converted_date)->setEventsFin($converted_date); } - $this->changeCategory($article,$data,$this->getCatDossierDoc()->getId()); + $this->changeCategory($article, $data, $this->getCatDossierDoc()->getId()); return $article; } - public function existCategoriesAsForeign($data,$array_catid) { - foreach ($this->t3db->findAllForeignUidForNewsCat($data['uid']) as $domain) { - if (in_array($domain['uid_foreign'],$array_catid)) + public function existCategoriesAsForeign($data, $array_catid) { + foreach ($this->t3db->findAllForeignUidForNewsCat($data['uid']) as $domain) + if (in_array($domain['uid_foreign'], $array_catid)) return true; - } + return false; } public function changeCategory($article,$data,$id_cat) { - if ($this->existCategoriesAsForeign($data,[$id_cat])) + if ($this->existCategoriesAsForeign($data, [$id_cat])) $article->setIdCat($this->news_categories_map->find($id_cat)->getId()); } @@ -469,43 +505,50 @@ class Class_Import_Typo3 { return $date_str ? date("Y-m-d H:i", $date_str) : ''; } - public function updateArticle($article,$new,$id_cat) { + public function updateArticle($article,$new,$id_cat) { $date_creation = date("Y-m-d H:i:s", $new['crdate']); $debut = $this->formatDate($new['starttime']); $fin = $this->formatDate($new['endtime']); + $description = $new['short'] ? + $this->manipulate_body($new['short'], $new['image']) : + $this->addImage($new['image']); + + $contenu = $new['bodytext'] ? + $this->manipulate_body($new['bodytext'], $new['image']) : + ' '; + + $tags = str_replace(', ', ';', $new['tx_danpextendnews_tags']); $article->updateAttributes(['date_creation' => $date_creation, 'debut' => $debut, 'fin' => $new['hidden'] ? null : $fin, 'id_user' => $this->userMap->find($new['cruser_id']), - 'description' => $new['short']? $this->manipulate_body($new['short'],$new['image']): $this->addImage($new['image']), - 'contenu' => $new['bodytext'] ? $this->manipulate_body($new['bodytext'],$new['image']) : ' ', + 'description' => $description, + 'contenu' => $contenu, 'id_cat' => $id_cat, 'status' => Class_Article::STATUS_VALIDATED, - 'tags' => str_replace(', ', ';', $new['tx_danpextendnews_tags']), + 'tags' => $tags, 'titre' => $new['title']]); + $article = $this->setMiopDateEvent($article,$new); $article->setCustomField('uid_typo3',$new['uid']); - if (!$article->saveWithCustomFields()) { - Class_Import_Typo3_Logs::getInstance()->incrementArticleRejected($article, [$new['uid'], - $new['title']]); + Class_Import_Typo3_Logs::getInstance() + ->incrementArticleRejected($article, [$new['uid'], $new['title']]); return $article; } - Class_Import_Typo3_Logs::getInstance()->incrementArticlesSaved(); return $article; } public function createArticle($new,$id_cat) { - $article = new Class_Article(); - $article = $this->updateArticle($article,$new,$id_cat); - return $article; + return $this->updateArticle(new Class_Article(), $new, $id_cat); } + private function typo_event_to_date($date, $time) { $year = substr($date, 0, 4); $month = substr($date, 4, 2); @@ -525,13 +568,12 @@ class Class_Import_Typo3 { public function importCalendar() { - $cal_news = $this->t3db->findAllCalendarEvents(); - foreach($cal_news as $new) { - $this->traceUnknownKohaUrls( function() use ($new) { - return $this->createCalendar($new); - }, - $new['uid']); - } + foreach($this->t3db->findAllCalendarEvents() as $new) + $this->traceUnknownKohaUrls( + function() use ($new) { + return $this->createCalendar($new); + }, + $new['uid']); return $this; } @@ -543,6 +585,11 @@ class Class_Import_Typo3 { $event_debut = $this->typo_event_to_date($new['start_date'], $new['start_time']); $event_fin = $this->typo_event_to_date($new['end_date'], $new['end_time']); + $contenu = $this->manipulate_body($new['description'] ? + $new['description'] : $new['title'], + $new['image']); + + $id_cat = $this->cal_categories_map->find($new['category_id'])->getId(); $element = Class_Article::newInstance(['date_creation' => $date_creation, 'debut' => $debut, @@ -552,8 +599,8 @@ class Class_Import_Typo3 { 'id_user' => $this->userMap->find($new['cruser_id']), 'all_day' => $new['allday'], 'description' => '', - 'contenu' => $this->manipulate_body($new['description'] ? $new['description'] : $new['title'],$new['image']), - 'id_cat' => $this->cal_categories_map->find($new['category_id'])->getId(), + 'contenu' => $contenu, + 'id_cat' => $id_cat, 'status' => Class_Article::STATUS_VALIDATED, 'tags' => '', 'titre' => $new['title']]); @@ -561,34 +608,38 @@ class Class_Import_Typo3 { $element->setCustomField(self::UID_TYPO3_CF,$new['uid']); if (!$element->saveWithCustomFields()) { $this->report['calendar_errors']++; - Class_Import_Typo3_Logs::getInstance()->addErrorRow('calendar with uid: ' . $new['uid'] . '(' . $new['title'] . ') - ' . implode(', ', $element->getErrors())); + Class_Import_Typo3_Logs::getInstance() + ->addErrorRow('calendar with uid: ' . $new['uid'] + . '(' . $new['title'] . ') - ' + . implode(', ', $element->getErrors())); return $element; } $element->setDateCreation($date_creation)->save(); - $this->report['calendar_created']++; - - $this->cal_categories_map->setDomainsFor($element, - $this->t3db->findAllForeignUidForCalendarEventCategory($new['uid'])); + $this->cal_categories_map + ->setDomainsFor($element, + $this->t3db->findAllForeignUidForCalendarEventCategory($new['uid'])); return $element; - } - public function createCategory($category_name,$id_cat_mere=0) { - if ($category=Class_ArticleCategorie::findFirstBy(['libelle' => $category_name])) + public function createCategory($category_name, $id_cat_mere=0) { + if ($category = Class_ArticleCategorie::findFirstBy(['libelle' => $category_name])) return $category; - $category=Class_ArticleCategorie::newInstance(['libelle' => $category_name,'id_cat_mere' =>$id_cat_mere]); + + $category = Class_ArticleCategorie::newInstance(['libelle' => $category_name,'id_cat_mere' =>$id_cat_mere]); + if ($category->save()) return $category; + return null; } + public function updateArticlePages($update_date) { - $rows = $this->t3db->findAllContentsSince($update_date); $pages_cat = Class_ArticleCategorie::findFirstBy(['libelle' => 'Pages fixes']); - foreach($rows as $new) { + foreach($this->t3db->findAllContentsSince($update_date) as $new) { $this->traceUnknownKohaUrls( function() use ($new, $pages_cat){ if ($article = $this->_getArticle($new['uid'])) @@ -596,9 +647,9 @@ class Class_Import_Typo3 { return $this->createArticlePage($new, $pages_cat); }, $new['uid']); - $this->report['pages_created']++; } + return $this; } @@ -624,13 +675,16 @@ class Class_Import_Typo3 { $date_creation = date("Y-m-d H:i:s", $new['crdate']); $debut = $this->formatDate($new['starttime']); $fin = $this->formatDate($new['endtime']); + $contenu = $new['bodytext'] ? + $this->manipulate_body($new['bodytext'], $new['image']) + :''; $element = $article->updateAttributes(['date_creation' => $date_creation, 'debut' => $debut, 'fin' => $new['hidden'] ? null : $fin, 'id_user' => $this->userMap->find($new['cruser_id']), 'description' => '', - 'contenu' => $new['bodytext'] ? $this->manipulate_body($new['bodytext'],$new['image']) :'', + 'contenu' => $contenu, 'id_cat' => $pages_cat->getId(), 'status' => Class_Article::STATUS_VALIDATED, 'tags' => '', @@ -639,26 +693,27 @@ class Class_Import_Typo3 { $element->setCustomField(self::UID_TYPO3_CF,$new['uid']); if (!$element->saveWithCustomFields()) { $this->report['pages_errors']++; - Class_Import_Typo3_Logs::getInstance()->addErrorRow('pages with uid: ' . $new['uid'] . ' (' . $new['title'] . ') - ' . implode(', ', $element->getErrors())); + Class_Import_Typo3_Logs::getInstance() + ->addErrorRow('pages with uid: ' . $new['uid'] + . ' (' . $new['title'] . ') - ' + . implode(', ', $element->getErrors())); } - return $element; + return $element; } + public function createArticlePage($new, $pages_cat) { - $article=new Class_Article(); - return $this->updateArticlePage($article,$new,$pages_cat); + return $this->updateArticlePage(new Class_Article(), $new, $pages_cat); } - public function traceUnknownKohaUrls($closure,$uid) { + public function traceUnknownKohaUrls($closure, $uid) { $this->unknown_koha_urls = []; $element = $closure(); - foreach($this->unknown_koha_urls as $url) { - Class_Import_Typo3_Logs::getInstance()->addUnknownUrl($element, - $url, - $uid); - } + foreach($this->unknown_koha_urls as $url) + Class_Import_Typo3_Logs::getInstance()->addUnknownUrl($element, $url, $uid); + return $element; } @@ -667,13 +722,12 @@ class Class_Import_Typo3 { public function format_url($url) { if (!$url = explode(' ', $url)[0]) return ''; + $url = preg_replace(['/^http:\/\//', '/^\/\//'], '', trim(strtolower($url))); return (0 !== strpos('http', $url)) ? 'http://'.$url : $url; } - - public function reset_auto_increment() { Zend_Registry::get('sql')->execute('alter table cms_article AUTO_INCREMENT=1'); Zend_Registry::get('sql')->execute('alter table cms_categorie AUTO_INCREMENT=1'); @@ -685,67 +739,65 @@ class Class_Import_Typo3 { } -class UserMap { - +class UserMap { protected $map = []; - protected $saved = 0; - protected $updated = 0; - protected $errors = []; - protected $t3users = 0; - - public function __construct() { + public function init() { $where = 'ROLE_LEVEL < ' . ZendAfi_Acl_AdminControllerRoles::ADMIN_PORTAIL; // exclude ADMIN. $where .= ' AND ROLE_LEVEL <> ' . ZendAfi_Acl_AdminControllerRoles::ABONNE_SIGB; // exclude SIGB members. - Class_Users::deleteBy(['where' => $where]); } - public function updateUser($data, &$user) { + public function updateUser($data, $user) { $user->setNom($data['realName']); $user->setMail($data['email']); } public function newUser($data) { - Class_Import_Typo3_Logs::getInstance()->incrementT3Users(); - $user; - $updated = 0; - if ($user = Class_Users::findFirstBy(['login'=> $data['username']])) { - $updated = 1; - $this->updateUser($data, $user); - } - else { - $user = Class_Users::newInstance(['nom' => $data['realName'], - 'mail' => $data['email'], - 'login' => $data['username'], - 'password' => 'achanger']); - } + $logs = Class_Import_Typo3_Logs::getInstance(); + $logs->incrementT3Users(); + + $user = $this->_findOrCreate($data['username']); + $updated = !$user->isNew(); + if ($user = Class_Users::findFirstBy(['login' => $data['username']])) { + $updated = true; + $this->updateUser($data, $user); + + } else { + $user = Class_Users::newInstance(['nom' => $data['realName'], + 'mail' => $data['email'], + 'login' => $data['username'], + 'password' => 'achanger']); + } - if ($data['admin'] == '1') { - $user->beAdminPortail(); - } - else { - $user->changeRoleTo(ZendAfi_Acl_AdminControllerRoles::MODO_PORTAIL); - } + ($data['admin'] == '1') ? + $user->beAdminPortail() : + $user->changeRoleTo(ZendAfi_Acl_AdminControllerRoles::MODO_PORTAIL); - if (!$user->save()) - return Class_Import_Typo3_Logs::getInstance()->incrementRejected($user, $data); + if (!$user->save()) { + $logs->incrementRejected($user, $data); + return; + } + + $updated + ? $logs->incrementUpdated() + : $logs->incrementSaved(); + + $this->map[$data['uid']] = $user->getId(); + } - $updated - ? Class_Import_Typo3_Logs::getInstance()->incrementUpdated() - : Class_Import_Typo3_Logs::getInstance()->incrementSaved(); - $this->map[$data['uid']] = $user->getId(); + protected function _findOrCreate($login) { + return ($user = Class_Users::findFirstBy(['login' => $login])) ? + $user : Class_Users::newInstance(['login' => $login]); } public function find($uid) { - return isset($this->map[$uid]) - ? $this->map[$uid] - : 0; + return isset($this->map[$uid]) ? $this->map[$uid] : 0; } } @@ -781,9 +833,11 @@ abstract class CategoriesMap { $cat = $this->map[$t3id]; return $cat->getId(); } + return 0; } + public function newCategory($title, $t3uid, $t3pid) { $category = $this->_buildNewCategory($title, $this->getParentCatId($t3pid)); $category->setCustomField(Class_Import_Typo3::UID_TYPO3_CF,$t3uid); @@ -806,35 +860,36 @@ abstract class CategoriesMap { } - public static function getOrCreateDefaultCategory() { - if ($category=Class_ArticleCategorie::findFirstBy(['libelle' => 'Non classé'])) + public static function getOrCreateDefaultCategory() { + if ($category = Class_ArticleCategorie::findFirstBy(['libelle' => 'Non classé'])) return $category; - $cat = Class_ArticleCategorie::newInstance(['libelle' => 'Non classé', - 'id_cat_mere' => 0]); + + $cat = Class_ArticleCategorie::newInstance(['libelle' => 'Non classé', + 'id_cat_mere' => 0]); $cat->save(); return $cat; } + public static function getCategory($uid) { - return Class_ArticleCategorie::findFirstByCustomFieldValue(Class_Import_Typo3::UID_TYPO3_CF,$uid); + return Class_ArticleCategorie::findFirstByCustomFieldValue(Class_Import_Typo3::UID_TYPO3_CF, $uid); } + public static function getCategoryOrDefaultCategory($uid) { - if ($category=self::getCategory($uid)) - return $category; - return self::getOrCreateDefaultCategory(); + return ($category = self::getCategory($uid)) ? + $category : self::getOrCreateDefaultCategory(); } public function fixParentCategory($t3uid, $t3pid) { $category = self::getCategory($t3uid); - if ( - $category->getId() !== $this->default_cat->getId() - ) { - $category->setParentCategorie(self::getCategory($t3pid)) - ->save(); - } + if ($category->getId() == $this->default_cat->getId()) + return; + + $category->setParentCategorie(self::getCategory($t3pid)) + ->save(); } @@ -850,7 +905,7 @@ abstract class CategoriesMap { public function find($t3id) { - return isset($this->map[$t3id]) + return isset($this->map[$t3id]) ? $this->map[$t3id] : $this->default_cat; } @@ -860,12 +915,12 @@ abstract class CategoriesMap { $domains = []; foreach ($t3_domain_ids as $t3_domain) { $category = $this->find($t3_domain['uid_foreign']); - $domains[] = $this->domaine_map->findOrCreateDomaine($category,$t3_domain['uid_foreign']); + $domains[] = $this->domaine_map + ->findOrCreateDomaine($category,$t3_domain['uid_foreign']); } - if (isset($category)) { + if (isset($category)) $model->setCategorie($category); - } $model->setDomaines($domains)->save(); } @@ -876,7 +931,7 @@ abstract class CategoriesMap { class DomaineMap { protected $map = []; - public function findOrCreateDomaine($category,$uid=0) { + public function findOrCreateDomaine($category, $uid=0) { if (!$category) return null; @@ -891,7 +946,8 @@ class DomaineMap { 'parent_id' => $parent_domaine ? $parent_domaine->getId() : 0]); if (!$uid) - $catalogue->setCustomField(Class_Import_Typo3::UID_TYPO3_CF,$uid); + $catalogue->setCustomField(Class_Import_Typo3::UID_TYPO3_CF, $uid); + if (!$catalogue->saveWithCustomFields()) { Class_Import_Typo3_Logs::getInstance()->incrementDomainsRejected($catalogue); return; @@ -903,19 +959,16 @@ class DomaineMap { } -class ArticleCategoriesMap extends CategoriesMap { +class ArticleCategoriesMap extends CategoriesMap { protected function _buildNewCategory($libelle, $parent_id=0) { - $cat = Class_ArticleCategorie::newInstance(['libelle' => $libelle, - 'id_cat_mere' => $parent_id]); - /* if ($double_cat = $this->findByPath($cat->getPath)) */ - /* return $double_cat; */ - /* $this->path_map [] = $cat; */ - return $cat; + return Class_ArticleCategorie::newInstance(['libelle' => $libelle, + 'id_cat_mere' => $parent_id]); } protected function addError($category, $title, $t3uid, $t3pid) { - return Class_Import_Typo3_Logs::getInstance()->incrementArticleCategoriesRejected($category, $title, $t3uid, $t3pid); + return Class_Import_Typo3_Logs::getInstance() + ->incrementArticleCategoriesRejected($category, $title, $t3uid, $t3pid); } @@ -925,7 +978,7 @@ class ArticleCategoriesMap extends CategoriesMap { } -class SiteCategoriesMap extends CategoriesMap { +class SiteCategoriesMap extends CategoriesMap { protected function _buildNewCategory($libelle, $parent_id=0) { return Class_SitothequeCategorie::newInstance(['libelle' => $libelle, 'id_cat_mere' => $parent_id]); @@ -933,7 +986,8 @@ class SiteCategoriesMap extends CategoriesMap { protected function addError($category, $title, $t3uid, $t3pid) { - return Class_Import_Typo3_Logs::getInstance()->incrementSitoCategoriesRejected($category, $title, $t3uid, $t3pid); + return Class_Import_Typo3_Logs::getInstance() + ->incrementSitoCategoriesRejected($category, $title, $t3uid, $t3pid); } @@ -942,7 +996,6 @@ class SiteCategoriesMap extends CategoriesMap { } - public function findOrCreateByPath($path) { if ($path == Trait_TreeNode::$PATH_SEPARATOR) return null; @@ -950,14 +1003,20 @@ class SiteCategoriesMap extends CategoriesMap { $parts = array_values(array_filter(explode(Trait_TreeNode::$PATH_SEPARATOR, $path))); $category = null; - foreach($parts as $part) { - if (!$sub_category = Class_SitothequeCategorie::findFirstBy(['libelle' => $part, - 'id_cat_mere' => $category ? $category->getId() : 0])) { - $sub_category = Class_SitothequeCategorie::newInstance(['libelle' => $part, - 'id_cat_mere' => $category ? $category->getId() : 0]); - $sub_category->save(); - } - $category = $sub_category; + foreach($parts as $part) + $category = $this->_findOrCreateUnder($part, $category); + + return $category; + } + + + protected function _findOrCreateUnder($label, $parent) { + $attribs = ['libelle' => $label, + 'id_cat_mere' => $parent ? $parent->getId() : 0]; + + if (!$category = Class_SitothequeCategorie::findFirstBy($attribs)) { + $category = Class_SitothequeCategorie::newInstance($attribs); + $category->save(); } return $category; @@ -980,8 +1039,6 @@ class Typo3DB { $t3db, $pages_titles; - - public function __construct() { $this->t3db = new Class_Systeme_Sql('','','',''); $this @@ -1000,11 +1057,10 @@ class Typo3DB { if (isset($this->pages_titles[$uid])) return $this->pages_titles[$uid]; - if (!$row = $this->t3db->fetchEnreg('select title from pages where uid='.$uid)) { - return $this->pages_titles[$uid] = ''; - } + $title = ($row = $this->t3db->fetchEnreg('select title from pages where uid=' . $uid)) ? + $row['title'] : ''; - return $this->pages_titles[$uid] = $row['title']; + return $this->pages_titles[$uid] = $title; } @@ -1017,6 +1073,7 @@ class Typo3DB { return $this->t3db->fetchAll('select * from be_users where uid > 1'); } + public function findAllNewsCat() { return $this->t3db->fetchAll('select * from tt_news_cat where deleted=0 order by uid ASC'); } @@ -1026,20 +1083,22 @@ class Typo3DB { return $this->t3db->fetchAll('select * from tt_news_cat where deleted=0 and tstamp >='.$update_date.' order by uid ASC'); } + public function findAllEventCat() { return $this->t3db->fetchAll("select * from tx_cal_category where deleted=0 order by uid"); } + public function findAllEventCatSince($update_date) { return $this->t3db->fetchAll("select * from tx_cal_category where deleted=0 and tstamp >=".$update_date." order by uid"); } + public function findAllSites() { return $this->t3db->fetchAll("select * from tt_news where deleted=0 and hidden=0 and ext_url>'' order by uid ASC"); } - public function findAllArticlesSince($last_import_date) { return $this->t3db->fetchAll("select * from tt_news where hidden=0 and ext_url='' and tstamp >= ".$last_import_date." order by uid ASC"); } @@ -1049,10 +1108,12 @@ class Typo3DB { return $this->t3db->fetchAll("select * from tt_news where deleted=0 and hidden=0 and ext_url='' order by uid ASC"); } + public function findAllForeignUidForNewsCat($uid) { return $this->t3db->fetchAll("select distinct uid_foreign from tt_news_cat_mm where uid_local=". $uid ." order by sorting"); } + public function findAllForeignUidForCalendarEventCategory($uid) { return $this->t3db->fetchAll("select distinct uid_foreign from tx_cal_event_category_mm where uid_local=" . $uid . " order by sorting"); } @@ -1062,17 +1123,18 @@ class Typo3DB { return $this->t3db->fetchAll("select * from tx_cal_event where deleted=0 and hidden=0 order by uid ASC"); } + public function findAllContents($page_id=false) { $pid_sql=''; if ($page_id) $pid_sql='and pid='.$page_id; + return $this->t3db->fetchAll("select * from tt_content where deleted=0 and hidden=0 and header>'' and bodytext>'' and (ctype='text' or ctype='textpic') ".$pid_sql." order by uid ASC"); } public function findAllContentsSince($update_date) { - $pid_sql=''; - return $this->t3db->fetchAll("select * from tt_content where deleted=0 and tstamp>=".$update_date." and hidden=0 and header>'' and bodytext>'' and (ctype='text' or ctype='textpic') ".$pid_sql." order by uid ASC"); + return $this->t3db->fetchAll("select * from tt_content where deleted=0 and tstamp>=".$update_date." and hidden=0 and header>'' and bodytext>'' and (ctype='text' or ctype='textpic') order by uid ASC"); } }