<commit_before><?php
/**
 * OpenEyes.
 *
 * (C) Moorfields Eye Hospital NHS Foundation Trust, 2008-2011
 * (C) OpenEyes Foundation, 2011-2013
 * This file is part of OpenEyes.
 * OpenEyes is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 * OpenEyes is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License along with OpenEyes in a file titled COPYING. If not, see <http://www.gnu.org/licenses/>.
 *
 * @link http://www.openeyes.org.uk
 *
 * @author OpenEyes <<EMAIL>>
 * @copyright Copyright (c) 2008-2011, Moorfields Eye Hospital NHS Foundation Trust
 * @copyright Copyright (c) 2011-2013, OpenEyes Foundation
 * @license http://www.gnu.org/licenses/gpl-3.0.html The GNU General Public License V3.0
 */
?>


<?php echo $form->hiddenInput($element, 'draft', 1) ?>
<?php
$layoutColumns = $form->layoutColumns;
$macro_id = isset($_POST['macro_id']) ? $_POST['macro_id'] : (isset($element->macro->id) ? $element->macro->id : null);
$patient_id = Yii::app()->request->getQuery('patient_id', null);
$patient = Patient::model()->findByPk($patient_id);

$element->letter_type = ($element->letter_type ? $element->letter_type : ( $macro_id == 2 ? 2 : null  ) );
?>
<div class="element-fields">
    <div class="row field-row">
        <div class="large-<?php echo $layoutColumns['label']; ?> column">
            <label>Site:</label>
        </div>
        <div class="large-3 column end">
            <?php echo $form->dropDownList($element, 'site_id', Site::model()->getLongListForCurrentInstitution(), array('empty' => '- Please select -', 'nowrapper' => true),
                false, array('field' => 2)) ?>
        </div>
    </div>

    <div class="row field-row">
        <div class="large-<?php echo $layoutColumns['label']; ?> column">
            <label>Date:</label>
        </div>
        <div class="large-2 column end">
            <?php echo $form->datePicker($element, 'date', array('maxDate' => 'today'), array('nowrapper' => true)) ?>
        </div>
    </div>

    <div class="row field-row">
        <div class="large-<?php echo $layoutColumns['label']; ?> column">
            <label>Macro:</label>
        </div>
        <div class="large-2 column end">
            <?php echo CHtml::dropDownList('macro_id', $macro_id, $element->letter_macros, array('empty' => '- Macro -', 'nowrapper' => true, 'class' => 'full-width')); ?>
        </div>
    </div>

    <div class="row field-row">
        <div class="large-<?php echo $layoutColumns['label']; ?> column">
            <label>Letter type:</label>
        </div>
        <div class="large-2 column end">
            <?php echo $form->dropDownList($element, 'letter_type', 
                    array(  '1' => 'Clinic discharge letter',
                            '2' => 'Post-op letter',
                            '3' => 'Clinic letter',
                            '4' => 'Other letter'),
                array('empty' => '- Please select -', 'nowrapper' => true, 'class' => 'full-width')) ?>
        </div>
    </div>

    <div class="row field-row">
        <div id="docman_block" class="large-12 column">
            <?php
            $macro_data = array();
            $patient_id = Yii::app()->request->getQuery('patient_id');

            if (isset($element->macro) && !isset($_POST['DocumentTarget'])) {
                $api = Yii::app()->moduleAPI->get('OphCoCorrespondence');
                $macro_data = $api->getMacroTargets($patient_id, $macro_id);
            }

            // set back posted data on error
            if (isset($_POST['DocumentTarget'])) {

                foreach ($_POST['DocumentTarget'] as $document_target) {

                    if (isset($document_target['attributes']['ToCc']) && $document_target['attributes']['ToCc'] == 'To') {
                        $macro_data['to'] = array(
                            'contact_type' => $document_target['attributes']['contact_type'],
                            'contact_id' => isset($document_target['attributes']['contact_id']) ? $document_target['attributes']['contact_id'] : null,
                            'contact_name' => isset($document_target['attributes']['contact_name']) ? $document_target['attributes']['contact_name'] : null,
                            'address' => isset($document_target['attributes']['address']) ? $document_target['attributes']['address'] : null,
                        );
                    } else {
                        if (isset($document_target['attributes']['ToCc']) && $document_target['attributes']['ToCc'] == 'Cc') {                      
                            $macro_data['cc'][] = array(
                                'contact_type' => $document_target['attributes']['contact_type'],
                                'contact_id' => isset($document_target['attributes']['contact_id']) ? $document_target['attributes']['contact_id'] : null,
                                'contact_name' => isset($document_target['attributes']['contact_name']) ? $document_target['attributes']['contact_name'] : null,
                                'address' => isset($document_target['attributes']['address']) ? $document_target['attributes']['address'] : null,
                            );
                        }
                    }
                }
            }
            $gp_address = isset($patient->gp->contact->correspondAddress) ? $patient->gp->contact->correspondAddress : $patient->gp->contact->address;
            if (!$gp_address) {
                $gp_address = isset($patient->practice->contact->correspondAddress) ? $patient->practice->contact->correspondAddress : $patient->practice->contact->address;
            }

            if (!$gp_address) {
                $gp_address = "N/A";
            } else {
                $gp_address = implode("\n", $gp_address->getLetterArray());
            }
            
            
            $patient_address = isset($patient->contact->correspondAddress) ? $patient->contact->correspondAddress : $patient->contact->address;

            if (!$patient_address) {
                $patient_address = "N/A";
            } else {
                $patient_address = implode("\n", $patient_address->getLetterArray());
            }
            

            $this->renderPartial('//docman/_create', array(
                'row_index' => (isset($row_index) ? $row_index : 0),
                'macro_data' => $macro_data,
                'macro_id' => $macro_id,
                'element' => $element,
                'defaults' => array(
                    'To' => array(
                        'contact_id' => $patient->gp->contact->id,
                        'contact_type' => 'GP',
                        'contact_name' => $patient->gp->contact->getFullName(),
                        'address' => $gp_address
                    ),
                    'Cc' => array(
                        'contact_id' => $patient->contact->id,
                        'contact_name' => $patient->contact->getFullName(),
                        'contact_type' => 'PATIENT',
                        'address' => $patient_address
                        
                    ),
                )
            ));

            ?>
        </div>
    </div>

    <div class="row field-row">
        <div class="large-<?php echo $layoutColumns['label']; ?> column OphCoCorrespondence_footerLabel">
            <label for="<?php echo get_class($element) . '_clinic_date_0'; ?>">
                <?php echo $element->getAttributeLabel('clinic_date') ?>:
            </label>
        </div>
        <div class="large-2 column end">
            <?php echo $form->datePicker($element, 'clinic_date', array('maxDate' => 'today'), array('nowrapper' => true, 'null' => true)) ?>
        </div><commit_msg>OE-6313 : FIX : if patient has no GP error<commit_after><?php
/**
 * OpenEyes.
 *
 * (C) Moorfields Eye Hospital NHS Foundation Trust, 2008-2011
 * (C) OpenEyes Foundation, 2011-2013
 * This file is part of OpenEyes.
 * OpenEyes is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 * OpenEyes is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License along with OpenEyes in a file titled COPYING. If not, see <http://www.gnu.org/licenses/>.
 *
 * @link http://www.openeyes.org.uk
 *
 * @author OpenEyes <<EMAIL>>
 * @copyright Copyright (c) 2008-2011, Moorfields Eye Hospital NHS Foundation Trust
 * @copyright Copyright (c) 2011-2013, OpenEyes Foundation
 * @license http://www.gnu.org/licenses/gpl-3.0.html The GNU General Public License V3.0
 */
?>


<?php echo $form->hiddenInput($element, 'draft', 1) ?>
<?php
$layoutColumns = $form->layoutColumns;
$macro_id = isset($_POST['macro_id']) ? $_POST['macro_id'] : (isset($element->macro->id) ? $element->macro->id : null);
$patient_id = Yii::app()->request->getQuery('patient_id', null);
$patient = Patient::model()->findByPk($patient_id);

$element->letter_type = ($element->letter_type ? $element->letter_type : ( $macro_id == 2 ? 2 : null  ) );
?>
<div class="element-fields">
    <div class="row field-row">
        <div class="large-<?php echo $layoutColumns['label']; ?> column">
            <label>Site:</label>
        </div>
        <div class="large-3 column end">
            <?php echo $form->dropDownList($element, 'site_id', Site::model()->getLongListForCurrentInstitution(), array('empty' => '- Please select -', 'nowrapper' => true),
                false, array('field' => 2)) ?>
        </div>
    </div>

    <div class="row field-row">
        <div class="large-<?php echo $layoutColumns['label']; ?> column">
            <label>Date:</label>
        </div>
        <div class="large-2 column end">
            <?php echo $form->datePicker($element, 'date', array('maxDate' => 'today'), array('nowrapper' => true)) ?>
        </div>
    </div>

    <div class="row field-row">
        <div class="large-<?php echo $layoutColumns['label']; ?> column">
            <label>Macro:</label>
        </div>
        <div class="large-2 column end">
            <?php echo CHtml::dropDownList('macro_id', $macro_id, $element->letter_macros, array('empty' => '- Macro -', 'nowrapper' => true, 'class' => 'full-width')); ?>
        </div>
    </div>

    <div class="row field-row">
        <div class="large-<?php echo $layoutColumns['label']; ?> column">
            <label>Letter type:</label>
        </div>
        <div class="large-2 column end">
            <?php echo $form->dropDownList($element, 'letter_type', 
                    array(  '1' => 'Clinic discharge letter',
                            '2' => 'Post-op letter',
                            '3' => 'Clinic letter',
                            '4' => 'Other letter'),
                array('empty' => '- Please select -', 'nowrapper' => true, 'class' => 'full-width')) ?>
        </div>
    </div>

    <div class="row field-row">
        <div id="docman_block" class="large-12 column">
            <?php
            $macro_data = array();
            $patient_id = Yii::app()->request->getQuery('patient_id');

            if (isset($element->macro) && !isset($_POST['DocumentTarget'])) {
                $api = Yii::app()->moduleAPI->get('OphCoCorrespondence');
                $macro_data = $api->getMacroTargets($patient_id, $macro_id);
            }

            // set back posted data on error
            if (isset($_POST['DocumentTarget'])) {

                foreach ($_POST['DocumentTarget'] as $document_target) {

                    if (isset($document_target['attributes']['ToCc']) && $document_target['attributes']['ToCc'] == 'To') {
                        $macro_data['to'] = array(
                            'contact_type' => $document_target['attributes']['contact_type'],
                            'contact_id' => isset($document_target['attributes']['contact_id']) ? $document_target['attributes']['contact_id'] : null,
                            'contact_name' => isset($document_target['attributes']['contact_name']) ? $document_target['attributes']['contact_name'] : null,
                            'address' => isset($document_target['attributes']['address']) ? $document_target['attributes']['address'] : null,
                        );
                    } else {
                        if (isset($document_target['attributes']['ToCc']) && $document_target['attributes']['ToCc'] == 'Cc') {                      
                            $macro_data['cc'][] = array(
                                'contact_type' => $document_target['attributes']['contact_type'],
                                'contact_id' => isset($document_target['attributes']['contact_id']) ? $document_target['attributes']['contact_id'] : null,
                                'contact_name' => isset($document_target['attributes']['contact_name']) ? $document_target['attributes']['contact_name'] : null,
                                'address' => isset($document_target['attributes']['address']) ? $document_target['attributes']['address'] : null,
                            );
                        }
                    }
                }
            }
            $gp_address = isset($patient->gp->contact->correspondAddress) ? $patient->gp->contact->correspondAddress : (isset($patient->gp->contact->address) ? $patient->gp->contact->address : null);
            if (!$gp_address) {
                $gp_address = isset($patient->practice->contact->correspondAddress) ? $patient->practice->contact->correspondAddress : (isset($patient->practice->contact->address) ? $patient->practice->contact->address : null);
            }

            if (!$gp_address) {
                $gp_address = "N/A";
            } else {
                $gp_address = implode("\n", $gp_address->getLetterArray());
            }
            
            
            $patient_address = isset($patient->contact->correspondAddress) ? $patient->contact->correspondAddress : $patient->contact->address;

            if (!$patient_address) {
                $patient_address = "N/A";
            } else {
                $patient_address = implode("\n", $patient_address->getLetterArray());
            }
            

            $this->renderPartial('//docman/_create', array(
                'row_index' => (isset($row_index) ? $row_index : 0),
                'macro_data' => $macro_data,
                'macro_id' => $macro_id,
                'element' => $element,
                'defaults' => array(
                    'To' => array(
                        'contact_id' => isset($patient->gp->contact->id) ? $patient->gp->contact->id : null,
                        'contact_type' => 'GP',
                        'contact_name' => isset($patient->gp->contact->id) ? $patient->gp->contact->getFullName() : null,
                        'address' => $gp_address
                    ),
                    'Cc' => array(
                        'contact_id' => isset($patient->contact->id) ? $patient->contact->id : null,
                        'contact_name' => isset($patient->contact->id) ? $patient->contact->getFullName() : null,
                        'contact_type' => 'PATIENT',
                        'address' => $patient_address
                        
                    ),
                )
            ));

            ?>
        </div>
    </div>

    <div class="row field-row">
        <div class="large-<?php echo $layoutColumns['label']; ?> column OphCoCorrespondence_footerLabel">
            <label for="<?php echo get_class($element) . '_clinic_date_0'; ?>">
                <?php echo $element->getAttributeLabel('clinic_date') ?>:
            </label>
        </div>
        <div class="large-2 column end">
            <?php echo $form->datePicker($element, 'clinic_date', array('maxDate' => 'today'), array('nowrapper' => true, 'null' => true)) ?>
        </div>
{'fraction_non_alphanumeric': 0.16378322985110616, 'fraction_numerical': 0.006751461812044126, 'mean_word_length': 2.1201805529433893, 'pattern_counts': {'":': 0, '<': 129, '<?xml version=': 0, '>': 351, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '19491', 'n_tokens_mistral': 4633, 'n_tokens_neox': 4399, 'n_words': 964}