198 lines
7.4 KiB
PHP
198 lines
7.4 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace common\models;
|
||
|
|
|
||
|
|
use Yii;
|
||
|
|
use yii\behaviors\TimestampBehavior;
|
||
|
|
use yii\behaviors\BlameableBehavior;
|
||
|
|
use yii\helpers\FileHelper;
|
||
|
|
use yii\web\UploadedFile;
|
||
|
|
use Imagine\Image\Box;
|
||
|
|
use yii\imagine\Image;
|
||
|
|
|
||
|
|
|
||
|
|
/**
|
||
|
|
* This is the model class for table "person".
|
||
|
|
*
|
||
|
|
* @property int $id
|
||
|
|
* @property int $department_id
|
||
|
|
* @property int $person_type
|
||
|
|
* @property int $person_type_sub
|
||
|
|
* @property string $name
|
||
|
|
* @property string $birthdate
|
||
|
|
* @property int $gender
|
||
|
|
* @property string $position
|
||
|
|
* @property string $type_operation
|
||
|
|
* @property string $educate
|
||
|
|
* @property string $educate_major
|
||
|
|
* @property string $expert
|
||
|
|
* @property string $date_operate
|
||
|
|
* @property string $work_group
|
||
|
|
* @property int $created_at
|
||
|
|
* @property int $updated_at
|
||
|
|
* @property int $created_by
|
||
|
|
* @property int $updated_by
|
||
|
|
*
|
||
|
|
* @property Department $department
|
||
|
|
*/
|
||
|
|
class Person extends \yii\db\ActiveRecord
|
||
|
|
{
|
||
|
|
const gender_list = [1 => 'ชาย', 2 => 'หญิง'];
|
||
|
|
const person_type_list = [1 => 'แพทย์', 2 => 'แพทย์ประจำบ้าน', 3 => 'นักวิทยาศาสตร์', 4 => 'นักเซลล์วิทยา', 5 => 'อื่นๆ'];
|
||
|
|
|
||
|
|
public $uploadPhoto = 'uploads/person';
|
||
|
|
public $age;
|
||
|
|
|
||
|
|
|
||
|
|
public function behaviors()
|
||
|
|
{
|
||
|
|
return [
|
||
|
|
TimestampBehavior::className(),
|
||
|
|
BlameableBehavior::className()
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
public function beforeSave($insert)
|
||
|
|
{
|
||
|
|
$year = $this->getAttribute('date_operate');
|
||
|
|
if(!empty($year))
|
||
|
|
$this->setAttribute('date_operate', (int) $year-543);
|
||
|
|
return parent::beforeSave($insert); // TODO: Change the autogenerated stub
|
||
|
|
}
|
||
|
|
|
||
|
|
public function afterFind()
|
||
|
|
{
|
||
|
|
$year = $this->getAttribute('date_operate');
|
||
|
|
if(!empty($year))
|
||
|
|
$this->setAttribute('date_operate', (int)$year+543);
|
||
|
|
parent::afterFind(); // TODO: Change the autogenerated stub
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* {@inheritdoc}
|
||
|
|
*/
|
||
|
|
public static function tableName()
|
||
|
|
{
|
||
|
|
return 'person';
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* {@inheritdoc}
|
||
|
|
*/
|
||
|
|
public function rules()
|
||
|
|
{
|
||
|
|
return [
|
||
|
|
[['department_id', 'name', 'person_type', 'birthdate', 'gender', 'birthdate', 'date_operate', 'position', 'educate', 'educate_major', 'expert'], 'required'],
|
||
|
|
[['edu_year', 'department_id', 'gender', 'created_at', 'updated_at', 'created_by', 'updated_by', 'person_type'], 'integer'],
|
||
|
|
[['birthdate', 'date_operate'], 'safe'],
|
||
|
|
[['name'], 'string', 'max' => 300],
|
||
|
|
[['id_card'], 'string', 'max' => 45],
|
||
|
|
[['edu_place', 'edu_department_org'], 'string', 'max' => 200],
|
||
|
|
[['photo'], 'file', 'extensions' => 'jpg, png, gif', 'skipOnEmpty' => true],
|
||
|
|
[['position', 'type_operation', 'educate', 'educate_major', 'expert', 'work_group'], 'string', 'max' => 45],
|
||
|
|
[['department_id'], 'exist', 'skipOnError' => true, 'targetClass' => Department::className(), 'targetAttribute' => ['department_id' => 'id']],
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* {@inheritdoc}
|
||
|
|
*/
|
||
|
|
public function attributeLabels()
|
||
|
|
{
|
||
|
|
return [
|
||
|
|
'id' => 'ID',
|
||
|
|
'department_id' => 'หน่วยงาน',
|
||
|
|
'person_type' => 'ประเภทการปฏิบัติงาน',
|
||
|
|
'name' => 'ชื่อ นามสกุล',
|
||
|
|
'birthdate' => 'วันเดือนปีเกิด',
|
||
|
|
'gender' => 'เพศ',
|
||
|
|
'position' => 'ตำแหน่ง',
|
||
|
|
'type_operation' => 'ประเภท',
|
||
|
|
'educate' => 'วุฒิการศึกษา',
|
||
|
|
'educate_major' => 'สาขา',
|
||
|
|
'expert' => 'ความเชี่ยวชาญ (แพทย์)',
|
||
|
|
'date_operate' => 'ปีที่เริ่มปฏิบัติงาน',
|
||
|
|
'work_group' => 'กลุ่มงาน/ฝ่าย',
|
||
|
|
'created_at' => 'เพิ่มเมื่อ',
|
||
|
|
'updated_at' => 'แก้ไขเมื่อ',
|
||
|
|
'created_by' => 'เพิ่มโดย',
|
||
|
|
'updated_by' => 'แก้ไขโดย',
|
||
|
|
'photo' => 'รูปภาพ',
|
||
|
|
'edu_place' => 'สถานศึกษา (แพทย์ประจำบ้าน)',
|
||
|
|
'id_card' => 'หมายเลขบัตรประชาชน',
|
||
|
|
'edu_department_org' => 'หน่วยงานต้นสังกัด (แพทย์ประจำบ้าน)',
|
||
|
|
'edu_year' => 'ชั้นปีที่ศึกษา (แพทย์ประจำบ้าน)'
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
public function attributeHints()
|
||
|
|
{
|
||
|
|
return [
|
||
|
|
'position' => 'หากไม่มีในรายการให้พิมพ์เพิ่มใหม่ได้',
|
||
|
|
'type_operation' => 'หากไม่มีในรายการให้พิมพ์เพิ่มใหม่ได้',
|
||
|
|
'educate' => 'หากไม่มีในรายการให้พิมพ์เพิ่มใหม่ได้',
|
||
|
|
'educate_major' => 'หากไม่มีในรายการให้พิมพ์เพิ่มใหม่ได้',
|
||
|
|
'expert' => 'หากไม่มีในรายการให้พิมพ์เพิ่มใหม่ได้',
|
||
|
|
'work_group' => 'หากไม่มีในรายการให้พิมพ์เพิ่มใหม่ได้',
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @return \yii\db\ActiveQuery
|
||
|
|
*/
|
||
|
|
public function getDepartment()
|
||
|
|
{
|
||
|
|
return $this->hasOne(Department::className(), ['id' => 'department_id']);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function getCreatedBy()
|
||
|
|
{
|
||
|
|
return $this->hasOne(User::className(), ['id' => 'created_by']);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function getUpdatedBy()
|
||
|
|
{
|
||
|
|
return $this->hasOne(User::className(), ['id' => 'updated_by']);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function uploadPhoto($model, $attribute){
|
||
|
|
|
||
|
|
$photo = UploadedFile::getInstance($model, $attribute);
|
||
|
|
FileHelper::createDirectory($this->getPath());
|
||
|
|
if($photo){
|
||
|
|
if($this->isNewRecord || empty($this->getOldAttribute('photo'))){
|
||
|
|
$fileName = Yii::$app->user->identity->profile->department_id.'_'.time().'_'.substr(md5(rand(1,1000).time()),0,5).'.'.$photo->extension;
|
||
|
|
}else{
|
||
|
|
$fileName = $this->getOldAttribute('photo');
|
||
|
|
}
|
||
|
|
$photo->saveAs($this->getPath().'/'.$fileName);
|
||
|
|
|
||
|
|
Image::thumbnail($this->getPath().'/'.$fileName, 300, 400)
|
||
|
|
->resize(new Box(300, 400))
|
||
|
|
->save($this->getPath().'/'.$fileName);
|
||
|
|
|
||
|
|
return $fileName;
|
||
|
|
}//end image upload
|
||
|
|
|
||
|
|
return $this->isNewRecord ? false : $this->getOldAttribute($attribute); //ถ้าไม่มีการ upload ให้ใช้ข้อมูลเดิม
|
||
|
|
}
|
||
|
|
|
||
|
|
public function getPath()
|
||
|
|
{
|
||
|
|
return Yii::getAlias('@frontend').'/web/'.$this->uploadPhoto;
|
||
|
|
}
|
||
|
|
public function getPhotoUrl()
|
||
|
|
{
|
||
|
|
return Yii::getAlias('@web').'/'.$this->uploadPhoto;
|
||
|
|
}
|
||
|
|
public function getPhoto()
|
||
|
|
{
|
||
|
|
return empty($this->photo) ? 'http://via.placeholder.com/180x220' : Yii::getAlias('@web').'/'.$this->uploadPhoto.'/'.$this->photo;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function getAge()
|
||
|
|
{
|
||
|
|
return Yii::$app->hanuman->getAge($this->birthdate);
|
||
|
|
}
|
||
|
|
}
|