<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
 */
class SiteAndFirmWidget extends CWidget
{
    public $title = 'Please confirm Site and Firm';
    public $subspecialty;
    public $support_services;
    public $patient;
    public $returnUrl;

    public function init()
    {
        if (!$this->returnUrl) {
            $this->returnUrl = Yii::app()->request->url;
        }
    }

    public function run()
    {
        $model = new SiteAndFirmForm();
        $user = User::model()->findByPk(Yii::app()->user->id);
        if (isset($_POST['SiteAndFirmForm'])) {
            $model->attributes = $_POST['SiteAndFirmForm'];
            if ($model->validate()) {
                $user->changeFirm($model->firm_id);
                $user->last_site_id = $model->site_id;
                if (!$user->save(false)) {
                    throw new CException('Error saving user');
                }
                $user->audit('user', 'change-firm', $user->last_firm_id);

                Yii::app()->session['selected_site_id'] = $model->site_id;
                $this->controller->selectedSiteId = $model->site_id;
                Yii::app()->session['selected_firm_id'] = $model->firm_id;
                $this->controller->selectedFirmId = $model->firm_id;
                Yii::app()->session['confirm_site_and_firm'] = false;

                Yii::app()->event->dispatch('firm_changed', array('firm_id' => $model->firm_id));

                $firm = Firm::model()->findByPk($model->firm_id);
                $subspecialty_id = $firm->serviceSubspecialtyAssignment ? $firm->serviceSubspecialtyAssignment->subspecialty_id : null;

                if ($this->patient && $episode = $this->patient->getOpenEpisodeOfSubspecialty($subspecialty_id)) {
                    Yii::app()->session['episode_hide_status'] = array(
                        $episode->id => 1,
                    );
                }

                // Redirect browser to clear POST
                $this->controller->redirect($this->returnUrl);
                Yii::app()->end();
            }
        } else {
            $model->firm_id = Yii::app()->session['selected_firm_id'];
            $model->site_id = Yii::app()->session['selected_site_id'];
        }

        if (!$sites = $user->siteSelections) {
            $sites = Institution::model()->getCurrent()->sites;
        }

        $user_firm_ids = array();
        if (Yii::app()->params['profile_user_can_edit']) {
            // firm selections only apply when the user is able to change them.
            // if firms should be restricted then this should be done through UserFirmRights
            foreach ($user->firmSelections as $firm) {
                $user_firm_ids[] = $firm->id;
            }
        }

        $firms = array();
        if ($preferred_firms = $user->preferred_firms) {
            foreach ($preferred_firms as $preferred_firm) {
                if (
                    $preferred_firm->active &&
                    (count($user_firm_ids) === 0 || in_array($preferred_firm->id, $user_firm_ids)) &&
                    (!$this->subspecialty || ($preferred_firm->serviceSubspecialtyAssignment && $this->subspecialty->id == $preferred_firm->serviceSubspecialtyAssignment->subspecialty_id))
                ) {
                    if ($preferred_firm->serviceSubspecialtyAssignment) {
                        $firms['Recent'][$preferred_firm->id] = "$preferred_firm->name ({$preferred_firm->serviceSubspecialtyAssignment->subspecialty->name})";
                    } else {
                        $firms['Recent'][$preferred_firm->id] = "$preferred_firm->name";
                    }
                }
            }
        }

        foreach ($this->controller->firms as $firm_id => $firm_label) {
            if ((count($user_firm_ids) === 0 || in_array($firm_id,
                        $user_firm_ids)) && !isset($firms['Recent'][$firm_id])
            ) {
                $firm = Firm::model()->findByPk($firm_id);
                if ($firm->active &&
                    (!$this->subspecialty ||
                        ($firm->serviceSubspecialtyAssignment && $firm->serviceSubspecialtyAssignment->subspecialty_id == $this->subspecialty->id)
                    )
                ) {
                    if ($preferred_firms) {
                        $firms['Other'][$firm_id] = $firm_label;
                    } else {
                        $firms[$firm_id] = $firm_label;
                    }
                }
            }
        }

        $this->render('SiteAndFirmWidget', array(
            'model' => $model,
            'firms' => $firms,
            'sites' => CHtml::listData($sites, 'id', 'short_name'),
        ));
    }<commit_msg>Checks firm from session hasn't been deleted.<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
 */
class SiteAndFirmWidget extends CWidget
{
    public $title = 'Please confirm Site and Firm';
    public $subspecialty;
    public $support_services;
    public $patient;
    public $returnUrl;

    public function init()
    {
        if (!$this->returnUrl) {
            $this->returnUrl = Yii::app()->request->url;
        }
    }

    public function run()
    {
        $model = new SiteAndFirmForm();
        $user = User::model()->findByPk(Yii::app()->user->id);
        if (isset($_POST['SiteAndFirmForm'])) {
            $model->attributes = $_POST['SiteAndFirmForm'];
            if ($model->validate()) {
                $user->changeFirm($model->firm_id);
                $user->last_site_id = $model->site_id;
                if (!$user->save(false)) {
                    throw new CException('Error saving user');
                }
                $user->audit('user', 'change-firm', $user->last_firm_id);

                Yii::app()->session['selected_site_id'] = $model->site_id;
                $this->controller->selectedSiteId = $model->site_id;
                Yii::app()->session['selected_firm_id'] = $model->firm_id;
                $this->controller->selectedFirmId = $model->firm_id;
                Yii::app()->session['confirm_site_and_firm'] = false;

                Yii::app()->event->dispatch('firm_changed', array('firm_id' => $model->firm_id));

                $firm = Firm::model()->findByPk($model->firm_id);
                $subspecialty_id = $firm->serviceSubspecialtyAssignment ? $firm->serviceSubspecialtyAssignment->subspecialty_id : null;

                if ($this->patient && $episode = $this->patient->getOpenEpisodeOfSubspecialty($subspecialty_id)) {
                    Yii::app()->session['episode_hide_status'] = array(
                        $episode->id => 1,
                    );
                }

                // Redirect browser to clear POST
                $this->controller->redirect($this->returnUrl);
                Yii::app()->end();
            }
        } else {
            $model->firm_id = Yii::app()->session['selected_firm_id'];
            $model->site_id = Yii::app()->session['selected_site_id'];
        }

        if (!$sites = $user->siteSelections) {
            $sites = Institution::model()->getCurrent()->sites;
        }

        $user_firm_ids = array();
        if (Yii::app()->params['profile_user_can_edit']) {
            // firm selections only apply when the user is able to change them.
            // if firms should be restricted then this should be done through UserFirmRights
            foreach ($user->firmSelections as $firm) {
                $user_firm_ids[] = $firm->id;
            }
        }

        $firms = array();
        if ($preferred_firms = $user->preferred_firms) {
            foreach ($preferred_firms as $preferred_firm) {
                if (
                    $preferred_firm->active &&
                    (count($user_firm_ids) === 0 || in_array($preferred_firm->id, $user_firm_ids)) &&
                    (!$this->subspecialty || ($preferred_firm->serviceSubspecialtyAssignment && $this->subspecialty->id == $preferred_firm->serviceSubspecialtyAssignment->subspecialty_id))
                ) {
                    if ($preferred_firm->serviceSubspecialtyAssignment) {
                        $firms['Recent'][$preferred_firm->id] = "$preferred_firm->name ({$preferred_firm->serviceSubspecialtyAssignment->subspecialty->name})";
                    } else {
                        $firms['Recent'][$preferred_firm->id] = "$preferred_firm->name";
                    }
                }
            }
        }

        foreach ($this->controller->firms as $firm_id => $firm_label) {
            if ((count($user_firm_ids) === 0 || in_array($firm_id,
                        $user_firm_ids)) && !isset($firms['Recent'][$firm_id])
            ) {
                $firm = Firm::model()->findByPk($firm_id);
                if ($firm instanceof Firm && $firm->active &&
                    (!$this->subspecialty || ($firm->serviceSubspecialtyAssignment && $firm->serviceSubspecialtyAssignment->subspecialty_id == $this->subspecialty->id))
                ) {
                    if ($preferred_firms) {
                        $firms['Other'][$firm_id] = $firm_label;
                    } else {
                        $firms[$firm_id] = $firm_label;
                    }
                }
            }
        }

        $this->render('SiteAndFirmWidget', array(
            'model' => $model,
            'firms' => $firms,
            'sites' => CHtml::listData($sites, 'id', 'short_name'),
        ));
    }
{'fraction_non_alphanumeric': 0.13697566027964786, 'fraction_numerical': 0.0069048852062834455, 'mean_word_length': 2.066155067478169, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 185, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, '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': '2355481', 'n_tokens_mistral': 3468, 'n_tokens_neox': 3141, 'n_words': 697}