/** * Migrates the existing footprints */ public function run() { $count = 0; $skipped = 0; // Get or create node for the imported footprints $footprintCategory = FootprintSetup::addFootprintPath(explode("/", "Imported Footprints"), FootprintCategoryManager::getInstance()->getRootNode()); $r = mysql_query("SELECT * FROM footprints"); while ($sFootprint = mysql_fetch_assoc($r)) { $name = PartDBMigration::convertText($sFootprint["name"]); try { FootprintManager::getInstance()->getFootprintByName($name); $skipped++; } catch (\Exception $e) { $footprint = new Footprint(); $footprint->setName($name); $footprint->setCategory($footprintCategory->getNode()); $this->entityManager->persist($footprint); $count++; } } $this->entityManager->flush(); $this->logMessage(sprintf("Migrated %d footprints, skipped %d because they already exist", $count, $skipped)); }
/** * Finds or creates a footprint by name. * * @param mixed $footprint Either the ID or the footprint's name to find */ public function getOrCreateFootprint($footprint) { try { $footprint = Footprint::loadById($footprint); return $footprint; } catch (EntityNotFoundException $e) { } $dql = "SELECT f FROM PartKeepr\\Footprint\\Footprint f WHERE f.name = :name"; $query = PartKeepr::getEM()->createQuery($dql); $query->setParameter("name", $footprint); try { $footprint = $query->getSingleResult(); return $footprint; } catch (\Exception $e) { } $fp = new Footprint(); $fp->setName($footprint); PartKeepr::getEM()->persist($fp); return $fp; }
/** * 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)); }