Exemplo n.º 1
0
 /**
  * @see Description in abstract base class
  */
 public function fromArray(array $data)
 {
     $resultData = array();
     $resultTmx = new Util_TmxFile();
     foreach ($data as $translationUnit => $variants) {
         foreach ($variants as $language => $contents) {
             if (is_array($contents) && isset($contents['filename']) && isset($contents['contents'])) {
                 $filePath = $this->contentBasepath . DIRECTORY_SEPARATOR . $contents['filename'];
                 $resultData[$filePath] = $contents['contents'];
                 $contents = $contents['filename'];
             }
             $resultTmx->setVariantSegment($translationUnit, $language, $contents);
         }
     }
     $this->setContent($resultData);
     $this->setTranslation($resultTmx->toArray());
 }
Exemplo n.º 2
0
 /**
  * Store all tmx and content that has been set.
  * This is performed as a transaction, reverting 
  * changes already written if one write fails.
  * 
  * @return bool Returns true on success, false on failure.
  */
 public function store()
 {
     $result = true;
     $tmxBackup = $savedContent = array();
     try {
         if (!empty($this->tmxContents)) {
             $this->verifyWriteAccess($this->tmxTarget);
             $tmxFile = new Util_TmxFile();
             if (is_file($this->tmxTarget)) {
                 $tmxFile->load($this->tmxTarget);
                 // backup in case a write operation fails
                 $tmxBackup = $tmxFile->toArray();
             }
             $tmxFile->fromArray($this->tmxContents);
             $result = $tmxFile->save($this->tmxTarget);
             if (!$result) {
                 throw new Setup_Model_Exception("Saving File '{$this->tmxTarget}' failed");
             }
         }
         foreach ($this->contentSources as $filename => $contents) {
             if (!is_null($contents)) {
                 $this->verifyWriteAccess($filename);
                 // backup stored content to restore in case a write operation fails
                 $savedContent[$filename] = file_get_contents($filename);
                 $result = false !== file_put_contents($filename, $contents) && $result;
                 if (!$result) {
                     throw new Setup_Model_Exception("Saving File '{$filename}' failed");
                 }
             }
         }
     } catch (Setup_Model_Exception $se) {
         if (!empty($tmxBackup)) {
             $tmxFile = new Util_TmxFile();
             $tmxFile->fromArray($tmxBackup);
             $tmxFile->save($this->tmxTarget);
         }
         if (!empty($savedContent)) {
             foreach ($savedContent as $filename => $contents) {
                 file_put_contents($filename, $contents);
             }
         }
         $this->log($se->getMessage());
         $result = false;
     }
     return $result;
 }
Exemplo n.º 3
0
 /**
  * Returns translations in modules set via @see setModules()
  * and (optionally) matching string set via @see setFilter()
  * 
  * @param string $sortKey   Key used to sort result array. 
  *                          Valid keys are defined as class constants
  * @param int $sortOrder    Sort order as expected by @see array_multisort()
  * @throw Setup_Model_FileNotReadableException Thrown if loading tmx file(s) fails.
  */
 public function getTranslations($sortKey = self::SORT_UNIT, $sortOrder = SORT_ASC)
 {
     $fileData = $this->getFiles();
     $translations = array();
     $sortArray = array();
     foreach ($fileData as $module => $files) {
         foreach ($files as $dir => $filenames) {
             foreach ($filenames as $fileName) {
                 $relativeFilePath = "{$module}/{$dir}/{$fileName}";
                 $filePath = APPLICATION_PATH . "/modules/{$relativeFilePath}";
                 $tmxFile = new Util_TmxFile();
                 if ($tmxFile->load($filePath)) {
                     $translationUnits = $tmxFile->toArray();
                     foreach ($translationUnits as $key => $values) {
                         if (empty($this->filter) || strpos($key, $this->filter) !== false) {
                             foreach ($values as $lang => $value) {
                                 $row = array('unit' => $key, 'module' => $module, 'directory' => $dir, 'filename' => $fileName, 'language' => $lang, 'variant' => $value);
                                 $translations[] = $row;
                                 $sortArray[] = $row[$sortKey];
                             }
                         }
                     }
                 } else {
                     throw new Setup_Model_FileNotReadableException($filePath);
                 }
             }
         }
     }
     array_multisort($sortArray, $sortOrder, SORT_STRING, $translations);
     return $translations;
 }
Exemplo n.º 4
0
 /**
  * @see Description in abstract base class
  */
 public function fromArray(array $data)
 {
     $resultData = array();
     $resultTmx = new Util_TmxFile();
     foreach ($data as $language => $fields) {
         foreach ($fields as $key => $val) {
             switch ($key) {
                 case 'file':
                     if (!is_array($val) || !isset($val['filename']) || !isset($val['contents'])) {
                         throw new Setup_Model_Exception('Invalid data structure');
                     }
                     $filePath = $this->contentBasepath . DIRECTORY_SEPARATOR . $val['filename'];
                     $resultData[$filePath] = $val['contents'];
                     break;
                 case 'key':
                     foreach ($val as $translationUnit => $variant) {
                         $resultTmx->setVariantSegment($translationUnit, $language, $variant);
                     }
                     break;
                 default:
                     throw new Setup_Model_Exception('Failed loading array. Invalid data structure.');
             }
         }
     }
     $this->setContent($resultData);
     $this->setTranslation($resultTmx->toArray());
 }