Tuesday, March 3, 2015

Symfony 2 hari 14: Preview job sebelum dipublish

Artikel ini dirunut berdasarkan Jobeet Tutorial, yang dibuat oleh Fabien Potencier, untuk Symfony 1.4.

Halaman Preview

Membuat halaman preview sebenarnya serupa dengan menampilkan halaman job, hanya saja di job preview kita menggunakan $token sebagai pengganti parameter $id. Untuk itu sesuaikan configurasi routingnya, seperti berikut ini:

src/Ibw/JobeetBundle/Resources/config/routing/job.yml

# ...
 
ibw_job_show:
    pattern:  /{company}/{location}/{id}/{position}
    defaults: { _controller: "IbwJobeetBundle:Job:show" }
    requirements:
        id:  \d+
 
ibw_job_preview:
    pattern:  /{company}/{location}/{token}/{position}
    defaults: { _controller: "IbwJobeetBundle:Job:preview" }
    requirements:
        token:  \w+
 
# ...
 
Kemudian buat satu method di JobController dengan nama preview action, lalu tambahkan kode berikut:
 
src/Ibw/JobeetBundle/Controller/JobController.php
 

// ...

    public function previewAction($token)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('IbwJobeetBundle:Job')->findOneByToken($token);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Job entity.');
        }

        $deleteForm = $this->createDeleteForm($entity->getId());

        return $this->render('IbwJobeetBundle:Job:show.html.twig', array(
            'entity'      => $entity,
            'delete_form' => $deleteForm->createView(),
        ));
    }

// ...  
 
 
 
Jika halaman preview muncul tampilkan top-bar untuk admin, dengan skrip berikut di file show.html.twig
 
src/Ibw/JobeetBundle/Resources/views/Job/show.html.twig 
 
<!-- ... -->

{% block content %}
    {% if app.request.get('token') %}
        {% include 'IbwJobeetBundle:Job:admin.html.twig' with {'job': entity} %}
    {% endif %}

<!-- ... -->

{% endblock %}
 
 
Kemudian kita buat halaman admin yang akan digunakan untuk mengelola postingannya. Buat file admin.html.twig
 
src/Ibw/JobeetBundle/Resources/views/Job/admin.html.twig
 

<div id="job_actions">
    <h3>Admin</h3>
    <ul>
        {% if not job.isActivated %}
            <li><a href="{{ path('ibw_job_edit', { 'token': job.token }) }}">Edit</a></li>
            <li><a href="{{ path('ibw_job_edit', { 'token': job.token }) }}">Publish</a></li>
        {% endif %}
        <li>
            <form action="{{ path('ibw_job_delete', { 'token': job.token }) }}" method="post">
                {{ form_widget(delete_form) }}
                <button type="submit" onclick="if(!confirm('Are you sure?')) { return false; }">Delete</button>
            </form>
        </li>
        {% if job.isActivated %}
            <li {% if job.expiresSoon %} class="expires_soon" {% endif %}>
                {% if job.isExpired %}
                    Expired
                {% else %}
                    Expires in <strong>{{ job.getDaysBeforeExpires }}</strong> days
                {% endif %}

                {% if job.expiresSoon %}
                    - <a href="">Extend</a> for another 30 days
                {% endif %}
            </li>
        {% else %}
            <li>
                [Bookmark this <a href="{{ url('ibw_job_preview', { 'token': job.token, 'company': job.companyslug, 'location': job.locationslug, 'position': job.positionslug }) }}">URL</a> to manage this job in the future.]
            </li>
        {% endif %}
    </ul>
</div>
 
 
Tambahkan sedikit kode pada model Job.php di dalam method untuk mengelola kapan postingan Job tersebut berakhir:
 
src/Ibw/JobeetBundle/Entity/Job.php
 
  // ...

    public function isExpired()
    {
        return $this->getDaysBeforeExpires() < 0;
    }

    public function expiresSoon()
    {
        return $this->getDaysBeforeExpires() < 5;   
    }

    public function getDaysBeforeExpires()
    {
        return ceil(($this->getExpiresAt()->format('U') - time()) / 86400);
    }

    // ...
  
 
 
 
 
Sekarang saatnya meredirect aksi create dan update ke halaman preview:
 
 
src/Ibw/JobeetBundle/Controller/JobController.php
 
public function createAction(Request $request)
{
    // ...
    if ($form->isValid()) {
        // ...
        return $this->redirect($this->generateUrl('ibw_job_preview', array(
            'company' => $entity->getCompanySlug(),
            'location' => $entity->getLocationSlug(),
            'token' => $entity->getToken(),
            'position' => $entity->getPositionSlug()
        )));
    }
    // ...
}

public function updateAction(Request $request, $token)
{
    // ...
    if ($editForm->isValid()) {
        // ...

        return $this->redirect($this->generateUrl('ibw_job_preview', array(
            'company' => $entity->getCompanySlug(),
            'location' => $entity->getLocationSlug(),
            'token' => $entity->getToken(),
            'position' => $entity->getPositionSlug()
        )));
    }
    // ...
}
 
 
Hilangkan link edit pada halaman show.html.twig
 
src/Ibw/JobeetBundle/Resources/views/Job/show.html.twig
 
<div style="padding: 20px 0">
    <a href="{{ path('ibw_job_edit', { 'token': entity.token }) }}">
        Edit
    </a>
</div>
 
 
  

No comments:

Post a Comment