<?php
class pagesComponents extends sfComponents
{
    public function executeSubMenu(sfWebRequest $request)
    {
        $menu = array(

            'Каталог'               => '@catalog',
            'Новости и статьи'      => '@news',

        );

        $pagesFromDb = Doctrine::getTable("Page")->findAll();


        $this->path = $request->getPathInfo();
        $this->menu = $menu;
        $this->pagesFromDb = $pagesFromDb;

    }


    public function executeEmailSender(sfWebRequest $request)
    {
        $type = $this->type;
        $emailTo = $this->emailTo;
        $param = $this->param;


        $mailBody = false;
        $subject = false;

        if ($type == 'simple')
        {
            // param - mailBody, subject

            $subject = $param['subject'];
            $mailBody = $param['mailBody'];

        }

        if ($type == 'userRecoveryPassword')
        {

            // param - secretString

            $subject = 'Востановление пароля';

            $mailBody = get_partial('pages/userRecoveryPasswordLetter',
                array(
                    'secretString' => $param['secretString'],
                    'host' => $request->getHost()
                )
            );

        }

        if ($type == 'adminNotificationAboutOrder')
        {

            // param - delivery_address, delivery, org, products_order(array), order_id, inn, phone_number

            $subject = 'Оповещение о новом заказе';

            $mailBody = get_partial('pages/adminNotificationAboutOrderLetter',
                array(
                    'delivery_address' => $param['delivery_address'],
                    'delivery' => $param['delivery'],
                    'org' => $param['org'],
                    'comment' => $param['comment'],
                    'products_order' => $param['products_order'],
                    'order_id' => $param['order_id'],
                    'inn' => $param['inn'],
                    'phone_number' => $param['phone_number'],
                    'host' => $request->getHost()
                )
            );

        }

        if ($type == 'adminNotificationAboutFastOrder')
        {

            // param - delivery_address, delivery, phone, products_order(array)

            $subject = 'Новый заказ на сайте "Иди в баню"';

            $mailBody = get_partial('pages/adminNotificationAboutFastOrderLetter',
                array(
                    'delivery_address' => $param['delivery_address'],
                    'delivery_type' => $param['delivery_type'],
                    'payment_type' => $param['payment_type'],
                    'name' => $param['name'],
                    'phone' => $param['phone'],
                    'order_id' => $param['order_id'],
                    'email' => $param['email'],
                    'comment' => $param['comment'],
                    'products_order' => $param['products_order'],
                    'host' => $request->getHost()
                )
            );

        }

        if ($type == 'userRegistration')
        {
            // param - organization, username, password

            $subject = 'Доступ к сайту ТриАр';

            $mailBody = get_partial('pages/userRegistrationLetter',
                array(
                    'organization' => $param['organization'],
                    'username' => $param['username'],
                    'password' => $param['password'],
                    'host' => $request->getHost()
                )
            );
        }

        if ($type == 'adminNotificationAboutReg')
        {
            // param - organization, username, email, address, city, phone_number

            $subject = 'Заявка на регистрацию';

            $mailBody = get_partial('pages/adminNotificationAboutRegLetter',
                array(
                    'organization' => $param['organization'],
                    'username' => $param['username'],
                    'email' => $param['email'],
                    'address' => $param['address'],
                    'city' => $param['city'],
                    'phone_number' => $param['phone_number'],
                    'host' => $request->getHost()
                )
            );

        }

        if ($type == 'adminNotificationAboutFeedback')
        {
            // param - organization, username, text, time

            $subject = 'Обратная связь';

            $mailBody = get_partial('pages/adminNotificationAboutFeedbackLetter',
                array(
                    'organization' => $param['organization'],
                    'username' => $param['username'],
                    'text' => $param['text'],
                    'time' => $param['time'],
                    'host' => $request->getHost()
                )
            );

        }


        //sending
        if ($mailBody && $emailTo)
        {
            $message = Swift_Message::newInstance();

            if ($type == 'simple')
            {
                $content_type = 'pain';
            }
            else
            {
                $content_type = 'html';
            }

            if($request->getHost())
            {
                $from = 'noreply@' . $request->getHost();
            }
            else
            {
                $from = sfConfig::get('app_email_from');
            }

            $message->setFrom($from)
                ->setContentType('text/' . $content_type . ', charset = utf-8');

            if ($subject)
            {
                $message->setSubject($subject);
            }
            else
            {
                $message->setSubject('Сайт');
            }

            $message->setBody($mailBody);

            if ($emailTo == 'admin')
            {
                $admin_emails = Doctrine::getTable("NotificationEmails")->findByIsActive(1);

                $emails_array = array();

                foreach ($admin_emails as $email)
                {
                    $emails_array[] = $email->getEmail();
                }

                $message->setTo($emails_array);
            }
            else
            {
                $message->setTo($emailTo);
            }

            $this->getMailer()->send($message);

            file_put_contents(sfConfig::get('sf_log_dir') . '/mail.log', date('Y-m-d H:i:s') . ' mail send from ' . $from . ' to ' . $emailTo . "\n\r", FILE_APPEND);
        }

        return sfView::NONE;

    }

}