Exemplo n.º 1
0
 function export($id = 0)
 {
     $costingDb = $this->Costing->findById($id);
     $this->checkCanDo($costingDb);
     App::uses('ExcelLib', 'Lib');
     $costingRecord = $this->Costing->getCostingRecord($costingDb);
     $data = $costingRecord['excelData'];
     $excel = new ExcelLib();
     $excel->init();
     $excel->writeFromArray($data);
     $excel->PHPExcel->getActiveSheet()->setTitle('Costing');
     $excel->PHPExcel->setActiveSheetIndex(0);
     $excel->send2Browser();
     die;
 }
Exemplo n.º 2
0
 public function importCms()
 {
     $this->MultiLanguageModel->useTable = false;
     $listLanguage = array_keys(array_merge(Configure::read('MultiLanguage.fallback'), Configure::read('MultiLanguage.list')));
     $msgError = '';
     if (isset($this->request->params['form']['MultilanguageImportCms'])) {
         if (isset($this->request->params['form']['MultilanguageImportCms']['tmp_name']) && is_file($this->request->params['form']['MultilanguageImportCms']['tmp_name'])) {
             if (($handle = fopen($this->request->params['form']['MultilanguageImportCms']['tmp_name'], "r")) !== FALSE) {
                 App::uses('ExcelLib', 'Lib');
                 $excel = new ExcelLib();
                 $excel->initFromFile($this->request->params['form']['MultilanguageImportCms']['tmp_name']);
                 $listData = $excel->getAllRowsCurrentSheet();
                 if (count($listData) > 0) {
                     $mapping = Configure::read('MultiLanguage.app_mapping_list');
                     for ($ii = 1; $ii < count($listData[0]); $ii++) {
                         $listData[0][$ii] = isset($mapping[strtolower($listData[0][$ii])]) ? $mapping[strtolower($listData[0][$ii])] : $listData[0][$ii];
                     }
                     $data = $listData[0];
                     if ($data[0] != 'Key' || count(array_intersect($listLanguage, $data)) != count($listLanguage)) {
                         $msgError = __('The header of Excel file shoud be in format') . ': Key';
                         foreach ($listLanguage as $lang) {
                             $msgError .= ', ' . $lang;
                         }
                     } else {
                         $listLanguage = $data;
                         $listKey = $this->getCMSLanguages(true);
                         $numField = count($listLanguage);
                         for ($row = 1; $row < count($listData); $row++) {
                             $data = $listData[$row];
                             $num = count($data);
                             if ($num != $numField || !isset($listKey[$data[0]])) {
                                 $msgError .= __('Cannot import line %s', $row) . "<br />";
                             } else {
                                 for ($c = 1; $c < $num; $c++) {
                                     $listKey[$data[0]][$listLanguage[$c]] = $data[$c];
                                 }
                             }
                         }
                         $dataSave = array();
                         foreach ($listKey as $key => $translates) {
                             foreach ($translates as $lang => $text) {
                                 $dataSave[$lang][$key] = trim($text);
                             }
                         }
                         foreach ($dataSave as $lang => $data) {
                             $file = ROOT . '/app/Locale/' . $lang . '/LC_MESSAGES/default.po';
                             $str = '';
                             foreach ($data as $key => $val) {
                                 $str .= "msgid   \"{$key}\"\nmsgstr  \"{$val}\"\n\n";
                             }
                             file_put_contents($file, $str);
                         }
                     }
                     fclose($handle);
                 }
             } else {
                 $msgError = __('Cannot open the uploaded file');
             }
         } else {
             $msgError = __('Cannot open the uploaded file');
         }
         if (empty($msgError)) {
             $this->Session->setFlash(__('Import successfully'), 'flash/success');
             $this->redirect(array('plugin' => 'MultiLanguage', 'controller' => 'MultiLanguage', 'action' => 'updateCms'));
         } else {
             $this->Session->setFlash($msgError, 'flash/error');
         }
     }
 }