Skip to content

Commit

Permalink
FORSLAG-38: Added handling of documents in GetOrganized
Browse files Browse the repository at this point in the history
  • Loading branch information
rimi-itk committed Jul 3, 2023
1 parent d269bf0 commit fbe4730
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 11 deletions.
1 change: 1 addition & 0 deletions config/sync/core.extension.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ module:
page_cache: 0
path: 0
path_alias: 0
pluginformalter: 0
quick_node_clone: 0
redirect: 0
responsive_image: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ abstract public function getArchivalInfo(NodeInterface $node): ?array;
/**
* Set archival info.
*/
protected function setArchivalInfo(NodeInterface $node, array $data) {
protected function addArchivalInfo(NodeInterface $node, array $data, $reset = FALSE) {
$info = $this->loadArchivalInfo($node);
$now = \Drupal::time()->getCurrentTime();
if (NULL === $info) {
Expand All @@ -53,6 +53,9 @@ protected function setArchivalInfo(NodeInterface $node, array $data) {
->execute();
}
else {
if (!$reset) {
$data += $info;
}
$this->database->update(self::TABLE_NAME)
->condition('archiver', static::class)
->condition('node_id', $node->id())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Drupal\hoeringsportal_citizen_proposal_archiving\Exception\GetOrganizedException;
use Drupal\node\NodeInterface;
use ItkDev\GetOrganized\Client;
use ItkDev\GetOrganized\Service\Documents;
use Psr\Log\LoggerInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

Expand All @@ -20,6 +21,13 @@ final class GetOrganizedArchiver extends AbstractArchiver {
*/
private array $options;

/**
* The GetOrganized documents service.
*
* @var \ItkDev\GetOrganized\Service\Documents
*/
private Documents $documents;

/**
* Constructor.
*/
Expand Down Expand Up @@ -84,24 +92,28 @@ private function archiveCitizenProposal(NodeInterface $node, string $content, st
}

try {
$client = new Client($this->options['api_username'], $this->options['api_password'], $this->options['api_url']);
/** @var \ItkDev\GetOrganized\Service\Documents $documents */
$documents = $client->api('documents');
$path = $this->fileSystem->getTempDirectory() . '/citizen-proposal-' . $node->id() . '.pdf';
$path = $this->fileSystem->getTempDirectory() . '/test-citizen-proposal-' . $node->id() . '.pdf';
file_put_contents($path, $content);

$result = $documents->AddToDocumentLibrary($path, $caseID);
if (empty($result)) {
throw new GetOrganizedException(sprintf('Error archiving citizen proposal %s (%s)', $node->id(), $node->label()));
// Unfinalize document in order to be able to update it.
$info = $this->getArchivalInfo($node);
if (isset($info['response'])) {
$this->unfinalizeDocument($info['response']);
}

$response = $this->addToCase($path, $caseID);
if (empty($response)) {
throw new GetOrganizedException(sprintf('Error archiving citizen proposal %s (%s); empty response', $node->id(), $node->label()));
}
$this->addArchivalInfo($node, ['response' => $response]);

$this->setArchivalInfo($node, $result);
$this->finalizeDocument($response);

$this->debug('@message', [
'@message' => json_encode([
'node' => $node->label(),
'getOrganizedCaseId' => $caseID,
'result' => $result,
'response' => $response,
'options' => $this->options,
], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES),
]);
Expand All @@ -122,4 +134,61 @@ public function log($level, $message, array $context = []) {
$this->logger->log($level, $message, $context);
}

/**
* Get the GetOrganized documents service.
*/
public function getDocuments(): Documents {
if (empty($this->documents)) {
$client = new Client(
$this->options['api_username'],
$this->options['api_password'],
$this->options['api_url']
);
$this->documents = $client->api('documents');
}

return $this->documents;
}

/**
* Add file to GetOrganized case.
*/
private function addToCase(string $path, string $caseID): ?array {
$this->logger->debug(sprintf('Add to case %s: %s', $caseID, $path));

return $this->getDocuments()->AddToDocumentLibrary($path, $caseID, basename($path));
}

/**
* Unfinalize document.
*/
private function unfinalizeDocument(array $response): ?array {
if (!isset($response['DocId'])) {
$this->logger->error(sprintf('Unfinalize document; unexpected response: %s', json_encode($response)));
}

$docId = (int) $response['DocId'];
$this->logger->debug(sprintf('Unfinalize document %s', $docId));

return $this->getDocuments()->UnmarkFinalized([$docId]);
}

/**
* Finalize document.
*/
private function finalizeDocument(array $response): ?array {
if (!isset($response['DocId'])) {
$this->logger->error(sprintf('Finalize document; unexpected response: %s', json_encode($response)));

return NULL;
}

$docId = (int) $response['DocId'];
$this->logger->debug(sprintf('Finalize document %s', $docId));
$response = $this->getDocuments()->Finalize((int) $response['DocId']);
$this->logger->debug(sprintf('Finalize document %s; response: %s', $docId, json_encode($response)));

return $response;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* }
* )
*/
class CitizenProposalFormAlter extends FormAlterBase {
final class CitizenProposalFormAlter extends FormAlterBase {

/**
* Constructor.
Expand Down

0 comments on commit fbe4730

Please sign in to comment.