<?php

class pagesActions extends sfActions
{
    public function executeIndex(sfWebRequest $request)
    {
        $this->setLayout('layoutHome');
    }

    public function executeShow(sfWebRequest $request)
    {
        $this->page = $this->getRoute()->getObject();
    }

    public function executeContacts(sfWebRequest $request)
    {
        if ($request->isMethod('post'))
        {
            $text = $request->getParameter('text');
            $name = $request->getParameter('name');

            if ($name && $text)
            {
                $newFeedback = new Feedback();
                $newFeedback->setAuthorName(trim(strip_tags($name)));
                $newFeedback->setMsg(trim(strip_tags($text)));
                $newFeedback->save();

                sfContext::getInstance()->getConfiguration()->loadHelpers('Partial');

                if ($this->getUser()->isAuthenticated())
                {
                    $param = array(
                        'organization' => $this->getUser()->getAccount()->getOrganization(),
                        'username' => $this->getUser()->getUsername(),
                        'text' => trim(strip_tags($text)),
                        'time' => $newFeedback->getCreatedAt()
                    );
                }
                else
                {
                    $param = array(
                        'organization' => false,
                        'username' => false,
                        'text' => trim(strip_tags($text)),
                        'time' => $newFeedback->getCreatedAt()
                    );
                }

                get_component('pages', 'emailSender',
                    array(
                        'type' => 'adminNotificationAboutFeedback',
                        'emailTo' => 'admin',
                        'param' => $param
                    )
                );

                echo 'ok';

                return sfView::NONE;

            }
        }
    }



    public function executeService(sfWebRequest $request)
    {

    }

    public function executePartners(sfWebRequest $request)
    {

    }

    public function executeHow_to_buy(sfWebRequest $request)
    {

    }

    public function executeAbout(sfWebRequest $request)
    {

    }

    public function executeDelivery(sfWebRequest $request)
    {

    }

    public function executeError404(sfWebRequest $request)
    {
        $this->setLayout('layout404');
    }
    public function executeSitemap(sfWebRequest $request)
    {
        $this->getResponse()->setHttpHeader('Content-type', 'text/xml');
        $this->getResponse()->sendHttpHeaders();
        echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";


        sfApplicationConfiguration::getActive()->loadHelpers(array('Url'));

        $routes = array(
            'homepage',
            'catalog',
            'video',
            'article',
            'review'
        );

        echo '<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';

        foreach ($routes as $route)
        {
            $url = sfConfig::get('app_protocol') . '://' . $request->getHost() . url_for('@' . $route);
            echo '<url><loc>' . $url . '</loc><lastmod>' . date('c') . '</lastmod><priority>1.0</priority></url>';
        }


        $products = Doctrine_Query::create()
            ->select("p.alias")
            ->from("Product p")
            ->where('p.is_delete = 0')
            ->fetchArray()
        ;

        foreach ($products as $product)
        {
            $url = sfConfig::get('app_protocol') . '://' . $request->getHost() . url_for('@product_show?alias=' . $product['alias']);
            echo '<url><loc>' . $url . '</loc><lastmod>' . date('c') . '</lastmod><priority>1.0</priority></url>';
        }

        $catalogs = Doctrine_Query::create()
            ->select("c.alias")
            ->from("Cat c")
            ->fetchArray()
        ;
        foreach ($catalogs as $catalog)
        {
            $url = sfConfig::get('app_protocol') . '://' . $request->getHost() . url_for('@catalog_show?alias=' . $catalog['alias']);
            echo '<url><loc>' . $url . '</loc><lastmod>' . date('c') . '</lastmod><priority>1.0</priority></url>';
        }

        $pages = Doctrine_Query::create()
            ->select("p.alias")
            ->from("Page p")
            ->fetchArray()
        ;
        foreach ($pages as $page)
        {
            $url = sfConfig::get('app_protocol') . '://' . $request->getHost() . url_for('@page_show?alias=' . $page['alias']);
            echo '<url><loc>' . $url . '</loc><lastmod>' . date('c') . '</lastmod><priority>0.5</priority></url>';
        }

        echo '</urlset>';
        return sfView::NONE;
    }

    public function executeRobots(sfWebRequest $request)
    {

        $this->getResponse()->setHttpHeader('Content-type', 'text/plain');
        $this->getResponse()->sendHttpHeaders();
        $this->host = sfConfig::get('app_protocol') . '://' . $request->getHost();


        $this->setLayout(false);
    }

}