/** * (non-PHPdoc) * @see PartKeepr\Service.RestfulService::create() */ public function create() { $this->requireParameter("tmp_id"); $this->requireParameter("footprint_id"); $tmpImage = TempUploadedFile::loadById($this->getParameter("tmp_id")); $file = new FootprintAttachment(); $footprint = Footprint::loadById($this->getParameter("footprint_id")); $file->setFootprint($footprint); $file->replace($tmpImage->getFilename()); $file->setOriginalFilename($tmpImage->getOriginalFilename()); $file->setDescription($this->getParameter("description")); PartKeepr::getEM()->persist($file); PartKeepr::getEM()->flush(); return $file->serialize(); }
/** * Imports the footprints * @throws \Exception */ public function importFootprintData() { $count = 0; $skipped = 0; /* Import pre-defined footprints */ $data = Setup::loadYAML(self::FOOTPRINT_FILE); foreach ($data as $footprintName => $footprintData) { /* Check if the footprint with the name already exists. If yes, skip the import for the single footprint */ if ($this->footprintExists($footprintName)) { $skipped++; continue; } $footprint = new Footprint(); $footprint->setName($footprintName); if (array_key_exists("description", $footprintData)) { $footprint->setDescription($footprintData["description"]); } if (array_key_exists("category", $footprintData)) { $footprintCategory = $this->addFootprintPath(explode("/", $footprintData["category"]), FootprintCategoryManager::getInstance()->getRootNode()); $footprint->setCategory($footprintCategory->getNode()); } if (array_key_exists("image", $footprintData)) { $footprintImage = new FootprintImage(); $footprintImage->setFootprint($footprint); $footprintImage->replace(self::FOOTPRINT_PATH . $footprintData["image"]); $footprint->setImage($footprintImage); } if (array_key_exists("attachments", $footprintData) && is_array($footprintData["attachments"])) { foreach ($footprintData["attachments"] as $attachment) { if (!is_array($attachment)) { throw new \Exception("Error: The property 'attachments' of {$footprintName} is not an array!"); } if (array_key_exists("url", $attachment)) { try { $footprintAttachment = new FootprintAttachment(); $footprintAttachment->setFootprint($footprint); $footprintAttachment->replaceFromURL($attachment["url"]); if (array_key_exists("description", $attachment)) { $footprintAttachment->setDescription($attachment["description"]); } $footprint->getAttachments()->add($footprintAttachment); } catch (\Exception $e) { //echo "error with url ".$attachment["url"]."\n"; } } } } $this->entityManager->persist($footprint); $count++; } $this->entityManager->flush(); $this->logMessage(sprintf("Imported %d footprints, skipped %d because they already existed", $count, $skipped)); }