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(); // Ceate an array of ProductTypes to be queried try{ if(!isset($_POST['Types'])){ Utils::reportError("POST does not contain 'Types' sub-array", $outputFormat); } if(count($_POST['Types']) == 0){ Utils::reportError("No product types were specified in POST", $outputFormat); } $queryTypes = array(); $allTypes = $client->getProductTypes(); if($_POST['Types'][0] == '*'){ $queryTypes = $allTypes; }else{ $allTypeNames = array_map(create_function('$t', 'return $t->getName();'), $allTypes); foreach($_POST['Types'] as $type){ if(!in_array($type, $allTypeNames)){ $errStr = "Error: The type " . $type . " is not used in the repository. Please use one of: "; $errStr .= $allTypeNames[0]; for($i = 1; $i < count($allTypeNames); $i++){ $errStr .= ", " . $allTypeNames[$i]; } Utils::reportError($errStr, $outputFormat); } array_push($queryTypes, $client->getProductTypeByName($type)); } if(!count($queryTypes)){ Utils::reportError("No ProductTypes were given to query", $outputFormat); } } }catch(Exception $e){ Utils::reportError($e->getMessage(), $outputFormat); } // Check if results are desired in a ProductPage and which page of results is desired $pagedResults = false; $pageNum = 1; if(isset($_POST['PagedResults'])){ if($_POST['PagedResults']){ if(count($queryTypes) != 1){ Utils::reportError("Paged queries can only be performed on one ProductType", $outputFormat); } $pagedResults = true; if(isset($_POST['PageNum'])){ $pageNum = intval($_POST['PageNum']); } } } // Create the tree of criteria objects that define the query if(!isset($_POST['Criteria'])){ Utils::reportError("POST does not contain 'Criteria' sub-array", $outputFormat); } if(!count($_POST['Criteria'])){ Utils::reportError("POST sub-array 'Criteria' contains no criteria", $outputFormat); } $rootIndex = (isset($_POST['RootIndex'])) ? intval($_POST['RootIndex']) : 0; try{ $criteriaTree = Utils::createCriteriaTree($rootIndex, $queryTypes, null); }catch(Exception $e){ Utils::reportError($e->getMessage(), $outputFormat); } // Add criteria to query object $query = new CAS_Filemgr_Query(); $query->addCriterion($criteriaTree); // Perform the query and collect results $results = array(); try{ if($pagedResults){ $resultPage = $client->pagedQuery($query, $queryTypes[0], $pageNum); foreach($resultPage->getPageProducts() as $p){ array_push($results, array('product'=>$p)); } }else{ foreach($queryTypes as $type){ foreach($client->query($query, $type) as $p){ array_push($results, array('product'=>$p)); } } } }catch(Exception $e){ Utils::reportError($e->getMessage(), $outputFormat); } // Format results try{ if($outputFormat == 'html'){ $payload = '\n"; if($pagedResults){ $payload .= ''; $payload .= ''; $payload .= ''; } }elseif ($outputFormat == 'json') { $payload = array(); $payload['results'] = Utils::formatResults($results); if($pagedResults){ $payload['totalPages'] = $resultPage->getTotalPages(); $payload['pageSize'] = $resultPage->getPageSize(); $payload['totalProducts'] = $client->getNumProducts($queryTypes[0]); } $payload = json_encode($payload); } }catch(Exception $e){ Utils::reportError($e->getMessage(), $outputFormat); } echo $payload; ?>