awustemann

FisheryInspection Repository (BaseClass)

Dec 6th, 2013
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.10 KB | None | 0 0
  1. <?php
  2. namespace LALLF\Intranet\FisheryInspection\Domain\Repository;
  3.  
  4. /*                                                                        *
  5.  * This script belongs to the TYPO3 Flow package "LALLF.Intranet.FisheryInspection".*
  6.  *                                                                        *
  7.  *                                                                        */
  8.  
  9. use TYPO3\Flow\Annotations as Flow;
  10. use TYPO3\Flow\Persistence\Repository;
  11. use TYPO3\Flow\Persistence\QueryInterface;
  12.  
  13. /**
  14.  * Base class for fisheryInspections repositories
  15.  * implements findAllFiltered
  16.  *
  17.  * @Flow\Scope("singleton")
  18.  */
  19. class FisheryInspectionRepository extends Repository
  20. {
  21.    
  22.     /**
  23.      * Doctrine's Entity Manager.
  24.      * Note that "ObjectManager" is the name of the related interface ...
  25.      *
  26.      * @Flow\Inject
  27.      * @var \Doctrine\Common\Persistence\ObjectManager
  28.      */
  29.     protected $entityManager;
  30.    
  31.     /**
  32.      * @Flow\Inject
  33.      * @var \TYPO3\Flow\Reflection\ReflectionService
  34.      */
  35.     protected $reflectionService;
  36.    
  37.     /**
  38.      * sets the dafaultOrdering by date | DESC
  39.      */
  40.     public function __construct()
  41.     {
  42.         parent::__construct();
  43.        
  44.         $defaultOrderings = array('date' => QueryInterface::ORDER_DESCENDING);
  45.         $this->setDefaultOrderings($defaultOrderings);
  46.     }
  47.  
  48.     /**
  49.      * finds all inspection objects by a given filter
  50.      *
  51.      * @param array $filter
  52.      *              array(
  53.      *                  'date' => '13.08.2013',
  54.      *                  'fisheryBadge' => 'WAR 75N'.
  55.      *                  'depositHarbour' => 'WAR',
  56.      *                  'length' => '12',
  57.      *                  'fast' => 'Warnemünde'
  58.      *              )
  59.      * @return \TYPO3\Flow\Persistence\QueryResultProxy
  60.      */
  61.     public function findAllFiltered($filter)
  62.     {
  63.         $query = $this->createQuery();
  64.        
  65.         $constraints = array();
  66.         foreach($filter as $key => $value)
  67.         {
  68.             if ( ! empty($filter[$key]))
  69.             {
  70.                 if ($key == 'date')
  71.                 {
  72.                     $value = $this->formatGermanDate($value, 'd.m.y');
  73.                 }
  74.                 // like($propertyName, $operand, $caseSensitive = TRUE)
  75.                 $constraints[] = $query->like($key, '%'.$value.'%');
  76.             }
  77.         }
  78.  
  79.         if ( ! empty($constraints))
  80.         {
  81.             $query->matching($query->logicalAnd($constraints));
  82.             return $query->execute();
  83.         }
  84.         else
  85.         {
  86.             return $this->findAll();
  87.         }
  88.     }
  89.    
  90.     // ================= Functions for OneToMany handling ====================================
  91.     /**
  92.      * removes all empty deposit objects and then
  93.      * assigns the parent $object to all remaining deposits
  94.      * we use reflectionService to retrieve nessecary object names
  95.      *
  96.      * @param object $object the parent object
  97.      * @return \TYPO3\Flow\Persistence\Repository\Repository $this
  98.      *
  99.      */
  100.     protected function prepareChildrenForPersistence($object)
  101.     {
  102.         $children = $this->getChildObjects($object);
  103.         if (count($children) == 0) return $this;
  104.        
  105.         // the property which marks a child as being empty is annotated by '@LALLF\ObjectIsEmptyMarker'
  106.         $childClassName = $this->reflectionService->getClassNameByObject($children[0]);
  107.         $childIsEmptyProperty = $this->reflectionService->getPropertyNamesByTag($childClassName, 'objectisemptymarker');
  108.         $childIsEmptyPropertyGetFunctionName = 'get' . ucfirst($childIsEmptyProperty[0]);
  109.        
  110.         // the property which marks the collection of child objects is annotated by '@LALLF\ChildCollection'
  111.         $childrenProperty = $this->reflectionService->getPropertyNamesByTag($this->entityClassName, 'childcollection');
  112.         $childrenPropertyName = $this->reflectionService->getPropertyTagsValues($this->entityClassName, $childrenProperty[0]);
  113.         $childrenPropertyName = $childrenPropertyName["childcollection"][0];
  114.         $childrenPropertyRemoveFunctionName = 'remove' . ucfirst($childrenPropertyName);
  115.  
  116.         // get the child's '@ORM\ManyToOne' / inversedBY property
  117.         $childManyToOneProperty = $this->reflectionService->getPropertyNamesByTag($childClassName, 'manytoone');
  118.         $childManyToOnePropertySetFunctionName = 'set' . ucfirst($childManyToOneProperty[0]);
  119.  
  120.         foreach($children as $child)
  121.         {
  122.             // remove presetted but left empty child objects
  123.             if ( ! $child->$childIsEmptyPropertyGetFunctionName())
  124.             {
  125.                 $object->$childrenPropertyRemoveFunctionName($child);
  126.             }
  127.  
  128.             // set the parent object as property to the child (i.e. the foreign key)
  129.             else
  130.             {
  131.                 $child->$childManyToOnePropertySetFunctionName($object);
  132.             }
  133.         }
  134.         return $this;
  135.     }
  136.    
  137.     /**
  138.      * deletes all child objects of $object physically within the database
  139.      * we use reflectionService to retrieve nessecary object names
  140.      *
  141.      * @param object $object the parent object
  142.      * @return @return \TYPO3\Flow\Persistence\Repository\Repository
  143.      */
  144.     protected function deleteChildren($object)
  145.     {
  146.         $children = $this->getChildObjects($object);
  147.         if (count($children) == 0) return $this;
  148.  
  149.         $childClassName = $this->reflectionService->getClassNameByObject($children[0]);
  150.        
  151.         // get the table name by class annotation '@ORM\Table'
  152.         $classAnnotationTable = $this->reflectionService->getClassAnnotations($childClassName, 'Doctrine\ORM\Mapping\Table');
  153.         $childTableName = $classAnnotationTable[0]->name;
  154.        
  155.         // get the child's '@ORM\JoinColumn' property to find the FK' column name
  156.         $childJoinColumnProperty = (string) $this->reflectionService->getPropertyNamesByTag($childClassName, 'joincolumn')[0];
  157.         $childJoinColumnPropertyAnnotations = $this->reflectionService->getPropertyAnnotations($childClassName, $childJoinColumnProperty);
  158.         $FKColumnName= $childJoinColumnPropertyAnnotations['Doctrine\ORM\Mapping\JoinColumn'][0]->name;
  159.  
  160.         // build and execute the delete statement
  161.         $connection = $this->entityManager->getConnection();
  162.         $sql = 'DELETE FROM ' . $childTableName . ' WHERE ' . $FKColumnName . ' = ?';
  163.  
  164.         /* @var \Doctrine\DBAL\Connection $connection */
  165.         $connection->executeUpdate(
  166.                 $sql,
  167.                 array(
  168.                     $object->getId()
  169.                 )
  170.             );
  171.         return $this;
  172.     }
  173.    
  174.     /**
  175.      * retrieves the child pobjects of the parent $object by using the reflectionService
  176.      * the children property carries the annotation '@LALLF\ChildCollection'
  177.      *
  178.      * @param object $object the parent object
  179.      * @return array
  180.      */
  181.     protected function getChildObjects($object)
  182.     {
  183.         // the property which marks the collection of child objects is annotated by '@LALLF\ChildCollection'
  184.         $childrenProperty = $this->reflectionService->getPropertyNamesByTag($this->entityClassName, 'childcollection');
  185.         $childrenPropertyFunctionName = 'get' . ucfirst($childrenProperty[0]);
  186.         $children = $object->$childrenPropertyFunctionName();
  187.        
  188.         return $children;
  189.     }
  190.    
  191.     /**
  192.      * formats a german date
  193.      *
  194.      * @param string $date
  195.      * @param string $format
  196.      */
  197.     public function formatGermanDate($date, $format)
  198.     {
  199.         // transform into english format
  200.         $dt = explode(' ', $date);
  201.        
  202.         // only m.y given - add 'd'
  203.         $d = explode('.', $dt[0]);
  204.         if (count($d) == 2)
  205.         {
  206.             $dt[0] = '01.' . $dt[0];
  207.         }
  208.         $date = preg_replace('#^(\d{1,2})\.(\d{1,2})\.(\d{2,4})$#', '\3-\2-\1', $dt[0]);
  209.         if (key_exists(1, $dt))
  210.         {
  211.             $date = $date . ' ' . $dt[1];
  212.         }
  213.  
  214.         // build a DateTime object based on the english format
  215.         try
  216.         {
  217.             $date = new \DateTime($date);
  218.         }
  219.         catch (\Exception $e)
  220.         {
  221.             return $date;
  222.         }
  223.         return $date->format($format);
  224.     }
  225.    
  226.  
  227. }
  228. ?>
Advertisement
Add Comment
Please, Sign In to add comment