cập nhật tính năng quản trị phòng ban
This commit is contained in:
111
models/Department.php
Normal file
111
models/Department.php
Normal file
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
|
||||
namespace app\models;
|
||||
|
||||
use Yii;
|
||||
|
||||
/**
|
||||
* This is the model class for table "department".
|
||||
*
|
||||
* @property int $id
|
||||
* @property int $code
|
||||
* @property int $pid
|
||||
* @property string $name
|
||||
* @property int $created_at
|
||||
* @property int $modified_at
|
||||
*/
|
||||
class Department extends \yii\db\ActiveRecord {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function tableName() {
|
||||
return 'department';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function rules() {
|
||||
return [
|
||||
[['code', 'pid', 'name', 'created_at', 'modified_at'], 'required'],
|
||||
[['code', 'pid', 'created_at', 'modified_at'], 'integer'],
|
||||
[['name'], 'string', 'max' => 100],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function attributeLabels() {
|
||||
return [
|
||||
'id' => 'ID',
|
||||
'code' => 'Mã phòng ban',
|
||||
'pid' => 'Trực thuộc',
|
||||
'name' => 'Tên phòng ban',
|
||||
'created_at' => 'Thời gian tạo',
|
||||
'modified_at' => 'Thời gian sửa',
|
||||
];
|
||||
}
|
||||
|
||||
public static function departmentArray() {
|
||||
$lists = self::find()->all();
|
||||
$results = [];
|
||||
foreach ($lists as $key => $value) {
|
||||
$results[$value->code] = $value->name;
|
||||
}
|
||||
return $results;
|
||||
}
|
||||
|
||||
public function create($data) {
|
||||
$r = $this->load([
|
||||
"code" => $data["Code"],
|
||||
"pid" => $data["Pid"],
|
||||
"name" => $data["Name"],
|
||||
"created_at" => time(),
|
||||
"modified_at" => time()
|
||||
], '');
|
||||
if ($r) {
|
||||
try {
|
||||
$this->save();
|
||||
return $this->id;
|
||||
} catch (\Exception $ex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function multiCreate($datas) {
|
||||
$field = ['code', 'pid', 'name', 'created_at', 'modified_at'];
|
||||
static::getDb()->createCommand()->batchInsert($this->tableName(), $field, $datas)->execute();
|
||||
return;
|
||||
}
|
||||
|
||||
public static function deleteDepartment($id) {
|
||||
if ($id == 1)
|
||||
return;
|
||||
$model = self::findOne($id);
|
||||
if ($model) {
|
||||
$childs = self::find()->andWhere(["pid" => $model->code])->all();
|
||||
foreach ($childs as $key => $value) {
|
||||
self::deleteDepartment($value->id);
|
||||
}
|
||||
$model->delete();
|
||||
self::insertSystemLogs(["action" => "delete", "description" => "Xóa phòng ban: " . $model->name]);
|
||||
}
|
||||
}
|
||||
|
||||
public static function insertSystemLogs($data) {
|
||||
$model = new LogsDepartment();
|
||||
return $model->create($data);
|
||||
}
|
||||
|
||||
public function checkStatusImport($data) {
|
||||
if ($this->findOne(["code" => $data["A"]]))
|
||||
return ["status" => false, "description" => "Mã phòng ban đã tồn tại"];
|
||||
if (!$this->findOne(["name" => $data["C"]]))
|
||||
return ["status" => true, "description" => "Phòng ban trực thuộc không tồn tại"];
|
||||
return ["status" => true, "description" => ""];
|
||||
}
|
||||
|
||||
}
|
||||
71
models/DepartmentSearch.php
Normal file
71
models/DepartmentSearch.php
Normal file
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
namespace app\models;
|
||||
|
||||
use Yii;
|
||||
use yii\base\Model;
|
||||
use yii\data\ActiveDataProvider;
|
||||
use app\models\Department;
|
||||
|
||||
/**
|
||||
* DepartmentSearch represents the model behind the search form of `app\models\Department`.
|
||||
*/
|
||||
class DepartmentSearch extends Department {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function rules() {
|
||||
return [
|
||||
[['id', 'code', 'pid'], 'integer'],
|
||||
[['name'], 'safe'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function scenarios() {
|
||||
// bypass scenarios() implementation in the parent class
|
||||
return Model::scenarios();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates data provider instance with search query applied
|
||||
*
|
||||
* @param array $params
|
||||
*
|
||||
* @return ActiveDataProvider
|
||||
*/
|
||||
public function search($params) {
|
||||
$query = Department::find();
|
||||
|
||||
// add conditions that should always apply here
|
||||
|
||||
$dataProvider = new ActiveDataProvider([
|
||||
'query' => $query,
|
||||
]);
|
||||
|
||||
$this->load($params);
|
||||
|
||||
if (!$this->validate()) {
|
||||
// uncomment the following line if you do not want to return any records when validation fails
|
||||
// $query->where('0=1');
|
||||
return $dataProvider;
|
||||
}
|
||||
|
||||
// grid filtering conditions
|
||||
$query->andFilterWhere([
|
||||
'id' => $this->id,
|
||||
'code' => $this->code,
|
||||
'pid' => $this->pid,
|
||||
'created_at' => $this->created_at,
|
||||
'modified_at' => $this->modified_at,
|
||||
]);
|
||||
|
||||
$query->andFilterWhere(['like', 'name', $this->name]);
|
||||
|
||||
return $dataProvider;
|
||||
}
|
||||
|
||||
}
|
||||
67
models/LogsDepartment.php
Normal file
67
models/LogsDepartment.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace app\models;
|
||||
|
||||
use Yii;
|
||||
|
||||
/**
|
||||
* This is the model class for table "logs_department".
|
||||
*
|
||||
* @property int $id
|
||||
* @property int $user_id
|
||||
* @property int $time
|
||||
* @property string $action
|
||||
* @property string $description
|
||||
*/
|
||||
class LogsDepartment extends \yii\db\ActiveRecord {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function tableName() {
|
||||
return 'logs_department';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function rules() {
|
||||
return [
|
||||
[['user_id', 'time', 'action', 'description'], 'required'],
|
||||
[['user_id', 'time'], 'integer'],
|
||||
[['description'], 'string'],
|
||||
[['action'], 'string', 'max' => 10],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function attributeLabels() {
|
||||
return [
|
||||
'id' => 'ID',
|
||||
'user_id' => 'User ID',
|
||||
'time' => 'Time',
|
||||
'action' => 'Action',
|
||||
'description' => 'Description',
|
||||
];
|
||||
}
|
||||
|
||||
public function create($data) {
|
||||
$r = $this->load([
|
||||
'user_id' => Yii::$app->user->id,
|
||||
'time' => time(),
|
||||
'action' => $data['action'],
|
||||
'description' => $data['description']
|
||||
], '');
|
||||
if ($r) {
|
||||
try {
|
||||
$this->save();
|
||||
return $this->id;
|
||||
} catch (\Exception $ex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -201,4 +201,13 @@ class User extends ActiveRecord implements \yii\web\IdentityInterface {
|
||||
return $this->user_image == null ? $directoryAsset . "/img/user2-160x160.jpg" : Yii::getAlias("@images_folder") . $this->user_image;
|
||||
}
|
||||
|
||||
public static function userArray() {
|
||||
$lists = self::find()->all();
|
||||
$results = [];
|
||||
foreach ($lists as $key => $value) {
|
||||
$results[$value->id] = $value->first_name;
|
||||
}
|
||||
return $results;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -56,13 +56,13 @@ class common extends \yii\db\ActiveRecord {
|
||||
}
|
||||
|
||||
public function formatTime($time, $format = "d/m/Y") {
|
||||
// $now = time();
|
||||
// $range = $now - $time;
|
||||
// if ($range > 60 * 60 * 12) {
|
||||
$now = time();
|
||||
$range = $now - $time;
|
||||
if ($range > 60 * 60 * 12) {
|
||||
return date($format, $time);
|
||||
// } else {
|
||||
// return Yii::$app->formatter->asRelativeTime($time);
|
||||
// }
|
||||
} else {
|
||||
return Yii::$app->formatter->asRelativeTime($time);
|
||||
}
|
||||
}
|
||||
|
||||
//Upload
|
||||
|
||||
Reference in New Issue
Block a user