public function up()
 {
     $this->renameClass('GcmRelation', GcmRelation::className());
 }
Ejemplo n.º 2
0
 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);
 }