<?php

/**
 * main actions.
 *
 * @package    sf
 * @subpackage main
 * @author     Atma
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class mainActions extends sfActions
{
    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request)
    {
        $this->setLayout('layoutHome');
        $this->video = Doctrine_Query::create()
            ->select("v.*, t.*")
            ->from("Video v")
            ->innerJoin("v.Translation t")
            ->where("t.lang = ?", $this->getUser()->getCulture())
            ->andWhere("t.available = 1")
            ->orderBy("v.created_at DESC")
            ->fetchOne();
        $this->products = Doctrine_Query::create()
            ->select("p.id, p.alias, p.product_img, p.is_detail, p.offer_count, p.is_delete, pt.id, pt.lang, pt.title, o.id, o.product_id, o.price, o.quantity_type_select, ot.*")
            ->from("Product p")
            ->leftJoin("p.Translation pt")
            ->leftJoin("p.Offer o WITH o.is_wholesale = 0")
            ->leftJoin("o.Translation ot")
            ->where("p.offer_count > 0")
            ->andWhere("p.is_delete = 0")
            ->andWhere("CHAR_LENGTH(p.product_img) > 0")
            ->limit(10)
            ->orderBy("FIELD(p.show_main, '1') DESC, p.sort, RAND()")
            ->fetchArray();
        $this->group_offers = Doctrine_Query::create()
            ->select("p.id, p.alias, p.product_img, p.is_detail, p.offer_count, p.is_delete, pt.id, pt.lang, pt.title, o.id, o.product_id, o.price, o.quantity_type_select, ot.*, c.id, ct.title, ct.id, ct.lang")
            ->from("Product p")
            ->innerJoin("p.Translation pt")
            ->innerJoin("p.Offer o")
            ->innerJoin("o.Translation ot")
            ->innerJoin("p.Cats c")
            ->innerJoin("c.Translation ct WITH ct.title LIKE '%камень для бани%'")
            ->where("p.offer_count > 0")
            ->andWhere("p.is_delete = 0")
            ->andWhere("p.is_main = 1")
            ->andWhere("CHAR_LENGTH(p.product_img) > 0")
            ->limit(2)
            ->orderBy("RAND()")
            ->fetchArray();
        $this->group_cat = Doctrine_Query::create()
            ->select("c.*, t.*")
            ->from("Cat c")
            ->innerJoin("c.Translation t")
            ->where("CHAR_LENGTH(c.photo) > 0")
            ->andWhere("c.is_main = 1")
            ->orderBy("RAND()")
            ->fetchOne();

    }
    public function executeDl(sfWebRequest $request)
    {
        /*
        $type = $request->getParameter('type');
        $id = trim(strip_tags($request->getParameter('id')));
        if($type == 'certificate' && $id){
            $product = Doctrine::getTable("Product")->findOneByAlias($id);
            $this->forward404Unless($product && $product->getCertificate());
            $path = sfConfig::get('sf_upload_dir') . '/i/' . $product->getCertificate();
            if(file_exists($path) && !is_dir($path)){
                $exp = explode('.', $product->getCertificate());
                $this->getResponse()->setHttpHeader('Content-type', mime_content_type($path));
                $this->getResponse()->setHttpHeader('Content-Disposition', 'attachment; filename="certificate.' . ($exp[count($exp) - 1]) . '"');
                $this->getResponse()->setHttpHeader('X-Accel-Redirect', '/web/u/i/' . $product->getCertificate());
            }
        }
        */
        return sfView::NONE;
    }
    public function executeUploader(sfWebRequest $request)
    {
        $file = $request->getFiles('file');
        if(substr($file['type'], 0, 5) == 'image'){
            echo Page::uploader($file, 'file', true, array('key' => $request->getParameter('key')));
        }
        return sfView::NONE;
    }
}
