110 lines
5.6 KiB
PHP
110 lines
5.6 KiB
PHP
|
|
<?php
|
|||
|
|
/*********************************************************************************
|
|||
|
|
* API-интерфейс для добавления загруженного с сайта PDF-файла в сущность CRM
|
|||
|
|
* All Rights Reserved.
|
|||
|
|
* Contributor(s): Илья Руденко itsaturn@yandex.ru
|
|||
|
|
********************************************************************************/
|
|||
|
|
|
|||
|
|
include_once 'include/Webservices/Query.php';
|
|||
|
|
include_once 'modules/Users/Users.php';
|
|||
|
|
require_once('include/Webservices/Utils.php');
|
|||
|
|
require_once 'include/Webservices/Create.php';
|
|||
|
|
require_once 'include/Webservices/Revise.php';
|
|||
|
|
require_once 'includes/Loader.php';
|
|||
|
|
vimport ('includes.runtime.Globals');
|
|||
|
|
vimport ('includes.runtime.BaseModel');
|
|||
|
|
vimport ('includes.runtime.LanguageHandler');
|
|||
|
|
|
|||
|
|
function vtws_addpdf($crmid, $file, $description, $user = false) {
|
|||
|
|
|
|||
|
|
$logstring = date("Y-m-d H:i:s").' '.json_encode($_REQUEST);
|
|||
|
|
file_put_contents('logs/AddPDF.log', $logstring.PHP_EOL, FILE_APPEND);
|
|||
|
|
|
|||
|
|
if(empty($crmid) or empty($file) or empty($description)){
|
|||
|
|
throw new WebServiceException(WebServiceErrorCode::$INVALIDID, "Не заполнены обязательные поля");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
global $adb, $current_user;
|
|||
|
|
|
|||
|
|
// 0. Копируем файл
|
|||
|
|
$parts = explode('/', $file);
|
|||
|
|
$filename = end($parts); // Выкурили имя файла из относительной ссылки
|
|||
|
|
$noteid = $adb->getUniqueID("vtiger_crmentity");
|
|||
|
|
$attachid = $adb->getUniqueID("vtiger_crmentity");
|
|||
|
|
|
|||
|
|
$upload_file_path = decideFilePath(); // Получили папку для сохранения
|
|||
|
|
$newfile = $upload_file_path . $attachid . "_" . $filename; // Собрали имя новому файлу
|
|||
|
|
$file = '../form.clientright.ru/'.$file; // К относительному пути добавили переход из папки CRM
|
|||
|
|
$result = copy($file, $newfile); // Собственно копируем файл в storage
|
|||
|
|
|
|||
|
|
$logstring = date("Y-m-d H:i:s").' копирование файла в: '.$newfile.' завершилось '.$result;
|
|||
|
|
file_put_contents('logs/AddPDF.log', $logstring.PHP_EOL, FILE_APPEND);
|
|||
|
|
|
|||
|
|
if ($result) {
|
|||
|
|
// 1. Создаем сущность Документ
|
|||
|
|
$date_var = date('Y-m-d H:i:s');
|
|||
|
|
$ownerid = getUserId($crmid); // Поднимем ответственного из той сущности, куда добавляем файл
|
|||
|
|
|
|||
|
|
// 1.1 Запись в crmentity
|
|||
|
|
$sql1 = "insert into vtiger_crmentity (crmid,smcreatorid,smownerid,setype,createdtime,modifiedtime) values(?,?,?,?,?,?)";
|
|||
|
|
$params1 = array($noteid, $ownerid, $ownerid, 'Documents', $adb->formatDate($date_var, true), $adb->formatDate($date_var, true));
|
|||
|
|
$adb->pquery($sql1, $params1);
|
|||
|
|
$logstring = date('Y-m-d H:i:s').' Добавили Документ в crmentity'.PHP_EOL;
|
|||
|
|
file_put_contents('logs/AddPDF.log', $logstring, FILE_APPEND);
|
|||
|
|
|
|||
|
|
// 1.2 Запись в notes
|
|||
|
|
$sql2 = "insert into vtiger_notes(notesid, title, filename, folderid, filetype, filelocationtype, filedownloadcount, filestatus, filesize) values(?,?,?,?,?,?,?,?,?)";
|
|||
|
|
$params2 = array($noteid, $description, $filename, 3, 'application/pdf', 'I', 0, 1, filesize($file));
|
|||
|
|
$adb->pquery($sql2, $params2);
|
|||
|
|
$logstring = date('Y-m-d H:i:s').' Добавили Документ в notes'.PHP_EOL;
|
|||
|
|
file_put_contents('logs/AddPDF.log', $logstring, FILE_APPEND);
|
|||
|
|
|
|||
|
|
// 2. Создаем сущность Аттачи Документов
|
|||
|
|
// 2.1 Запись в crmentity
|
|||
|
|
$sql1 = "insert into vtiger_crmentity (crmid,smcreatorid,smownerid,setype,createdtime,modifiedtime) values(?,?,?,?,?,?)";
|
|||
|
|
$params1 = array($attachid, $ownerid, $ownerid, 'Documents Attachment', $adb->formatDate($date_var, true), $adb->formatDate($date_var, true));
|
|||
|
|
$adb->pquery($sql1, $params1);
|
|||
|
|
$logstring = date('Y-m-d H:i:s').' Добавили файл в crmentity'.PHP_EOL;
|
|||
|
|
file_put_contents('logs/AddPDF.log', $logstring, FILE_APPEND);
|
|||
|
|
|
|||
|
|
// 2.2 Запись в attachments
|
|||
|
|
$sql2 = "insert into vtiger_attachments(attachmentsid, name, type, path, storedname) values(?,?,?,?,?)";
|
|||
|
|
$params2 = array($attachid, $filename, 'application/pdf', $upload_file_path, $filename);
|
|||
|
|
$adb->pquery($sql2, $params2);
|
|||
|
|
$logstring = date('Y-m-d H:i:s').' Добавили файл в attachments'.PHP_EOL;
|
|||
|
|
file_put_contents('logs/AddPDF.log', $logstring, FILE_APPEND);
|
|||
|
|
|
|||
|
|
// 3. Связываем Документ с Аттачем
|
|||
|
|
$sql3 = "insert into vtiger_seattachmentsrel(crmid, attachmentsid) values(?,?)";
|
|||
|
|
$adb->pquery($sql3, array($noteid, $attachid));
|
|||
|
|
$logstring = date('Y-m-d H:i:s').' связали файл с Документом'.PHP_EOL;
|
|||
|
|
file_put_contents('logs/AddPDF.log', $logstring, FILE_APPEND);
|
|||
|
|
|
|||
|
|
// 4. Привязываем Документ к Проекту (или другой сущности) по crmid
|
|||
|
|
$sql4 = "insert into vtiger_senotesrel(crmid, notesid) values(?,?)";
|
|||
|
|
$adb->pquery($sql4, array($crmid, $noteid));
|
|||
|
|
$logstring = date('Y-m-d H:i:s').' связали Документ с Проектом'.PHP_EOL;
|
|||
|
|
file_put_contents('logs/AddPDF.log', $logstring, FILE_APPEND);
|
|||
|
|
} else {
|
|||
|
|
throw new WebServiceException(WebServiceErrorCode::$INVALIDID, "Ошибка копирования файла PDF");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return 'YES';
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function getUserId($crmid) {
|
|||
|
|
global $adb;
|
|||
|
|
$query = 'select smownerid as userid
|
|||
|
|
from vtiger_crmentity
|
|||
|
|
where deleted = 0 and crmid = ?';
|
|||
|
|
$result = $adb->pquery($query, array($crmid));
|
|||
|
|
|
|||
|
|
if ($adb->num_rows($result) == 1) {
|
|||
|
|
// Что-то нашлось
|
|||
|
|
$output = $adb->query_result($result, 0, 'userid');
|
|||
|
|
} else {
|
|||
|
|
// Активных Проектов нет, а может быть несколько - значит ответственным будет админ
|
|||
|
|
$output = 1;
|
|||
|
|
}
|
|||
|
|
return $output;
|
|||
|
|
}
|