Remember user's preference of seeing unified or side-by-side blobdiffs
Remember user's preference of seeing unified or side-by-side blobdiffs

--- a/include/controller/Controller_Blobdiff.class.php
+++ b/include/controller/Controller_Blobdiff.class.php
@@ -11,6 +11,22 @@
  */
 
 /**
+ * Constants for blobdiff modes
+ */
+define('GITPHP_BLOBDIFF_UNIFIED', 1);
+define('GITPHP_BLOBDIFF_SIDEBYSIDE', 2);
+
+/**
+ * Constant of the blobdiff mode cookie in the user's browser
+ */
+define('GITPHP_BLOBDIFF_MODE_COOKIE', 'GitPHPBlobdiffMode');
+
+/**
+ * Blobdiff mode cookie lifetime
+ */
+define('GITPHP_BLOBDIFF_MODE_COOKIE_LIFETIME', 60*60*24*365);		// 1 year
+
+/**
  * Blobdiff controller class
  *
  * @package GitPHP
@@ -98,12 +114,40 @@
 			$this->params['hashbase'] = $_GET['hb'];
 		if (isset($_GET['hp']))
 			$this->params['hashparent'] = $_GET['hp'];
-		if (isset($_GET['o'])) {
-			if (!isset($this->params['plain']) || $this->params['plain'] != true) {
+
+		if (!isset($this->params['plain']) || $this->params['plain'] != true) {
+
+			$mode = GITPHP_BLOBDIFF_UNIFIED;	// default
+
+			/*
+			 * Check cookie
+			 */
+			if (!empty($_COOKIE[GITPHP_BLOBDIFF_MODE_COOKIE])) {
+				$mode = $_COOKIE[GITPHP_BLOBDIFF_MODE_COOKIE];
+			} else {
+				/*
+				 * Create cookie to prevent browser delay
+				 */
+				setcookie(GITPHP_BLOBDIFF_MODE_COOKIE, $mode, time()+GITPHP_BLOBDIFF_MODE_COOKIE_LIFETIME);
+			}
+
+			if (isset($_GET['o'])) {
+				/*
+				 * User is choosing a new mode
+				 */
 				if ($_GET['o'] == 'sidebyside') {
-					$this->params['sidebyside'] = true;
+					$mode = GITPHP_BLOBDIFF_SIDEBYSIDE;
+					setcookie(GITPHP_BLOBDIFF_MODE_COOKIE, GITPHP_BLOBDIFF_SIDEBYSIDE, time()+GITPHP_BLOBDIFF_MODE_COOKIE_LIFETIME);
+				} else if ($_GET['o'] == 'unified') {
+					$mode = GITPHP_BLOBDIFF_UNIFIED;
+					setcookie(GITPHP_BLOBDIFF_MODE_COOKIE, GITPHP_BLOBDIFF_UNIFIED, time()+GITPHP_BLOBDIFF_MODE_COOKIE_LIFETIME);
 				}
 			}
+
+			if ($mode == GITPHP_BLOBDIFF_SIDEBYSIDE) {
+				$this->params['sidebyside'] = true;
+			}
+
 		}
 	}
 

comments