import upload file change

This commit is contained in:
Yuri Kuznetsov
2014-04-03 17:06:32 +03:00
parent 005c03bcfd
commit 429af1ec24
2 changed files with 44 additions and 4 deletions

View File

@@ -27,7 +27,33 @@ use \Espo\Core\Exceptions\Error;
use \Espo\Core\Exceptions\Forbidden;
class Import extends \Espo\Core\Controllers\Base
{
{
protected function getFileManager()
{
return $this->getContainer()->get('fileManager');
}
protected function getEntityManager()
{
return $this->getContainer()->get('entityManager');
}
public function actionUploadFile($params, $data)
{
$contents = $data;
$attachment = $this->getEntityManager()->getEntity('Attachment');
$attachment->set('type', 'text/csv');
$this->getEntityManager()->saveEntity($attachment);
$this->getFileManager()->putContents('data/upload/' . $attachment->id, $contents);
return array(
'attachmentId' => $attachment->id
);
}
public function actionCreate($params, $data)
{
$importParams = array(
@@ -42,11 +68,13 @@ class Import extends \Espo\Core\Controllers\Base
'action' => $data['action'],
);
$attachmentId = $data['attachmentId'];
if (!$this->getAcl()->check($data['entityType'], 'edit')) {
throw new Forbidden();
}
return $this->getService('Import')->import($data['entityType'], $data['fields'], $data['fileContents'], $importParams);
return $this->getService('Import')->import($data['entityType'], $data['fields'], $attachmentId, $importParams);
}
}

View File

@@ -24,6 +24,7 @@ namespace Espo\Services;
use \Espo\Core\Exceptions\Forbidden;
use \Espo\Core\Exceptions\NotFound;
use \Espo\Core\Exceptions\Error;
use Espo\ORM\Entity;
@@ -37,6 +38,7 @@ class Import extends \Espo\Core\Services\Base
'selectManagerFactory',
'config',
'serviceFactory',
'fileManager',
);
protected $dateFormatsMap = array(
@@ -67,6 +69,11 @@ class Import extends \Espo\Core\Services\Base
{
return $this->injections['entityManager'];
}
protected function getFileManager()
{
return $this->injections['fileManager'];
}
protected function getUser()
{
@@ -93,7 +100,7 @@ class Import extends \Espo\Core\Services\Base
return $this->injections['serviceFactory'];
}
public function import($scope, array $fields, $contents, array $params = array())
public function import($scope, array $fields, $attachmentId, array $params = array())
{
$delimiter = ',';
if (!empty($params['fieldDelimiter'])) {
@@ -103,7 +110,12 @@ class Import extends \Espo\Core\Services\Base
if (!empty($params['textQualifier'])) {
$enclosure = $params['textQualifier'];
}
$contents = $this->getFileManager()->getContents('data/upload/' . $attachmentId);
if (empty($contents)) {
throw new Error('Import error');
}
$lines = explode("\n", $contents);