partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

commit 9f5fbd9e1e2288e122fdda7f51dffd467d9be86c
parent 3343a3635fe521cee5e6e049ddbdc4a8b80713ce
Author: Felicitus <felicitus@felicitus.org>
Date:   Fri, 24 Jul 2015 18:52:25 +0200

Migrated storage locations to their own bundle, added tree implementation.

Diffstat:
Mapp/AppKernel.php | 1+
Msrc/PartKeepr/CoreBundle/DoctrineMigrations/Version20150719213922.php | 6++----
Asrc/PartKeepr/CoreBundle/DoctrineMigrations/Version20150724174030.php | 55+++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/PartKeepr/StorageLocationBundle/Entity/StorageLocation.php | 117+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/PartKeepr/StorageLocationBundle/Entity/StorageLocationCategory.php | 121+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/PartKeepr/StorageLocationBundle/Entity/StorageLocationImage.php | 49+++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/PartKeepr/StorageLocationBundle/PartKeeprStorageLocationBundle.php | 8++++++++
Msrc/backend/PartKeepr/Part/Part.php | 10++++++----
Msrc/backend/PartKeepr/Part/PartManager.php | 2+-
Msrc/backend/PartKeepr/PartKeepr.php | 4++--
Msrc/backend/PartKeepr/Setup/Migration/PartDB/StorageLocationMigration.php | 2+-
Dsrc/backend/PartKeepr/StorageLocation/Exceptions/StorageLocationNotFoundException.php | 13-------------
Dsrc/backend/PartKeepr/StorageLocation/StorageLocation.php | 107-------------------------------------------------------------------------------
Dsrc/backend/PartKeepr/StorageLocation/StorageLocationImage.php | 52----------------------------------------------------
Dsrc/backend/PartKeepr/StorageLocation/StorageLocationManager.php | 95-------------------------------------------------------------------------------
Dsrc/backend/PartKeepr/StorageLocation/StorageLocationService.php | 109-------------------------------------------------------------------------------
16 files changed, 363 insertions(+), 388 deletions(-)

diff --git a/app/AppKernel.php b/app/AppKernel.php @@ -90,6 +90,7 @@ class AppKernel extends Kernel $bundles[] = new PartKeepr\DistributorBundle\PartKeeprDistributorBundle(); $bundles[] = new PartKeepr\ManufacturerBundle\PartKeeprManufacturerBundle(); $bundles[] = new PartKeepr\ImageBundle\PartKeeprImageBundle(); + $bundles[] = new PartKeepr\StorageLocationBundle\PartKeeprStorageLocationBundle(); return $bundles; } diff --git a/src/PartKeepr/CoreBundle/DoctrineMigrations/Version20150719213922.php b/src/PartKeepr/CoreBundle/DoctrineMigrations/Version20150719213922.php @@ -7,14 +7,12 @@ use Gedmo\Tree\Entity\Repository\NestedTreeRepository; use PartKeepr\FootprintBundle\Entity\FootprintCategory; /** - * + * Re-generates the category paths. It is enough to trigger an update on the root node, as the event listener + * will recursively update the child categories. */ class Version20150719213922 extends BaseMigration { /** - * Re-generates the category paths. It is enough to trigger an update on the root node, as the event listener - * will recursively update the child categories. - * * @param Schema $schema */ public function up(Schema $schema) diff --git a/src/PartKeepr/CoreBundle/DoctrineMigrations/Version20150724174030.php b/src/PartKeepr/CoreBundle/DoctrineMigrations/Version20150724174030.php @@ -0,0 +1,55 @@ +<?php + +namespace PartKeepr\CoreBundle\DoctrineMigrations; + +use Doctrine\DBAL\Schema\Schema; +use PartKeepr\StorageLocationBundle\Entity\StorageLocationCategory; + +/** + * Migrates all storage locations to support categories. Creates a new root category if it doesn't exist, then applies + * the root category to all storage locations. + */ +class Version20150724174030 extends BaseMigration +{ + /** + * @param Schema $schema + */ + public function up(Schema $schema) + { + $this->performDatabaseUpgrade(); + + $repository = $this->getEM()->getRepository( + 'PartKeeprStorageLocationBundle:StorageLocationCategory' + ); + + $rootNodes = $repository->getRootNodes(); + + if (count($rootNodes) === 0) { + // Ensure that the root category exists + $rootNode = new StorageLocationCategory(); + $rootNode->setName("Root Node"); + $this->getEM()->persist($rootNode); + $this->getEM()->flush(); + } else { + $rootNode = array_values($rootNodes)[0]; + } + + $storageLocationRepository = $this->getEM()->getRepository( + 'PartKeeprStorageLocationBundle:StorageLocation' + ); + + $allStorageLocations = $storageLocationRepository->findAll(); + + foreach ($allStorageLocations as $storageLocation) { + $storageLocation->setCategory($rootNode); + } + $this->getEM()->flush(); + } + + /** + * @param Schema $schema + */ + public function down(Schema $schema) + { + } +} diff --git a/src/PartKeepr/StorageLocationBundle/Entity/StorageLocation.php b/src/PartKeepr/StorageLocationBundle/Entity/StorageLocation.php @@ -0,0 +1,117 @@ +<?php +namespace PartKeepr\StorageLocationBundle\Entity; + +use Doctrine\ORM\Mapping as ORM; +use PartKeepr\Util\BaseEntity; +use Symfony\Component\Serializer\Annotation\Groups; + +/** @ORM\Entity * */ +class StorageLocation extends BaseEntity +{ + /** + * Holds the name for our storage location + * @ORM\Column(type="string",unique=true) + * @Groups({"default"}) + * + * @var string + */ + private $name; + + /** + * Holds the storage location image + * @ORM\OneToOne(targetEntity="PartKeepr\StorageLocationBundle\Entity\StorageLocationImage", + * mappedBy="storageLocation",cascade={"persist", "remove"}) + * @Groups({"default"}) + * + * @var StorageLocationImage + */ + private $image; + + /** + * The category of the footprint + * + * @ORM\ManyToOne(targetEntity="PartKeepr\StorageLocationBundle\Entity\StorageLocationCategory") + * @Groups({"default"}) + * + * @var StorageLocationCategory + */ + private $category; + + /** + * Returns the category path + * @Groups({"default"}) + * + * @return string + */ + public function getCategoryPath() + { + if ($this->category !== null) { + return $this->category->getCategoryPath(); + } else { + return ""; + } + } + + /** + * Sets the category for this storage location + * + * @param StorageLocationCategory $category The category + * + * @return void + */ + public function setCategory(StorageLocationCategory $category) + { + $this->category = $category; + } + + /** + * Returns the category of this storage location + * + * @return StorageLocationCategory The storage location category + */ + public function getCategory() + { + return $this->category; + } + + /** + * Sets the name for the storage location + * + * @param string $name the name to set + */ + public function setName($name) + { + $this->name = $name; + } + + /** + * Returns the name of the storage location + * + * @return string The name + */ + public function getName() + { + return $this->name; + } + + /** + * Sets the storage location image + * + * @param StorageLocationImage $image The storage location image + */ + public function setImage(StorageLocationImage $image) + { + $this->image = $image; + $image->setStorageLocation($this); + } + + /** + * Returns the storage location image + * + * @return StorageLocationImage The storage location image + */ + public function getImage() + { + return $this->image; + } +} diff --git a/src/PartKeepr/StorageLocationBundle/Entity/StorageLocationCategory.php b/src/PartKeepr/StorageLocationBundle/Entity/StorageLocationCategory.php @@ -0,0 +1,121 @@ +<?php +namespace PartKeepr\StorageLocationBundle\Entity; + +use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\ORM\Mapping as ORM; +use Gedmo\Mapping\Annotation as Gedmo; +use PartKeepr\CategoryBundle\Entity\AbstractCategory; +use PartKeepr\DoctrineReflectionBundle\Annotation\TargetService; +use Symfony\Component\Serializer\Annotation\Groups; + +/** + * @ORM\Entity(repositoryClass="Gedmo\Tree\Entity\Repository\NestedTreeRepository") + * @Gedmo\Tree(type="nested") + * @ORM\Table(indexes={@ORM\Index(columns={"lft"}),@ORM\Index(columns={"rgt"})}) + * The entity for our storage location categories + * @TargetService(uri="/api/storage_location_categories") + */ +class StorageLocationCategory extends AbstractCategory +{ + /** + * @Gedmo\TreeParent + * @ORM\ManyToOne(targetEntity="StorageLocationCategory", inversedBy="children") + * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE") + * @Groups({"default"}) + */ + protected $parent; + + /** + * @ORM\OneToMany(targetEntity="StorageLocationCategory", mappedBy="parent") + * @ORM\OrderBy({"lft" = "ASC"}) + * @Groups({"default"}) + */ + protected $children; + + /** + * @ORM\OneToMany(targetEntity="PartKeepr\StorageLocationBundle\Entity\StorageLocation", mappedBy="category") + * + */ + protected $storageLocations; + + /** + * @ORM\Column(type="text",nullable=true) + * @Groups({"default"}) + * @var string + */ + protected $categoryPath; + + /** + * Sets the parent category + * + * @param AbstractCategory|null $parent + */ + public function setParent(AbstractCategory $parent = null) + { + $this->parent = $parent; + } + + /** + * Returns the parent category + * + * @return mixed + */ + public function getParent() + { + return $this->parent; + } + + /** + * Returns the storage locations + * + * @return ArrayCollection + */ + public function getStorageLocations() + { + return $this->storageLocations; + } + + /** + * Returns the children + * + * @return ArrayCollection + */ + public function getChildren() + { + return $this->children; + } + + /** + * Returns the category path + * + * @return string + */ + public function getCategoryPath() + { + return $this->categoryPath; + } + + /** + * Sets the category path + * + * @param string $categoryPath The category path + */ + public function setCategoryPath($categoryPath) + { + $this->categoryPath = $categoryPath; + } + + /** + * Generates the category path + * + * @return string The category path + */ + public function generateCategoryPath() + { + if ($this->getParent() !== null) { + return $this->getParent()->generateCategoryPath()." / ".$this->getName(); + } else { + return $this->getName(); + } + } +} diff --git a/src/PartKeepr/StorageLocationBundle/Entity/StorageLocationImage.php b/src/PartKeepr/StorageLocationBundle/Entity/StorageLocationImage.php @@ -0,0 +1,49 @@ +<?php +namespace PartKeepr\StorageLocationBundle\Entity; + +use Doctrine\ORM\Mapping as ORM; +use PartKeepr\ImageBundle\Entity\Image; + +/** + * Holds a storage location image + * + * @ORM\Entity + **/ +class StorageLocationImage extends Image +{ + /** + * The storage location object + * @ORM\OneToOne(targetEntity="PartKeepr\StorageLocationBundle\Entity\StorageLocation",inversedBy="image") + * + * @var StorageLocation + */ + private $storageLocation = null; + + /** + * Creates a new storage location image instance + */ + public function __construct() + { + parent::__construct(Image::IMAGE_STORAGELOCATION); + } + + /** + * Sets the storage location + * + * @param StorageLocation $storageLocation The storage location to set + */ + public function setStorageLocation(StorageLocation $storageLocation) + { + $this->storageLocation = $storageLocation; + } + + /** + * Returns the storage location + * + * @return StorageLocation the storage location + */ + public function getStorageLocation() + { + return $this->storageLocation; + } +} diff --git a/src/PartKeepr/StorageLocationBundle/PartKeeprStorageLocationBundle.php b/src/PartKeepr/StorageLocationBundle/PartKeeprStorageLocationBundle.php @@ -0,0 +1,8 @@ +<?php +namespace PartKeepr\StorageLocationBundle; + +use Symfony\Component\HttpKernel\Bundle\Bundle; + +class PartKeeprStorageLocationBundle extends Bundle +{ +} diff --git a/src/backend/PartKeepr/Part/Part.php b/src/backend/PartKeepr/Part/Part.php @@ -2,7 +2,7 @@ namespace PartKeepr\Part; use PartKeepr\PartBundle\Entity\PartMeasurementUnit; -use PartKeepr\StorageLocation\StorageLocation, +use PartKeepr\StorageLocationBundle\Entity\StorageLocation, \PartKeepr\FootprintBundle\Entity\Footprint, \PartKeepr\PartBundle\Entity\PartCategoryManager, PartKeepr\Util\Deserializable, @@ -63,7 +63,7 @@ class Part extends BaseEntity implements Serializable, Deserializable { /** * Defines the storage location of this part - * @ORM\ManyToOne(targetEntity="PartKeepr\StorageLocation\StorageLocation") + * @ORM\ManyToOne(targetEntity="PartKeepr\StorageLocationBundle\Entity\StorageLocation") * @var StorageLocation */ private $storageLocation; @@ -350,7 +350,8 @@ class Part extends BaseEntity implements Serializable, Deserializable { /** * Sets the storage location for this part - * @param \PartKeepr\StorageLocation\StorageLocation $storageLocation The storage location + * +*@param \PartKeepr\StorageLocationBundle\Entity\StorageLocation $storageLocation The storage location */ public function setStorageLocation (StorageLocation $storageLocation) { $this->storageLocation = $storageLocation; @@ -358,7 +359,8 @@ class Part extends BaseEntity implements Serializable, Deserializable { /** * Returns the storage location for this part - * @return \PartKeepr\StorageLocation\StorageLocation $storageLocation The storage location + * +*@return \PartKeepr\StorageLocationBundle\Entity\StorageLocation $storageLocation The storage location */ public function getStorageLocation () { return $this->storageLocation; diff --git a/src/backend/PartKeepr/Part/PartManager.php b/src/backend/PartKeepr/Part/PartManager.php @@ -12,7 +12,7 @@ use PartKeepr\UploadedFileBundle\Entity\TempUploadedFile, PartKeepr\SiPrefixBundle\Entity\SiPrefix, PartKeepr\Part\PartDistributor, PartKeepr\Part\PartManufacturer, - PartKeepr\StorageLocation\StorageLocation, + PartKeepr\StorageLocationBundle\Entity\StorageLocation, PartKeepr\StorageLocation\StorageLocationManager, PartKeepr\Part\Part, Doctrine\ORM\Query, diff --git a/src/backend/PartKeepr/PartKeepr.php b/src/backend/PartKeepr/PartKeepr.php @@ -175,8 +175,8 @@ class PartKeepr { 'PartKeepr\Project\ProjectPart', 'PartKeepr\Project\ProjectAttachment', - 'PartKeepr\StorageLocation\StorageLocation', - 'PartKeepr\StorageLocation\StorageLocationImage', + 'PartKeepr\StorageLocationBundle\Entity\StorageLocation', + 'PartKeepr\StorageLocationBundle\Entity\StorageLocationImage', 'PartKeepr\Stock\StockEntry', diff --git a/src/backend/PartKeepr/Setup/Migration/PartDB/StorageLocationMigration.php b/src/backend/PartKeepr/Setup/Migration/PartDB/StorageLocationMigration.php @@ -2,7 +2,7 @@ namespace PartKeepr\Setup\Migration\PartDB; use PartKeepr\PartKeepr, - PartKeepr\StorageLocation\StorageLocation, + PartKeepr\StorageLocationBundle\Entity\StorageLocation, PartKeepr\StorageLocation\StorageLocationManager, PartKeepr\Setup\AbstractSetup; diff --git a/src/backend/PartKeepr/StorageLocation/Exceptions/StorageLocationNotFoundException.php b/src/backend/PartKeepr/StorageLocation/Exceptions/StorageLocationNotFoundException.php @@ -1,12 +0,0 @@ -<?php -namespace PartKeepr\StorageLocation\Exceptions; - -use PartKeepr\Util\SerializableException, - PartKeepr\PartKeepr; - -class StorageLocationNotFoundException extends SerializableException { - public function __construct () { - parent::__construct(PartKeepr::i18n("Storage Location not found.")); - } -} -?>- \ No newline at end of file diff --git a/src/backend/PartKeepr/StorageLocation/StorageLocation.php b/src/backend/PartKeepr/StorageLocation/StorageLocation.php @@ -1,106 +0,0 @@ -<?php -namespace PartKeepr\StorageLocation; - -use PartKeepr\Util\Deserializable, - PartKeepr\Util\Serializable, - PartKeepr\Util\BaseEntity, - Doctrine\ORM\Mapping as ORM; - -/** @ORM\Entity **/ -class StorageLocation extends BaseEntity implements Serializable, Deserializable { - /** - * Holds the name for our storage location - * @ORM\Column(type="string",unique=true) - * @var string - */ - private $name; - - /** - * Holds the storage location image - * @ORM\OneToOne(targetEntity="PartKeepr\StorageLocation\StorageLocationImage",mappedBy="storageLocation",cascade={"persist", "remove"}) - * @var StorageLocationImage - */ - private $image; - - /** - * Sets the name for the storage location - * @param string $name the name to set - */ - public function setName ($name) { - $this->name = $name; - } - - /** - * Returns the name of the storage location - * @return string The name - */ - public function getName () { - return $this->name; - } - - /** - * Sets the storage location image - * @param StorageLocationImage $image The storage location image - */ - public function setImage (StorageLocationImage $image) { - $this->image = $image; - $image->setStorageLocation($this); - } - - /** - * Returns the storage location image - * @return StorageLocationImage The storage location image - */ - public function getImage () { - return $this->image; - } - - /** - * Returns this storage location in serialized form - * @return array The serialized storage location - */ - public function serialize () { - return array( - "id" => $this->getId(), - "name" => $this->getName(), - "image_id" => is_object($this->getImage()) ? $this->getImage()->getId() : null); - } - - /** - * Deserializes the storage location - * @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 "image_id": - if ($value == "") { - echo "/** Breaking because of empty value */"; - break; - } - - try { - $image = StorageLocationImage::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 = StorageLocationImage::createFromTemporaryFile($value); - $this->setImage($image); - echo "/**"; - echo $image->getId(); - echo "*/"; - echo "/** FOO */"; - } - } - - break; - } - } - } -}- \ No newline at end of file diff --git a/src/backend/PartKeepr/StorageLocation/StorageLocationImage.php b/src/backend/PartKeepr/StorageLocation/StorageLocationImage.php @@ -1,51 +0,0 @@ -<?php -namespace PartKeepr\StorageLocation; - -use PartKeepr\Util\Serializable, - PartKeepr\ImageBundle\Entity\Image, - Doctrine\ORM\Mapping as ORM; - -/** - * Holds a storage location image - * @ORM\Entity - **/ -class StorageLocationImage extends Image implements Serializable { - /** - * The storage location object - * @ORM\OneToOne(targetEntity="PartKeepr\StorageLocation\StorageLocation",inversedBy="image") - * @var StorageLocation - */ - private $storageLocation = null; - - /** - * Creates a new storage location image instance - */ - public function __construct () { - parent::__construct(Image::IMAGE_STORAGELOCATION); - } - - /** - * Sets the storage location - * @param StorageLocation $storageLocation The storage location to set - */ - public function setStorageLocation (StorageLocation $storageLocation) { - $this->storageLocation = $storageLocation; - } - - /** - * Returns the storage location - * @return StorageLocation the storage location - */ - public function getStorageLocation () { - return $this->storageLocation; - } - - /** - * - * Serializes this storage location image - * @return array The serialized storage location image - */ - public function serialize () { - return array("id" => $this->getId(), "storageLocation_id" => $this->getStorageLocation()->getId()); - } -}- \ No newline at end of file diff --git a/src/backend/PartKeepr/StorageLocation/StorageLocationManager.php b/src/backend/PartKeepr/StorageLocation/StorageLocationManager.php @@ -1,94 +0,0 @@ -<?php -namespace PartKeepr\StorageLocation; - -use PartKeepr\Util\Singleton, - PartKeepr\StorageLocation\StorageLocation, - PartKeepr\PartKeepr, - PartKeepr\Category\CategoryManager, - PartKeepr\StorageLocation\Exceptions\StorageLocationNotFoundException; - -class StorageLocationManager extends Singleton { - public function getStorageLocations ($start = 0, $limit = 10, $sort = "footprint", $dir = "asc", $filter = "") { - - $qb = PartKeepr::getEM()->createQueryBuilder(); - $qb->select("st.id, st.name")->from("PartKeepr\StorageLocation\StorageLocation","st"); - - if ($filter != "") { - $qb = $qb->where("LOWER(st.name) LIKE :filter"); - $qb->setParameter("filter", "%".strtolower($filter)."%"); - } - - if ($limit > -1) { - $qb->setMaxResults($limit); - $qb->setFirstResult($start); - } - - $qb->orderBy("st.".$sort, $dir); - - $query = $qb->getQuery(); - - $result = $query->getResult(); - - $totalQueryBuilder = PartKeepr::getEM()->createQueryBuilder(); - $totalQueryBuilder->select("COUNT(st.id)")->from("PartKeepr\StorageLocation\StorageLocation","st"); - - - - if ($filter != "") { - $totalQueryBuilder = $totalQueryBuilder->where("LOWER(st.name) LIKE :filter"); - $totalQueryBuilder->setParameter("filter", "%".strtolower($filter)."%"); - } - - $totalQuery = $totalQueryBuilder->getQuery(); - - return array("data" => $result, "start" => $start, "totalCount" => $totalQuery->getSingleScalarResult()); - } - - public function getStorageLocation ($id) { - $storageLocation = PartKeepr::getEM()->find("PartKeepr\StorageLocation\StorageLocation", $id); - - if ($storageLocation) { - return $storageLocation; - } else { - throw new StorageLocationNotFoundException(); - } - } - - public function getStorageLocationByName ($name) { - $query = PartKeepr::getEM()->createQuery("SELECT s FROM PartKeepr\StorageLocation\StorageLocation s WHERE s.name = :name"); - $query->setParameter("name", $name); - - return $query->getSingleResult(); - } - - public function addStorageLocation ($name) { - $storageLocation = new StorageLocation(); - $storageLocation->setName($name); - - PartKeepr::getEM()->persist($storageLocation); - PartKeepr::getEM()->flush(); - - return $storageLocation; - } - public function deleteStorageLocation ($id) { - $storageLocation = $this->getStorageLocation($id); - - PartKeepr::getEM()->remove($storageLocation); - PartKeepr::getEM()->flush(); - } - - 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; - } -}- \ No newline at end of file diff --git a/src/backend/PartKeepr/StorageLocation/StorageLocationService.php b/src/backend/PartKeepr/StorageLocation/StorageLocationService.php @@ -1,108 +0,0 @@ -<?php -namespace PartKeepr\StorageLocation; - -use PartKeepr\Service\RestfulService, - PartKeepr\Service\Service, - PartKeepr\Part\PartManager, - PartKeepr\Util\SerializableException, - PartKeepr\Stock\StockEntry, - PartKeepr\PartKeepr, - PartKeepr\Session\SessionManager; - -class StorageLocationService extends Service implements RestfulService { - - public function get () { - if ($this->hasParameter("id")) { - return array("data" => StorageLocationManager::getInstance()->getStorageLocation($this->getParameter("id"))->serialize()); - } else { - if ($this->hasParameter("sort")) { - $tmp = json_decode($this->getParameter("sort"), true); - - $aSortParams = $tmp[0]; - } else { - $aSortParams = array( - "property" => "name", - "direction" => "ASC"); - } - return StorageLocationManager::getInstance()->getStorageLocations( - $this->getParameter("start", $this->getParameter("start", 0)), - $this->getParameter("limit", $this->getParameter("limit", 25)), - $this->getParameter("sortby", $aSortParams["property"]), - $this->getParameter("dir", $aSortParams["direction"]), - $this->getParameter("query", "")); - } - } - - public function create () { - $this->requireParameter("name"); - - $storageLocation = new StorageLocation(); - $storageLocation->deserialize($this->getParameters()); - - PartKeepr::getEM()->persist($storageLocation); - - try { - PartKeepr::getEM()->flush(); - } catch (\PDOException $e) { - if ($e->getCode() == "23505") { - $exception = new SerializableException(sprintf(PartKeepr::i18n("Storage Location %s already exists!"), $storageLocation->getName())); - $exception->setDetail(sprintf(PartKeepr::i18n("You tried to add the storage location %s, but a storage location with the same name already exists."), $storageLocation->getName())); - - throw $exception; - } else { - throw $e; - } - } - - - return array("data" => $storageLocation->serialize()); - } - - public function update () { - $this->requireParameter("id"); - $this->requireParameter("name"); - $storageLocation = StorageLocationManager::getInstance()->getStorageLocation($this->getParameter("id")); - $storageLocation->deserialize($this->getParameters()); - - PartKeepr::getEM()->flush(); - - return array("data" => $storageLocation->serialize()); - - } - - public function destroy () { - $this->requireParameter("id"); - - StorageLocationManager::getInstance()->deleteStorageLocation($this->getParameter("id")); - - return array("data" => null); - } - - /** - * 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); - } -}- \ No newline at end of file