Version: current

      1 <?php
      2 /**
      3  * Контроллер экспорта данных в Я.Маркет
      4  * Готовит данные, доступен по адресу http://адрес-сайта/export_yml.xml
      5  *
      6  * @author dvp, menedem
      7  */
      8 class YMLExporter extends Controller {
      9
     10     static $cache_expire = 86400;
     11
     12     static function local_exportfile_path() {
     13         return ASSETS_PATH . '/export_yml.xml';
     14     }
     15
     16     static function clear_cache() {
     17         $file = self::local_exportfile_path();
     18         if (@file_exists($file))
     19             unlink($file);
     20     }
     21
     22     function index($request) {
     23         Versioned::reading_stage('Live');
     24
     25         $cache_path = self::local_exportfile_path();
     26         if ($request->requestVar('force')) {
     27             self::clear_cache();
     28         }
     29
     30         if (!Director::is_cli()) {
     31             if (@file_exists($cache_path) && (@filemtime($cache_path) + self::$cache_expire > time())) {
     32                 Controller::curr()->getResponse()->addHeader("Content-Type", "text/xml");
     33                 readfile($cache_path);
     34                 return;
     35             }
     36         }
     37
     38         increase_time_limit_to(1000);
     39         increase_memory_limit_to('1000M');
     40         $markStart = time();
     41
     42         if (is_file($cache_path)) {
     43             rename($cache_path, $cache_path . '_' . date('Y_m_d_H_i_s'));
     44         }
     45         $sc = SiteConfig::current_site_config();
     46         // выводим шапку ХML-ки
     47
     48         $html = $this->customise(array(
     49             'Date' => date('Y-m-d H:m'),
     50             'Name' => $sc->YMLName,
     51             'Company' => $sc->YMLCompany,
     52             'URL' => $sc->YMLHref,
     53             'Currency' => $sc->YMLCurrency,
     54             'Rate' => $sc->YMLRate,
     55         ))->renderWith('YMarketFormat');
     56         file_put_contents($cache_path, $html);
     57
     58         $html = '';
     59         // выводим поле шапки список каталогов
     60         $categories = DB::Query('SELECT ID, ClassName, ParentID, Title, MenuTitle FROM "SiteTree_Live" WHERE (ClassName = \'Catalog\' OR ClassName = \'StartCatalog\')');
     61         if ($categories->numRecords()) {
     62             $html .= '<categories>';
     63             foreach($categories as $category) {
     64                 $t = new ArrayData($category);
     65                 $t->Title = Convert::raw2xml(($t->Title ? $t->Title : $t->MenuTitle));
     66                 $html .= $t->renderWith('CatalogLine');
     67             }
     68             $html .= '</categories>';
     69         }
     70         file_put_contents($cache_path, $html, FILE_APPEND);
     71         unset($categories);
     72
     73         // выводим список товаров
     74         $productWhere = $this->getWhere();
     75         $f = true;
     76         $start = 0;
     77         $count = 1000;
     78         $totCount = 0;
     79         file_put_contents($cache_path, '<offers>', FILE_APPEND);
     80         while ($f) {
     81             $products = DataObject::get('Product', $productWhere, 'ID', '', "{$start},{$count}");
     82             if (!$products) {
     83                 $f = false;
     84             } else {
     85                 $html = '';
     86                 $pCount = $products->Count();
     87                 foreach($products as $product) {
     88                     $product->ProductCurrency = $sc->YMLCurrency;
     89                     $html .= $product->renderWith('ProductLine');
     90                     $product->destroy();
     91                     $totCount++;
     92                 }
     93                 file_put_contents($cache_path, $html, FILE_APPEND);
     94                 $start += $pCount;
     95                 unset($products);
     96             }
     97         }
     98         file_put_contents($cache_path, '</offers></shop></yml_catalog>', FILE_APPEND);
     99         if (Director::is_cli()) {
    100             $markEnd = time();
    101             echo
    102                 'start: ' . date('Y-m-d H:i:s', $markStart) . "\n"
    103                 . '  end: ' . date('Y-m-d H:i:s', $markEnd) . "\n"
    104                 . 'duration: ' . ($markEnd - $markStart) . "\n"
    105                 . Director::absoluteURL(Director::makeRelative($cache_path)) ."\n"
    106             ;
    107         }
    108         else {
    109             Controller::curr()->getResponse()->addHeader("Content-Type", "text/xml");
    110             readfile($cache_path);
    111         }
    112
    113     }
    114
    115
    116     function getWhere() {
    117         $sc = SiteConfig::current_site_config();
    118         $productWhere = ($sc->YMLAvailableOnly) ? 'Available = 1' : '';
    119         $this->extend('updateYMProductWhere', $productWhere);
    120         return $productWhere;
    121     }
    122 }
    123

[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. -