public function loadModel($id)
 {
     if (($model = RealtyGalleryImage::model()->findByPk($id)) === null) {
         throw new CHttpException(404, Yii::t('RealtyModule.realty', 'Page was not found!'));
     }
     return $model;
 }
 public function actionIndex()
 {
     exit;
     $prefix = $_GET['prefix'];
     // 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 logParse($message)
     {
         Yii::log($message, CLogger::LEVEL_TRACE, 'parse');
         Yii::getLogger()->flush(true);
     }
     //logParse("Begin");
     $begin = getmicrotime();
     // Начало
     $parsePath = Yii::app()->uploadManager->getBasePath() . DIRECTORY_SEPARATOR . $this->module->uploadPath . DIRECTORY_SEPARATOR . 'parse';
     $csvFile = $parsePath . DIRECTORY_SEPARATOR . "realty_{$prefix}.csv";
     $csvData = file_get_contents($csvFile);
     $csvData = mb_convert_encoding($csvData, 'utf-8', 'windows-1251');
     $csvData = explode("\r\n", $csvData);
     //var_dump($csvData);
     $imagesFiles = [];
     $galleries = [];
     foreach ($csvData as $key => $row) {
         if ($key == 0) {
             continue;
         }
         //if($key >= 50)
         //    break;
         $fields = explode(";", $row);
         // Номер
         $num = (int) $fields[0];
         $imgFolderNum = $fields[1];
         if (empty($num)) {
             continue;
         }
         // Изображения
         if ($imgFolderNum != '' && $imgFolderNum != '-') {
             $imagesPath = $parsePath . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . $imgFolderNum;
             if (file_exists($imagesPath)) {
                 if (key_exists($imgFolderNum, $galleries)) {
                     continue;
                 }
                 $gallery = new RealtyGallery();
                 $gallery->title = trim($fields[6]);
                 $gallery->xls_id = $imgFolderNum;
                 $galleries[$imgFolderNum] = $gallery;
                 $imagesList = FileHelper::findFilesNatSort($imagesPath);
                 $itemImages = [];
                 foreach ($imagesList as $key => $item) {
                     //echo $item . '<br>';
                     $itemName = basename($item);
                     $newName = getNewFileName($item);
                     $imagesFiles[] = array('fullPath' => $item, 'name' => $itemName, 'newName' => $newName);
                     $itemImages[] = $newName;
                 }
                 $gallery->images = $itemImages;
             }
         }
     }
     echo 'Галерей: ' . count($galleries) . '<br />';
     echo 'Изображений: ' . count($imagesFiles) . '<br />';
     echo '<hr>';
     /*
             foreach($imagesFiles as $key => $image) {
        echo ++$key . ' - ' . $image['fullPath'] . '<br />';
             }
     *
     */
     //exit();
     // Сохраняем массив с изображениями в файл
     file_put_contents($parsePath . DIRECTORY_SEPARATOR . "images_{$prefix}.txt", json_encode($imagesFiles));
     unset($imagesFiles);
     // Сохраняем галереи и изображения в БД
     foreach ($galleries as $gallery) {
         //logParse($realtyItem->title);
         if ($gallery->save()) {
             // Изображения
             $imagesCounter = 0;
             foreach ($gallery->images as $imageName) {
                 $image = new RealtyGalleryImage();
                 $image->image = $imageName;
                 $imagesCounter += 100;
                 $image->item_id = $gallery->id;
                 $image->sort = $imagesCounter;
                 if (!$image->save(false)) {
                     //logParse($image->image . ' - error');
                     echo $image->image . ' - error<br />';
                 }
                 unset($image);
             }
             //logParse('success');
         } else {
             //logParse('error');
             echo $gallery->title . ' - error<br />';
             var_dump($gallery->getErrors());
         }
     }
     $end = getmicrotime();
     $time = $end - $begin;
     echo "Время выполнения скрипта: " . $time . "с.";
     //logParse("Время выполнения скрипта: " . $time . "с.");
     echo '<hr>';
 }