Location: PHPKode > projects > OSShare > osshare-project-v1.0.1/app/controllers/plsitems_controller.php
<?php
/*
OsShare v1 , 
Coded By Paimpozhil B. , SaravanaKumar M.S.
*/
class PlsitemsController extends AppController {
	var $layout = 'user';
	var $name = 'Plsitems';
	var $helpers = array('Html', 'Form','Javascript','ajax','Pagination' ,'Time');
	var $uses= array('Pl','User','Video','Plsitem','Sitesetting');
	var $components = array ('Pagination');

	function index() {
		$this->Plsitem->recursive = 0;
		$this->set('plsitems', $this->paginate());
	}
	
	function addtolist($id = null)
	{
		$data = $this->dolayout();
		$usid = $data["id"];
		$this->set('vdid',$id);		
		if(!$id) {
			$this->Session->setFlash('Invalid Playlist.');
			$this->redirect(array('action'=>'index'), null, true);
		}
		$play_list = $this->Pl->findallbyuser_id($usid);
		$this->set('pl',$play_list);
	}	

	function add($id = null , $vdid = null)
	{
		$data = $this->dolayout();
		$usid = $data["id"];
		if(!empty($this->data)) {
			$this->cleanUpFields();
			$this->Plsitem->create();	
			if($this->Plsitem->save($this->data))
			{
				if($this->data["Plsitem"]["ajax_id"] ==1 )
				{
				$this->layout = false;
				$this->render('ajaxadd');
				Configure::write('debug', '0');
				return 0;
				}
				$this->redirect(array('action'=>'itemview',$this->data["Plsitem"]["pl_id"]));	
				$this->Session->setFlash('Successfully Added');
			}
		}
//		$items = $this->Plsitem->findbysql('SELECT sum(precedence) from `Plsitem`');
		$play_items = $this->Video->findbyid($vdid);
		$play_name = $this->Pl->findbyid($id);
		
		//debug($play_items);
		$this->set('vdname',$play_items);
		$this->set('plname',$play_name);
		/*if(($items == null) or (count($items) == 1))
			//debug($items);
			$this->set('preced',$items);
		else
		{
			$items = $this->Plsitem->findbypl_id($id,"sum(precedence) as sp");
			//echo array_sum($items);
			//$num = array_sum($items);
			debug($items);
			$this->set('preced',0);
			$this->set('spreced',$items);
		}*/
		$this->set('plid',$id);
		$this->set('vdid',$vdid);
	
	}
	
	function itemview($id = null)
	{
		$data = $this->dolayout();
		$usid = $data["id"];
		$items = $this->Plsitem->findallbypl_id($id);
		$playlist_name = $this->Pl->findbyid($id);
		$this->set('plitems',$items);
		$this->set('plname',$playlist_name);
//		debug($items);
//		debug($playlist_name);
		if($items == null)
		{
			$this->Session->SetFlash("Your Playlist is Empty");
		}
		else
		{
		$i=1;
		foreach($items as $it)
		{
			//echo $it["Plsitem"]["precedence"];
			$it["Plsitem"]["precedence"] = $i++;
			$this->Plsitem->save($it);
		}
		}
	}
	function playall($id = null)
	{
		$data = $this->dolayout();
		$usid = $data["id"];
		$items = $this->Plsitem->findallbypl_id($id);
		if($items == null)
		{
			$this->Session->SetFlash("Please add the items to play");
			$this->redirect(array('action'=>'index','controller'=>'pls'));
		}
		$playlist_name = $this->Pl->findbyid($id);
		$rel_playlist = $this->Pl->findallbyuser_id($usid,"*",null,10);
//		debug($rel_playlist);
		$settings = $this->Sitesetting->findbyid(1);
		//Social
		$social = $settings['Sitesetting']['SocialTools'];
		$this->set('social',$social);
		//debug($social);
		$this->set('plitems',$items);
		$this->set('plname',$playlist_name);
		$this->set('id',$id);
		$this->set('rel_playlist',$rel_playlist);
		$this->set('settings',$settings);		
	}
	
	function userplayall($id = null,$usid = null)
	{
		$data = $this->dolayout();
		$items = $this->Plsitem->findallbypl_id($id);
		$playlist_name = $this->Pl->findbyid($id);
		$rel_playlist = $this->Pl->findallbyuser_id($usid,"*",null,10);
//		debug($rel_playlist);
		$settings = $this->Sitesetting->findbyid(1);
		//Social
		$social = $settings['Sitesetting']['SocialTools'];
		$this->set('social',$social);
		//debug($social);
		$this->set('plitems',$items);
		$this->set('plname',$playlist_name);
		$this->set('id',$id);
		$this->set('rel_playlist',$rel_playlist);
		$this->set('settings',$settings);		
	}
	
	function pl_rss($id = null)
	{
		$items = $this->Plsitem->findallbypl_id($id);
		$playlist_name = $this->Pl->findbyid($id);
		$settings = $this->Sitesetting->findbyid(1);
		$no_items = count($items);
		$i=1;
		foreach($items as $it)
		{		
			//echo $it["Video"]["user_id"];
			$user_name = $this->User->findbyid($it["Video"]["user_id"]);
			$usname = 'usname'.$i;
			$this->set($usname,$user_name);
			//echo 'usname'.$i.'Z';
	//		debug($user_name);
		}
	
		$this->set('plitems',$items);
		$this->set('playlist_name',$playlist_name);
		$this->set('settings',$settings);	
		Configure::write('debug', '0');	
		$this->render('pl_rss','rss');
	}
	function operate($id = null,$plid = null,$swid = null,$opid = null)
	{
		$operate_item = $this->Plsitem->findbyid($id);
		$items = $this->Plsitem->findallbypl_id($plid);
		$playlist_name = $this->Pl->findbyid($plid);
		$this->set('plitems',$items);
		$this->set('plname',$playlist_name);
		$this->set('operate_item',$operate_item);
		if(isset($swid))
		{
			$swap_item = $this->Plsitem->findbyid($swid);
			$op_item = $this->Plsitem->findbyid($opid);
			//debug($swap_item);
			//debug($op_item);
			$temp_item = $swap_item["Plsitem"]["video_id"];
			$swap_item["Plsitem"]["video_id"] = $op_item["Plsitem"]["video_id"];
			$this->Plsitem->save($swap_item);
			$op_item["Plsitem"]["video_id"] = $temp_item;
			if($this->Plsitem->save($op_item))
			{
				$this->Session->setFlash('Successfully Altred');
				$this->redirect(array('action'=>'itemview',$plid));	
			}
			else
			{
				$this->Session->SetFlash('Could not Move');
			}
		}
		/*$this->set('plitems',$items);
		//debug($items);
		$i=1;
		foreach($items as $it)
		{
			//echo $it["Plsitem"]["precedence"];
			$it["Plsitem"]["precedence"] = $i++;
			$this->Plsitem->save($it);
		}*/
	}
	function operate_up($id = null,$plid = null,$preced_id = null)
	{
		$operate_item = $this->Plsitem->findbyid($id);
		$items = $this->Plsitem->findallbypl_id($plid);
//		$tpre = $operate_item["Plsitem"]["precedence"];
		if($preced_id == 1)
		{
			$preced_id = count($items);
			$condition = "`Plsitem`.precedence=".$preced_id . " AND `Plsitem`.pl_id=".$plid;
		}
		else
		{
			$preced_id = $preced_id - 1;
			$condition = "`Plsitem`.precedence=".$preced_id . " AND `Plsitem`.pl_id=".$plid;
		}
//		$tpre = tpre - 1;
//		debug($tpre);
//		die();
		$swap_item = $this->Plsitem->find($condition);
		$temp_item = $swap_item["Plsitem"]["video_id"];
		$swap_item["Plsitem"]["video_id"] = $operate_item["Plsitem"]["video_id"];
		$this->Plsitem->save($swap_item);
		$operate_item["Plsitem"]["video_id"] = $temp_item;
		if($this->Plsitem->save($operate_item))
			{
				$this->Session->setFlash('Successfully Altred');
				$this->redirect(array('action'=>'itemview',$plid));	
			}
		else
			{
				$this->Session->SetFlash('Could not Move');
			}
		
	}
	
	function operate_down($id = null,$plid = null,$preced_id = null)
	{
		$operate_item = $this->Plsitem->findbyid($id);
		$items = $this->Plsitem->findallbypl_id($plid);
//		$tpre = $operate_item["Plsitem"]["precedence"];
		if($preced_id == count($items))
		{
			$condition = "`Plsitem`.precedence=1 AND `Plsitem`.pl_id=".$plid;
		}
		else
		{	
			$preced_id = $preced_id + 1;
			$condition = "`Plsitem`.precedence=".$preced_id . " AND `Plsitem`.pl_id=".$plid;
		}
//		$tpre = tpre - 1;
//		debug($tpre);
//		die();
		$swap_item = $this->Plsitem->find($condition);
		$temp_item = $swap_item["Plsitem"]["video_id"];
		$swap_item["Plsitem"]["video_id"] = $operate_item["Plsitem"]["video_id"];
		$this->Plsitem->save($swap_item);
		$operate_item["Plsitem"]["video_id"] = $temp_item;
		if($this->Plsitem->save($operate_item))
			{
				$this->Session->setFlash('Successfully Altred');
				$this->redirect(array('action'=>'itemview',$plid));	
			}
		else
			{
				$this->Session->SetFlash('Could not Move');
			}
		
	}
	function removeitem($id = null,$plid = null)
	{
 		if(!$id) {
			$this->Session->setFlash('Could not Be Deleted');
			$this->redirect(array('action'=>'itemview',$plid));
		}
		if($this->Plsitem->del($id)) {
			$this->Session->setFlash('Item deleted');
			$this->redirect(array('action'=>'itemview',$plid));
		}
	}	
		
	/*function add($id = null , $vdid = null)
	{	
		if(!empty($this->data)) {
			$this->cleanUpFields();
			$this->Plsitem->create();		
			if($this->Plsitem->save($this->data))
			{
				$this->Session->setFlash('Successfully Added');
				$this->redirect(array('action'=>'index'), null, true);
			}
		}
		$items = $this->Plsitem->findallbypl_id($id);
		//debug($items);
		if($items == null)
			$this->set('preced',1);
		else
		{
			$num = count($items) + 1;
			$this->set('preced',$num);
		}
		$this->set('plid',$id);
			$this->set('vdid',$vdid);
	}*/
		
}	
	
?>
Return current item: OSShare