Last active
December 14, 2015 20:09
-
-
Save nazt/5141800 to your computer and use it in GitHub Desktop.
array2xml.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function generate_xml_element( $dom, $data ) { | |
if ( empty( $data['name'] ) ) | |
return false; | |
// Create the element | |
$element_value = ( ! empty( $data['value'] ) ) ? $data['value'] : null; | |
$element = $dom->createElement( $data['name'], $element_value ); | |
// Add any attributes | |
if ( ! empty( $data['attributes'] ) && is_array( $data['attributes'] ) ) { | |
foreach ( $data['attributes'] as $attribute_key => $attribute_value ) { | |
$element->setAttribute( $attribute_key, $attribute_value ); | |
} | |
} | |
// Any other items in the data array should be child elements | |
foreach ( $data as $data_key => $child_data ) { | |
if ( ! is_numeric( $data_key ) ) | |
continue; | |
$child = generate_xml_element( $dom, $child_data ); | |
if ( $child ) | |
$element->appendChild( $child ); | |
} | |
return $element; | |
} | |
function generate_author( $author, $primary_contact ){ | |
if($primary_contact == 0){ | |
$primary_contact_boolean = true; | |
} | |
else { | |
$primary_contact_boolean = false; | |
} | |
return array( | |
'name' => 'author', | |
'attributes' => array( | |
'primary_contact' => $primary_contact_boolean, | |
), | |
array( | |
'name' => 'firstname', | |
'value' => $author['firstname'], | |
), | |
array( | |
'name' => 'lastname', | |
'value' => $author['lastname'], | |
), | |
array( | |
'name' => 'affiliation', | |
'value' => $author['affiliation'], | |
), | |
array( | |
'name' => 'country', | |
'value' => $author['country'], | |
), | |
array( | |
'name' => 'email', | |
'value' => $author['email'], | |
), | |
); | |
} | |
function generate_article( $article, $file_absolute_url ){ | |
$authors = array(); | |
// for($i = 0; $i < count($article['author']); $i++){ | |
// $authors[] = generate_article($article['author'][$i], $i); | |
// } | |
$article = array( | |
'name' => 'article', | |
array( | |
'name' => 'title', | |
'attributes' => array( | |
'locale' => 'en_US', | |
), | |
'value' => $article->dc_title, | |
), | |
array( | |
'name' => 'abstract', | |
'attributes' => array( | |
'locale' => 'en_US', | |
), | |
'value' => $article->description_en, | |
), | |
array( | |
'name' => 'date_published', | |
'value' => $article->dc_date, | |
), | |
array( | |
'name' => 'supplimental_file', | |
'attributes' => array( | |
'language' => 'en', | |
'public_id' => $article->uid, | |
'type' => 'other', | |
), | |
// array( | |
// 'name' => 'title', | |
// 'attributes' => array( | |
// 'locale' => 'en_US', | |
// ), | |
// 'value' => $article->dc_title, | |
// ), | |
array( | |
'name' => 'creator', | |
'attributes' => array( | |
'locale' => 'en_US', | |
), | |
'value' => $article->dc_creator, | |
), | |
array( | |
'name' => 'publisher', | |
'attributes' => array( | |
'locale' => 'en_US', | |
), | |
'value' => $article->dc_publisher, | |
), | |
array( | |
'name' => 'sponsor', | |
'attributes' => array( | |
'locale' => 'en_US', | |
), | |
'value' => $article->dc_contributor, | |
), | |
// array( | |
// 'name' => 'description', | |
// 'attributes' => array( | |
// 'locale' => 'en_US', | |
// ), | |
// 'value' => $article->description_en, | |
// ), | |
array( | |
'name' => 'type_other', | |
'attributes' => array( | |
'locale' => 'en_US', | |
), | |
'value' => $article->dc_type, | |
), | |
array( | |
'name' => 'subject', | |
'attributes' => array( | |
'locale' => 'en_US', | |
), | |
'value' => $article->subject_en, | |
), | |
array( | |
'name' => 'date_created', | |
'value' => $article->dc_date, | |
), | |
array( | |
'name' => 'file', | |
array( | |
'name' => 'href', | |
'attributes' => array( | |
'mime_type' => $article->dc_format, | |
'src' => $file_absolute_url, | |
), | |
), | |
), | |
) | |
); | |
$output=$article+=$authors; | |
return $output; | |
} | |
function generate_section( $section, $articles ) { | |
// $articles = array(); | |
// for( $i = 0; $i < count($section['article']); $i++ ){ | |
// $articles[] = generate_article( $section['article'][$i] ); | |
// } | |
$section = array( | |
'name' => 'section', | |
array( | |
'name' => 'title', | |
'attributes' => array( | |
'locale' => 'en_US' | |
), | |
'value' => $section->title | |
), | |
array( | |
'name' => 'abbrev', | |
'attributes' => array( | |
'locale' => 'en_US' | |
), | |
'value' => $section->abbrev | |
), | |
); | |
foreach ($articles as $article) { | |
$section[] = $article; | |
} | |
return $section; | |
} | |
function get_section_journal( $section_id ) { | |
$section = open_journal_load_section( $section_id ); | |
// $all_journal = open_journal_load_multiple( $section_id ); | |
$articles = array(); | |
for ($i=0; $i < count($section->articles); $i++) { | |
$file_last = open_journal_get_file_list($section->articles[$i]->jid, 1); | |
$file = file_load($file_last[0]->fid); | |
$file_url = file_create_url($file->uri); | |
$file_absolute_url = url($file_url, array('absolute' => TRUE)); | |
$articles[]= generate_article($section->articles[$i], $file_absolute_url); | |
} | |
return generate_section( $section, $articles ); | |
} | |
function open_journal_ojs_export() { | |
global $user; | |
$section = get_section_journal( 1 ); | |
$data = $section; | |
$doc = new DOMDocument(); | |
$child = generate_xml_element( $doc, $data ); | |
if ( $child ) | |
$doc->appendChild( $child ); | |
$doc->formatOutput = true; // Add whitespace to make easier to read XML | |
$xml = $doc->saveXML(); | |
dpm($section); | |
dpm($xml); | |
return "HELLO"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
function generate_author( $author, $primary_contact ){
if($primary_contact == 0){
$primary_contact_boolean = true;
}
else {
$primary_contact_boolean = false;
}
return array(
'name' => 'author',
'attributes' => array(
'primary_contact' => $primary_contact_boolean,
),
array(
'name' => 'firstname',
'value' => $author['firstname'],
),
array(
'name' => 'lastname',
'value' => $author['lastname'],
),
array(
'name' => 'affiliation',
'value' => $author['affiliation'],
),
array(
'name' => 'country',
'value' => $author['country'],
),
array(
'name' => 'email',
'value' => $author['email'],
),
)
}
function generate_article( $article ){
return array(
'name' => 'article',
array(
'name' => 'title',
'attributes' => array(
'locale' => 'en_US',
),
'value' => $article['title'],
),
array(
'name' => 'abstract',
'attributes' => array(
'locale' => 'en_US',
),
'value' => $article['abstract'],
),
for($i = 0; $i < count($article['author']); $i++){
generate_article($article['author'][$i], $i),
}
array(
'name' => 'pages',
'value' => $article['page'],
),
array(
'name' => 'date_published',
'value' => $article['date_published'],
),
array(
'name' => 'galley',
'attributes' => array(
'locale' => 'en_US',
),
array(
'name' => 'label',
'value' => $article['label'],
),
array(
'name' => 'file',
array(
'name' => 'href',
'attributes' => array(
'mime_type' => $article['mime_type'],
'src' => $article['src'],
),
),
),
),
}
function generate_section( $section ){
return array(
'name' => 'section',
array(
'name' => 'title',
'attributes' => array(
'locale' => 'en_US'
),
'value' => $section['title']
),
array(
'name' => 'abbrev',
'attributes' => array(
'locale' => 'en_US'
),
'value' => $section['abbrev']
),
for( $i = 0; $i < count($section['article']); $i++ ){
generate_article( $section['article'][$i] );
}
),
}