Exemple #1
0
 /**
  * Sets up the default units
  * @throws \Exception
  */
 public function setupUnits()
 {
     $count = 0;
     $skipped = 0;
     $data = Setup::loadYAML(self::UNIT_DATA_FILE);
     $aUnits = array();
     foreach ($data as $unitName => $unitData) {
         if (UnitManager::getInstance()->unitExists($unitName)) {
             $skipped++;
             continue;
         }
         $unit = new Unit();
         $unit->setName($unitName);
         $unit->setSymbol($unitData["symbol"]);
         if (array_key_exists("prefixes", $unitData)) {
             if (!is_array($unitData["prefixes"])) {
                 throw new \Exception($unitName . " doesn't contain a prefix list, or the prefix list is not an array.");
             }
             foreach ($unitData["prefixes"] as $prefix) {
                 $siPrefix = SiPrefixManager::getInstance()->getSiPrefixBySymbol($prefix);
                 if ($siPrefix === false) {
                     throw new \Exception("Unable to find prefix " . $prefix);
                 }
                 $unit->getPrefixes()->add($siPrefix);
             }
         }
         PartKeepr::getEM()->persist($unit);
         $count++;
     }
     $this->entityManager->flush();
     $this->logMessage(sprintf("Imported %d Units, skipped %d because they already exist", $count, $skipped));
 }
 /**
  * Sets up the manufacturers using the YAML file.
  * @param $yaml string The path to the manufacturers YAML file
  */
 public function setupManufacturers()
 {
     $count = 0;
     $skipped = 0;
     $data = Setup::loadYAML(self::MANUFACTURER_FILE);
     foreach ($data as $mfgname => $logos) {
         try {
             ManufacturerManager::getInstance()->getManufacturerByName($mfgname);
             $skipped++;
         } catch (\Exception $e) {
             $manufacturer = new Manufacturer();
             $manufacturer->setName($mfgname);
             $this->entityManager->persist($manufacturer);
             foreach ($logos as $logo) {
                 $mfglogo = new ManufacturerICLogo();
                 $mfglogo->setManufacturer($manufacturer);
                 $mfglogo->replace(self::MANUFACTURER_PATH . "images/" . $logo);
                 $mfglogo->setOriginalFilename($logo);
                 $this->entityManager->persist($mfglogo);
             }
             $count++;
         }
     }
     $this->entityManager->flush();
     $this->logMessage(sprintf("Imported %d Manufacturers, skipped %d because they already exist", $count, $skipped));
 }
 /**
  * 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));
 }
 /**
  * Sets up the SI prefixes
  */
 public function setupSiPrefixes()
 {
     $count = 0;
     $skipped = 0;
     $data = Setup::loadYAML(self::SIPREFIX_DATA_FILE);
     foreach ($data as $prefixName => $prefixData) {
         if (!SiPrefixManager::getInstance()->siPrefixExists($prefixName)) {
             $prefix = new SiPrefix();
             $prefix->setPrefix($prefixName);
             $prefix->setPower($prefixData["power"]);
             $prefix->setSymbol($prefixData["symbol"]);
             $this->entityManager->persist($prefix);
             $count++;
         } else {
             $skipped++;
         }
     }
     $this->entityManager->flush();
     $this->logMessage(sprintf("Imported %d Si Prefixes, skipped %d", $count, $skipped));
 }