Advertisement
Guest User

Untitled

a guest
Nov 29th, 2015
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.32 KB | None | 0 0
  1. <?php
  2.  
  3. define('MODX_API_MODE', true);
  4. require_once dirname(dirname(dirname(dirname(__FILE__)))) . '/config/config.inc.php';
  5. require_once MODX_BASE_PATH . 'index.php';
  6.  
  7. if (XPDO_CLI_MODE) {
  8. $file = @$argv[1];
  9. $fields = @$argv[2];
  10. $update = (bool) !empty($argv[3]);
  11. $key = @$argv[4];
  12. $is_debug = (bool) !empty($argv[5]);
  13. $delimeter = @$argv[6];
  14. }
  15. else {
  16. $file = @$_REQUEST['file'];
  17. $fields = @$_REQUEST['fields'];
  18. $update = (bool) !empty($_REQUEST['update']);
  19. $key = @$_REQUEST['key'];
  20. $is_debug = (bool) !empty($_REQUEST['debug']);
  21. $delimeter = @$_REQUEST['delimeter'];
  22. }
  23.  
  24. // Load main services
  25. $modx->setLogTarget(XPDO_CLI_MODE ? 'ECHO' : 'HTML');
  26. $modx->setLogLevel($is_debug ? modX::LOG_LEVEL_INFO : modX::LOG_LEVEL_ERROR);
  27. $modx->getService('error','error.modError');
  28. $modx->lexicon->load('minishop2:default');
  29. $modx->lexicon->load('minishop2:manager');
  30.  
  31. // Time limit
  32. set_time_limit(600);
  33. $tmp = 'Trying to set time limit = 600 sec: ';
  34. $tmp .= ini_get('max_execution_time') == 600 ? 'done' : 'error';
  35. $modx->log(modX::LOG_LEVEL_INFO, $tmp);
  36.  
  37. // Check required options
  38. if (empty($fields)) {
  39. $modx->log(modX::LOG_LEVEL_ERROR, 'You must specify the parameter "fields". It needed for parse of your file.');
  40. exit;
  41. }
  42. if (empty($key)) {
  43. $modx->log(modX::LOG_LEVEL_ERROR, 'You must specify the parameter "key". It needed for check for duplicates.');
  44. exit;
  45. }
  46. $keys = array_map('trim', explode(',', strtolower($fields)));
  47. $tv_enabled = false;
  48. foreach ($keys as $v) {
  49. if (preg_match('/^tv(\d)$/', $v)) {
  50. $tv_enabled = true;
  51. break;
  52. }
  53. }
  54. if (empty($delimeter)) {$delimeter = ';';}
  55.  
  56. // Check file
  57. if (empty($file)) {
  58. $error = 'You must specify an file in the ';
  59. $error .= XPDO_CLI_MODE ? 'first parameter of console call' : '$_GET["file"] parameter';
  60. $error .= '!';
  61. $modx->log(modX::LOG_LEVEL_ERROR, $error);
  62. exit;
  63. }
  64. elseif (!preg_match('/\.csv$/i', $file)) {
  65. $modx->log(modX::LOG_LEVEL_ERROR, 'Wrong file extension. File must be an *.csv.');
  66. exit;
  67. }
  68.  
  69. $file = str_replace('//', '/', MODX_BASE_PATH . $file);
  70. if (!file_exists($file)) {
  71. $modx->log(modX::LOG_LEVEL_ERROR, 'File not found at '.$file.'.');
  72. exit;
  73. }
  74.  
  75. // Import!
  76. $handle = fopen($file, "r");
  77. $rows = $created = $updated = 0;
  78. while (($csv = fgetcsv($handle, 0, $delimeter)) !== false) {
  79. $rows ++;
  80. $data = $gallery = array();
  81. $modx->error->reset();
  82. $modx->log(modX::LOG_LEVEL_INFO, "Raw data for import: \n".print_r($csv,1));
  83. foreach ($keys as $k => $v) {
  84. if (!isset($csv[$k])) {
  85. exit('Field "' . $v . '" not exists in file. Please fix import file or parameter "fields".');
  86. }
  87. if ($v == 'gallery') {
  88. $gallery[] = $csv[$k];
  89. }
  90. elseif (isset($data[$v]) && !is_array($data[$v])) {
  91. $data[$v] = array($data[$v], $csv[$k]);
  92. }
  93. elseif (isset($data[$v]) && is_array($data[$v])) {
  94. $data[$v][] = $csv[$k];
  95. }
  96. else {
  97. $data[$v] = $csv[$k];
  98. }
  99. }
  100. $is_product = false;
  101.  
  102. // Set default values
  103. if (empty($data['class_key'])) {$data['class_key'] = 'msProduct';}
  104. if (empty($data['context_key'])) {
  105. if (isset($data['parent']) && $parent = $modx->getObject('modResource', $data['parent'])) {
  106. $data['context_key'] = $parent->get('context_key');
  107. }
  108. elseif (isset($modx->resource) && isset($modx->context)) {
  109. $data['context_key'] = $modx->context->key;
  110. }
  111. else {
  112. $data['context_key'] = 'web';
  113. }
  114. }
  115. $data['tvs'] = $tv_enabled;
  116. $modx->log(modX::LOG_LEVEL_INFO, "Array with importing data: \n" . print_r($data, 1));
  117.  
  118. // Duplicate check
  119. $q = $modx->newQuery($data['class_key']);
  120. $q->select($data['class_key'].'.id');
  121. if (strtolower($data['class_key']) == 'msproduct') {
  122. $q->innerJoin('msProductData', 'Data', $data['class_key'].'.id = Data.id');
  123. $is_product = true;
  124. }
  125. $tmp = $modx->getFields($data['class_key']);
  126. if (isset($tmp[$key])) {
  127. $q->where(array($key => $data[$key]));
  128. }
  129. elseif ($is_product) {
  130. $q->where(array('Data.'.$key => $data[$key]));
  131. }
  132. $q->prepare();
  133. $modx->log(modX::LOG_LEVEL_INFO, "SQL query for check for duplicate: \n" . $q->toSql());
  134.  
  135. /** @var modResource $exists */
  136. if ($exists = $modx->getObject($data['class_key'], $q)) {
  137. $modx->log(modX::LOG_LEVEL_INFO, "Key $key = $data[$key] has duplicate.");
  138. if (!$update) {
  139. $modx->log(modX::LOG_LEVEL_ERROR, "Skipping line with $key = \"$data[$key]\" because update is disabled.");
  140. if ($is_debug) {
  141. $modx->log(modX::LOG_LEVEL_INFO, 'You in debug mode, so we process only 1 row. Time: '.number_format(microtime(true) - $modx->startTime, 7) . " s");
  142. exit;
  143. }
  144. else {continue;}
  145. }
  146. else {
  147. $action = 'update';
  148. $data['id'] = $exists->id;
  149. }
  150. }
  151. else {
  152. $action = 'create';
  153. }
  154.  
  155. // Create or update resource
  156. /** @var modProcessorResponse $response */
  157. $response = $modx->runProcessor('resource/'.$action, $data);
  158. if ($response->isError()) {
  159. $modx->log(modX::LOG_LEVEL_ERROR, "Error on $action: \n". print_r($response->getAllErrors(), 1));
  160. }
  161. else {
  162. if ($action == 'update') {$updated ++;}
  163. else {$created ++;}
  164.  
  165. $resource = $response->getObject();
  166. $modx->log(modX::LOG_LEVEL_INFO, "Successful $action: \n". print_r($resource, 1));
  167.  
  168. // Process gallery images, if exists
  169. if (!empty($gallery)) {
  170. $modx->log(modX::LOG_LEVEL_INFO, "Importing images: \n". print_r($gallery, 1));
  171. foreach ($gallery as $v) {
  172. if (empty($v)) {continue;}
  173. $image = str_replace('//', '/', MODX_BASE_PATH . $v);
  174. if (!file_exists($image)) {
  175. $modx->log(modX::LOG_LEVEL_ERROR, "Could not import image \"$v\" to gallery. File \"$image\" not found on server.");
  176. }
  177. else {
  178. $response = $modx->runProcessor('gallery/upload',
  179. array('id' => $resource['id'], 'name' => $v, 'file' => $image),
  180. array('processors_path' => MODX_CORE_PATH.'components/minishop2/processors/mgr/')
  181. );
  182. if ($response->isError()) {
  183. $modx->log(modX::LOG_LEVEL_ERROR, "Error on upload \"$v\": \n". print_r($response->getAllErrors(), 1));
  184. }
  185. else {
  186. $modx->log(modX::LOG_LEVEL_INFO, "Successful upload \"$v\": \n". print_r($response->getObject(), 1));
  187. }
  188. }
  189. }
  190. }
  191. }
  192.  
  193. if ($is_debug) {
  194. $modx->log(modX::LOG_LEVEL_INFO, 'You in debug mode, so we process only 1 row. Time: '.number_format(microtime(true) - $modx->startTime, 7) . " s");
  195. exit;
  196. }
  197. }
  198. fclose($handle);
  199.  
  200. if (!XPDO_CLI_MODE) {echo '<pre>';}
  201. echo "\nImport complete in ".number_format(microtime(true) - $modx->startTime, 7) . " s\n";
  202. echo "\nTotal rows: $rows\n";
  203. echo "Created: $created\n";
  204. echo "Updated: $updated\n";
  205. if (!XPDO_CLI_MODE) {echo '</pre>';}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement