Example #1
0
 public function deliverReport()
 {
     $this->_logger->log(__METHOD__);
     ini_set('memory_limit', '-1');
     $destinations = ScheduleReportDestination::getList($this->schedule_info->schedule_id);
     $total = count($destinations);
     $this->_logger->log(__METHOD__, array('destination_count' => $total));
     $file_path = dirname(Yii::app()->request->scriptFile) . DIRECTORY_SEPARATOR . "files" . DIRECTORY_SEPARATOR . "schedule_reports" . DIRECTORY_SEPARATOR . $this->schedule_process_info->schedule_processed_id;
     $report_type = strtoupper($this->schedule_info->report_type);
     $file_name = $this->station_info->station_id_code . '_' . $report_type . '_' . gmdate('Y-m-d_Hi', strtotime($this->schedule_process_info->check_period_end)) . '.' . $this->schedule_info->report_format;
     if (count($destinations) > 0) {
         foreach ($destinations as $i => $destination) {
             if ($destination->method === 'mail' and !$this->schedule_info->send_email_together) {
                 $this->_logger->log(__METHOD__, array('destination' => $i + 1, 'report_type' => $report_type, 'method' => $destination->method, 'email' => $destination->destination_email));
                 if ($this->schedule_info->send_like_attach) {
                     $mail_params = array('{station_id_code}' => $this->station_info->station_id_code, '{actuality_time}' => $this->schedule_process_info->created, '{schedule_period}' => Yii::app()->params['schedule_generation_period'][$this->schedule_info->period], '{report_file_name}' => $file_name, '{link}' => Yii::app()->params['site_url_for_console'] . '/site/schedulehistory/schedule_id/' . $this->schedule_info->schedule_id, '{report_type}' => $report_type);
                     $subject = Yii::t('letter', 'scheduled_report_mail_subject', $mail_params, null, 'en');
                     $body = Yii::t('letter', 'scheduled_report_mail_message', $mail_params, null, 'en');
                     It::sendLetter($destination->destination_email, $subject, $body, array(0 => array('file_path' => $file_path, 'file_name' => $file_name)));
                     $this->_logger->log(__METHOD__ . ' Message send with attached file');
                 } else {
                     $mail_params = array('{station_id_code}' => $this->station_info->station_id_code, '{actuality_time}' => $this->schedule_process_info->created, '{schedule_period}' => Yii::app()->params['schedule_generation_period'][$this->schedule_info->period], '{messages_content}' => file_get_contents($file_path), '{link}' => Yii::app()->params['site_url_for_console'] . '/site/schedulehistory/schedule_id/' . $this->schedule_info->schedule_id, '{report_type}' => $report_type);
                     $subject = Yii::t('letter', 'scheduled_report_mail_subject', $mail_params, null, 'en');
                     $body = Yii::t('letter', 'scheduled_report__messages_inside_mail_message', $mail_params, null, 'en');
                     It::sendLetter($destination->destination_email, $subject, $body);
                     $this->_logger->log(__METHOD__ . ' Message send in letter body');
                     $this->_logger->log(__METHOD__ . '     ' . file_get_contents($file_path));
                 }
                 $this->_logger->log(__METHOD__ . ' Deliver via mail DONE.');
             } else {
                 if ($destination->method === 'ftp') {
                     $this->_logger->log(__METHOD__, array('destination' => $i + 1, 'report_type' => $report_type, 'method' => $destination->method, 'destination_ip' => $destination->destination_ip, 'destination_ip_port' => $destination->destination_ip_port, 'destination_ip_user' => $destination->destination_ip_user, 'destination_ip_password' => $destination->destination_ip_password));
                     $errors = array();
                     $conn_id = @ftp_connect($destination->destination_ip, $destination->destination_ip_port);
                     if ($conn_id !== false) {
                         $result = false;
                         if (isset($destination->destination_ip_user)) {
                             $result = @ftp_login($conn_id, $destination->destination_ip_user, $destination->destination_ip_password);
                         }
                         if ($result === true) {
                             if (@ftp_chdir($conn_id, $destination->destination_ip_folder)) {
                                 $mode = $this->schedule_info->report_type === 'bufr' ? 'rb' : 'r';
                                 $fp = @fopen($file_path, $mode);
                                 if ($fp !== false) {
                                     // try to upload $file
                                     $res = @ftp_pasv($conn_id, true);
                                     if ($res === true) {
                                         $mode = $this->schedule_info->report_type == 'bufr' ? FTP_BINARY : FTP_ASCII;
                                         if (!@ftp_fput($conn_id, $file_name, $fp, $mode)) {
                                             $errors[] = 'There was a problem with uploading file ' . $file_name . ' to ftp ' . $destination->destination_ip;
                                         }
                                     } else {
                                         $errors[] = 'Can not set passive mode';
                                     }
                                     @fclose($fp);
                                 } else {
                                     $errors[] = 'Can not open stream to copy file to ' . $file_path;
                                 }
                             } else {
                                 $errors[] = 'Can not change ftp directory to ' . $destination->destination_ip_folder;
                             }
                         } else {
                             $errors[] = 'Connection with FTP ' . $destination->destination_ip . ' was failed with given login & password';
                         }
                         @ftp_close($conn_id);
                     } else {
                         $errors[] = 'Connection with FTP ' . $destination->destination_ip . ' was failed';
                     }
                     if ($errors) {
                         foreach ($errors as $error) {
                             $this->errors[] = date('Y-m-d H:i') . ' ' . $error;
                         }
                         $this->saveProcess();
                     }
                     $this->_logger->log(__METHOD__ . ' Deliver via ftp DONE.');
                 } else {
                     if ($destination->method === 'local_folder') {
                         $this->_logger->log(__METHOD__, array('destination' => $i + 1, 'report_type' => $report_type, 'method' => $destination->method, 'destination_folder' => $destination->destination_local_folder));
                         $settings = Settings::model()->findByPk(1);
                         $destinationPath = $settings->scheduled_reports_path . DIRECTORY_SEPARATOR . $destination->destination_local_folder;
                         if (!is_dir($destinationPath)) {
                             @mkdir($destinationPath, 0777, true);
                         }
                         copy($file_path, $destinationPath . DIRECTORY_SEPARATOR . $file_name);
                         $this->_logger->log(__METHOD__ . ' Deliver to local folder DONE.');
                     }
                 }
             }
         }
     }
     $this->_logger->log(__METHOD__ . ' Delivery completed.');
 }
Example #2
0
 public function actionSchedule()
 {
     /** @var ScheduleReport $form */
     /** @var ScheduleReportDestination[] $forms_d] */
     $forms_s[] = new ScheduleReportToStation();
     if (isset($_REQUEST['delete_id'])) {
         $form = ScheduleReport::model()->findByPk(intval($_REQUEST['delete_id']));
         if ($form && $form->delete()) {
             It::memStatus('schedule_deleted');
             $this->redirect($this->createUrl('site/schedule'));
         }
     }
     if (isset($_REQUEST['resend_schedule_id'])) {
         $form = ScheduleReport::model()->findByPk(intval($_REQUEST['resend_schedule_id']));
     }
     if (isset($_REQUEST['schedule_id'])) {
         $form = ScheduleReport::model()->findByPk(intval($_REQUEST['schedule_id']));
     }
     if (isset($form)) {
         $forms_d = ScheduleReportDestination::model()->findAllByAttributes(array('schedule_id' => $form->schedule_id));
         $forms_s = ScheduleReportToStation::model()->findAllByAttributes(array('schedule_id' => $form->schedule_id));
     } else {
         $form = new ScheduleReport();
         $forms_d = array();
     }
     $valid = true;
     if (Yii::app()->request->isPostRequest && isset($_POST['ScheduleReport'])) {
         $form->attributes = $_POST['ScheduleReport'];
         $valid = $valid & $form->validate();
         if (isset($_POST['ScheduleReportDestination'])) {
             foreach ($_POST['ScheduleReportDestination'] as $key => $value) {
                 if ($value['schedule_destination_id']) {
                     $forms_d[$key] = ScheduleReportDestination::model()->findByPk($value['schedule_destination_id']);
                 } else {
                     $forms_d[$key] = new ScheduleReportDestination();
                 }
                 $forms_d[$key]->attributes = $value;
                 $valid = $valid & $forms_d[$key]->validate();
             }
         }
         if (isset($_POST['ScheduleReportToStation'])) {
             $forms_s = array();
             foreach ($_POST['ScheduleReportToStation'] as $key => $value) {
                 if ($value['id']) {
                     $forms_s[$key] = ScheduleReportToStation::model()->findByPk($value['id']);
                 } elseif ($value['remove_id']) {
                     $stationToRemove = ScheduleReportToStation::model()->findByPk($value['remove_id']);
                     $stationToRemove->delete();
                     unset($forms_s[$key]);
                     continue;
                 } else {
                     $forms_s[$key] = new ScheduleReportToStation();
                 }
                 $forms_s[$key]->attributes = $value;
                 //                    $valid = $valid & $forms_s[$key]->validate();
             }
         }
         if ($valid) {
             $form->save(false);
             foreach ($forms_d as $key => $value) {
                 $forms_d[$key]->schedule_id = $form->schedule_id;
                 $forms_d[$key]->save();
             }
             foreach ($forms_s as $key => $value) {
                 $forms_s[$key]->schedule_id = $form->schedule_id;
                 $forms_s[$key]->save();
             }
             //
             It::memStatus($form->isNewRecord ? 'schedule_added' : 'schedule_updated');
             //                if ($form->isNewRecord)
             $this->redirect($this->createUrl('site/schedule'));
         }
     }
     $schedule_list = ScheduleReport::getScheduleList();
     $this->render('schedule_report', array('form' => $form, 'forms_d' => $forms_d, 'forms_s' => $forms_s, 'reportsList' => $schedule_list));
 }
<?php

$dest_types = ScheduleReportDestination::getTypes();
?>


<div class="middlewide">
    <div class="middlenarrow">
        <h1><?php 
echo It::t('home_schedule', 'title');
?>
</h1>

        <?php 
echo CHtml::beginForm($this->createUrl('site/schedule'), 'post');
?>
        <input type="hidden" name="schedule_id" value="<?php 
echo $form->schedule_id;
?>
" />
        <?php 
echo CHtml::errorSummary($form);
?>

        
        <table class="formtable">
            <tr>
                <td>
                    <?php 
echo CHtml::activeDropDownList($form, 'report_type', Yii::app()->params['schedule_report_type'], array('style' => 'width: 50px;'));
?>
 public function m_0_4_16()
 {
     @apache_setenv('no-gzip', 1);
     @ini_set('zlib.output_compression', 0);
     @ini_set('implicit_flush', 1);
     ini_set('memory_limit', '-1');
     $this->flushNotification("\n...Please wait... Updating is going on... DO NOT LEAVE THIS PAGE!");
     $this->flushNotification("<br/><br/>...Update settings table....");
     Yii::app()->db->createCommand("ALTER TABLE `settings` ADD `local_timezone_id` VARCHAR( 100 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT 'UTC'")->query();
     Yii::app()->db->createCommand("COMMIT")->query();
     $this->flushNotification("<br/><br/>...Update settings table....");
     Yii::app()->db->createCommand("ALTER TABLE `settings` ADD `local_timezone_offset` VARCHAR( 20 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '+00:00'")->query();
     Yii::app()->db->createCommand("COMMIT")->query();
     $this->flushNotification("<br/><br/>...Create new table....");
     $sql = "CREATE TABLE `schedule_report_destination` (\n                  `schedule_destination_id` int(11) NOT NULL AUTO_INCREMENT,\n                  `schedule_id` int(11) NOT NULL,\n                  `method` varchar(20) NOT NULL DEFAULT 'mail',\n                  `destination_email` varchar(255) NOT NULL,\n                  `destination_local_folder` varchar(255) NOT NULL,\n                  `destination_ip` varchar(15) NOT NULL,\n                  `destination_ip_port` smallint(5) NOT NULL DEFAULT '21',\n                  `destination_ip_folder` varchar(255) NOT NULL DEFAULT '/',\n                  `destination_ip_user` varchar(255) NOT NULL,\n                  `destination_ip_password` varchar(255) NOT NULL,\n                  PRIMARY KEY (`schedule_destination_id`),\n                  KEY `schedule_id` (`schedule_id`),\n                  CONSTRAINT `schedule_report_destination_fk` FOREIGN KEY (`schedule_id`) REFERENCES `schedule_report` (`schedule_id`) ON DELETE CASCADE ON UPDATE NO ACTION\n                ) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
     Yii::app()->db->createCommand($sql)->query();
     Yii::app()->db->createCommand("COMMIT")->query();
     $this->flushNotification("<br/><br/>...Done!");
     $this->flushNotification("<br/><br/>...Group existed schedules...");
     $sql = "SELECT * from `" . ScheduleReport::model()->tableName() . "`\n                ORDER BY station_id, report_type, report_format, period";
     $res = Yii::app()->db->createCommand($sql)->queryAll();
     if ($res) {
         $groupped = array();
         $delete = array();
         foreach ($res as $key => $value) {
             $groupped_code = $value['station_id'] . '_' . $value['report_type'] . '_' . $value['period'] . '_' . $value['report_format'];
             if (!$groupped[$groupped_code]) {
                 $groupped[$groupped_code] = array('schedule_id' => $value['schedule_id'], 'destinations' => array());
             } else {
                 $delete[] = $value['schedule_id'];
             }
             $groupped[$groupped_code]['destinations'][] = array('schedule_id' => $value['schedule_id'], 'method' => $value['method'], 'destination_email' => $value['destination_email'], 'destination_local_folder' => $value['destination_local_folder'], 'destination_ip' => $value['destination_ip'], 'destination_ip_port' => $value['destination_ip_port'], 'destination_ip_folder' => $value['destination_ip_folder'], 'destination_ip_user' => $value['destination_ip_user'], 'destination_ip_password' => $value['destination_ip_password']);
         }
         foreach ($groupped as $groupped_code => $groupped_data) {
             $schedule_id = $groupped_data['schedule_id'];
             foreach ($groupped_data['destinations'] as $key => $value) {
                 $dest = new ScheduleReportDestination();
                 $dest->schedule_id = $schedule_id;
                 $dest->method = $value['method'];
                 $dest->destination_email = $value['destination_email'];
                 $dest->destination_local_folder = $value['destination_local_folder'];
                 $dest->destination_ip = $value['destination_ip'];
                 $dest->destination_ip_port = $value['destination_ip_port'];
                 $dest->destination_ip_folder = $value['destination_ip_folder'];
                 $dest->destination_ip_user = $value['destination_ip_user'];
                 $dest->destination_ip_password = $value['destination_ip_password'];
                 $dest->save();
             }
         }
     }
     It::memStatus('update__success');
     $this->flushNotification('<script type="text/javascript"> setTimeout(function(){document.location.href="' . Yii::app()->controller->createUrl('update/index') . '"}, 5000)</script>');
 }
Example #5
0
 public function actionDeleteScheduleDestination()
 {
     $schedule_id = isset($_REQUEST['sid']) ? intval($_REQUEST['sid']) : null;
     $destination_id = isset($_REQUEST['did']) ? intval($_REQUEST['did']) : null;
     $return = array('ok' => 0);
     if ($schedule_id && $destination_id) {
         $res = ScheduleReportDestination::model()->deleteAllByAttributes(array('schedule_id' => $schedule_id, 'schedule_destination_id' => $destination_id));
         if ($res) {
             $return['ok'] = 1;
         }
     }
     print json_encode($return);
     CApplication::end();
 }
 public static function getList($schedule_id)
 {
     return ScheduleReportDestination::model()->findAllByAttributes(array('schedule_id' => $schedule_id));
 }