<commit_before><?php

    ini_set('memory_limit', '256M');
    require_once('mpdf/mpdf.php');

    # ------------------------------------------------------------------------
    # PDF Generation
    # In order to keep things simple, pdfs are rendered from html templates using
    # mdpf, this should make it easy to update reports and labels in the future.
    # Templates are kept in templates/pdf, member variables of this class are
    # automatically available in the template files
    
    class Pdf extends Page {
        private $vars  = array();
        
        var $arg_list = array('visit' => '\w+\d+\-\d+',
                              'sid' => '\d+',
                              'p' => '\d',
                              'cid' => '\d+',
                              'did' => '\d+',
                              'sid' => '\d+',
                              );
        
        var $dispatch = array('shipment' => '_shipment_label',
                              'report' => '_visit_report',
                              'container' => '_container_report',
                              );
        var $def = 'shipment';

        
        
        # ------------------------------------------------------------------------
        # Shipment Labels
        function _shipment_label() {
            if (!$this->has_arg('prop')) $this->error('No proposal specified', 'Please select a proposal first');
            if (!$this->has_arg('sid')) $this->error('No shipment specified', 'No shipment id was specified');
            
            $ship = $this->db->pq("SELECT s.safetylevel, p.proposalcode || p.proposalnumber as prop, s.shippingid, s.shippingname, pe.givenname, pe.familyname, pe.phonenumber,pe.faxnumber, l.name as labname, l.address, l.city, l.country, pe2.givenname as givenname2, pe2.familyname as familyname2, pe2.phonenumber as phonenumber2, pe2.faxnumber as faxnumber2, l2.name as labname2, l2.address as address2, l2.city as city2, l2.country as country2, c2.courieraccount, c2.billingreference, c2.defaultcourriercompany FROM ispyb4a_db.shipping s INNER JOIN ispyb4a_db.labcontact c ON s.sendinglabcontactid = c.labcontactid INNER JOIN ispyb4a_db.person pe ON c.personid = pe.personid INNER JOIN ispyb4a_db.laboratory l ON l.laboratoryid = pe.laboratoryid INNER JOIN ispyb4a_db.labcontact c2 ON s.returnlabcontactid = c2.labcontactid  INNER JOIN ispyb4a_db.person pe2 ON c2.personid = pe2.personid INNER JOIN ispyb4a_db.laboratory l2 ON l2.laboratoryid = pe2.laboratoryid INNER JOIN ispyb4a_db.proposal p ON p.proposalid = s.proposalid  WHERE s.shippingid=:1", array($this->arg('sid')));
            if (!sizeof($ship)) $this->error('No such shipment', 'The specified shipment doesnt exist');
            else $ship = $ship[0];
            
            $addr = array($ship['ADDRESS']);
            if ($ship['CITY']) array_push($addr, $ship['CITY']."\n");
            if ($ship['COUNTRY']) array_push($addr, $ship['COUNTRY']."\n");
            $ship['ADDRESS'] = str_replace("\n", '<br/>',  implode(', ', $addr));

            $addr = array($ship['ADDRESS2']);
            if ($ship['CITY2']) array_push($addr, $ship['CITY2']."\n");
            if ($ship['COUNTRY2']) array_push($addr, $ship['COUNTRY2']."\n");
            $ship['ADDRESS2'] = str_replace("\n", '<br/>',  implode(', ', $addr));
            
            $this->ship = $ship;
            
            $this->dewars = $this->db->pq("SELECT bl.beamlinename, bl.beamlineoperator, TO_CHAR(bl.startdate, 'DD-MM-YYYY') as st, d.transportvalue, d.customsvalue, d.code, d.barcode FROM ispyb4a_db.dewar d LEFT OUTER JOIN ispyb4a_db.blsession bl ON d.firstexperimentid = bl.sessionid WHERE d.shippingid=:1", array($ship['SHIPPINGID']));
            
            $this->_render('shipment_label');
        }
        
        
        # ------------------------------------------------------------------------
        # Report of Data Collections
        function _visit_report() {
            if (!$this->has_arg('visit')) $this->error('No visit specified', 'You need to specify a visit to view this page');
            
            $info = $this->db->pq("SELECT (s.enddate - s.startdate)*24 as len, s.sessionid as sid, s.beamlinename, s.beamlineoperator as lc, TO_CHAR(s.startdate, 'DD-MM-YYYY HH24:MI') as st, TO_CHAR(s.enddate, 'DD-MM-YYYY HH24:MI') as en, p.proposalcode||p.proposalnumber||'-'||s.visit_number as visit, p.proposalcode||p.proposalnumber as prop FROM ispyb4a_db.blsession s INNER JOIN ispyb4a_db.proposal p ON p.proposalid = s.proposalid WHERE p.proposalcode||p.proposalnumber||'-'||s.visit_number LIKE :1", array($this->arg('visit')));
            
            if (!sizeof($info)) $this->error('No such visit', 'The specified visit doesnt exist');
            else $info = $info[0];
            
            $this->info = $info;
            
            $this->users = $this->db->pq("SELECT u.name,u.fullname FROM investigation@D<EMAIL> i INNER JOIN investigationuser<EMAIL> iu on i.id = iu.investigation_id INNER JOIN user_@DICAT_RO u on u.id = iu.user_id WHERE lower(i.visit_id)=:1 AND iu.role='NORMAL_USER'", array($info['VISIT']));
            
            #$lc = $this->lc_lookup($info['SID']);
            #$this->lc = $lc ? $lc->name : 'N/A';
            
            $rows = $this->db->pq("SELECT dc.datacollectionid as id, dc.overlap,dc.imageprefix,dc.imagedirectory as dir,dc.datacollectionnumber,TO_CHAR(dc.starttime, 'DD/MM/YYYY HH24:MI:SS'), sa.name, p.name as protein, dc.numberofimages, dc.wavelength, dc.detectordistance, dc.exposuretime, dc.axisstart, dc.axisrange, dc.xbeam, dc.ybeam, dc.resolution, dc.comments FROM ispyb4a_db.datacollection dc LEFT OUTER JOIN ispyb4a_db.blsample sa ON dc.blsampleid = sa.blsampleid LEFT OUTER JOIN ispyb4a_db.crystal c ON sa.crystalid = c.crystalid LEFT OUTER JOIN ispyb4a_db.protein p ON c.proteinid = p.proteinid WHERE dc.sessionid=:1 ORDER BY dc.starttime", array($info['SID']));
            
            $screen = array();
            $dcs = array();
            foreach ($rows as $dc) {
                if ($dc['OVERLAP'] == 0) array_push($dcs, 'api.datacollectionid='.$dc['ID']);
                else array_push($screen, 'datacollectionid='.$dc['ID']);
            }
            
            $screen = implode(' OR ', $screen);
            $dcs = implode(' OR ', $dcs);
            
            $aps = sizeof($dcs) ? $this->db->pq("SELECT api.datacollectionid as id, app.autoprocprogramid,app.processingcommandline as type, apss.ntotalobservations as ntobs, apss.ntotaluniqueobservations as nuobs, apss.resolutionlimitlow as rlow, apss.resolutionlimithigh as rhigh, apss.scalingstatisticstype as shell, apss.rmeasalliplusiminus as rmeas, apss.rmerge, apss.completeness, apss.anomalouscompleteness as anomcompleteness, apss.anomalousmultiplicity as anommultiplicity, apss.multiplicity, apss.meanioversigi as isigi, ap.spacegroup as sg, ap.refinedcell_a as cell_a, ap.refinedcell_b as cell_b, ap.refinedcell_c as cell_c, ap.refinedcell_alpha as cell_al, ap.refinedcell_beta as cell_be, ap.refinedcell_gamma as cell_ga FROM ispyb4a_db.autoprocintegration api INNER JOIN ispyb4a_db.autoprocscaling_has_int aph ON api.autoprocintegrationid = aph.autoprocintegrationid INNER JOIN ispyb4a_db.autoprocscaling aps ON aph.autoprocscalingid = aps.autoprocscalingid INNER JOIN ispyb4a_db.autoproc ap ON aps.autoprocid = ap.autoprocid INNER JOIN ispyb4a_db.autoprocscalingstatistics apss ON apss.autoprocscalingid = aph.autoprocscalingid INNER JOIN ispyb4a_db.autoprocprogram app ON api.autoprocprogramid = app.autoprocprogramid WHERE $dcs", array($this->arg('id'))) : array();
            <commit_msg>don't try and generate report for empty visit<commit_after><?php

    ini_set('memory_limit', '256M');
    require_once('mpdf/mpdf.php');

    # ------------------------------------------------------------------------
    # PDF Generation
    # In order to keep things simple, pdfs are rendered from html templates using
    # mdpf, this should make it easy to update reports and labels in the future.
    # Templates are kept in templates/pdf, member variables of this class are
    # automatically available in the template files
    
    class Pdf extends Page {
        private $vars  = array();
        
        var $arg_list = array('visit' => '\w+\d+\-\d+',
                              'sid' => '\d+',
                              'p' => '\d',
                              'cid' => '\d+',
                              'did' => '\d+',
                              'sid' => '\d+',
                              );
        
        var $dispatch = array('shipment' => '_shipment_label',
                              'report' => '_visit_report',
                              'container' => '_container_report',
                              );
        var $def = 'shipment';

        
        
        # ------------------------------------------------------------------------
        # Shipment Labels
        function _shipment_label() {
            if (!$this->has_arg('prop')) $this->error('No proposal specified', 'Please select a proposal first');
            if (!$this->has_arg('sid')) $this->error('No shipment specified', 'No shipment id was specified');
            
            $ship = $this->db->pq("SELECT s.safetylevel, p.proposalcode || p.proposalnumber as prop, s.shippingid, s.shippingname, pe.givenname, pe.familyname, pe.phonenumber,pe.faxnumber, l.name as labname, l.address, l.city, l.country, pe2.givenname as givenname2, pe2.familyname as familyname2, pe2.phonenumber as phonenumber2, pe2.faxnumber as faxnumber2, l2.name as labname2, l2.address as address2, l2.city as city2, l2.country as country2, c2.courieraccount, c2.billingreference, c2.defaultcourriercompany FROM ispyb4a_db.shipping s INNER JOIN ispyb4a_db.labcontact c ON s.sendinglabcontactid = c.labcontactid INNER JOIN ispyb4a_db.person pe ON c.personid = pe.personid INNER JOIN ispyb4a_db.laboratory l ON l.laboratoryid = pe.laboratoryid INNER JOIN ispyb4a_db.labcontact c2 ON s.returnlabcontactid = c2.labcontactid  INNER JOIN ispyb4a_db.person pe2 ON c2.personid = pe2.personid INNER JOIN ispyb4a_db.laboratory l2 ON l2.laboratoryid = pe2.laboratoryid INNER JOIN ispyb4a_db.proposal p ON p.proposalid = s.proposalid  WHERE s.shippingid=:1", array($this->arg('sid')));
            if (!sizeof($ship)) $this->error('No such shipment', 'The specified shipment doesnt exist');
            else $ship = $ship[0];
            
            $addr = array($ship['ADDRESS']);
            if ($ship['CITY']) array_push($addr, $ship['CITY']."\n");
            if ($ship['COUNTRY']) array_push($addr, $ship['COUNTRY']."\n");
            $ship['ADDRESS'] = str_replace("\n", '<br/>',  implode(', ', $addr));

            $addr = array($ship['ADDRESS2']);
            if ($ship['CITY2']) array_push($addr, $ship['CITY2']."\n");
            if ($ship['COUNTRY2']) array_push($addr, $ship['COUNTRY2']."\n");
            $ship['ADDRESS2'] = str_replace("\n", '<br/>',  implode(', ', $addr));
            
            $this->ship = $ship;
            
            $this->dewars = $this->db->pq("SELECT bl.beamlinename, bl.beamlineoperator, TO_CHAR(bl.startdate, 'DD-MM-YYYY') as st, d.transportvalue, d.customsvalue, d.code, d.barcode FROM ispyb4a_db.dewar d LEFT OUTER JOIN ispyb4a_db.blsession bl ON d.firstexperimentid = bl.sessionid WHERE d.shippingid=:1", array($ship['SHIPPINGID']));
            
            $this->_render('shipment_label');
        }
        
        
        # ------------------------------------------------------------------------
        # Report of Data Collections
        function _visit_report() {
            if (!$this->has_arg('visit')) $this->error('No visit specified', 'You need to specify a visit to view this page');
            
            $info = $this->db->pq("SELECT (s.enddate - s.startdate)*24 as len, s.sessionid as sid, s.beamlinename, s.beamlineoperator as lc, TO_CHAR(s.startdate, 'DD-MM-YYYY HH24:MI') as st, TO_CHAR(s.enddate, 'DD-MM-YYYY HH24:MI') as en, p.proposalcode||p.proposalnumber||'-'||s.visit_number as visit, p.proposalcode||p.proposalnumber as prop FROM ispyb4a_db.blsession s INNER JOIN ispyb4a_db.proposal p ON p.proposalid = s.proposalid WHERE p.proposalcode||p.proposalnumber||'-'||s.visit_number LIKE :1", array($this->arg('visit')));
            
            if (!sizeof($info)) $this->error('No such visit', 'The specified visit doesnt exist');
            else $info = $info[0];
            
            $this->info = $info;
            
            $this->users = $this->db->pq("SELECT u.name,u.fullname FROM investigation@DICAT_RO i INNER JOIN investigationuser@DICAT_RO iu on i.id = iu.investigation_id INNER JOIN user_@DICAT_RO u on u.id = iu.user_id WHERE lower(i.visit_id)=:1", array($info['VISIT']));
            
            #$lc = $this->lc_lookup($info['SID']);
            #$this->lc = $lc ? $lc->name : 'N/A';
            
            $rows = $this->db->pq("SELECT dc.datacollectionid as id, dc.overlap,dc.imageprefix,dc.imagedirectory as dir,dc.datacollectionnumber,TO_CHAR(dc.starttime, 'DD/MM/YYYY HH24:MI:SS'), sa.name, p.name as protein, dc.numberofimages, dc.wavelength, dc.detectordistance, dc.exposuretime, dc.axisstart, dc.axisrange, dc.xbeam, dc.ybeam, dc.resolution, dc.comments FROM ispyb4a_db.datacollection dc LEFT OUTER JOIN ispyb4a_db.blsample sa ON dc.blsampleid = sa.blsampleid LEFT OUTER JOIN ispyb4a_db.crystal c ON sa.crystalid = c.crystalid LEFT OUTER JOIN ispyb4a_db.protein p ON c.proteinid = p.proteinid WHERE dc.sessionid=:1 ORDER BY dc.starttime", array($info['SID']));
            
            if (!sizeof($rows)) $this->error('No data', 'No data collections for this visit yet');
            
            $screen = array();
            $dcs = array();
            foreach ($rows as $dc) {
                if ($dc['OVERLAP'] == 0) array_push($dcs, 'api.datacollectionid='.$dc['ID']);
                else array_push($screen, 'datacollectionid='.$dc['ID']);
            }
            
            $screen = implode(' OR ', $screen);
            $dcs = implode(' OR ', $dcs);
            
            $aps = sizeof($dcs) ? $this->db->pq("SELECT api.datacollectionid as id, app.autoprocprogramid,app.processingcommandline as type, apss.ntotalobservations as ntobs, apss.ntotaluniqueobservations as nuobs, apss.resolutionlimitlow as rlow, apss.resolutionlimithigh as rhigh, apss.scalingstatisticstype as shell, apss.rmeasalliplusiminus as rmeas, apss.rmerge, apss.completeness, apss.anomalouscompleteness as anomcompleteness, apss.anomalousmultiplicity as anommultiplicity, apss.multiplicity, apss.meanioversigi as isigi, ap.spacegroup as sg, ap.refinedcell_a as cell_a, ap.refinedcell_b as cell_b, ap.refinedcell_c as cell_c, ap.refinedcell_alpha as cell_al, ap.refinedcell_beta as cell_be, ap.refinedcell_gamma as cell_ga FROM ispyb4a_db.autoprocintegration api INNER JOIN ispyb4a_db.autoprocscaling_has_int aph ON api.autoprocintegrationid = aph.autoprocintegrationid INNER JOIN ispyb4a_db.autoprocscaling aps ON aph.autoprocscalingid = aps.autoprocscalingid INNER JOIN ispyb4a_db.autoproc ap ON aps.autoprocid = ap.autoprocid INNER JOIN ispyb4a_db.autoprocscalingstatistics apss ON apss.autoprocscalingid = aph.autoprocscalingid INNER JOIN ispyb4a_db.autoprocprogram app ON api.autoprocprogramid = app.autoprocprogramid WHERE $dcs", array($this->arg('id'))) : array();
            
{'fraction_non_alphanumeric': 0.15722985229334024, 'fraction_numerical': 0.009587976159626846, 'mean_word_length': 2.522820629849384, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 90, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '332919', 'n_tokens_mistral': 5116, 'n_tokens_neox': 4926, 'n_words': 1160}