/** * 配列をCSVレコード風の文字列に変換して返します。 * @param string[] $fields * @return string */ public function convertToCSVRecord(array $fields) : string { $csv = new \SplTempFileObject(); $csv->fputcsv($fields); $csv->rewind(); return rtrim($csv->fgets(), "\r\n"); }
/** * Export Job as Tmx File * * @param $jid * @param $jPassword * @param $sourceLang * @param $targetLang * * @return SplTempFileObject $tmpFile * */ public function exportJobAsCSV($jid, $jPassword, $sourceLang, $targetLang) { $tmpFile = new SplTempFileObject(15 * 1024 * 1024); $csv_fields = array("Source: {$sourceLang}", "Target: {$targetLang}"); $tmpFile->fputcsv($csv_fields); $result = getTranslationsForTMXExport($jid, $jPassword); foreach ($result as $k => $row) { $row_array = array($row['segment'], $row['translation']); $tmpFile->fputcsv($row_array); } $tmpFile->rewind(); return $tmpFile; }
/** * Get the csv file as a string. * * @return string */ public function asString() { $tmp = new \SplTempFileObject(); foreach ($this->data as $row) { $tmp->fputcsv($row, $this->delimiter); if ($this->lineEnding !== "\n") { $tmp->fseek(-1, \SEEK_CUR); $tmp->fwrite($this->lineEnding); } } # Find out how much data we have written $length = $tmp->ftell(); if ($length < 1) { return ""; } # Reset the internal pointer and return all the data we have written $tmp->fseek(0); return $tmp->fread($length); }
public function doAction() { $data = CatUtils::getEditingLogData($this->jid, $this->password, true); $data = $data[0]; $csvHandler = new SplTempFileObject(-1); $csvHandler->setCsvControl(';'); $csv_fields = array("Job ID", "Segment ID", "Suggestion Source", "Words", "Match percentage", "Time-to-edit", "Post-editing effort", "HTER", "Segment", "Suggestion", "Translation", "MT QE", "ID translator", "Suggestion1-source", "Suggestion1-translation", "Suggestion1-match", "Suggestion1-origin", "Suggestion2-source", "Suggestion2-translation", "Suggestion2-match", "Suggestion2-origin", "Suggestion3-source", "Suggestion3-translation", "Suggestion3-match", "Suggestion3-origin", "Choosen-Suggestion", "Statistically relevant"); $csvHandler->fputcsv($csv_fields); foreach ($data as $d) { $statistical_relevant = $d['stats-valid'] == 'No' ? 0 : 1; $sid = $d['sid']; $sugg_source = $d['ss']; $rwc = $d['rwc']; $sugg_match = $d['sm']; $sugg_tte = $d['tte']; $pe_effort_perc = $d['pe_effort_perc']; $hter = $d['ter']; $segment = $d['source_csv']; $suggestion = $d['sug_csv']; $translation = $d['translation_csv']; $id_translator = $d['tid']; $s1_source = ""; $s2_source = ""; $s3_source = ""; $s1_translation = ""; $s2_translation = ""; $s3_translation = ""; $s1_match = ""; $s2_match = ""; $s3_match = ""; $s1_origin = ""; $s2_origin = ""; $s3_origin = ""; $mt_qe = $d['mt_qe']; if (!empty($d['sar'])) { $sar = json_decode($d['sar']); $s1_source = $sar[0]->segment; $s1_translation = $sar[0]->translation; $s1_match = $sar[0]->match; if (isset($sar[1])) { $s2_source = $sar[1]->segment; $s2_translation = $sar[1]->translation; $s2_match = $sar[1]->match; } if (isset($sar[2])) { $s3_source = $sar[2]->segment; $s3_translation = $sar[2]->translation; $s3_match = $sar[2]->match; } $s1_origin = substr($sar[0]->created_by, 0, 2) == 'MT' ? 'MT' : 'TM'; $s2_origin = substr($sar[1]->created_by, 0, 2) == 'MT' ? 'MT' : 'TM'; $s3_origin = substr($sar[2]->created_by, 0, 2) == 'MT' ? 'MT' : 'TM'; } $row_array = array($this->jid, $sid, $sugg_source, $rwc, $sugg_match, $sugg_tte, $pe_effort_perc, $hter, $segment, $suggestion, $translation, $mt_qe, $id_translator, $s1_source, $s1_translation, $s1_match, $s1_origin, $s2_source, $s2_translation, $s2_match, $s2_origin, $s3_source, $s3_translation, $s3_match, $s3_origin, $d['sp'], $statistical_relevant); $csvHandler->fputcsv($row_array); } $csvHandler->rewind(); foreach ($csvHandler as $row) { $this->content .= $row; } }
public function actionIndex() { $uploadedFile = CUploadedFile::getInstanceByName("blackListedFile"); if (isset($_GET['downloadSampleFile'])) { $headers = array("Mobilephone"); $contents = array(array("447123456789"), array("447123456455"), array("447123456321")); $tempFile = new SplTempFileObject(); $tempFile->fputcsv($headers); foreach ($contents as $currentLine) { $tempFile->fputcsv($currentLine); } $tempFile->rewind(); header("Content-type: text/csv"); header("Content-Disposition: attachment; filename=SampleFile.csv"); header("Pragma: no-cache"); header("Expires: 0"); $tempFile->fpassthru(); die; } if (isset($_POST['singleBlackList']) && !empty($_POST['singleBlackList'])) { $newMobileNum = new BlackListedMobile(); $newMobileNum->mobile_number = $_POST['singleBlackList']; $newMobileNum->queue_id = 1; if ($newMobileNum->save()) { Yii::app()->user->setFlash('success', '<strong>New blacklist mobile numberSaved!</strong>'); } else { Yii::app()->user->setFlash('error', '<strong>Error!</strong>' . CHtml::errorSummary($newMobileNum)); } } if (isset($_POST['massiveTextArea']) && !empty($_POST['massiveTextArea'])) { /*@TODO get total now , using randomQueue */ $queueName = $_POST['randomQueue'] ? $_POST['randomQueue'] : 'Task : upload - ' . rand(0, 9999); $criteriaJobQueue = new CDbCriteria(); $criteriaJobQueue->compare("queue_name", $queueName); $jobQueueModl = JobQueue::model()->find($criteriaJobQueue); /*@TODO $total_before*/ $total_before = 0; if (!$jobQueueModl) { $jobQueueModl = new JobQueue(); //@TODO declare above , $jobQueueModl->queue_name = $queueName; $jobQueueModl->save(false); //pre save } else { $criteriaTotalBefore = new CDbCriteria(); $criteriaTotalBefore->compare("queue_id", $jobQueueModl->queue_id); $total_before = BlackListedMobile::model()->count($criteriaTotalBefore); } /*write posted textarea to file*/ $tp = tempnam(Yii::getPathOfAlias('application.data'), "tempWrite"); $tempName = $tp . '.csv'; $linecount = 0; $tempContainer2 = explode("\n", $_POST['massiveTextArea']); $tempContainer2 = array_filter($tempContainer2); $linecount = count($tempContainer2); file_put_contents($tempName, implode(PHP_EOL, $tempContainer2)); // $handle = fopen($tempName, "r"); // while(!feof($handle)){ // $line = fgets($handle); // ++$linecount; // } // fclose($handle); $jobQueueModl->filename = $tempName; //ignore this $jobQueueModl->status = JobQueue::$JOBQUEUE_STATUS_PRELOADED; /*check number files' number of line*/ // Total_records , $jobQueueModl->total_records = $linecount; $jobQueueModl->processed_record = $linecount; /*update job queue record*/ if ($jobQueueModl->save()) { $filePath = $tempName; /*load to database*/ $tempFile2 = tempnam(__DIR__, "tempContainer"); $appendCommand = sprintf('cat "%s" | gawk \'{print $0",0%s"}\' > "%s" ', $filePath, $jobQueueModl->queue_id, $tempFile2); system($appendCommand); system("mv \"{$tempFile2}\" \"{$filePath}\""); $sqlCommand = <<<EOL LOAD DATA LOCAL INFILE "%s" INTO TABLE black_listed_mobile FIELDS TERMINATED BY "%s" LINES TERMINATED BY "%s" IGNORE 0 LINES (mobile_number,queue_id) EOL; $sqlCommand = sprintf($sqlCommand, $filePath, ',', '\\n'); $mainCommand = "mysql --user=dncsyste_dnc --password=hitman052529 --database=dncsyste_dnc -e '{$sqlCommand}'"; exec($mainCommand); $criteriaTotalBefore = new CDbCriteria(); $criteriaTotalBefore->compare("queue_id", $jobQueueModl->queue_id); /* $total_now*/ $total_now = BlackListedMobile::model()->count($criteriaTotalBefore); if (Yii::app()->request->isAjaxRequest) { header("Content-Type: application/json"); $numOfInsertedData = $total_now - $total_before; $numOfInsertedData = $numOfInsertedData <= 0 ? 0 : $numOfInsertedData; $numOfDeletedData = $linecount - ($total_now - $total_before); $numOfDeletedData = $numOfDeletedData <= 0 ? 0 : $numOfDeletedData; $jsonResult = array("numOfInsertedData" => $numOfInsertedData, "numOfDeletedData" => $numOfDeletedData); echo json_encode($jsonResult); die; } else { Yii::app()->user->setFlash('success', '<strong>File Imported!</strong>You have successfully imported new blacklisted mobile number. '); } } else { Yii::app()->user->setFlash('error', CHtml::errorSummary($newQueueFile)); } } if ($uploadedFile) { set_time_limit(0); $newFileLocation = Yii::getPathOfAlias("application.uploaded_files") . DIRECTORY_SEPARATOR . rand(0, 9999) . '-' . $uploadedFile->name; //save new queue file $newQueueFile = new JobQueue(); $uploadedFile->saveAs($newFileLocation); $newQueueFile->queue_name = "Task : " . $uploadedFile->name . ' - ' . rand(0, 9999); $newQueueFile->filename = $newFileLocation; $newQueueFile->status = JobQueue::$JOBQUEUE_STATUS_PRELOADED; /*check number files' number of line*/ $linesTotal = 0; $handle = fopen($newQueueFile->filename, "r"); while (!feof($handle)) { $line = fgets($handle); ++$linesTotal; } fclose($handle); $newQueueFile->total_records = $linesTotal; $newQueueFile->processed_record = $linesTotal; if ($newQueueFile->save()) { $filePath = $newQueueFile->filename; $tempFile2 = tempnam(__DIR__, "tempContainer"); $appendCommand = sprintf('gawk \'{print $0",0%s"}\' "%s" > "%s"', $newQueueFile->queue_id, $filePath, $tempFile2); system($appendCommand); system("mv \"{$tempFile2}\" \"{$filePath}\""); /*append queue identification for blacklist*/ $rawContents = file_get_contents($filePath); // print_r( explode("\n",$rawContents) ); // die(); $sqlCommand = <<<EOL LOAD DATA LOCAL INFILE "%s" INTO TABLE black_listed_mobile FIELDS TERMINATED BY "%s" LINES TERMINATED BY "%s" IGNORE 0 LINES (mobile_number,queue_id) EOL; $sqlCommand = sprintf($sqlCommand, $filePath, ',', '\\n'); $mainCommand = "mysql --user=dncsyste_dnc --password=hitman052529 --database=dncsyste_dnc -e '{$sqlCommand}'"; exec($mainCommand); $undoLink = "<a href='" . Yii::app()->getBaseUrl(true) . "/blacklist/delete?queue_id={$newQueueFile->queue_id}' onClick='return confirm(\"Are you sure you want to delete this ?\")'> Undo Action </a>"; Yii::app()->user->setFlash('success', '<strong>File Imported!</strong>You have successfully imported new blacklisted mobile number. ' . $undoLink); } else { Yii::app()->user->setFlash('error', CHtml::errorSummary($newQueueFile)); } } $this->render('index'); }
public function actionExportpi() { $piSitesId = array("6", "10", "3", "7", "14", "16", "17", "18", "19"); $accountIds = implode(",", $piSitesId); $fileName = date("Y-m-d") . '-All-PI-sites-entry'; header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: private", false); header("Content-Type: application/octet-stream"); header("Content-Disposition: attachment; filename=\"{$fileName}.csv\";"); header("Content-Transfer-Encoding: binary"); $results = Yii::app()->db->createCommand("SELECT * FROM claimrecord WHERE account_id IN ({$accountIds}) ORDER BY date_created DESC")->queryAll(); //write the contents to csv and passthru it $tempExportCsv = new SplTempFileObject(); foreach ($results as $currentLine) { $curArr = json_decode($currentLine['claimData'], true); $curArr = array_values($curArr); /*check if mobile number exists in the blacklist record*/ $tempExportCsv->fputcsv($curArr); } $tempExportCsv->rewind(); $tempExportCsv->fpassthru(); }