<?php

class editVideoSegmentAction extends sfAction
{
  public function execute($request)
  {
    // Parse request
    $film_slug = $request->getParameter('film_slug');

    // Retrieve video
    $query = Doctrine_Query::create()
      ->from('ThdVideo vi')
      ->leftJoin('vi.ThdFilm fi')
      ->where("fi.slug_url='{$film_slug}'");
    $this->video = $query->execute()->getFirst();
    if (!$this->video) return $this->forward404();

    if ($request->getMethodName() == sfWebRequest::POST) {
      // Parse request
      $start = (int) $request->getParameter('frmIn');
      $end = (int) $request->getParameter('frmOut');
      $dirtyTags = explode(',', $request->getParameter('tags'));

      // Trim tags
      $tags = Array();

      foreach($dirtyTags as $tag) {
        $tag = trim($tag);
        if (!$tag) continue;
        $tags[] = $tag;
      }

      // Add video segment
      $user = sfContext::getInstance()->getUser();
      $userUid = $user->getUid();
      $this->video->addSegment($start, $end, $tags, $userUid);
    }

    return "Form";
  }
}