<?php
class mainActions extends sfActions
{

  public function executeUploader(sfWebRequest $request)
  {
      if ($request->getParameter('key') != 'partner' && $request->getParameter('key') != 'review' && $request->getParameter('key') != 'video' && $request->getParameter('key') != 'article')
      {
          echo Page::uploader(($request->hasParameter('url') ? $request->getParameter('url') : $request->getFiles('file')), ($request->hasParameter('url') ? 'url' : 'file'), true, array('key' => $request->getParameter('key')), true);
      }else
      {
          echo Page::uploader(($request->hasParameter('url') ? $request->getParameter('url') : $request->getFiles('file')), ($request->hasParameter('url') ? 'url' : 'file'), true, array('key' => $request->getParameter('key')));

      }
      return sfView::NONE;
  }

  public function executeB64uploader(sfWebRequest $request)
  {
      $json = array(
          'state' => 'error',
          'errorTxt' => 'Not uploaded',
      );
      $file = $request->getFiles('file');
      if (file_exists($file['tmp_name']))
      {
          $ext_post = strrpos($file['name'], '.');
          $ext = strtolower(substr($file['name'], $ext_post));
          $newName = sha1_file($file['tmp_name']) . $ext;

          if(rename($file['tmp_name'], sfConfig::get('sf_upload_dir') . '/tmp/' . $newName))
          {
              $path = sfConfig::get('sf_upload_dir') . '/tmp/' . $newName;
              $fileInBase64 = base64_encode(file_get_contents($path));

              unlink($path);

              $json = array(
                  'state' => 'success',
                  'b64' => $fileInBase64
              );

          }
      }

      echo json_encode($json);
      return sfView::NONE;

  }

  public function executeJcrop(sfWebRequest $request)
  {
  	if($request->isMethod('get'))
  	{
  		$c = $request->getParameter('c');
  		$key = $request->getParameter('key');

  		if(is_array($cfg_sizes = sfConfig::get('app_' . $key . '_sizes')))
  		{
  			if($cfg_sizes['min'])
  			{
  				$min_width = $cfg_sizes['min']['width'];
  				$min_height = $cfg_sizes['min']['height'];

  				if(($request->getParameter('w') * $c) > $min_width && ($request->getParameter('h') * $c) > $min_height)
  				{
  					$img = new sfImage(sfConfig::get('sf_upload_dir') . '/i/' . $request->getParameter('src'));
  					$img->crop(($request->getParameter('x') * $c), ($request->getParameter('y') * $c), ($request->getParameter('w') * $c), ($request->getParameter('h') * $c));
  					$img->thumbnail($min_width, $min_height, true);
  					$image_src = sfConfig::get('sf_upload_dir') . '/i/' . str_replace('.', '-M.', $request->getParameter('src'));
  					$img->saveAs($image_src);

  					if($cfg_sizes['M']['watermark'])
  					{
  						Page::watermarkAdd($image_src);
  					}

  					$result = 'ok';
  				}
  				else
  				{
  					$result = 'small';
  				}
  			}
  		}
  	}
  	echo $result;
  	return sfView::NONE;
  }

  public function executeError404(sfWebRequest $request)
  {
    $this->setLayout('404');
  }
}
