/** * Execute the console command. * * @return mixed */ public function fire() { $jobs = Job::whereNull('texted_at')->where('job_status_id', '=', JobStatus::$OPEN)->where('start_date', '>=', date("Y-m-d"))->orderBy("start_date")->get(); $catJobs = array(); $users = array(); $userJobs = array(); $userCategories = array(); $user_ids = array(); $jobs->load("jobCategory", "user"); // echo '<pre>'; //print_r($jobs); //echo '</pre>'; foreach ($jobs as $job) { if (trim($job->latitude) != "" && trim($job->longitude) != "") { $userAddresses = Address::select("addresses.*", "student_profile.preferred_job_radius", Address::distanceSelectStatement($job->latitude, $job->longitude, 'distance', 'addresses'))->join("student_profile", "addresses.user_id", "=", "student_profile.user_id")->get(); foreach ($userAddresses as $address) { if ($address->distance > 0 && $address->distance <= $address->preferred_job_radius) { //echo "here".$address->distance; if (!isset($userCategories[$address->user_id])) { $userCategories[$address->user_id] = StudentJobPreference::where('user_id', '=', $address->user_id)->lists('job_category_id'); } if (in_array($job->job_category_id, $userCategories[$address->user_id]) == 1) { if (!isset($userJobs[$address->user_id])) { $user_ids[] = $address->user_id; $userJobs[$address->user_id] = array(); $users[$address->user_id] = $address->user; } $userJobs[$address->user_id][] = $job; } } } } //$job->notified_at=date('Y-m-d h:i:s'); $job->texted_at = date('Y-m-d h:i:s'); $job->save(); } $c = 0; if (count($user_ids) > 0) { $notifications = UserNotification::whereIn('user_id', $user_ids)->where("notification_types_id", '=', NotificationType::$SMS)->get(); foreach ($notifications as $notification) { $user = $users[$notification->user_id]; //$this->info($user->id); $c++; $this->info($notification->notification_value); $message = View::make('sms.commands.job_notifications'); if (trim($notification->notification_value) != "" && !is_null($notification->notification_value)) { try { Twilio::message($notification->notification_value, substr($message, 0, 160)); } catch (Exception $e) { \Log::info('Error: ' . $e->getMessage()); $this->info('Error: ' . $e->getMessage()); } } } } \Log::info('Cron Notification Text: ' . $c); $this->info($c . 'text sent'); }
/** * Send a notification to a single user at a time * * @param UserNotification $notification * @param string $email * @param array $data */ public function sendToUser($notification, $context, $user, $data) { $cls = new stdClass(); $cls->notification = $notification; $cls->text = $notification->format($notification->NotificationText, $context, $user, $data); $cls->context = $context; $cls->user = $user; $cls->data = $data; $this->notifications[] = $cls; }
public function actionUpdateEmailNotification($id) { $user = $this->_loadUser($id); $newValues = array(); if (!empty($_POST['value'])) { $newValues = (array) $_POST['value']; } $oldValues = UserNotification::model()->findAll('user_id=:user_id', array(':user_id' => $user->id)); if (!empty($oldValues)) { foreach ($oldValues as $key => $value) { if (false !== ($key_ar = array_search($value->notification_id, $newValues))) { // get key of $newValues array if value = id $value->by_email = true; $value->save(false); unset($newValues[$key_ar]); } else { $value->by_email = false; $value->save(false); } } } if (!empty($newValues)) { foreach ($newValues as $value) { $userNotification = new UserNotification(); $userNotification->user_id = $user->id; $userNotification->notification_id = (int) $value; $userNotification->by_email = true; if (!$userNotification->save()) { header('HTTP 400 Bad Request', true, 400); echo CJSON::encode($userNotification->errors); Yii::app()->end(); } } } echo '[]'; }
/** * @inheritdoc */ public function send($message, $category = null, $object_pk = null, $subject_pk = null, $notification = null) { $by_email = false; $notifications = null; $subscriptions = NfyDbSubscription::model()->current()->withQueue($this->id)->matchingCategory($category)->findAll(); if($message == null && !empty($notification)){ $notifications = Notifications::model()->findByPk($notification); if($notifications){ $message = $notifications->body; } if(empty($message)){ $success = false; } else { $queueMessage = $this->createNotification($message, $notifications->id, $object_pk, $subject_pk); } } else { $queueMessage = $this->createMessage($message, $object_pk, $subject_pk); } if ($this->beforeSend($queueMessage) !== true) { Yii::log(Yii::t('NfyModule.app', "Not sending message '{msg}' to queue {queue_label}.", array('{msg}' => $queueMessage->body, '{queue_label}' => $this->label)), CLogger::LEVEL_INFO, 'nfy'); return; } $success = true; $trx = $queueMessage->getDbConnection()->getCurrentTransaction() !== null ? null : $queueMessage->getDbConnection()->beginTransaction(); if (!$queueMessage->save()) { Yii::log(Yii::t('NfyModule.app', "Failed to save message '{msg}' in queue {queue_label}.", array('{msg}' => $queueMessage->body, '{queue_label}' => $this->label)), CLogger::LEVEL_ERROR, 'nfy'); return false; } if(!empty($subscriptions)){ foreach ($subscriptions as $subscription) { $simple = true; $subscriptionMessage = clone $queueMessage; $subscriptionMessage->subscription_id = $subscription->id; $subscriptionMessage->message_id = $queueMessage->id; if ($this->beforeSendSubscription($subscriptionMessage, $subscription->subscriber_id) !== true) { continue; } if($notifications){ $user_notification = UserNotification::model()->find('user_id=:user_id and notification_id=:notification_id',array(':user_id'=>$subscription->subscriber_id,':notification_id'=>$notification)); if($user_notification && $user_notification->by_email == true){ if(!$this->sendMessageByEmail($queueMessage, $subscription)) { Yii::log(Yii::t('NfyModule.app', "Failed to send message '{msg}' by email in queue {queue_label} for the subscription {subscription_id}.", array( '{msg}' => $queueMessage->body, '{queue_label}' => $this->label, '{subscription_id}' => $subscription->id, )), CLogger::LEVEL_ERROR, 'nfy'); $success = false; } } if($notifications->available == false){ // not send simple notification $simple = false; } } if ($simple == false) { continue; } if (!$subscriptionMessage->save()) { Yii::log(Yii::t('NfyModule.app', "Failed to save message '{msg}' in queue {queue_label} for the subscription {subscription_id}.", array( '{msg}' => $queueMessage->body, '{queue_label}' => $this->label, '{subscription_id}' => $subscription->id, )), CLogger::LEVEL_ERROR, 'nfy'); $success = false; } $this->afterSendSubscription($subscriptionMessage, $subscription->subscriber_id); } } $this->afterSend($queueMessage); if ($trx !== null) { $trx->commit(); } Yii::log(Yii::t('NfyModule.app', "Sent message '{msg}' to queue {queue_label}.", array('{msg}' => $queueMessage->body, '{queue_label}' => $this->label)), CLogger::LEVEL_INFO, 'nfy'); return $success; }
/** * @covers ::delete */ public function testDelete() { $mockdb = new \TMT\MockDB(); $mockdb->expectPrepare("UPDATE userNotifications SET deleted=1 WHERE netId=:netId AND notificationGuid=:guid"); $mockdb->expectExecute(array(":netId" => "netId", ":guid" => "guid1")); $accessor = new UserNotification($mockdb); $accessor->delete("netId", "guid1"); $mockdb->verify(); }