<?php

require_once dirname(__FILE__).'/../lib/articleGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/articleGeneratorHelper.class.php';

/**
 * article actions.
 *
 * @package    sf
 * @subpackage article
 * @author     Atma
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class articleActions extends autoArticleActions
{
    protected function processForm(sfWebRequest $request, sfForm $form)
    {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid())
        {
            $notice = $form->getObject()->isNew() ? 'The item was created successfully.' : 'The item was updated successfully.';
            try
            {
                $article = $form->save();
                $article_custom = $request->getParameter('article_custom');
                if(count($article_custom['tags']) > 0)
                {

                    foreach($article_custom['tags'] as $tag_id)
                    {
                        $newProductTags = new ArticleTags();
                        $newProductTags->setArticleId($article->getId());
                        $newProductTags->setTagId($tag_id);
                        $newProductTags->save();
                    }

                }
            }
            catch (Doctrine_Validator_Exception $e)
            {
                $errorStack = $form->getObject()->getErrorStack();
                $message = get_class($form->getObject()) . ' has ' . count($errorStack) . " field" . (count($errorStack) > 1 ?  's' : null) . " with validation errors: ";
                foreach ($errorStack as $field => $errors)
                {
                    $message .= "$field (" . implode(", ", $errors) . "), ";
                }
                $message = trim($message, ', ');
                $this->getUser()->setFlash('error', $message);
                return sfView::SUCCESS;
            }
            $this->dispatcher->notify(new sfEvent($this, 'admin.save_object', array('object' => $article)));
            if ($request->hasParameter('_save_and_add'))
            {
                $this->getUser()->setFlash('notice', $notice.' You can add another one below.');
                $this->redirect('@article_new');
            }
            elseif ($request->hasParameter('_save_and_list'))
            {
                $this->redirect('@article');
            }
            else
            {
                if(!$request->isXmlHttpRequest())
                {
                    $this->getUser()->setFlash('notice', $notice);
                    $this->redirect($request->hasParameter('return') ? $request->getParameter('return') : array('sf_route' => 'article_edit', 'sf_subject' => $article));
                }
            }
        }
        else
        {
            $this->getUser()->setFlash('error', 'The item has not been saved due to some errors.', false);
            $this->getResponse()->addHttpMeta('Sf-Form-Error', '1', true);
        }
    }
    protected function buildQuery()
    {
        $query = parent::buildQuery();
        $query->leftJoin("r.Translation t");
        return $query;
    }
}
