/**
  * Returns a list of footprint attachments
  *
  * @param int $start Start of the list, default 0
  * @param int $limit Number of users to list, default 10
  * @param string $sort The field to sort by, default "name"
  * @param string $dir The direction to sort (ASC or DESC), default ASC
  * @param string $filter The footprint id
  */
 public function getFootprintAttachments($start = 0, $limit = 10, $sort = "name", $dir = "asc", $filter = "")
 {
     $qb = PartKeepr::getEM()->createQueryBuilder();
     $qb->select("st")->from("PartKeepr\\Footprint\\FootprintAttachment", "st")->leftJoin('st.footprint', "fp");
     if ($filter != "") {
         $footprint = Footprint::loadById($filter);
         $qb = $qb->where("st.footprint = :footprint");
         $qb->setParameter("footprint", $footprint);
     }
     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\\Footprint\\FootprintAttachment", "st");
     if ($filter != "") {
         $totalQueryBuilder = $totalQueryBuilder->where("st.footprint = :footprint");
         $totalQueryBuilder->setParameter("footprint", $footprint);
     }
     $totalQuery = $totalQueryBuilder->getQuery();
     $aData = array();
     foreach ($result as $item) {
         $aData[] = $item->serialize();
     }
     return array("data" => $aData, "totalCount" => $totalQuery->getSingleScalarResult());
 }
Ejemplo n.º 2
0
 public function moveFootprint()
 {
     $this->requireParameter("targetCategory");
     $this->requireParameter("id");
     $footprint = Footprint::loadById($this->getParameter("id"));
     $category = FootprintCategory::loadById($this->getParameter("targetCategory"));
     $footprint->setCategory($category);
     PartKeepr::getEM()->flush();
 }
Ejemplo n.º 3
0
 /**
  * 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));
 }
 /**
  * (non-PHPdoc)
  * @see PartKeepr\Service.RestfulService::create()
  */
 public function create()
 {
     $this->requireParameter("tmp_id");
     $this->requireParameter("footprint_id");
     $tmpImage = TempUploadedFile::loadById($this->getParameter("tmp_id"));
     $file = new FootprintAttachment();
     $footprint = Footprint::loadById($this->getParameter("footprint_id"));
     $file->setFootprint($footprint);
     $file->replace($tmpImage->getFilename());
     $file->setOriginalFilename($tmpImage->getOriginalFilename());
     $file->setDescription($this->getParameter("description"));
     PartKeepr::getEM()->persist($file);
     PartKeepr::getEM()->flush();
     return $file->serialize();
 }
Ejemplo n.º 5
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;
         }
     }
 }
Ejemplo n.º 6
0
 /**
  * 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;
 }
Ejemplo n.º 7
0
 /**
  * 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));
 }