Skip to content

Instantly share code, notes, and snippets.

@larscwallin
Created September 2, 2011 08:49
Show Gist options
  • Save larscwallin/1188212 to your computer and use it in GitHub Desktop.
Save larscwallin/1188212 to your computer and use it in GitHub Desktop.
SIMPLX Controller from the Sodexo site
<?php
$active_processes = array();
$parse_error = false;
/*
Abstract the Request Object by assigning it to a "proxy".
Note the force_get property which may force the Resource id and Routing Table actions to
be taken from the QueryString.
*/
if($_SERVER['REQUEST_METHOD'] == 'POST' && $force_get != '1'){
$requestObject = $_POST;
}else{
$requestObject = $_GET;
}
// Check validity of identifier param
if(!isset($requestObject['id']) && !isset($modx->resourceIdentifier)){
if($allow_default_context_action == '1'){
$current_location_id = '*';
}else{
return;
}
}else{
// Assign id from Request
$current_location_id = $requestObject['id'];
if($current_location_id == ''){
if(is_numeric($modx->resourceIdentifier)){
$current_location_id = $modx->resourceIdentifier;
}else{
if($allow_default_context_action == '1'){
$current_location_id = '*';
}else{
return;
}
}
}
}
if(!$parse_error){
if(isset($active_processes[$current_location_id])){
// Already running process for resource. Exiting
return;
}else{
$active_processes[$current_location_id] = $current_location_id;
}
// Get the JSON serialized routing table
$routingTable = $modx->cacheManager->get($routing_table);
if(!$routingTable){
$routingTable = $modx->getObject('modChunk',array('name' => $routing_table ));
$routingTable = $routingTable->getContent();
$modx->cacheManager->set($routing_table,$routingTable);
}
$routingTableError = false;
$routingTableLookupError = false;
$torch='default';
try{
$routingTable = json_decode($routingTable,true);
// Get the contexts collection from the decoded array
$routingTable = $routingTable['contexts'];
$routingTableError = false;
}catch(Exception $e){
$modx->log(modX::LOG_LEVEL_ERROR, 'SIMPLX_Controller: Error loading routing table.');
$routingTableError = true;
}
if(!$routingTableError && !$parse_error){
$current_context_key = $modx->context->key;
// Check if the routing table has any instructions for the current context.
if(array_key_exists($current_context_key,$routingTable)){
// Get the actual context array.
$current_context = $routingTable[$current_context_key];
/*
Check if the context has any instructions for the current page id.
*/
if(array_key_exists($current_location_id,$current_context)){
/*
Ok now we get all the request parameters
*/
$current_location_params = $current_context[$current_location_id];
/*
Go through the request object (GET/POST) and look for instructions for each parameter
*/
foreach ($requestObject as $param => $value) {
/*
Look if there is a name match for the current request parameter in the
current location list
*/
if(array_key_exists($param,$current_location_params)){
/*
For each parameter there can be different values which in turn have different
routing instructions in the location list.
Lets search for a value match.
*/
$current_param_instructions = $current_location_params[$param];
$current_instruction = $current_param_instructions[$value];
/*
Check if there was a valid instruction present
*/
if($current_instruction){
/*
Good lets run the Snippet which was in the instruction and exit
*/
$modx->runSnippet($current_instruction,array());
return;
}else{
/*
No instruction was specified for this specific querystring parameter value. Lets look and
see if there is a default instruction. The default instruction is has a key of '*'.
*/
$default_instruction = $current_param_instructions['*'];
if($default_instruction){
/*
Good lets run the Snippet which was in the default instruction and exit
*/
$modx->log(modX::LOG_LEVEL_ERROR, 'Running default');
$torch = $modx->runSnippet($default_instruction,array('torch'=>$torch));
return;
}
}
}else{
// This QS parameter did not have an instruction (Snippet name)
}
} // End of foreach
// If we got this far, lets look for a default instruction for the current id
if(array_key_exists('*',$current_location_params)){
$default_instruction = $current_location_params['*'];
if($default_instruction){
$torch = $modx->runSnippet($default_instruction);
}
}
}else{
// Check for a default instruction for the current context
if(array_key_exists('*',$current_context)){
$default_instruction = $current_context['*'];
if($default_instruction){
$torch = $modx->runSnippet($default_instruction);
}
}
} // if(array_key_exists($current_location_id,$current_context))
}else{
// This Context has no instructions
} // if(array_key_exists($current_context_key,$routingTable))
}else{
//print 'No routing table';
} //if(!$routingTableError)​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
array_pop($active_processes);
}else{
} // if(!$parse_error)
function simplx_controller_getroutingtable($routing_table){
global $modx;
$routingTable = $modx->cacheManager->get($routing_table);
if(!$routingTable){
// Get the JSON serialized routing table
$routingTable = $modx->getObject('modChunk',array('name' => $routing_table ));
$routingTable = $routingTable->getContent();
$modx->cacheManager->set($routing_table,$routingTable);
}
return $routingTable;
}
{
"objectypename":"routing_table",
"createdBy":"larscwallin",
"dateCreated":"",
"dateLastUpdated":"",
"lastUpdatedBy":"larscwallin",
"contexts":{
"web":{
"1":{
"service":{
"logout":"visioncart.logout"
},
"*":"visioncart.gotostore"
},
"5":{
"step":{
"2":"visioncart.gotoconfirm",
"6":"visioncart.finishorder"
}
}
},
"mgr":{
"*":{
"do":{
"deleteorder":"visioncart.deleteorder",
"getorderpdf":"visioncart.getorderpdf"
},
"a":{
"3":"modx.filemanagerfix"
},
"action":{
"mgr/orders/update":"visioncart.test"
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment