예제 #1
0
 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']);
 }