PageRenderTime 75ms CodeModel.GetById 24ms RepoModel.GetById 3ms app.codeStats 1ms

/magmi/plugins/base/itemprocessors/importlimiter/01_importlimiter.php

https://bitbucket.org/jit_bec/shopifine
PHP | 159 lines | 145 code | 13 blank | 1 comment | 20 complexity | f332e69652546e6681f6e51028140c8a MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. class ImportLimiter extends Magmi_ItemProcessor
  3. {
  4. protected $_recranges;
  5. protected $_rmax=-1;
  6. protected $_filters;
  7. public function getPluginInfo()
  8. {
  9. return array("name"=>"Magmi Import Limiter",
  10. "author"=>"Dweeves",
  11. "version"=>"0.0.5",
  12. "url"=>"http://sourceforge.net/apps/mediawiki/magmi/index.php?title=Magmi_Import_Limiter");
  13. }
  14. public function filtermatch($item,$fltdef)
  15. {
  16. $negate=0;
  17. $field=$fltdef[0];
  18. $match=false;
  19. if($field[0]=="!")
  20. {
  21. $field=substr($field,1);
  22. $negate=1;
  23. }
  24. $re=$fltdef[1];
  25. if(in_array($field,array_keys($item)))
  26. {
  27. $v=$item[$field];
  28. $match=preg_match("|$re|",$v);
  29. if($negate)
  30. {
  31. $match=!$match;
  32. }
  33. if($match)
  34. {
  35. $this->log("skipping sku {$item['sku']} => Filter '$field::$re'","info");
  36. }
  37. }
  38. return $match;
  39. }
  40. public function processItemBeforeId(&$item,$params=null)
  41. {
  42. $crow=$this->getCurrentRow();
  43. $ok=(count($this->_recranges)==0);
  44. if(!$ok)
  45. {
  46. if($this->_rmax>-1 && $crow==$this->_rmax)
  47. {
  48. $this->setLastItem($item);
  49. }
  50. foreach($this->_recranges as $rr)
  51. {
  52. $ok=($crow>=$rr[0] && ($crow<=$rr[1] || $rr[1]==-1));
  53. if($ok)
  54. {
  55. break;
  56. }
  57. }
  58. }
  59. if($ok)
  60. {
  61. foreach($this->_filters as $fltdef)
  62. {
  63. //negative filters
  64. $ok=$ok && (!$this->filtermatch($item,$fltdef));
  65. if(!$ok)
  66. {
  67. break;
  68. }
  69. }
  70. }
  71. else
  72. {
  73. $this->log("Filtered row $crow not in range ".$this->getParam("LIMITER:ranges",""));
  74. }
  75. return $ok;
  76. }
  77. public function parseFilters($fltstr)
  78. {
  79. $this->_filters=array();
  80. if($fltstr=="")
  81. {
  82. return;
  83. }
  84. $fltlist=explode(";;",$fltstr);
  85. foreach($fltlist as $fltdef)
  86. {
  87. $fltinf=explode("::",$fltdef);
  88. $this->_filters[]=$fltinf;
  89. }
  90. }
  91. public function parseRanges($rangestr)
  92. {
  93. $this->_recranges=array();
  94. if($rangestr=="")
  95. {
  96. return;
  97. }
  98. $rangelist=explode(",",$rangestr);
  99. foreach($rangelist as $rdef)
  100. {
  101. $rlist=explode("-",$rdef);
  102. if($rlist[0]=="")
  103. {
  104. $rlist[0]=-1;
  105. }
  106. else
  107. {
  108. $rmin=$rlist[0];
  109. }
  110. if(count($rlist)>1)
  111. {
  112. if($rlist[1]=="")
  113. {
  114. $rlist[1]=-1;
  115. }
  116. else
  117. {
  118. $rmax=$rlist[1];
  119. if($rmax>$this->_rmax && $this->_rmax!=-1)
  120. {
  121. $this->_rmax=$rmax;
  122. }
  123. }
  124. }
  125. else
  126. {
  127. $rmax=$rmin;
  128. }
  129. $this->_recranges[]=array($rmin,$rmax);
  130. }
  131. }
  132. public function initialize($params)
  133. {
  134. $this->parseRanges($this->getParam("LIMITER:ranges",""));
  135. $this->parseFilters($this->getParam("LIMITER:filters",""));
  136. return true;
  137. }
  138. public function getPluginParamNames()
  139. {
  140. return array('LIMITER:ranges','LIMITER:filters');
  141. }
  142. static public function getCategory()
  143. {
  144. return "Input Data Preprocessing";
  145. }
  146. }