Commits

Vasiliy Toporov committed a0eb8cf

Complete Day 7.

  • Participants
  • Parent commits 6955969

Comments (0)

Files changed (11)

File app/config/config.yml

     spool:     { type: memory }
 
 parameters:
-    max_jobs_on_homepage: 10
+    max_jobs_on_homepage: 10
+    max_jobs_on_category: 20

File src/Ens/JobeetBundle/Controller/CategoryController.php

+<?php
+
+namespace Ens\JobeetBundle\Controller;
+use Symfony\Bundle\FrameworkBundle\Controller\Controller;
+use Ens\JobeetBundle\Entity\Category;
+ 
+/**
+* Category controller.
+*
+*/
+class CategoryController extends Controller
+{
+    public function showAction($slug, $page)
+    {
+        $em = $this->getDoctrine()->getEntityManager();
+ 
+        $category = $em->getRepository('EnsJobeetBundle:Category')->findOneBySlug($slug);
+
+        if (!$category) {
+          throw $this->createNotFoundException('Unable to find Category entity.');
+        }
+
+        $total_jobs = $em->getRepository('EnsJobeetBundle:Job')->countActiveJobs($category->getId());
+        $jobs_per_page = $this->container->getParameter('max_jobs_on_category');
+        $last_page = ceil($total_jobs / $jobs_per_page);
+        $previous_page = $page > 1 ? $page - 1 : 1;
+        $next_page = $page < $last_page ? $page + 1 : $last_page;
+
+        $category->setActiveJobs($em->getRepository('EnsJobeetBundle:Job')->getActiveJobs($category->getId(), $jobs_per_page, ($page - 1) * $jobs_per_page));
+
+        return $this->render('EnsJobeetBundle:Category:show.html.twig', array(
+          'category' => $category,
+          'last_page' => $last_page,
+          'previous_page' => $previous_page,
+          'current_page' => $page,
+          'next_page' => $next_page,
+          'total_jobs' => $total_jobs
+        ));
+    }
+}

File src/Ens/JobeetBundle/Controller/JobController.php

         foreach($categories as $category)
         {
             $category->setActiveJobs($em->getRepository('EnsJobeetBundle:Job')->getActiveJobs($category->getId(), $this->container->getParameter('max_jobs_on_homepage')));
+            $category->setMoreJobs($em->getRepository('EnsJobeetBundle:Job')->countActiveJobs($category->getId()) - $this->container->getParameter('max_jobs_on_homepage'));
         }
 
         return $this->render('EnsJobeetBundle:Job:index.html.twig', array(

File src/Ens/JobeetBundle/Entity/Category.php

 namespace Ens\JobeetBundle\Entity;
 
 use Doctrine\ORM\Mapping as ORM;
+use Ens\JobeetBundle\Utils\Jobeet;
 
 /**
  * Ens\JobeetBundle\Entity\Category
      * @var \Doctrine\Common\Collections\ArrayCollection
      */
     private $active_jobs;
+    
+    private $more_jobs;
+    
+    /**
+     * @var string $slug
+     */
+    private $slug;
 
     /**
      * Constructor
     {
         return $this->active_jobs;
     }
+    
+    public function setMoreJobs($jobs)
+    {
+        $this->more_jobs = $jobs >=  0 ? $jobs : 0;
+    }
+    
+    public function getMoreJobs()
+    {
+        return $this->more_jobs;
+    }
+
+    /**
+     * Set slug
+     *
+     * @param string $slug
+     * @return Category
+     */
+    public function setSlug($slug)
+    {
+        $this->slug = $slug;
+    
+        return $this;
+    }
+
+    /**
+     * Get slug
+     *
+     * @return string 
+     */
+    public function getSlug()
+    {
+        return $this->slug;
+    }
+    /**
+     * @ORM\PrePersist
+     */
+    public function setSlugValue()
+    {
+        $this->slug = Jobeet::slugify($this->getName());
+    }
 }

File src/Ens/JobeetBundle/Entity/Category.php~

 namespace Ens\JobeetBundle\Entity;
 
 use Doctrine\ORM\Mapping as ORM;
+use Ens\JobeetBundle\Utils\Jobeet;
 
 /**
  * Ens\JobeetBundle\Entity\Category
      * @var \Doctrine\Common\Collections\ArrayCollection
      */
     private $category_affiliates;
+    
+    /**
+     * @var \Doctrine\Common\Collections\ArrayCollection
+     */
+    private $active_jobs;
+    
+    private $more_jobs;
 
     /**
      * Constructor
     {
         return $this->getName();
     }
+    
+    public function setActiveJobs($jobs)
+    {
+        $this->active_jobs = $jobs;
+    }
+
+    public function getActiveJobs()
+    {
+        return $this->active_jobs;
+    }
+    
+    public function setMoreJobs($jobs)
+    {
+        $this->more_jobs = $jobs >=  0 ? $jobs : 0;
+    }
+    
+    public function getMoreJobs()
+    {
+        return $this->more_jobs;
+    }
 }

File src/Ens/JobeetBundle/Repository/JobRepository.php

  */
 class JobRepository extends EntityRepository
 {
-    public function getActiveJobs($category_id = null, $max = null)
+    public function getActiveJobs($category_id = null, $max = null, $offset = null)
     {
         $qb = $this->createQueryBuilder('j')
           ->where('j.expires_at > :date')
         {
             $qb->setMaxResults($max);
         }
+        
+        if($offset)
+        {
+            $qb->setFirstResult($offset);
+        }
 
         if($category_id)
         {
 
         return $job;
     }
+    
+    public function countActiveJobs($category_id = null)
+    {
+        $qb = $this->createQueryBuilder('j')
+          ->select('count(j.id)')
+          ->where('j.expires_at > :date')
+          ->setParameter('date', date('Y-m-d H:i:s', time()));
+
+        if($category_id)
+        {
+          $qb->andWhere('j.category = :category_id')
+          ->setParameter('category_id', $category_id);
+        }
+
+        $query = $qb->getQuery();
+
+        return $query->getSingleScalarResult();
+    }
 }

File 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
       type: string
       length: 255
       unique: true
+    slug:
+      type: string
+      length: 255
+      unique: true
   oneToMany:
     jobs:
       targetEntity: Job
       mappedBy: category
     category_affiliates:
       targetEntity: CategoryAffiliate
-      mappedBy: category
+      mappedBy: category
+  lifecycleCallbacks:
+    prePersist: [ setSlugValue ]
+    preUpdate: [ setSlugValue ]

File src/Ens/JobeetBundle/Resources/config/routing.yml

     
 EnsJobeetBundle_homepage:
     pattern:  /
-    defaults: { _controller: EnsJobeetBundle:Job:index }
+    defaults: { _controller: EnsJobeetBundle:Job:index }
+    
+EnsJobeetBundle_category:
+    pattern:  /category/{slug}/{page}
+    defaults: { _controller: EnsJobeetBundle:Category:show, page: 1 }

File src/Ens/JobeetBundle/Resources/views/Category/show.html.twig

+{% extends 'EnsJobeetBundle::layout.html.twig' %}
+ 
+{% block title %}
+  Jobs in the {{ category.name }} category
+{% endblock %}
+ 
+{% block stylesheets %}
+  {{ parent() }}
+  <link rel="stylesheet" href="{{ asset('bundles/ensjobeet/css/jobs.css') }}" type="text/css" media="all" />
+{% endblock %}
+ 
+{% block content %}
+  <div class="category">
+    <div class="feed">
+      <a href="">Feed</a>
+    </div>
+    <h1>{{ category.name }}</h1>
+  </div>
+ 
+  {% include 'EnsJobeetBundle:Job:list.html.twig' with {'jobs': category.activejobs} %}
+ 
+  {% if last_page > 1 %}
+    <div class="pagination">
+      <a href="{{ path('EnsJobeetBundle_category', { 'slug': category.slug, 'page': 1 }) }}">
+        <img src="{{ asset('bundles/ensjobeet/images/first.png') }}" alt="First page" title="First page" />
+      </a>
+ 
+      <a href="{{ path('EnsJobeetBundle_category', { 'slug': category.slug, 'page': previous_page }) }}">
+        <img src="{{ asset('bundles/ensjobeet/images/previous.png') }}" alt="Previous page" title="Previous page" />
+      </a>
+ 
+      {% for page in 1..last_page %}
+        {% if page == current_page %}
+          {{ page }}
+        {% else %}
+          <a href="{{ path('EnsJobeetBundle_category', { 'slug': category.slug, 'page': page }) }}">{{ page }}</a>
+        {% endif %}
+      {% endfor %}
+ 
+      <a href="{{ path('EnsJobeetBundle_category', { 'slug': category.slug, 'page': next_page }) }}">
+        <img src="{{ asset('bundles/ensjobeet/images/next.png') }}" alt="Next page" title="Next page" />
+      </a>
+ 
+      <a href="{{ path('EnsJobeetBundle_category', { 'slug': category.slug, 'page': last_page }) }}">
+        <img src="{{ asset('bundles/ensjobeet/images/last.png') }}" alt="Last page" title="Last page" />
+      </a>
+    </div>
+  {% endif %}
+ 
+  <div class="pagination_desc">
+    <strong>{{ total_jobs }}</strong> jobs in this category
+ 
+    {% if last_page > 1 %}
+      - page <strong>{{ current_page }}/{{ last_page }}</strong>
+    {% endif %}
+  </div>
+{% endblock %}

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

           <div class="feed">
             <a href="">Feed</a>
           </div>
-          <h1>{{ category.name }}</h1>
+          <h1><a href="{{ path('EnsJobeetBundle_category', { 'slug': category.slug }) }}">{{ category.name }}</a></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>
+        
+        {% include 'EnsJobeetBundle:Job:list.html.twig' with {'jobs': category.activejobs} %}
+              
+        {% if category.morejobs %}
+            <div class="more_jobs">
+              and <a href="{{ path('EnsJobeetBundle_category', { 'slug': category.slug }) }}">{{ category.morejobs }}</a>
+              more...
+            </div>
+        {% endif %}
       </div>
     {% endfor %}
   </div>

File src/Ens/JobeetBundle/Resources/views/Job/list.html.twig

+<!-- src/Ens/JobeetBundle/Resources/views/Job/list.html.twig -->
+ 
+<table class="jobs">
+  {% for entity in jobs %}
+    <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>