<commit_before><?php
/**
 * OpenEyes
 *
 * (C) Moorfields Eye Hospital NHS Foundation Trust, 2008-2011
 * (C) OpenEyes Foundation, 2011-2012
 * 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/>.
 *
 * @package OpenEyes
 * @link http://www.openeyes.org.uk
 * @author OpenEyes <<EMAIL>>
 * @copyright Copyright (c) 2008-2011, Moorfields Eye Hospital NHS Foundation Trust
 * @copyright Copyright (c) 2011-2012, OpenEyes Foundation
 * @license http://www.gnu.org/licenses/gpl-3.0.html The GNU General Public License V3.0
 */

class AdminController extends BaseAdminController
{
	public $layout = 'admin';
	public $items_per_page = 30;

	/**
	 * @var int
	 *
	 */
	public $displayOrder = 0;


	public function actionIndex()
	{
		$this->redirect(array('/admin/users'));
	}

	public function actionEditPreviousOperation()
	{
		$this->genericAdmin('Edit Previous Ophthalmic Surgery Choices', 'CommonPreviousOperation');
	}

	public function actionEditMedicationStopReason()
	{
		$this->genericAdmin('Edit Medication Stop Reason', 'MedicationStopReason');
	}

	public function actionEditCommonOphthalmicDisorderGroups()
	{
		$this->genericAdmin('Common Ophthalmic Disorder Groups', 'CommonOphthalmicDisorderGroup');
	}

	public function actionEditCommonOphthalmicDisorder()
	{
		$this->genericAdmin('Common Ophthalmic Disorder', 'CommonOphthalmicDisorder',
			array(
				'label_relation' => 'disorder',
				'label_field_type' => 'search_lookup',
				'label_field_model' => 'Disorder',
				'new_row_url' => Yii::app()->createUrl('/admin/newCommonOphthalmicDisorderRow'),
				'filter_fields' => array(
					array('field' => 'subspecialty_id', 'model' => 'Subspecialty'),
				),
				'extra_fields' => array(
					array(
						'field' => 'group_id',
						'type' => 'lookup',
						'model' => 'CommonOphthalmicDisorderGroup'
					),
					array(
						'field' => 'finding_id',
						'relation' => 'finding',
						'type' => 'search_lookup',
						'model' => 'Finding'
					),
					array(
						'field' => 'alternate_disorder_id',
						'relation' => 'alternate_disorder',
						'type' => 'search_lookup',
						'model' => 'Disorder',
					),
					array(
						'field' => 'alternate_disorder_label',
						'type' => 'text',
						'model' => 'CommonOphthalmicDisorder'
					)
				)
			));
	}

	public function actionNewCommonOphthalmicDisorderRow($key)
	{
		$this->genericAdmin('Common Ophthalmic Disorder', 'CommonOphthalmicDisorder',
			array(
				'label_relation' => 'disorder',
				'label_field_type' => 'search_lookup',
				'label_field_model' => 'Disorder',
				'new_row_url' => Yii::app()->createUrl('/admin/newCommonOphthalmicDisorderRow'),
				'filter_fields' => array(
					array('field' => 'subspecialty_id', 'model' => 'Subspecialty'),
				),
				'extra_fields' => array(
					array(
						'field' => 'group_id',
						'type' => 'lookup',
						'model' => 'CommonOphthalmicDisorderGroup'
					),
					array(
						'field' => 'finding_id',
						'relation' => 'finding',
						'type' => 'search_lookup',
						'model' => 'Finding'
					),
					array(
						'field' => 'alternate_disorder_id',
						'relation' => 'alternate_disorder',
						'type' => 'search_lookup',
						'model' => 'Disorder',
					),
					array(
						'field' => 'alternate_disorder_label',
						'type' => 'text',
						'model' => 'CommonOphthalmicDisorder'
					)
				)
			),
			$key
		);
	}

	public function actionEditSecondaryToCommonOphthalmicDisorder()
	{
		$this->genericAdmin('Secondary Common Ophthalmic Disorder', 'SecondaryToCommonOphthalmicDisorder',
			array(
				'label_relation' => 'disorder',
				'label_field_type' => 'search_lookup',
				'label_field_model' => 'Disorder',
				'new_row_url' => Yii::app()->createUrl('/admin/newsecondarytocommonophthalmicdisorderrow'),
				'filter_fields' => array(
					array('field' => 'parent_id', 'model' => 'CommonOphthalmicDisorder')
				),
				'extra_fields' => array(
					array(
						'field' => 'finding_id',
						'relation' => 'finding',
						'type' => 'search_lookup',
						'model' => 'Finding'
					),
					array(
						'field' => 'letter_macro_text',
						'type' => 'text',
					),
				)
			));
	}

	public function actionNewSecondaryToCommonOphthalmicDisorderRow($key)
	{
		$this->genericAdmin('Secondary Common Ophthalmic Disorder', 'SecondaryToCommonOphthalmicDisorder',
			array(
				'label_relation' => 'disorder',
				'label_field_type' => 'search_lookup',
				'label_field_model' => 'Disorder',
				'new_row_url' => Yii::app()->createUrl('/admin/newCommonOphthalmicDisorderRow'),
				'filter_fields' => array(
					array('field' => 'parent_id', 'model' => 'CommonOphthalmicDisorder')
				),
				'extra_fields' => array(
					array(
						'field' => 'finding_id',
						'relation' => 'finding',
						'type' => 'search_lookup',
						'model' => 'Finding'
					),
					array(
						'field' => 'letter_macro_text',
						'type' => 'text',
					),
				)
			),
			$key
		);
	}

	public function actionManageFindings()
	{
		$this->genericAdmin(
			'Findings',
			'Finding',
			array(
				'extra_fields' => array(
					array(
						'field' => 'subspecialties',
						'type' => 'multilookup',
						'noSelectionsMessage' => 'All Subspecialties',
						'htmlOptions' => array(
							'empty' => '- Please Select -',
							'nowrapper' => true
						),
						'options' => \CHtml::listData(\Subspecialty::model()->findAll(), 'id', 'name')
					),
					array(
						'field' => 'requires_description',
						'type' => 'boolean',
					),
				),
			)
		);
	}

	public function actionDrugs()
	{
		$criteria = new CDbCriteria;
		if (isset($_REQUEST['search'])) $criteria->compare('name', $_REQUEST['search'], true);
		$pagination = $this->initPagination(Drug::model(), $criteria);
		$this->render('/admin/drugs',array(
				'drugs' => Drug::model()->findAll($criteria),
				'pagination' => $pagination,
			));
	}

	public function actionAddDrug()
	{
		return; //disabled OE-4474
		$drug=new Drug('create');

		if (!empty($_POST)) {

			$drug->attributes = $_POST['Drug'];

			if (!$drug->validate()) {
				$errors = $drug->getErrors();
			} else {
				if (!$drug->save()) {
					throw new Exception("Unable to save drug: ".print_r($drug->getErrors(),true));
				}

				if(isset($_POST['allergies']))
				{
					$posted_allergy_ids = $_POST['allergies'];

					//add new allergy mappings
					foreach($posted_allergy_ids as $asign){
						$allergy_assignment = new DrugAllergyAssignment();
						$allergy_assignment->drug_id=$drug->id;
						$allergy_assignment->allergy_id=$asign;
						$allergy_assignment->save();
					}
				}

				$this->redirect('/admin/drugs/'.ceil($drug->id/$this->items_per_page));
			}
		}

		$this->render('/admin/adddrug',array(
				'drug' => $drug,
				'errors' => @$errors,
			));
	}

	public function actionEditDrug($id)
	{
		return; //disabled OE-4474
		if (!$drug = Drug::model()->findByPk($id)) {
			throw new Exception("Drug not found: $id");
		}
		$drug->scenario = 'update';

		if (!empty($_POST)) {

			$drug->attributes = $_POST['Drug'];

			if (!$drug->validate()) {
				$errors = $drug->getErrors();
			} else {
				if (!$drug->save()) {
					throw new Exception("Unable to save drug: ".print_r($drug->getErrors(),true));
				}

				$posted_allergy_ids = array();

				if(isset($_POST['allergies'])){
					$posted_allergy_ids = $_POST['allergies'];
				}

				$criteria=new CDbCriteria;
				$criteria->compare('drug_id',$drug->id);
				$allergy_assignments = DrugAllergyAssignment::model()->findAll($criteria);

				$allergy_assignment_ids = array();
				foreach($allergy_assignments as $allergy_assignment){
					$allergy_assignment_ids[]=$allergy_assignment->allergy_id;
				}

				$allergy_assignment_ids_to_delete = array_diff($allergy_assignment_ids,$posted_allergy_ids);
				$posted_allergy_ids_to_assign =  array_diff($posted_allergy_ids , $allergy_assignment_ids);

				//add new allergy mappings
				foreach($posted_allergy_ids_to_assign as $asign){
					$allergy_assignment = new DrugAllergyAssignment();
					$allergy_assignment->drug_id=$drug->id;
					$allergy_assignment->allergy_id=$asign;
					$allergy_assignment->save();
				}

				//delete redundant allergy mappings
				foreach($allergy_assignments as $asigned){
					if(in_array($asigned->allergy_id,$allergy_assignment_ids_to_delete)){
						$asigned->delete();
					}
				}

				$this->redirect('/admin/drugs/'.ceil($drug->id/$this->items_per_page));
			}
		}

		$this->render('/admin/editdrug',array(
				'drug' => $drug,
				'errors' => @$errors,
			));
	}

	public function actionUserFind()
	{
		$res = array();
		if (Yii::app()->request->isAjaxRequest && !empty($_REQUEST['search'])) {
			$criteria = new CDbCriteria;
			$criteria->compare("LOWER(username)", strtolower($_REQUEST['search']),true, 'OR');
			$criteria->compare("LOWER(first_name)",strtolower($_REQUEST['search']),true, 'OR');
			$criteria->compare("LOWER(last_name)",strtolower($_REQUEST['search']),true, 'OR');
			foreach (User::model()->findAll($criteria) as $user) {
				$res[] = array(
					'id' => $user->id,
					'label' => $user->getFullName() . '(' . $user->username . ')',
					'value' => $user->getFullName(),
					'username' => $user->username,
				);
			}
		}
		echo CJSON::encode($res);
	}

	public function actionUsers($id=false)
	{
		Audit::add('admin-User','list');

		$criteria = new CDbCriteria;
		if (!empty($_POST['search'])) {
			$criteria->compare("LOWER(username)", strtolower($_POST['search']),true, 'OR');
			$criteria->compare("LOWER(first_name)",strtolower($_POST['search']),true, 'OR');
			$criteria->compare("LOWER(last_name)",strtolower($_POST['search']),true, 'OR');
		}

		$pagination = $this->initPagination(User::model(), $criteria);

		$this->render('/admin/users',array(
			'users' => User::model()->findAll($criteria),
			'pagination' => $pagination,
		));
	}

	public function actionAddUser()
	{
		$user = new User;
		$request = Yii::app()->getRequest();

		if ($request->getIsPostRequest()) {
			$userAtt = $request->getPost( 'User');
			$user->attributes = $userAtt;

			if (!$user->validate()) {
				$errors = $user->getErrors();
			} else {
				if (!$user->save()) {
					throw new Exception("Unable to save user: ".print_r($user->getErrors(),true));
				}
				Audit::add('admin-User','add',$user->id);

				if(!isset($userAtt['roles'])){
					$userAtt['roles']=array();
				}

				$user->saveRoles($userAtt['roles']);

				$this->redirect('/admin/users/'.ceil($user->id/$this->items_per_page));
			}
		}

		$user->password = '';

		$this->render('/admin/adduser',array(
			'user' => $user,
			'errors' => @$errors,
		));
	}

	public function actionEditUser($id)
	{
		if (!$user = User::model()->findByPk($id)) {
			throw new Exception("User not found: $id");
		}

		$request = Yii::app()->getRequest();

		if ($request->getIsPostRequest()) {
			$userAtt = $request->getPost( 'User');
			if(empty($userAtt['password'])){
				unset($userAtt['password']);
			}
			$user->attributes = $userAtt;

			if (!$user->validate()) {
				$errors = $user->getErrors();
			} else {

				if($user->is_doctor != 1)
					$user->doctor_grade_id = '';

				if (!$user->save()) {
					throw new Exception("Unable to save user: ".print_r($user->getErrors(),true));
				}

				if (!$contact = $user->contact) {
					$contact = new Contact;
				}

				$contact->title = $user->title;
				$contact->first_name = $user->first_name;
				$contact->last_name = $user->last_name;
				$contact->qualifications = $user->qualifications;

				if (!$contact->save()) {
					throw new Exception("Unable to save user contact: ".print_r($contact->getErrors(),true));
				}

				if (!$user->contact) {
					$user->contact_id = $contact->id;



					if (!$user->save()) {
						throw new Exception("Unable to save user: ".print_r($user->getErrors(),true));
					}<commit_msg>OE-5540  ::  Added if { }<commit_after><?php
/**
 * OpenEyes
 *
 * (C) Moorfields Eye Hospital NHS Foundation Trust, 2008-2011
 * (C) OpenEyes Foundation, 2011-2012
 * 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/>.
 *
 * @package OpenEyes
 * @link http://www.openeyes.org.uk
 * @author OpenEyes <<EMAIL>>
 * @copyright Copyright (c) 2008-2011, Moorfields Eye Hospital NHS Foundation Trust
 * @copyright Copyright (c) 2011-2012, OpenEyes Foundation
 * @license http://www.gnu.org/licenses/gpl-3.0.html The GNU General Public License V3.0
 */

class AdminController extends BaseAdminController
{
	public $layout = 'admin';
	public $items_per_page = 30;

	/**
	 * @var int
	 *
	 */
	public $displayOrder = 0;


	public function actionIndex()
	{
		$this->redirect(array('/admin/users'));
	}

	public function actionEditPreviousOperation()
	{
		$this->genericAdmin('Edit Previous Ophthalmic Surgery Choices', 'CommonPreviousOperation');
	}

	public function actionEditMedicationStopReason()
	{
		$this->genericAdmin('Edit Medication Stop Reason', 'MedicationStopReason');
	}

	public function actionEditCommonOphthalmicDisorderGroups()
	{
		$this->genericAdmin('Common Ophthalmic Disorder Groups', 'CommonOphthalmicDisorderGroup');
	}

	public function actionEditCommonOphthalmicDisorder()
	{
		$this->genericAdmin('Common Ophthalmic Disorder', 'CommonOphthalmicDisorder',
			array(
				'label_relation' => 'disorder',
				'label_field_type' => 'search_lookup',
				'label_field_model' => 'Disorder',
				'new_row_url' => Yii::app()->createUrl('/admin/newCommonOphthalmicDisorderRow'),
				'filter_fields' => array(
					array('field' => 'subspecialty_id', 'model' => 'Subspecialty'),
				),
				'extra_fields' => array(
					array(
						'field' => 'group_id',
						'type' => 'lookup',
						'model' => 'CommonOphthalmicDisorderGroup'
					),
					array(
						'field' => 'finding_id',
						'relation' => 'finding',
						'type' => 'search_lookup',
						'model' => 'Finding'
					),
					array(
						'field' => 'alternate_disorder_id',
						'relation' => 'alternate_disorder',
						'type' => 'search_lookup',
						'model' => 'Disorder',
					),
					array(
						'field' => 'alternate_disorder_label',
						'type' => 'text',
						'model' => 'CommonOphthalmicDisorder'
					)
				)
			));
	}

	public function actionNewCommonOphthalmicDisorderRow($key)
	{
		$this->genericAdmin('Common Ophthalmic Disorder', 'CommonOphthalmicDisorder',
			array(
				'label_relation' => 'disorder',
				'label_field_type' => 'search_lookup',
				'label_field_model' => 'Disorder',
				'new_row_url' => Yii::app()->createUrl('/admin/newCommonOphthalmicDisorderRow'),
				'filter_fields' => array(
					array('field' => 'subspecialty_id', 'model' => 'Subspecialty'),
				),
				'extra_fields' => array(
					array(
						'field' => 'group_id',
						'type' => 'lookup',
						'model' => 'CommonOphthalmicDisorderGroup'
					),
					array(
						'field' => 'finding_id',
						'relation' => 'finding',
						'type' => 'search_lookup',
						'model' => 'Finding'
					),
					array(
						'field' => 'alternate_disorder_id',
						'relation' => 'alternate_disorder',
						'type' => 'search_lookup',
						'model' => 'Disorder',
					),
					array(
						'field' => 'alternate_disorder_label',
						'type' => 'text',
						'model' => 'CommonOphthalmicDisorder'
					)
				)
			),
			$key
		);
	}

	public function actionEditSecondaryToCommonOphthalmicDisorder()
	{
		$this->genericAdmin('Secondary Common Ophthalmic Disorder', 'SecondaryToCommonOphthalmicDisorder',
			array(
				'label_relation' => 'disorder',
				'label_field_type' => 'search_lookup',
				'label_field_model' => 'Disorder',
				'new_row_url' => Yii::app()->createUrl('/admin/newsecondarytocommonophthalmicdisorderrow'),
				'filter_fields' => array(
					array('field' => 'parent_id', 'model' => 'CommonOphthalmicDisorder')
				),
				'extra_fields' => array(
					array(
						'field' => 'finding_id',
						'relation' => 'finding',
						'type' => 'search_lookup',
						'model' => 'Finding'
					),
					array(
						'field' => 'letter_macro_text',
						'type' => 'text',
					),
				)
			));
	}

	public function actionNewSecondaryToCommonOphthalmicDisorderRow($key)
	{
		$this->genericAdmin('Secondary Common Ophthalmic Disorder', 'SecondaryToCommonOphthalmicDisorder',
			array(
				'label_relation' => 'disorder',
				'label_field_type' => 'search_lookup',
				'label_field_model' => 'Disorder',
				'new_row_url' => Yii::app()->createUrl('/admin/newCommonOphthalmicDisorderRow'),
				'filter_fields' => array(
					array('field' => 'parent_id', 'model' => 'CommonOphthalmicDisorder')
				),
				'extra_fields' => array(
					array(
						'field' => 'finding_id',
						'relation' => 'finding',
						'type' => 'search_lookup',
						'model' => 'Finding'
					),
					array(
						'field' => 'letter_macro_text',
						'type' => 'text',
					),
				)
			),
			$key
		);
	}

	public function actionManageFindings()
	{
		$this->genericAdmin(
			'Findings',
			'Finding',
			array(
				'extra_fields' => array(
					array(
						'field' => 'subspecialties',
						'type' => 'multilookup',
						'noSelectionsMessage' => 'All Subspecialties',
						'htmlOptions' => array(
							'empty' => '- Please Select -',
							'nowrapper' => true
						),
						'options' => \CHtml::listData(\Subspecialty::model()->findAll(), 'id', 'name')
					),
					array(
						'field' => 'requires_description',
						'type' => 'boolean',
					),
				),
			)
		);
	}

	public function actionDrugs()
	{
		$criteria = new CDbCriteria;
		if (isset($_REQUEST['search'])) $criteria->compare('name', $_REQUEST['search'], true);
		$pagination = $this->initPagination(Drug::model(), $criteria);
		$this->render('/admin/drugs',array(
				'drugs' => Drug::model()->findAll($criteria),
				'pagination' => $pagination,
			));
	}

	public function actionAddDrug()
	{
		return; //disabled OE-4474
		$drug=new Drug('create');

		if (!empty($_POST)) {

			$drug->attributes = $_POST['Drug'];

			if (!$drug->validate()) {
				$errors = $drug->getErrors();
			} else {
				if (!$drug->save()) {
					throw new Exception("Unable to save drug: ".print_r($drug->getErrors(),true));
				}

				if(isset($_POST['allergies']))
				{
					$posted_allergy_ids = $_POST['allergies'];

					//add new allergy mappings
					foreach($posted_allergy_ids as $asign){
						$allergy_assignment = new DrugAllergyAssignment();
						$allergy_assignment->drug_id=$drug->id;
						$allergy_assignment->allergy_id=$asign;
						$allergy_assignment->save();
					}
				}

				$this->redirect('/admin/drugs/'.ceil($drug->id/$this->items_per_page));
			}
		}

		$this->render('/admin/adddrug',array(
				'drug' => $drug,
				'errors' => @$errors,
			));
	}

	public function actionEditDrug($id)
	{
		return; //disabled OE-4474
		if (!$drug = Drug::model()->findByPk($id)) {
			throw new Exception("Drug not found: $id");
		}
		$drug->scenario = 'update';

		if (!empty($_POST)) {

			$drug->attributes = $_POST['Drug'];

			if (!$drug->validate()) {
				$errors = $drug->getErrors();
			} else {
				if (!$drug->save()) {
					throw new Exception("Unable to save drug: ".print_r($drug->getErrors(),true));
				}

				$posted_allergy_ids = array();

				if(isset($_POST['allergies'])){
					$posted_allergy_ids = $_POST['allergies'];
				}

				$criteria=new CDbCriteria;
				$criteria->compare('drug_id',$drug->id);
				$allergy_assignments = DrugAllergyAssignment::model()->findAll($criteria);

				$allergy_assignment_ids = array();
				foreach($allergy_assignments as $allergy_assignment){
					$allergy_assignment_ids[]=$allergy_assignment->allergy_id;
				}

				$allergy_assignment_ids_to_delete = array_diff($allergy_assignment_ids,$posted_allergy_ids);
				$posted_allergy_ids_to_assign =  array_diff($posted_allergy_ids , $allergy_assignment_ids);

				//add new allergy mappings
				foreach($posted_allergy_ids_to_assign as $asign){
					$allergy_assignment = new DrugAllergyAssignment();
					$allergy_assignment->drug_id=$drug->id;
					$allergy_assignment->allergy_id=$asign;
					$allergy_assignment->save();
				}

				//delete redundant allergy mappings
				foreach($allergy_assignments as $asigned){
					if(in_array($asigned->allergy_id,$allergy_assignment_ids_to_delete)){
						$asigned->delete();
					}
				}

				$this->redirect('/admin/drugs/'.ceil($drug->id/$this->items_per_page));
			}
		}

		$this->render('/admin/editdrug',array(
				'drug' => $drug,
				'errors' => @$errors,
			));
	}

	public function actionUserFind()
	{
		$res = array();
		if (Yii::app()->request->isAjaxRequest && !empty($_REQUEST['search'])) {
			$criteria = new CDbCriteria;
			$criteria->compare("LOWER(username)", strtolower($_REQUEST['search']),true, 'OR');
			$criteria->compare("LOWER(first_name)",strtolower($_REQUEST['search']),true, 'OR');
			$criteria->compare("LOWER(last_name)",strtolower($_REQUEST['search']),true, 'OR');
			foreach (User::model()->findAll($criteria) as $user) {
				$res[] = array(
					'id' => $user->id,
					'label' => $user->getFullName() . '(' . $user->username . ')',
					'value' => $user->getFullName(),
					'username' => $user->username,
				);
			}
		}
		echo CJSON::encode($res);
	}

	public function actionUsers($id=false)
	{
		Audit::add('admin-User','list');

		$criteria = new CDbCriteria;
		if (!empty($_POST['search'])) {
			$criteria->compare("LOWER(username)", strtolower($_POST['search']),true, 'OR');
			$criteria->compare("LOWER(first_name)",strtolower($_POST['search']),true, 'OR');
			$criteria->compare("LOWER(last_name)",strtolower($_POST['search']),true, 'OR');
		}

		$pagination = $this->initPagination(User::model(), $criteria);

		$this->render('/admin/users',array(
			'users' => User::model()->findAll($criteria),
			'pagination' => $pagination,
		));
	}

	public function actionAddUser()
	{
		$user = new User;
		$request = Yii::app()->getRequest();

		if ($request->getIsPostRequest()) {
			$userAtt = $request->getPost( 'User');
			$user->attributes = $userAtt;

			if (!$user->validate()) {
				$errors = $user->getErrors();
			} else {
				if (!$user->save()) {
					throw new Exception("Unable to save user: ".print_r($user->getErrors(),true));
				}
				Audit::add('admin-User','add',$user->id);

				if(!isset($userAtt['roles'])){
					$userAtt['roles']=array();
				}

				$user->saveRoles($userAtt['roles']);

				$this->redirect('/admin/users/'.ceil($user->id/$this->items_per_page));
			}
		}

		$user->password = '';

		$this->render('/admin/adduser',array(
			'user' => $user,
			'errors' => @$errors,
		));
	}

	public function actionEditUser($id)
	{
		if (!$user = User::model()->findByPk($id)) {
			throw new Exception("User not found: $id");
		}

		$request = Yii::app()->getRequest();

		if ($request->getIsPostRequest()) {
			$userAtt = $request->getPost( 'User');
			if(empty($userAtt['password'])){
				unset($userAtt['password']);
			}
			$user->attributes = $userAtt;

			if (!$user->validate()) {
				$errors = $user->getErrors();
			} else {

				if($user->is_doctor != 1) {
					$user->doctor_grade_id = '';
				}

				if (!$user->save()) {
					throw new Exception("Unable to save user: ".print_r($user->getErrors(),true));
				}

				if (!$contact = $user->contact) {
					$contact = new Contact;
				}

				$contact->title = $user->title;
				$contact->first_name = $user->first_name;
				$contact->last_name = $user->last_name;
				$contact->qualifications = $user->qualifications;

				if (!$contact->save()) {
					throw new Exception("Unable to save user contact: ".print_r($contact->getErrors(),true));
				}

				if (!$user->contact) {
					$user->contact_id = $contact->id;



					if (!$user->save()) {
						throw new Exception("Unable to save user: ".print_r($user->getErrors(),true));
					}
{'fraction_non_alphanumeric': 0.1807738814993954, 'fraction_numerical': 0.004111245465538089, 'mean_word_length': 3.6786724495568546, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 423, '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': '3941758', 'n_tokens_mistral': 10420, 'n_tokens_neox': 8141, 'n_words': 1499}