<?php

require_once dirname(__FILE__).'/../lib/callbackGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/callbackGeneratorHelper.class.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 autoCallbackActions
{
    public function executeIndex(sfWebRequest $request)
    {
        // sorting
        if ($request->getParameter('sort') && $this->isValidSortColumn($request->getParameter('sort')))
        {
            $this->setSort(array($request->getParameter('sort'), $request->getParameter('sort_type')));
        }

        // pager
        if ($request->getParameter('page'))
        {
            $this->setPage($request->getParameter('page'));
        }
        else
        {
            $this->setPage(1);
        }

        //search
        if ($request->getParameter('q'))
        {
            $this->setSearch($request->getParameter('q'), ($request->hasParameter('checked') ? $request->getParameter('checked') : false));
        }
        else
        {
            $this->setSearch(false);
        }

        //checked
        if ($request->getParameter('checked'))
        {
            $this->setChecked($request->getParameter('checked'));
        }
        else
        {
            $this->setChecked(false);
        }
        //tabs
        if ($request->hasParameter('tab'))
        {
            $this->setTab($request->getParameter('tab'));
        }


        $this->pager = $this->getPager($request);
        $this->sort = $this->getSort();
        $this->tab = $this->getTab();





        if($request->hasParameter('export'))
        {

            require_once sfConfig::get('sf_lib_dir') . '/Classes/PHPExcel.php';
            $objPHPExcel = new PHPExcel();
            $sheet = $objPHPExcel->setActiveSheetIndex(0);
            $sheet->setCellValue('A1', 'Имя');
            $styleArray = array(
                'font' => array(
                    'bold' => true,
                )
            );


            $sheet->getStyle('A1')->applyFromArray($styleArray);

            $sheet->setCellValue('B1', 'Телефон');
            $styleArray = array(
                'font' => array(
                    'bold' => true,
                )
            );


            $sheet->getStyle('B1')->applyFromArray($styleArray);

            $sheet->setCellValue('C1', 'Позвонили');
            $styleArray = array(
                'font' => array(
                    'bold' => true,
                )
            );


            $sheet->getStyle('C1')->applyFromArray($styleArray);

            $sheet->setCellValue('D1', 'Товар');
            $styleArray = array(
                'font' => array(
                    'bold' => true,
                )
            );


            $sheet->getStyle('D1')->applyFromArray($styleArray);

            $sheet->setCellValue('E1', 'Дата');
            $styleArray = array(
                'font' => array(
                    'bold' => true,
                )
            );


            $sheet->getStyle('E1')->applyFromArray($styleArray);


            foreach ($this->pager->getResults() as $i => $callback)
            {
                $value = '';
                $method = 'getName';

                if(is_callable(array($callback, 'getName')))
                {
                    $method = $callback->$method();
                    if(is_object($method) && count($method) > 0)
                    {
                        $value = $method->__toString();
                    }

                    else
                    {
                        $value = $method;
                    }
                }



                $sheet->setCellValue('A' . ($i + 2), $value);
                $sheet->getColumnDimension('A')->setAutoSize(true);

                $value = '';
                $method = 'getPhone';

                if(is_callable(array($callback, 'getPhone')))
                {
                    $method = $callback->$method();
                    if(is_object($method) && count($method) > 0)
                    {
                        $value = $method->__toString();
                    }

                    else
                    {
                        $value = $method;
                    }
                }



                $sheet->setCellValue('B' . ($i + 2), $value);
                $sheet->getColumnDimension('B')->setAutoSize(true);

                $value = '';
                $method = 'getIsDone';

                if(is_callable(array($callback, 'getIsDone')))
                {
                    $method = $callback->$method();
                    if(is_object($method) && count($method) > 0)
                    {
                        $value = $method->__toString();
                    }

                    else
                    {
                        $value = $method;
                    }
                }



                $sheet->setCellValue('C' . ($i + 2), $value);
                $sheet->getColumnDimension('C')->setAutoSize(true);

                $value = '';

                ob_start();
                ob_implicit_flush(0);

                try
                {
                    require(sfConfig::get('sf_app_module_dir') . '/callback/templates/_offer_id.php');
                }
                catch (Exception $e)
                {
                    ob_end_clean();
                    throw $e;
                }

                $value = ob_get_clean();





                $sheet->setCellValue('D' . ($i + 2), $value);
                $sheet->getColumnDimension('D')->setAutoSize(true);

                $value = '';
                $method = 'getCreatedAt';

                if(is_callable(array($callback, 'getCreatedAt')))
                {
                    $method = $callback->$method();
                    if(is_object($method) && count($method) > 0)
                    {
                        $value = $method->__toString();
                    }

                    else
                    {
                        $value = $method;
                    }
                }
                $value = date('d.m.Y', strtotime($value));



                $sheet->setCellValue('E' . ($i + 2), $value);
                $sheet->getColumnDimension('E')->setAutoSize(true);

            }
            $sheet->setAutoFilter('A1:E' . $i);
            $sheet->setTitle('Обратный звонок');
            $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
            $uuid = Page::generateUuid();

            $objWriter->save('php://output');
            $this->getResponse()->setHttpHeader('Content-type', 'application/octet-stream');
            $this->getResponse()->setHttpHeader('Content-Disposition', 'attachment; filename="Обратный звонок.xlsx"');

            return sfView::NONE;

        }
    }

}
