<?php

namespace Market\ZodacoBundle\Entity;

use Doctrine\ORM\EntityRepository;

/**
 * CategoriesRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class CategoriesRepository extends EntityRepository
{
    public function _getCatlist() {
        $q = 'SELECT c FROM MarketZodacoBundle:Categories c WHERE c.active=1 AND c.mid=0 ORDER BY c.sort ASC';
        return $this->getEntityManager()
                        ->createQuery($q)
                        ->getResult();
    }

    /*
     * For Admin
     */

    public function _getAdminCatlist2() {
        $q = 'SELECT c FROM MarketZodacoBundle:Categories c WHERE c.mid!=0 ORDER BY c.sort ASC';
        return $this->getEntityManager()
                        ->createQuery($q)
                        ->getResult();
    }

    public function _getAdminCatlist() {
        $q = 'SELECT c FROM MarketZodacoBundle:Categories c WHERE c.mid=0 ORDER BY c.sort ASC';
        return $this->getEntityManager()
                        ->createQuery($q)
                        ->getResult();
    }

    public function _getSubId($id) {
        $q = 'SELECT c.id FROM MarketZodacoBundle:Categories c WHERE c.mid=' . $id . ' ORDER BY c.sort ASC';
        return $this->getEntityManager()
                        ->createQuery($q)
                        ->getResult();
    }

    public function _getSublist($id) {
        $q = 'SELECT c FROM MarketZodacoBundle:Categories c WHERE c.mid=' . $id . ' ORDER BY c.sort ASC';
        return $this->getEntityManager()
                        ->createQuery($q)
                        ->getResult();
    }

    public function _getSublistShow($id) {
        $q = 'SELECT c FROM MarketZodacoBundle:Categories c WHERE c.active=1 AND c.mid=' . $id . ' ORDER BY c.sort ASC';
        return $this->getEntityManager()
                        ->createQuery($q)
                        ->getResult();
    }

    public function _delId($id) {
        $q = 'DELETE FROM MarketZodacoBundle:Categories c WHERE c.id=' . $id;
        return $this->getEntityManager()
                        ->createQuery($q)
                        ->getResult();
    }

    public function getPopular($page = 1, $limit = 5) {
        $q = 'SELECT c FROM MarketZodacoBundle:Categories c ';
        $q .= 'WHERE c.mid != 0 ORDER BY c.total DESC';
        return $this->getEntityManager()
                        ->createQuery($q)
                        ->setMaxResults($limit)
                        ->setFirstResult(($page - 1) * $limit)
                        ->getResult();
    }
    
    public function updateTotal($param, $id) {
        $q = "UPDATE MarketZodacoBundle:Categories c  SET $param = $param + 1 WHERE c.id = $id";
        $this->getEntityManager()->createQuery($q)->execute();
    }
}
