Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use TYPO3\Flow\Annotations as Flow;
- use \TYPO3\Flow\Persistence\QueryInterface;
- use \TYPO3\Fluid\View\TemplateView;
- use \TYPO3\Flow\Property\TypeConverter\DateTimeConverter;
- use \Doctrine\Common\Collections\Collection; <<<<< does it exist?
- use \Doctrine\Common\Collections\ArrayCollection;
- (think of Crono as Blog, Evento as Post, Categoria as Category (of posts))
- $newFile: each file line contains data defining an Evento obj:
- title#descrption#comment#dateBegin#dataEnd#catA:catB
- /**
- * Adds the given new collection of Evento objects to the Evento repository
- *
- * @param \Mario\Crono\Domain\Model\Crono $crono
- * @param array $newFile <<<< why do I need Array (I upload a single file)?
- * @return void
- */
- public function createbulkAction(\Mario\Crono\Domain\Model\Crono $crono, array $newFile) {
- // thanks to http://www.layh.com/typo3-flow-tutorials/file-resources/comment-page-1/#comment-85622
- $fileResource = $this->resourceManager->importUploadedResource($newFile);
- $uri = $fileResource->getUri();
- // read file
- $csvLines = array();
- $fHandle = \fopen($uri, 'r');
- if ($fHandle) {
- while (($buffer = \fgets($fHandle, 4096)) !== false) {
- $csvLines[] = $buffer;
- }}
- \fclose($fHandle);
- foreach($csvLines as $recordString){
- \TYPO3\FLOW\var_dump($recordString);
- $recordArr = explode("#", trim($recordString));
- $evento = new \Mario\Crono\Domain\Model\Evento();
- $evento->setCrono($crono);
- $evento->setTitolo($recordArr[0]);
- $evento->setDescrizione($recordArr[1]);
- $evento->setCommento($recordArr[2]);
- $propertyMappingConfiguration = new \TYPO3\Flow\Property\PropertyMappingConfiguration();
- $propertyMappingConfiguration->forProperty('datainizio')->setTypeConverterOption(
- 'TYPO3\Flow\Property\TypeConverter\DateTimeConverter',
- \TYPO3\Flow\Property\TypeConverter\DateTimeConverter::CONFIGURATION_DATE_FORMAT,
- 'Y-m-d');
- $dataInizio = $this->propertyMapper->convert($recordArr[3], 'DateTime');
- $dataFine = $this->propertyMapper->convert($recordArr[4], 'DateTime');
- $evento->setDatainizio($dataInizio);
- $evento->setDatafine($dataFine);
- /// SET CATEGORIE .
- $categorias = new \Doctrine\Common\Collections\ArrayCollection;
- $evento->setCategorias($categorias);
- <<< I define a empty collection of categories and set to the object
- $catArr = explode(":", trim($recordArr[5]));
- foreach($catArr as $cat){$catObj = $this->categoriaRepository->findBySigla($cat)->getFirst(); $evento->addCategorias($catObj); } //
- $this->eventoRepository->add($evento);
- $crono->addEvento($evento);
- $this->cronoRepository->update($crono);
- }
- $this->addFlashMessage('Created many new Evento(s)');
- $this->redirect('index', 'Evento', NULL, array('crono' => $crono));
- }
Advertisement
Add Comment
Please, Sign In to add comment