Skip to content

Instantly share code, notes, and snippets.

@avoronkin
Created May 26, 2011 09:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avoronkin/992853 to your computer and use it in GitHub Desktop.
Save avoronkin/992853 to your computer and use it in GitHub Desktop.
MODX Evolution xml import+transliteration
<?php
$parent = isset($parent) ? $parent : 0;//родительская папка
$published = isset($published) ? $published : 0;//публиковать?
$hidemenu = isset($hidemenu) ? $hidemenu : 0;//показывать в меню?
$template = isset($template) ? $template : 0;//ид нужного шаблона
//настройки транслитерации
if (!isset ($plugin_dir) ) { $plugin_dir = 'transalias'; }
if (!isset ($plugin_path) ) { $plugin_path = $modx->config['base_path'].'assets/plugins/'.$plugin_dir; }
if (!isset ($table_name)) { $table_name = 'russian'; }
if (!isset ($char_restrict)) { $char_restrict = 'lowercase alphanumeric'; }
if (!isset ($remove_periods)) { $remove_periods = 'No'; }
if (!isset ($word_separator)) { $word_separator = 'dash'; }
if (!isset ($override_tv)) { $override_tv = ''; }
if (!class_exists('TransAlias')) {
require_once $plugin_path.'/transalias.class.php';
}
$trans = new TransAlias($modx);
$trans->loadTable($table_name, $remove_periods);
//xml файл существует?
if (!isset($file) || !file_exists($modx->config['base_path'].$file)) {
echo 'не найден xml файл: '.$modx->config['base_path'].$file;
return '';
}
$tvId = 8;
$result = $modx->db->select("value", $modx->getFullTableName('site_tmplvar_contentvalues'), "tmplvarid='" . $tvId . "'");
$ids = $modx->db->getColumn( 'value', $result );
sort($ids);
//echo '<pre>'.print_r($ids,1).'</pre>';
$doc = new DOMDocument();
$doc->load($modx->config['base_path'].$file);
if ($doc) {
$pageCount = 0;
foreach ($doc->getElementsByTagName('movie') as $node) {
$alias = $node->getElementsByTagName('name_ru')->item(0)->nodeValue;
$alias = $trans->stripAlias($alias,$char_restrict,$word_separator);
$fields = array(
'pagetitle' => $node->getElementsByTagName('name_ru')->item(0)->nodeValue,
'content' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'parent' => $parent,
'published' => $published,
'alias' => $alias,
'hidemenu' => $hidemenu,
'template' => $template,
);
$tvs = array( //tv id => tv value
'4' => $node->getElementsByTagName('id')->item(0)->nodeValue,
'5' => $node->getElementsByTagName('addtime')->item(0)->nodeValue,
'6' => $node->getElementsByTagName('image_small')->item(0)->nodeValue,
'7' => $node->getElementsByTagName('image')->item(0)->nodeValue,
'8' => $node->getElementsByTagName('image_big')->item(0)->nodeValue,
'9' => $node->getElementsByTagName('name_ru')->item(0)->nodeValue,
'10' => $node->getElementsByTagName('name_eng')->item(0)->nodeValue,
);
if(!in_array($tvs[$tvId],$ids)){//если ид фильма нет в массиве, то импортируем
createPage($fields, $tvs);
$pageCount++;
}
}
if($pageCount>0){
$modx->clearCache();
echo $pageCount.' фильмов импортировано';
//добавляем родительскому документу запись, что он папка
$table = $modx->getFullTableName( 'site_content' );
$fields = array('isfolder' => '1',);
$modx->db->update( $fields, $table, 'id = "' . $parent . '"' );
}
else{
echo 'Фильмы из этого файла уже импортированы';
}
return;
}
else{
return 'что-то не так...';
}
//запись данных в базу
function createPage($fields, $tvs) {
global $modx;
$table_name = $modx->getFullTableName('site_content');
$modx->db->insert($fields, $table_name);
$doc_id = $modx->db->getInsertId();
foreach ($tvs as $tv_id => $tv) {
$insert = array(
'tmplvarid' => $tv_id
, 'contentid' => $doc_id
, 'value' => $tv
);
$modx->db->insert($insert, $modx->getFullTableName('site_tmplvar_contentvalues'));
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment