diff --git a/core/Controllers/ResController.php b/core/Controllers/ResController.php index 12b313891b28586e13d8edfd1cd1ec3a0bc6df38..80dec8092dfb992b63b246d5a455e2b34e439024 100644 --- a/core/Controllers/ResController.php +++ b/core/Controllers/ResController.php @@ -60,6 +60,7 @@ class ResController if (empty($aArgs['fileFormat'])) { return ['errors' => 'fileFormat ' . _EMPTY]; +<<<<<<< HEAD } if (empty($aArgs['status'])) { @@ -149,9 +150,104 @@ class ResController } catch (Exception $e) { return ['errors' => 'unknown error' . $e->getMessage()]; +======= +>>>>>>> lgi-feat-attachments } } +<<<<<<< HEAD +======= + if (empty($aArgs['status'])) { + + return ['errors' => 'status ' . _EMPTY]; + } + $encodedFile = $aArgs['encodedFile']; + $data = $aArgs['data']; + $collId = $aArgs['collId']; + $table = $aArgs['table']; + $fileFormat = $aArgs['fileFormat']; + $status = $aArgs['status']; + + try { + $count = count($data); + for ($i=0;$i<$count;$i++) { + $data[$i]['column'] = strtolower($data[$i]['column']); + } + + $returnCode = 0; + //copy sended file on tmp + $fileContent = base64_decode($encodedFile); + $random = rand(); + $fileName = 'tmp_file_' . $random . '.' . $fileFormat; + $Fnm = $_SESSION['config']['tmppath'] . $fileName; + $inF = fopen($Fnm,"w"); + fwrite($inF, $fileContent); + fclose($inF); + + //store resource on docserver + $ds = new DocserverController(); + $aArgs = [ + 'collId' => $collId, + 'fileInfos' => + [ + 'tmpDir' => $_SESSION['config']['tmppath'], + 'size' => filesize($Fnm), + 'format' => $fileFormat, + 'tmpFileName' => $fileName, + ] + ]; + + $storeResult = array(); + $storeResult = $ds->storeResourceOnDocserver($aArgs); + + if (!empty($storeResult['errors'])) { + + return ['errors' => $storeResult['errors']]; + } + + //store resource metadata in database + $aArgs = [ + 'data' => $data, + 'docserverId' => $storeResult['docserver_id'], + 'status' => $status, + 'fileFormat' => $fileFormat, + ]; + + $data = $this->prepareStorage($aArgs); + + unlink($Fnm); + + require_once 'core/class/class_resource.php'; + $resource = new \resource(); + $resId = $resource->load_into_db( + $table, + $storeResult['destination_dir'], + $storeResult['file_destination_name'], + $storeResult['path_template'], + $storeResult['docserver_id'], + $data, + $_SESSION['config']['databasetype'], + true + ); + + if (!is_numeric($resId)) { + + return ['errors' => 'Pb with SQL insertion : ' .$resId]; + } + + if ($resId == 0) { + $resId = ''; + } + + return [$resId]; + + } catch (Exception $e) { + + return ['errors' => 'unknown error' . $e->getMessage()]; + } + } + +>>>>>>> lgi-feat-attachments /** * Prepares storage on database. * @param $data array @@ -168,6 +264,7 @@ class ResController } if (empty($aArgs['docserverId'])) { +<<<<<<< HEAD return ['errors' => 'docserverId ' . _EMPTY]; } @@ -380,5 +477,387 @@ class ResController ); return $data; +======= + + return ['errors' => 'docserverId ' . _EMPTY]; + } + + if (empty($aArgs['status'])) { + + return ['errors' => 'status ' . _EMPTY]; + } + + if (empty($aArgs['fileFormat'])) { + + return ['errors' => 'fileFormat ' . _EMPTY]; + } + + $statusFound = false; + $typistFound = false; + $typeIdFound = false; + $toAddressFound = false; + $userPrimaryEntity = false; + $destinationFound = false; + $initiatorFound = false; + + $data = $aArgs['data']; + $docserverId = $aArgs['docserverId']; + $status = $aArgs['status']; + $fileFormat = $aArgs['fileFormat']; + + $userModel = new UserModel(); + $entityModel = new \Entities\Models\EntitiesModel(); + + $countD = count($data); + for ($i=0;$i<$countD;$i++) { + + if ( + strtoupper($data[$i]['type']) == 'INTEGER' || + strtoupper($data[$i]['type']) == 'FLOAT' + ) { + if ($data[$i]['value'] == '') { + $data[$i]['value'] = '0'; + } + } + + if (strtoupper($data[$i]['type']) == 'STRING') { + $data[$i]['value'] = $data[$i]['value']; + $data[$i]['value'] = str_replace(";", "", $data[$i]['value']); + $data[$i]['value'] = str_replace("--", "", $data[$i]['value']); + } + + if (strtoupper($data[$i]['column']) == strtoupper('status')) { + $statusFound = true; + } + + if (strtoupper($data[$i]['column']) == strtoupper('typist')) { + $typistFound = true; + } + + if (strtoupper($data[$i]['column']) == strtoupper('type_id')) { + $typeIdFound = true; + } + + if (strtoupper($data[$i]['column']) == strtoupper('custom_t10')) { + $mail = array(); + $theString = str_replace(">", "", $data[$i]['value']); + $mail = explode("<", $theString); + $user = $userModel->getByEmail(['mail' => $mail[count($mail) -1]]); + $userIdFound = $user[0]['user_id']; + if (!empty($userIdFound)) { + $toAddressFound = true; + $destUser = $userIdFound; + $entity = $entityModel->getByUserId(['user_id' => $destUser]); + if (!empty($entity[0]['entity_id'])) { + $userEntity = $entity[0]['entity_id']; + $userPrimaryEntity = true; + } + } else { + $entity = $entityModel->getByEmail(['email' => $mail[count($mail) -1]]); + if (!empty($entity[0]['entity_id'])) { + $userPrimaryEntity = true; + } + } + } + } + + if (!$typistFound && !$toAddressFound) { + array_push( + $data, + array( + 'column' => 'typist', + 'value' => 'auto', + 'type' => 'string', + ) + ); + } + + if (!$typeIdFound) { + array_push( + $data, + array( + 'column' => 'type_id', + 'value' => '10', + 'type' => 'string', + ) + ); + } + + if (!$statusFound) { + array_push( + $data, + array( + 'column' => 'status', + 'value' => $status, + 'type' => 'string', + ) + ); + } + + if ($toAddressFound) { + array_push( + $data, + array( + 'column' => 'dest_user', + 'value' => $destUser, + 'type' => 'string', + ) + ); + array_push( + $data, + array( + 'column' => 'typist', + 'value' => $destUser, + 'type' => 'string', + ) + ); + } + + if ($userPrimaryEntity) { + for ($i=0;$i<count($data);$i++) { + if (strtoupper($data[$i]['column']) == strtoupper('destination')) { + if ($data[$i]['value'] == "") { + $data[$i]['value'] = $userEntity; + } + $destinationFound = true; + break; + } + } + if (!$destinationFound) { + array_push( + $data, + array( + 'column' => 'destination', + 'value' => $userEntity, + 'type' => 'string', + ) + ); + } + } + + if ($userPrimaryEntity) { + for ($i=0;$i<count($data);$i++) { + if (strtoupper($data[$i]['column']) == strtoupper('initiator')) { + if ($data[$i]['value'] == "") { + $data[$i]['value'] = $userEntity; + } + $initiatorFound = true; + break; + } + } + if (!$initiatorFound) { + array_push( + $data, + array( + 'column' => 'initiator', + 'value' => $userEntity, + 'type' => 'string', + ) + ); + } + } + array_push( + $data, + array( + 'column' => 'format', + 'value' => $fileFormat, + 'type' => 'string', + ) + ); + array_push( + $data, + array( + 'column' => 'offset_doc', + 'value' => '', + 'type' => 'string', + ) + ); + array_push( + $data, + array( + 'column' => 'logical_adr', + 'value' => '', + 'type' => 'string', + ) + ); + array_push( + $data, + array( + 'column' => 'docserver_id', + 'value' => $docserverId, + 'type' => 'string', + ) + ); + + return $data; + } + + //TODO + public function storeExtResource($resId, $data, $table) + { + try { + if ($resId <> "") { + $func = new functions(); + $data = $func->object2array($data); + $queryExtFields = '('; + $queryExtValues = '('; + $queryExtValuesFinal = '('; + $parameters = array(); + $db = new Database(); + $findProcessLimitDate = false; + $findProcessNotes = false; + $delayProcessNotes = 0; + + for ($i=0;$i<count($data);$i++) { + if ($data[$i]['column'] == 'process_limit_date') { + $findProcessLimitDate = true; + } + // if ($data[$i]['column'] == 'process_notes') { + // $findProcessNotes = true; + // $delayProcessNotes = $data[$i]['value']; + // } + if ($data[$i]['column'] == 'process_notes') { + $findProcessNotes = true; + $donnees = explode(',',$data[$i]['value']); + $delayProcessNotes = $donnees['0']; + $calendarType = $donnees['1']; + } + } + + if ($table == 'mlb_coll_ext') { + if ($delayProcessNotes > 0) { + $processLimitDate = $this->retrieveProcessLimitDate( + $resId, + $delayProcessNotes, + $calendarType + ); + } else { + $processLimitDate = $this->retrieveProcessLimitDate($resId); + } + //echo $processLimitDate; + } + + if (!$findProcessLimitDate && $processLimitDate <> '') { + array_push( + $data, + array( + 'column' => 'process_limit_date', + 'value' => $processLimitDate, + 'type' => 'date', + ) + ); + } + + //var_dump($data); + for ($i=0;$i<count($data);$i++) { + if (strtoupper($data[$i]['type']) == 'INTEGER' || strtoupper($data[$i]['type']) == 'FLOAT') { + if ($data[$i]['value'] == '') { + $data[$i]['value'] = '0'; + } + $data[$i]['value'] = str_replace(',' , '.', $data[$i]['value']); + } + if (strtoupper($data[$i]['column']) == strtoupper('category_id')) { + $categoryId = $data[$i]['value']; + } + if (strtoupper($data[$i]['column']) == strtoupper('alt_identifier') && $data[$i]['value'] == "") { + require_once 'apps' . DIRECTORY_SEPARATOR . $_SESSION['config']['app_id'] + . DIRECTORY_SEPARATOR . 'class' . DIRECTORY_SEPARATOR . 'class_chrono.php'; + $chronoX = new chrono(); + for ($iColl=0; $iColl<=count($_SESSION['collections']); $iColl++) { + if ($_SESSION['collections'][$iColl]['extensions'][0] == $table) { + $resViewTable = $_SESSION['collections'][$iColl]['view']; + break; + } + } + $stmt = $db->query("SELECT destination, type_id FROM ".$resViewTable." WHERE res_id = ?", array($resId)); + $resView = $stmt->fetchObject(); + $myVars = array( + 'entity_id' => $resView->destination, + 'type_id' => $resView->type_id, + 'category_id' => $categoryId, + 'folder_id' => "", + ); + $myChrono = $chronoX->generate_chrono($categoryId, $myVars, 'false'); + $data[$i]['value'] = $myChrono; + } + if (strtoupper($data[$i]['column']) == strtoupper('exp_contact_id') && $data[$i]['value'] <> "" && !is_numeric($data[$i]['value'])) { + $theString = str_replace(">", "", $data[$i]['value']); + $mail = explode("<", $theString); + $stmt = $db->query("SELECT contact_id FROM view_contacts WHERE email = ? and enabled = 'Y' order by creation_date asc", array($mail[count($mail) -1])); + $contact = $stmt->fetchObject(); + + if ($contact->contact_id <> "") { + $data[$i]['value'] = $contact->contact_id; + } else { + $data[$i]['value'] = 0; + } + } + if (strtoupper($data[$i]['column']) == strtoupper('address_id') && $data[$i]['value'] <> "" && !is_numeric($data[$i]['value'])) { + $theString = str_replace(">", "", $data[$i]['value']); + $mail = explode("<", $theString); + $stmt = $db->query("SELECT ca_id FROM view_contacts WHERE email = ? and enabled = 'Y' order by creation_date asc", array($mail[count($mail) -1])); + $contact = $stmt->fetchObject(); + if ($contact->ca_id <> "") { + $data[$i]['value'] = $contact->ca_id; + } else { + $data[$i]['value'] = 0; + } + } + //COLUMN + $data[$i]['column'] = strtolower($data[$i]['column']); + $queryExtFields .= $data[$i]['column'] . ','; + //VALUE + if ($data[$i]['type'] == 'string' || $data[$i]['type'] == 'date') { + $queryExtValues .= "'" . $data[$i]['value'] . "',"; + } else { + $queryExtValues .= $data[$i]['value'] . ","; + } + $parameters[] = $data[$i]['value']; + $queryExtValuesFinal .= "?,"; + } + $queryExtFields = preg_replace('/,$/', ',res_id)', $queryExtFields); + $queryExtValues = preg_replace( + '/,$/', ',' . $resId . ')', $queryExtValues + ); + $queryExtValuesFinal = preg_replace( + '/,$/', ',' . $resId . ')', $queryExtValuesFinal + ); + /*$queryExt = " insert into " . $table . " " . $queryExtFields + . ' values ' . $queryExtValues ;*/ + $queryExt = " insert into " . $table . " " . $queryExtFields + . ' values ' . $queryExtValuesFinal ; + //echo $queryExt;exit; + $returnCode = 0; + if ($db->query($queryExt, $parameters)) { + $returnResArray = array( + 'returnCode' => (int) 0, + 'resId' => $resId, + 'error' => '', + ); + } else { + $returnResArray = array( + 'returnCode' => (int) -2, + 'resId' => '', + 'error' => 'Pb with SQL insertion', + ); + } + return $returnResArray; + } else { + $returnResArray = array( + 'returnCode' => (int) -3, + 'resId' => '', + 'error' => 'resId is not set', + ); + return $returnResArray; + } + } catch (Exception $e) { + $returnResArray = array( + 'returnCode' => (int) -1, + 'resId' => '', + 'error' => 'unknown error' . $e->getMessage(), + ); + return $returnResArray; + } +>>>>>>> lgi-feat-attachments } } \ No newline at end of file diff --git a/core/Test/ResControllerTest.php b/core/Test/ResControllerTest.php index 83d22203d2a739d3fc21a9601406ac7d1dabe714..7bdd3667806c020e3c2172d792fa57fd87f94bfa 100644 --- a/core/Test/ResControllerTest.php +++ b/core/Test/ResControllerTest.php @@ -118,4 +118,79 @@ class ResControllerTest extends PHPUnit_Framework_TestCase $this->assertGreaterThanOrEqual(0, $response); } +<<<<<<< HEAD +======= + + public function testStoreExtResource() + { + $action = new \Core\Controllers\ResController(); + + $path = $_SESSION['config']['tmppath'] . '/test/'; + + if (!is_dir($path)) { + mkdir($path); + } + + $fileSource = 'test_source.txt'; + + $fp = fopen($path . $fileSource, 'a'); + fwrite($fp, 'a unit test'); + fclose($fp); + + $fileContent = file_get_contents($path . $fileSource, FILE_BINARY); + $encodedFile = base64_encode($fileContent); + + $data = []; + + array_push( + $data, + array( + 'column' => 'subject', + 'value' => 'UNIT TEST', + 'type' => 'string', + ) + ); + + array_push( + $data, + array( + 'column' => 'type_id', + 'value' => 110, + 'type' => 'integer', + ) + ); + + array_push( + $data, + array( + 'column' => 'custom_t1', + 'value' => 'TEST', + 'type' => 'string', + ) + ); + + array_push( + $data, + array( + 'column' => 'custom_t10', + 'value' => 'lgi@maarch.org', + 'type' => 'string', + ) + ); + + $aArgs = [ + 'encodedFile' => $encodedFile, + 'data' => $data, + 'collId' => 'letterbox_coll', + 'table' => 'res_letterbox', + 'fileFormat' => 'txt', + 'status' => 'new', + ]; + + //TODO + //$response = $action->storeExtResource($aArgs); + + $this->assertGreaterThanOrEqual(0, $response); + } +>>>>>>> lgi-feat-attachments } \ No newline at end of file