/**
  * Post metadata to ElasticSearch
  *
  * @param $id
  */
 public function postMetadata($id)
 {
     $contract = $this->contract->find($id);
     $updated_by = ['name' => '', 'email' => ''];
     if (!empty($contract->updated_user)) {
         $updated_by = ['name' => $contract->updated_user->name, 'email' => $contract->updated_user->email];
     }
     $contract->metadata->contract_id = $contract->id;
     $contract->metadata->page_number = $contract->pages()->count();
     $translatedFrom = [];
     $metadataAttr = $contract->metadata;
     if (isset($contract->metadata->translated_from) && !empty($contract->metadata->translated_from)) {
         $translatedFrom = $this->contract->getcontracts((int) $contract->metadata->translated_from);
     }
     $metadataAttr->translated_from = $translatedFrom;
     $contract->metadata = $metadataAttr;
     $metadata = ['id' => $contract->id, 'metadata' => collect($contract->metadata)->toJson(), 'total_pages' => $contract->pages->count(), 'created_by' => json_encode(['name' => $contract->created_user->name, 'email' => $contract->created_user->email]), 'supporting_contracts' => $this->contract->getSupportingDocuments($contract->id), 'updated_by' => json_encode($updated_by), 'created_at' => $contract->created_datetime->format('Y-m-d H:i:s'), 'updated_at' => $contract->last_updated_datetime->format('Y-m-d H:i:s')];
     try {
         $request = $this->http->post($this->apiURL('contract/metadata'), null, $metadata);
         $response = $request->send();
         $this->logger->info('Metadata successfully submitted to Elastic Search.', $response->json());
     } catch (Exception $e) {
         $this->logger->error($e->getMessage());
     }
 }
 /**
  * Move pdf file
  *
  * @param null $contract_id
  * @return bool
  */
 public function movePdFToFolder($contract_id = null)
 {
     if (is_null($contract_id)) {
         $contracts = $this->contract->getProcessCompleted();
         foreach ($contracts as $contract) {
             $file = $contract->file;
             $moveTo = sprintf('%s/%s', $contract->id, $contract->file);
             if ($this->contract->moveS3File($file, $moveTo)) {
                 $this->info(sprintf('Contract %s : completed.', $contract_id));
                 continue;
             }
             $this->info(sprintf('Contract %s : failed.', $contract_id));
         }
         return true;
     }
     $contract = $this->contract->find($contract_id);
     $file = $contract->file;
     $moveTo = sprintf('%s/%s', $contract->id, $contract->file);
     if ($this->contract->moveS3File($file, $moveTo)) {
         $this->info(sprintf('Contract %s : completed.', $contract_id));
         return true;
     }
     $this->info(sprintf('Contract %s : failed.', $contract_id));
     return true;
 }
 /**
  * Text send to RC
  *
  * @param $contract_id
  * @return bool
  */
 public function copyTextToRC($contract_id)
 {
     $tasks = $this->task->getAll($contract_id);
     foreach ($tasks as $task) {
         $this->page->saveText($contract_id, $task->page_no, $task->assignments->assignment->answer, false);
     }
     $contract = $this->contract->find($contract_id);
     $contract->mturk_status = Contract::MTURK_COMPLETE;
     $this->logger->info('Contract text updated from MTurk', ['Contract id' => $contract_id]);
     $this->logger->activity('mturk.log.sent_to_rc', null, $contract_id);
     return $contract->save();
 }
 /**
  * Download Word File
  *
  * @param $contract_id
  */
 public function download($contract_id)
 {
     $contract = $this->contract->find($contract_id);
     if (empty($contract)) {
         abort(404);
     }
     $text = $this->contract->getTextFromS3($contract->id, $contract->file);
     if (empty($text)) {
         abort(404);
     }
     $filename = sprintf('%s-%s', $contract->id, str_limit(str_slug($contract->title), 70));
     header("Content-type: application/vnd.ms-wordx");
     header("Content-Disposition: attachment;Filename={$filename}.doc");
     $html = "<html>";
     $html .= "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">";
     $html .= "<body>";
     $html .= $text;
     $html .= "</body>";
     $html .= "</html>";
     echo $html;
     exit;
 }
 /**
  * Update process status
  *
  * @param $status
  * @return bool
  * @throws \Exception
  */
 public function processStatus($status)
 {
     $contract = $this->contract->find($this->contract_id);
     $contract->pdf_process_status = $status;
     return $contract->save();
 }