<?php
class VideoController extends BaseObjectControllerAction
{
const CONTROLLER= 'video';
const DATA_TABLE='videos';
public function init()
{
parent::init(self::CONTROLLER, self::DATA_TABLE);
}
public function viewAction()
{
$options= $this->preprocessViewParams();
$this->videos= array();
$options['limit']= $this->user_settings->pp_videos;
$options['order']= 'p.ts_created desc';
if (strlen($this->param_tag) > 0) {
$this->videos= DatabaseObject_Video::GetVideos($this->db, $options);
$this->total_objcount= DatabaseObject_Video::GetVideos($this->db,
$this->genCountOptions($options));
}
else if (strlen($this->param_url) > 0) {
$video= new DatabaseObject_Video($this->db);
if ($video->load($this->param_url, 'url')){
$this->videos= array($video);
$this->total_objcount= 1;
}
}
else if ($this->param_unread != '') {
$this->videos= DatabaseObject_Video::getNewForUser($this->db,
$this->user_id, $options);
$this->total_objcount= DatabaseObject_Video::
getNewForUser($this->db,
$this->user_id,
$this->genCountOptions($options));
}
else if ($this->param_starred != '') {
$this->videos= DatabaseObject_Video::getStarredByUser($this->db,
$this->user_id, $options);
$this->total_objcount= DatabaseObject_Video::
getStarredByUser($this->db,
$this->user_id,
$this->genCountOptions($options));
}
else {
if (($this->param_month == 0) &&($this->param_year == 0)) {
$options= DatabaseObject_Video::getLatestParams($this->db, $options);
$ts= strtotime($options['to']);
$month= date('n', $ts);
$year= date('Y', $ts);
$this->currentpage = $this->currentpage . $year . '/' . $month ;
}
$this->videos= DatabaseObject_Video::GetVideos($this->db,$options);
$this->total_objcount= DatabaseObject_Video::GetVideos($this->db,
$this->genCountOptions($options));
if (isset($this->submenu1) &&
!isset($this->selected_submenu1_item) &&
(count($this->videos)>0)) {
$this->selected_submenu1_item= $this->submenu1[0]->name;
}
}
$this->objcount= 0;
$this->objects= array();
foreach ($this->videos as $video_id => $video) {
$this->objects[$this->objcount]= $this->fillObjectInfo($video);
$this->objcount++;
}
//Fill action specific view variables
if (count($this->objects) > 0) {
$this->view->objects= $this->objects;
}
//call common functions, independent of action
$this->addUserMenu();
$this->generatePageInfo($this->user_settings->pp_videos);
$this->fillViewVariables();
}
protected function fillObjectInfo(DatabaseObject_Video $video)
{
$options= array('order' => 'p.ts_created desc');
$vid_object= new Utility_Object();
$vid_object->type= $this->_type;
$vid_object->author= DatabaseObject_User::
getUserNamefromId($this->db,
$video->user_id);
if ($video->user_id == $this->user_id) {
$vid_object->owner= true;
}
//copy all other members from video
$vid_object->ts_created= $video->ts_created;
$vid_object->ts_last_modified= $video->ts_last_modified;
$vid_object->name = $video->videoname;
$vid_object->desc = $video->videodesc;
$vid_object->content = $video->embedcode;
$vid_object->url = $video->url;
$vid_object->tags = $video->getTags();
$vid_object->objid = $video->getId();
$options['object_id']= $video->getId() ;
$options['order']= 'p.ts_created';
$this->comments = DatabaseObject_Comment::GetComments($this->db,
'videos_comments', $options);
$comment_ids = array_keys($this->comments);
if (count($comment_ids) != 0 ) {
foreach ($this->comments as $key=>$value) {
$this->comments[$key]->username= DatabaseObject_User::
getUserNamefromId($this->db,
$value->user_id);
}
$vid_object->comments= $this->comments;
}
$vid_object->unread= DatabaseObject_Unread::IsExists($this->db,
$video->getId(), $this->user_id,
DatabaseObject_Video::getObjType());
$vid_object->starred= DatabaseObject_Starred::IsExists($this->db,
$video->getId(), $this->user_id,
DatabaseObject_Video::getObjType());
return $vid_object;
}
public function editAction()
{
$this->preprocessEditParams();
$request = $this->getRequest();
$video_id= (int) $request->getParam('id');
if (strlen($this->param_delete) >0) {
$video= new DatabaseObject_Video($this->db);
if($video->load($video_id)) {
if ($video->user_id == $this->user_id) {
$flag= $video->delete();
$this->view->actionsuccess= "Deleting Video is successful";
$this->addUserMenu();
$this->fillViewVariables();
return;
}
}
}
else if (strlen($this->param_url) >0) {
$video= new DatabaseObject_Video($this->db);
if($video->load($this->param_url, 'url')) {
if ($video->user_id == $this->user_id) {
$video_id= $video->getId();
$this->selected_usermenu_item= 'Edit Video';
}
}
}
else {
$this->selected_usermenu_item= 'Add Video';
}
$fp = new FormProcessor_NewVideo($this->db, $this->user_id, $video_id);
$validate = $request->isXmlHttpRequest();
if ($request->isPost()) {
if ($validate) {
$fp->validateOnly(true);
$fp->process($request);
}
else if ($fp->process($request)) {
$this->view->url = $this->getUrl('view'). '/url/'. $fp->video->url ;
$this->_redirect($this->getUrl('view'). '/url/'.$fp->video->url) ;
}
}
if ($validate) {
$json = array('errors' => $fp->getErrors());
$this->sendJson($json);
}
else {
$this->view->fp = $fp;
}
//call common functions, independent of action
$this->addUserMenu();
$this->fillViewVariables();
}
public function saveAction()
{
$this->preprocessSaveParams(DatabaseObject_Video::getObjType());
$request= $this->getRequest();
if ($this->objid> 0) {
if ($this->tagschanged != '') {
$this->tags= DatabaseObject_Video::updateTags($this->db, $this->objid, $this->tags);
}
}
if ($request->isXmlHttpRequest()) {
$this->_helper->viewRenderer->setNoRender();
if ($this->tagschanged != ''){
echo $this->tags;
}
}
else {
//redirect
}
}
public function commentAction()
{
$request = $this->getRequest();
$this->param_url= trim($request->getUserParam('url'));
$video= new DatabaseObject_Video($this->db);
if ($video->load($this->param_url, 'url')) {
$this->objid= $video->getId();
}
parent::commentAction();
}
}
?>