Exemplo n.º 1
0
 public function getHTML()
 {
     $out = '<button';
     if ($this->action == self::ACTION_CANCEL) {
         $out .= ' type="reset"';
     } else {
         $out .= ' type="submit"';
     }
     $out .= UI_Link::getButtonClass($this->type);
     if (!empty($this->key)) {
         $out .= ' name="' . htmlspecialchars($this->key) . '"';
         if (!empty($this->value)) {
             $out .= ' value="' . htmlspecialchars($this->value) . '"';
         }
     }
     if (!$this->enabled) {
         $out .= ' disabled="disabled"';
     }
     if (!empty($this->id)) {
         $out .= ' id="' . htmlspecialchars($this->id) . '"';
     }
     if (!empty($this->jsEvents)) {
         $out .= ' onclick="' . $this->jsEvents . '"';
     }
     $out .= '>' . $this->label . '</button>';
     return $out;
 }
Exemplo n.º 2
0
 public static function getPage_Project($contents, $containers)
 {
     $page = self::getDataGET('p');
     $repositoryID = self::validateID(self::getDataGET('project'), true);
     $languageID = self::validateID(self::getDataGET('language'), true);
     $isAddingMode = isset($page) && $page == 'add_phrase';
     $isExportMode = isset($page) && $page == 'export';
     $isImportMode = isset($page) && $page == 'import';
     $repositoryData = Database::getRepositoryData($repositoryID);
     $languageData = Database::getLanguageData($languageID);
     if (empty($repositoryData)) {
         self::addBreadcrumbItem(URL::toProject($repositoryID), 'Project not found');
         self::setTitle('Project not found');
         $contents[] = new UI_Heading('Project not found', true);
         $contents[] = new UI_Paragraph('We\'re sorry, but we could not find the project that you requested.');
         $contents[] = new UI_Paragraph('Please check if you have made any typing errors.');
     } else {
         self::addBreadcrumbItem(URL::toProject($repositoryID), htmlspecialchars($repositoryData['name']));
         Authentication::saveCachedRepository($repositoryID, $repositoryData['name']);
         $repository = new Repository($repositoryID, $repositoryData['name'], $repositoryData['visibility'], $repositoryData['defaultLanguage']);
         $role = Database::getRepositoryRole(Authentication::getUserID(), $repositoryID);
         $permissions = $repository->getPermissions(Authentication::getUserID(), $role);
         if (Authentication::getUserID() <= 0) {
             self::setTitle($repositoryData['name']);
             $contents[] = new UI_Heading(htmlspecialchars($repositoryData['name']), true);
             $contents[] = self::getLoginForm();
         } elseif ($permissions->isInvitationMissing()) {
             self::setTitle($repositoryData['name']);
             $contents[] = new UI_Heading(htmlspecialchars($repositoryData['name']), true);
             $contents[] = self::getInvitationForm($repositoryID);
         } else {
             $defaultLanguage = new Language_Android($repository->getDefaultLanguage());
             if ($isAddingMode) {
                 $formTargetURL = URL::toAddPhrase($repositoryID, $languageID);
                 self::addBreadcrumbItem($formTargetURL, 'Add phrase');
                 $form = new UI_Form($formTargetURL, false);
                 $radioType = new UI_Form_Radio('Phrase type', 'add_phrase[type]');
                 $radioType->addOption('<abbr title="Resource type for single phrases">string</abbr>', 1, 'addPhraseTypeSelect(\'addPhraseGroup_String\');');
                 $radioType->addOption('<abbr title="Resource type for arrays of phrases">string-array</abbr>', 2, 'addPhraseTypeSelect(\'addPhraseGroup_StringArray\');');
                 $radioType->addOption('<abbr title="Resource type for quantity strings">plurals</abbr>', 3, 'addPhraseTypeSelect(\'addPhraseGroup_Plurals\');');
                 $form->addContent($radioType);
                 $textPhraseKey = new UI_Form_Text('Key', 'add_phrase[key]', 'Unique identifier', false, 'This is the short string that you\'ll identify the phrase(s) with later.');
                 $form->addContent($textPhraseKey);
                 $textPhraseStringValue = new UI_Form_Textarea('String', 'add_phrase[string]', 'String for ' . $defaultLanguage->getNameFull(), 'You can later translate this string to other languages.', false, '', 2, $defaultLanguage->isRTL(), '', 'addPhraseGroup_String');
                 $form->addContent($textPhraseStringValue);
                 $textPhraseStringArrayValue = new UI_Form_Textarea('Item', 'add_phrase[string_array][]', 'Item for ' . $defaultLanguage->getNameFull(), 'You can later translate this item to other languages.', false, '', 2, $defaultLanguage->isRTL(), '', 'addPhraseGroup_StringArray', 'display:none;', false);
                 $form->addContent($textPhraseStringArrayValue);
                 $quantities = Phrase_Android_Plurals::getList();
                 foreach ($quantities as $quantity) {
                     $textPhrasePluralsValue = new UI_Form_Textarea($quantity, 'add_phrase[plurals][' . $quantity . ']', 'Quantity for ' . $defaultLanguage->getNameFull(), 'You can later translate this quantity to other languages.', false, '', 2, $defaultLanguage->isRTL(), '', 'addPhraseGroup_Plurals', 'display:none;');
                     $form->addContent($textPhrasePluralsValue);
                 }
                 $buttonSubmit = new UI_Form_Button('Save phrase(s)', UI_Link::TYPE_SUCCESS);
                 $buttonAddItem = new UI_Link('Add item', '#', UI_Link::TYPE_INFO, 'addPhraseGroup_StringArray', 'display:none;', 'addPhraseAddItem(\'add_phrase[string_array][]\'); return false;');
                 $buttonCancel = new UI_Link('Cancel', URL::toLanguage($repositoryID, $languageID), UI_Link::TYPE_UNIMPORTANT);
                 $form->addContent(new UI_Form_ButtonGroup(array($buttonSubmit, $buttonAddItem, $buttonCancel)));
                 self::setTitle('Add phrase to default language');
                 $contents[] = new UI_Heading('Add phrase to default language', true);
                 $contents[] = $form;
             } elseif ($isExportMode) {
                 $formTargetURL = URL::toExport($repositoryID);
                 self::addBreadcrumbItem($formTargetURL, 'Export');
                 $form = new UI_Form($formTargetURL, false);
                 $textFilename = new UI_Form_Text('Filename', 'export[filename]', 'strings', false, 'Please choose a filename (without extension) for the output files that will be exported inside each language folder.');
                 $textFilename->setDefaultValue('strings');
                 $form->addContent($textFilename);
                 $selectGroupID = new UI_Form_Select('Phrase groups', 'export[groupID]', 'Do you want to export all phrases or only a single group?');
                 $selectGroupID->addOption('(All groups)', Phrase::GROUP_ALL);
                 $selectGroupID->addOption('(Default group)', Phrase::GROUP_NONE);
                 $phraseGroups = Database::getPhraseGroups($repositoryID, $repositoryData['defaultLanguage'], false);
                 foreach ($phraseGroups as $phraseGroup) {
                     $selectGroupID->addOption($phraseGroup['name'], $phraseGroup['id']);
                 }
                 $form->addContent($selectGroupID);
                 $selectMinCompletion = new UI_Form_Select('Minimum completion', 'export[minCompletion]', 'You can either export all languages or only those with a given minimum completion.');
                 $selectMinCompletion->addOption('Export all languages', 0);
                 $selectMinCompletion->addOption('5% completion or more', 5);
                 $selectMinCompletion->addOption('10% completion or more', 10);
                 $selectMinCompletion->addOption('25% completion or more', 25);
                 $selectMinCompletion->addOption('50% completion or more', 50);
                 $selectMinCompletion->addOption('75% completion or more', 75);
                 $selectMinCompletion->addOption('100% completion or more', 100);
                 $form->addContent($selectMinCompletion);
                 $selectFormat = new UI_Form_Select('Output format', 'export[format]', 'Which output format do you want to export in? When developing for Android, you should usually keep the default choice.');
                 $selectFormat->addOption('Android XML', File_IO::FORMAT_ANDROID_XML);
                 $selectFormat->addOption('Android XML with escaped HTML', File_IO::FORMAT_ANDROID_XML_ESCAPED_HTML);
                 $selectFormat->addOption('JSON', File_IO::FORMAT_JSON);
                 $selectFormat->addOption('Plaintext', File_IO::FORMAT_PLAINTEXT);
                 $form->addContent($selectFormat);
                 $ignoreIfSameAsDefault = new UI_Form_Select('Exclude defaults', 'export[ignoreIfSameAsDefault]', 'Do you want to exclude phrases which are the same as the respective phrase in the default language?');
                 $ignoreIfSameAsDefault->addOption('No, export a complete file for each language', 0);
                 $ignoreIfSameAsDefault->addOption('Yes, export only what\'s necessary', 1);
                 $form->addContent($ignoreIfSameAsDefault);
                 $isAdmin = Repository::hasUserPermissions(Authentication::getUserID(), $repositoryID, $repositoryData, Repository::ROLE_ADMINISTRATOR);
                 $buttonSubmit = new UI_Form_Button('Export', UI_Link::TYPE_SUCCESS);
                 $buttonManageGroups = new UI_Link('Manage groups', URL::toEditProject($repositoryID, true), UI_Link::TYPE_UNIMPORTANT);
                 $buttonCancel = new UI_Link('Cancel', URL::toProject($repositoryID), UI_Link::TYPE_UNIMPORTANT);
                 if ($isAdmin) {
                     $form->addContent(new UI_Form_ButtonGroup(array($buttonSubmit, $buttonManageGroups, $buttonCancel)));
                 } else {
                     $form->addContent(new UI_Form_ButtonGroup(array($buttonSubmit, $buttonCancel)));
                 }
                 self::setTitle('Export');
                 $contents[] = new UI_Heading('Export', true);
                 $contents[] = $form;
             } elseif ($isImportMode) {
                 $formTargetURL = URL::toImport($repositoryID);
                 self::addBreadcrumbItem($formTargetURL, 'Import XML');
                 $form = new UI_Form($formTargetURL, false, true);
                 $radioOverwrite = new UI_Form_Radio('Overwrite', 'import[overwrite]');
                 $radioOverwrite->addOption('<strong>Import only new phrases</strong> and do not overwrite any existing phrases', 0);
                 $radioOverwrite->addOption('<strong>Import all phrases</strong> and overwrite any phrases that do already exist', 1);
                 $form->addContent($radioOverwrite);
                 $selectLanguage = new UI_Form_Select('Language', 'import[languageID]', 'Which language do you want to import the phrases for?', false, '', '', 'if (this.value === \'' . $repositoryData['defaultLanguage'] . '\') { $(\'.import-group-id\').show(400); } else { $(\'.import-group-id\').hide(400); }');
                 $selectLanguage->addOption('— Please choose —', 0);
                 $languageIDs = Language::getList($repositoryData['defaultLanguage']);
                 foreach ($languageIDs as $languageID) {
                     $selectLanguage->addOption(Language_Android::getLanguageNameFull($languageID), $languageID);
                 }
                 $form->addContent($selectLanguage);
                 $selectGroupID = new UI_Form_Select('Phrase group', 'import[groupID]', 'Which group do you want to import the phrases to?', false, 'import-group-id', 'display:none;');
                 $selectGroupID->addOption('(Default group)', Phrase::GROUP_NONE);
                 $phraseGroups = Database::getPhraseGroups($repositoryID, $repositoryData['defaultLanguage'], false);
                 foreach ($phraseGroups as $phraseGroup) {
                     $selectGroupID->addOption($phraseGroup['name'], $phraseGroup['id']);
                 }
                 $form->addContent($selectGroupID);
                 $fileSizeHidden = new UI_Form_Hidden('MAX_FILE_SIZE', File_IO::getMaxFileSize());
                 $form->addContent($fileSizeHidden);
                 $fileXML = new UI_Form_File('XML file', 'importFileXML', 'The XML resources file that you want to extract the phrases from.');
                 $form->addContent($fileXML);
                 $isAdmin = Repository::hasUserPermissions(Authentication::getUserID(), $repositoryID, $repositoryData, Repository::ROLE_ADMINISTRATOR);
                 $buttonSubmit = new UI_Form_Button('Import XML', UI_Link::TYPE_SUCCESS);
                 $buttonManageGroups = new UI_Link('Manage groups', URL::toEditProject($repositoryID, true), UI_Link::TYPE_UNIMPORTANT);
                 $buttonCancel = new UI_Link('Cancel', URL::toProject($repositoryID), UI_Link::TYPE_UNIMPORTANT);
                 if ($isAdmin) {
                     $form->addContent(new UI_Form_ButtonGroup(array($buttonSubmit, $buttonManageGroups, $buttonCancel)));
                 } else {
                     $form->addContent(new UI_Form_ButtonGroup(array($buttonSubmit, $buttonCancel)));
                 }
                 self::setTitle('Import XML');
                 $contents[] = new UI_Heading('Import XML', true);
                 $contents[] = $form;
             } elseif (empty($languageData)) {
                 self::setTitle($repositoryData['name']);
                 $heading = new UI_Heading(htmlspecialchars($repositoryData['name']), true, 1, $repository->getShareURL());
                 $languageTable = new UI_Table(array('Language', 'Completion'));
                 $languageTable->setColumnPriorities(8, 4);
                 $languages = Language::getList($defaultLanguage->getID());
                 $cachedLanguageProgress = Authentication::getCachedLanguageProgress($repositoryID);
                 $newCachedLanguageProgress = array();
                 if (empty($cachedLanguageProgress)) {
                     $repository->loadLanguages(false, Repository::SORT_NO_LANGUAGE, Repository::LOAD_ALL_LANGUAGES);
                 }
                 foreach ($languages as $languageID) {
                     $linkURL = URL::toLanguage($repositoryID, $languageID);
                     $nameLink = new UI_Link(Language::getLanguageNameFull($languageID), $linkURL, UI_Link::TYPE_UNIMPORTANT);
                     if (empty($cachedLanguageProgress)) {
                         $languageObject = $repository->getLanguage($languageID);
                         $completeness = intval($languageObject->getCompleteness() * 100);
                     } else {
                         $completeness = intval($cachedLanguageProgress[$languageID]);
                     }
                     $progressBar = new UI_Progress($completeness);
                     $rowClass = $languageID == $defaultLanguage->getID() ? 'active' : '';
                     $languageTable->addRow(array($nameLink->getHTML(), $progressBar->getHTML()), '', $rowClass);
                     $newCachedLanguageProgress[$languageID] = $completeness;
                 }
                 Authentication::setCachedLanguageProgress($repositoryID, $newCachedLanguageProgress);
                 $actionsForm = new UI_Form(URL::toProject($repositoryID), false);
                 $actionsForm->addContent(new UI_Form_Hidden('exportXML', 1));
                 $actionButtons = array();
                 if (Repository::hasUserPermissions(Authentication::getUserID(), $repositoryID, $repositoryData, Repository::ROLE_DEVELOPER)) {
                     $actionButtons[] = new UI_Link('Export', URL::toExport($repositoryID), UI_Link::TYPE_SUCCESS);
                     $actionButtons[] = new UI_Link('Import XML', URL::toImport($repositoryID), UI_Link::TYPE_UNIMPORTANT);
                     if (Repository::hasUserPermissions(Authentication::getUserID(), $repositoryID, $repositoryData, Repository::ROLE_ADMINISTRATOR)) {
                         $actionButtons[] = new UI_Link('Edit project', URL::toEditProject($repositoryID), UI_Link::TYPE_UNIMPORTANT);
                     }
                     $actionButtons[] = new UI_Link('Add phrase', URL::toAddPhrase($repositoryID, $defaultLanguage->getID()), UI_Link::TYPE_UNIMPORTANT);
                     $actionButtons[] = new UI_Link('Watch', URL::toWatchProject($repositoryID), UI_Link::TYPE_INFO);
                 }
                 if (!empty($actionButtons)) {
                     $actionsForm->addContent(new UI_Form_ButtonGroup($actionButtons, true));
                 }
                 $contents[] = $heading;
                 $contents[] = $actionsForm;
                 $contents[] = $languageTable;
                 $contents[] = $actionsForm;
             } else {
                 try {
                     $mayMovePhrases = Repository::isRoleAllowedToMovePhrases($role);
                     $language = new Language_Android($languageID);
                     self::addBreadcrumbItem(URL::toLanguage($repositoryID, $languageID), $language->getNameFull());
                     self::setTitle($languageData->getNameFull());
                     $heading = new UI_Heading($languageData->getNameFull(), true);
                     $repository->loadLanguages(false, $languageID, $languageID);
                     $languageLeft = $repository->getLanguage($repository->getDefaultLanguage());
                     $languageRight = $repository->getLanguage($language->getID());
                     $languageLeftPhrases = $languageLeft->getPhrases();
                     $languageRightPhrases = $languageRight->getPhrases();
                     if (count($languageLeftPhrases) != count($languageRightPhrases)) {
                         throw new Exception('Count of left language\'s phrases does not match right language\'s count of phrases');
                     }
                     $phrasesTableColumnNames = array('left' => '', 'right' => '');
                     if ($language->getID() == $defaultLanguage->getID()) {
                         // viewing the default language itself
                         $phrasesTableColumnNames['left'] = 'Unique key';
                         $phrasesTableColumnNames['right'] = $language->getNameFull();
                     } else {
                         // viewing another language that will be compared to default language
                         $phrasesTableColumnNames['left'] = $defaultLanguage->getNameFull();
                         $phrasesTableColumnNames['right'] = $language->getNameFull();
                     }
                     if ($mayMovePhrases) {
                         $phrasesTable = new UI_Table(array('&nbsp;', $phrasesTableColumnNames['left'], $phrasesTableColumnNames['right']));
                         $phrasesTable->setColumnPriorities(1, 5, 6);
                     } else {
                         $phrasesTable = new UI_Table(array($phrasesTableColumnNames['left'], $phrasesTableColumnNames['right']));
                         $phrasesTable->setColumnPriorities(6, 6);
                     }
                     $translationSecret = Authentication::createSecret();
                     if (count($languageLeftPhrases) <= 0) {
                         if ($mayMovePhrases) {
                             $phrasesTable->addRow(array('', 'No phrases yet', 'No phrases yet'));
                         } else {
                             $phrasesTable->addRow(array('No phrases yet', 'No phrases yet'));
                         }
                     } else {
                         Database::initTranslationSession($repositoryID, $languageID, Authentication::getUserID(), $translationSecret, time());
                         if ($language->getID() == $defaultLanguage->getID()) {
                             // viewing the default language itself
                             /** @var Phrase $defaultPhrase */
                             foreach ($languageLeftPhrases as $defaultPhrase) {
                                 $values = $defaultPhrase->getPhraseValues();
                                 foreach ($values as $subKey => $value) {
                                     $phraseFormKey = 'updatePhrases[edits][' . URL::encodeID($defaultPhrase->getID()) . '][' . $subKey . ']';
                                     $value = Authentication::getCachedEdit($repositoryID, $languageID, URL::encodeID($defaultPhrase->getID()), $subKey, $value);
                                     $valuePrevious = new UI_Form_Hidden(str_replace('[edits]', '[previous]', $phraseFormKey), $value);
                                     $valueEdit = new UI_Form_Textarea('', $phraseFormKey, $value, '', true, htmlspecialchars($value), UI_Form_Textarea::getOptimalRowCount($value, 2), $defaultLanguage->isRTL(), 'setUnsavedChanges(true);');
                                     if ($mayMovePhrases) {
                                         $editPhraseLink = new UI_Link('Edit', URL::toPhraseDetails($repositoryID, $languageID, $defaultPhrase->getID()), UI_Link::TYPE_UNIMPORTANT);
                                         $editPhraseLink->setSize(UI_Link::SIZE_SMALL);
                                         $editPhraseLink->setTabIndex('-1');
                                         $phrasesTable->addRow(array($editPhraseLink->getHTML(), Phrase_Android::getFullyQualifiedName($defaultPhrase, $subKey), $valuePrevious->getHTML() . $valueEdit->getHTML()));
                                     } else {
                                         $phrasesTable->addRow(array(Phrase_Android::getFullyQualifiedName($defaultPhrase, $subKey), $valuePrevious->getHTML() . $valueEdit->getHTML()));
                                     }
                                 }
                             }
                         } else {
                             // viewing another language that will be compared to default language
                             foreach ($languageRightPhrases as $rightPhrase) {
                                 /** @var Phrase $rightPhrase */
                                 $defaultPhrase = $languageLeft->getPhraseByKey($rightPhrase->getPhraseKey());
                                 $valuesLeft = $defaultPhrase->getPhraseValues();
                                 $valuesRight = $rightPhrase->getPhraseValues();
                                 foreach ($valuesLeft as $subKey => $valueLeft) {
                                     $valueLeftHTML = '<span dir="' . ($defaultLanguage->isRTL() ? 'rtl' : 'ltr') . '">' . nl2br(htmlspecialchars($valueLeft)) . '</span><span dir="ltr" class="small" style="display:block; color:#999; margin-top:4px;">' . Phrase_Android::getFullyQualifiedName($defaultPhrase, $subKey) . '</span>';
                                     $phraseKey = 'updatePhrases[edits][' . URL::encodeID($defaultPhrase->getID()) . '][' . $subKey . ']';
                                     $valuesRight[$subKey] = Authentication::getCachedEdit($repositoryID, $languageID, URL::encodeID($defaultPhrase->getID()), $subKey, $valuesRight[$subKey]);
                                     $valuePrevious = new UI_Form_Hidden(str_replace('[edits]', '[previous]', $phraseKey), $valuesRight[$subKey]);
                                     $valueEdit = new UI_Form_Textarea('', $phraseKey, $valuesRight[$subKey], '', true, htmlspecialchars($valuesRight[$subKey]), UI_Form_Textarea::getOptimalRowCount($valueLeft), $language->isRTL(), 'setUnsavedChanges(true);');
                                     if ($mayMovePhrases) {
                                         $editPhraseLink = new UI_Link('Edit', URL::toPhraseDetails($repositoryID, $languageID, $defaultPhrase->getID()), UI_Link::TYPE_UNIMPORTANT);
                                         $editPhraseLink->setSize(UI_Link::SIZE_SMALL);
                                         $editPhraseLink->setTabIndex('-1');
                                         $phrasesTable->addRow(array($editPhraseLink->getHTML(), $valueLeftHTML, $valuePrevious->getHTML() . $valueEdit->getHTML()));
                                     } else {
                                         $phrasesTable->addRow(array($valueLeftHTML, $valuePrevious->getHTML() . $valueEdit->getHTML()));
                                     }
                                 }
                             }
                         }
                     }
                     $formTargetURL = URL::toLanguage($repositoryID, $languageID);
                     $addPhraseURL = URL::toAddPhrase($repositoryID, $languageID);
                     $saveButton = new UI_Form_Button('Save changes', UI_Link::TYPE_SUCCESS, UI_Form_Button::ACTION_SUBMIT, '', '', 'setUnsavedChanges(false);');
                     if ($mayMovePhrases) {
                         $formButtonList = array($saveButton, new UI_Link('Add phrase', $addPhraseURL, UI_Link::TYPE_UNIMPORTANT));
                     } else {
                         $formButtonList = array($saveButton);
                     }
                     $formButtons = new UI_Form_ButtonGroup($formButtonList, true);
                     $phrasesTable->setShowSearchFilter(Authentication::isUserDeveloper());
                     $form = new UI_Form($formTargetURL, false);
                     $form->addContent($formButtons);
                     $form->addContent($phrasesTable);
                     $form->addContent(new UI_Form_Hidden('updatePhrases[secret]', $translationSecret));
                     $form->addContent(new UI_Form_Hidden('updatePhrases[translatorID]', Authentication::getUserID()));
                     $form->addContent($formButtons);
                     $contents[] = $heading;
                     $contents[] = $form;
                 } catch (Exception $e) {
                     self::addBreadcrumbItem(URL::toLanguage($repositoryID, $languageID), 'Language not found');
                     self::setTitle('Language not found');
                     $contents[] = new UI_Heading('Language not found', true);
                     $contents[] = new UI_Paragraph('We\'re sorry, but we could not find the language that you requested.');
                 }
             }
         }
     }
     $cell = new UI_Cell($contents);
     $row = new UI_Row(array($cell));
     $containers[] = new UI_Container(array($row));
     return new UI_Group($containers);
 }