public function actionXmlfeed() { $report = array(); $model = $this->loadModel(Yii::app()->user->id); if (isset($_REQUEST['xml_path']) and !empty($_REQUEST['xml_path'])) { $xml = @file_get_contents($_REQUEST['xml_path']); $xml = trim($xml); $xml_read = @simplexml_load_string($xml); if (false !== $xml_read and false !== $xml) { $xml_items = array(); $lists = XMLFeedItem::generateLists(); foreach ($xml_read->offer as $item) { $xml_items[] = XMLFeedItem::populate($item, $lists); } $report['errors'] = array(); $report['success'] = 0; $report['fail'] = 0; $report['total'] = count($xml_items); foreach ($xml_items as $item) { if ($item->validate()) { if (Apartment::saveFromXml($item)) { $report['success']++; } else { $report['fail']++; } } else { $report['errors'][$item->internal_id] = CHtml::errorSummary($item, '', ''); $report['fail']++; } } $new_feed = new ApartmentXMLFeed(); $new_feed->owner_id = yii::app()->user->id; $new_feed->report = json_encode($report); $new_feed->xml = $xml; $new_feed->save(); if (!empty($report['errors'])) { Yii::import('application.extensions.mailer.EMailer'); $mailer = new EMailer(); if (param('mailUseSMTP', 0)) { $mailer->IsSMTP(); $mailer->SMTPAuth = true; $mailer->Host = param('mailSMTPHost', 'localhost'); $mailer->Port = param('mailSMTPPort', 25); $mailer->Username = param('mailSMTPLogin'); // SMTP login $mailer->Password = param('mailSMTPPass'); // SMTP password $mailer->SMTPSecure = param('mailSMTPSecure'); } $mailer->From = param('adminEmail'); $mailer->FromName = param('mail_fromName', User::getAdminName()); $mailer->AddAddress($model->email); $mailer->Subject = 'Результаты импорта объектов'; $mailer->Body = $this->renderPartial('_mailReport', array('report' => $report), true); $mailer->CharSet = 'UTF-8'; $mailer->IsHTML(true); if (!$mailer->Send()) { throw new CHttpException(503, tt('message_not_send', 'notifier') . ' ErrorInfo: ' . $mailer->ErrorInfo); //showMessage(tc('Error'), tt('message_not_send', 'notifier')); } } } } $this->render('xmlfeed', ['model' => $model, 'report' => $report, 'feeds' => ApartmentXMLFeed::model()->mine()->findAll(array('limit' => 5, 'order' => 'id DESC')), 'baseURL' => Yii::app()->theme->baseUrl . '/views/modules/usercpanel']); }
private function _processEvent($rule, $model, $to, $toModerators = false) { $user = $this->getFromParam('user'); if ($this->sendToAdmin) { $lang = 'admin'; } else { $lang = Yii::app()->user->checkAccess('backend_access') ? 'default' : 'current'; } $body = ''; if (isset($rule['body'])) { $body = $rule['body']; $body = str_replace('{host}', IdnaConvert::checkDecode(Yii::app()->request->hostInfo), $body); $body = str_replace('{fullhost}', IdnaConvert::checkDecode(Yii::app()->getBaseUrl(true)), $body); if ($user && !isset($model->username) && !isset($model->ownerName)) { $body = str_replace('{username}', $user->username, $body); } if (isset($rule['url']) && $model) { $params = array(); if (isset($rule['url'][1])) { foreach ($rule['url'][1] as $param) { $params[$param] = $model->{$param}; } $params['lang'] = $lang; } $url = Yii::app()->controller->createAbsoluteUrl($rule['url'][0], $params); $body = str_replace('{url}', IdnaConvert::checkDecode($url), $body); } if (isset($rule['fields']) && $model) { foreach ($rule['fields'] as $field) { $val = isset($model->{$field}) ? $model->{$field} : tc('No information'); $body = str_replace('{' . $field . '}', IdnaConvert::checkDecode($val), $body); } } if (isset($rule['i18nFields']) && $model) { foreach ($rule['i18nFields'] as $field) { $field_val = $model->{$field}; $body = str_replace('{' . $field . '}', isset($field_val[$lang]) ? CHtml::encode($field_val[$lang]) : tc('No information'), $body); } } $body = str_replace("\n.", "\n..", $body); } if ($body) { Yii::import('application.extensions.mailer.EMailer'); $mailer = new EMailer(); if (param('mailUseSMTP', 0)) { $mailer->IsSMTP(); $mailer->SMTPAuth = true; $mailer->Host = param('mailSMTPHost', 'localhost'); $mailer->Port = param('mailSMTPPort', 25); $mailer->Username = param('mailSMTPLogin'); // SMTP login $mailer->Password = param('mailSMTPPass'); // SMTP password $mailer->SMTPSecure = param('mailSMTPSecure'); } $mailer->From = param('adminEmail'); $mailer->FromName = param('mail_fromName', User::getAdminName()); $mailer->AddAddress($to); if ($toModerators && issetModule('rbac')) { $moderators = User::model()->findAllByAttributes(array('role' => User::ROLE_MODERATOR)); if ($moderators && is_array($moderators)) { foreach ($moderators as $moderator) { if (isset($moderator->email) && $moderator->email) { $mailer->AddAddress($moderator->email); } } } } if (isset($rule['subject'])) { $mailer->Subject = $rule['subject']; } $mailer->Body = $body; $mailer->CharSet = 'UTF-8'; $mailer->IsHTML(true); if (!$mailer->Send()) { throw new CHttpException(503, tt('message_not_send', 'notifier') . ' ErrorInfo: ' . $mailer->ErrorInfo); //showMessage(tc('Error'), tt('message_not_send', 'notifier')); } } }