<commit_before><?php

namespace AppBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

/**
 *
 */
class SourceController extends ArticleController
{
    protected function renderSourcePdf($parts)
    {
        $sourceArticle = $parts['article'];

        $templating = $this->container->get('templating');

        $html = $templating->render('AppBundle:Article:source-printview.html.twig', [
            'article' => $sourceArticle,
            'meta' => $sourceArticle,
            'layers' => $parts['layers'],
            'description' => $parts['description'],
            'interpretations' => $parts['interpretations'],
            'name' => $sourceArticle->getName(),
            'license' => $parts['license'],
        ]);

        /*
        echo $html;
        exit;
        */

        $this->renderPdf($html, str_replace(':', '-', $sourceArticle->getSlug(true)) . '.pdf');
    }


    protected function renderSourceViewer(Request $request, $uid, $sourceArticle)
    {
        if (in_array($request->get('_route'), [ 'source-jsonld' ])) {
            // return jsonld-rendition
            return new JsonLdResponse($sourceArticle->jsonLdSerialize($request->getLocale()));
        }

        $generatePrintView = 'source-pdf' == $request->get('_route');

        $fname = $this->buildArticleFname($sourceArticle);

        $params = [
            'params' => [
                // localize labels in xslt
                'lang' => \AppBundle\Utils\Iso639::code1To3($request->getLocale()),

                // notes per page in pdf
                'noteplacement' => 'perpage',
            ],
        ];

        // render the transcript / translation in the current language to get $license
        // and rendered content for non-iview2 display
        $html = $this->renderTei($fname,
                                 $generatePrintView ? 'dtabf_article-printview.xsl' : 'dtabf_article.xsl',
                                 $params);

        list($authors, $section_headers, $license, $entities, $bibitemLookup, $glossaryTerms, $refs) = $this->extractPartsFromHtml($html);

        $interpretation = $sourceArticle->getIsPartOf();
        $sourceDescription = null; // $sourceDescription is part of $interpretation
        $related = []; // if there are multipe sources for $interpretation
        if (isset($interpretation)) {
            $sourceDescription = [
                'article' => $interpretation,
                'html' => $this->renderSourceDescription($interpretation),
            ];
            list($dummy, $dummy, $license, $entitiesSourceDescription, $bibitemLookup, $glossaryTermsSourceDescription, $refs) = $this->extractPartsFromHtml($sourceDescription['html']);

            $entities = array_merge($entities, $entitiesSourceDescription);
            $glossaryTerms += $glossaryTermsSourceDescription;

            $related = $this->getDoctrine()
                ->getRepository('AppBundle:Article')
                ->findBy([ 'isPartOf' => $interpretation ],
                         [ 'dateCreated' => 'ASC', 'name' => 'ASC']);
        }

        $entityLookup = $this->buildEntityLookup($entities);
        $glossaryLookup = $this->buildGlossaryLookup($glossaryTerms, $request->getLocale());

        $fnameMets = $this->buildArticleFname($sourceArticle, '.mets.xml');
        $parts = explode('.', $fnameMets);
        $path = $parts[0]; // paths are of the format 'source-%05d', e.g. 'source-00123'

        /*
         * We have three different display formats
         *  1) Transcript/Translation without facsimile for AV and Transcript only:
         *      viewer-layers.html.twig
         *  2) Image/Object without a transcript:
         *      viewer-media.html.twig
         *  3) Facsimile and Transcript/Translation through iview2:
         *      viewer.html.twig
         *
         * 1) and 3) should support a PDF-rendition<commit_msg>Improve query for related SourceArticle<commit_after><?php

namespace AppBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

/**
 *
 */
class SourceController extends ArticleController
{
    protected function renderSourcePdf($parts)
    {
        $sourceArticle = $parts['article'];

        $templating = $this->container->get('templating');

        $html = $templating->render('AppBundle:Article:source-printview.html.twig', [
            'article' => $sourceArticle,
            'meta' => $sourceArticle,
            'layers' => $parts['layers'],
            'description' => $parts['description'],
            'interpretations' => $parts['interpretations'],
            'name' => $sourceArticle->getName(),
            'license' => $parts['license'],
        ]);

        /*
        echo $html;
        exit;
        */

        $this->renderPdf($html, str_replace(':', '-', $sourceArticle->getSlug(true)) . '.pdf');
    }

    protected function renderSourceViewer(Request $request, $uid, $sourceArticle)
    {
        if (in_array($request->get('_route'), [ 'source-jsonld' ])) {
            // return jsonld-rendition
            return new JsonLdResponse($sourceArticle->jsonLdSerialize($request->getLocale()));
        }

        $generatePrintView = 'source-pdf' == $request->get('_route');

        $fname = $this->buildArticleFname($sourceArticle);

        $params = [
            'params' => [
                // localize labels in xslt
                'lang' => \AppBundle\Utils\Iso639::code1To3($request->getLocale()),

                // notes per page in pdf
                'noteplacement' => 'perpage',
            ],
        ];

        // render the transcript / translation in the current language to get $license
        // and rendered content for non-iview2 display
        $html = $this->renderTei($fname,
                                 $generatePrintView ? 'dtabf_article-printview.xsl' : 'dtabf_article.xsl',
                                 $params);

        list($authors, $section_headers, $license, $entities, $bibitemLookup, $glossaryTerms, $refs) = $this->extractPartsFromHtml($html);

        $interpretation = $sourceArticle->getIsPartOf();
        $sourceDescription = null; // $sourceDescription is part of $interpretation
        $related = []; // if there are multiple sources for $interpretation
        if (isset($interpretation)) {
            $sourceDescription = [
                'article' => $interpretation,
                'html' => $this->renderSourceDescription($interpretation),
            ];
            list($dummy, $dummy, $license, $entitiesSourceDescription, $bibitemLookup, $glossaryTermsSourceDescription, $refs) = $this->extractPartsFromHtml($sourceDescription['html']);

            $entities = array_merge($entities, $entitiesSourceDescription);
            $glossaryTerms += $glossaryTermsSourceDescription;

            $relatedCriteria = new \Doctrine\Common\Collections\Criteria();
            $relatedCriteria
                ->where($relatedCriteria->expr()->eq('isPartOf', $interpretation))
                ->andWhere($relatedCriteria->expr()->neq('uid', $sourceArticle->getUid()));
            $relatedCriteria->orderBy([ 'dateCreated' => 'ASC', 'name' => 'ASC' ]);

            $related = $this->getDoctrine()
                ->getRepository('AppBundle:Article')
                ->matching($relatedCriteria);
        }

        $entityLookup = $this->buildEntityLookup($entities);
        $glossaryLookup = $this->buildGlossaryLookup($glossaryTerms, $request->getLocale());

        $fnameMets = $this->buildArticleFname($sourceArticle, '.mets.xml');
        $parts = explode('.', $fnameMets);
        $path = $parts[0]; // paths are of the format 'source-%05d', e.g. 'source-00123'

        /*
         * We have three different display formats
         *  1) Transcript/Translation without facsimile for AV and Transcript only:
         *      viewer-layers.html.twig
         *  2) Image/Object without a transcript:
         *      viewer-media.html.twig
         *  3) Facsimile and Transcript/Translation through iview2:
         *      viewer.html.twig
         *
         * 1) and 3) should support a PDF-rendition
{'fraction_non_alphanumeric': 0.13837965700768776, 'fraction_numerical': 0.004730928444707274, 'mean_word_length': 2.4262560777957862, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 88, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6710896', 'n_tokens_mistral': 2358, 'n_tokens_neox': 2248, 'n_words': 438}