Example #1
0
 public static function multiResponse($options, $overrideFormat = false)
 {
     $format = $overrideFormat ? $overrideFormat : $options['requestParams']['format'];
     if (empty($options['results'])) {
         $options['results'] = ['results' => [], 'total' => 0];
     }
     if ($options['results'] && isset($options['results']['results'])) {
         $totalResults = $options['results']['total'];
         $options['results'] = $options['results']['results'];
         if ($options['requestParams']['v'] >= 3) {
             header("Total-Results: {$totalResults}");
         }
     }
     switch ($format) {
         case 'atom':
         case 'csljson':
         case 'json':
         case 'keys':
         case 'versions':
             $link = Zotero_API::buildLinkHeader($options['action'], $options['uri'], $totalResults, $options['requestParams']);
             if ($link) {
                 header($link);
             }
             break;
     }
     if (!empty($options['head'])) {
         return;
     }
     switch ($format) {
         case 'atom':
             $t = microtime(true);
             $response = Zotero_Atom::createAtomFeed($options['action'], $options['title'], $options['uri'], $options['results'], $totalResults, $options['requestParams'], $options['permissions'], isset($options['fixedValues']) ? $options['fixedValues'] : null);
             StatsD::timing("api." . $options['action'] . ".multiple.createAtomFeed." . implode("-", $options['requestParams']['content']), (microtime(true) - $t) * 1000);
             return $response;
         case 'csljson':
             $json = Zotero_Cite::getJSONFromItems($options['results'], true);
             echo Zotero_Utilities::formatJSON($json);
             break;
         case 'json':
             echo Zotero_API::createJSONResponse($options['results'], $options['requestParams'], $options['permissions']);
             break;
         case 'keys':
             echo implode("\n", $options['results']) . "\n";
             break;
         case 'versions':
             if (!empty($options['results'])) {
                 echo Zotero_Utilities::formatJSON($options['results']);
             } else {
                 echo Zotero_Utilities::formatJSON(new stdClass());
             }
             break;
         case 'writereport':
             echo Zotero_Utilities::formatJSON($options['results']);
             break;
         default:
             throw new Exception("Unexpected format '" . $options['requestParams']['format'] . "'");
     }
 }