loadModule(); require_once(dirname(dirname(__FILE__)) . '/classes/CasBrowser.class.php'); // Extract desired output format from POST if(isset($_POST['OutputFormat'])){ try{ $outputFormat = Utils::getRequestedReturnType($_POST['OutputFormat']); }catch(Exception $e){ Utils::reportError($e->getMessage(), 'html'); } }else{ $outputFormat = 'html'; } // Get client handle $cb = new CasBrowser(); $client = $cb->getClient(); // Extract ProductType from POST if(!isset($_POST['Type'])){ Utils::reportError("POST does not contain 'Type' ProductType", $outputFormat); } $typeName = $_POST['Type']; try{ $allTypes = $client->getProductTypes(); $allTypeNames = array_map(create_function('$t', 'return $t->getName();'), $allTypes); if(!in_array($typeName, $allTypeNames)){ $errStr = "The type " . $typeName . " is not used in the repository. Please use one of: "; for($i = 0; $i < count($allTypeNames) - 1; $i++){ $errStr .= $allTypeNames[i] . ", "; } $errStr .= $allTypeNames[count($allTypeNames) - 1]; Utils::reportError($errStr, $outputFormat); } $type = $client->getProductTypeByName($typeName); }catch(Exception $e){ Utils::reportError($e->getMessage(), $outputFormat); } // Extract page number from POST if(!isset($_POST['PageNum'])){ Utils::reportError("POST does not contain 'PageNum'", $outputFormat); } $pageNum = intval($_POST['PageNum']); // Get the requested page try{ $page = Utils::getPage($type, $pageNum); }catch(Exception $e){ Utils::reportError($e->getMessage(), $outputFormat); } // Get the products from the requested page -- what we're really after $pageProducts = array(); foreach($page->getPageProducts() as $p){ array_push($pageProducts, array('product'=>$p)); } // Format results if($outputFormat == 'html'){ $payload = '\n"; $payload .= ''; $payload .= ''; }elseif ($outputFormat == 'json') { $payload = array(); try{ $payload['results'] = Utils::formatResults($pageProducts); $payload['totalProducts'] = $client->getNumProducts($type); }catch(Exception $e){ Utils::reportError($e->getMessage(), $outputFormat); } $payload['totalPages'] = $page->getTotalPages(); $payload['pageSize'] = $page->getPageSize(); $payload = json_encode($payload); } echo $payload; ?>