<?php

/**
 * callback actions.
 *
 * @package    sf
 * @subpackage callback
 * @author     Atma
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class callbackActions extends sfActions
{
    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeNew(sfWebRequest $request)
    {
        $callback_form = new wwwCallbackForm();
        $params = $request->getParameter($callback_form->getName());
        $params['phone'] = preg_replace("/[^0-9]/", '', $params['phone']);
        if ($params['offer_id'] == '')
        {
            $params['offer_id'] = null;
        }
        $callback_form->bind($params);
        $is_valid = $callback_form->isValid();
        if($is_valid){
            $callback = $callback_form->save();

            $mail_body = "Обратный звонок" . "\n";
            $mail_body .= "Телефон: " . $callback->getPhone() . "\n";
            $mail_body .= "Имя: " . $callback->getName() . "\n";

            $emails_dirty = NotificationEmailsTable::getInstance()->createQuery('e')
                ->select('e.email')
                ->where("e.is_active = 1")
                ->andWhere("e.notic_type = 'call' OR e.notic_type = 'all'")
                ->fetchArray();

            if (count($emails_dirty) > 0)
            {
                $emails = array();

                foreach ($emails_dirty as $e)
                {
                    $emails[] = $e['email'];
                }

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

                get_component('pages', 'emailSender',
                    array(
                        'type' => 'simple',
                        'emailTo' => $emails,
                        'param' => array('mailBody' => $mail_body, 'subject' => 'Обратный звонок')
                    )
                );
            }

            $callback_form = new wwwCallbackForm();
        }
        echo $this->getPartial('callback/callback', array('callback_form' => $callback_form, 'is_valid' => $is_valid));
        return sfView::NONE;
    }
}
