×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: Richard Trussler
Added: Mar 21, 2018 3:00 PM
Views: 2903
Tags: no tags
  1. <?php
  2.  
  3. class DefaultController extends Controller
  4. {
  5.         public function actionIndex()
  6.         {
  7.             $this->exportKeyHolders();
  8.                 $this->render('index');
  9.         }
  10.        
  11.         public function actionTag(){
  12.             $data =  Yii::app()->db->createCommand()
  13.                     ->select('b.title as store, q.title, q.id as question_id, a.answer, a.id as answer_id, d.page_number, r.id')
  14.                     ->from('ga_answer a')
  15.                     ->join('ga_question as q', 'a.question_id=q.id')
  16.                     ->join('ga_incident_report_detail as d', 'a.id=d.answer_id')
  17.                     ->join('ga_incident_report as r', 'd.incident_report_id= r.id')
  18.                     ->join('ga_branch as b', 'r.branch_id=b.id')
  19.                     ->where('r.incident_id = 3')
  20.                     // ->order("b.ownership asc, k.position asc")
  21.                     ->queryAll();
  22.             $result = array();
  23.             foreach ($data as $d){
  24.                 $result[$d['id']]=array();
  25.             }
  26.             $questionID = array(108,109,110,111,112,113, 127,128,129,130,131);
  27.             $notcorrect = array(217,218,219);
  28.             $final = array();
  29.             foreach ($data as $i){
  30.                 $l = array();
  31.                 $l['inc_id'] = $i['id'];
  32.                 $l['store'] = $i['store'];
  33.                 $l[$i['title']] = $i['answer'];
  34.                 $l['page_number'] = $i['page_number'];
  35.                 // $l['qid'] = $i['question_id'];
  36.                 // $l['aid'] = $i['answer_id'];
  37.                    
  38.                     if(in_array($i['question_id'] ,$questionID )){
  39.                         $product = $l;
  40.                     }
  41.                     if(in_array($i['answer_id'], $notcorrect)){
  42.                         $product['status'] = $l['Tagging - Status of  tagging'];
  43.                         //array_push($result[$i['id']], $product);
  44.                         array_push($final, $product);
  45.                     }
  46.             }
  47.            
  48.             $result = array_filter($final);
  49.             $this->createCSVandEmail($result);
  50.             // $this->render('tagging', array('data'=>$result));
  51.            
  52.         }
  53.        
  54.      
  55.         public function exportKeyHolders(){
  56.            
  57.             $keyholders = Yii::app()->db->createCommand()
  58.                     ->select('b.ownership, u.first_name firstname, '
  59.                             . 'u.last_name lastname, u.mobile_number mobile,'
  60.                             . 'u.contact_number contact, '
  61.                             . 'k.position, k.branch_id branch')
  62.                     ->from('ga_key_holder k')
  63.                     ->join('ga_user as u', 'k.user_id=u.id')
  64.                     ->join('ga_branch as b', 'k.branch_id= b.id')
  65.                     ->order("b.ownership asc, k.position asc")
  66.                     ->queryAll();
  67.          
  68.            
  69.                 $doc = new DOMDocument('1.0', 'UTF-8');
  70.                 $doc->formatOutput = true;
  71.                 $root = $doc->createElement('kh:keyholders');
  72.                 $root->setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xmlns:kh', 'https://ssc-mitiesmart.com');
  73.                 $doc->appendChild($root);
  74.  
  75.                 foreach($keyholders as $key=>$value)
  76.                 {
  77.                    $kh = $doc->createElement("kh:keyholder");
  78.                    $kh = $doc->appendChild($kh);
  79.                    
  80.                    $em = $doc->createElement("kh:branch");      
  81.                    $text = $doc->createTextNode($value['ownership']);
  82.                    $em->appendChild($text);
  83.                    $kh->appendChild($em);
  84.                    $em = $doc->createElement("kh:status");      
  85.                    $text = $doc->createTextNode('Active');
  86.                    $em->appendChild($text);
  87.                    $kh->appendChild($em);
  88.                    $em = $doc->createElement("kh:priority");      
  89.                    $text = $doc->createTextNode($value['position']);
  90.                    $em->appendChild($text);
  91.                    $kh->appendChild($em);
  92.                    $em = $doc->createElement("kh:keyid");      
  93.                    $text = $doc->createTextNode('1');
  94.                    $em->appendChild($text);
  95.                    $kh->appendChild($em);
  96.                    $em = $doc->createElement("kh:name");      
  97.                    $text = $doc->createTextNode($value['firstname'] . " " . $value['lastname'] );
  98.                    $em->appendChild($text);
  99.                    $kh->appendChild($em);
  100.                    $em = $doc->createElement("kh:home");      
  101.                    $text = $doc->createTextNode("");
  102.                    $em->appendChild($text);
  103.                    $kh->appendChild($em);
  104.                    $em = $doc->createElement("kh:mobile");      
  105.                    $text = $doc->createTextNode($value['mobile']);
  106.                    $em->appendChild($text);
  107.                    $kh->appendChild($em);
  108.                    
  109.                    $root->appendChild($kh);
  110.  
  111.                 }
  112.                
  113.             $doc->save('d:\\shared\incoming\file.xml'); // change path for live system
  114.             $this->encryptFile();
  115.            
  116.             }    
  117.  
  118.             public function encryptFile(){
  119.                
  120. //                MUST CREATE KEY PAIR ON Server
  121. //                1) Generate private and public keys.
  122. //                    openssl req -x509 -nodes -days 100000 -newkey rsa:2048  -keyout privatekey.pem  -out publickey.pem  -subj ‘/’
  123. //
  124. //                2) Encrypt seemingly endless amount of data.
  125. //                    openssl  smime  -encrypt -aes256  -in  LargeFile.zip  -binary  -outform DEM  -out  LargeFile_encrypted.zip  publickey.pem
  126. //
  127. //                3) Decrypt
  128. //                    openssl  smime -decrypt  -in  LargeFile_encrypted.zip  -binary -inform DEM -inkey privatekey.pem  -out  LargeFile.zip
  129.                
  130.                 $output = shell_exec('d:\\shared\incoming\script.sh'); // change path for live system
  131.                 echo $output;
  132.  
  133.                
  134.             }
  135.            
  136.           public function createCSVandEmail($result){
  137.               var_dump($result);
  138.               // die();
  139.                 $filename = date('d-m-Y') . "_taggingData.csv";
  140.                // $live_absolute_path = "var/www/html/themes/sainsburys/export/" . $filename;
  141.                 $live_absolute_path = '/' . $filename;
  142.                 // $live_absolute_path = yii::app()->basePath . "/themes/" . $filename;
  143.                 // $test_absolute_path = "/home/sscgiirp/public_html/themes/sainsburys/export/" . $filename;
  144.                 // $test_download_path = "http://sainsburys.giirp.com/themes/sainsburys/export/" . $filename;
  145.  
  146.                 $fp = fopen($live_absolute_path , 'w');
  147.  
  148.                 fputcsv($fp, array_keys($result[0]));
  149.  
  150.                 foreach ($result as $fields) {
  151.                     fputcsv($fp, $fields);
  152.                 }
  153.  
  154.                 fclose($fp);
  155.                 // rename('/'. $filename, '/var/www/html/themes/sainsburys/export/'.$filename );
  156.  
  157. //                $message = "Hi Terry," . "\r\n" .
  158. //                        "you can access the latest copy of DWF recovery data from $live_download_path";
  159. //                $headers = 'From: noreply@testing.giirp.com' . "\r\n" .
  160. //                        'Reply-To: noreply@testing.giirp.com' . "\r\n" .
  161. //                        'X-Mailer: PHP/' . phpversion();
  162. //                mail($to, $subject, $message, $headers);
  163. //            }else{
  164. //
  165. //
  166. //                $message = "Hi Terry," . "\r\n" .
  167. //                        "The script to get the recovery data for DWF ran but didn't return any results.";
  168. //                $headers = 'From: noreply@testing.giirp.com' . "\r\n" .
  169. //                        'Reply-To: noreply@testing.giirp.com' . "\r\n" .
  170. //                        'X-Mailer: PHP/' . phpversion();
  171. //                mail($to, $subject, $message, $headers);
  172.             }
  173.  
  174.                        
  175. }