@@ -566,7 +566,7 @@ public function getOptionsFromValues($attid, $store_id, $optvals=null)
566
566
}
567
567
$ t1 = $ this ->tablename ('eav_attribute_option ' );
568
568
$ t2 = $ this ->tablename ('eav_attribute_option_value ' );
569
- $ sql = "SELECT optvals.option_id as opvs,optvals.value FROM $ t2 as optvals " ;
569
+ $ sql = "SELECT optvals.option_id as opvs,optvals.value,opt.sort_order FROM $ t2 as optvals " ;
570
570
$ sql .= " JOIN $ t1 as opt ON opt.option_id=optvals.option_id AND opt.attribute_id=? " ;
571
571
$ sql .= " WHERE optvals.store_id=? $ extra " ;
572
572
return $ this ->selectAll ($ sql , array_merge (array ($ attid ,$ store_id ), $ optvals ));
@@ -599,6 +599,16 @@ public function createOptionValue($optid, $store_id, $optval)
599
599
return $ optval_id ;
600
600
}
601
601
602
+ /**
603
+ * updates option positioning
604
+ * @param $optid option id
605
+ * @param $newpos new position
606
+ */
607
+ public function updateOptPos ($ optid ,$ newpos )
608
+ {
609
+ $ t = $ this ->tablename ('eav_attribute_option ' );
610
+ $ this ->update ("UPDATE $ t SET sort_order=? WHERE option_id=? " ,array ($ newpos ,$ optid ));
611
+ }
602
612
/**
603
613
* Returns option ids for a given store for a set of values (for select/multiselect attributes)
604
614
* - Create new entries if values do not exist
@@ -615,9 +625,25 @@ public function getOptionIds($attid, $storeid, $values)
615
625
{
616
626
$ svalues = array (); // store specific values
617
627
$ avalues = array (); // default (admin) values
618
- // Checking if we want to "translate" values existing in admin
619
- foreach ($ values as $ val )
628
+ $ pvalues =array ();
629
+
630
+ for ($ i =0 ;$ i <count ($ values );$ i ++)
620
631
{
632
+ //if a position is defined for the option, memorize it
633
+ $ pvals =explode ("|| " ,$ values [$ i ]);
634
+ if (count ($ pvals )>1 )
635
+ {
636
+ $ pval =trim ($ pvals [1 ]);
637
+ if ($ pval !="" ) {
638
+ $ pvalues [] = intval ($ pval );
639
+ }
640
+ }
641
+ else
642
+ {
643
+ $ pvalues []=-1 ;
644
+ }
645
+ $ val =$ pvals [0 ];
646
+
621
647
// if we have a reference value in admin
622
648
if (preg_match ("|^(.*)::\[(.*)\]$| " , $ val , $ matches ))
623
649
{
@@ -638,34 +664,45 @@ public function getOptionIds($attid, $storeid, $values)
638
664
// this array contains two items:
639
665
// 'opvs' => the option id;
640
666
// 'value' => the corresponding admin value
641
- $ optAdmin = $ this ->getCachedOptIds ($ attid ,0 );
642
- //missing admin values in cache
667
+ $ optAdmin = $ this ->getCachedOpts ($ attid ,0 );
668
+ //for all defined values
643
669
for ($ i =0 ;$ i <$ cval ;$ i ++)
644
670
{
671
+ $ pos =$ pvalues [$ i ];
672
+ //if not existing in cache,create it
645
673
if (!isset ($ optAdmin [$ avalues [$ i ]]))
646
674
{
647
675
//create new option entry
648
- $ newoptid = $ this ->createOption ($ attid );
676
+ $ newoptid = $ this ->createOption ($ attid, $ pos );
649
677
$ this ->createOptionValue ($ newoptid , 0 ,$ avalues [$ i ]);
650
678
//cache new created one
651
- $ this ->cacheOptId ($ attid , 0 , $ newoptid , $ avalues [$ i ]);
679
+ $ this ->cacheOpt ($ attid , 0 , $ newoptid , $ avalues [$ i ],$ pos ==-1 ?0 :$ pos );
680
+ }
681
+ //else check for position change
682
+ else {
683
+ $ curopt =$ optAdmin [$ avalues [$ i ]];
684
+ if ($ pos !=-1 && $ pos !=$ curopt [1 ])
685
+ {
686
+ $ this ->updateOptPos ($ curopt [0 ],$ pvalues [$ i ]);
687
+ $ this ->cacheOpt ($ attid , 0 , $ curopt [0 ], $ avalues [$ i ],$ pos );
688
+ }
652
689
}
653
690
}
654
691
655
692
//operating on store values
656
693
if ($ storeid !=0 )
657
694
{
658
- $ optExisting =$ this ->getCachedOptIds ($ attid ,$ storeid );
695
+ $ optExisting =$ this ->getCachedOpts ($ attid ,$ storeid );
659
696
//iterating on store values
660
697
for ($ i =0 ;$ i <$ cval ;$ i ++)
661
698
{
662
699
//if missing
663
700
if (!isset ($ optExisting [$ svalues [$ i ]]))
664
701
{
665
702
//get option id from admin
666
- $ optid =$ this ->getCachedOptId ($ attid ,0 ,$ avalues [$ i ]);
667
- $ this ->createOptionValue ($ optid ,$ storeid ,$ svalues [$ i ]);
668
- $ this ->cacheOptId ($ attid ,$ storeid ,$ optid ,$ svalues [$ i ]);
703
+ $ opt =$ this ->getCachedOpt ($ attid ,0 ,$ avalues [$ i ]);
704
+ $ this ->createOptionValue ($ opt [ 0 ] ,$ storeid ,$ svalues [$ i ]);
705
+ $ this ->cacheOpt ($ attid ,$ storeid ,$ opt [ 0 ] ,$ svalues [$ i], $ opt [ 1 ]);
669
706
670
707
}
671
708
}
@@ -675,7 +712,8 @@ public function getOptionIds($attid, $storeid, $values)
675
712
for ($ i =0 ;$ i <$ cval ;$ i ++)
676
713
{
677
714
$ av =$ avalues [$ i ];
678
- $ optids [$ av ]=$ this ->getCachedOptId ($ attid ,0 ,$ av );
715
+ $ opt =$ this ->getCachedOpt ($ attid ,0 ,$ av );
716
+ $ optids [$ av ]=$ opt [0 ];
679
717
}
680
718
681
719
@@ -687,30 +725,30 @@ public function getOptionIds($attid, $storeid, $values)
687
725
return $ optids ;
688
726
}
689
727
728
+
690
729
/**
691
- * Adds a new option definition row in the cache
692
- *
693
- * @param unknown $attid
694
- * attribute id
695
- * @param unknown $row
696
- * option definition
730
+ * Cache an option definition
731
+ * @param $attid attribute id
732
+ * @param $storeid store id
733
+ * @param $optid option id
734
+ * @param $val value for option
735
+ * @param int $pos position for option
697
736
*/
698
- public function cacheOptIds ($ attid , $ storeid ,$ data )
699
- {
700
- $ akey ="a $ attid " ;
701
- $ skey ="s $ storeid " ;
702
- $ this ->_optidcache [$ akey ][$ skey ] = $ data ;
703
- }
704
-
705
-
706
- public function cacheOptId ($ attid ,$ storeid ,$ optid ,$ val )
737
+ public function cacheOpt ($ attid ,$ storeid ,$ optid ,$ val ,$ pos =0 )
707
738
{
708
739
$ akey ="a $ attid " ;
709
740
$ skey ="s $ storeid " ;
710
- $ this ->_optidcache [$ akey ][$ skey ][$ val ]=$ optid ;
741
+ $ this ->_optidcache [$ akey ][$ skey ][$ val ]=array ( $ optid, $ pos ) ;
711
742
}
712
743
713
- public function getCachedOptId ($ attid ,$ storeid ,$ val )
744
+ /**
745
+ * Retrieve a cache entry for option
746
+ * @param $attid attribute id
747
+ * @param $storeid store id
748
+ * @param $val value to get option id
749
+ * @return mixed cache entry for option (array with value=>array(option_id,position)
750
+ */
751
+ public function getCachedOpt ($ attid ,$ storeid ,$ val )
714
752
{
715
753
$ akey ="a $ attid " ;
716
754
$ skey ="s $ storeid " ;
@@ -724,7 +762,7 @@ public function getCachedOptId($attid,$storeid,$val)
724
762
* attribute id
725
763
* @return NULL or option definition rows found
726
764
*/
727
- public function getCachedOptIds ($ attid ,$ storeid =0 )
765
+ public function getCachedOpts ($ attid ,$ storeid =0 )
728
766
{
729
767
$ akey ="a $ attid " ;
730
768
$ skey ="s $ storeid " ;
@@ -738,7 +776,7 @@ public function getCachedOptIds($attid,$storeid=0)
738
776
$ exvals = $ this ->getOptionsFromValues ($ attid , $ storeid );
739
777
foreach ($ exvals as $ optdesc )
740
778
{
741
- $ this ->cacheOptId ($ attid ,$ storeid ,$ optdesc ['opvs ' ],$ optdesc ['value ' ]);
779
+ $ this ->cacheOpt ($ attid ,$ storeid ,$ optdesc ['opvs ' ],$ optdesc ['value ' ], $ optdesc [ ' sort_order ' ]);
742
780
}
743
781
}
744
782
0 commit comments