Commits

Vasiliy Toporov committed 6955969

Jobs by category and active Jobs.

Comments (0)

Files changed (8)

app/config/config.yml

     username:  "%mailer_user%"
     password:  "%mailer_password%"
     spool:     { type: memory }
+
+parameters:
+    max_jobs_on_homepage: 10

src/Ens/JobeetBundle/Controller/JobController.php

     {        
         $em = $this->getDoctrine()->getEntityManager();
  
-        $entities = $em->getRepository('EnsJobeetBundle:Job')->getActiveJobs();
+        $categories = $em->getRepository('EnsJobeetBundle:Category')->getWithJobs();
+ 
+        foreach($categories as $category)
+        {
+            $category->setActiveJobs($em->getRepository('EnsJobeetBundle:Job')->getActiveJobs($category->getId(), $this->container->getParameter('max_jobs_on_homepage')));
+        }
 
         return $this->render('EnsJobeetBundle:Job:index.html.twig', array(
-          'entities' => $entities
+          'categories' => $categories
         ));
     }
 
     {
         $em = $this->getDoctrine()->getManager();
 
-        $entity = $em->getRepository('EnsJobeetBundle:Job')->find($id);
+        $entity = $em->getRepository('EnsJobeetBundle:Job')->getActiveJob($id);
 
         if (!$entity) {
             throw $this->createNotFoundException('Unable to find Job entity.');

src/Ens/JobeetBundle/DataFixtures/ORM/LoadJobData.php

     $em->persist($job_sensio_labs);
     $em->persist($job_extreme_sensio);
     $em->persist($job_expired);
+    
+    for($i = 100; $i <= 130; $i++)
+    {
+        $job = new Job();
+        $job->setCategory($em->merge($this->getReference('category-programming')));
+        $job->setType('full-time');
+        $job->setCompany('Company '.$i);
+        $job->setPosition('Web Developer');
+        $job->setLocation('Paris, France');
+        $job->setDescription('Lorem ipsum dolor sit amet, consectetur adipisicing elit.');
+        $job->setHowToApply('Send your resume to lorem.ipsum [at] dolor.sit');
+        $job->setIsPublic(true);
+        $job->setIsActivated(true);
+        $job->setToken('job_'.$i);
+        $job->setEmail('job@example.com');
+
+        $em->persist($job);
+    }
  
     $em->flush();
   }

src/Ens/JobeetBundle/Entity/Category.php

      * @var \Doctrine\Common\Collections\ArrayCollection
      */
     private $category_affiliates;
+    
+    /**
+     * @var \Doctrine\Common\Collections\ArrayCollection
+     */
+    private $active_jobs;
 
     /**
      * Constructor
     {
         return $this->getName();
     }
+    
+    public function setActiveJobs($jobs)
+    {
+        $this->active_jobs = $jobs;
+    }
+
+    public function getActiveJobs()
+    {
+        return $this->active_jobs;
+    }
 }

src/Ens/JobeetBundle/Repository/CategoryRepository.php

+<?php
+
+namespace Ens\JobeetBundle\Repository;
+
+use Doctrine\ORM\EntityRepository;
+
+/**
+ * CategoryRepository
+ *
+ * This class was generated by the Doctrine ORM. Add your own custom
+ * repository methods below.
+ */
+class CategoryRepository extends EntityRepository
+{
+    public function getWithJobs()
+    {
+        $query = $this->getEntityManager()->createQuery(
+          'SELECT c FROM EnsJobeetBundle:Category c LEFT JOIN c.jobs j WHERE j.expires_at > :date'
+        )->setParameter('date', date('Y-m-d H:i:s', time()));
+
+        return $query->getResult();
+    }
+}

src/Ens/JobeetBundle/Repository/JobRepository.php

  */
 class JobRepository extends EntityRepository
 {
-    public function getActiveJobs($category_id = null)
+    public function getActiveJobs($category_id = null, $max = null)
     {
         $qb = $this->createQueryBuilder('j')
           ->where('j.expires_at > :date')
           ->setParameter('date', date('Y-m-d H:i:s', time()))
           ->orderBy('j.expires_at', 'DESC');
+        
+        if($max)
+        {
+            $qb->setMaxResults($max);
+        }
 
         if($category_id)
         {
 
         return $query->getResult();
     }
+    
+    public function getActiveJob($id)
+    {
+        $query = $this->createQueryBuilder('j')
+          ->where('j.id = :id')
+          ->setParameter('id', $id)
+          ->andWhere('j.expires_at > :date')
+          ->setParameter('date', date('Y-m-d H:i:s', time()))
+          ->setMaxResults(1)
+          ->getQuery();
+
+        try {
+            $job = $query->getSingleResult();
+        } catch (\Doctrine\Orm\NoResultException $e) {
+            $job = null;
+        }
+
+        return $job;
+    }
 }

src/Ens/JobeetBundle/Resources/config/doctrine/Category.orm.yml

 # src/Ens/JobeetBundle/Resources/config/doctrine/Category.orm.yml
 Ens\JobeetBundle\Entity\Category:
   type: entity
+  repositoryClass: Ens\JobeetBundle\Repository\CategoryRepository
   table: category
   id:
     id:

src/Ens/JobeetBundle/Resources/views/Job/index.html.twig

 {% endblock %}
 
 {% block content %}
-    <div id="jobs">
-      <table class="jobs">
-        {% for entity in entities %}
-          <tr class="{{ cycle(['even', 'odd'], loop.index) }}">
-            <td class="location">{{ entity.location }}</td>
-            <td class="position">
-              <a href="{{ path('ens_job_show', { 'id': entity.id, 'company': entity.companyslug, 'location': entity.locationslug, 'position': entity.positionslug}) }}">
-                {{ entity.position }}
-              </a>
-            </td>
-            <td class="company">{{ entity.company }}</td>
-          </tr>
-        {% endfor %}
-      </table>
-    </div>
+  <div id="jobs">
+    {% for category in categories %}
+      <div>
+        <div class="category">
+          <div class="feed">
+            <a href="">Feed</a>
+          </div>
+          <h1>{{ category.name }}</h1>
+        </div>
+        <table class="jobs">
+          {% for entity in category.activejobs %}
+            <tr class="{{ cycle(['even', 'odd'], loop.index) }}">
+              <td class="location">{{ entity.location }}</td>
+              <td class="position">
+                <a href="{{ path('ens_job_show', { 'id': entity.id, 'company': entity.companyslug, 'location': entity.locationslug, 'position': entity.positionslug }) }}">
+                  {{ entity.position }}
+                </a>
+              </td>
+              <td class="company">{{ entity.company }}</td>
+            </tr>
+          {% endfor %}
+        </table>
+      </div>
+    {% endfor %}
+  </div>
 {% endblock %}