diff --git a/assets/xmlimporter.js b/assets/xmlimporter.js index c373832..89173d6 100644 --- a/assets/xmlimporter.js +++ b/assets/xmlimporter.js @@ -18,4 +18,38 @@ }).trigger('change.xmlimporter'); }); + $(document).on('click','.throttle',function(){ + var data = { + "created":0, + "updated":0, + "skipped":0, + "failed":0, + }; + data.next = $('.next-page').data('next'); + data.nexturi = $('.next-page').data('uri'); + throttle(data,0); + }); + + function throttle(data,maxpage){ + $importdetails = $('.xmlimport-details'); + $importdetails.data('created', $importdetails.data('created') + data.created); + $importdetails.data('updated', $importdetails.data('updated') + data.updated); + $importdetails.data('skipped', $importdetails.data('skipped') + data.skipped); + $importdetails.data('failed', $importdetails.data('failed') + data.failed); + $importdetails.text( $importdetails.data('created') + ' new entries were created, ' + + $importdetails.data('updated') +' updated, ' + + $importdetails.data('skipped') +' skipped and ' + + $importdetails.data('failed') +' failed' + ); + + if (data.next != null && (data.next <= maxpage || maxpage == 0)){ + $importdetails.prev().text('Currently Importing Page ' + data.next); + $.getJSON( data.nexturi + '&ajax=1' , function( data ) { + throttle(data,maxpage); + }); + } else { + $importdetails.prev().text('Import Complete'); + } + } + })(jQuery); diff --git a/content/content.importers.php b/content/content.importers.php index b552199..14a2699 100644 --- a/content/content.importers.php +++ b/content/content.importers.php @@ -276,7 +276,13 @@ public function __viewRun() { $importer_result['updated'], $importer_result['skipped'], $importer_result['failed'] - )) + )), array( + 'data-created' => $importer_result['created'], + 'data-updated' => $importer_result['updated'], + 'data-skipped' => $importer_result['skipped'], + 'data-failed' => $importer_result['failed'], + 'class' => 'xmlimport-details', + ) )); // Import errors @@ -303,9 +309,35 @@ public function __viewRun() { $importer_result['created'], $importer_result['updated'], $importer_result['skipped'] - )) + )), array( + 'data-created' => $importer_result['created'], + 'data-updated' => $importer_result['updated'], + 'data-skipped' => $importer_result['skipped'], + 'data-failed' => $importer_result['failed'], + 'class' => 'xmlimport-details', + ) )); + $throttle = $importer->getThrottle(); + $pagination = $importer->pagination(); + if ( !empty($throttle)){ + $fieldset->appendChild(Widget::Anchor( + __('Next Page'), + $this->_uri . '/importers/run/' . $this->_context[1] . '/?' . $pagination['variable'] . '=' . $throttle['next-page'], + __('Next Page'), + 'button next-page',null,array( + 'data-next'=>$throttle['next-page'], + 'data-uri' => $this->_uri . '/importers/run/' . $this->_context[1] . '/?' . $pagination['variable'] . '=' . $throttle['next-page'] + ) + )); + $fieldset->appendChild(Widget::Anchor( + __('Throttle'), + '#throttle', + __('Throttle'), + 'button throttle' + )); + } + } $this->Form->appendChild($fieldset); @@ -323,6 +355,37 @@ public function __viewRun() { 'entries' => $entries ) ); + + $throttle = $importer->getThrottle(); + $pagination = $importer->pagination(); + if ($throttle['ajax']){ + + $failedEntries = new XMLElement( + 'div', null, array('class'=>'failed-entries') + ); + + if (!empty($failed)){ + $fieldset->appendChild(new XMLElement( + 'h3', __('Import Errors: Page %d', array( + $throttle['current-page'] + )) + )); + + $this->addFailedEntries($failedEntries, $failed); + } + + echo json_encode( array( + 'created' => $importer_result['created'], + 'updated' => $importer_result['updated'], + 'skipped' => $importer_result['skipped'], + 'failed' => $importer_result['failed'], + 'next' => $throttle['next-page'], + 'nexturi' => $this->_uri . '/importers/run/' . $this->_context[1] . '/?' . $pagination['variable'] . '=' . $throttle['next-page'], + 'errors' => $failedEntries->generate() + )); + + die; + } } } @@ -459,6 +522,18 @@ public function __actionEditNormal() { : 'no' ); + // pagination/throttling settings + + // validate has next page xpath + if (!empty($fields['pagination']['next'])){ + try { + $this->_driver->validateXPath($fields['pagination']['next'], $fields['namespaces']); + } + catch (Exception $e) { + $this->_errors['pagination']['next'] = $e->getMessage(); + } + } + $this->_fields = $fields; if (!empty($this->_errors)) { @@ -669,6 +744,81 @@ public function __viewEdit() { $this->Form->appendChild($fieldset); + // Pagination -------------------------------------------------- + + + $fieldset = new XMLElement('fieldset'); + $fieldset->setAttribute('class', 'settings'); + $fieldset->appendChild(new XMLElement('legend', __('Throttling'))); + + $group = new XMLElement('div'); + $group->setAttribute('class', 'two columns'); + + $label = Widget::Label(__('Pagination Variable Optional')); + $label->setAttribute('class', 'column'); + $input = Widget::Input( + 'fields[pagination][variable]', + General::sanitize( + isset($this->_fields['pagination']['variable']) + ? $this->_fields['pagination']['variable'] + : null + ) + ); + $input->setAttribute('placeholder', 'p'); + $label->appendChild($input); + + if (isset($this->_errors['variable'])) { + $label = Widget::Error($label, $this->_errors['variable']); + } + + $group->appendChild($label); + + $label = Widget::Label(__('Start Page Optional')); + $label->setAttribute('class', 'column'); + $input = Widget::Input( + 'fields[pagination][start]', + General::sanitize( + isset($this->_fields['pagination']['start']) + ? $this->_fields['pagination']['start'] + : null + ) + ); + $input->setAttribute('placeholder', '1'); + $label->appendChild($input); + + $fieldset->appendChild($group); + + $group->appendChild($label); + $help = new XMLElement('p'); + $help->setAttribute('class', 'help'); + $help->setValue(__('The pagination variable has to be included within your Datasource as a url parameter. Not setting a variable will ignore throttling.')); + $fieldset->appendChild($help); + + $label = Widget::Label(__('Next Page Optional')); + $input = Widget::Input( + 'fields[pagination][next]', + General::sanitize( + isset($this->_fields['pagination']['next']) + ? $this->_fields['pagination']['next'] + : null + ) + ); + $input->setAttribute('placeholder', '/data/datasource/pagination/@current-page != /data/datasource/pagination/@total-pages'); + $label->appendChild($input); + + if (isset($this->_errors['pagination']['next'])) { + $label = Widget::Error($label, $this->_errors['pagination']['next']); + } + + $fieldset->appendChild($label); + + $help = new XMLElement('p'); + $help->setAttribute('class', 'help'); + $help->setValue(__('Use an XPath expression to determine if there is a next page (boolean).')); + $fieldset->appendChild($help); + + $this->Form->appendChild($fieldset); + // Section ------------------------------------------------------------ $sections = SectionManager::fetch(null, 'ASC', 'name'); @@ -713,6 +863,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',"