Created
June 29, 2018 09:20
-
-
Save firecentaur/d590b001ac37bb242ed75bcfa2391b63 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Copyright (c) 2018. Movie English Project | |
* Unauthorized copying of this file, via any medium is strictly prohibited | |
* Proprietary and confidential | |
* Written by Navya Devadiga <devadiga.navya@gmail.com> | |
*/ | |
class ChannelManagerController extends Controller | |
{ | |
/** | |
* @return array | |
*/ | |
public function filters() | |
{ | |
return [ | |
'accessControl', // perform access control for CRUD operations | |
['application.filters.CanModifyHomepageChannelFilter + getChannels'],//must be run after fid, and visibility filter | |
['application.filters.GetObjIdFilter + checkDuplicateChannelName, remove,deleteChannelWithVideoTags,updateChannelAndVideoTags'], | |
['application.filters.GetNameFilter + checkDuplicateChannelName, create,deleteChannelWithVideoTags,updateChannelAndVideoTags'], | |
['application.filters.GetOrderFilter + create,updateChannelAndVideoTags'], | |
['application.filters.GetLandingOrderFilter + create, updateChannelAndVideoTags'], | |
['application.filters.GetThumbnailFilter + create, updateChannelAndVideoTags'], | |
['application.filters.GetCategoryListFilter + create, updateChannelAndVideoTags'], | |
['application.filters.OutputFilter'], | |
]; | |
} | |
/** | |
* @return array|void | |
*/ | |
public function accessRules() | |
{ | |
} | |
/** | |
* This is triggered via an api call | |
* This function is used in tagService | |
*/ | |
public function actionGetAllHomePageChannels() | |
{ | |
$tags = []; | |
try{ | |
$tags = ChannelManager::getAllPossibleHomePageChannels(); | |
if($tags){ | |
$this->message = Yii::t('tag','Successfully retrieved homepage tags'); | |
$this->success = true; | |
$this->data = ["homePageTags" => $tags]; | |
}else{ | |
$this->message = Yii::t('tag','There was a problem while retrieving the homepage tags'); | |
$this->success = false; | |
$this->data = []; | |
} | |
} | |
catch (CException $ex) { | |
$this->data = []; | |
$this->message = $ex->getMessage(); | |
$this->success = false; | |
} | |
} | |
/** | |
* this funciton is used to check if the channel name created is duplicate | |
* the api is trigerred via channelService | |
*/ | |
public function actionCheckDuplicateChannelName() { | |
$result = ChannelManager::getChannelByName($this->name); | |
if ($result) { | |
if ($this->objid === 'new') { | |
$this->success = true;//value was found so report as a duplicate | |
$this->message = Yii::t('users','A duplicate was found'); | |
} else { | |
if($this->objid === $result->id) { | |
$this->success = false;//value was found so report as a duplicate | |
$this->message = Yii::t('users', 'A duplicate was not found'); | |
} else { | |
$this->success = true; //value was found but it already belongs to the user so dont report as a duplicate | |
$this->message = Yii::t('users','A duplicate was found'); | |
} | |
} | |
} else { | |
$this->success = false;//value was NOT found so don't report as a duplicate | |
$this->message = Yii::t('users','A duplicate was not found'); | |
} | |
$this->data = []; | |
} | |
/** | |
* this function returns channels list along with videos count tagged to the channels | |
* @return array | |
*/ | |
public function getChannelsWithVideoCount(){ | |
$key= "getChannelsList"; | |
$listChannels=Yii::app()->cache->get($key); | |
if ($listChannels){ | |
return $listChannels; | |
} | |
$listChannels = ChannelManager::getAllChannels(); | |
$video = new VideoLibraryVideo(); | |
$channelList = []; | |
foreach ($listChannels as $channel){ | |
$channelData = []; | |
$channelData['id'] = $channel->id; | |
$channelData['name'] = $channel->name; | |
$channelData['attachments'] = $channel->getTranslations($channel->id); | |
$channelData['landing_order'] = $channel->landing_order; | |
$channelData['channel_order'] = $channel->channel_order; | |
if($channel->thumbnail) | |
$channelData['thumbnail'] = $channel->thumbnail; | |
else | |
$channelData['thumbnail'] = ""; | |
if(isset($channel->channelCategories)){ | |
$categoryList = []; | |
foreach ($channel->channelCategories as $key=>$eachChannelInCategory){ | |
$categoryData = CategoryManager::model()->findByPk($eachChannelInCategory->categoryId); | |
$categoryList[$key]['id'] = $categoryData->id; | |
$categoryList[$key]['name'] = $categoryData->name; | |
$categoryList[$key]['disabled'] = true; | |
} | |
if($categoryList) | |
$channelData['categories'] = $categoryList ; | |
else | |
$channelData['categories'] = []; | |
} | |
$data = $video->getVideosByChannelName($channel->name, 'Group', 9, 1, "", "", true); | |
if($data) | |
$channelData['channelVideoCount'] = $data[0]['total_videos'] ; | |
else | |
$channelData['channelVideoCount'] = 0; | |
$channelList[] = $channelData ; | |
} | |
app()->cache->set($key,$channelList); | |
return $channelList ; | |
} | |
/** | |
* This function returns the channel list and data along with the categories list to be used for category drop down when adding or creating channels | |
*/ | |
public function actionGetChannels(){ | |
$listChannels = $this->getChannelsWithVideoCount(); | |
$listCategories = CategoryManager::getAllCategoriesWithChannelData(); | |
$this->data = ['channels'=>$listChannels,'categories'=>$listCategories]; | |
if(count($listChannels) > 0){ | |
$this->success = true; | |
$this->message = Yii::t("channel","Successfully fetched all homepage channels"); | |
} | |
else{ | |
$this->success = true; // because even if channels doesn't exists, its not an error , returning false when | |
// channels doesn't exists forbids the user to navigate to channel manager to create a new channels | |
$this->message = Yii::t("channel","No homepage channels found."); | |
} | |
} | |
/** | |
* This function is used to create channels and returns the updated channels list and categories list to frontend | |
*/ | |
public function actionCreate(){ | |
$newChannel = ChannelManager::createChannel($this->name, $this->thumbnail, $this->channelOrder, $this->landingOrder); | |
//Now insert the channel categories if exists | |
if($this->categoryList !== null){ | |
foreach ($this->categoryList as $eachChannelCategory){ | |
ChannelCategories::insertChannelCategories($eachChannelCategory['id'],$newChannel->id); | |
} | |
} | |
$this->resetCache(); | |
$listChannels = $this->getChannelsWithVideoCount(); | |
$listCategories = CategoryManager::getAllCategoriesWithChannelData(); | |
if(count($listChannels) > 0){ | |
$this->data = ['channel'=>$listChannels,'categories'=>$listCategories]; | |
$this->success = true; | |
$this->message = Yii::t("channel","Successfully added channel : ".$this->name); | |
} | |
else{ | |
$this->data = []; | |
$this->success = false; | |
$this->message = Yii::t("channel","There was problem fetching the channels"); | |
} | |
} | |
/** | |
* This function is used to update the channel along with the tags data and returns the updated channels list and categories list to frontend | |
*/ | |
public function actionUpdateChannelAndVideoTags(){ | |
set_time_limit ( 300 ); | |
$oldChannelData = ChannelManager::model()->findByPk($this->objid); | |
$updatedChannel = ChannelManager::updateChannel($this->objid, $this->name, $this->thumbnail, | |
$this->channelOrder, $this->landingOrder); | |
//Now update the categories list | |
ChannelCategories::model()->deleteAllByAttributes( | |
['channelId' => $oldChannelData->id] | |
); | |
foreach ($this->categoryList as $eachChannelCategory){ | |
ChannelCategories::insertChannelCategories($eachChannelCategory['id'],$oldChannelData->id); | |
} | |
//first retrieve all channels and put in an array | |
$allChannels = ChannelManager::model()->findAll(); | |
$placeToInsert = $this->channelOrder; | |
$arr=[]; | |
$channelIndex = null; | |
//get channel's index, and build array of channels | |
foreach($allChannels as $index => $channel){ | |
$arr[]=$channel; | |
if ($channel->id ===$this->objid ){ | |
$channelIndex =$index; | |
} | |
} | |
$elementToReOrder=$updatedChannel; | |
//remove the item first | |
unset($arr[$channelIndex]); | |
//then insert into correct place | |
array_splice($arr, $this->channelOrder, 0, array($updatedChannel)); | |
foreach ($arr as $index => $element){ | |
$arr[$index]->channelOrder=$index; | |
} | |
//Update the tags for all the videos only if the channel name is changed | |
if($oldChannelData->name !== $this->name){ | |
$criteria = new CDbCriteria(); | |
$criteria->condition = "content = :oldChannelName and elementType = :elementType and type = :type and status is null"; | |
$criteria->params = [':oldChannelName' => $oldChannelData->name, ':elementType' => "Video", ':type' => "homepage"]; | |
Tag::model()->updateAll(array('content'=>$this->name),$criteria); | |
} | |
$this->resetCache(); | |
$listChannels = $this->getChannelsWithVideoCount(); | |
$listCategories = CategoryManager::getAllCategoriesWithChannelData(); | |
if(count($listChannels) > 0){ | |
$this->data = ['channel'=>$listChannels,'categories'=>$listCategories]; | |
$this->success = true; | |
$this->message = Yii::t("channel","Successfully updated channel : ".$this->name." and homapage tags for video"); | |
} | |
else{ | |
$this->data = []; | |
$this->success = false; | |
$this->message = Yii::t("channel","There was problem fetching the channels"); | |
} | |
} | |
/** | |
* This function is used to remove (soft delete) the channel and returns the updated channels list and categories list to frontend | |
*/ | |
public function actionRemove(){ | |
ChannelManager::removeChannel($this->objid, uid()); | |
$this->resetCache(); | |
//since we are not updating channel or landing page, set to null. | |
$listChannels = $this->getChannelsWithVideoCount(); | |
$listCategories = CategoryManager::getAllCategoriesWithChannelData(); | |
if(count($listChannels) > 0){ | |
$this->data = ['channel'=>$listChannels,'categories'=>$listCategories]; | |
$this->success = true; | |
$this->message = Yii::t("channel","Successfully removed channel : ".$this->name." from homepage."); | |
} | |
else{ | |
$this->data = []; | |
$this->success = false; | |
$this->message = Yii::t("channel","There was problem fetching the channels"); | |
} | |
} | |
/** | |
* This function is used to remove (soft delete) the channel along with the tags data and returns the updated channels list and categories list to frontend | |
*/ | |
public function actionDeleteChannelWithVideoTags(){ | |
ChannelManager::removeChannel($this->objid, uid()); | |
//since we are not updating channel or landing page, set to null. | |
$criteria = new CDbCriteria(); | |
$criteria->condition = "content = :channelName and elementType = :elementType and type = :type and status is null"; | |
$criteria->params = [':channelName' => $this->name, ':elementType' => "Video", ':type' => "homepage"]; | |
Tag::model()->updateAll(array('status'=>'deleted'),$criteria); | |
$this->resetCache(); | |
$listChannels = $this->getChannelsWithVideoCount(); | |
$listCategories = CategoryManager::getAllCategoriesWithChannelData(); | |
if(count($listChannels) > 0){ | |
$this->data = ['channel'=>$listChannels,'categories'=>$listCategories]; | |
$this->success = true; | |
$this->message = Yii::t("channel","Successfully removed channel : ".$this->name." from homepage and homepage tags from video"); | |
} | |
else{ | |
$this->data = []; | |
$this->success = false; | |
$this->message = Yii::t("channel","There was problem fetching the channels"); | |
} | |
} | |
/** | |
* This function is used to update the elastic search data in tagIndex data after a new channel is created or any channel is updated | |
*/ | |
public function actionUpdateTagIndex(){ | |
set_time_limit ( 900 ); // set the time to 15 minutes because tagindexing may take time | |
if($this->resetCache()){ // reset cache data | |
if(Tag::tagReIndex()){ | |
$listChannels = $this->getChannelsWithVideoCount(); | |
$listCategories = CategoryManager::getAllCategoriesWithChannelData(); | |
if(count($listChannels) > 0){ | |
$this->data = ['channel'=>$listChannels,'categories'=>$listCategories]; | |
$this->success = true; | |
$this->message = Yii::t("channel","Successfully reindexed the channels"); | |
} | |
else{ | |
$this->data = []; | |
$this->success = false; | |
$this->message = Yii::t("channel","There was problem reindexing the channels"); | |
} | |
} //Reindex Data | |
else{ | |
$this->data = []; | |
$this->success = false; | |
$this->message = Yii::t("channel","There was problem reindexing the channels"); | |
} | |
} | |
else{ | |
$this->data = []; | |
$this->success = false; | |
$this->message = Yii::t("channel","There was problem reindexing the channels"); | |
} | |
} | |
/** | |
* Resets the cache | |
* @return bool | |
*/ | |
public function resetCache(){ | |
$key= "getChannelsList"; | |
Yii::app()->cache->delete($key); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment