public function getOrCreateStorageLocation($storageLocation)
 {
     if (is_int($storageLocation)) {
         try {
             return $this->getStorageLocation($storageLocation);
         } catch (StorageLocationNotFoundException $e) {
         }
     }
     $sl = new StorageLocation();
     $sl->setName($storageLocation);
     PartKeepr::getEM()->persist($sl);
     return $sl;
 }
 /**
  * Creates multiple storage locations at once. 
  * 
  * Requires that the parameter "storageLocations" is set to an array with the names of the storage locations.
  * Returns all error messages as "data" index in the result array.
  */
 public function massCreate()
 {
     $this->requireParameter("storageLocations");
     $aMessages = array();
     foreach ($this->getParameter("storageLocations") as $storageLocation) {
         try {
             $obj = StorageLocationManager::getInstance()->getStorageLocationByName($storageLocation);
             $aMessages[] = sprintf(PartKeepr::i18n("Storage Location %s already exists"), $storageLocation);
         } catch (\Exception $e) {
             $obj = new StorageLocation();
             $obj->setName($storageLocation);
             PartKeepr::getEM()->persist($obj);
         }
     }
     PartKeepr::getEM()->flush();
     return array("data" => $aMessages);
 }
 /**
  * Migrates the storage locations
  */
 public function run()
 {
     $count = 0;
     $skipped = 0;
     $r = mysql_query("SELECT * FROM storeloc");
     while ($store = mysql_fetch_assoc($r)) {
         $name = PartDBMigration::convertText($store["name"]);
         try {
             $storageLocation = StorageLocationManager::getInstance()->getStorageLocationByName($name);
             $skipped++;
         } catch (\Exception $e) {
             $oStorageLocation = new StorageLocation();
             $oStorageLocation->setName($name);
             $this->entityManager->persist($oStorageLocation);
             $count++;
         }
     }
     $this->entityManager->flush();
     $this->logMessage(sprintf("Migrated %d storage locations, skipped %d because they already exist", $count, $skipped));
 }
Example #4
0
 /**
  * Deserializes the part
  * @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 "comment":
                 $this->setComment($value);
                 break;
             case "internalPartNumber":
                 $this->setInternalPartNumber($value);
                 break;
             case "footprint":
                 if ($value === 0) {
                     $this->setFootprint(null);
                 } else {
                     try {
                         $footprint = Footprint::loadById($value);
                         $this->setFootprint($footprint);
                     } catch (\Exception $e) {
                         // No footprint was found. Ignore it.
                     }
                 }
                 break;
             case "minStockLevel":
                 $this->setMinStockLevel($value);
                 break;
             case "partUnit":
                 $partUnit = PartUnit::loadById($value);
                 $this->setPartUnit($partUnit);
                 break;
             case "category":
                 $category = PartCategory::loadById($value);
                 $this->setCategory($category);
                 break;
             case "status":
                 $this->setStatus($value);
                 break;
             case "storageLocation":
                 $storageLocation = StorageLocation::loadById($value);
                 $this->setStorageLocation($storageLocation);
                 break;
             case "manufacturers":
                 $this->deserializeChildren($value, $this->getManufacturers(), "PartKeepr\\Part\\PartManufacturer");
                 foreach ($this->getManufacturers() as $manufacturer) {
                     $manufacturer->setPart($this);
                 }
                 break;
             case "distributors":
                 $this->deserializeChildren($value, $this->getDistributors(), "PartKeepr\\Part\\PartDistributor");
                 foreach ($this->getDistributors() as $distributor) {
                     $distributor->setPart($this);
                 }
                 break;
             case "parameters":
                 $this->deserializeChildren($value, $this->getParameters(), "PartKeepr\\PartParameter\\PartParameter");
                 foreach ($this->getParameters() as $parameter) {
                     $parameter->setPart($this);
                 }
                 break;
             case "needsReview":
                 $this->setReviewFlag($value);
                 break;
             case "partCondition":
                 $this->setCondition($value);
                 break;
             case "attachments":
                 $this->deserializeChildren($value, $this->getAttachments(), "PartKeepr\\Part\\PartAttachment");
                 foreach ($this->getAttachments() as $attachment) {
                     $attachment->setPart($this);
                 }
                 break;
         }
     }
 }