PageRenderTime 20ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/importmag/plugins/base/datasources/csv/magmi_csvdatasource.php

https://gitlab.com/inglobe/mgt-clemente-css
PHP | 136 lines | 27 code | 5 blank | 104 comment | 1 complexity | fd05dc51b1e778255abf27fef58b1352 MD5 | raw file
  1. <?php
  2. require_once("magmi_csvreader.php");
  3. require_once("fshelper.php");
  4. class Magmi_CSVDataSource extends Magmi_Datasource
  5. {
  6. protected $_csvreader;
  7. public function initialize($params)
  8. {
  9. $this->_csvreader = new Magmi_CSVReader();
  10. //csv reader is now independent from plugin
  11. $this->_csvreader->initialize($params);
  12. }
  13. public function getAbsPath($path)
  14. {
  15. return abspath($path, $this->getScanDir());
  16. }
  17. public function getScanDir($resolve = true)
  18. {
  19. $scandir = $this->getParam("CSV:basedir", "var/import");
  20. if (!isabspath($scandir)) {
  21. $scandir = abspath($scandir, Magmi_Config::getInstance()->getMagentoDir(), $resolve);
  22. }
  23. return $scandir;
  24. }
  25. public function getCSVList()
  26. {
  27. $scandir = $this->getScanDir();
  28. $files = glob("$scandir/*.csv");
  29. return $files;
  30. }
  31. public function getPluginParams($params)
  32. {
  33. $pp = array();
  34. foreach ($params as $k => $v) {
  35. if (preg_match("/^CSV:.*$/", $k)) {
  36. $pp[$k] = $v;
  37. }
  38. }
  39. return $pp;
  40. }
  41. public function getPluginInfo()
  42. {
  43. return array("name"=>"CSV Datasource","author"=>"Dweeves","version"=>"1.3.1");
  44. }
  45. public function getRecordsCount()
  46. {
  47. return $this->_csvreader->getLinesCount();
  48. }
  49. public function getAttributeList()
  50. {
  51. }
  52. public function getRemoteFile($url)
  53. {
  54. $fg = RemoteFileGetterFactory::getFGInstance();
  55. if ($this->getParam("CSV:remoteauth", false) == true) {
  56. $user = $this->getParam("CSV:remoteuser");
  57. $pass = $this->getParam("CSV:remotepass");
  58. $fg->setCredentials($user, $pass);
  59. }
  60. $cookies = $this->getParam("CSV:remotecookie");
  61. if ($cookies) {
  62. $fg->setCookie($cookies);
  63. }
  64. $this->log("Fetching CSV: $url", "startup");
  65. // output filename (current dir+remote filename)
  66. $csvdldir = dirname(__FILE__) . "/downloads";
  67. if (!file_exists($csvdldir)) {
  68. @mkdir($csvdldir);
  69. @chmod($csvdldir, Magmi_Config::getInstance()->getDirMask());
  70. }
  71. $outname = $csvdldir . "/" . basename($url);
  72. $ext = substr(strrchr($outname, '.'), 1);
  73. if ($ext != "txt" && $ext != "csv") {
  74. $outname = $outname . ".csv";
  75. }
  76. // open file for writing
  77. if (file_exists($outname)) {
  78. if ($this->getParam("CSV:forcedl", false) == true) {
  79. unlink($outname);
  80. } else {
  81. return $outname;
  82. }
  83. }
  84. $fg->copyRemoteFile($url, $outname);
  85. // return the csv filename
  86. return $outname;
  87. }
  88. public function beforeImport()
  89. {
  90. if ($this->getParam("CSV:importmode", "local") == "remote") {
  91. $url = $this->getParam("CSV:remoteurl", "");
  92. $outname = $this->getRemoteFile($url);
  93. $this->setParam("CSV:filename", $outname);
  94. $this->_csvreader->initialize($this->_params);
  95. }
  96. return $this->_csvreader->checkCSV();
  97. }
  98. public function afterImport()
  99. {
  100. }
  101. public function startImport()
  102. {
  103. $this->_csvreader->openCSV();
  104. }
  105. public function getColumnNames($prescan = false)
  106. {
  107. return $this->_csvreader->getColumnNames($prescan);
  108. }
  109. public function endImport()
  110. {
  111. $this->_csvreader->closeCSV();
  112. }
  113. public function getNextRecord()
  114. {
  115. return $this->_csvreader->getNextRecord();
  116. }
  117. }