Files
asd-control/src/Controller/EmailsController.php
2021-05-28 07:24:22 +02:00

399 lines
16 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Controller;
use App\Controller\AppController;
use Cake\Mailer\Email;
use Cake\I18n\Time;
use Cake\Event\Event;
/**
* Emails Controller
*
* @property \App\Model\Table\EmailsTable $Emails
*/
class EmailsController extends AppController
{
public function beforeFilter(Event $event) {
$this->Auth->setConfig('checkAuthIn', 'Controller.initialize');
//$this->Auth->allow('ftp');
}
public function testmail() {
$email = new Email();
$email
->emailFormat('html')
->from(['abschlusspruefung@schulen.bayern.de' => 'Abschlusspruefung'])
->to('asd.trinkl@schulen.bayern.de')
->subject('Teste E-Mail Verbindung')
->send('Das ist nur eine Testnachricht.');
$this->set('email',$email);
}
public function emailFoerder() {
debug($this->request['?']);
$filename = $this->request['?']['file'];
$date = $this->request['?']['date'];
//die;
$options = [
'length' => 0,
'delimiter' => ';',
'enclosure' => ' ',
'escape' => '\\',
'headers' => false,
'text' => false,
'excel_bom' => false,
];
$column_names = array('Regierung','SNR','Klasse','Diff','Klassengruppe','Klasse Bildungsgang','Anzahl','Schwerpunkte');
$column_alias = array('Regierung','SNR','Klasse','differenzierter Förderschwerpunkt der Klasse','Klassengruppe','Bildungsgang der Klassengruppe','Anzahl der Schüler','mit folgenden Förderschwerpunkten');
$filepath = '/var/www/html/asd-control/webroot/files/ap/'.$filename;
$handle = fopen($filepath, "r");
//$i = 1;
//$header = fgetcsv($handle, $options['length'], $options['delimiter']);
$all = array();
while ($row = fgetcsv($handle, $options['length'], $options['delimiter'])) {
//debug($row);
$data = array();
$i=0;
foreach ($column_names as $column_name) {
$data[$column_name] = trim($row[$i]);
$i++;
}
//debug($data);
array_push($all,$data);
}
//debug($all);die;
$regierungen = array();
foreach ($all as $line) {
if(!in_array($line['Regierung'],$regierungen)) {
array_push($regierungen,$line['Regierung']);
}
}
debug($regierungen);
foreach($regierungen as $regierung) {
$$regierung = array();
foreach ($all as $line) {
if ($line['Regierung'] == $regierung) {
array_shift($line);
array_push($$regierung,$line);
}
}
//debug($$regierung);
}
$column = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN');
foreach($regierungen as $regierung) {
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$spreadsheet->getProperties()->setCreator('Wilfried Trinkl StMBW')
->setLastModifiedBy('Wilfried Trinkl')
->setTitle('Förderschwerpunkt')
->setSubject('ASD Wertelisten')
->setDescription('Wertelisten created by php')
->setKeywords('Förderschwerpunkt')
->setCategory('Auswertungen');
$size = 8;
$rows = sizeof($$regierung);
//debug($rows);die;
for ($i=1;$i<8;$i++) {
// debug($column[$i-1]);
// debug($column_names[$i]);
$spreadsheet->setActiveSheetIndex(0)
->setCellValue($column[$i-1] . '1', $column_alias[$i]);
$spreadsheet->getActiveSheet()->getStyle($column[$i] . '1')->getFont()->setBold(true);
}
//die;
$j=1;
foreach ($$regierung as $line) {
//debug($line);die;
for ($i=0;$i<($size-1);$i++) {
//debug($column[$i]);
//debug($j);
//debug($line[$column_names[$i+1]]);
$spreadsheet->setActiveSheetIndex(0)->setCellValueExplicit($column[($i)] . ($j+1) , $line[$column_names[$i+1]], \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
}
$j++;
}
for ($i=0;$i<7;$i++) {
$spreadsheet->setActiveSheetIndex(0)->getColumnDimension($column[$i])->setAutoSize(true);
}
$spreadsheet->getActiveSheet()->setAutoFilter(
$spreadsheet->getActiveSheet()
->calculateWorksheetDimension()
);
//debug($regierung);
$to_mail = $regierung.'.41.ASD@schulen.bayern.de';
//debug($to_mail);
//die;
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('/test/Report_'.$regierung.'.xlsx');
$email = new Email();
$email->viewVars(['date' => $date]);
$email
->emailFormat('html')
//->from(['asd.trinkl@schulen.bayern.de' => 'ASD Trinkl'])
->from(['asd@schulen.bayern.de' => 'ASD'])
//->cc('asd.trinkl@schulen.bayern.de')
->to($to_mail)
->cc('asd.kraupner@schulen.bayern.de')
//->to($mail->snr . '@schulen.bayern.de')
//->cc('abschlusspruefung@schulen.bayern.de')
->template('foerder')
->subject('schulaufsichtliche Prüfung der Förderzentren Auswertung ' . $regierung )
->attachments('/test/Report_'.$regierung.'.xlsx')
->send();
}
//die;
debug($filename);die;
}
public function deleteall() {
$this->Emails->connection()->transactional(function ($conn) {
$sqls = $this->Emails->schema()->truncateSql($this->Emails->connection());
foreach ($sqls as $sql) {
$this->Emails->connection()->execute($sql)->execute();
}
});
}
public function sendresult() {
$this->loadModel('Offices');
$emails = $this->Emails->find('all', [
'conditions' => ['sent' => 0]
]);
foreach($emails as $mail) {
$query = $this->Offices->find('all', [
'conditions' => ['dst_schluessel' => $mail->snr]
]);
//debug($mail->snr);die;
$office = $query->first();
//debug($office->dst_name);
//die;
$email = new Email();
$email->viewVars(['content' => $mail->text]);
$email
->emailFormat('html')
->from(['abschlusspruefung@schulen.bayern.de' => 'Abschlusspruefung'])
//->to('asd.statistik@schulen.bayern.de')
->to($mail->snr . '@schulen.bayern.de')
->cc('abschlusspruefung@schulen.bayern.de')
->template('abschluss')
->subject($mail->subject . ' ' . $office->dst_name . ' - Übermittlung Abschlussprüfung Typ ' . $mail->examinationtype)
->send();
$mail['sent'] = 1;
//$line = $this->Apimports->patchEntity($line, (array) $data);
$result = $this->Emails->save($mail);
//die;
}
//die;
return $this->redirect(['controller' => 'Apresults', 'action' => 'export']);
}
public function create() {
$query = $this->Emails->find('all',[
'order' => ['lieferung DESC']
]);
$lastmail = $query->first();
if(!empty($lastmail)) {
$lastsent = $lastmail->lieferung;
}
else {
$lastsent = 0;
}
//debug($lastsent);
$this->loadModel('Apimports');
$deliveries = $this->Apimports->find('all', [
'conditions' => ["lieferung > $lastsent"],
'order' => 'lieferung ASC'
]);
$deliveries->select(['lieferung'])
->distinct(['lieferung']);
//debug($deliveries);
foreach ($deliveries as $delivery) {
//debug($delivery->lieferung);
$items = $this->Apimports->find('all',[
'conditions' => ['lieferung' => $delivery->lieferung ]
])->toArray();
$data = (object) [];
$data->examinationtype = $items[0]['pruefungsart'];
$data->snr = $items[0]['schule'];
$data->createdate = $items[0]['erzeugungsdatum'];
$data->date = Time::now();
$data->lieferung = $delivery->lieferung;
$data->sent = 0;
$data->subject = $items[0]['schule'];
$data->text = '<h2>Übermittlung der Ergebnisse der Abschlussprüfung Typ ' . $items[0]['pruefungsart'] . '</h2>';
$data->text .= '<p>Für die Schule ' . $items[0]['schule'] . ' wurden am ' . $items[0]['erzeugungsdatum']->i18nFormat('dd.MM.yyyy') . ' um ' . $items[0]['erzeugungsdatum']->i18nFormat('HH:mm') . ' Uhr folgende Ergebnisse übermittelt:</p>';
$data->text .= '<table border="1"><tr><th align="left">Anzahl</th><th align="left">Schülerstatus</th></tr>';
foreach ($items as $item) {
//debug($item);
$data->text .= '<tr><td align="left">'. $item->anzahl . '</td><td align="left">' . $item->schuelerstatus .'</td></tr>';
}
$data->text .= '</table>';
$data->text .= '<p>Bitte überprüfen Sie ob die Anzahl der Prüflinge, externen Prüfungsteilnehmer und insbesondere die der <b>Nichtschüler</b> plausibel ist.</p>';
$data->text .= '<p>Wenden Sie sich bitte umgehend an den ASV Support, falls Unstimmigkeiten auftreten sollten.</p>';
$data->text .= '<p>Ihr ASD-Team</p>';
//debug($data);
$email = $this->Emails->newEntity();
$email = $this->Emails->patchEntity($email, (array) $data);
$this->Emails->save($email);
}
//die;
return $this->redirect(['action' => 'sendresult']);
}
public function import() {
$options = [
'length' => 0,
'delimiter' => '|',
'enclosure' => ' ',
'escape' => '\\',
'headers' => false,
'text' => false,
'excel_bom' => false,
];
$this->loadModel('Apimports');
// Tabelle leeren
$this->Apimports->connection()->transactional(function ($conn) {
$sqls = $this->Apimports->schema()->truncateSql($this->Apimports->connection());
foreach ($sqls as $sql) {
$this->Apimports->connection()->execute($sql)->execute();
}
});
$filepath = '/var/www/html/asd-control/webroot/files/ap/results.csv';
$handle = fopen($filepath, "r");
//$i = 1;
//$header = fgetcsv($handle, $options['length'], $options['delimiter']);
while ($row = fgetcsv($handle, $options['length'], $options['delimiter'])) {
//debug($row);
$data = (object) [];
$data->schule = trim($row[0]);
$data->pruefungsart = trim($row[1]);
$data->anzahl = trim($row[2]);
$data->schuelerstatus = trim($row[3]);
$data->erzeugungsdatum = date('Y-m-d H:i:s', strtotime(str_replace('-', '/', trim($row[4]))));
$data->lieferung = trim($row[5]);
$line = $this->Apimports->newEntity();
$line = $this->Apimports->patchEntity($line, (array) $data);
//debug($line);
$result = $this->Apimports->save($line);
//debug($result);
//$i++;
}
//die;
return $this->redirect(['action' => 'create']);
}
/**
* Index method
*
* @return \Cake\Network\Response|null
*/
public function index()
{
$this->paginate = [
'contain' => ['Examinations']
];
$emails = $this->paginate($this->Emails);
$this->set(compact('emails'));
$this->set('_serialize', ['emails']);
}
/**
* View method
*
* @param string|null $id Email id.
* @return \Cake\Network\Response|null
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function view($id = null)
{
$email = $this->Emails->get($id, [
'contain' => ['Examinations']
]);
$this->set('email', $email);
$this->set('_serialize', ['email']);
}
/**
* Add method
*
* @return \Cake\Network\Response|null Redirects on successful add, renders view otherwise.
*/
public function add()
{
$email = $this->Emails->newEntity();
if ($this->request->is('post')) {
$email = $this->Emails->patchEntity($email, $this->request->data);
if ($this->Emails->save($email)) {
$this->Flash->success(__('The email has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The email could not be saved. Please, try again.'));
}
$examinations = $this->Emails->Examinations->find('list', ['limit' => 200]);
$this->set(compact('email', 'examinations'));
$this->set('_serialize', ['email']);
}
/**
* Edit method
*
* @param string|null $id Email id.
* @return \Cake\Network\Response|null Redirects on successful edit, renders view otherwise.
* @throws \Cake\Network\Exception\NotFoundException When record not found.
*/
public function edit($id = null)
{
$email = $this->Emails->get($id, [
'contain' => []
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$email = $this->Emails->patchEntity($email, $this->request->data);
if ($this->Emails->save($email)) {
$this->Flash->success(__('The email has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The email could not be saved. Please, try again.'));
}
$examinations = $this->Emails->Examinations->find('list', ['limit' => 200]);
$this->set(compact('email', 'examinations'));
$this->set('_serialize', ['email']);
}
/**
* Delete method
*
* @param string|null $id Email id.
* @return \Cake\Network\Response|null Redirects to index.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function delete($id = null)
{
$this->request->allowMethod(['post', 'delete']);
$email = $this->Emails->get($id);
if ($this->Emails->delete($email)) {
$this->Flash->success(__('The email has been deleted.'));
} else {
$this->Flash->error(__('The email could not be deleted. Please, try again.'));
}
return $this->redirect(['action' => 'index']);
}
}