diff --git a/etemplate/js/et2_widget_favorites.js b/etemplate/js/et2_widget_favorites.js index cf369242eb..84da129433 100644 --- a/etemplate/js/et2_widget_favorites.js +++ b/etemplate/js/et2_widget_favorites.js @@ -99,18 +99,20 @@ var et2_favorites = et2_dropdown_button.extend([et2_INextmatchHeader], var egw_fw = egw_getFramework(); this.sidebox_target = $j("#"+this.options.sidebox_target,egw_fw.sidemenuDiv); } - + // Store array of sorted items + this.favSortedList = []; + var apps = egw().user('apps'); this.is_admin = (typeof apps['admin'] != "undefined"); - + this.stored_filters = this.load_favorites(this.options.app); - + this.preferred = egw.preference(this.options.default_pref,this.options.app); if(!this.preferred || typeof this.stored_filters[this.preferred] == "undefined") { this.preferred = "blank"; } - + // It helps to have the ID properly set before we get too far this.set_id(this.id); @@ -149,7 +151,33 @@ var et2_favorites = et2_dropdown_button.extend([et2_INextmatchHeader], self.button.removeClass("ui-state-active",2000); }); }); - + + //Add Sortable handler to nm fav. menu + $j(this.menu).sortable({ + + items:'li:not([data-id^="add"])', + placeholder:'ui-fav-sortable-placeholder', + update: function (event, ui) + { + self.favSortedList = jQuery(this).sortable('toArray', {attribute:'data-id'}); + + self.egw().set_preference(self.options.app,'fav_sort_pref',self.favSortedList); + this.sidebox_target.children() + } + }); + $j(this.sidebox_target.children()).sortable({ + + items:'li:not([data-id^="add"])', + placeholder:'ui-fav-sortable-placeholder', + update: function (event, ui) + { + self.favSortedList = jQuery(this).sortable('toArray', {attribute:'data-id'}); + + self.egw().set_preference(self.options.app,'fav_sort_pref',self.favSortedList); + self.init_filters(self,self.load_favorites(self.options.app)); + } + }); + // Add a listener on the delete to remove this.menu.on("click","div.ui-icon-trash", app[self.options.app], function() { // App instance might not be ready yet, so don't bind directly @@ -167,7 +195,7 @@ var et2_favorites = et2_dropdown_button.extend([et2_INextmatchHeader], this.init_filters(this); } }, - + /** * Load favorites from preferences * @@ -182,7 +210,7 @@ var et2_favorites = et2_dropdown_button.extend([et2_INextmatchHeader], state: {} } }; - + // Load saved favorites var preferences = egw.preference("*",app); for(var pref_name in preferences) @@ -197,12 +225,34 @@ var et2_favorites = et2_dropdown_button.extend([et2_INextmatchHeader], stored_filters[pref_name].state = preferences[pref_name].filters; } } + if (pref_name == 'fav_sort_pref') + { + this.favSortedList = preferences[pref_name]; + } } if(typeof stored_filters == "undefined" || !stored_filters) { stored_filters = {}; } + else if (this.favSortedList.length > 0) + { + var sortedListObj = {}; + for (var i=0;i < this.favSortedList.length;i++) + { + if (typeof stored_filters[this.favSortedList[i]] != 'undefined') + { + sortedListObj[this.favSortedList[i]] = stored_filters[this.favSortedList[i]]; + } + else + { + this.favSortedList.splice(i,1); + this.egw().set_preference (this.options.app,'fav_sort_pref',this.favSortedList); + } + } + stored_filters = jQuery.extend(sortedListObj,stored_filters); + } + return stored_filters; }, diff --git a/phpgwapi/inc/class.egw_favorites.inc.php b/phpgwapi/inc/class.egw_favorites.inc.php index 1c8eed7f10..ac0d0cd927 100644 --- a/phpgwapi/inc/class.egw_favorites.inc.php +++ b/phpgwapi/inc/class.egw_favorites.inc.php @@ -40,32 +40,36 @@ class egw_favorites */ public static function list_favorites($app, $default=null) { - if(!$app) return ''; + if (!$app) + { + return ''; + } - if (!$default) $default = "nextmatch-$app.index.rows-favorite"; + if (!$default) + { + $default = "nextmatch-$app.index.rows-favorite"; + } // This target is used client-side to find & enable adding new favorites $target = 'favorite_sidebox_'.$app; - $pref_prefix = 'favorite_'; - $filters = array( - 'blank' => array( - 'name' => lang('No filters'), - // Old - 'filter' => array(), - // New - 'state' => array(), - 'group' => true - ) - ) + self::get_favorites($app); + + /* @var $filters array an array of favorites*/ + $filters = self::get_favorites($app); $is_admin = $GLOBALS['egw_info']['user']['apps']['admin']; $html = "'; @@ -95,7 +99,29 @@ class egw_favorites ), ); } - + + /** + * Get preferenced favorites sorted list + * + * @param string $app Application name as string + * + * @return (array|boolean) An array of sorted favorites or False if there's no preferenced sorted list + * + */ + public static function get_fav_sort_pref ($app) + { + $fav_sorted_list = array(); + + if (($fav_sorted_list = $GLOBALS['egw_info']['user']['preferences'][$app]['fav_sort_pref'])) + { + return $fav_sorted_list; + } + else + { + return false; + } + } + /** * Get a list of actual user favorites * The default 'Blank' favorite is not included here @@ -106,9 +132,21 @@ class egw_favorites */ public static function get_favorites($app) { - $favorites = array(); + $favorites = array( + 'blank' => array( + 'name' => lang('No filters'), + // Old + 'filter' => array(), + // New + 'state' => array(), + 'group' => true + ) + ); $pref_prefix = 'favorite_'; - + + $sorted_list = array(); + $fav_sort_pref = self::get_fav_sort_pref($app); + // Look through all preferences & pull out favorites foreach($GLOBALS['egw_info']['user']['preferences'][$app] as $pref_name => $pref) { @@ -116,7 +154,10 @@ class egw_favorites { if(!is_array($pref)) // old favorite { - if (!($pref = unserialize($pref))) continue; + if (!($pref = unserialize($pref))) + { + continue; + } $pref = array( 'name' => substr($pref_name,strlen($pref_prefix)), 'group' => !isset($GLOBALS['egw']->preferences->user[$app][$pref_name]), @@ -128,7 +169,14 @@ class egw_favorites $favorites[(string)substr($pref_name,strlen($pref_prefix))] = $pref; } } - + if (is_array($sorted_list)) + { + foreach ($fav_sort_pref as $key) + { + $sorted_list[$key] = $favorites[$key]; + } + $favorites = $sorted_list; + } return $favorites; }