Advertisement
Eddz

Untitled

Sep 3rd, 2013
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.64 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Fcm\Entity\Repository;
  4.  
  5. use DoctrineORMModule\Paginator\Adapter\DoctrinePaginator as PaginatorAdapter;
  6. use Doctrine\ORM\Tools\Pagination\Paginator as ORMPaginator;
  7. use Zend\Paginator\Paginator as ZendPaginator;
  8. use Doctrine\ORM\EntityRepository;
  9.  
  10. class ClientRepository extends EntityRepository implements \Doctrine\Common\Persistence\ObjectRepository
  11. {
  12.  
  13.     /**
  14.      * @var \Zend\Escaper\Escaper
  15.      */
  16.     private static $escaperInstance = NULL;
  17.  
  18.     // Pattern Singleton for EscaperInstance
  19.     public static function getEscaperInstance()
  20.     {
  21.         if (!self::$escaperInstance)
  22.             self::$escaperInstance = new \Zend\Escaper\Escaper('utf-8');
  23.  
  24.         return self::$escaperInstance;
  25.     }
  26.  
  27.     /**
  28.      * Display all Entities with pagination and sorting system
  29.      *
  30.      * @param string $order_by the field to sort
  31.      * @param string $order the type of sorting (ASC or DESC)
  32.      * @param integer $page the current page
  33.      *
  34.      * @return object ZendPaginator
  35.      */
  36.     public function displayAllEntities($order_by, $order, $page)
  37.     {
  38.         // Creation query
  39.         $qb = $this->getEntityManager()->createQueryBuilder();
  40.         $qb->add('select', 'c')
  41.                 ->add('from', 'Fcm\Entity\client c')
  42.                 ->add('orderBy', 'c.id DESC');
  43.         if ($order_by != null) {
  44.             if ($order == 'ASC') {
  45.                 $qb->add('orderBy', 'c.' . self::getEscaperInstance()->escapeHtmlAttr($order_by) . ' ASC');
  46.             } elseif ($order == 'DESC') {
  47.                 $qb->add('orderBy', 'c.' . self::getEscaperInstance()->escapeHtmlAttr($order_by) . ' DESC');
  48.             }
  49.         }
  50.         $qb->setFirstResult(1)
  51.                 ->setMaxResults(5);
  52.         $query = $qb->getQuery();
  53.  
  54.         // Creation paginator
  55.         $paginator = new ZendPaginator(new PaginatorAdapter(new ORMPaginator($query)));
  56.         $paginator
  57.                 ->setCurrentPageNumber($page)
  58.                 ->setItemCountPerPage(5)
  59.                 ->setPageRange(10);
  60.  
  61.         return $paginator;
  62.     }
  63.  
  64.     /**
  65.      * Search Sytem
  66.      *
  67.      * @param string $order_by the field to sort
  68.      * @param string $order the type of sorting (ASC or DESC)
  69.      * @param integer $page the current page
  70.      *
  71.      * @return object ZendPaginator
  72.      */
  73.     public function search($key)
  74.     {
  75.         //Creation query
  76.         $qb = $this->getEntityManager()->createQueryBuilder();
  77.         $qb->add('select', 'c')
  78.                 ->add('from', 'Fcm\Entity\client c')
  79.                 ->add('orderBy', 'c.id ASC');
  80.         $qb->orWhere('c.id LIKE :key')
  81.                 ->orWhere('c.name LIKE :key')
  82.                 ->orWhere('c.website LIKE :key')
  83.                 ->orWhere('c.phone LIKE :key')
  84.                 ->orWhere('c.email LIKE :key')
  85.                 ->orWhere('c.contact LIKE :key')
  86.                 ->orWhere('c.comment LIKE :key')
  87.                 ->setParameter('key', '%' . $key . '%');
  88.  
  89.         $qb->setFirstResult(1)
  90.                 ->setMaxResults(5);
  91.         $query = $qb->getQuery();
  92.  
  93.         // Creation paginator
  94.         $paginator = new ZendPaginator(new PaginatorAdapter(new ORMPaginator($query)));
  95.         $paginator
  96.                 ->setCurrentPageNumber(1)
  97.                 ->setItemCountPerPage(5)
  98.                 ->setPageRange(10);
  99.  
  100.         return $paginator;
  101.     }
  102.  
  103.     /**
  104.      * Finds an entity by its primary key / identifier.
  105.      *
  106.      * @param string $order_by the field to sort
  107.      *
  108.      * @return object
  109.      */
  110.     public function findEntity($id)
  111.     {
  112.         $client = $this->getEntityManager()->find("Fcm\Entity\Client", $id);
  113. //       $campaign = $this->getEntityManager()->find("Fcm\Entity\Campaign", 1);
  114. //       $campaign->client = $client;
  115. //       $em = $this->getEntityManager();
  116. //        $em->persist($campaign);
  117. //        $em->flush();
  118. //       die(var_dump($campaign->typeCampaign->type));
  119.         return $client;
  120.     }
  121.  
  122.     /**
  123.      * Finds all entities as a array
  124.      *
  125.      * @return Array
  126.      */
  127.     public function findAllNameId($client_id = null)
  128.     {
  129.         if ($client_id != null) {
  130.             $query = $this->getEntityManager()->createQuery('SELECT c.id,c.name FROM Fcm\Entity\Client c WHERE c.id = :client_id');
  131.             $query->setParameter('client_id', $client_id);
  132.         } else {
  133.             $query = $this->getEntityManager()->createQuery('SELECT c.id,c.name FROM Fcm\Entity\Client c');
  134.         }
  135.         $entities = $query->getArrayResult();
  136.  
  137.         return $entities;
  138.     }
  139.  
  140.     /**
  141.      */
  142.     public function checkIfEmailExists($email)
  143.     {
  144.         $query = $this->getEntityManager()->createQuery('SELECT c.email FROM Fcm\Entity\Client c WHERE c.email= :email');
  145.         $query->setParameter('email', $email);
  146.         return $query->getResult();
  147.     }
  148.     /**
  149.      * Finds an entity by its primary key / identifier.
  150.      *
  151.      * @param string $order_by the field to sort
  152.      *
  153.      * @return object
  154.      */
  155.     public function insert(array $data)
  156.     {
  157.         $entity = $this->getEntityName();
  158.         $entity = new $entity;
  159.         $entity->populate($data);
  160.         $em = $this->getEntityManager();
  161.         $em->persist($entity);
  162.         $em->flush();
  163.         return $entity;
  164.     }
  165.  
  166.     /**
  167.      * Update an entity
  168.      *
  169.      * @param array $data
  170.      *
  171.      * @return object
  172.      */
  173.     public function update(array $data)
  174.     {
  175.         $entity = $this->getReference($data['id']);
  176.         $entity->populate($data);
  177.         $em = $this->getEntityManager();
  178.         $em->persist($entity);
  179.         $em->flush();
  180.  
  181.         return $entity;
  182.     }
  183.  
  184.     /**
  185.      * Delete an entity by its primary key / identifier.
  186.      *
  187.      * @param string $order_by the field to sort
  188.      *
  189.      * @return object
  190.      */
  191.     public function delete($id)
  192.     {
  193.         $entity = $this->getReference($id);
  194.         if ($entity) {
  195.             $em = $this->getEntityManager();
  196.             $em->remove($entity);
  197.             $em->flush();
  198.             return $entity;
  199.         }
  200.     }
  201.  
  202.     /**
  203.      * Create entity
  204.      *
  205.      * @param array $data entity data
  206.      *
  207.      * @return object
  208.      */
  209.     protected function createEntity(array $data)
  210.     {
  211.         $entity = $this->getEntityName();
  212.         return new $entity($data);
  213.     }
  214.  
  215.     /**
  216.      * Get reference entity
  217.      *
  218.      * @param integer $id id entity
  219.      *
  220.      * @return object
  221.      */
  222.     protected function getReference($id, $entity = null)
  223.     {
  224.         if (null === $entity) {
  225.             $entity = $this->getEntityName();
  226.         }
  227.         return $this->getEntityManager()->getReference($entity, $id);
  228.     }
  229.  
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement