Joywork/engine/classes/WebAppRaiser/forms/OrderDocsForm.php
2026-05-22 21:21:54 +03:00

264 lines
7.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
class OrderDocsForm extends WebAppRaiserForm
{
public function __construct($form_id)
{
parent::__construct($form_id);
}
public function form()
{
$form = parent::form();
$header = '<div style="border-bottom: 2px solid rgb(67, 160, 71); margin-bottom: 1.5em;"><h3>%s</h3></div>';
$label = function($data) {
return array('#text' => $data, '#class' => ['label'], '#style' => ['float:none;']);
};
$form['form_attributes']['enctype'] = 'multipart/form-data';
$form['form_attributes']['js'][] = 'engine/classes/WebAppRaiser/forms/OrderDocsForm.js';
$form['order_id'] = array(
'inputs' => array(
'#type'=> 'hidden',
),
);
$form['object_id'] = array(
'inputs' => array(
'#type'=> 'hidden',
),
);
$form['markup_start'] = array(
'#markup' => '<div class="row">',
);
$form['category'] = array(
'#prefix' => '<div class="col-12 col-md-6">'.sprintf($header, 'Файлы').'<p>Загрузите сканы документов</p>',
'label'=> $label('Категория'),
'inputs' => array(
'#type'=> 'select',
'#options' => array(
'Правоустанавливающие документы' => 'Правоустанавливающие документы',
'Разворот паспорта' => 'Разворот паспорта',
'Техническая документация' => 'Техническая документация',
),
'#required'=> true,
'#inputs_style' => 'float:none; width:90%',
),
);
$categories = WebAppRaiserDocs::getCategories();
$form['category']['inputs']['#options'] = array_merge(
$form['category']['inputs']['#options'],
$categories,
['Другое' => 'Другое']
);
$form['other_category'] = array(
'inputs' => array(
'#type' => 'text',
'#attributes' => array(
'placeholder' => 'Категория',
),
'#inputs_style' => 'float:none; width:90%',
),
);
$form['files'] = array(
'label'=> $label('Файлы'),
'inputs' => array(
'#type'=> 'file',
'#required'=> true,
'#multiple' => true,
'#attributes' => array(
'accept' => '.jpg, .jpeg, .png, .pdf',
),
'#inputs_style' => 'float:none; width:90%',
),
'description' => 'Можно выбрать несколько файлов',
'#suffix' => '<div id="files-info" style="padding: 6px 12px;"></div>
<style>
a.remove-file {
color: red;
text-decoration: none;
border: 0px solid;
padding: 1px 5px;
font-size: 1.3em;
}
</style>
</div>',
);
$form['comment'] = array(
'#prefix' => '<div class="col col-12 col-md-6">'.sprintf($header, 'Комментарий'),
'label'=> $label('Комментарий'),
'inputs' => array(
'#type'=> 'textarea',
'#required'=> false,
'#rows' => 8,
'#inputs_style' => 'float:none; width:90%',
),
'#suffix' => '</div>',
);
$form['markup_end'] = array(
'#markup' => '</div>',
);
$form['submit'] = array(
'inputs' => array(
'#type'=> 'submit',
'#value'=> 'Сохранить',
),
);
return $form;
}
public function validate()
{
$status = parent::validate();
return $status;
}
public function submit($status, &$page, WebAppRaiserAPI $WebAppRaiserAPI)
{
parent::submit($status, $page, $WebAppRaiserAPI);
$page['status'] = $status['code'];
$page['msg'] = array_merge($page['msg'], $status['message']);
if($status['code'] == 'success'){
$files_dir = WebAppRaiserDocs::filesDir();
$filepath = $status['post']['object_id'].'/'.$status['post']['order_id'].'/';
if(!is_dir($files_dir.$filepath)){
mkdir($files_dir.$filepath, 0755, true);
}
$files_for_api = array();
$files_for_db = array();
$flag_files_ok = true;
if(!empty($_FILES['files'])) {
foreach($_FILES['files']['error'] as $key => $value){
if($value != UPLOAD_ERR_OK) {
$flag_files_ok = false;
$page['status'] = 'error';
$page['msg'][] = WebAppRaiserDocs::errorCodes()[$value].': '.$_FILES['files']['name'][$key];
}
}
if($flag_files_ok){
// сохраняем файлы в папку
$agency = User::getUserAgencyID();
$category = ($status['post']['category']=='Другое')?$status['post']['other_category']:$status['post']['category'];
foreach($_FILES['files']['tmp_name'] as $key => $tmp_name){
$fname_submit = $_FILES['files']['name'][$key];
$fname = $this->checkFileName($fname_submit, $files_dir.$filepath);
if(move_uploaded_file($tmp_name, $files_dir.$filepath.$fname)){
$data = file_get_contents($files_dir.$filepath.$fname);
$files_for_api[] = array(
'name' => $fname,
'data' => base64_encode($data),
);
$files_for_db[] = array(
'fid' => 0,
'order_id' => $status['post']['order_id'],
'object_id' => $status['post']['object_id'],
'file_path' => $filepath,
'file_name' => $fname,
'category' => $category,
'manager' => $WebAppRaiserAPI->get('war_username'),
'owner' => $WebAppRaiserAPI->get('war_user_id'),
'agency' => $agency,
);
}
else {
$flag_files_ok = false;
$page['status'] = 'error';
$page['msg'][] = 'Не удалось сохранить файл в папку назначения: '.$files_dir.$filepath.$fname;
}
}
}
if($flag_files_ok){
// Отправляем файлы по API
$fields_api = array(
'comment' => $status['post']['comment'],
'files' => array(
0 => array(
'name' => $category,
'files' => $files_for_api,
),
),
);
//$page['msg'][] = dpm(['API FILES', $status['post']['order_id'], $fields_api]);
$result = $WebAppRaiserAPI->addDocuments($status['post']['order_id'], $fields_api);
//$page['msg'][] = dpm(['API result', $result]);
$page['status'] = $result['status'];
$page['msg'][] = $result['msg'];
//$page['msg'][] = $result['body'];
if($result['status'] == 'success'){
//TODO записать файлы в базу
$flag_db_ok = true;
$valid_fields = array(
'fid' => 'fid',
'order_id' => 'order_id',
'object_id' => 'object_id',
'file_path' => 'file_path',
'file_name' => 'file_name',
'category' => 'category',
'manager' => 'manager',
'owner' => 'owner',
'agency' => 'agency',
);
foreach($files_for_db as $fields_db){
$fid = WebAppRaiserDB::save('war_files', $fields_db, $valid_fields, 'fid');
if(!$fid) {
$flag_db_ok = false;
}
}
if($flag_db_ok){
$page["msg"][] = "Документы сохранены в базу";
$page["status"] = 'success';
}
else{
$page["msg"][] = "Возникла проблема при сохранении документов в базу данных.";
$page["status"] = 'error';
}
}
}
}
}
else {
// валидация не прошла
}
parent::redirect($page);
}
private function checkFileName($filename, $filepath, $i = 0)
{
if(!is_file($filepath.$filename)){
return $filename;
}
else {
$name = substr($filename, 0, strrpos($filename, '.'));
$ext = substr($filename, strrpos($filename, '.'));
if($i > 99) {
return $name.'_'.$i.$ext;
}
if(!is_file($filepath.$name.'_'.$i.$ext)) {
return $name.'_'.$i.$ext;
}
else {
$i++;
return $this->checkFileName($filename, $filepath, $i);
}
}
}
}