From 2b2a1db055970e3675e2d1b76997366f5dd54611 Mon Sep 17 00:00:00 2001
From: James <james@sourcefabric-DX4840.(none)>
Date: Tue, 13 Nov 2012 17:31:30 -0500
Subject: [PATCH] CC-2401: Shuffle option in playlists

- done
---
 .../controllers/PlaylistController.php        | 28 +++++++++++++++++--
 airtime_mvc/application/models/Playlist.php   | 27 ++++++++++++++++--
 .../views/scripts/playlist/playlist.phtml     |  3 ++
 airtime_mvc/public/js/airtime/library/spl.js  | 20 +++++++++++++
 .../js/airtime/playlist/smart_blockbuilder.js |  2 +-
 5 files changed, 75 insertions(+), 5 deletions(-)

diff --git a/airtime_mvc/application/controllers/PlaylistController.php b/airtime_mvc/application/controllers/PlaylistController.php
index 0d2ead443..7283bba48 100644
--- a/airtime_mvc/application/controllers/PlaylistController.php
+++ b/airtime_mvc/application/controllers/PlaylistController.php
@@ -25,6 +25,7 @@ class PlaylistController extends Zend_Controller_Action
                     ->addActionContext('smart-block-generate', 'json')
                     ->addActionContext('smart-block-shuffle', 'json')
                     ->addActionContext('get-block-info', 'json')
+                    ->addActionContext('shuffle', 'json')
                     ->initContext();
 
     }
@@ -72,7 +73,6 @@ class PlaylistController extends Zend_Controller_Action
             $isBlock = true;
             $viewPath = 'playlist/smart-block.phtml';
         }
-
         if (isset($obj)) {
             $formatter = new LengthFormatter($obj->getLength());
             $this->view->length = $formatter->format();
@@ -94,7 +94,11 @@ class PlaylistController extends Zend_Controller_Action
             } else {
                 $this->view->obj = $obj;
                 $this->view->id = $obj->getId();
-                $this->view->html = $this->view->render($viewPath);
+                if ($isJson) {
+                    return $this->view->html = $this->view->render($viewPath);
+                } else {
+                    $this->view->html = $this->view->render($viewPath);
+                }
                 unset($this->view->obj);
             }
         } else {
@@ -537,6 +541,26 @@ class PlaylistController extends Zend_Controller_Action
             $this->playlistUnknownError($e);
         }
     }
+    
+    public function shuffleAction()
+    {
+        $request = $this->getRequest();
+        $params = $request->getPost();
+        try {
+            $pl = new Application_Model_Playlist($params['obj_id']);
+            $result = $pl->shuffle();
+            
+            if ($result['result'] == 0) {
+                die(json_encode(array("result"=>0, "html"=>$this->createFullResponse($pl, true))));
+            } else {
+                die(json_encode($result));
+            }
+        } catch (PlaylistNotFoundException $e) {
+            $this->playlistNotFound('block', true);
+        } catch (Exception $e) {
+            $this->playlistUnknownError($e);
+        }
+    }
 
     public function getBlockInfoAction()
     {
diff --git a/airtime_mvc/application/models/Playlist.php b/airtime_mvc/application/models/Playlist.php
index 8abf649d1..4e9562c72 100644
--- a/airtime_mvc/application/models/Playlist.php
+++ b/airtime_mvc/application/models/Playlist.php
@@ -387,8 +387,8 @@ SQL;
         }
 
         if (isset($obj)) {
-            if (($obj instanceof CcFiles && $obj->visible()) 
-                || $obj instanceof CcWebstream || 
+            if (($obj instanceof CcFiles && $obj->visible())
+                || $obj instanceof CcWebstream ||
                 $obj instanceof CcBlock) {
 
                 $entry               = $this->plItem;
@@ -933,6 +933,29 @@ SQL;
     {
         CcPlaylistcontentsQuery::create()->findByDbPlaylistId($this->id)->delete();
     }
+    
+    public function shuffle()
+    {
+        $sql = <<<SQL
+SELECT max(position) from cc_playlistcontents WHERE playlist_id=:p1
+SQL;
+        $out = Application_Common_Database::prepareAndExecute($sql, array("p1"=>$this->id));
+        $maxPosition = $out[0]['max'];
+        
+        $map = range(0, $maxPosition);
+        shuffle($map);
+        
+        $currentPos = implode(',', array_keys($map));
+        $sql = "UPDATE cc_playlistcontents SET position = CASE position ";
+        foreach ($map as $current => $after) {
+            $sql .= sprintf("WHEN %d THEN %d ", $current, $after);
+        }
+        $sql .= "END WHERE position IN ($currentPos) and playlist_id=:p1";
+        
+        Application_Common_Database::prepareAndExecute($sql, array("p1"=>$this->id));
+        $result['result'] = 0;
+        return $result;
+    }
 
 } // class Playlist
 
diff --git a/airtime_mvc/application/views/scripts/playlist/playlist.phtml b/airtime_mvc/application/views/scripts/playlist/playlist.phtml
index 6f962b97b..d287abaec 100644
--- a/airtime_mvc/application/views/scripts/playlist/playlist.phtml
+++ b/airtime_mvc/application/views/scripts/playlist/playlist.phtml
@@ -16,6 +16,9 @@ if (isset($this->obj)) {
         </ul>
     </div>
 <?php if (isset($this->obj)) : ?>
+    <div class='btn-group pull-right'>
+        <button class="btn btn-inverse" title='Shuffle playlist' type="button" id="playlist_shuffle_button">Shuffle</button>
+    </div>
     <div class='btn-group pull-right'>
         <button class="btn btn-inverse" title='Save playlist' type="button" id="save_button">Save</button>
     </div>
diff --git a/airtime_mvc/public/js/airtime/library/spl.js b/airtime_mvc/public/js/airtime/library/spl.js
index 21a11218b..b5a808986 100644
--- a/airtime_mvc/public/js/airtime/library/spl.js
+++ b/airtime_mvc/public/js/airtime/library/spl.js
@@ -637,6 +637,26 @@ var AIRTIME = (function(AIRTIME){
 	        	$fs.addClass("closed");
 	        }
 	    });
+		
+
+	    $pl.on("click", 'button[id="playlist_shuffle_button"]', function(){
+	        obj_id = $('input[id="obj_id"]').val();
+	        url = "/Playlist/shuffle";
+    	    enableLoadingIcon();
+    	    $.post(url, {format: "json", obj_id: obj_id}, function(data){
+    	        var json = $.parseJSON(data)
+
+    	        if (json.error !== undefined) {
+    	            alert(json.error);
+    	        }
+    	        AIRTIME.playlist.fnOpenPlaylist(json);
+    	        if (json.result == "0") {
+    	            $pl.find('.success').text('Playlist shuffled');
+    	            $pl.find('.success').show();
+    	        }
+    	        disableLoadingIcon();
+    	    });
+	    })
 
 		$pl.on("click", "#webstream_save", function(){
             //get all fields and POST to server
diff --git a/airtime_mvc/public/js/airtime/playlist/smart_blockbuilder.js b/airtime_mvc/public/js/airtime/playlist/smart_blockbuilder.js
index f963ab9cd..c472448aa 100644
--- a/airtime_mvc/public/js/airtime/playlist/smart_blockbuilder.js
+++ b/airtime_mvc/public/js/airtime/playlist/smart_blockbuilder.js
@@ -351,7 +351,7 @@ function setupUI() {
      * It is only active if playlist is not empty
      */
     var plContents = $('#spl_sortable').children();
-    var shuffleButton = $('button[id="shuffle_button"]');
+    var shuffleButton = $('button[id="shuffle_button"], button[id="playlist_shuffle_button"]');
 
     if (!plContents.hasClass('spl_empty')) {
         if (shuffleButton.hasClass('ui-state-disabled')) {