public function testSetHeader() { $r = new Response(200, array('Content-Type' => 'text/plain')); $r->setHeader('Content-Type', 'text/plain'); $r->setHeaders(array('Content-Length' => 1024, 'Content-Encoding' => 'gzip')); $this->assertEquals('text/plain', $r->getHeader('Content-Type')); $this->assertEquals(1024, $r->getHeader('Content-Length')); $this->assertEquals('gzip', $r->getHeader('Content-Encoding')); }
/** * Method to get model types * * @return void */ public function json() { $body = ''; if (null !== $this->request->getPath(1)) { // Get the selected field history value if ($this->request->getPath(1) == 'history' && null !== $this->request->getPath(2) && is_numeric($this->request->getPath(2)) && null !== $this->request->getPath(3) && is_numeric($this->request->getPath(3)) && null !== $this->request->getPath(4) && is_numeric($this->request->getPath(4))) { $modelId = $this->request->getPath(2); $fieldId = $this->request->getPath(3); $time = $this->request->getPath(4); $value = ''; $encOptions = $this->project->module('Phire')->encryptionOptions->asArray(); $fv = Table\FieldValues::findById(array($fieldId, $modelId)); if (isset($fv->field_id) && null !== $fv->history) { $history = json_decode($fv->history, true); if (isset($history[$time])) { $value = $history[$time]; $f = Table\Fields::findById($fieldId); $value = Model\FieldValue::decrypt($value, $f->encryption, $encOptions); } } $body = array('fieldId' => $fieldId, 'modelId' => $modelId, 'value' => html_entity_decode($value, ENT_QUOTES, 'UTF-8')); // Get the field history timestamps } else { if ($this->request->getPath(1) == 'history' && null !== $this->request->getPath(2) && is_numeric($this->request->getPath(2)) && null !== $this->request->getPath(3) && is_numeric($this->request->getPath(3))) { $modelId = $this->request->getPath(2); $fieldId = $this->request->getPath(3); $fv = Table\FieldValues::findById(array($fieldId, $modelId)); if (isset($fv->field_id) && null !== $fv->history) { $body = array_keys(json_decode($fv->history, true)); rsort($body); } // Get the model types } else { $clsAry = $this->request->getPath(); unset($clsAry[0]); $cls = implode('_', $clsAry); $types = \Phire\Project::getModelTypes($cls); $body = array('types' => $types); } } // Build the response and send it $response = new Response(); $response->setHeader('Content-Type', 'application/json; charset=utf-8')->setBody(json_encode($body)); $response->send(); } }
/** * Method to get date format * * @return void */ public function json() { if (null !== $this->request->getPath(1)) { $format = str_replace('_', '/', urldecode($this->request->getPath(1))); // Build the response and send it $response = new Response(); $response->setHeader('Content-Type', 'application/json')->setBody(json_encode(array('format' => date($format)))); $response->send(); } }
/** * Method to get other resource permissions via JS * * @return void */ public function json() { if (null !== $this->request->getPath(1)) { $resources = \Phire\Model\UserRole::getResources($this->project->module('Phire')); $class = str_replace('_', '\\', urldecode($this->request->getPath(1))); $types = array(); $actions = array(); foreach ($resources as $key => $resource) { if ($key == $class) { $types = $resource['types']; $actions = $resource['actions']; } } $body = array('types' => $types, 'actions' => $actions); // Build the response and send it $response = new Response(); $response->setHeader('Content-Type', 'application/json')->setBody(json_encode($body)); $response->send(); } }
/** * Method to send a JSON response * * @param mixed $values * @param int $code * @param array $headers * @return void */ public function sendJson($values, $code = 200, array $headers = null) { // Build the response and send it $response = new Response(); $this->response->setCode($code); if (null !== $headers) { foreach ($headers as $name => $value) { $this->response->setHeader($name, $value); } } // Force JSON content-type header $response->setHeader('Content-Type', 'application/json')->setBody(json_encode($values)); $response->send(); }