<?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 NewsForm extends BaseNewsForm
{
  public function configure()
  {
        $this->useFields(array(
            'title',
            'body',
            'image_original',
            // 'type',
            'category_id',
            //'other_category_id',			
			'published_at',
            'is_top'
        ));




    $categories = Doctrine::getTable('Category')->getCategories(22,-1);

	 
	
    $types      = Doctrine::getTable('News')->getTypes();

    $this->widgetSchema['category_id'] = new sfWidgetFormChoice(array('choices' => $categories));
	
	
    $this->widgetSchema['other_category_id'] = new sfWidgetFormSelectCheckbox(array('choices' => $categories));
	
    $this->widgetSchema['image_original']      = new sfWidgetFormInputFile(array('label'=>'Зураг'));
    
    $this->widgetSchema['is_top'] = new sfWidgetFormChoice(array(
      'choices' => News::getStatusChoices()
    ));



	$this->widgetSchema->setLabels(array('other_category_id' => 'Категори s'));
	

    $this->widgetSchema->setLabels(array('title'       => 'Гарчиг'));

    $this->widgetSchema->setLabels(array('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' => 5000000,
		'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' => 'Зургийн файлын хэмжээ 5MB-аас хэтрэхгүй байх ёстой '
		));
		
	   $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 = 0777, $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);
  }
  
  
  
}