PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/magmi/plugins/base/itemprocessors/skufinder/001_skufinder.php

https://bitbucket.org/jit_bec/shopifine
PHP | 95 lines | 83 code | 5 blank | 7 comment | 13 complexity | c8596a6784af2dcb1f4a3dbe644f46aa MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. class SkuFinderItemProcessor extends Magmi_ItemProcessor
  3. {
  4. private $_compchecked=FALSE;
  5. public function getPluginInfo()
  6. {
  7. return array(
  8. "name" => "SKU Finder",
  9. "author" => "Dweeves",
  10. "version" => "0.0.1",
  11. "url"=>"http://sourceforge.net/apps/mediawiki/magmi/index.php?title=SKU_Finder"
  12. );
  13. }
  14. public function processItemBeforeId(&$item,$params=null)
  15. {
  16. $matchfield=trim($this->getParam("SKUF:matchfield"));
  17. //protection from tricky testers ;)
  18. if($matchfield=="sku")
  19. {
  20. return true;
  21. }
  22. $attinfo=$this->getAttrInfo($matchfield);
  23. if($this->_compchecked==FALSE)
  24. {
  25. //Checking attribute compatibility with sku matching
  26. if($attinfo==NULL)
  27. {
  28. $this->log("$matchfield is not a valid attribute","error");
  29. $item["__MAGMI_LAST__"]=1;
  30. return false;
  31. }
  32. if($attinfo["is_unique"]==0 || $attinfo["is_global"]==0)
  33. {
  34. $this->log("sku matching attribute $matchfield must be unique & global scope");
  35. $item["__MAGMI_LAST__"]=1;
  36. return false;
  37. }
  38. if($attinfo["backend_type"]=="static")
  39. {
  40. $this->log("$matchfield is ".$attinfo["backend_type"].", it cannot be used as sku matching field.","error");
  41. $item["__MAGMI_LAST__"]=1;
  42. return false;
  43. }
  44. if($attinfo["frontend_input"]=="select" || $attinfo["frontend_input"]=="multiselect" )
  45. {
  46. $this->log("$matchfield is ".$attinfo["frontend_input"].", it cannot be used as sku matching field.","error");
  47. $item["__MAGMI_LAST__"]=1;
  48. return false;
  49. }
  50. $this->_compchecked=true;
  51. }
  52. //no item data for selected matching field, skipping
  53. if(!isset($item[$matchfield]) && trim($item["matchfield"])!=='')
  54. {
  55. $this->log("No value for $matchfield in datasource","error");
  56. return false;
  57. }
  58. //now find sku
  59. $sql="SELECT sku FROM ".$this->tablename("catalog_product_entity")." as cpe JOIN
  60. catalog_product_entity_".$attinfo["backend_type"]. "ON value=? AND attribute_id=?";
  61. $stmt=$this->select($sql,array($item[$matchfield],$attinfo["attribute_id"]));
  62. $n=0;
  63. while($result=$stmt->fetch())
  64. {
  65. //if more than one result, cannot match single sku
  66. if($n>1)
  67. {
  68. $this->log("Several skus match $matchfield value : ".$item["matchfield"],"error");
  69. return false;
  70. }
  71. else
  72. {
  73. $item["sku"]=$result["sku"];
  74. }
  75. $n++;
  76. }
  77. //if no item found, warning & skip
  78. if($n==0)
  79. {
  80. $this->log("No sku found matching $matchfield value : ".$item["matchfield"],"warning");
  81. return false;
  82. }
  83. //found a single sku ! item sku is in place, continue with processor chain
  84. return true;
  85. }
  86. static public function getCategory()
  87. {
  88. return "Input Data Preprocessing";
  89. }
  90. }