EmailNotificationHelper.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace MGModule\DNSManager2\mgLibs\custom\manager;
  3. use \MGModule\DNSManager2\mgLibs\custom\mailer\Mailer;
  4. use \MGModule\DNSManager2\mgLibs\MySQL\query;
  5. use \MGModule\DNSManager2\models\custom\globalsetting\GlobalSettingEnum;
  6. use \MGModule\DNSManager2\models\custom\notification\NotificationNameEnum;
  7. use \MGModule\DNSManager2\models\custom\zone\Zone;
  8. use \MGModule\DNSManager2\models\whmcs\admin\admin;
  9. use MGModule\DNSManager2\models\whmcs\client\client;
  10. use MGModule\DNSManager2\models\whmcs\clients\repository;
  11. class EmailNotificationHelper {
  12. private static $messages_map = array(
  13. DefaultNotifications::GENERAL_ZONE_CREATED_NOTIFICATION => array(
  14. 'type' => 'client',
  15. 'option' => GlobalSettingEnum::CLIENT_NOTIFICATION_ZONE_CREATED,
  16. 'exclude' => GlobalSettingEnum::CLIENT_NOTIFICATION_ZONE_CREATED_EXCLUDE,
  17. 'notification' => NotificationNameEnum::ZONE_CREATED_EMAIL,
  18. ),
  19. DefaultNotifications::GENERAL_ZONE_ALTERED_NOTIFICATION => array(
  20. 'type' => 'client',
  21. 'option' => GlobalSettingEnum::CLIENT_NOTIFICATION_ZONE_ALTERED,
  22. 'exclude' => GlobalSettingEnum::CLIENT_NOTIFICATION_ZONE_ALTERED_EXCLUDE,
  23. 'notification' => NotificationNameEnum::ZONE_EDITED_EMAIL,
  24. ),
  25. DefaultNotifications::GENERAL_ZONE_REMOVED_NOTIFICATION => array(
  26. 'type' => 'client',
  27. 'option' => GlobalSettingEnum::CLIENT_NOTIFICATION_ZONE_REMOVED,
  28. 'exclude' => GlobalSettingEnum::CLIENT_NOTIFICATION_ZONE_REMOVED_EXCLUDE,
  29. 'notification' => NotificationNameEnum::ZONED_REMOVED_EMAIL,
  30. ),
  31. DefaultNotifications::ADMIN_CRON_CLEANER_NOTIFICATION => array(
  32. 'type' => 'admin',
  33. 'option' => GlobalSettingEnum::ADMIN_NOTIFICATION_CRON_CLEANER,
  34. 'include' => GlobalSettingEnum::ADMIN_NOTIFICATION_CRON_CLEANER_INCLUDE,w
  35. ),
  36. DefaultNotifications::ADMIN_CRON_MIGRATOR_NOTIFICATION => array(
  37. 'type' => 'admin',
  38. 'option' => GlobalSettingEnum::ADMIN_NOTIFICATION_CRON_MIGRATOR,
  39. 'include' => GlobalSettingEnum::ADMIN_NOTIFICATION_CRON_MIGRATOR_INCLUDE,
  40. ),
  41. DefaultNotifications::ADMIN_CRON_SYNCHRONIZATOR_NOTIFIACTION => array(
  42. 'type' => 'admin',
  43. 'option' => GlobalSettingEnum::ADMIN_NOTIFICATION_CRON_SYNCHRONIZATOR,
  44. 'include' => GlobalSettingEnum::ADMIN_NOTIFICATION_CRON_SYNCHRONIZATOR_INCLUDE,
  45. ),
  46. );
  47. public static function sendClientNotificationUsingZoneID($message_name, $zoneid, $data = array()) {
  48. $zone = Zone::factory($zoneid);
  49. self::sendClientNotificationUsingZone($message_name, $zone, $data);
  50. }
  51. public static function sendClientNotificationUsingZone($message_name, $zone, $data = array()) {
  52. $item = $zone->getPackageItem();
  53. if($item == false) {
  54. return ;
  55. }
  56. self::sendClientNotification($message_name, $zone->clientid, $item->type, $item->relid, $data);
  57. }
  58. public static function sendClientNotification($message_name, $clientid, $type, $relid, $data = array()) {
  59. $message = self::$messages_map[$message_name];
  60. if(GlobalSettingHelper::getSetting($message['option']) != 'on')
  61. return ;
  62. $exclude = GlobalSettingHelper::getSetting($message['exclude']);
  63. $exclude = explode(';', $exclude);
  64. foreach($exclude as &$arr)
  65. $arr = explode(',', $arr);
  66. if(in_array($relid, $exclude[$type-1]))
  67. return ;
  68. $data['customvars'] = $data;
  69. $data['id'] = $clientid;
  70. $client = new \MGModule\DNSManager2\models\whmcs\clients\client($clientid);
  71. $clientNames = "{$client->firstname} {$client->lastname}";
  72. $data['messagename'] = $message_name;
  73. $clientURL = '<a href="clientssummary.php?userid=%s"> #%s (%s) </a>';
  74. $client = sprintf($clientURL, $clientid, $clientid, $clientNames);
  75. $value = "Email $message_name sent to a client with ID $client";
  76. NotificationHelper::addInfoNotification($value, $message['notification']);
  77. return self::localApi('sendemail', $data);
  78. }
  79. public static function sendAdminNotification($message_name, $data = array()) {
  80. $message = self::$messages_map[$message_name];
  81. if(GlobalSettingHelper::getSetting($message['option']) != 'on')
  82. return ;
  83. $include = GlobalSettingHelper::getSetting($message['include']);
  84. $include = explode(',', $include);
  85. $mailer = new Mailer();
  86. foreach($include as $adminid) {
  87. try {
  88. $admin = new admin($adminid);
  89. $mailer->addAddress($admin->email);
  90. } catch (\Exception $e) {
  91. }
  92. }
  93. $mailer->addVariables($data);
  94. $mailer->sendWHMCSTemplate($message_name);
  95. }
  96. private static function localApi($command, $data = array()) {
  97. return localAPI($command, $data, self::getAdmin());
  98. }
  99. private static function getAdmin() {
  100. return query::query('SELECT tbladmins.username FROM tbladmins WHERE tbladmins.roleid = \'1\'')->fetchColumn();
  101. }
  102. }