public function get () { $qb = PartKeepr::getEM()->createQueryBuilder(); $qb->select("se")->from("de\RaumZeitLabor\PartKeepr\Stock\StockEntry","se") ->where("se.part = :part") ->orderBy("se.dateTime", "DESC") ->setParameter("part", $this->getParameter("item")); $results = $qb->getQuery()->getResult(); $aData = array(); foreach ($results as $result) { $aData[] = array( "username" => is_object($result->getUser()) ? $result->getUser()->getUsername() : PartKeepr::i18n("Unknown User"), "amount" => abs($result->getStockLevel()), "datetime" => $result->getDateTime()->format("Y-m-d H:i:s"), "id" => $result->getId(), "direction" => ($result->getStockLevel() < 0) ? "out" : "in", "price" => $result->getPrice() ); } return array("data" => $aData); }
/** * Loads the entity from the database. * @param integer $id The entity's id */ public static function loadById ($id) { $entity = PartKeepr::getEM()->find(get_called_class(), $id); if (!is_object($entity)) { throw new EntityNotFoundException(get_called_class(), $id); } return $entity; }
public function destroy () { $this->requireParameter("id"); $logo = ManufacturerICLogo::loadById($this->getParameter("id")); PartKeepr::getEM()->remove($logo); PartKeepr::getEM()->flush(); return array("data" => null); }
/** * (non-PHPdoc) * @see de\RaumZeitLabor\PartKeepr\Service.RestfulService::update() */ public function update () { $this->requireParameter("id"); $this->requireParameter("name"); $manufacturer = ManufacturerManager::getInstance()->getManufacturer($this->getParameter("id")); $this->setManufacturerData($manufacturer); PartKeepr::getEM()->flush(); return array("data" => $manufacturer->serialize()); }
/** * (non-PHPdoc) * @see de\RaumZeitLabor\PartKeepr\Service.RestfulService::update() */ public function update () { $this->requireParameter("id"); $this->requireParameter("name"); $distributor = DistributorManager::getInstance()->getDistributor($this->getParameter("id")); $this->setDistributorData($distributor); PartKeepr::getEM()->flush(); return array("data" => $distributor->serialize()); }
/** * (non-PHPdoc) * @see de\RaumZeitLabor\PartKeepr\Service.RestfulService::update() */ public function update () { $this->requireParameter("id"); $this->requireParameter("footprint"); $footprint = FootprintManager::getInstance()->getFootprint($this->getParameter("id")); $footprint->setName($this->getParameter("footprint")); $footprint->setDescription($this->getParameter("description")); PartKeepr::getEM()->flush(); return array("data" => $footprint->serialize()); }
public function update () { $this->requireParameter("id"); $this->requireParameter("name"); $unit = UnitManager::getInstance()->getUnit($this->getParameter("id")); $this->setUnitData($unit); PartKeepr::getEM()->flush(); return array("data" => $unit->serialize()); }
/** * Updates the given category. */ public function update () { $this->requireParameter("id"); $this->requireParameter("name"); $category = CategoryManager::getInstance()->getCategory($this->getParameter("id")); $category->getNode()->setName($this->getParameter("name")); $category->getNode()->setDescription($this->getParameter("description", "")); PartKeepr::getEM()->persist($category->getNode()); return array("data" => $this->serializeCategory($category)); }
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; }
public function resumeSession ($session) { $query = PartKeepr::getEM()->createQuery("SELECT s FROM de\\RaumZeitLabor\\PartKeepr\\Session\\Session s WHERE s.sessionid = :session"); $query->setParameter("session", $session); $query->execute(); try { self::$currentSession = $query->getSingleResult(); return self::$currentSession; } catch (\Doctrine\ORM\NonUniqueResultException $e) { throw new \Exception("Fatal error: Multiple sessions with id $session found."); } catch (\Doctrine\ORM\NoResultException $e) { throw new SessionNotFoundException($session); } }
public function testLogin () { $user = new User; $user->setUsername("test"); $user->setPassword("test"); $this->userManager->createUser($user); PartKeepr::getEM()->flush(); $user2 = new User; $user2->setRawUsername("2test"); $user2->setPassword("test"); /* User shouldn't be able to login, even if we cut numbers and special chars from usernames */ $this->setExpectedException("de\RaumZeitLabor\PartKeepr\Auth\Exceptions\InvalidLoginDataException"); $this->userManager->authenticate($user2); }
public function createSnapshot () { $snapshot = new StatisticSnapshot(); $snapshot->setParts(PartManager::getInstance()->getPartCount()); $snapshot->setCategories(CategoryManager::getInstance()->getCategoryCount()); $result = PartUnitManager::getInstance()->getUnitCounts(); foreach ($result as $row) { $snapshotUnit = new StatisticSnapshotUnit(); $snapshotUnit->setPartUnit($row[0]); $snapshotUnit->setStatisticSnapshot($snapshot); $snapshotUnit->setStockLevel($row["stockLevel"]); $snapshot->getUnits()->add($snapshotUnit); } PartKeepr::getEM()->persist($snapshot); PartKeepr::getEM()->flush(); }
public function upload () { $tmpFile = new TempUploadedFile(); if (array_key_exists("userfile", $_FILES) && file_exists($_FILES["userfile"]["tmp_name"])) { $file = $_FILES['userfile']['tmp_name']; $filename = $_FILES['userfile']['name']; $tmpFile->replace($file); $tmpFile->setOriginalFilename(basename($filename)); } elseif (array_key_exists("url", $_REQUEST)) { $tmpFile->replaceFromURL($_REQUEST["url"]); } else { throw new \Exception("Error: No valid file given"); } PartKeepr::getEM()->persist($tmpFile); PartKeepr::getEM()->flush(); return array("id" => $tmpFile->getId(), "extension" => $tmpFile->getExtension(), "size" => $tmpFile->getSize(), "originalFilename" => $tmpFile->getOriginalFilename()); }
/** * Returns a list of manufacturer ic logos. * * @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 manufacturer id */ public function getManufacturerICLogos ($start = 0, $limit = 10, $sort = "name", $dir = "asc", $filter = "") { $qb = PartKeepr::getEM()->createQueryBuilder(); $qb->select("st.id, maf.id AS manufacturer_id")->from("de\RaumZeitLabor\PartKeepr\Manufacturer\ManufacturerICLogo","st") ->leftJoin('st.manufacturer', "maf"); if ($filter != "") { $manufacturer = Manufacturer::loadById($filter); $qb = $qb->where("st.manufacturer = :manufacturer"); $qb->setParameter("manufacturer", $manufacturer); } 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("de\RaumZeitLabor\PartKeepr\Manufacturer\ManufacturerICLogo","st"); if ($filter != "") { $totalQueryBuilder = $totalQueryBuilder->where("st.manufacturer = :manufacturer"); $totalQueryBuilder->setParameter("manufacturer", $manufacturer); } $totalQuery = $totalQueryBuilder->getQuery(); return array("data" => $result, "totalCount" => $totalQuery->getSingleScalarResult()); }
public function deletePartDistributor ($id) { $manufacturer = $this->getManufacturer($id); PartKeepr::getEM()->remove($manufacturer); PartKeepr::getEM()->flush(); }
/** * Scales the image to fit within the given size. * * @param int $w The width * @param int $h The height * @param boolean $padding If true, pad the output image to the given size (transparent background). * @return string The path to the scaled file */ public function fitWithin ($w, $h, $padding = false) { $this->ensureCachedirExists(); if ($padding) { $pd = "p"; } else { $pd = ""; } $outputFile = Configuration::getOption("partkeepr.images.cache").md5($this->getFilename().$w."x".$h."fw".$pd).".png"; if (file_exists($outputFile)) { return $outputFile; } $image = new \Imagick(); $image->readImage($this->getFilename()); $sourceAspectRatio = $image->getImageWidth() / $image->getImageHeight(); $targetAspectRatio = $w / $h; $filter = \Imagick::FILTER_UNDEFINED; $blur = 1; $targetHeight = $h; $targetWidth = $w; if ($sourceAspectRatio < $targetAspectRatio) { $targetWidth = $h * $sourceAspectRatio; $image->resizeImage($h * $sourceAspectRatio, $h, $filter, $blur); } else { $targetHeight = $w / $sourceAspectRatio; $image->resizeImage($w, $w / $sourceAspectRatio, $filter, $blur); } if ($padding) { $posX = intval(($w - $targetWidth) / 2); $posY = intval(($h - $targetHeight) / 2); $image->extentImage($w, $h,-$posX, -$posY); } $image->writeImage($outputFile); $cachedImage = new CachedImage($this, $outputFile); PartKeepr::getEM()->persist($cachedImage); return $outputFile; }
/** * (non-PHPdoc) * @see de\RaumZeitLabor\PartKeepr\Service.RestfulService::destroy() */ public function destroy () { $this->requireParameter("id"); $file = FootprintAttachment::loadById($this->getParameter("id")); PartKeepr::getEM()->remove($file); PartKeepr::getEM()->flush(); return array("data" => null); }
/** * Updates the average price for a part */ public function updatePrice () { $query = PartKeepr::getEM()->createQuery("SELECT SUM(se.price*se.stockLevel) / SUM(se.stockLevel) FROM de\RaumZeitLabor\PartKeepr\Stock\StockEntry se WHERE se.part = :part AND se.stockLevel > 0"); $query->setParameter("part", $this->part); $val = $query->getSingleScalarResult(); $query = PartKeepr::getEM()->createQuery('UPDATE de\RaumZeitLabor\PartKeepr\Part\Part p SET p.averagePrice = :val WHERE p = :part'); $query->setParameter("val", $val); $query->setParameter("part", $this->part); $query->execute(); }
/** * Deletes a distributor by id * * @param int $id The ID of the distributor to delete */ public function deleteDistributor ($id) { $distributor = $this->getDistributor($id); PartKeepr::getEM()->remove($distributor); PartKeepr::getEM()->flush(); }
/** * Returns the class metadata for all entity classes * @return array an array of class metadata objects */ public static function getClassMetaData () { $classes = self::getEntityClasses(); $aClasses = array(); foreach ($classes as $class) { $aClasses[] = PartKeepr::getEM()->getClassMetadata($class); } return $aClasses; }
/** * Authenticates the given user. If successful, an instance * of the user is returned. * * @param User $user The user to authenticate * @throws InvalidLoginDataException Thrown if the user's credentials are not valid */ public function authenticate (User $user) { $result = PartKeepr::getEM() ->getRepository("de\RaumZeitLabor\PartKeepr\User\User") ->findOneBy( array( "username" => $user->getUsername(), "password" => $user->getHashedPassword() ) ); if ($result == null) { throw new InvalidLoginDataException(); } else { return $result; } }
<?php namespace de\RaumZeitLabor\PartKeepr\Tests; declare(encoding = 'UTF-8'); use de\raumzeitlabor\PartKeepr\PartKeepr; include(dirname(__DIR__). "/src/de/RaumZeitLabor/PartKeepr/PartKeepr.php"); PartKeepr::initialize("test"); $tool = new \Doctrine\ORM\Tools\SchemaTool(PartKeepr::getEM()); $classes = PartKeepr::getClassMetaData(); $tool->dropSchema($classes); $tool->createSchema($classes); ?>
public function get () { $query = PartKeepr::getEM()->createQuery("SELECT si.id, si.prefix, si.symbol, si.power FROM de\RaumZeitLabor\PartKeepr\SiPrefix\SiPrefix si"); return array("data" => $query->getArrayResult()); }
/** * Removes all caches for a specific image. This is usually called * when a new version of an image is uploaded. * * Automatically calls "flush" on the entity manager. * * @param Image $image The image to invalidate */ public static function invalidate (Image $image) { $qb = PartKeepr::getEM()->createQueryBuilder(); $qb->select(array("c")) ->from('de\RaumZeitLabor\PartKeepr\Image\CachedImage', 'c') ->where("c.originalId = :id") ->andWhere("c.originalType = :type") ->setParameter("id", $image->getId()) ->setParameter("type", $image->getType()); $query = $qb->getQuery(); $bImagesRemoved = false; foreach ($query->getResult() as $file) { unlink($file->getCacheFile()); PartKeepr::getEM()->remove($file); $bImagesRemoved = true; } if ($bImagesRemoved) { PartKeepr::getEM()->flush(); } }
public static function call () { $request = new Request(array('restful' => true)); $service = $request->getService(); //print_r($request->action); if ($service->hasHeader("call")) { $call = $service->getHeader("call"); } elseif (array_key_exists("call", $_REQUEST) && $_REQUEST["call"] != "") { $call = $_REQUEST["call"]; } elseif ($request->action != "") { $call = $request->action; } else { switch (strtoupper($request->getMethod())) { case "POST": $call = "create"; break; case "GET": $call = "get"; break; case "PUT": $call = "update"; break; case "DELETE": $call = "destroy"; break; default: $call = $request->getMethod(); break; } } $allowCall = true; if (!is_subclass_of($service, "de\\RaumZeitLabor\\PartKeepr\\Service\\AnonService")) { $session = null; if ($service->hasHeader("session")) { $sessionid = $service->getHeader("session"); } if (array_key_exists("session", $_REQUEST) && $session === null) { $sessionid = $_REQUEST["session"]; } if ($sessionid === null) { $session = SessionManager::getInstance()->startSession(); throw new ServiceException("You called a non-anonymous service, but did not pass the 'session' parameter."); } else { $session = SessionManager::getInstance()->resumeSession($sessionid); } if (!$service->mayCall($call)) { $allowCall = false; } } if (!$allowCall) { throw new ServiceException("Permission denied"); } $result = $service->$call(); PartKeepr::getEM()->flush(); return $result; }
/** * 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 de\RaumZeitLabor\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; }
<?php use de\RaumZeitLabor\PartKeepr\Service\ServiceManager; use de\RaumZeitLabor\PartKeepr\PartKeepr; use Doctrine\Common\ClassLoader; include("src/de/RaumZeitLabor/PartKeepr/PartKeepr.php"); PartKeepr::initialize(""); $em = PartKeepr::getEM(); $classes = PartKeepr::getEntityClasses(); $helperSet = new \Symfony\Component\Console\Helper\HelperSet(array( 'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()), 'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em) ));
public function deleteStock () { $part = PartManager::getInstance()->getPart($this->getParameter("part")); $user = SessionManager::getCurrentSession()->getUser(); $stock = new StockEntry($part, 0-intval($this->getParameter("stock")), $user); PartKeepr::getEM()->persist($stock); PartKeepr::getEM()->flush(); $part->updateStockLevel(); PartKeepr::getEM()->flush(); return true; }
public function saveStorageLocation () { $this->requireParameter("id"); $this->requireParameter("name"); $storageLocation = StorageLocationManager::getInstance()->getStorageLocation($this->getParameter("id")); $storageLocation->setName($this->getParameter("name")); PartKeepr::getEM()->flush(); return $storageLocation->serialize(); }
$oPartParameter->setValue($val); $oPartParameter->setSiPrefix($prefix); PartKeepr::getEM()->persist($oPartParameter); } $fc++; if ($fc>100) { PartKeepr::getEM()->flush(); $fc=0; } } PartKeepr::getEM()->flush(); echo "All done.\n"; apc_clear_cache(); apc_clear_cache("user"); function convertText ($string) { $string = stripslashes($string); $string = html_entity_decode($string, ENT_QUOTES, 'UTF-8');