diff --git a/content/content.importers.php b/content/content.importers.php index b552199..6a984b0 100644 --- a/content/content.importers.php +++ b/content/content.importers.php @@ -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',"

Entry ID

",array('class'=>'unique template','data-type'=>'entry-id')); + $li->appendChild(new XMLElement('header', '

Entry ID

')); + + $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'); diff --git a/lib/class.xmlimporter.php b/lib/class.xmlimporter.php index 956c404..c963db7 100644 --- a/lib/class.xmlimporter.php +++ b/lib/class.xmlimporter.php @@ -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)) { @@ -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