Skip to content

Commit

Permalink
w.i.p. for publication pdf download
Browse files Browse the repository at this point in the history
  • Loading branch information
WilcoLouwerse committed Aug 12, 2024
1 parent 68b61e2 commit 64b103d
Show file tree
Hide file tree
Showing 7 changed files with 284 additions and 44 deletions.
12 changes: 10 additions & 2 deletions lib/Controller/PublicationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -400,14 +400,22 @@ public function createPublicationFile(ObjectService $objectService, ?array $publ
}

// Initialize Twig
$loader = new FilesystemLoader('lib/Templates');
$loader = new FilesystemLoader('lib/Templates', '/var/www/html/apps-extra/opencatalogi');
$twig = new Environment($loader);

// Render the Twig template
$html = $twig->render('publication.html.twig', ['publication' => $publication]);

// Check if the directory exists, if not, create it
if (!file_exists('/tmp/mpdf')) {
mkdir('/tmp/mpdf', 0777, true);
}

// Set permissions for the directory (ensure it's writable)
chmod('/tmp/mpdf', 0777);

// Initialize mPDF
$mpdf = new Mpdf();
$mpdf = new Mpdf(['tempDir' => '/tmp/mpdf']);

// Write HTML to PDF
$mpdf->WriteHTML($html);
Expand Down
38 changes: 19 additions & 19 deletions lib/Db/ListingMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ public function find(int $id): Listing
$qb = $this->db->getQueryBuilder();

$qb->select(
'l.*',
'o.id AS organisation_id',
'o.title AS organisation_title',
'l.*',
'o.id AS organisation_id',
'o.title AS organisation_title',
'o.summary AS organisation_summary',
'o.description AS organisation_description',
'o.image AS organisation_image',
'o.oin AS organisation_oin',
'o.tooi AS organisation_tooi',
'o.rsin AS organisation_rsin',
'o.image AS organisation_image',
'o.oin AS organisation_oin',
'o.tooi AS organisation_tooi',
'o.rsin AS organisation_rsin',
'o.pki AS organisation_pki'
)
->from('listings', 'l')
Expand All @@ -42,7 +42,7 @@ public function find(int $id): Listing
}

/**
* Returns an db result and throws exceptions when there are more or less
* Returns a db result and throws exceptions when there are more or less
* results CUSTOM FOR JOINS
*
* @param IQueryBuilder $query
Expand Down Expand Up @@ -117,25 +117,25 @@ protected function findEntitiesCustom(IQueryBuilder $query): array {
public function findAll(?int $limit = null, ?int $offset = null, ?array $filters = [], ?array $searchConditions = [], ?array $searchParams = []): array
{
$qb = $this->db->getQueryBuilder();

$qb->select(
'l.*',
'o.id AS organisation_id',
'o.title AS organisation_title',
'l.*',
'o.id AS organisation_id',
'o.title AS organisation_title',
'o.summary AS organisation_summary',
'o.description AS organisation_description',
'o.image AS organisation_image',
'o.oin AS organisation_oin',
'o.tooi AS organisation_tooi',
'o.rsin AS organisation_rsin',
'o.image AS organisation_image',
'o.oin AS organisation_oin',
'o.tooi AS organisation_tooi',
'o.rsin AS organisation_rsin',
'o.pki AS organisation_pki'
)
->from('listings', 'l')
->leftJoin('l', 'organizations', 'o', 'l.organisation = o.id')
->setMaxResults($limit)
->setFirstResult($offset);


// Apply filters
foreach ($filters as $filter => $value) {
if ($value === 'IS NOT NULL') {
Expand All @@ -146,15 +146,15 @@ public function findAll(?int $limit = null, ?int $offset = null, ?array $filters
$qb->andWhere($qb->expr()->eq($filter, $qb->createNamedParameter($value)));
}
}

// Apply search conditions
if (!empty($searchConditions)) {
$qb->andWhere('(' . implode(' OR ', $searchConditions) . ')');
foreach ($searchParams as $param => $value) {
$qb->setParameter($param, $value);
}
}

// Use the existing findEntities method to fetch and map the results
return $this->findEntitiesCustom($qb);
}
Expand Down
115 changes: 108 additions & 7 deletions lib/Db/PublicationMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,105 @@ public function find(int $id): Publication
{
$qb = $this->db->getQueryBuilder();

$qb->select('*')
->from('publications')
$qb->select(
'p.*',
'c.id AS catalogi_id',
'c.title AS catalogi_title',
'm.id AS metadata_id',
'm.title AS metadata_title'
)
->from('publications', 'p')
->leftJoin('p', 'catalogi', 'c', 'p.catalogi = c.id')
->leftJoin('p', 'metadata', 'm', 'p.metaData = m.id')
->where(
$qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))
$qb->expr()->eq('p.id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))
);

return $this->findEntity(query: $qb);
return $this->findEntityCustom(query: $qb);
}

/**
* Returns a db result and throws exceptions when there are more or less
* results CUSTOM FOR JOINS
*
* @param IQueryBuilder $query
* @return Entity the entity
* @psalm-return T the entity
* @throws Exception
* @throws MultipleObjectsReturnedException if more than one item exist
* @throws DoesNotExistException if the item does not exist
* @since 14.0.0
*/
protected function findEntityCustom(IQueryBuilder $query): Entity {
return $this->mapRowToEntityCustom($this->findOneQuery($query));
}

/**
* CUSTOM FOR JOINS
*/
protected function mapRowToEntityCustom(array $row): Entity {
unset($row['DOCTRINE_ROWNUM']); // remove doctrine/dbal helper column

// Map the Catalogi fields to a sub-array
$catalogiData = [
'id' => $row['catalogi_id'] ?? null,
'title' => $row['catalogi_title'] ?? null,
];

$catalogiIsEmpty = true;
foreach ($catalogiData as $key => $value) {
if ($value !== null) {
$catalogiIsEmpty = false;
}

if (array_key_exists("catalogi_$key", $row) === true) {
unset($row["catalogi_$key"]);
}
}

// Map the MetaData fields to a sub-array
$metaDataData = [
'id' => $row['metadata_id'] ?? null,
'title' => $row['metadata_title'] ?? null,
];

$metaDataIsEmpty = true;
foreach ($metaDataData as $key => $value) {
if ($value !== null) {
$metaDataIsEmpty = false;
}

if (array_key_exists("metadata_$key", $row) === true) {
unset($row["metadata_$key"]);
}
}

$row['catalogi'] = $catalogiIsEmpty === true ? null : json_encode(Catalog::fromRow($catalogiData)->jsonSerialize());
$row['metadata'] = $catalogiIsEmpty === true ? null : json_encode(MetaData::fromRow($metaDataData)->jsonSerialize());

return \call_user_func($this->entityClass .'::fromRow', $row);
}

/**
* Runs a sql query and returns an array of entities CUSTOM FOR JOINS
*
* @param IQueryBuilder $query
* @return Entity[] all fetched entities
* @psalm-return T[] all fetched entities
* @throws Exception
* @since 14.0.0
*/
protected function findEntitiesCustom(IQueryBuilder $query): array {
$result = $query->executeQuery();
try {
$entities = [];
while ($row = $result->fetch()) {
$entities[] = $this->mapRowToEntityCustom($row);
}
return $entities;
} finally {
$result->closeCursor();
}
}

private function parseComplexFilter(IQueryBuilder $queryBuilder, array $filter, string $name): IQueryBuilder
Expand Down Expand Up @@ -102,8 +194,16 @@ public function findAll(?int $limit = null, ?int $offset = null, ?array $filters
{
$qb = $this->db->getQueryBuilder();

$qb->select('*')
->from('publications')
$qb->select(
'p.*',
'c.id AS catalogi_id',
'c.title AS catalogi_title',
'm.id AS metadata_id',
'm.title AS metadata_title'
)
->from('publications', 'p')
->leftJoin('p', 'catalogi', 'c', 'p.catalogi = c.id')
->leftJoin('p', 'metadata', 'm', 'p.metaData = m.id')
->setMaxResults($limit)
->setFirstResult($offset);

Expand All @@ -123,7 +223,8 @@ public function findAll(?int $limit = null, ?int $offset = null, ?array $filters
}
}

return $this->findEntities(query: $qb);
// Use the existing findEntities method to fetch and map the results
return $this->findEntitiesCustom($qb);
}

public function createFromArray(array $object): Publication
Expand Down
14 changes: 7 additions & 7 deletions lib/Templates/publication.html.twig
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<h1>Publicatie #{{ publicatie.title }}</h1>
<p>Catalogi: {{ publicatie.catalogi.title }}</p>
<p>Publicatie Type: {{ publicatie.metadata.title }}</p>
<p>Referentie: {{ publicatie.reference }}</p>
<p>Samenvatting: {{ publicatie.summary }}</p>
<p>Beschrijving: {{ publicatie.description }}</p>
<h1>Publicatie #{{ publication.title }}</h1>
{#<p>Catalogi: {{ publication.catalogi.title }}</p>#}
{#<p>Publicatie Type: {{ publication.metadata.title }}</p>#}
<p>Referentie: {{ publication.reference }}</p>
<p>Samenvatting: {{ publication.summary }}</p>
<p>Beschrijving: {{ publication.description }}</p>
<table>
<tr>
<th>Key</th>
<th>Value</th>
</tr>
{% for key, value in publicatie.data %}
{% for key, value in publication.data %}
<tr>
<td>{{ key }}</td>
<td>{{ value }}</td>
Expand Down
Loading

0 comments on commit 64b103d

Please sign in to comment.