示例#1
0
 /**
  * Method to save data
  *
  * @param  array  the data to save
  * @return  boolean  success or failure
  */
 public function save($data)
 {
     // Get the package name
     $name = $data['name'];
     // Get the package
     $package = $this->getItem();
     $path = JPATH_COMPONENT_ADMINISTRATOR . "/packages/{$name}.xml";
     $manifest = $package->manifest ? $package->manifest : 'fil_localise_package_' . $name;
     $client = $package->client ? $package->client : 'site';
     if ($package->standalone) {
         $title = $package->title ? $package->title : 'fil_localise_package_' . $name;
         $description = $package->description ? $package->description : 'fil_localise_package_' . $name . '_desc';
         // Prepare text to save for the xml package description
         $text = '';
         $text .= '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
         $text .= '<package>' . "\n";
         $text .= '<title>' . $title . '</title>' . "\n";
         $text .= '<description>' . $description . '</description>' . "\n";
         $text .= '<manifest client="' . $client . '">' . $manifest . '</manifest>' . "\n";
         $text .= '<icon>' . $data['icon'] . '</icon>' . "\n";
         $text .= '<author>' . $data['author'] . '</author>' . "\n";
         $text .= '<copyright>' . $data['copyright'] . '</copyright>' . "\n";
         $text .= '<license>' . $data['license'] . '</license>' . "\n";
         $administrator = array();
         $site = array();
         $installation = array();
         foreach ($data['translations'] as $translation) {
             if (preg_match('/^site_(.*)$/', $translation, $matches)) {
                 $site[] = $matches[1];
             }
             if (preg_match('/^administrator_(.*)$/', $translation, $matches)) {
                 $administrator[] = $matches[1];
             }
             if (preg_match('/^installation_(.*)$/', $translation, $matches)) {
                 $installation[] = $matches[1];
             }
         }
         if (count($site)) {
             $text .= '<site>' . "\n";
             foreach ($site as $translation) {
                 $text .= '<filename>' . $translation . '.ini</filename>' . "\n";
             }
             $text .= '</site>' . "\n";
         }
         if (count($administrator)) {
             $text .= '<administrator>' . "\n";
             foreach ($administrator as $translation) {
                 $text .= '<filename>' . $translation . '.ini</filename>' . "\n";
             }
             $text .= '</administrator>' . "\n";
         }
         if (count($installation)) {
             $text .= '<installation>' . "\n";
             foreach ($installation as $translation) {
                 $text .= '<filename>' . $translation . '.ini</filename>' . "\n";
             }
             $text .= '</installation>' . "\n";
         }
         $text .= '</package>' . "\n";
         // Set FTP credentials, if given.
         JClientHelper::setCredentialsFromRequest('ftp');
         $ftp = JClientHelper::getCredentials('ftp');
         // Try to make the file writeable.
         if ($exists && !$ftp['enabled'] && JPath::isOwner($path) && !JPath::setPermissions($path, '0644')) {
             $this->setError(JText::sprintf('COM_LOCALISE_ERROR_PACKAGE_WRITABLE', $path));
             return false;
         }
         $return = JFile::write($path, $text);
         // Try to make the file unwriteable.
         if (!$ftp['enabled'] && JPath::isOwner($path) && !JPath::setPermissions($path, '0444')) {
             $this->setError(JText::sprintf('COM_LOCALISE_ERROR_PACKAGE_UNWRITABLE', $path));
             return false;
         } else {
             if (!$return) {
                 $this->setError(JText::sprintf('COM_LOCALISE_ERROR_PACKAGE_FILESAVE', $path));
                 return false;
             }
         }
     }
     // Save the title and the description in the language file
     $translation_path = LocaliseHelper::findTranslationPath($client, JFactory::getLanguage()->getTag(), $manifest);
     $translation_id = LocaliseHelper::getFileId($translation_path);
     $translation_model = JModelLegacy::getInstance('Translation', 'LocaliseModel', array('ignore_request' => true));
     if ($translation_model->checkout($translation_id)) {
         $translation_model->setState('translation.path', $translation_path);
         $translation_model->setState('translation.client', $client);
         $translation = $translation_model->getItem();
         $sections = LocaliseHelper::parseSections($translation_path);
     } else {
     }
     $text = '';
     $text .= strtoupper($title) . '="' . str_replace('"', '"_QQ_"', $data['title']) . "\"\n";
     $text .= strtoupper($description) . '="' . str_replace('"', '"_QQ_"', $data['description']) . "\"\n";
     $tag = JFactory::getLanguage()->getTag();
     $languagePath = JPATH_SITE . "/language/{$tag}/{$tag}.{$manifest}.ini";
     // Try to make the file writeable.
     if ($exists && !$ftp['enabled'] && JPath::isOwner($languagePath) && !JPath::setPermissions($languagePath, '0644')) {
         $this->setError(JText::sprintf('COM_LOCALISE_ERROR_PACKAGE_WRITABLE', $languagePath));
         return false;
     }
     $return = JFile::write($languagePath, $text);
     // Try to make the file unwriteable.
     if (!$ftp['enabled'] && JPath::isOwner($languagePath) && !JPath::setPermissions($languagePath, '0444')) {
         $this->setError(JText::sprintf('COM_LOCALISE_ERROR_PACKAGE_UNWRITABLE', $languagePath));
         return false;
     } else {
         if (!$return) {
             $this->setError(JText::sprintf('COM_LOCALISE_ERROR_PACKAGE_FILESAVE', $languagePath));
             return false;
         }
     }
     $id = LocaliseHelper::getFileId($path);
     $this->setState('package.id', $id);
     // Bind the rules.
     $table = $this->getTable();
     $table->load($id);
     if (isset($data['rules'])) {
         $rules = new JAccessRules($data['rules']);
         $table->setRules($rules);
     }
     // Check the data.
     if (!$table->check()) {
         $this->setError($table->getError());
         return false;
     }
     // Store the data.
     if (!$table->store()) {
         $this->setError($table->getError());
         return false;
     }
     return true;
 }
示例#2
0
 /**
  * Method to allow derived classes to preprocess the form.
  *
  * @param  object  A form object.
  * @param  mixed  The data expected for the form.
  * @param  string  The name of the plugin group to import (defaults to "content").
  * @throws  Exception if there is an error in the form event.
  * @since  1.6
  */
 protected function preprocessForm(JForm $form, $item, $group = 'content')
 {
     // Initialize variables.
     $filename = $this->getState('translation.filename');
     $client = $this->getState('translation.client');
     $tag = $this->getState('translation.tag');
     $origin = LocaliseHelper::getOrigin($filename, $client);
     $app = JFactory::getApplication();
     $false = false;
     // Compute all known languages
     static $languages = array();
     jimport('joomla.language.language');
     if (!array_key_exists($client, $languages)) {
         $languages[$client] = JLanguage::getKnownLanguages(constant('LOCALISEPATH_' . strtoupper($client)));
     }
     if (is_object($item)) {
         $form->setFieldAttribute('legend', 'unchanged', $item->unchanged, 'legend');
         $form->setFieldAttribute('legend', 'translated', $item->translated, 'legend');
         $form->setFieldAttribute('legend', 'untranslated', $item->total - $item->translated - $item->unchanged, 'legend');
         $form->setFieldAttribute('legend', 'extra', $item->extra, 'legend');
     }
     if ($this->getState('translation.layout') != 'raw') {
         $path = $this->getState('translation.path');
         $refpath = $this->getState('translation.refpath');
         $sections = LocaliseHelper::parseSections($path);
         $refsections = LocaliseHelper::parseSections($refpath);
         $addform = new JXMLElement('<form />');
         $group = $addform->addChild('fields');
         $group->addAttribute('name', 'strings');
         $fieldset = $group->addChild('fieldset');
         $fieldset->addAttribute('name', 'Default');
         $fieldset->addAttribute('label', 'Default');
         if (JFile::exists($refpath)) {
             $stream = new JStream();
             $stream->open($refpath);
             $header = true;
             $lineNumber = 0;
             while (!$stream->eof()) {
                 $line = $stream->gets();
                 $lineNumber++;
                 // Blank lines
                 if (preg_match('/^\\s*$/', $line)) {
                     $header = true;
                     $field = $fieldset->addChild('field');
                     $field->addAttribute('label', '');
                     $field->addAttribute('type', 'spacer');
                     $field->addAttribute('class', 'text');
                     continue;
                 } elseif (preg_match('/^\\[([^\\]]*)\\]\\s*$/', $line, $matches)) {
                     $header = false;
                     $form->load($addform, false);
                     $section = $matches[1];
                     $addform = new JXMLElement('<form />');
                     $group = $addform->addChild('fields');
                     $group->addAttribute('name', 'strings');
                     $fieldset = $group->addChild('fieldset');
                     $fieldset->addAttribute('name', $section);
                     $fieldset->addAttribute('label', $section);
                     continue;
                 } elseif (!$header && preg_match('/^;(.*)$/', $line, $matches)) {
                     $key = $matches[1];
                     $field = $fieldset->addChild('field');
                     $field->addAttribute('label', $key);
                     $field->addAttribute('type', 'spacer');
                     $field->addAttribute('class', 'text');
                     continue;
                 } elseif (preg_match('/^([A-Z][A-Z0-9_\\-\\.]*)\\s*=/', $line, $matches)) {
                     $header = false;
                     $key = $matches[1];
                     $field = $fieldset->addChild('field');
                     $string = $refsections['keys'][$key];
                     $translated = isset($sections['keys'][$key]);
                     $modified = $translated && $sections['keys'][$key] != $refsections['keys'][$key];
                     $status = $modified ? 'translated' : ($translated ? 'unchanged' : 'untranslated');
                     $default = $translated ? $sections['keys'][$key] : '';
                     $label = '<b>' . $key . '</b><br />' . htmlspecialchars($string, ENT_COMPAT, 'UTF-8');
                     $field->addAttribute('status', $status);
                     $field->addAttribute('description', $string);
                     if ($default) {
                         $field->addAttribute('default', $default);
                     } else {
                         $field->addAttribute('default', $string);
                     }
                     $field->addAttribute('label', $label);
                     $field->addAttribute('name', $key);
                     $field->addAttribute('type', 'key');
                     $field->addAttribute('filter', 'raw');
                     continue;
                 } elseif (!preg_match('/^(|(\\[[^\\]]*\\])|([A-Z][A-Z0-9_\\-\\.]*\\s*=(\\s*(("[^"]*")|(_QQ_)))+))\\s*(;.*)?$/', $line)) {
                     $this->item->error[] = $lineNumber;
                 }
             }
             $stream->close();
             $newstrings = false;
             if (!empty($sections['keys'])) {
                 foreach ($sections['keys'] as $key => $string) {
                     if (!isset($refsections['keys'][$key])) {
                         if (!$newstrings) {
                             $newstrings = true;
                             $form->load($addform, false);
                             $section = 'New Strings';
                             $addform = new JXMLElement('<form />');
                             $group = $addform->addChild('fields');
                             $group->addAttribute('name', 'strings');
                             $fieldset = $group->addChild('fieldset');
                             $fieldset->addAttribute('name', $section);
                             $fieldset->addAttribute('label', $section);
                         }
                         $field = $fieldset->addChild('field');
                         $status = 'extra';
                         $default = $string;
                         $label = '<b>' . $key . '</b>';
                         $field->addAttribute('status', $status);
                         $field->addAttribute('description', $string);
                         if ($default) {
                             $field->addAttribute('default', $default);
                         } else {
                             $field->addAttribute('default', $string);
                         }
                         $field->addAttribute('label', $label);
                         $field->addAttribute('name', $key);
                         $field->addAttribute('type', 'key');
                         $field->addAttribute('filter', 'raw');
                     }
                 }
             }
         }
         $form->load($addform, false);
     }
     // Check the session for previously entered form data.
     $data = $app->getUserState('com_localise.edit.translation.data', array());
     // Bind the form data if present.
     if (!empty($data)) {
         $form->bind($data);
     }
     if ($origin != '_thirdparty' && $origin != '_override') {
         $packages = LocaliseHelper::getPackages();
         $package = $packages[$origin];
         if (!empty($package->author)) {
             $form->setValue('author', $package->author);
             $form->setFieldAttribute('author', 'readonly', 'true');
         }
         if (!empty($package->copyright)) {
             $form->setValue('maincopyright', $package->copyright);
             $form->setFieldAttribute('maincopyright', 'readonly', 'true');
         }
         if (!empty($package->license)) {
             $form->setValue('license', $package->license);
             $form->setFieldAttribute('license', 'readonly', 'true');
         }
     }
     if ($form->getValue('description') == '' && array_key_exists($tag, $languages[$client])) {
         $form->setValue('description', $filename . ' ' . $languages[$client][$tag]['name']);
     }
     return $form;
 }