from(Qs_Db::getPair('Admin', 'a'), ['email']); $select->join(Qs_Db::getPair('AdminAcl', 'acl'), '`a`.`id` = `acl`.`adminId`', []); $select->where('`a`.`email` IS NOT NULL'); $select->where('`a`.`type` = ?' , App_Admin_Obj::TYPE_ADMIN); $select->where('`acl`.`aclResourceId` = ?', App_Admin_Acl::ACL_LICENSING_ID); return Qs_Db::getInstance()->fetchCol($select, ['email']); } public static function getTribalAccountEmails($tribeId) { $select = Qs_Db::getSelect(); $select->from(Qs_Db::getPair('Admin', 'a'), ['email']); $select->join(Qs_Db::getPair('TribalAccountAcl', 'acl'), '`a`.`id` = `acl`.`adminId`', []); $select->where('`a`.`tribeId` = ?' , $tribeId, Qs_Db::INT_TYPE); $select->where('`a`.`type` = ?' , App_Admin_Obj::TYPE_TRIBAL_ACCOUNT); $select->where('`a`.`status` = ?' , App_Admin_Obj::STATUS_ACTIVE); $select->where('`a`.`email` IS NOT NULL'); $select->where('`acl`.`aclResourceId` IN (?)', [ TribalAccountAcl::ACL_LICENSES_READ_ID, TribalAccountAcl::ACL_LICENSES_READ_APPROVE_ID, TribalAccountAcl::ACL_LICENSES_READ_APPROVE_DECLINE_ID ]); $select->group('a.id'); return Qs_Db::getInstance()->fetchCol($select, ['email']); } private static function getLicenseAndVendor($licenseId, $tribeId) { $licenseModel = new Model(['db' => Qs_Db::getInstance()]); $licenseModel->setFilter('tribeId', $tribeId); if (!($license = $licenseModel->find(['id' => $licenseId]))) { return false; } $licenseModel->mapCollections($license); $vendorModel = new VendorModel(); $vendor = $vendorModel->get(['id' => $license['vendorId']], VendorModel::MAIN_DATA); return compact('license', 'vendor'); } private static function sendTemplateToUser($licenseId, $tribeId, $template, $placeholdersCallback = null) { if (false === ($data = self::getLicenseAndVendor($licenseId, $tribeId))) { return false; } if (!($to = (new VendorModel())->getUserEmails($data['license']['vendorId']))) { return false; } $placeholders = [ 'id' => $licenseId, 'tribe' => htmlspecialchars($data['license']['tribeTitle']), 'vendorDbaName' => htmlspecialchars($data['vendor']['dbaName']), 'vendorSoleProprietorName' => htmlspecialchars($data['vendor']['soleProprietorName']), 'vendorFederalId' => htmlspecialchars($data['vendor']['federalId']), 'vendorPhone' => htmlspecialchars($data['vendor']['phone']), 'detailsUrl' => LicenseUserView::getPage('url') . '?action=view&id=' . $licenseId, 'receiptUrl' => PdfView::getPremiumReceiptUrl($licenseId, $tribeId), ]; if ($placeholdersCallback && is_callable($placeholdersCallback)) { $placeholders = array_merge($placeholders, $placeholdersCallback($data)); } return Qs_Mail::sendTemplate($template, $placeholders, $to); } private static function sendTemplateToAdmin($licenseId, $tribeId, $template, $placeholdersCallback = null) { if (false === ($data = self::getLicenseAndVendor($licenseId, $tribeId))) { return false; } $placeholders = [ 'id' => $licenseId, 'tribe' => htmlspecialchars($data['license']['tribeTitle']), 'vendorDbaName' => htmlspecialchars($data['vendor']['dbaName']), 'vendorSoleProprietorName' => htmlspecialchars($data['vendor']['soleProprietorName']), 'vendorFederalId' => htmlspecialchars($data['vendor']['federalId']), 'vendorPhone' => htmlspecialchars($data['vendor']['phone']), 'detailsUrl' => LicenseAdminView::getPage('url') . '?action=view&id=' . $licenseId, ]; if ($placeholdersCallback && is_callable($placeholdersCallback)) { $placeholders = array_merge($placeholders, $placeholdersCallback($data)); } //Send to General $toGeneral = App_Settings_Obj::getAdminEmails(); if ($toGeneral) { Qs_Mail::sendTemplate($template, $placeholders, $toGeneral); } //Send to Admins $toAdmins = self::getAdminEmails(); foreach ($toAdmins as $toAdmin) { Qs_Mail::sendTemplate($template, $placeholders, $toAdmin); } //Send to Tribal Accounts $aliasParts = explode('/', LicenseAdminView::getPage('fullAlias')); $aliasParts[0] = 'tribal-admin'; $pageUrl = BASE_URL_LANGUAGE . '/' . implode('/', $aliasParts); $placeholders['detailsUrl'] = $pageUrl . '?action=view&id=' . $placeholders['id']; $toTribalAccounts = self::getTribalAccountEmails($tribeId); foreach ($toTribalAccounts as $toTribalAccount) { Qs_Mail::sendTemplate($template, $placeholders, $toTribalAccount); } } public static function sendPremiumPaidToUser($licenseId, $tribeId) { return self::sendTemplateToUser($licenseId, $tribeId, 'licensePremiumPaidToUser'); } public static function sendPremiumPaidToAdmin($licenseId, $tribeId) { return self::sendTemplateToAdmin($licenseId, $tribeId, 'licensePremiumPaidToAdmin'); } public static function sendLicensePremiumExpiredToUser($licenseId, $tribeId, $template) { $placeholders = function ($data) { return [ 'expiredOn' => date('m/d/Y', strtotime($data['license']['tribeSurchargeExpiredOn'])), 'gracePeriod' => App_Settings_Obj::get('licenseGracePeriod'), 'licenseSectionUrl' => UserView::getPage('url'), ]; }; return self::sendTemplateToUser($licenseId, $tribeId, $template, $placeholders); } public static function sendPremiumRenewedToUser($licenseId, $tribeId) { $placeholders = function ($data) { return [ 'expiredOn' => date('m/d/Y', strtotime($data['license']['tribeSurchargeExpiredOn'])), ]; }; return self::sendTemplateToUser($licenseId, $tribeId, 'licensePremiumRenewedToUser', $placeholders); } public static function sendPremiumRenewedToAdmin($licenseId, $tribeId) { $placeholders = function ($data) { return [ 'expiredOn' => date('m/d/Y', strtotime($data['license']['tribeSurchargeExpiredOn'])), ]; }; return self::sendTemplateToAdmin($licenseId, $tribeId, 'licensePremiumRenewedToAdmin', $placeholders); } }