コード例 #1
0
 /**
  * Broadcast a message to all users
  */
 public function run()
 {
     //Send GCM notifications
     $model = new $this->modelClass();
     $records = $model->findBySql("SELECT * FROM gcm_relation")->all();
     $params = Yii::$app->getRequest()->getBodyParams();
     $data = array('message' => $params['message'], 'url' => $params['url']);
     $results = array();
     foreach ($records as $record) {
         if ($record->register_key) {
             //Enviar missatge
             $gcm = new GoogleCloudMessage();
             $gcm->apiKey = Setting::Get('gcmAPIKey', 'gcm');
             $gcm->url = Setting::Get('gcmURL', 'gcm');
             $result = $gcm->send($data, array($record->register_key));
             $results[$record->register_key] = $result;
         }
     }
     //Send mail notificacion
     $users = User::find()->distinct()->joinWith(['httpSessions', 'profile'])->where(['status' => User::STATUS_ENABLED]);
     Yii::setAlias('@gcmmodule', Yii::$app->getModule('gcm')->getBasePath());
     foreach ($users->each() as $user) {
         $mail = Yii::$app->mailer->compose(['html' => '@gcmmodule/views/emails/NewMessage'], ['message' => $params['message'], 'url' => $params['url']]);
         $mail->setFrom([Setting::Get('systemEmailAddress', 'mailing') => Setting::Get('systemEmailName', 'mailing')]);
         $mail->setTo($user->email);
         $mail->setSubject('Nova notícia al web: ' . $params['message']);
         $mail->send();
     }
     return true;
 }
コード例 #2
0
ファイル: Events.php プロジェクト: ravalnet/humhub-Gedi
 public static function onCronRun($event)
 {
     $controller = $event->sender;
     $interval = "";
     if (Yii::$app->controller->action->id == 'hourly') {
         $interval = CronController::EVENT_ON_HOURLY_RUN;
     } elseif (Yii::$app->controller->action->id == 'daily') {
         $interval = CronController::EVENT_ON_DAILY_RUN;
     } else {
         return;
     }
     $users = User::find()->distinct()->joinWith(['httpSessions', 'profile'])->where(['status' => User::STATUS_ENABLED]);
     $totalUsers = $users->count();
     $done = 0;
     $mailsSent = 0;
     $defaultLanguage = Yii::$app->language;
     Console::startProgress($done, $totalUsers, 'Sending update e-mails to users... ', false);
     foreach ($users->each() as $user) {
         if ($user->email === "") {
             continue;
         }
         // Check user should receive an email
         Yii::$app->user->switchIdentity($user);
         if ($user->language != "") {
             Yii::$app->language = $user->language;
         } else {
             Yii::$app->language = $defaultLanguage;
         }
         $notifications = Yii::$app->getModule('notification')->getMailUpdate($user, $interval);
         $activities = Yii::$app->getModule('activity')->getMailUpdate($user, $interval);
         if ($notifications != "" || $activities != "") {
             try {
                 /**************************************
                  * TEBPATCH (ADD) @fcasanellas 14/01/2016
                  * #MSS001 Afegeix enviament via GCM
                  * NEWCODE ***************************/
                 $gcm = new GoogleCloudMessage();
                 $gcm->apiKey = Setting::Get('gcmAPIKey', 'gcm');
                 $gcm->url = Setting::Get('gcmURL', 'gcm');
                 $data = array('message' => 'Noves publicacions');
                 foreach (GcmRelation::findAll(array('user_id' => $user->id)) as $pair) {
                     $result = $gcm->send($data, array($pair->register_key));
                 }
                 /**************************************
                  * END TEBPATCH **********************/
                 $mail = Yii::$app->mailer->compose(['html' => '@humhub/modules/content/views/mails/Update'], ['activities' => $activities, 'notifications' => $notifications]);
                 $mail->setFrom([Setting::Get('systemEmailAddress', 'mailing') => Setting::Get('systemEmailName', 'mailing')]);
                 $mail->setTo($user->email);
                 if ($interval == CronController::EVENT_ON_HOURLY_RUN) {
                     $mail->setSubject(Yii::t('base', "Latest news"));
                 } else {
                     $mail->setSubject(Yii::t('base', "Your daily summary"));
                 }
                 $mail->send();
                 $mailsSent++;
             } catch (\Swift_SwiftException $ex) {
                 Yii::error('Could not send mail to: ' . $user->email . ' - Error:  ' . $ex->getMessage());
             } catch (Exception $ex) {
                 Yii::error('Could not send mail to: ' . $user->email . ' - Error:  ' . $ex->getMessage());
             }
         }
         Console::updateProgress(++$done, $totalUsers);
     }
     Console::endProgress(true);
     $controller->stdout('done - ' . $mailsSent . ' email(s) sent.' . PHP_EOL, \yii\helpers\Console::FG_GREEN);
 }