<?php

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


  public function preInsert($event) {
    $this->uniqueid = UUID::generate(UUID::UUID_TIME, UUID::FMT_STRING,     "mosatags");
    $this->creation_date = strftime('%Y-%m-%d %H:%M:%S');
  }


  public function getThdTags() {
    $objs = Array();
    $query = Doctrine_Query::create()
      ->from('ThdSegmentTag')
      ->where("segment_id='{$this->getId()}'");

    foreach ($query->execute() as $item) {
      $objs[] = $item->getThdTag();
    }

    return $objs;
  }

  public function getTags() {
    $tags = Array();

    foreach ($this->getThdTags() as $item) {
      $tags[] = $item->getTag();
    }

    return $tags;
  }

  public function getDistinctTags() {
    $tags = Array();

    foreach ($this->getThdTags() as $item) {
      $tag = $item->getTag();
      if (in_array($tag, $tags)) continue;
      $tags[] = $tag;
    }

    return $tags;
  }

      /*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 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)));


    }*/

}
