added services, added documentation
This commit is contained in:
@ -110,13 +110,4 @@ class Document extends \yii\db\ActiveRecord
|
||||
{
|
||||
return $this->hasMany(DocumentFieldValue::className(), ['document_id' => 'id']);
|
||||
}
|
||||
|
||||
public static function getDocument($document_id)
|
||||
{
|
||||
return self::find()
|
||||
->joinWith(['documentFieldValues.field'])
|
||||
->where(['document.id' => $document_id])
|
||||
->asArray()
|
||||
->all();
|
||||
}
|
||||
}
|
||||
|
@ -123,4 +123,10 @@ class Template extends \yii\db\ActiveRecord
|
||||
{
|
||||
return $this->title;
|
||||
}
|
||||
|
||||
public function getFields()
|
||||
{
|
||||
return $this->hasMany(DocumentField::className(), ['id' => 'field_id'])
|
||||
->via('templateDocumentFields');
|
||||
}
|
||||
}
|
||||
|
74
common/services/DocumentFileService.php
Normal file
74
common/services/DocumentFileService.php
Normal file
@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace common\services;
|
||||
|
||||
|
||||
use common\models\Document;
|
||||
use PhpOffice\PhpWord\Exception\CopyFileException;
|
||||
use PhpOffice\PhpWord\Exception\CreateTemporaryFileException;
|
||||
use PhpOffice\PhpWord\TemplateProcessor;
|
||||
use Yii;
|
||||
|
||||
class DocumentFileService
|
||||
{
|
||||
private $model;
|
||||
private $document;
|
||||
private $file_title;
|
||||
private $documentFieldValuesArr;
|
||||
|
||||
/**
|
||||
* @throws CopyFileException
|
||||
* @throws CreateTemporaryFileException
|
||||
*/
|
||||
public function __construct($modelID)
|
||||
{
|
||||
$this->model = Document::findOne($modelID);
|
||||
|
||||
$this->initDocument();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws CopyFileException
|
||||
* @throws CreateTemporaryFileException
|
||||
*/
|
||||
private function initDocument()
|
||||
{
|
||||
$this->file_title = $this->model->title . '.docx';
|
||||
|
||||
$template_title = $this->model->template->template_file_name;
|
||||
$this->document = new TemplateProcessor(
|
||||
Yii::getAlias('@templates') . "/$template_title");
|
||||
|
||||
$this->documentFieldValuesArr = $this->model->documentFieldValues;
|
||||
}
|
||||
|
||||
public function setFields()
|
||||
{
|
||||
foreach ($this->documentFieldValuesArr as $docFieldValue) {
|
||||
$this->document->setValue(
|
||||
$docFieldValue->field->field_template,
|
||||
$docFieldValue->value
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function downloadDocument()
|
||||
{
|
||||
$this->document->saveAs($this->file_title);
|
||||
|
||||
// Имя скачиваемого файла
|
||||
$downloadFile = $this->file_title;
|
||||
// Контент-тип означающий скачивание
|
||||
header("Content-Type: application/octet-stream");
|
||||
// Размер в байтах
|
||||
header("Accept-Ranges: bytes");
|
||||
// Размер файла
|
||||
header("Content-Length: ".filesize($downloadFile));
|
||||
// Расположение скачиваемого файла
|
||||
header("Content-Disposition: attachment; filename=".$downloadFile);
|
||||
|
||||
// Прочитать файл
|
||||
readfile($downloadFile);
|
||||
unlink($this->file_title);
|
||||
}
|
||||
}
|
@ -2,73 +2,27 @@
|
||||
|
||||
namespace common\services;
|
||||
|
||||
|
||||
use common\models\Document;
|
||||
use PhpOffice\PhpWord\Exception\CopyFileException;
|
||||
use PhpOffice\PhpWord\Exception\CreateTemporaryFileException;
|
||||
use PhpOffice\PhpWord\TemplateProcessor;
|
||||
use Yii;
|
||||
|
||||
class DocumentService
|
||||
{
|
||||
private $model;
|
||||
private $document;
|
||||
private $file_title;
|
||||
private $documentFieldValuesArr;
|
||||
|
||||
/**
|
||||
* @throws CopyFileException
|
||||
* @throws CreateTemporaryFileException
|
||||
*/
|
||||
public function __construct($modelID)
|
||||
public static function getDocumentList($document_type): array
|
||||
{
|
||||
$this->model = Document::findOne($modelID);
|
||||
|
||||
$this->initDocument();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws CopyFileException
|
||||
* @throws CreateTemporaryFileException
|
||||
*/
|
||||
private function initDocument()
|
||||
{
|
||||
$this->file_title = $this->model->title . '.docx';
|
||||
|
||||
$template_title = $this->model->template->template_file_name;
|
||||
$this->document = new TemplateProcessor(
|
||||
Yii::getAlias('@templates') . "/$template_title");
|
||||
|
||||
$this->documentFieldValuesArr = $this->model->documentFieldValues;
|
||||
}
|
||||
|
||||
public function setFields()
|
||||
{
|
||||
foreach ($this->documentFieldValuesArr as $docFieldValue) {
|
||||
$this->document->setValue(
|
||||
$docFieldValue->field->field_template,
|
||||
$docFieldValue->value
|
||||
);
|
||||
if (!empty($document_type)) {
|
||||
return Document::find()->joinWith('template')
|
||||
->where(['document_type' => $document_type])->asArray()->all();
|
||||
}
|
||||
else {
|
||||
return Document::find()->asArray()->all();
|
||||
}
|
||||
}
|
||||
|
||||
public function downloadDocument()
|
||||
public static function getDocument($document_id)
|
||||
{
|
||||
$this->document->saveAs($this->file_title);
|
||||
return Document::find()
|
||||
->joinWith(['documentFieldValues.field'])
|
||||
->where(['document.id' => $document_id])
|
||||
->asArray()->all();
|
||||
|
||||
// Имя скачиваемого файла
|
||||
$downloadFile = $this->file_title;
|
||||
// Контент-тип означающий скачивание
|
||||
header("Content-Type: application/octet-stream");
|
||||
// Размер в байтах
|
||||
header("Accept-Ranges: bytes");
|
||||
// Размер файла
|
||||
header("Content-Length: ".filesize($downloadFile));
|
||||
// Расположение скачиваемого файла
|
||||
header("Content-Disposition: attachment; filename=".$downloadFile);
|
||||
|
||||
// Прочитать файл
|
||||
readfile($downloadFile);
|
||||
unlink($this->file_title);
|
||||
}
|
||||
}
|
35
common/services/ManagerService.php
Normal file
35
common/services/ManagerService.php
Normal file
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace common\services;
|
||||
|
||||
use common\models\UserCard;
|
||||
|
||||
class ManagerService
|
||||
{
|
||||
public static function getManagerList()
|
||||
{
|
||||
return UserCard::find()->select(['fio','manager.id' , 'email'])
|
||||
->joinWith('manager')->where(['NOT',['manager.user_card_id' => null]])->all();
|
||||
}
|
||||
|
||||
public static function getManager($manager_id)
|
||||
{
|
||||
return UserCard::find()
|
||||
->select(['manager.id', 'fio', 'email', 'photo', 'gender'])
|
||||
->joinWith([
|
||||
'manager' => function ($query) { $query->select(['id']); }
|
||||
])
|
||||
->where(['manager.id' => $manager_id])
|
||||
->asArray()
|
||||
->one();
|
||||
}
|
||||
|
||||
public static function getManagerEmployeesList($manager_id)
|
||||
{
|
||||
return UserCard::find()
|
||||
->select(['user_card.id', 'user_card.fio', 'user_card.email'])
|
||||
->joinWith('managerEmployee')
|
||||
->where(['manager_employee.manager_id' => $manager_id])
|
||||
->all();
|
||||
}
|
||||
}
|
46
common/services/TaskService.php
Normal file
46
common/services/TaskService.php
Normal file
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace common\services;
|
||||
|
||||
use common\models\Task;
|
||||
|
||||
class TaskService
|
||||
{
|
||||
public static function createTask($taskParams)
|
||||
{
|
||||
$task = new Task();
|
||||
$task->load($taskParams, '');
|
||||
$task->save();
|
||||
return $task;
|
||||
}
|
||||
|
||||
public static function getTask($task_id): ?Task
|
||||
{
|
||||
return Task::findOne($task_id);
|
||||
}
|
||||
|
||||
public static function getTaskList($task_id): array
|
||||
{
|
||||
return Task::find()->asArray()->all();
|
||||
}
|
||||
|
||||
public static function getTaskListByProject($project_id): array
|
||||
{
|
||||
return Task::find()->where(['project_id' => $project_id])->asArray()->all();
|
||||
}
|
||||
|
||||
public static function updateTask($task_params): ?Task
|
||||
{
|
||||
$modelTask = Task::findOne($task_params['task_id']);
|
||||
|
||||
$modelTask->load($task_params, '');
|
||||
$modelTask->save();
|
||||
|
||||
return $modelTask;
|
||||
}
|
||||
|
||||
public static function taskExists($task_id): bool
|
||||
{
|
||||
return Task::find()->where(['id' => $task_id])->exists();
|
||||
}
|
||||
}
|
38
common/services/TemplateService.php
Normal file
38
common/services/TemplateService.php
Normal file
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace common\services;
|
||||
|
||||
use common\models\Template;
|
||||
|
||||
class TemplateService
|
||||
{
|
||||
public static function getTemplateList($document_type = null): array
|
||||
{
|
||||
if (!empty($document_type)) {
|
||||
return Template::find()->where(['document_type' => $document_type])->asArray()->all();
|
||||
}
|
||||
else {
|
||||
return Template::find()->asArray()->all();
|
||||
}
|
||||
}
|
||||
|
||||
public static function getTemplateWithFields($template_id): array
|
||||
{
|
||||
return Template::find()
|
||||
// ->select('title')
|
||||
->joinWith('templateDocumentFields.field')
|
||||
// ->with([
|
||||
// 'fields' => function ($query) { $query->select(['id', 'title', 'field_template']); }
|
||||
// ])
|
||||
->where(['template.id' => $template_id])
|
||||
->asArray()
|
||||
->one();
|
||||
}
|
||||
|
||||
public static function getTemplate($template_id): array
|
||||
{
|
||||
return Template::find()->where(['template.id' => $template_id])
|
||||
->asArray()
|
||||
->one();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user