<?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 VideoForm extends BaseNewsForm
{
  public function configure()
  {
        $this->useFields(array(
            'title',
            'body',
            'video_path',
            'category_id',
            'published_at',
            '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['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->widgetSchema->setHelp('video_path', 'Youtube дээрхи url-г шууд оруулан уу !');
    
    $this->setDefault('is_top', 0);
	
	  $this->validatorSchema['other_category_id'] = new sfValidatorPass();

	$this->validatorSchema->setPostValidator(

        new sfValidatorCallback(array('callback' => array($this, 'extractUTubeVidId')))

    );


  }

  public function extractUTubeVidId($validator, $values){
    
	
	
	/*
    * type1: http://www.youtube.com/watch?v=9Jr6OtgiOIw
    * type2: http://www.youtube.com/watch?v=9Jr6OtgiOIw&feature=related
    * type3: http://youtu.be/9Jr6OtgiOIw
    */

    
    $url = $values['video_path'];
	$url_back = $values['video_path'];
    $vid_id = "";
    $flag = false;
if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $match)) {	
	
    if(isset($url) && !empty($url)){
	    /*case1 and 2*/
        $parts = explode("?", $url);
        if(isset($parts) && !empty($parts) && is_array($parts) && count($parts)>1){
            $params = explode("&", $parts[1]);
            if(isset($params) && !empty($params) && is_array($params)){
                foreach($params as $param){
                    $kv = explode("=", $param);
                    if(isset($kv) && !empty($kv) && is_array($kv) && count($kv)>1){
                        if($kv[0]=='v'){
                            $vid_id = $kv[1];
                            $flag = true;
                            break;							
                        }
                    }
                }
            }
        }
	
      
		
		
        /*case 3*/
        if(!$flag){
            $needle = "youtu.be/";
            $pos = null;
            $pos = strpos($url, $needle);
            if ($pos !== false) {
                $start = $pos + strlen($needle);
                $vid_id = substr($url, $start, 11);
                $flag = true;
            }
        }
    }
	$values['video_path'] = $vid_id;
 }else{
	  $values['video_path'] = $url_back;
	  }  
    
	
    return $values;
    }    

   public function checkUrl($validator, $values)
   {
   
   	echo $values['video_path'] ; die();
	
        $url = $values['video_path'];
	    if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $match)) {
	        $video_id = $match[1]; 
	        $urls= $video_id;
        	$values['video_path'] = $urls;
		}else{
        	$values['video_path'] = $url;
		}

		return $values;
  	}
}
