Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import using System ID #40

Open
wants to merge 1 commit into
base: integration
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions content/content.importers.php
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,84 @@ public function __viewEdit() {

if ($fields === false) continue;

//entry ID markup
$field_id = 'entry-id';
// $field_name = "fields[fields][-1]";
$field_name = "fields[fields][id]"; //not sure that is correct but should avoid conflicts with other fields
$field_data = null;
$template_index = null;
if (isset($this->_fields['fields'])) {
foreach ($this->_fields['fields'] as $i => $temp_data) {
if ($temp_data['field'] != $field_id) continue;
$field_data = $temp_data;
$template_index = $i;
// always force the unique to entry id if exists
$this->_fields['unique-field'] = "entry-id";
}
}
$li = new XMLElement('li',"<h4>Entry ID</h4>",array('class'=>'unique template','data-type'=>'entry-id'));
$li->appendChild(new XMLElement('header', '<h4>Entry ID</h4>'));

$input = Widget::Input("{$field_name}[field]", $field_id, 'hidden');
$li->appendChild($input);

$group = new XMLElement('div');
$group->setAttribute('class', 'two columns');

$label = Widget::Label(__('XPath Expression'));
$label->setAttribute('class', 'column');
$input = Widget::Input(
"{$field_name}[xpath]",
General::sanitize(
( isset($field_data) && isset($field_data['xpath']) )
? $field_data['xpath']
: null
)
);
$label->appendChild($input);
$group->appendChild($label);

$label = Widget::Label(__('PHP Function'));
$label->appendChild(new XMLElement('i', __('Optional')));
$label->setAttribute('class', 'column');
$input = Widget::Input(
"{$field_name}[php]",
General::sanitize(
( isset($field_data) && isset($field_data['php']) )
? $field_data['php']
: null
)
);
$label->appendChild($input);
$group->appendChild($label);

$li->appendChild($group);

$label = Widget::Label();
$label->setAttribute('class', 'meta');

$label->setValue(__('Entry ID is used to determine uniqeness.'));

$li->appendChild($label);
$section_fields->appendChild($li);

if (!is_null($field_data)){
//clone to avoid re-setting all the variables
$newLi = clone $li;
$newLi->setAttribute('class', 'unique');

//an entry ID must be unique - check only when showing in selected view
$input = Widget::Input("fields[unique-field]", $field_id, 'radio');
$input->setAttribute("checked","checked");
$input->setAttribute("style","display:none");
$newLi->appendChild($input);

//append item to the field list
$section_fields->appendChild($newLi);
}

//end entry id

// Templates
foreach ($fields as $index => $field) {
$field_id = $field->get('id');
Expand Down
42 changes: 42 additions & 0 deletions lib/class.xmlimporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,16 +251,48 @@ function handleXMLError($errno, $errstr, $errfile, $errline, $context) {
$passed = true;

foreach ($this->_entries as $index => &$current) {

$entry = EntryManager::create();
$entry->set('section_id', $options['section']);
$entry->set('author_id', is_null(Symphony::Engine()->Author()) ? '1' : Symphony::Engine()->Author()->get('id'));
$entry->set('modification_date_gmt', DateTimeObj::getGMT('Y-m-d H:i:s'));
$entry->set('modification_date', DateTimeObj::get('Y-m-d H:i:s'));

//if it has a pre-set entry id use it
if(isset($current['values']['entry-id'])){
$entry_id = $current['values']['entry-id'];
if (is_array($entry_id)){
$entry_id = current($entry_id);
}

//entry has an id - should check if it's a new entry
$existingEntry = EntryManager::fetch($entry_id, null, null, null, null, null, false, false);
if (!empty($existingEntry)){
$existingEntry = current($existingEntry);
//check that entry is in the same section else there is something wrong
if ($entry->get('section_id') != $existingEntry['section_id']){
$errorMessage = sprintf(__("An entry with id: %d already exists in a the '%s' Section, and you're trying to import it into the '%s' Section."),
$existingEntry['id'],
SectionManager::fetch($existingEntry['section_id'])->get('name'),
SectionManager::fetch($entry->get('section_id'))->get('name')
);
$current['errors'] = array($errorMessage);
$passed = false;
}
}

//set the entry id to continue
$entry->set('id', $entry_id);
}

$values = array();

// Map values:
foreach ($current['values'] as $field_id => $value) {
if ($field_id == "entry-id"){
continue;
}

$field = FieldManager::fetch($field_id);

if(is_array($value)) {
Expand Down Expand Up @@ -368,6 +400,16 @@ public function commit($status) {
$this->checkExisting($field, $entry, $index, $existing);
};

//this entry has an entry id - ignore any existing field previously set and re-set according to this id
if ($entry->get('id')){
//entry has an id - should check if it's a new entry
$existingEntry = EntryManager::fetch($entry->get('id'), null, null, null, null, null, false, false);
if (!empty($existingEntry)){
$existingEntry = current($existingEntry);
$existing[$index] = $existingEntry['id'];
}
}

// Matches an existing entry
if (!is_null($existing[$index])) {
// Update
Expand Down