Index: admin/preferences.php
===================================================================
--- admin/preferences.php	(revision 2206)
+++ admin/preferences.php	(revision 2229)
@@ -215,32 +215,13 @@
 			throw new Exception(__('No favorite selected'));
 		}
-
-		$ws = $core->auth->user_prefs->addWorkspace('favorites');
-		$user_favs = $ws->DumpLocalPrefs();
-		$count = count($user_favs);
+		$user_favs = $core->favs->getFavoriteIDs(false);
 		foreach ($_POST['append'] as $k => $v)
 		{
-			try {
-				$found = false;
-				foreach ($user_favs as $f) {
-					$f = unserialize($f['value']);
-					if ($f['name'] == $v) {
-						$found = true;
-						break;
-					}
-				}
-				if (!$found) {
-					$uid = sprintf("u%03s",$count);
-					$fav = array('name' => $_fav[$v][0],'title' => $_fav[$v][1],'url' => $_fav[$v][2],'small-icon' => $_fav[$v][3],
-						'large-icon' => $_fav[$v][4],'permissions' => $_fav[$v][5],'id' => $_fav[$v][6],'class' => $_fav[$v][7]);
-					$core->auth->user_prefs->favorites->put($uid,serialize($fav),'string');
-					$count++;
-				}
-			} catch (Exception $e) {
-				$core->error->add($e->getMessage());
-				break;
+			if ($core->favs->exists($v)) {
+				$user_favs[] = $v;
 			}
 		}
-	
+		$core->favs->setFavoriteIDs($user_favs,false);
+
 		if (!$core->error->flag()) {
 			http::redirect('preferences.php?append=1');
@@ -258,33 +239,15 @@
 			throw new Exception(__('No favorite selected'));
 		}
-		
-		$ws = $core->auth->user_prefs->addWorkspace('favorites');
-		foreach ($_POST['remove'] as $k => $v)
+		$user_fav_ids = array();
+		foreach ($core->favs->getFavoriteIDs(false) as $v) {
+			$user_fav_ids[$v]=true;
+		}
+		foreach ($_POST['remove'] as $v)
 		{
-			try {
-				$core->auth->user_prefs->favorites->drop($v);
-			} catch (Exception $e) {
-				$core->error->add($e->getMessage());
-				break;
+			if (isset($user_fav_ids[$v])) {
+				unset($user_fav_ids[$v]);
 			}
 		}
-		// Update pref_id values
-		try {
-			$user_favs = $ws->DumpLocalPrefs();
-			$core->auth->user_prefs->favorites->dropAll();
-			$count = 0;
-			foreach ($user_favs as $k => $v)
-			{
-				$uid = sprintf("u%03s",$count);
-				$f = unserialize($v['value']);
-				$fav = array('name' => $f['name'],'title' => $f['title'],'url' => $f['url'],'small-icon' => $f['small-icon'],
-					'large-icon' => $f['large-icon'],'permissions' => $f['permissions'],'id' => $f['id'],'class' => $f['class']);
-				$core->auth->user_prefs->favorites->put($uid,serialize($fav),'string');
-				$count++;
-			}
-		} catch (Exception $e) {
-			$core->error->add($e->getMessage());
-		}
-	
+		$core->favs->setFavoriteIDs(array_keys($user_fav_ids),false);
 		if (!$core->error->flag()) {
 			http::redirect('preferences.php?removed=1');
@@ -307,21 +270,10 @@
 if (!empty($_POST['saveorder']) && !empty($order))
 {
-	try {
-		$ws = $core->auth->user_prefs->addWorkspace('favorites');
-		$user_favs = $ws->DumpLocalPrefs();
-		$core->auth->user_prefs->favorites->dropAll();
-		$count = 0;
-		foreach ($order as $i => $k) {
-			$uid = sprintf("u%03s",$count);
-			$f = unserialize($user_favs[$k]['value']);
-			$fav = array('name' => $f['name'],'title' => $f['title'],'url' => $f['url'],'small-icon' => $f['small-icon'],
-				'large-icon' => $f['large-icon'],'permissions' => $f['permissions'],'id' => $f['id'],'class' => $f['class']);
-			$core->auth->user_prefs->favorites->put($uid,serialize($fav),'string');
-			$count++;
-		}
-	} catch (Exception $e) {
-		$core->error->add($e->getMessage());
-	}
-
+	foreach ($order as $k => $v) {
+		if (!$core->favs->exists($v)) {
+			unset($order[$k]);
+		}
+	}	
+	$core->favs->setFavoriteIDs($order,false);
 	if (!$core->error->flag()) {
 		http::redirect('preferences.php?&neworder=1');
@@ -331,21 +283,6 @@
 # Replace default favorites by current set (super admin only)
 if (!empty($_POST['replace']) && $core->auth->isSuperAdmin()) {
-	try {
-		$ws = $core->auth->user_prefs->addWorkspace('favorites');
-		$user_favs = $ws->DumpLocalPrefs();
-		$core->auth->user_prefs->favorites->dropAll(true);
-		$count = 0;
-		foreach ($user_favs as $k => $v)
-		{
-			$uid = sprintf("g%03s",$count);
-			$f = unserialize($v['value']);
-			$fav = array('name' => $f['name'],'title' => $f['title'],'url' => $f['url'],'small-icon' => $f['small-icon'],
-				'large-icon' => $f['large-icon'],'permissions' => $f['permissions'],'id' => $f['id'],'class' => $f['class']);
-			$core->auth->user_prefs->favorites->put($uid,serialize($fav),'string',null,null,true);
-			$count++;
-		}
-	} catch (Exception $e) {
-		$core->error->add($e->getMessage());
-	}
+	$user_favs = $core->favs->getFavoriteIDs(false);
+	$core->favs->setFavoriteIDs($user_favs,true);
 
 	if (!$core->error->flag()) {
@@ -548,20 +485,17 @@
 
 $count = 0;
-$user_fav = array();
-foreach ($ws->dumpPrefs() as $k => $v) {
-	// User favorites only
-	if (!$v['global']) {
-		$fav = unserialize($v['value']);
-		if (($fav['permissions'] == '*') || $core->auth->check($fav['permissions'],$core->blog->id)) {
-			if ($count == 0) echo '<ul class="fav-list">';
-			$count++;
-			echo '<li id="fu-'.$k.'">'.'<label for="fuk-'.$k.'">'.
-				'<img src="'.dc_admin_icon_url($fav['small-icon']).'" alt="" /> '.'<span class="zoom"><img src="'.dc_admin_icon_url($fav['large-icon']).'" alt="" /></span>'.
-				form::field(array('order['.$k.']'),2,3,$count,'position','',false,'title="'.sprintf(__('position of %s'),$fav['title']).'"').
-				form::hidden(array('dynorder[]','dynorder-'.$k.''),$k).
-				form::checkbox(array('remove[]','fuk-'.$k),$k).__($fav['title']).'</label>'.
-				'</li>';
-			$user_fav[] = $fav['name'];
-		}
+$user_fav = $core->favs->getFavoriteIDs(false);
+foreach ($user_fav as $id) {
+	$fav = $core->favs->getFavorite($id);
+	if ($fav != false) {
+		// User favorites only
+		if ($count == 0) echo '<ul class="fav-list">';
+		$count++;
+		echo '<li id="fu-'.$id.'">'.'<label for="fuk-'.$id.'">'.
+			'<img src="'.dc_admin_icon_url($fav['small-icon']).'" alt="" /> '.'<span class="zoom"><img src="'.dc_admin_icon_url($fav['large-icon']).'" alt="" /></span>'.
+			form::field(array('order['.$id.']'),2,3,$count,'position','',false,'title="'.sprintf(__('position of %s'),$fav['title']).'"').
+			form::hidden(array('dynorder[]','dynorder-'.$id.''),$id).
+			form::checkbox(array('remove[]','fuk-'.$id),$id).__($fav['title']).'</label>'.
+			'</li>';
 	}
 }
@@ -593,15 +527,9 @@
 }
 
-$default_fav = array();
-foreach ($ws->dumpPrefs() as $k => $v) {
-	// Global favorites only
-	if ($v['global']) {
-		$fav = unserialize($v['value']);
-		if (($fav['permissions'] == '*') || $core->auth->check($fav['permissions'],$core->blog->id)) {
-			$default_fav[] = $fav['name'];
-		}
-	}
-}
-
+$avail_fav = $core->favs->getFavorites($core->favs->getAvailableFavoritesIDs());
+$default_fav_ids = array();
+foreach($core->favs->getFavoriteIDs(true) as $v) {
+	$default_fav_ids[$v]=true;
+}
 echo '</div>'; # /box my-fav
 
@@ -610,27 +538,27 @@
 echo '<h5 class="pretty-title">'.__('Other available favorites').'</h5>';
 $count = 0;
-$array = $_fav;
 function cmp($a,$b) {
-    if (__($a[1]) == __($b[1])) {
+    if ($a['title'] == $b['title']) {
         return 0;
     }
-    return (__($a[1]) < __($b[1])) ? -1 : 1;
-}
-$array=$array->getArrayCopy();
-uasort($array,'cmp');
-foreach ($array as $k => $fav) {
-	if (($fav[5] == '*') || $core->auth->check($fav[5],$core->blog->id)) {
-		if (!in_array($fav[0], $user_fav)) {
-			if ($count == 0) echo '<ul class="fav-list">';
-			$count++;
-			echo '<li id="fa-'.$fav[0].'">'.'<label for="fak-'.$fav[0].'">'.
-				'<img src="'.dc_admin_icon_url($fav[3]).'" alt="" /> '.
-				'<span class="zoom"><img src="'.dc_admin_icon_url($fav[4]).'" alt="" /></span>'.
-				form::checkbox(array('append[]','fak-'.$fav[0]),$k).
-				__($fav[1]).'</label>'.
-				(in_array($fav[0], $default_fav) ? ' <span class="default-fav"><img src="images/selected.png" alt="'.__('(default favorite)').'" /></span>' : '').
-				'</li>';
-		}
-	}
+    return ($a['title'] < $b['title']) ? -1 : 1;
+}
+uasort($avail_fav,'cmp');
+
+foreach ($avail_fav as $k => $v) {
+	if (in_array($k,$user_fav)) {
+		unset($avail_fav[$k]);
+	}
+}
+foreach ($avail_fav as $k=>$fav) {
+	if ($count == 0) echo '<ul class="fav-list">';
+	$count++;
+	echo '<li id="fa-'.$k.'">'.'<label for="fak-'.$k.'">'.
+		'<img src="'.dc_admin_icon_url($fav['small-icon']).'" alt="" /> '.
+		'<span class="zoom"><img src="'.dc_admin_icon_url($fav['large-icon']).'" alt="" /></span>'.
+		form::checkbox(array('append[]','fak-'.$k),$k).
+		$fav['title'].'</label>'.
+		(isset($default_fav_ids[$k]) ? ' <span class="default-fav"><img src="images/selected.png" alt="'.__('(default favorite)').'" /></span>' : '').
+		'</li>';
 }
 if ($count > 0) echo '</ul>';
