2
votes

I have a modx revolution snippet [ajax processor actually] that passes some data to another snippet calling a function...

<?php
// processor.formdata

$status = 'success';

$msg = '';

$output = $modx->runSnippet($_POST['snippet'],array(
   'function' => $_POST['function'],
   'formdata' => $_POST
));

$modx->log(modX::LOG_LEVEL_ERROR,'got updateSurchargeData status. '.$output['status']);
$modx->log(modX::LOG_LEVEL_ERROR,'got updateSurchargeData message. '.$output['msg']);

$response_array = array(
    'status' => $output['status'],
    'msg' => $output['msg'],
    );

header('Content-type: application/json');

$output = json_encode($response_array);

return $output;

The $modx-runSnippet runs this:

<?php
//AmericanSurcharge

//echo $scriptProperties('form');
//$modx->log(modX::LOG_LEVEL_ERROR,'Running snippet. '.$function);
//$modx->log(modX::LOG_LEVEL_ERROR,'Running snippet. '.implode(',',$formdata));

$output = '';

$core = $modx->getOption('core_path').'components/americansurcharge/model/';

$surcharge = $modx->getService('americansurcharge', 'AmericanSurcharge', $core, $scriptProperties);

if (!$surcharge instanceof AmericanSurcharge){

    return 'Insantiation failed';

}else{

  $scriptProperties['formdata'] = $formdata;

  $output = $surcharge->$scriptProperties['function']($scriptProperties);

}

return $output;

which in turn runs this:

/**
*
* update one surcharge data
* AmericaSurcharge.class.php
*
*/
public function updateSurchargeData($scriptProperties){

    $this->modx->log(modX::LOG_LEVEL_ERROR,'Running updateSurchargeData. '.implode(',',$scriptProperties['formdata']));

    $output = array(
        'status' => 'success',
        'msg' => 'this is the message to return.',
        );

    $this->modx->log(modX::LOG_LEVEL_ERROR,'Finished running updateSurchargeData. '.$output['status']);
    $this->modx->log(modX::LOG_LEVEL_ERROR,'Finished running updateSurchargeData. '.$output['msg']);

    return $output;

}

Everything works & runs successfully, but the returned values from the $output = $modx-runSnippet return both "A" in each array key!

looking at the logs from AmericaSurcharge.class.php I get this:

(ERROR @ /index.php) Finished running updateSurchargeData. success
(ERROR @ /index.php) Finished running updateSurchargeData. this is the message to return.

and immediately following that from processor.formdata:

[2014-07-05 23:10:29] (ERROR @ /index.php) got updateSurchargeData status. A
[2014-07-05 23:10:29] (ERROR @ /index.php) got updateSurchargeData message. A

So somewhere between returning the $ouput array from AmericaSurcharge.class.php and getting it back in processor.formdata, something is happening to the values.

what am I doing wrong?

1
Try adding log statements at more places along the line, to see where it's happening.Barmar

1 Answers

2
votes

what am I doing wrong?

You are using snippets :)

When $modx->runSnippet($snippetName) is called it returns only string. Not an array or smth else.

Just look at modScript process method:

public function process($properties= null, $content= null) {
    parent :: process($properties, $content);
    if (!$this->_processed) {
        $scriptName= $this->getScriptName();
        $this->_result= function_exists($scriptName);
        if (!$this->_result) {
            $this->_result= $this->loadScript();
        }
        if ($this->_result) {
            if (empty($this->xpdo->event)) $this->xpdo->event = new stdClass();
            $this->xpdo->event->params= $this->_properties; /* store params inside event object */
            ob_start();
            $this->_output= $scriptName($this->_properties);
            $this->_output= ob_get_contents() . $this->_output;
            ob_end_clean();
            if ($this->_output && is_string($this->_output)) {
                /* collect element tags in the evaluated content and process them */
                $maxIterations= intval($this->xpdo->getOption('parser_max_iterations',null,10));
                $this->xpdo->parser->processElementTags(
                    $this->_tag,
                    $this->_output,
                    $this->xpdo->parser->isProcessingUncacheable(),
                    $this->xpdo->parser->isRemovingUnprocessed(),
                    '[[',
                    ']]',
                    array(),
                    $maxIterations
                );
            }
            $this->filterOutput();
            unset ($this->xpdo->event->params);
            $this->cache();
        }
    }
    $this->_processed= true;
    $this->xpdo->parser->setProcessingElement(false);
    /* finally, return the processed element content */
    return $this->_output;
}

But you can still call your snippet as function. Something like this:

if($modx->getParser()){
   $snippet= $modx->parser->getElement('modSnippet', $snippetName);
   if ($snippet instanceof modSnippet) {

    $s = $snippet->getScriptName();
    if(!function_exists($s)){
        $snippet->loadScript();
    }
    print_r($s());
   }
}

P.S.: The best way to solve you problem is using a real class-based processor instead of your processor-like processor. Advantages:

  1. Returns anything you want,
  2. One processor can extend another,
  3. Standardize format of response;