Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. <?php
  2.  
  3. if ( ! defined('DIAFAN'))
  4. {
  5. $path = __FILE__;
  6. while(! file_exists($path.'/includes/404.php'))
  7. {
  8. $parent = dirname($path);
  9. if($parent == $path) exit;
  10. $path = $parent;
  11. }
  12. include $path.'/includes/404.php';
  13. }
  14.  
  15. /**
  16. * Shop_export
  17. */
  18. class Feedback_export extends Diafan
  19. {
  20. /**
  21. * @var array конфигурация текущего экспорта
  22. */
  23. private $config;
  24.  
  25. /**
  26. * @var array название полей списка
  27. */
  28. private $select_values;
  29.  
  30. /**
  31. * @var array поля, заданные для текущего экспорта
  32. */
  33. private $fields;
  34.  
  35. /**
  36. * Инициирует экспорт
  37. *
  38. * @return void
  39. */
  40. public function init()
  41. {
  42.  
  43. if(! $this->diafan->_users->roles("init", "shop/importexport", array(), 'admin'))
  44. {
  45. Custom::inc('includes/404.php');
  46. }
  47.  
  48. if (empty($_GET["site_id"]))
  49. {
  50. Custom::inc('includes/404.php');
  51. }
  52.  
  53. $feedback = DB::query_fetch_all("SELECT id FROM {feedback} WHERE site_id = %d AND trash='0'",(int)$_GET["site_id"]);
  54.  
  55. $params = DB::query_fetch_all("SELECT id,[name],type FROM {feedback_param} WHERE site_id = 32 AND trash='0'",(int)$_GET["site_id"]);
  56.  
  57. $text = '';
  58.  
  59. $list = array();
  60. if(!empty($params))
  61. {
  62. foreach($params as $field)
  63. {
  64. $list[] = $field["name"];
  65. }
  66. $text .= $this->putcsv($list);
  67.  
  68. $list = array();
  69.  
  70.  
  71. if (!empty($feedback))
  72. {
  73. foreach ($feedback as $key => $feed) {
  74. $list = array();
  75. foreach ($params as $param) {
  76. switch ($param["type"]) {
  77. case 'select':
  78. $value = DB::query_result("SELECT [name] FROM {feedback_param_select} WHERE id IN (SELECT value FROM {feedback_param_element} WHERE param_id=%d AND element_id=%d)",$param["id"],$feed["id"]);
  79. break;
  80. default:
  81. $value = DB::query_result("SELECT value FROM {feedback_param_element} WHERE param_id=%d AND element_id=%d",$param["id"],$feed["id"]);
  82. break;
  83. }
  84. $list[] = $value;
  85. }
  86. $text .= $this->putcsv($list);
  87. }
  88.  
  89. }
  90. }
  91.  
  92. $name = preg_replace('/[^a-z_ ]+/', '', str_replace(array(' ', '-'), '_', substr(strtolower($this->diafan->translit(TIT1)), 0, 50)));
  93. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  94. header('Cache-Control: max-age=86400');
  95. header("Content-type: text/plain");
  96. header("Content-Disposition: attachment; filename=shop_export_".$name.".csv");
  97. header('Content-transfer-encoding: binary');
  98. header("Connection: close");
  99. echo utf::to_windows1251($text);
  100. exit;
  101. }
  102.  
  103.  
  104. /**
  105. * Форматирует строку в виде CSV
  106. *
  107. * @param array $list исходные данные
  108. * @param strign $q символ ограничителя поля
  109. */
  110. private function putcsv($list, $q = '"')
  111. {
  112. $line = "";
  113. foreach ($list as $i => $field)
  114. {
  115. // remove any windows new lines,
  116. // as they interfere with the parsing at the other end
  117. $field = str_replace("\r\n", "\n", $field);
  118. // if a deliminator char, a double quote char or a newline
  119. // are in the field, add quotes
  120. if(preg_match("/[;"."$q\n\r]/", $field))
  121. {
  122. $field = $q.str_replace($q, $q.$q, $field).$q;
  123. }
  124. $line .= $field;
  125. if($i != count($list) - 1)
  126. {
  127. $line .= ';';
  128. }
  129. }
  130. $line .= "\n";
  131. return $line;
  132. }
  133. }
  134.  
  135. $shop_export = new Feedback_export($this->diafan);
  136. $shop_export->init();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement