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 run($args) { $countLimit = SiteHelper::getConfig("subscribee_count_send"); $emails = array(); $countSend = 0; $res = SubscribeItems::findByAttributes(array("status_id" => 2)); foreach ($res as $line) { // 1 - отправляем и зарегеным и подписчикам if ($line->users == 1 || $line->users == 2) { $lisUsers = CatalogUsers::sql("SELECT u.* FROM catalog_users u WHERE `active`=1 AND subscribe=1 AND !exists( SELECT id FROM subscribe_send WHERE email=u.email AND item_id='" . $line->id . "' AND is_reg=1 ) LIMIT " . $countLimit); foreach ($lisUsers as $userLine) { $emails[] = array("email" => $userLine["email"], "name" => $userLine["name"]); $newSend = new SubscribeSend(); $newSend->item_id = $line->id; $newSend->user_id = $userLine["id"]; $newSend->email = $userLine["email"]; $newSend->is_reg = 1; if (!$newSend->save()) { print_r($newSend->getErrors()); } } } if (sizeof($emails) < $countLimit && ($line->users == 1 || $line->users == 3)) { $lisUsers = CatalogUsers::sql("SELECT u.* FROM subscribe_users u WHERE !exists( SELECT id FROM subscribe_send WHERE email=u.email AND item_id='" . $line->id . "' AND is_reg=0 ) LIMIT " . ($countLimit - sizeof($emails))); foreach ($lisUsers as $userLine) { $emails[] = array("email" => $userLine["email"], "name" => $userLine["name"]); $newSend = new SubscribeSend(); $newSend->item_id = $line->id; $newSend->user_id = null; $newSend->email = $userLine["email"]; $newSend->is_reg = 0; if (!$newSend->save()) { print_r($newSend->getErrors()); } } } if ($line->users == 4) { $usersList = trim(strip_tags($line->users_list)); if (!empty($usersList)) { $listEmail = explode(",", $usersList); for ($m = 0; $m < sizeof($listEmail); $m++) { $listEmail[$m] = trim($listEmail[$m]); $ext = SubscribeSend::findByAttributes(array("item_id" => $line->id, "email" => $listEmail[$m])); if (sizeof($ext) == 0) { $emails[] = array("email" => $listEmail[$m], "name" => "пользователь"); $newSend = new SubscribeSend(); $newSend->item_id = $line->id; $newSend->user_id = null; $newSend->email = $listEmail[$m]; $newSend->is_reg = 0; if (!$newSend->save()) { print_r($newSend->getErrors()); } } } } } if (sizeof($emails) > 0) { for ($n = 0; $n < sizeof($emails); $n++) { $countSend++; $message = $line->description; $message = str_replace("@user_name@", $emails[$n]["name"], $message); SiteHelper::mailto($line->subject, $line->from, $emails[$n]["email"], stripslashes($message), "", "", array("<!-- @openSubscribeLink@ -->" => "<img src=\"" . Yii::app()->params["baseUrl"] . "site/subscribeOpen/subscribe/" . $line->id . "/email/" . $emails[$n]["email"] . "\" alt=\"\" style=\"width:0px;height:0px\" />")); } } // Сохраняем количество оптравленных, чтобы не считать каждый раз if ($countSend > 0) { $line->count_send += $countSend; $line->save(); } // Если адресатов нет или их количество меньше чем лимит то финализируем рассылку if (sizeof($emails) == 0 || $countSend < $countLimit) { $line->status_id = 3; $line->save(); } } }