<?php

/**
 * News form.
 *
 * @package    mongol
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class PhotoForm extends BaseNewsForm
{
  public function configure()
  {
        $this->useFields(array(
            'title',
            'body',
            'image_original',
            'published_at',
            'category_id',
            'is_top'
        ));




    $categories = Doctrine::getTable('Category')->getCategoryOptions(22,-1);

    $types      = Doctrine::getTable('News')->getTypes();

    $this->widgetSchema['category_id'] = new sfWidgetFormChoice(array('choices' => $categories));
    // $this->widgetSchema['type']        = new sfWidgetFormChoice(array('choices' => $types), array('onchange' => 'onChangeNewsType()'));
    $this->widgetSchema['is_top'] = new sfWidgetFormChoice(array(
      'choices' => News::getStatusChoices()
    ));
    $this->widgetSchema['image_original']      = new sfWidgetFormInputFile(array('label'=>'Зураг'));
     $this->widgetSchema['other_category_id'] = new sfWidgetFormSelectCheckbox(array('choices' => $categories));



    $this->widgetSchema->setLabels(array('title'       => 'Гарчиг'));

    $this->widgetSchema->setLabels(array('other_category_id'       => 'Ангилал'));

    $this->widgetSchema->setLabels(array('description' => 'Товч агуулга'));

    $this->widgetSchema->setLabels(array('body'        => 'Үндсэн агуулга'));

    $this->widgetSchema->setLabels(array('is_top'      => 'Топ мэдээ'));

    $this->widgetSchema->setLabels(array('stop_press'      => 'Тоймчийн нийтлэл'));

    $this->widgetSchema->setLabels(array('is_iframe'      => 'Бусад сайтд харуулах'));

    $this->widgetSchema->setLabels(array('is_featured' => 'Нүүр xуудсанд онцлоx'));

    $this->widgetSchema->setLabels(array('featured_toim' => 'Онцлоx тойм'));

    $this->widgetSchema->setLabels(array('is_sub_featured' => 'Дэд бүлэгт онцлоx'));

    $this->widgetSchema->setLabels(array('category_id' => 'Категори'));

    $this->widgetSchema->setLabels(array('type'        => 'Мэдээний төрөл'));

    $this->widgetSchema->setLabels(array('image'       => 'Мэдээний зураг'));

    $this->widgetSchema->setLabels(array('tags'        => 'Мэдээний шошго'));

    $this->widgetSchema->setLabels(array('video_path'  => 'Видео'));

    $this->widgetSchema->setLabels(array('publisher_id'=> 'Нийтлэгч'));

    $this->widgetSchema->setLabels(array('published_at'=> 'Хугацаа тохируулах'));

    $this->widgetSchema->setLabels(array('allow_comment'=> 'Сэтгэгдэл зөвшөөрнө'));

    $this->widgetSchema->setLabels(array('is_wall'=> 'Манай булан'));

    $this->setDefault('is_top', 0);

 
    if($this->getObject()->isNew()){
        sfConfig::set('app_date_folder', date('Ym'));
    }else{
        sfConfig::set('app_date_folder', date('Ym', strtotime($this->getObject()->getCreatedAt())));
    }
   $this->validatorSchema['image_original'] = new sfValidatorFile(array(
    'required' => false,
    'mime_types' => 'web_images',
    'max_size' => 2000000,
    'path' => sfConfig::get('sf_upload_dir').'/post/'.sfConfig::get('app_date_folder').'/orginal/',
    'validated_file_class' => 'ThumbnailValidatedFile',
    )
   , array(
    'mime_types' => 'Та .jpg, .gif, .png зургууд сонгоно уу', 'max_size' => 'Зургийн файлын хэмжээ 1MB-аас хэтрэхгүй байх ёстой '
    ));
   $this->validatorSchema['image_name_delete'] = new sfValidatorPass();
   $this->validatorSchema['other_category_id'] = new sfValidatorPass();



  }
}

class ThumbnailValidatedFile extends sfValidatedFile {

  private $savedFilename;
    // Override sfValidatedFile's save method
  public function save($file = null, $fileMode = 0666, $create = true, $dirMode = 0777) {

    $this->savedFilename = $this->generateFilename();
        // This makes sure we use only one savedFilename (it will be the first)
    if ($this->savedFilename === null) {
      $this->savedFilename = $file;
    }


    foreach (sfConfig::get('app_content_photo_sizes') as $size) {
      $upload_directory = sfConfig::get('sf_upload_dir').'/post/'.sfConfig::get('app_date_folder').'/'. $size;

      if (!is_dir($upload_directory)) {
        mkdir($upload_directory, 0777);
      }

      $thumbnail = new sfThumbnail($size);
      $thumbnail->loadFile($this->getTempName());
      $thumbnail->save($upload_directory .'/'. $this->savedFilename);
            // Let the original save method do its magic :)
    }

    return parent::save($this->savedFilename, $fileMode, $create, $dirMode);
  }
}