@@ -3047,126 +3047,40 @@ public function getTreeModel()
30473047 }
30483048
30493049 /**
3050- * Get ordered models by their parents.
3051- *
3050+ * Get ordered models by their parents
3051+ * add extra
3052+ * @lonalore
30523053 * @return e_admin_tree_model
30533054 */
30543055 public function getTreeModelSorted ()
30553056 {
30563057 $ tree = $ this ->getTreeModel ();
30573058
3058- // Helper arrays for preparing new tree model.
3059- $ models = array ();
3060- $ levels = array ();
3061-
3062-
3059+ $ parentField = $ this ->getSortParent ();
3060+ $ orderField = $ this ->getSortField ();
30633061
3064- // Calculate depth for each model.
3065- /** @var e_admin_model $model */
3066- foreach ($ tree ->getTree () as $ id => $ model )
3062+ $ arr = array ();
3063+ foreach ($ tree ->getTree () as $ id => $ model )
30673064 {
3068- $ depth = $ this ->calculateModelDepth ($ model );
3069-
3070- if (!in_array ($ depth , $ levels ))
3071- {
3072- $ levels [] = $ depth ;
3073- }
3065+ $ parent = $ model ->get ($ parentField );
3066+ $ order = $ model ->get ($ orderField );
30743067
3075- $ model ->set ('_depth ' , $ depth );
3076- $ model ->set ('_id ' , $ id );
3077- $ models [$ id ] = $ model ;
3078- }
3079-
3080- // First, we sort models by $sortField.
3081- uasort ($ models , function ($ modelA , $ modelB ) {
3082- $ sortField = $ this ->getSortField ();
3083-
3084- /** @var e_admin_model $modelA */
3085- /** @var e_admin_model $modelB */
3086-
3087- $ weightA = (int ) $ modelA ->get ($ sortField );
3088- $ weightB = (int ) $ modelB ->get ($ sortField );
3089-
3090- if ($ weightA == $ weightB ) {
3091- return 0 ;
3092- }
3068+ $ model ->set ('_depth ' , '9999 ' ); // include extra field in output, just as the MySQL function did.
30933069
3094- return ($ weightA < $ weightB ) ? -1 : 1 ;
3095- });
30963070
3097- $ direction = 'ASC ' ;
3098-
3099- if ($ direction == 'DESC ' )
3100- {
3101- $ models = array_reverse ($ models , true );
3071+ $ arr [$ id ] = $ model ;
31023072 }
31033073
3104- // Now, we sort models by hierarchy.
3105- foreach ($ levels as $ level )
3106- {
3107- uasort ($ models , function ($ modelA , $ modelB ) {
3108- $ parentField = $ this ->getSortParent ();
3109-
3110- /** @var e_admin_model $modelA */
3111- /** @var e_admin_model $modelB */
31123074
3113- $ parentA = (int ) $ modelA ->get ($ parentField );
3114- $ parentB = (int ) $ modelB ->get ($ parentField );
3115- $ idA = (int ) $ modelA ->get ('_id ' );
3075+ // usort($arr); array_multisort() ?
31163076
3117- // If A is the parent of B or both parents are the same.
3118- if ($ idA == $ parentB || $ parentA == $ parentB ) {
3119- return 0 ;
3120- }
3121-
3122- return 1 ;
3123- });
3124- }
3077+ $ tree ->setTree ($ arr ,true ); // set the newly ordered tree.
31253078
3126- // Set the newly ordered tree.
3127- $ tree ->setTree ($ models , true );
3079+ var_dump ($ arr );
31283080
31293081 return $ this ->_tree_model ;
31303082 }
31313083
3132- /**
3133- * Calculates '_depth' property for the given model.
3134- *
3135- * @param e_admin_model $model
3136- * Admin model we want to get '_depth' property for.
3137- *
3138- * @return int
3139- * Depth for the e_admin_model object.
3140- */
3141- public function calculateModelDepth ($ model )
3142- {
3143- $ parentField = $ this ->getSortParent ();
3144- $ parentID = (int ) $ model ->get ($ parentField );
3145-
3146- // Default depth.
3147- $ depth = 1 ;
3148-
3149- // If no parent.
3150- if ($ parentID === 0 )
3151- {
3152- return $ depth ;
3153- }
3154-
3155- $ tree = $ this ->getTreeModel ();
3156-
3157- /** @var e_admin_model $_model */
3158- foreach ($ tree ->getTree () as $ id => $ _model )
3159- {
3160- if ($ id == $ parentID )
3161- {
3162- $ depth += $ this ->calculateModelDepth ($ _model );
3163- break ;
3164- }
3165- }
3166-
3167- return $ depth ;
3168- }
3169-
31703084
31713085 /**
31723086 * @lonalore - found online.
@@ -4289,25 +4203,20 @@ protected function _modifyListQry($raw = false, $isfilter = false, $forceFrom =
42894203 {
42904204 $ qry = $ this ->parseCustomListQry ($ listQry );
42914205 }
4292- /* elseif($this->sortField && $this->sortParent) // automated 'tree' sorting.
4206+ elseif ($ this ->sortField && $ this ->sortParent && ! deftrue ( ' e_DEBUG_TREESORT ' ) ) // automated 'tree' sorting.
42934207 {
4294- // $qry = $this->getParentChildQry();
4295- // $this->listOrder = '_treesort '; // .$this->sortField;
4208+ // $qry = "SELECT SQL_CALC_FOUND_ROWS a. *, CASE WHEN a.".$this->sortParent." = 0 THEN a.".$this->sortField." ELSE b.".$this->sortField." + (( a.".$this->sortField.")/1000) END AS treesort FROM `#".$this->table."` AS a LEFT JOIN `#".$this->table."` AS b ON a.".$this->sortParent." = b.".$this->pid;
4209+ $ qry = $ this ->getParentChildQry ();
4210+ $ this ->listOrder = '_treesort ' ; // .$this->sortField;
42964211 // $this->orderStep = ($this->orderStep === 1) ? 100 : $this->orderStep;
4297- }*/
4212+ }
42984213 else
42994214 {
43004215 $ qry = "SELECT SQL_CALC_FOUND_ROWS " .$ tableSFields ." FROM " .$ tableFrom ;
43014216 }
43024217
43034218 }
43044219
4305- if (empty ($ this ->listOrder ) && $ this ->sortField && $ this ->sortParent )
4306- {
4307- $ this ->listOrder = $ this ->sortField ;
4308- }
4309-
4310-
43114220 // group field - currently auto-added only if there are joins
43124221 // TODO - groupField property
43134222 $ groupField = '' ;
@@ -6607,15 +6516,12 @@ public function getList($ajax = false, $view='default')
66076516 $ request = $ controller ->getRequest ();
66086517 $ id = $ this ->getElementId ();
66096518 $ tree = $ options = array ();
6519+ $ tree [$ id ] = $ controller ->getTreeModel ();
66106520
6611- if ($ view === 'default ' && $ controller ->getSortParent () && $ controller ->getSortField ()) // parent / child sorted tree.
6612- {
6613- e107 ::getDebug ()->log ("getTreeModelSorted " );
6614- $ tree [$ id ] = $ controller ->getTreeModelSorted ();
6615- }
6616- else
6521+
6522+ if (deftrue ('e_DEBUG_TREESORT ' ) && $ view === 'default ' )
66176523 {
6618- $ tree [ $ id ] = $ controller ->getTreeModel ();
6524+ $ controller ->getTreeModelSorted ();
66196525 }
66206526
66216527 // if going through confirm screen - no JS confirm
0 commit comments