Skip to content

Commit 5a7e9c4

Browse files
committed
added support for option positioning (||pos in attribute value)
1 parent 672934c commit 5a7e9c4

File tree

1 file changed

+69
-31
lines changed

1 file changed

+69
-31
lines changed

magmi/engines/magmi_productimportengine.php

Lines changed: 69 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ public function getOptionsFromValues($attid, $store_id, $optvals=null)
566566
}
567567
$t1 = $this->tablename('eav_attribute_option');
568568
$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";
570570
$sql .= " JOIN $t1 as opt ON opt.option_id=optvals.option_id AND opt.attribute_id=?";
571571
$sql .= " WHERE optvals.store_id=? $extra";
572572
return $this->selectAll($sql, array_merge(array($attid,$store_id), $optvals));
@@ -599,6 +599,16 @@ public function createOptionValue($optid, $store_id, $optval)
599599
return $optval_id;
600600
}
601601

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+
}
602612
/**
603613
* Returns option ids for a given store for a set of values (for select/multiselect attributes)
604614
* - Create new entries if values do not exist
@@ -615,9 +625,25 @@ public function getOptionIds($attid, $storeid, $values)
615625
{
616626
$svalues = array(); // store specific values
617627
$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++)
620631
{
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+
621647
// if we have a reference value in admin
622648
if (preg_match("|^(.*)::\[(.*)\]$|", $val, $matches))
623649
{
@@ -638,34 +664,45 @@ public function getOptionIds($attid, $storeid, $values)
638664
// this array contains two items:
639665
// 'opvs' => the option id;
640666
// '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
643669
for($i=0;$i<$cval;$i++)
644670
{
671+
$pos=$pvalues[$i];
672+
//if not existing in cache,create it
645673
if(!isset($optAdmin[$avalues[$i]]))
646674
{
647675
//create new option entry
648-
$newoptid = $this->createOption($attid);
676+
$newoptid = $this->createOption($attid,$pos);
649677
$this->createOptionValue($newoptid, 0,$avalues[$i]);
650678
//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+
}
652689
}
653690
}
654691

655692
//operating on store values
656693
if($storeid!=0)
657694
{
658-
$optExisting=$this->getCachedOptIds($attid,$storeid);
695+
$optExisting=$this->getCachedOpts($attid,$storeid);
659696
//iterating on store values
660697
for($i=0;$i<$cval;$i++)
661698
{
662699
//if missing
663700
if(!isset($optExisting[$svalues[$i]]))
664701
{
665702
//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]);
669706

670707
}
671708
}
@@ -675,7 +712,8 @@ public function getOptionIds($attid, $storeid, $values)
675712
for($i=0;$i<$cval;$i++)
676713
{
677714
$av=$avalues[$i];
678-
$optids[$av]=$this->getCachedOptId($attid,0,$av);
715+
$opt=$this->getCachedOpt($attid,0,$av);
716+
$optids[$av]=$opt[0];
679717
}
680718

681719

@@ -687,30 +725,30 @@ public function getOptionIds($attid, $storeid, $values)
687725
return $optids;
688726
}
689727

728+
690729
/**
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
697736
*/
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)
707738
{
708739
$akey="a$attid";
709740
$skey="s$storeid";
710-
$this->_optidcache[$akey][$skey][$val]=$optid;
741+
$this->_optidcache[$akey][$skey][$val]=array($optid,$pos);
711742
}
712743

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)
714752
{
715753
$akey="a$attid";
716754
$skey="s$storeid";
@@ -724,7 +762,7 @@ public function getCachedOptId($attid,$storeid,$val)
724762
* attribute id
725763
* @return NULL or option definition rows found
726764
*/
727-
public function getCachedOptIds($attid,$storeid=0)
765+
public function getCachedOpts($attid,$storeid=0)
728766
{
729767
$akey="a$attid";
730768
$skey="s$storeid";
@@ -738,7 +776,7 @@ public function getCachedOptIds($attid,$storeid=0)
738776
$exvals = $this->getOptionsFromValues($attid, $storeid);
739777
foreach ($exvals as $optdesc)
740778
{
741-
$this->cacheOptId($attid,$storeid,$optdesc['opvs'],$optdesc['value']);
779+
$this->cacheOpt($attid,$storeid,$optdesc['opvs'],$optdesc['value'],$optdesc['sort_order']);
742780
}
743781
}
744782

0 commit comments

Comments
 (0)