/** * 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)); }
use PartKeepr\Part\PartImage, PartKeepr\StorageLocation\StorageLocationImage, PartKeepr\Footprint\FootprintImage, PartKeepr\TempImage\TempImage, PartKeepr\PartKeepr, PartKeepr\Image\Image, PartKeepr\Image\CachedImage, PartKeepr\Manufacturer\ManufacturerICLogo; include "../src/backend/PartKeepr/PartKeepr.php"; PartKeepr::initialize(""); $type = $_REQUEST["type"]; $id = $_REQUEST["id"]; if (substr($id, 0, 4) === "TMP:") { $tmpImageId = str_replace("TMP:", "", $id); $image = TempImage::loadById($tmpImageId); } else { try { switch ($type) { case Image::IMAGE_ICLOGO: $image = ManufacturerICLogo::loadById($id); break; case Image::IMAGE_FOOTPRINT: $image = FootprintImage::loadById($id); break; case Image::IMAGE_STORAGELOCATION: $image = StorageLocationImage::loadById($id); break; case "partattachment": $attachment = PartAttachment::loadById($id); $image = new PartImage(); $image->replace($attachment->getFilename()); break; default: $image = null; // Add default image? } } catch (\Exception $e) { $image = null;
/** * Deserializes the footprint * @param array $parameters The array with the parameters to set */ public function deserialize(array $parameters) { foreach ($parameters as $key => $value) { switch ($key) { case "name": $this->setName($value); break; case "description": $this->setDescription($value); break; case "image_id": if ($value == "") { echo "/** Breaking because of empty value */"; break; } try { $image = FootprintImage::loadById($value); $this->setImage($image); } catch (\Exception $e) { if ($this->getImage()) { // Image was not found, maybe a temporary image? $this->getImage()->replaceFromTemporaryFile($value); } else { $image = FootprintImage::createFromTemporaryFile($value); $this->setImage($image); } } break; case "category": try { $category = FootprintCategory::loadById($value); $this->setCategory($category); } catch (\Exception $e) { // Category was not found, do not change category. } break; case "attachments": $this->deserializeChildren($value, $this->getAttachments(), "PartKeepr\\Footprint\\FootprintAttachment"); foreach ($this->getAttachments() as $attachment) { $attachment->setFootprint($this); } break; } } }