This question has been answered by BobRay. See the first response.
* Forwards the request to another resource without changing the URL.
*
* @param integer $id The resource identifier.
* @param string $options An array of options for the process.
*/
public function sendForward($id, $options = null) {
if (!$this->getRequest()) {
$this->log(modX::LOG_LEVEL_FATAL, "Could not load request class.");
}
$idInt = intval($id);
if (is_string($options) && !empty($options)) {
$options = array('response_code' => $options);
} elseif (!is_array($options)) {
$options = array();
}
$this->elementCache = array();
if ($idInt > 0) {
$merge = array_key_exists('merge', $options) && !empty($options['merge']);
$currentResource = array();
if ($merge) {
$excludes = array_merge(
explode(',', $this->getOption('forward_merge_excludes', $options, 'type,published,class_key,context_key')),
array(
'content'
,'pub_date'
,'unpub_date'
,'richtext'
,'_content'
,'_processed'
)
);
reset($this->resource->_fields);
while (list($fkey, $fval) = each($this->resource->_fields)) {
if (!in_array($fkey, $excludes)) {
if (is_scalar($fval) && $fval !== '') {
$currentResource[$fkey] = $fval;
} elseif (is_array($fval) && count($fval) === 5 && $fval[1] !== '') {
$currentResource[$fkey] = $fval;
}
}
}
}
$this->resource= $this->request->getResource('id', $idInt, array('forward' => true));
if ($this->resource) {
if ($merge && !empty($currentResource)) {
$this->resource->_fields = array_merge($this->resource->_fields, $currentResource);
$this->elementCache = array();
unset($currentResource);
}
$this->resourceIdentifier= $this->resource->get('id');
$this->resourceMethod= 'id';
if (isset($options['response_code']) && !empty($options['response_code'])) {
header($options['response_code']);
}
$this->request->prepareResponse();
exit();
}
$options= array_merge(
array(
'error_type' => '404'
,'error_header' => $this->getOption('error_page_header', $options,'HTTP/1.1 404 Not Found')
,'error_pagetitle' => $this->getOption('error_page_pagetitle', $options,'Error 404: Page not found')
,'error_message' => $this->getOption('error_page_message', $options,'<h1>Page not found</h1><p>The page you requested was not found.</p>')
),
$options
);
}
$this->sendError($id, $options);
}
/**
* Send the user to a MODX virtual error page.
*
* @uses invokeEvent() The OnPageNotFound event is invoked before the error page is forwarded
* to.
* @param array $options An array of options to provide for the OnPageNotFound event and error
* page.
*/
public function sendErrorPage($options = null) {
if (!is_array($options)) $options = array();
$options= array_merge(
array(
'response_code' => $this->getOption('error_page_header', $options, 'HTTP/1.1 404 Not Found')
,'error_type' => '404'
,'error_header' => $this->getOption('error_page_header', $options, 'HTTP/1.1 404 Not Found')
,'error_pagetitle' => $this->getOption('error_page_pagetitle', $options, 'Error 404: Page not found')
,'error_message' => $this->getOption('error_page_message', $options, '<h1>Page not found</h1><p>The page you requested was not found.</p>')
),
$options
);
$this->invokeEvent('OnPageNotFound', $options);
$this->sendForward($this->getOption('error_page', $options, '404'), $options);
}
/**
* Forwards the request to another resource without changing the URL.
*
* @param integer $id The resource identifier.
* @param string $options An array of options for the process.
*/
public function sendForward($id, $options = null) {
if (!$this->getRequest()) {
$this->log(modX::LOG_LEVEL_FATAL, "Could not load request class.");
}
$idInt = intval($id);
if (is_string($options) && !empty($options)) {
$options = array('response_code' => $options);
} elseif (!is_array($options)) {
$options = array();
}
$this->elementCache = array();
if ($idInt > 0) {
$merge = array_key_exists('merge', $options) && !empty($options['merge']);
$currentResource = array();
if ($merge) {
$excludes = array_merge(
explode(',', $this->getOption('forward_merge_excludes', $options, 'type,published,class_key,context_key')),
array(
'content'
,'pub_date'
,'unpub_date'
,'richtext'
,'_content'
,'_processed'
)
);
reset($this->resource->_fields);
while (list($fkey, $fval) = each($this->resource->_fields)) {
if (!in_array($fkey, $excludes)) {
if (is_scalar($fval) && $fval !== '') {
$currentResource[$fkey] = $fval;
} elseif (is_array($fval) && count($fval) === 5 && $fval[1] !== '') {
$currentResource[$fkey] = $fval;
}
}
}
}
$this->resource= $this->request->getResource('id', $idInt, array('forward' => true));
if ($this->resource) {
if ($merge && !empty($currentResource)) {
$this->resource->_fields = array_merge($this->resource->_fields, $currentResource);
$this->elementCache = array();
unset($currentResource);
}
$this->resourceIdentifier= $this->resource->get('id');
$this->resourceMethod= 'id';
if (isset($options['response_code']) && !empty($options['response_code'])) {
header($options['response_code']);
}
$this->request->prepareResponse();
exit();
}
$options= array_merge(
array(
'error_type' => '404'
,'error_header' => $this->getOption('error_page_header', $options,'')
,'error_pagetitle' => $this->getOption('error_page_pagetitle', $options,'Error 404: Page not found')
,'error_message' => $this->getOption('error_page_message', $options,'<h1>Page not found</h1><p>The page you requested was not found.</p>')
),
$options
);
}
$this->sendError($id, $options);
}
/**
* Send the user to a MODX virtual error page.
*
* @uses invokeEvent() The OnPageNotFound event is invoked before the error page is forwarded
* to.
* @param array $options An array of options to provide for the OnPageNotFound event and error
* page.
*/
public function sendErrorPage($options = null) {
if (!is_array($options)) $options = array();
$options= array_merge(
array(
'response_code' => $this->getOption('error_page_header', $options, '')
,'error_type' => '404'
,'error_header' => $this->getOption('error_page_header', $options, '')
,'error_pagetitle' => $this->getOption('error_page_pagetitle', $options, 'Error 404: Page not found')
,'error_message' => $this->getOption('error_page_message', $options, '<h1>Page not found</h1><p>The page you requested was not found.</p>')
),
$options
);
$this->invokeEvent('OnPageNotFound', $options);
$this->sendForward($this->getOption('error_page', $options, '404'), $options);
}