Skip to content

Commit

Permalink
Allow datasources access to url parameters. Closes #4
Browse files Browse the repository at this point in the history
  • Loading branch information
brendo committed Dec 17, 2014
1 parent 80a4fc3 commit 679d096
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 13 deletions.
57 changes: 54 additions & 3 deletions content/content.importers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require_once(TOOLKIT . '/class.gateway.php');
require_once(TOOLKIT . '/class.administrationpage.php');
require_once(TOOLKIT . '/class.frontendpage.php');
require_once(TOOLKIT . '/class.sectionmanager.php');

require_once(EXTENSIONS . '/xmlimporter/lib/class.xmlimporter.php');
Expand Down Expand Up @@ -118,6 +119,8 @@ public function __prepareRun($context) {

foreach ($context as $handle) {
$importer = $importManager->create($handle);
$importer->setContext($this->getContext());

if($importer === false) {
Symphony::Log()->writeToLog(__('The XMLImporter %s could not be found.', array($handle)), E_USER_ERROR, true);
continue;
Expand Down Expand Up @@ -232,7 +235,7 @@ public function __viewRun() {
));

$this->addFailedEntries($fieldset, $failed);

###
# Delegate: XMLImporterImportPostRunErrors
# Description: Notify Delegate for Errors
Expand Down Expand Up @@ -649,7 +652,7 @@ public function __viewEdit() {

$label = Widget::Label(__('Included Elements'));
$input = Widget::Input(
'fields[included-elements]',
'fields[included-elements]',
General::sanitize(
isset($this->_fields['included-elements'])
? $this->_fields['included-elements']
Expand Down Expand Up @@ -1101,7 +1104,7 @@ public function __viewIndex() {
if (!empty($importer['source'])) {
$handle = General::sanitize($importer['source']);
$datasources = DatasourceManager::listAll();

if(!empty($datasources[$handle]['name'])) {
$source = $datasources[$handle]['name'];
}
Expand Down Expand Up @@ -1249,4 +1252,52 @@ public function __viewIndex() {
$this->Form->appendChild($ul);
}
}

/**
* Given the REQUEST, parse out all the rubbish and emulate what the Frontend
* would do. This ensures any datasources that rely on URL parameters can continue
* to use them.
*
* @return array
*/
public function getContext()
{
$context = array();

if (isset($_REQUEST) && is_array($_REQUEST)) {
foreach ($_REQUEST as $key => $val) {
if (in_array($key, array('symphony-page', 'mode'))) {
continue;
}

// If the browser sends encoded entities for &, ie. a=1&b=2
// this causes the $_GET to output they key as amp;b, which results in
// $url-amp;b. This pattern will remove amp; allow the correct param
// to be used, $url-b
$key = preg_replace('/(^amp;|\/)/', null, $key);

// If the key gets replaced out then it will break the XML so prevent
// the parameter being set.
$key = General::createHandle($key);
if (!$key) {
continue;
}

// Handle ?foo[bar]=hi as well as straight ?foo=hi RE: #1348
if (is_array($val)) {
$val = General::array_map_recursive(array('FrontendPage', 'sanitizeParameter'), $val);
} else {
$val = FrontendPage::sanitizeParameter($val);
}

$context['url-' . $key] = $val;
}
}

return array(
'env' => array(
'url' => $context
)
);
}
}
25 changes: 15 additions & 10 deletions lib/class.xmlimporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ class XMLImporter {
const __ERROR_VALIDATING__ = 210;
const __ERROR_CREATING__ = 220;

public $_entries = array();
public $_errors = array();
protected $_entries = array();
protected $_errors = array();
public $context = array();

public function about() {
return array();
Expand All @@ -42,6 +43,11 @@ public function getErrors() {
return $this->_errors;
}

public function setContext(array $context)
{
$this->context = $context;
}

protected function getExpressionValue($xml, $entry, $xpath, $expression) {
$matches = $xpath->evaluate($expression, $entry);

Expand Down Expand Up @@ -109,8 +115,7 @@ function handleXMLError($errno, $errstr, $errfile, $errline, $context) {
}

else if (isset($options['source'])) {
$param_pool = array();
$ds = DatasourceManager::create($options['source'], $param_pool, true);
$ds = DatasourceManager::create($options['source'], $this->context, true);

// Not a DataSource (legacy)
if(!($ds instanceof Datasource)) {
Expand Down Expand Up @@ -464,15 +469,15 @@ public function commit($status) {
* to the `$existing` array.
*
* @param Field $field
* The unique field
* The unique field
* @param Entry $entry
* The current entry that is about to be imported
* The current entry that is about to be imported
* @param integer $index
* The current position of the Entry in the import
* The current position of the Entry in the import
* @param array $existing
* An associative array, by reference. The key is the position of
* the entry in the import, and the value is the `entry_id` if
* a match was found, otherwise null.
* An associative array, by reference. The key is the position of
* the entry in the import, and the value is the `entry_id` if
* a match was found, otherwise null.
*/
private function checkExisting(Field $field, Entry $entry, $index, array &$existing) {
$data = $entry->getData($field->get('id'));
Expand Down

0 comments on commit 679d096

Please sign in to comment.