<?php
class DefaultController extends Controller
{
public function actionIndex()
{
$this->exportKeyHolders();
$this->render('index');
}
public function actionTag(){
$data = Yii::app()->db->createCommand()
->select('b.title as store, q.title, q.id as question_id, a.answer, a.id as answer_id, d.page_number, r.id')
->from('ga_answer a')
->join('ga_question as q', 'a.question_id=q.id')
->join('ga_incident_report_detail as d', 'a.id=d.answer_id')
->join('ga_incident_report as r', 'd.incident_report_id= r.id')
->join('ga_branch as b', 'r.branch_id=b.id')
->where('r.incident_id = 3')
// ->order("b.ownership asc, k.position asc")
->queryAll();
foreach ($data as $d){
$result[$d['id']]=array();
}
$questionID = array(108,109,110,111,112,113, 127,128,129,130,131);
$notcorrect = array(217,218,219);
foreach ($data as $i){
$l['inc_id'] = $i['id'];
$l['store'] = $i['store'];
$l[$i['title']] = $i['answer'];
$l['page_number'] = $i['page_number'];
// $l['qid'] = $i['question_id'];
// $l['aid'] = $i['answer_id'];
if(in_array($i['question_id'] ,$questionID )){
$product = $l;
}
if(in_array($i['answer_id'], $notcorrect)){
$product['status'] = $l['Tagging - Status of tagging'];
//array_push($result[$i['id']], $product);
}
}
$this->createCSVandEmail($result);
// $this->render('tagging', array('data'=>$result));
}
public function exportKeyHolders(){
$keyholders = Yii::app()->db->createCommand()
->select('b.ownership, u.first_name firstname, '
. 'u.last_name lastname, u.mobile_number mobile,'
. 'u.contact_number contact, '
. 'k.position, k.branch_id branch')
->from('ga_key_holder k')
->join('ga_user as u', 'k.user_id=u.id')
->join('ga_branch as b', 'k.branch_id= b.id')
->order("b.ownership asc, k.position asc")
->queryAll();
$doc = new DOMDocument('1.0', 'UTF-8');
$doc->formatOutput = true;
$root = $doc->createElement('kh:keyholders');
$root->setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xmlns:kh', 'https://ssc-mitiesmart.com');
$doc->appendChild($root);
foreach($keyholders as $key=>$value)
{
$kh = $doc->createElement("kh:keyholder");
$kh = $doc->appendChild($kh);
$em = $doc->createElement("kh:branch");
$text = $doc->createTextNode($value['ownership']);
$em->appendChild($text);
$kh->appendChild($em);
$em = $doc->createElement("kh:status");
$text = $doc->createTextNode('Active');
$em->appendChild($text);
$kh->appendChild($em);
$em = $doc->createElement("kh:priority");
$text = $doc->createTextNode($value['position']);
$em->appendChild($text);
$kh->appendChild($em);
$em = $doc->createElement("kh:keyid");
$text = $doc->createTextNode('1');
$em->appendChild($text);
$kh->appendChild($em);
$em = $doc->createElement("kh:name");
$text = $doc->createTextNode($value['firstname'] . " " . $value['lastname'] );
$em->appendChild($text);
$kh->appendChild($em);
$em = $doc->createElement("kh:home");
$text = $doc->createTextNode("");
$em->appendChild($text);
$kh->appendChild($em);
$em = $doc->createElement("kh:mobile");
$text = $doc->createTextNode($value['mobile']);
$em->appendChild($text);
$kh->appendChild($em);
$root->appendChild($kh);
}
$doc->save('d:\\shared\incoming\file.xml'); // change path for live system
$this->encryptFile();
}
public function encryptFile(){
// MUST CREATE KEY PAIR ON Server
// 1) Generate private and public keys.
// openssl req -x509 -nodes -days 100000 -newkey rsa:2048 -keyout privatekey.pem -out publickey.pem -subj ‘/’
//
// 2) Encrypt seemingly endless amount of data.
// openssl smime -encrypt -aes256 -in LargeFile.zip -binary -outform DEM -out LargeFile_encrypted.zip publickey.pem
//
// 3) Decrypt
// openssl smime -decrypt -in LargeFile_encrypted.zip -binary -inform DEM -inkey privatekey.pem -out LargeFile.zip
$output = shell_exec('d:\\shared\incoming\script.sh'); // change path for live system
echo $output;
}
public function createCSVandEmail($result){
// die();
$filename = date('d-m-Y') . "_taggingData.csv";
// $live_absolute_path = "var/www/html/themes/sainsburys/export/" . $filename;
$live_absolute_path = '/' . $filename;
// $live_absolute_path = yii::app()->basePath . "/themes/" . $filename;
// $test_absolute_path = "/home/sscgiirp/public_html/themes/sainsburys/export/" . $filename;
// $test_download_path = "http://sainsburys.giirp.com/themes/sainsburys/export/" . $filename;
$fp = fopen($live_absolute_path , 'w');
foreach ($result as $fields) {
}
// rename('/'. $filename, '/var/www/html/themes/sainsburys/export/'.$filename );
// $message = "Hi Terry," . "\r\n" .
// "you can access the latest copy of DWF recovery data from $live_download_path";
// $headers = 'From: noreply@testing.giirp.com' . "\r\n" .
// 'Reply-To: noreply@testing.giirp.com' . "\r\n" .
// 'X-Mailer: PHP/' . phpversion();
// mail($to, $subject, $message, $headers);
// }else{
//
//
// $message = "Hi Terry," . "\r\n" .
// "The script to get the recovery data for DWF ran but didn't return any results.";
// $headers = 'From: noreply@testing.giirp.com' . "\r\n" .
// 'Reply-To: noreply@testing.giirp.com' . "\r\n" .
// 'X-Mailer: PHP/' . phpversion();
// mail($to, $subject, $message, $headers);
}
}