<?php
/**
 * FormEntry Repository
 *
 * @author Elze Kool <info@visualmedia.nl>
 */
namespace VisualM\FormBundle\Entity;

use VisualM\FormBundle\Model\FormEntryManager;
use Doctrine\ORM\QueryBuilder;
use VisualM\ModelManagementBundle\Definition\ModelManagerInterface;

/**
 * FormEntry Repository
 *
 * @author Elze Kool <info@visualmedia.nl>
 */
class FormEntryRepository extends FormEntryManager implements ModelManagerInterface
{
    /**
     * {@inheritDoc}
     */
    protected function processConditions(array $options, QueryBuilder &$qb)
    {
        foreach($options as $option => $value) {
            if ($option == 'id') {
                $qb->andWhere($qb->expr()->eq($this->getFullFieldName('id'), $qb->expr()->literal($value)));
            } elseif ($option == 'form_id' AND $value != '') {
                $qb->andWhere($qb->expr()->eq($this->getFullFieldName('form'), $qb->expr()->literal($value)));
            } elseif ($option == 'search' AND $value != '') {
                $qb->andWhere($qb->expr()->orX(
                    $qb->expr()->like($this->getFullFieldName('rawdata'), $qb->expr()->literal('%' . $value . '%')),
                    $qb->expr()->like($this->getFullFieldName('url'), $qb->expr()->literal('%' . $value . '%'))
                ));
            }
        }
    }

}
