Version: current

      1 <?php
      2 // Контроллер обработки запросов импорта/экспорта заказов из 1С
      3 class Orders1CExchange_Controller extends Import1C_Controller {
      4
      5     static $zip = 'no';
      6
      7     function init($request) {
      8         $log = Import1C_Controller::get_log('sale');
      9         $log->addStage('init', 'Согласование параметров обмена');
     10         $log->addLog(sprintf("import mode: zip=%s file_limit=%d", self::$zip, Import1C_Controller::$upload_limit));
     11
     12         printf("zip=%s\nfile_limit=%d\n", self::$zip, Import1C_Controller::$upload_limit);
     13         return;
     14     }
     15
     16     function query($request) {
     17         $exchangerClass = Orders1CExchange_SiteConfig::$exchanger_class;
     18
     19         $log = Import1C_Controller::get_log('sale');
     20         if (!$log) {
     21             echo "failure\n";
     22             echo "wrong exchange state (sale:query), need restart\n";
     23             return;
     24         }
     25
     26         $log->addStage('query');
     27         $exportClass = new $exchangerClass();
     28         $xml = $exportClass->exportOrders($log);
     29         if ($xml) {
     30             echo $xml;
     31             Import1C_Controller::debug_log("export success");
     32         } else {
     33             echo "failure\n";
     34             Import1C_Controller::debug_log("export empty");
     35         }
     36     }
     37
     38     function success($request) {
     39         $exchangerClass = Orders1CExchange_SiteConfig::$exchanger_class;
     40
     41         $log = Import1C_Controller::get_log('sale');
     42         if (!$log) {
     43             echo "failure\n";
     44             echo "wrong exchange state (sale:success), need restart\n";
     45             return;
     46         }
     47
     48         $log->addStage('success');
     49         $exportClass = new $exchangerClass();
     50         $exportClass->setOrdersExported($log);
     51         $log->addStatus('ok', _t('ProductCatalogImport.ImportInterrupted', 'Импорт прерван'));
     52
     53         // FIXME проверить на ошибки импорта
     54         echo "success\n";
     55         Import1C_Controller::debug_log("mark success");
     56     }
     57
     58     function file($request) {
     59         $exchangerClass = Orders1CExchange_SiteConfig::$exchanger_class;
     60
     61         $log = Import1C_Controller::get_log('sale');
     62         if (!$log) {
     63             echo "failure\n";
     64             echo "wrong exchange state (sale:file), need restart\n";
     65             return;
     66         }
     67
     68         $log->addStage('file');
     69         if (!$filename = basename($request->requestVar('filename'))) {
     70             echo "failure\n";
     71             Import1C_Controller::debug_log('error: no filename');
     72             return;
     73         }
     74
     75         ini_set('max_execution_time', 1800);
     76         /*
     77         $dirname = TEMP_FOLDER . '/1c_exchange';
     78         if (!file_exists($dirname)) {
     79             mkdir($dirname); // FIXME проверить создало или нет
     80             chmod($dirname, 0775);
     81         }
     82         */
     83
     84         // скидываем файл заказов в папку задачи
     85         $taskDir = '_tasks/' . $log->ID;
     86         $taskFolder = Base1CExchanger::absolute_import_path($taskDir);
     87         if (!is_dir($taskFolder)) {
     88             if (!mkdir($taskFolder, Filesystem::$folder_create_mask, true)) {
     89                 $log->addLog("Ошибка создания папки: {$taskFolder}", 'error');
     90                 $log->addStatus('error', "Ошибка создания папки очереди задач");
     91                 echo "failure\n";
     92                 echo "Can't create import queue folder\n";
     93             }
     94         }
     95
     96         $taskName = preg_replace('!/!', '-', $filename);
     97         $filePath = $taskFolder . '/' . $taskName;
     98         file_put_contents($filePath, $request->getBody());
     99         if (file_exists($filePath)) {
    100             $exportClass = new $exchangerClass();
    101             $exportClass->importOrders($filePath, $log);
    102             // FIXME проверить на ошибки импорта
    103             //unlink($filePath);
    104             echo "success\n";
    105             Import1C_Controller::debug_log("import success");
    106         }
    107         else {
    108             echo "failure\n";
    109             Import1C_Controller::debug_log("upload failure:" . $filePath);
    110         }
    111     }
    112 }
    113

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -