Artikel ini dirunut berdasarkan Jobeet Tutorial, yang dibuat oleh Fabien Potencier, untuk Symfony 1.4.
Proses aktivasi job dan publikasi
Langkah pertamanya adalah kita akan menambahkan routing baru di konfigurasi routing kita:src/Ibw/JobeetBundle/Resources/config/routing/job.yml
# ...
ibw_job_publish:
pattern: /{token}/publish
defaults: { _controller: "IbwJobeetBundle:Job:publish" }
requirements: { _method: post }
Sekarang kita bisa menambahkan link publish di halaman admin
src/Ibw/JobeetBundle/Resources/views/Job/admin.html.twig
<!-- ... -->
{% if not job.isActivated %}
<li><a href="{{ path('ibw_job_edit', { 'token': job.token }) }}">Edit</a></li>
<li>
<form action="{{ path('ibw_job_publish', { 'token': job.token }) }}" method="post">
{{ form_widget(publish_form) }}
<button type="submit">Publish</button>
</form>
</li>
{% endif %}
<!-- ... -->
{% if not job.isActivated %}
<li><a href="{{ path('ibw_job_edit', { 'token': job.token }) }}">Edit</a></li>
<li>
<form action="{{ path('ibw_job_publish', { 'token': job.token }) }}" method="post">
{{ form_widget(publish_form) }}
<button type="submit">Publish</button>
</form>
</li>
{% endif %}
<!-- ... -->
Langkah berikutnya adalah menambahkan skrip untuk mempublish postingan:
src/Ibw/JobeetBundle/Controller/JobController.php
// ...
public function previewAction($token)
{
// ...
$deleteForm = $this->createDeleteForm($entity->getToken());
$publishForm = $this->createPublishForm($entity->getToken());
return $this->render('IbwJobeetBundle:Job:show.html.twig', array(
'entity' => $entity,
'delete_form' => $deleteForm->createView(),
'publish_form' => $publishForm->createView(),
));
}
public function publishAction(Request $request, $token)
{
$form = $this->createPublishForm($token);
$form->bind($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('IbwJobeetBundle:Job')->findOneByToken($token);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Job entity.');
}
$entity->publish();
$em->persist($entity);
$em->flush();
$this->get('session')->getFlashBag()->add('notice', 'Your job is now online for 30 days.');
}
return $this->redirect($this->generateUrl('ibw_job_preview', array(
'company' => $entity->getCompanySlug(),
'location' => $entity->getLocationSlug(),
'token' => $entity->getToken(),
'position' => $entity->getPositionSlug()
)));
}
private function createPublishForm($token)
{
return $this->createFormBuilder(array('token' => $token))
->add('token', 'hidden')
->getForm()
;
}
// ...
public function previewAction($token)
{
// ...
$deleteForm = $this->createDeleteForm($entity->getToken());
$publishForm = $this->createPublishForm($entity->getToken());
return $this->render('IbwJobeetBundle:Job:show.html.twig', array(
'entity' => $entity,
'delete_form' => $deleteForm->createView(),
'publish_form' => $publishForm->createView(),
));
}
public function publishAction(Request $request, $token)
{
$form = $this->createPublishForm($token);
$form->bind($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('IbwJobeetBundle:Job')->findOneByToken($token);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Job entity.');
}
$entity->publish();
$em->persist($entity);
$em->flush();
$this->get('session')->getFlashBag()->add('notice', 'Your job is now online for 30 days.');
}
return $this->redirect($this->generateUrl('ibw_job_preview', array(
'company' => $entity->getCompanySlug(),
'location' => $entity->getLocationSlug(),
'token' => $entity->getToken(),
'position' => $entity->getPositionSlug()
)));
}
private function createPublishForm($token)
{
return $this->createFormBuilder(array('token' => $token))
->add('token', 'hidden')
->getForm()
;
}
// ...
Deklarasikan penggunaan method publish tersebut di dalam model job:
src/Ibw/JobeetBundle/Entity/Job.php
// ...
public function publish()
{
$this->setIsActivated(true);
}
// ...
src/Ibw/JobeetBundle/Repository/JobRepository.php
namespace Ibw\JobeetBundle\Repository;
use Doctrine\ORM\EntityRepository;
class JobRepository extends EntityRepository
{
public function getActiveJobs($category_id = null, $max = null, $offset = null)
{
$qb = $this->createQueryBuilder('j')
->where('j.expires_at > :date')
->setParameter('date', date('Y-m-d H:i:s', time()))
->andWhere('j.is_activated = :activated')
->setParameter('activated', 1)
->orderBy('j.expires_at', 'DESC');
if($max) {
$qb->setMaxResults($max);
}
if($offset) {
$qb->setFirstResult($offset);
}
if($category_id) {
$qb->andWhere('j.category = :category_id')
->setParameter('category_id', $category_id);
}
$query = $qb->getQuery();
return $query->getResult();
}
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()))
->andWhere('j.is_activated = :activated')
->setParameter('activated', 1);
if($category_id) {
$qb->andWhere('j.category = :category_id')
->setParameter('category_id', $category_id);
}
$query = $qb->getQuery();
return $query->getSingleScalarResult();
}
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()))
->andWhere('j.is_activated = :activated')
->setParameter('activated', 1)
->setMaxResults(1)
->getQuery();
try {
$job = $query->getSingleResult();
} catch (\Doctrine\Orm\NoResultException $e) {
$job = null;
}
return $job;
}
}
Lakukan hal yang sama di CategoryRepository:
src/Ibw/JobeetBundle/Repository/CategoryRepository.php
namespace Ibw\JobeetBundle\Repository;
use Doctrine\ORM\EntityRepository;
class CategoryRepository extends EntityRepository
{
public function getWithJobs()
{
$query = $this->getEntityManager()
->createQuery('SELECT c FROM IbwJobeetBundle:Category c LEFT JOIN c.jobs j WHERE j.expires_at > :date AND j.is_activated = :activated')
->setParameter('date', date('Y-m-d H:i:s', time()))
->setParameter('activated', 1);
return $query->getResult();
}
}
No comments:
Post a Comment