<?php

/**
 * This class has been auto-generated by the Doctrine ORM Framework
 */
class ThdSegment extends BaseThdSegment
{

    /*public function getSolrDocumentFields()
    {           // keys of this array are fields' name of solr's schema.xml 
        $type = "ThdFilm";
        $fields = array('type'           => $type,
                        'id'             => $this->getId(),
                        'title'          => array('value' => $this->getTitle(), 'boost' => 1.0),
                        'tags'           => array('value' => $this->getTags(), 'boost' => 1.0),
                        'desc'           => array('value' => $this->getDesc(), 'boost' => 1.0),
                        'uniqueid'       => array('value' => $this->getUniqueid(), 'boost' => 1.0),
        );  

        return $fields;
    }*/


    public function preInsert($event)
    {
        $this->uniqueid = UUID::generate(UUID::UUID_TIME, UUID::FMT_STRING,     "mosatags");
    }
    
    public function getTagsArray() {
    	
        $tags = $this->getTags();
        $res = array();
        
        foreach (explode(',',$tags) as $tag_name) {
            $res[] = trim($tag_name);
        }
        
        return $res;
    }
    
    
    public function postInsert($event) {

        $q = Doctrine_Query::create()
           ->from('ThdSegment s')
           ->innerJoin('s.ThdVideo v')
           ->innerJoin('v.ThdFilm f')
           ->where('s.id = ?', $this->id);
                   
        $segment = $q->fetchOne();
                
        $tags_array = array();
        
        $tags = $this->getTagsArray();
        foreach ($tags as $tag_name) {
            $tags_array[] = array('segment_id'=>$segment->ThdVideo->ThdFilm->uniqueid,'name'=>$tag_name);
        }
        
        sfContext::getInstance()->getLogger()->info("insert segment " . print_r($tags_array,true));
        
    	$dispatcher = sfContext::getInstance()->getEventDispatcher();
    	$dispatcher->notify(new sfEvent($this, 'iri_sonyengine.tag_add', array('object'=>$tags_array, 'retrain'=>true)));

    	$dispatcher->notify(new sfEvent($this, 'uvmc_solr.update_document', array('object' => $segment->ThdVideo->ThdFilm, 'commit' => true)));    	
    	
    	
    }

}
