Esempio n. 1
0
 public function add($post)
 {
     $model = new Notifications();
     $model->setAttributes($post);
     $model->created_at = time();
     $model->is_read = 0;
     if ($model->save(FALSE)) {
         return TRUE;
     }
     return FALSE;
 }
Esempio n. 2
0
 public static function note($msg, $opt = array(0, 0, 0), $is_assign = 1)
 {
     list($creator, $destination, $shared_id) = $opt;
     $note = new Notifications();
     $note->msg = $msg;
     $note->time = time();
     $note->creator_id = $creator;
     $note->dest_id = $destination;
     $note->shared_id = $shared_id;
     $note->status = $is_assign == 2 ? 2 : 1;
     return $note->validate() && $note->save();
 }
 /**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $model = new Notifications();
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['Notifications'])) {
         $model->attributes = $_POST['Notifications'];
         if ($model->save()) {
             $this->redirect(array('view', 'id' => $model->notification_id));
         }
     }
     $this->render('create', array('model' => $model));
 }
Esempio n. 4
0
 public function send($key, $types, $userId, array $arrayParams = array())
 {
     $status = false;
     $notification = NotificationsType::fetchAll(DBQueryParamsClass::CreateParams()->setConditions("`key`=:key")->setParams(array(":key" => $key)));
     if (!empty($notification) && sizeof($notification) > 0) {
         $notificationMessage = NotificationsActions::fetchAll(DBQueryParamsClass::CreateParams()->setCache(0)->setConditions("type_id=:type_id")->setParams(array(":type_id" => $notification[0]->id)));
         if (!empty($notificationMessage) && sizeof($notificationMessage) > 0) {
             for ($i = 0; $i < sizeof($notificationMessage); $i++) {
                 if (strtolower($notificationMessage[$i]->key_word) == "info") {
                     $NItem = new Notifications();
                     $NItem->type_id = $notification[0]->id;
                     $NItem->is_new = 1;
                     $NItem->action_id = $notificationMessage[$i]->id;
                     $message = $notificationMessage[$i]->mesage;
                     $subject = $notificationMessage[$i]->subject;
                     foreach ($arrayParams as $key => $value) {
                         $message = str_replace("{" . $key . "}", $value, $message);
                         $subject = str_replace("{" . $key . "}", $value, $subject);
                     }
                     $NItem->message = $message;
                     $NItem->subject = $subject;
                     if (!$notificationMessage[$i]->to_user) {
                         $NItem->user_id = $userId;
                     } else {
                         $toUserModel = CatalogUsers::findByAttributes(array("email" => $notificationMessage[$i]->to_user));
                         if ($toUserModel[0]->id > 0) {
                             $NItem->user_id = $toUserModel[0]->id;
                         } else {
                             $this->errors[] = array("Ошибка обработки действвий", "Событие: #" . $notification[$i] . ", Действие: #" . $notificationMessage[$i] . " - Email указыыный в поле TO_USER не зарегестрирован в базе");
                         }
                     }
                     $NItem->date = time();
                     if (!empty($arrayParams["catalog"])) {
                         $NItem->catalog = $arrayParams["catalog"];
                     }
                     if (!empty($arrayParams["item_id"])) {
                         $NItem->item_id = $arrayParams["item_id"];
                     }
                     if (sizeof($this->errors) == 0) {
                         if (!$NItem->save()) {
                             $this->errors[] = print_r($NItem->getErrors(), true);
                         }
                     }
                 }
                 if (strtolower($notificationMessage[$i]->key_word) == "mail") {
                     if (!$notificationMessage[$i]->to_user) {
                         $userTo = CatalogUsers::fetch($userId);
                     } else {
                         $toUserModel = CatalogUsers::findByAttributes(array("email" => $notificationMessage[$i]->to_user));
                         if ($toUserModel[0]->id > 0) {
                             $userTo = $toUserModel[0];
                         } else {
                             $this->errors[] = array("Ошибка обработки действвий", "Событие: #" . $notification[$i] . ", Действие: #" . $notificationMessage[$i] . " - Email указыыный в поле TO_USER не зарегестрирован в базе");
                         }
                     }
                     if (!empty($userTo) && $userTo->id > 0) {
                         $messages = $notificationMessage[$i]->mesage;
                         foreach ($arrayParams as $key => $value) {
                             $messages = str_replace("{" . $key . "}", $value, $messages);
                         }
                         SiteHelper::mailto($notificationMessage[$i]->subject, $notificationMessage[$i]->send_from, $userTo->email, $messages, $notificationMessage[$i]->copy_sender);
                         $status = true;
                     } else {
                         $this->errors[] = array("Ошибка отправки сообщения", "Указан не верный ID пользователя");
                         return false;
                     }
                 }
             }
         } else {
             $this->errors[] = array("Ошибка события", "Для данного соьытия ( #" . $notification[0]->id . " ) не указы события");
         }
     } else {
         $this->errors[] = array("Ошибка события", "Ошибка определения типа события");
     }
     if (is_array($this->errors) && sizeof($this->errors) > 0) {
         throw new Exception(print_r($this->errors, true));
     }
     return $status;
 }
 public function actionComplete($id)
 {
     $model = $this->loadModel($id);
     if (Yii::app()->user->getName() == $model->assignedTo || $model->assignedTo == 'Anyone' || $model->assignedTo == "" || Yii::app()->user->getName() == 'admin') {
         if (isset($_POST['note'])) {
             $model->actionDescription = $model->actionDescription . "\n\n" . $_POST['note'];
         }
         $model = $this->updateChangelog($model, 'Completed');
         $model->save();
         Actions::completeAction($id);
         $notif = new Notifications();
         $notif->record = "Actions:{$model->id}";
         $profile = CActiveRecord::model('ProfileChild')->findByAttributes(array('username' => Yii::app()->user->getName()));
         $notif->text = $profile->fullName . " completed an action.";
         $notif->user = '******';
         $notif->createDate = time();
         $notif->viewed = 0;
         $notif->save();
         $createNew = isset($_GET['createNew']) || isset($_POST['submit']) && $_POST['submit'] == 'completeNew';
         $redirect = isset($_GET['redirect']) || $createNew;
         if ($redirect) {
             if ($model->associationType != 'none' && !$createNew) {
                 // if the action has an association
                 $this->redirect(array($model->associationType . '/view', 'id' => $model->associationId));
                 // go back to the association
             } else {
                 // no association
                 if ($createNew) {
                     $this->redirect(array('/actions/create'));
                 } else {
                     $this->redirect(array('/actions/default/view', 'id' => $model->id));
                 }
                 // view the action
             }
         } else {
             $this->redirect(array('/actions/default/view', 'id' => $model->id));
         }
     } else {
         $this->redirect(array('/actions/invalid'));
     }
 }
 public function actionAddComment()
 {
     $soc = new Social();
     if (isset($_GET['comment'])) {
         $soc->data = $_GET['comment'];
         $id = $_GET['id'];
         $model = Social::model()->findByPk($id);
         $soc->user = Yii::app()->user->getName();
         $soc->type = 'comment';
         $soc->timestamp = time();
         $soc->associationId = $id;
         $model->lastUpdated = time();
         if ($soc->save() && $model->save()) {
             $notif = new Notifications();
             $prof = CActiveRecord::model('ProfileChild')->findByAttributes(array('username' => $soc->user));
             $notif->text = "{$prof->fullName} added a comment to a post.";
             $notif->record = "profile:{$model->associationId}";
             $notif->viewed = 0;
             $notif->createDate = time();
             $subject = CActiveRecord::model('ProfileChild')->findByAttributes(array('username' => $model->user));
             $notif->user = $subject->username;
             if ($notif->user != Yii::app()->user->getName()) {
                 $notif->save();
             }
             $notif = new Notifications();
             $prof = CActiveRecord::model('ProfileChild')->findByAttributes(array('username' => $soc->user));
             $subject = CActiveRecord::model('ProfileChild')->findByPk($model->associationId);
             $notif->text = "{$prof->fullName} added a comment to a post.";
             $notif->record = "profile:{$model->associationId}";
             $notif->viewed = 0;
             $notif->createDate = time();
             $notif->user = $subject->username;
             if ($notif->user != Yii::app()->user->getName()) {
                 $notif->save();
             }
         }
     }
     if (isset($_GET['redirect'])) {
         if ($_GET['redirect'] == "view") {
             $this->redirect(array('view', 'id' => $model->associationId));
         }
         if ($_GET['redirect'] == "index") {
             $this->redirect(array('index'));
         }
     } else {
         $this->redirect(array('index'));
     }
 }
 public function createQuote($model, $oldAttributes, $products)
 {
     $model->createDate = time();
     $model->lastUpdated = time();
     $model->createdBy = Yii::app()->user->getName();
     $model->updatedBy = Yii::app()->user->getName();
     if ($model->expectedCloseDate != "") {
         $model->expectedCloseDate = strtotime($model->expectedCloseDate);
     }
     $name = $this->modelClass;
     if ($model->save()) {
         $changes = $this->calculateChanges($oldAttributes, $model->attributes, $model);
         $this->updateChangelog($model, $changes);
         if ($model->assignedTo != Yii::app()->user->getName()) {
             $notif = new Notifications();
             $profile = CActiveRecord::model('ProfileChild')->findByAttributes(array('username' => $model->assignedTo));
             if (isset($profile)) {
                 $notif->text = "{$profile->fullName} has created a(n) " . $name . " for you";
             }
             $notif->user = $model->assignedTo;
             $notif->createDate = time();
             $notif->viewed = 0;
             $notif->record = "{$name}:{$model->id}";
             $notif->save();
         }
         // tie contacts to quote
         /*
         		   	foreach($contacts as $contact) {
         		   		$relate = new Relationships;
         		   		$relate->firstId = $model->id;
         		   		$relate->firstType = "quotes";
         		   		$relate->secondId = $contact;
         		   		$relate->secondType = "contacts";
         		   		$relate->save();
         		   	} */
         // tie products to quote
         foreach ($products as $product) {
             $qp = new QuoteProduct();
             $qp->quoteId = $model->id;
             $qp->productId = $product['id'];
             $qp->name = $product['name'];
             $qp->price = $product['price'];
             $qp->quantity = $product['quantity'];
             $qp->adjustment = $product['adjustment'];
             $qp->adjustmentType = $product['adjustmentType'];
             $qp->save();
         }
         $this->redirect(array('view', 'id' => $model->id));
     } else {
         return false;
     }
 }
Esempio n. 8
0
 protected function calculateChanges($old, $new, &$model = null)
 {
     $arr = array();
     $keys = array_keys($new);
     for ($i = 0; $i < count($keys); $i++) {
         if ($old[$keys[$i]] != $new[$keys[$i]]) {
             $arr[$keys[$i]] = $new[$keys[$i]];
             $allCriteria = Criteria::model()->findAllByAttributes(array('modelType' => $this->modelClass, 'modelField' => $keys[$i]));
             foreach ($allCriteria as $criteria) {
                 if ($criteria->comparisonOperator == "=" && $new[$keys[$i]] == $criteria->modelValue || $criteria->comparisonOperator == ">" && $new[$keys[$i]] >= $criteria->modelValue || $criteria->comparisonOperator == "<" && $new[$keys[$i]] <= $criteria->modelValue || $criteria->comparisonOperator == "change" && $new[$keys[$i]] != $old[$keys[$i]]) {
                     if ($criteria->type == 'notification') {
                         $pieces = explode(", ", $criteria->users);
                         foreach ($pieces as $piece) {
                             $notif = new Notifications();
                             $profile = CActiveRecord::model('ProfileChild')->findByAttributes(array('username' => Yii::app()->user->getName()));
                             if ($criteria->comparisonOperator == "=") {
                                 $notif->text = "A record of type " . $this->modelClass . " has been modified to meet {$criteria->modelField} {$criteria->comparisonOperator} {$criteria->modelValue}" . " by " . Yii::app()->user->getName();
                             } else {
                                 if ($criteria->comparisonOperator == ">") {
                                     $notif->text = "A record of type " . $this->modelClass . " has been modified to meet {$criteria->modelField} {$criteria->comparisonOperator} {$criteria->modelValue}" . " by " . Yii::app()->user->getName();
                                 } else {
                                     if ($criteria->comparisonOperator == "<") {
                                         $notif->text = "A record of type " . $this->modelClass . " has been modified to meet {$criteria->modelField} {$criteria->comparisonOperator} {$criteria->modelValue}" . " by " . Yii::app()->user->getName();
                                     } else {
                                         if ($criteria->comparisonOperator == "change") {
                                             $notif->text = "A record of type " . $this->modelClass . " has had its {$criteria->modelField} field changed from " . $old[$keys[$i]] . " to " . $new[$keys[$i]] . " by " . Yii::app()->user->getName();
                                         }
                                     }
                                 }
                             }
                             $notif->user = $piece;
                             $notif->createDate = time();
                             $notif->viewed = 0;
                             $notif->record = $this->modelClass . ":" . $new['id'];
                             $notif->save();
                         }
                     } else {
                         if ($criteria->type == 'action') {
                             $pieces = explode(", ", $criteria->users);
                             foreach ($pieces as $piece) {
                                 $action = new Actions();
                                 $action->assignedTo = $piece;
                                 if ($criteria->comparisonOperator == "=") {
                                     $action->actionDescription = "A record of type " . $this->modelClass . " has been modified to meet {$criteria->modelField} {$criteria->comparisonOperator} {$criteria->modelValue}" . " by " . Yii::app()->user->getName();
                                 } else {
                                     if ($criteria->comparisonOperator == ">") {
                                         $action->actionDescription = "A record of type " . $this->modelClass . " has been modified to meet {$criteria->modelField} {$criteria->comparisonOperator} {$criteria->modelValue}" . " by " . Yii::app()->user->getName();
                                     } else {
                                         if ($criteria->comparisonOperator == "<") {
                                             $action->actionDescription = "A record of type " . $this->modelClass . " has been modified to meet {$criteria->modelField} {$criteria->comparisonOperator} {$criteria->modelValue}" . " by " . Yii::app()->user->getName();
                                         } else {
                                             if ($criteria->comparisonOperator == "change") {
                                                 $action->actionDescription = "A record of type " . $this->modelClass . " has had its {$criteria->modelField} field changed from " . $old[$keys[$i]] . " to " . $new[$keys[$i]] . " by " . Yii::app()->user->getName();
                                             }
                                         }
                                     }
                                 }
                                 $action->dueDate = mktime('23', '59', '59');
                                 $action->createDate = time();
                                 $action->lastUpdated = time();
                                 $action->updatedBy = 'admin';
                                 $action->visibility = 1;
                                 $action->associationType = strtolower($this->modelClass);
                                 $action->associationId = $new['id'];
                                 $model = CActiveRecord::model($this->modelClass)->findByPk($new['id']);
                                 $action->associationName = $model->name;
                                 $action->save();
                             }
                         } else {
                             if ($criteria->type == 'assignment') {
                                 $model->assignedTo = $criteria->users;
                                 $model->save();
                                 $notif = new Notifications();
                                 $notif->text = "A record of type " . $this->modelClass . " has been re-assigned to you.";
                                 $notif->user = $model->assignedTo;
                                 $notif->createDate = time();
                                 $notif->viewed = 0;
                                 $notif->record = $this->modelClass . ":" . $new['id'];
                                 $notif->save();
                             }
                         }
                     }
                 }
             }
         }
     }
     $str = '';
     foreach ($arr as $key => $item) {
         $str .= "<b>{$key}</b> <u>FROM:</u> {$old[$key]} <u>TO:</u> {$item} <br />";
     }
     return $str;
 }
Esempio n. 9
0
 /**
  * Generates and sends a new notification to interested users.
  * If the configuration file is not found or an error occurs while sending mail, logs the error on database.
  * @param string $model_name the name of model to be loaded
  * @param string/integer $model_id the id of model to be loaded
  * @param string $category the category of notification
  * @param string $type the type of notification
  */
 public static function generate($model_name, $model_id, $category, $type, $other = null)
 {
     $roles = Notifications::findCategory($category, $type);
     foreach ($roles as $role => $role_name) {
         $model = new Notifications();
         $model->notification_category_ptr = $role;
         $log_error = false;
         $file_error = false;
         $error_message = "";
         $link_view = Yii::app()->createAbsoluteUrl(lcfirst($model_name) . '/view', array('id' => $model_id));
         if (file_exists(Yii::app()->basePath . '/views/' . lcfirst($model_name) . '/' . $model->config_file)) {
             include Yii::app()->basePath . '/views/' . lcfirst($model_name) . '/' . $model->config_file;
             if (isset($messages[$type]) && isset($messages[$type][$role_name])) {
                 if (isset($messages[$type][$role_name]['subject'])) {
                     $model->mail_subject = $messages[$type][$role_name]['subject'];
                 }
                 if (isset($messages[$type][$role_name]['description'])) {
                     $model->mail_body = $messages[$type][$role_name]['description'];
                 }
             } else {
                 $file_error = true;
                 $errore_message = 'Description and mail variables for ' . lcfirst($model_name) . 'category and ' . $type . 'type operation do not exist';
             }
         } else {
             $file_error = true;
             $error_message = 'File ' . Yii::app()->basePath . '/views/' . lcfirst($model_name) . '/' . $model->config_file . ' does not exists';
         }
         if ($file_error) {
             if (file_exists(Yii::app()->basePath . '/views/' . $model->config_file)) {
                 include Yii::app()->basePath . '/views/' . $model->config_file;
                 if (isset($description) && isset($mail)) {
                     $model->mail_subject = $description;
                     $model->mail_body = $mail;
                 } else {
                     $log_error = true;
                     $errore_message = 'Description and mail variables for general category and type operation do not exist';
                 }
             } else {
                 $log_error = true;
                 $error_message = 'File ' . Yii::app()->basePath . '/views/' . $model->config_file . ' does not exists';
             }
         }
         if (!$log_error) {
             $model_input = CActiveRecord::model($model_name)->findByPk($model_id);
             try {
                 $model->owner = $model_input->username;
             } catch (Exception $e) {
                 $model->owner = false;
             }
             foreach ($model_input->attributeNames() as $attribute) {
                 $model->mail_subject = str_replace('$' . $attribute, $model_input->{$attribute}, $model->mail_subject);
                 $model->mail_body = str_replace('$' . $attribute, $model_input->{$attribute}, $model->mail_body);
                 $model->mail_body = str_replace('$link_view', $link_view, $model->mail_body);
             }
             if ($model->owner) {
                 $model_user = Users::model()->findByPk($model_input->username);
                 foreach ($model_user->attributeNames() as $attribute) {
                     $model->mail_subject = str_replace('$' . $attribute, $model_user->{$attribute}, $model->mail_subject);
                     $model->mail_body = str_replace('$' . $attribute, $model_user->{$attribute}, $model->mail_body);
                     $model->mail_body = str_replace('-link', $model_user->activation_link, $model->mail_body);
                 }
             }
             if (!empty($other)) {
                 $model->mail_body = str_replace('$other', $other, $model->mail_body);
             }
             $model->attributes = array('description' => $model->mail_subject, 'link' => $link_view);
             $t = $model->save();
             Yii::log('saving...->' . $t, CLogger::LEVEL_INFO, 'Notifications::create()');
         } else {
             //logs send failure
             $logs = new SendmailLogs();
             $logs->attributes = array('mail_type' => 'notifications', 'mail_identifier' => 0, 'error_code' => 1, 'error_message' => $error_message, 'address' => $email);
             $logs->save();
         }
     }
 }