Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace LALLF\Intranet\FisheryInspection\Domain\Repository;
- /* *
- * This script belongs to the TYPO3 Flow package "LALLF.Intranet.FisheryInspection".*
- * *
- * */
- use TYPO3\Flow\Annotations as Flow;
- use TYPO3\Flow\Persistence\Repository;
- use TYPO3\Flow\Persistence\QueryInterface;
- /**
- * Base class for fisheryInspections repositories
- * implements findAllFiltered
- *
- * @Flow\Scope("singleton")
- */
- class FisheryInspectionRepository extends Repository
- {
- /**
- * Doctrine's Entity Manager.
- * Note that "ObjectManager" is the name of the related interface ...
- *
- * @Flow\Inject
- * @var \Doctrine\Common\Persistence\ObjectManager
- */
- protected $entityManager;
- /**
- * @Flow\Inject
- * @var \TYPO3\Flow\Reflection\ReflectionService
- */
- protected $reflectionService;
- /**
- * sets the dafaultOrdering by date | DESC
- */
- public function __construct()
- {
- parent::__construct();
- $defaultOrderings = array('date' => QueryInterface::ORDER_DESCENDING);
- $this->setDefaultOrderings($defaultOrderings);
- }
- /**
- * finds all inspection objects by a given filter
- *
- * @param array $filter
- * array(
- * 'date' => '13.08.2013',
- * 'fisheryBadge' => 'WAR 75N'.
- * 'depositHarbour' => 'WAR',
- * 'length' => '12',
- * 'fast' => 'Warnemünde'
- * )
- * @return \TYPO3\Flow\Persistence\QueryResultProxy
- */
- public function findAllFiltered($filter)
- {
- $query = $this->createQuery();
- $constraints = array();
- foreach($filter as $key => $value)
- {
- if ( ! empty($filter[$key]))
- {
- if ($key == 'date')
- {
- $value = $this->formatGermanDate($value, 'd.m.y');
- }
- // like($propertyName, $operand, $caseSensitive = TRUE)
- $constraints[] = $query->like($key, '%'.$value.'%');
- }
- }
- if ( ! empty($constraints))
- {
- $query->matching($query->logicalAnd($constraints));
- return $query->execute();
- }
- else
- {
- return $this->findAll();
- }
- }
- // ================= Functions for OneToMany handling ====================================
- /**
- * removes all empty deposit objects and then
- * assigns the parent $object to all remaining deposits
- * we use reflectionService to retrieve nessecary object names
- *
- * @param object $object the parent object
- * @return \TYPO3\Flow\Persistence\Repository\Repository $this
- *
- */
- protected function prepareChildrenForPersistence($object)
- {
- $children = $this->getChildObjects($object);
- if (count($children) == 0) return $this;
- // the property which marks a child as being empty is annotated by '@LALLF\ObjectIsEmptyMarker'
- $childClassName = $this->reflectionService->getClassNameByObject($children[0]);
- $childIsEmptyProperty = $this->reflectionService->getPropertyNamesByTag($childClassName, 'objectisemptymarker');
- $childIsEmptyPropertyGetFunctionName = 'get' . ucfirst($childIsEmptyProperty[0]);
- // the property which marks the collection of child objects is annotated by '@LALLF\ChildCollection'
- $childrenProperty = $this->reflectionService->getPropertyNamesByTag($this->entityClassName, 'childcollection');
- $childrenPropertyName = $this->reflectionService->getPropertyTagsValues($this->entityClassName, $childrenProperty[0]);
- $childrenPropertyName = $childrenPropertyName["childcollection"][0];
- $childrenPropertyRemoveFunctionName = 'remove' . ucfirst($childrenPropertyName);
- // get the child's '@ORM\ManyToOne' / inversedBY property
- $childManyToOneProperty = $this->reflectionService->getPropertyNamesByTag($childClassName, 'manytoone');
- $childManyToOnePropertySetFunctionName = 'set' . ucfirst($childManyToOneProperty[0]);
- foreach($children as $child)
- {
- // remove presetted but left empty child objects
- if ( ! $child->$childIsEmptyPropertyGetFunctionName())
- {
- $object->$childrenPropertyRemoveFunctionName($child);
- }
- // set the parent object as property to the child (i.e. the foreign key)
- else
- {
- $child->$childManyToOnePropertySetFunctionName($object);
- }
- }
- return $this;
- }
- /**
- * deletes all child objects of $object physically within the database
- * we use reflectionService to retrieve nessecary object names
- *
- * @param object $object the parent object
- * @return @return \TYPO3\Flow\Persistence\Repository\Repository
- */
- protected function deleteChildren($object)
- {
- $children = $this->getChildObjects($object);
- if (count($children) == 0) return $this;
- $childClassName = $this->reflectionService->getClassNameByObject($children[0]);
- // get the table name by class annotation '@ORM\Table'
- $classAnnotationTable = $this->reflectionService->getClassAnnotations($childClassName, 'Doctrine\ORM\Mapping\Table');
- $childTableName = $classAnnotationTable[0]->name;
- // get the child's '@ORM\JoinColumn' property to find the FK' column name
- $childJoinColumnProperty = (string) $this->reflectionService->getPropertyNamesByTag($childClassName, 'joincolumn')[0];
- $childJoinColumnPropertyAnnotations = $this->reflectionService->getPropertyAnnotations($childClassName, $childJoinColumnProperty);
- $FKColumnName= $childJoinColumnPropertyAnnotations['Doctrine\ORM\Mapping\JoinColumn'][0]->name;
- // build and execute the delete statement
- $connection = $this->entityManager->getConnection();
- $sql = 'DELETE FROM ' . $childTableName . ' WHERE ' . $FKColumnName . ' = ?';
- /* @var \Doctrine\DBAL\Connection $connection */
- $connection->executeUpdate(
- $sql,
- array(
- $object->getId()
- )
- );
- return $this;
- }
- /**
- * retrieves the child pobjects of the parent $object by using the reflectionService
- * the children property carries the annotation '@LALLF\ChildCollection'
- *
- * @param object $object the parent object
- * @return array
- */
- protected function getChildObjects($object)
- {
- // the property which marks the collection of child objects is annotated by '@LALLF\ChildCollection'
- $childrenProperty = $this->reflectionService->getPropertyNamesByTag($this->entityClassName, 'childcollection');
- $childrenPropertyFunctionName = 'get' . ucfirst($childrenProperty[0]);
- $children = $object->$childrenPropertyFunctionName();
- return $children;
- }
- /**
- * formats a german date
- *
- * @param string $date
- * @param string $format
- */
- public function formatGermanDate($date, $format)
- {
- // transform into english format
- $dt = explode(' ', $date);
- // only m.y given - add 'd'
- $d = explode('.', $dt[0]);
- if (count($d) == 2)
- {
- $dt[0] = '01.' . $dt[0];
- }
- $date = preg_replace('#^(\d{1,2})\.(\d{1,2})\.(\d{2,4})$#', '\3-\2-\1', $dt[0]);
- if (key_exists(1, $dt))
- {
- $date = $date . ' ' . $dt[1];
- }
- // build a DateTime object based on the english format
- try
- {
- $date = new \DateTime($date);
- }
- catch (\Exception $e)
- {
- return $date;
- }
- return $date->format($format);
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment