PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/magmi/plugins/base/itemprocessors/genericmapper/02_genericmapper.php

https://bitbucket.org/jit_bec/shopifine
PHP | 123 lines | 55 code | 9 blank | 59 comment | 5 complexity | 10c04aaafe183eebe46cbec36542a96f 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 GenericMapperProcessor extends Magmi_ItemProcessor
  9. {
  10. protected $_mapping;
  11. public function getPluginInfo()
  12. {
  13. return array(
  14. "name" => "Generic mapper",
  15. "author" => "Dweeves",
  16. "version" => "0.0.6a",
  17. "url" => "http://sourceforge.net/apps/mediawiki/magmi/index.php?title=Generic_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 instance (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 processItemBeforeId(&$item,$params=null)
  30. {
  31. foreach(array_keys($item) as $k)
  32. {
  33. $mapped=false;
  34. if(isset($this->_mapping["$k.csv"]))
  35. {
  36. $mpd=$this->_mapping["$k.csv"]["DIRECT"];
  37. if(isset($mpd[$item[$k]]))
  38. {
  39. $item[$k]=$mpd[$item[$k]];
  40. $mapped=true;
  41. }
  42. else
  43. {
  44. $mpr=$this->_mapping["$k.csv"]["RE"];
  45. foreach($mpr as $re=>$value)
  46. {
  47. if(preg_match("|$re|msi",$item[$k]))
  48. {
  49. $item[$k]=preg_replace("|$re|",$value,$item[$k]);
  50. $mapped=true;
  51. break;
  52. }
  53. }
  54. }
  55. }
  56. #if not found,try common mappings
  57. if(!$mapped)
  58. {
  59. $mpd=$this->_mapping["__common__.csv"]["DIRECT"];
  60. if(isset($mpd[$item[$k]]))
  61. {
  62. $item[$k]=$mpd[$item[$k]];
  63. }
  64. }
  65. }
  66. return true;
  67. }
  68. public function initialize($params)
  69. {
  70. $this->_mapping=array();
  71. $dlist=glob(dirname(__file__)."/mappings/default/*.csv");
  72. if($dlist==false)
  73. {
  74. $dlist=array();
  75. $this->log("No default mapping found","warning");
  76. }
  77. $slist=glob(dirname(__file__)."/mappings/*.csv");
  78. if($slist==false)
  79. {
  80. $slist=array();
  81. $this->log("No custom mapping found","startup");
  82. }
  83. $flist=array_merge($dlist,$slist);
  84. foreach($flist as $fname)
  85. {
  86. $idx=basename($fname);
  87. if(!isset($this->_mapping[$idx]))
  88. {
  89. $this->_mapping[$idx]=array("DIRECT"=>array(),"RE"=>array());
  90. }
  91. $mf=fopen("$fname","r");
  92. while (($data = fgetcsv($mf, 1000, ",")) !== FALSE)
  93. {
  94. if(substr($data[0],0,4)=="_RE:")
  95. {
  96. $target="RE";
  97. $key=substr($data[0],4);
  98. }
  99. else
  100. {
  101. $target="DIRECT";
  102. $key=$data[0];
  103. }
  104. $this->_mapping[$idx][$target][$key]=$data[1];
  105. }
  106. }
  107. }
  108. static public function getCategory()
  109. {
  110. return "Input Data Preprocessing";
  111. }
  112. }