PageRenderTime 44ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/magmi/plugins/base/itemprocessors/columnmapper/000_columnmapper.php

https://bitbucket.org/jit_bec/shopifine
PHP | 115 lines | 91 code | 9 blank | 15 comment | 8 complexity | 3173b435d4410411398489bbd8f2f2f4 MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. /**
  3. * Class SampleItemProcessor
  4. * @author dweeves
  5. *
  6. * This class is a sample for item processing
  7. */
  8. class ColumnMappingItemProcessor extends Magmi_ItemProcessor
  9. {
  10. protected $_dcols=array();
  11. public function getPluginInfo()
  12. {
  13. return array(
  14. "name" => "Column mapper",
  15. "author" => "Dweeves",
  16. "version" => "0.0.3",
  17. "url"=>"http://sourceforge.net/apps/mediawiki/magmi/index.php?title=Column_mapper"
  18. );
  19. }
  20. /**
  21. * you can add/remove columns for the item passed since it is passed by reference
  22. * @param Magmi_Engine $mmi : reference to magmi engine(convenient to perform database operations)
  23. * @param unknown_type $item : modifiable reference to item before import
  24. * the $item is a key/value array with column names as keys and values as read from csv file.
  25. * @return bool :
  26. * true if you want the item to be imported after your custom processing
  27. * false if you want to skip item import after your processing
  28. */
  29. public function processColumnList(&$cols,$params=null)
  30. {
  31. $icols=$cols;
  32. $ocols=array();
  33. $scols=array();
  34. foreach($icols as $cname)
  35. {
  36. if(isset($this->_dcols[$cname]))
  37. {
  38. $mlist=array_unique(explode(",",$this->_dcols[$cname]));
  39. $ncol=array_shift($mlist);
  40. $ocols[]=$ncol;
  41. if($ncol!=$cname)
  42. {
  43. $this->log("Replacing Column $cname by $ncol","startup");
  44. }
  45. if(count($mlist)>0)
  46. {
  47. $scols=array_merge($scols,$mlist);
  48. $this->log("Replicating Column $cname to ".implode(",",$mlist),"startup");
  49. }
  50. }
  51. else
  52. {
  53. $ocols[]=$cname;
  54. }
  55. }
  56. $ocols=array_unique(array_merge($ocols,$scols));
  57. $cols=$ocols;
  58. return true;
  59. }
  60. public function processItemBeforeId(&$item,$params=null)
  61. {
  62. foreach($this->_dcols as $oname=>$mnames)
  63. {
  64. if(isset($item[$oname]))
  65. {
  66. $mapped=explode(",",$mnames);
  67. foreach($mapped as $mname)
  68. {
  69. $item[$mname]=$item[$oname];
  70. }
  71. if(!in_array($oname,$mapped))
  72. {
  73. unset($item[$oname]);
  74. }
  75. }
  76. }
  77. return true;
  78. }
  79. public function initialize($params)
  80. {
  81. foreach($params as $k=>$v)
  82. {
  83. if(preg_match_all("/^CMAP:(.*)$/",$k,$m) && $k!="CMAP:columnlist")
  84. {
  85. $colname=rawurldecode($m[1][0]);
  86. $this->_dcols[$colname]=$params[$k];
  87. }
  88. }
  89. }
  90. public function getPluginParams($params)
  91. {
  92. $pp=array();
  93. foreach($params as $k=>$v)
  94. {
  95. if(preg_match("/^CMAP:.*$/",$k))
  96. {
  97. $pp[$k]=$v;
  98. }
  99. }
  100. return $pp;
  101. }
  102. static public function getCategory()
  103. {
  104. return "Input Data Preprocessing";
  105. }
  106. }