<?php

/**
 * autoPhoto actions.
 *
 * @package    ##PROJECT_NAME##
 * @subpackage autoPhoto
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
 * @version    SVN: $Id: actions.class.php 9926 2008-06-27 12:25:54Z noel $
 */
class autoPhotoActions extends sfActions
{
  public function executeIndex()
  {
    return $this->forward('photo', 'list');
  }

  public function executeList()
  {
    $this->processSort();

    $this->processFilters();


    // pager
    $this->pager = new sfPropelPager('Photo', 20);
    $c = new Criteria();
    $this->addSortCriteria($c);
    $this->addFiltersCriteria($c);
    $this->pager->setCriteria($c);
    $this->pager->setPage($this->getRequestParameter('page', $this->getUser()->getAttribute('page', 1, 'sf_admin/photo')));
    $this->pager->init();
    // save page
    if ($this->getRequestParameter('page')) {
        $this->getUser()->setAttribute('page', $this->getRequestParameter('page'), 'sf_admin/photo');
    }
  }

  public function executeCreate()
  {
    return $this->forward('photo', 'edit');
  }

  public function executeSave()
  {
    return $this->forward('photo', 'edit');
  }


  public function executeDeleteSelected()
  {
    $this->selectedItems = $this->getRequestParameter('sf_admin_batch_selection', array());

    try
    {
      foreach (PhotoPeer::retrieveByPks($this->selectedItems) as $object)
      {
        $object->delete();
      }
    }
    catch (PropelException $e)
    {
      $this->getRequest()->setError('delete', 'Could not delete the selected Photos. Make sure they do not have any associated items.');
      return $this->forward('photo', 'list');
    }

    return $this->redirect('photo/list');
  }

  public function executeEdit()
  {
    $this->photo = $this->getPhotoOrCreate();

    if ($this->getRequest()->isMethod('post'))
    {
      $this->updatePhotoFromRequest();

      try
      {
        $this->savePhoto($this->photo);
      }
      catch (PropelException $e)
      {
        $this->getRequest()->setError('edit', 'Could not save the edited Photos.');
        return $this->forward('photo', 'list');
      }

      $this->getUser()->setFlash('notice', 'Your modifications have been saved');

      if ($this->getRequestParameter('save_and_add'))
      {
        return $this->redirect('photo/create');
      }
      else if ($this->getRequestParameter('save_and_list'))
      {
        return $this->redirect('photo/list');
      }
      else
      {
        return $this->redirect('photo/edit?id='.$this->photo->getId());
      }
    }
    else
    {
      $this->labels = $this->getLabels();
    }
  }

  public function executeDelete()
  {
    $this->photo = PhotoPeer::retrieveByPk($this->getRequestParameter('id'));
    $this->forward404Unless($this->photo);

    try
    {
      $this->deletePhoto($this->photo);
    }
    catch (PropelException $e)
    {
      $this->getRequest()->setError('delete', 'Could not delete the selected Photo. Make sure it does not have any associated items.');
      return $this->forward('photo', 'list');
    }

      $currentFile = sfConfig::get('sf_upload_dir')."//".$this->photo->getFilePath();
      if (is_file($currentFile))
      {
        unlink($currentFile);
      }

    return $this->redirect('photo/list');
  }

  public function handleErrorEdit()
  {
    $this->preExecute();
    $this->photo = $this->getPhotoOrCreate();
    $this->updatePhotoFromRequest();

    $this->labels = $this->getLabels();

    return sfView::SUCCESS;
  }

  protected function savePhoto($photo)
  {
    $photo->save();

  }

  protected function deletePhoto($photo)
  {
    $photo->delete();
  }

  protected function updatePhotoFromRequest()
  {
    $photo = $this->getRequestParameter('photo');

    if (isset($photo['photo']))
    {
      $this->photo->setPhoto($photo['photo']);
    }
    $currentFile = sfConfig::get('sf_upload_dir')."//".$this->photo->getFilePath();
    if (!$this->getRequest()->hasErrors() && isset($photo['file_path_remove']))
    {
      $this->photo->setFilePath('');
      if (is_file($currentFile))
      {
        unlink($currentFile);
      }
    }

    if (!$this->getRequest()->hasErrors() && $this->getRequest()->getFileSize('photo[file_path]'))
    {
      $fileName = md5($this->getRequest()->getFileName('photo[file_path]').time().rand(0, 99999));
      $ext = $this->getRequest()->getFileExtension('photo[file_path]');
      if (is_file($currentFile))
      {
        unlink($currentFile);
      }
      $this->getRequest()->moveFile('photo[file_path]', sfConfig::get('sf_upload_dir')."//".$fileName.$ext);
      $this->photo->setFilePath($fileName.$ext);
    }
    if (isset($photo['description']))
    {
      $this->photo->setDescription($photo['description']);
    }
  }

  protected function getPhotoOrCreate($id = 'id')
  {
    if ($this->getRequestParameter($id) === ''
     || $this->getRequestParameter($id) === null)
    {
      $photo = new Photo();
    }
    else
    {
      $photo = PhotoPeer::retrieveByPk($this->getRequestParameter($id));

      $this->forward404Unless($photo);
    }

    return $photo;
  }

  protected function processFilters()
  {
  }

  protected function processSort()
  {
    if ($this->getRequestParameter('sort'))
    {
      $this->getUser()->setAttribute('sort', $this->getRequestParameter('sort'), 'sf_admin/photo/sort');
      $this->getUser()->setAttribute('type', $this->getRequestParameter('type', 'asc'), 'sf_admin/photo/sort');
    }

    if (!$this->getUser()->getAttribute('sort', null, 'sf_admin/photo/sort'))
    {
    }
  }

  protected function addFiltersCriteria($c)
  {
  }

  protected function addSortCriteria($c)
  {
    if ($sort_column = $this->getUser()->getAttribute('sort', null, 'sf_admin/photo/sort'))
    {
      $sort_column = sfInflector::camelize(strtolower($sort_column));
      $sort_column = PhotoPeer::translateFieldName($sort_column, BasePeer::TYPE_PHPNAME, BasePeer::TYPE_COLNAME);
      if ($this->getUser()->getAttribute('type', null, 'sf_admin/photo/sort') == 'asc')
      {
        $c->addAscendingOrderByColumn($sort_column);
      }
      else
      {
        $c->addDescendingOrderByColumn($sort_column);
      }
    }
  }

  protected function getLabels()
  {
    return array(
      'photo{photo}' => 'Photo:',
      'photo{file_path}' => 'File path:',
      'photo{description}' => 'Description:',
    );
  }
}
