public function actionIndex()
 {
     exit;
     // prepare logger to dump logs every time one comes in
     Yii::getLogger()->autoFlush = 1;
     Yii::getLogger()->autoDump = true;
     function getNewFileName($fileName)
     {
         $ext = pathinfo($fileName, PATHINFO_EXTENSION);
         $oldName = pathinfo($fileName, PATHINFO_FILENAME);
         return md5(uniqid($fileName)) . '.' . $ext;
     }
     function getmicrotime()
     {
         list($usec, $sec) = explode(" ", microtime());
         return (double) $usec + (double) $sec;
     }
     function logParse($message)
     {
         Yii::log($message, CLogger::LEVEL_TRACE, 'parse');
         Yii::getLogger()->flush(true);
     }
     logParse("Begin");
     $begin = getmicrotime();
     $regexpArray = array('p' => '~^p\\..+~iU', 'pl' => '~^pl\\..+~iU', 'plx' => '~^plx\\..+~iU');
     $titlesArray = array('p' => '', 'pl' => 'Плитка из камня', 'plx' => 'Плитка из камня с подсветкой');
     $parsePath = Yii::app()->uploadManager->getBasePath() . DIRECTORY_SEPARATOR . $this->module->uploadPath . DIRECTORY_SEPARATOR . 'parse_tile';
     $seriesList = glob($parsePath . DIRECTORY_SEPARATOR . '*', GLOB_BRACE | GLOB_ONLYDIR);
     $stones = array();
     $imagesFiles = array();
     // Обработка серий
     foreach ($seriesList as $skey => $seriesPath) {
         //if($skey > 1) break;
         $seriesAlias = basename($seriesPath);
         if ($seriesAlias == '.' || $seriesAlias == '..') {
             continue;
         }
         $series = Series::model()->findByAlias($seriesAlias);
         if (is_null($series)) {
             logParse("Серия " . $seriesAlias . " не найдена");
             echo "Серия " . $seriesAlias . " не найдена<br />";
             exit;
         }
         echo '<h1>' . $series->title . '</h1>';
         logParse("Series " . $series->title);
         $stonesList = glob($seriesPath . DIRECTORY_SEPARATOR . '*', GLOB_BRACE | GLOB_ONLYDIR);
         $stonesCounter = 0;
         $stoneImagesCounter = 0;
         $sort = 0;
         foreach ($stonesList as $stonePath) {
             $stoneTitle = basename($stonePath);
             if ($stoneTitle == '.' || $stoneTitle == '..') {
                 continue;
             }
             //echo '<h3>' . $stoneTitle . '</h3>';
             $stonesCounter++;
             $sort += 100;
             $stone = new Stone();
             $stone->type_id = 2;
             $stone->series_id = $series->id;
             $stone->title = $stoneTitle;
             $stone->metatitle = $stone->keywords = $stone->description = $stone->title;
             // Генерация алиаса
             $alias = \yupe\helpers\YText::translit($stone->title) . '-tile';
             if ($stone->findByAlias($alias) !== null) {
                 $alias = $alias . '-' . mt_rand(10000, 99999);
             }
             $stone->alias = $alias;
             $stone->sort = $sort;
             $imagesList = array();
             foreach ($regexpArray as $key => $regexp) {
                 foreach (CFileHelper::findFiles($stonePath) as $file) {
                     //echo $file . '<br />';
                     if (preg_match($regexp, basename($file))) {
                         $imagesList[$key] = $file;
                     }
                 }
             }
             $stoneImages = array();
             foreach ($imagesList as $key => $item) {
                 $itemName = basename($item);
                 $newName = getNewFileName($item);
                 $imagesFiles[] = array('fullPath' => $item, 'name' => $itemName, 'newName' => $newName);
                 if (preg_match($regexpArray['p'], $itemName)) {
                     $stone->image = $newName;
                     //echo $itemName . '<br />';
                 } else {
                     $image = new StoneImage();
                     $image->title = $titlesArray[$key];
                     $image->image = $newName;
                     $stoneImages[] = $image;
                 }
                 //echo $itemName . '<br />';
                 $stoneImagesCounter++;
             }
             $stone->images = $stoneImages;
             $stones[] = $stone;
         }
         echo 'Камней: ' . $stonesCounter . '<br />';
         echo 'Изображений: ' . $stoneImagesCounter . '<br />';
         echo '<hr>';
     }
     // Сохраняем камни и изображения в БД
     foreach ($stones as $stone) {
         logParse($stone->title);
         if ($stone->save(false)) {
             $imagesCounter = 0;
             foreach ($stone->images as $image) {
                 $imagesCounter += 100;
                 $image->item_id = $stone->id;
                 $image->sort = $imagesCounter;
                 if (!$image->save(false)) {
                     logParse($image->image . ' - error');
                     echo $image->image . ' - error<br />';
                 }
             }
             logParse('success');
         } else {
             logParse('error');
             //echo $stone->title . ' - error<br />';
             //var_dump($stone->getErrors());
         }
     }
     // Копируем изображения
     foreach ($imagesFiles as $image) {
         copy($image['fullPath'], Yii::app()->uploadManager->getBasePath() . DIRECTORY_SEPARATOR . $this->module->getStonesUploadPath() . DIRECTORY_SEPARATOR . $image['newName']);
     }
     $end = getmicrotime();
     $time = $end - $begin;
     echo "Время выполнения скрипта: " . $time . "с.";
     logParse("Время выполнения скрипта: " . $time . "с.");
     echo '<hr>';
 }
 public function actionIndex()
 {
     // prepare logger to dump logs every time one comes in
     Yii::getLogger()->autoFlush = 1;
     Yii::getLogger()->autoDump = true;
     function getNewFileName($fileName)
     {
         $ext = pathinfo($fileName, PATHINFO_EXTENSION);
         $oldName = pathinfo($fileName, PATHINFO_FILENAME);
         return md5(uniqid($fileName)) . '.' . $ext;
     }
     function getmicrotime()
     {
         list($usec, $sec) = explode(" ", microtime());
         return (double) $usec + (double) $sec;
     }
     function logParse($message)
     {
         Yii::log($message, CLogger::LEVEL_TRACE, 'parse');
         Yii::getLogger()->flush(true);
     }
     logParse("Begin");
     $begin = getmicrotime();
     /*
             $regexpArray = array(
        'mm'    => '~^mm[^x]*\..+~iU',
        'fr1'   => '~^fr[\d]{0,2}[^x]*\..+~iU',
        'sl'    => '~^sl[\d]{0,2}[^x]*\..+~iU',
        'b1'    => '~^b1[^x]*\..+~iU',
        'b2'    => '~^b2[^x]*\..+~iU',
        'frx'   => '~^fr[\d]{0,2}x.*\..+~iU',
        'slx'   => '~^slx.*\..+~iU',
        'b1x'   => '~^b1x.*\..+~iU',
        'b2x'   => '~^b2x.*\..+~iU',
             );
     *
     */
     $regexpArray = array('mm' => '~^mm\\..+~iU', 'fr' => '~^fr\\..+~iU', 'fr2' => '~^fr2\\..+~iU', 'sl' => '~^sl\\..+~iU', 'b1' => '~^b1\\..+~iU', 'b2' => '~^b2\\..+~iU', 'frx' => '~^frx.*\\..+~iU', 'slx' => '~^slx.*\\..+~iU', 'b1x' => '~^b1x.*\\..+~iU', 'b2x' => '~^b2x.*\\..+~iU');
     $titlesArray = array('mm' => '', 'fr' => 'Фрагмент', 'fr2' => 'Фрагмент 2', 'sl' => 'Слеб', 'b1' => 'Bookmatch 1', 'b2' => 'Bookmatch 2', 'frx' => 'Фрагмент с подсветкой', 'slx' => 'Слеб с подсветкой', 'b1x' => 'Bookmatch 1 с подсветкой', 'b2x' => 'Bookmatch 2 с подсветкой');
     $parsePath = Yii::app()->uploadManager->getBasePath() . DIRECTORY_SEPARATOR . $this->module->geParsePath();
     $seriesList = glob($parsePath . DIRECTORY_SEPARATOR . '*', GLOB_BRACE | GLOB_ONLYDIR);
     $stones = array();
     $imagesFiles = array();
     // Обработка серий
     foreach ($seriesList as $seriesPath) {
         $seriesAlias = basename($seriesPath);
         if ($seriesAlias == '.' || $seriesAlias == '..') {
             continue;
         }
         $series = Series::model()->findByAlias($seriesAlias);
         if (is_null($series)) {
             logParse("Серия " . $seriesAlias . " не найдена");
             echo "Серия " . $seriesAlias . " не найдена<br />";
             exit;
         }
         echo '<h1>' . $series->title . '</h1>';
         logParse("Series " . $series->title);
         $stonesList = glob($seriesPath . DIRECTORY_SEPARATOR . '*', GLOB_BRACE | GLOB_ONLYDIR);
         $stonesCounter = 0;
         $stoneImagesCounter = 0;
         $sort = 0;
         foreach ($stonesList as $stonePath) {
             $stoneTitle = basename($stonePath);
             if ($stoneTitle == '.' || $stoneTitle == '..') {
                 continue;
             }
             //echo '<h3>' . $stoneTitle . '</h3>';
             $stonesCounter++;
             $sort += 100;
             $stone = new Stone();
             $stone->series_id = $series->id;
             $stone->title = $stoneTitle;
             $stone->metatitle = $stone->keywords = $stone->description = $stone->title;
             $stone->generateAlias();
             $stone->sort = $sort;
             $imagesList = array();
             foreach ($regexpArray as $key => $regexp) {
                 foreach (CFileHelper::findFiles($stonePath) as $file) {
                     if (preg_match($regexp, basename($file))) {
                         $imagesList[$key] = $file;
                     }
                 }
             }
             $stoneImages = array();
             foreach ($imagesList as $key => $item) {
                 $itemName = basename($item);
                 $newName = getNewFileName($item);
                 $imagesFiles[] = array('fullPath' => $item, 'name' => $itemName, 'newName' => $newName);
                 if (preg_match($regexpArray['mm'], $itemName)) {
                     $stone->image = $newName;
                     //echo $itemName . '<br />';
                 } else {
                     $image = new StoneImage();
                     $image->title = $titlesArray[$key];
                     $image->image = $newName;
                     $stoneImages[] = $image;
                 }
                 //echo $itemName . '<br />';
                 $stoneImagesCounter++;
             }
             $stone->images = $stoneImages;
             $stones[] = $stone;
         }
         echo 'Камней: ' . $stonesCounter . '<br />';
         echo 'Изображений: ' . $stoneImagesCounter . '<br />';
         echo '<hr>';
     }
     // Сохраняем камни и изображения в БД
     foreach ($stones as $stone) {
         logParse($stone->title);
         if ($stone->save(false)) {
             $imagesCounter = 0;
             foreach ($stone->images as $image) {
                 $imagesCounter += 100;
                 $image->item_id = $stone->id;
                 $image->sort = $imagesCounter;
                 if (!$image->save(false)) {
                     logParse($image->image . ' - error');
                     echo $image->image . ' - error<br />';
                 }
             }
             logParse('success');
         } else {
             logParse('error');
             //echo $stone->title . ' - error<br />';
             //var_dump($stone->getErrors());
         }
     }
     // Копируем изображения
     foreach ($imagesFiles as $image) {
         copy($image['fullPath'], Yii::app()->uploadManager->getBasePath() . DIRECTORY_SEPARATOR . $this->module->getStonesUploadPath() . DIRECTORY_SEPARATOR . $image['newName']);
     }
     $end = getmicrotime();
     $time = $end - $begin;
     echo "Время выполнения скрипта: " . $time . "с.";
     logParse("Время выполнения скрипта: " . $time . "с.");
     echo '<hr>';
 }