<?php

class MemberController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout = '//layouts/column2';

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
			'postOnly + delete', // we only allow deletion via POST request
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array(
				'allow',
				'actions' => array('create', 'update', 'view', 'delete', 'admin', 'index', 'enable', 'disable'),
				'users' => array('@'),
				'expression' => 'Yii::app()->user->record->level==1',
			),
			array(
				'allow',
				'actions' => array('my', 'view', 'update', 'upload', 'create', 'update', 'delete'),
				'users' => array('@'),
			),
			array(
				'deny',
				'users' => array('*'),
			),
		);
	}


	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id, $type)
	{
		if (Yii::app()->request->isAjaxRequest) {
			$this->renderPartial('image', array(
				'model' => $this->loadModel($id),
				'type' => $type,
			), false, true);
		} else {
			$this->render('image', array(
				'model' => $this->loadModel($id),
				'type' => $type,
			));
		}
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model = new Member;
		$model->setScenario('create');

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if (isset($_POST['Member'])) {
			$model->attributes = $_POST['Member'];
			$model->create_time = date('Y-m-d h:i:s');
			// $model->status = 1;
			$model->team_id = YII::app()->user->id;

			$tmp;
			if (strlen(trim(CUploadedFile::getInstance($model, 'image_identity'))) > 0) {
				$tmp = CUploadedFile::getInstance($model, 'image_identity');
				$model->image_identity = uniqid() . "-" . time() . '.' . $tmp->extensionName;
			}

			$tmp2;
			if (strlen(trim(CUploadedFile::getInstance($model, 'image_foto'))) > 0) {
				$tmp2 = CUploadedFile::getInstance($model, 'image_foto');
				$model->image_foto = uniqid() . "-" . time() . '.' . $tmp2->extensionName;
			}


			$tmp3;
			if (strlen(trim(CUploadedFile::getInstance($model, 'image_sosmed'))) > 0) {
				$tmp3 = CUploadedFile::getInstance($model, 'image_sosmed');
				$model->image_sosmed = uniqid() . "-" . time() . '.' . $tmp3->extensionName;
			}

			if ($model->save()) {

				$team = $this->loadUsers($model->team_id);
				$team->status = 0;
				$team->update();

				$paper = $this->loadPaper($model->team_id);
				$paper->status = 0;
				$paper->update();

				// $description,$activityid,$type,$point,$part,$object
				Activities::model()->my("Peserta an. " . YII::app()->user->record->fullname . " Tambah Anggota", 1, 7, 1, 1, 1);

				if (strlen(trim($model->image_identity)) > 0) {
					$tmp->saveAs(Yii::getPathOfAlias('webroot') . '/image/files/identity/' . $model->image_identity);
				}

				if (strlen(trim($model->image_foto)) > 0) {
					$tmp2->saveAs(Yii::getPathOfAlias('webroot') . '/image/files/foto/' . $model->image_foto);
				}

				if (strlen(trim($model->image_sosmed)) > 0) {
					$tmp3->saveAs(Yii::getPathOfAlias('webroot') . '/image/files/sosmed/' . $model->image_sosmed);
				}

				$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('my'));
			}
		}

		$this->render('create', array(
			'model' => $model,
		));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$data = $this->loadModel($id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($data);

		if (isset($_POST['Member'])) {
			$data->attributes = $_POST['Member'];
			// $data->update_id = YII::app()->user->id;
			$data->update_time = date('Y-m-d h:i:s');
			if ($data->save()) {
				$team = $this->loadUsers($data->team_id);
				$team->status = 0;
				$team->update();

				$paper = $this->loadPaper($data->team_id);
				$paper->status = 0;
				$paper->update();

				// $description,$activityid,$type,$point,$part,$object
				Activities::model()->my("Peserta an. " . YII::app()->user->record->fullname . " Memperbaharui Data Peserta", 1, 7, 1, 1, 1);

				$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('my'));
			}
		}

		if (Yii::app()->request->isAjaxRequest) {
			$this->renderPartial('update', array(
				'data' => $data,
			), false, true);
		} else {
			$this->render('update', array(
				'data' => $data,
			));
		}
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		$this->loadModel($id)->delete();

		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
		if (!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('my'));
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$dataProvider = new CActiveDataProvider('Member');
		$this->render('index', array(
			'dataProvider' => $dataProvider,
		));
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model = new Member('search');
		$model->unsetAttributes();  // clear any default values
		if (isset($_GET['Member']))
			$model->attributes = $_GET['Member'];

		$data = new Member;
		if (isset($_POST['Member'])) {
			$data->attributes = $_POST['Member'];
			// $data->created_id = YII::app()->user->id;
			$data->created_time = date('Y-m-d h:i:s');
			// $data->status = 1;
			if ($data->save())
				$this->redirect(array('view', 'id' => $data->id_member));
		}

		$this->render('admin', array(
			'model' => $model,
			'data' => $data,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer $id the ID of the model to be loaded
	 * @return Member the loaded model
	 * @throws CHttpException
	 */
	public function loadModel($id)
	{
		$model = Member::model()->findByPk($id);
		if ($model === null)
			throw new CHttpException(404, 'The requested page does not exist.');
		return $model;
	}

	public function loadUsers($id)
	{
		$model = Users::model()->findByPk($id);
		if ($model === null)
			throw new CHttpException(404, 'The requested page does not exist.');
		return $model;
	}

	public function loadPaper($id)
	{
		$model = Paper::model()->findByAttributes(array('researchers_id' => $id));
		if ($model === null)
			throw new CHttpException(404, 'The requested page does not exist.');
		return $model;
	}


	/**
	 * Performs the AJAX validation.
	 * @param Member $model the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if (isset($_POST['ajax']) && $_POST['ajax'] === 'member-form') {
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}

	public function actionEnable($id)
	{
		$model = $this->loadModel($id);
		$model->status = 1;
		$model->save();
		$this->redirect(array('index'));
	}

	public function actionDisable($id)
	{
		$model = $this->loadModel($id);
		$model->status = 0;
		$model->save();
		$this->redirect(array('index'));
	}

	public function actionMy()
	{
		$data = new Member;
		$data->setScenario('create');
		if (isset($_POST['Member'])) {
			$data->attributes = $_POST['Member'];
			$data->team_id = YII::app()->user->id;
			$data->created_time = date('Y-m-d h:i:s');
			// $data->status = 1;

			$tmp;
			if (strlen(trim(CUploadedFile::getInstance($model, 'image_identity'))) > 0) {
				$tmp = CUploadedFile::getInstance($model, 'image_identity');
				$model->image_identity = uniqid() . "-" . time() . '.' . $tmp->extensionName;
			}

			$tmp2;
			if (strlen(trim(CUploadedFile::getInstance($model, 'image_foto'))) > 0) {
				$tmp2 = CUploadedFile::getInstance($model, 'image_foto');
				$model->image_foto = uniqid() . "-" . time() . '.' . $tmp->extensionName;
			}

			$tmp3;
			if (strlen(trim(CUploadedFile::getInstance($model, 'image_sosmed'))) > 0) {
				$tmp3 = CUploadedFile::getInstance($model, 'image_sosmed');
				$model->image_sosmed = uniqid() . "-" . time() . '.' . $tmp->extensionName;
			}

			if ($data->save()) {

				$team = $this->loadUsers($model->team_id);
				$team->status = 0;
				$team->update();

				$paper = $this->loadPaper($model->team_id);
				$paper->status = 0;
				$paper->update();

				if (strlen(trim($model->image_identity)) > 0) {
					$tmp->saveAs(Yii::getPathOfAlias('webroot') . '/image/files/identity/' . $model->image_identity);
				}

				if (strlen(trim($model->image_foto)) > 0) {
					$tmp2->saveAs(Yii::getPathOfAlias('webroot') . '/image/files/foto/' . $model->image_foto);
				}

				if (strlen(trim($model->image_sosmed)) > 0) {
					$tmp3->saveAs(Yii::getPathOfAlias('webroot') . '/image/files/sosmed/' . $model->image_sosmed);
				}


				$this->redirect(array('view', 'id' => $data->id_member));
			}
		}

		$dataProvider = new CActiveDataProvider('Member', array('criteria' => array('condition' => 'team_id=' . Yii::app()->user->record->id_user)));
		$this->render('my', array(
			'dataProvider' => $dataProvider,
			'data' => $data,
		));
	}


	public function actionUpload($id, $type)
	{
		$model = $this->loadModel($id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if (isset($_POST['Member'])) {
			$model->attributes = $_POST['Member'];
			// $model->update_id = YII::app()->user->id;
			$model->update_time = date('Y-m-d h:i:s');

			if ($type == 1) {
				$tmp;
				if (strlen(trim(CUploadedFile::getInstance($model, 'image_identity'))) > 0) {
					$tmp = CUploadedFile::getInstance($model, 'image_identity');
					$model->image_identity = uniqid() . "-" . time() . '.' . $tmp->extensionName;
				}
				// $description,$activityid,$type,$point,$part,$object
				Activities::model()->my("Peserta an. " . YII::app()->user->record->fullname . " Upload Ulang File KTP", 1, 7, 1, 1, 1);
			} elseif ($type == 2) {
				$tmp2;
				if (strlen(trim(CUploadedFile::getInstance($model, 'image_foto'))) > 0) {
					$tmp2 = CUploadedFile::getInstance($model, 'image_foto');
					$model->image_foto = uniqid() . "-" . time() . '.' . $tmp2->extensionName;
				}
				// $description,$activityid,$type,$point,$part,$object
				Activities::model()->my("Peserta an. " . YII::app()->user->record->fullname . " Upload Ulang File Pas Foto", 1, 7, 1, 1, 1);
			} elseif ($type == 3) {
				$tmp3;
				if (strlen(trim(CUploadedFile::getInstance($model, 'image_sosmed'))) > 0) {
					$tmp3 = CUploadedFile::getInstance($model, 'image_sosmed');
					$model->image_sosmed = uniqid() . "-" . time() . '.' . $tmp3->extensionName;
				}
				// $description,$activityid,$type,$point,$part,$object
				Activities::model()->my("Peserta an. " . YII::app()->user->record->fullname . " Upload Ulang File Bukti Follow Sosmed", 1, 7, 1, 1, 1);
			}


			if ($model->save()) {

				$team = $this->loadUsers($model->team_id);
				$team->status = 0;
				$team->update();

				$paper = $this->loadPaper($model->team_id);
				$paper->status = 0;
				$paper->update();

				if ($type == 1) {
					if (strlen(trim($model->image_identity)) > 0) {
						$tmp->saveAs(Yii::getPathOfAlias('webroot') . '/image/files/identity/' . $model->image_identity);
					}
				} elseif ($type == 2) {
					if (strlen(trim($model->image_foto)) > 0) {
						$tmp2->saveAs(Yii::getPathOfAlias('webroot') . '/image/files/foto/' . $model->image_foto);
					}
				} elseif ($type == 3) {
					if (strlen(trim($model->image_sosmed)) > 0) {
						$tmp3->saveAs(Yii::getPathOfAlias('webroot') . '/image/files/sosmed/' . $model->image_sosmed);
					}
				}

				$this->redirect(array('main/member/my'));
				// $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('my'));
			}
		}

		if (Yii::app()->request->isAjaxRequest) {
			$this->renderPartial('upload', array(
				'model' => $model,
				'type' => $type,
			), false, true);
		} else {
			$this->render('upload', array(
				'model' => $model,
				'type' => $type,
			));
		}
	}
}
