public function btnSave_Click($strFormId, $strControlId, $strParameter)
 {
     if ($this->txtAccessKey && $this->txtAccessKey->Text != $this->objContextInfo->SuggestionAccessKey) {
         $this->objContextInfo->SuggestionAccessKey = $this->txtAccessKey->Text;
         $this->objContextInfo->Save();
     }
     if ($this->txtCommandKey && $this->txtCommandKey->Text != $this->objContextInfo->SuggestionCommandKey) {
         $this->objContextInfo->SuggestionCommandKey = $this->txtCommandKey->Text;
         $this->objContextInfo->Save();
     }
     if ($this->txtTranslation->Text != '' && ($this->chkChanged->Checked || $this->btnSaveIgnore && $this->btnSaveIgnore->ControlId == $strControlId)) {
         if (!$this->btnSaveIgnore && !$this->Validate()) {
             $this->btnSaveIgnore_Create();
             $this->btnSaveIgnore->RemoveAllActions('click');
             $this->btnSaveIgnore->AddAction(new QClickEvent(), new QAjaxControlAction($this, 'btnSave_Click'));
             $this->btnSaveIgnore->Text = t('Ignore and save');
             $this->lblMessage->Text .= sprintf(t('Clear the textbox to skip this translation, click Ignore and save or click the save button to try again'));
             $this->btnSaveIgnore->Display = true;
             $this->chkChanged->Checked = false;
             return false;
         }
         if ($this->btnSaveIgnore) {
             $this->btnSaveIgnore->Display = false;
             $this->btnSave->Display = true;
         }
         if (!($objSuggestion = NarroSuggestion::LoadByTextIdLanguageIdSuggestionValueMd5($this->objContextInfo->Context->TextId, QApplication::GetLanguageId(), md5($this->txtTranslation->Text)))) {
             $objSuggestion = new NarroSuggestion();
             $objSuggestion->IsImported = false;
             $objSuggestion->HasComments = false;
             $objSuggestion->LanguageId = QApplication::GetLanguageId();
             $objSuggestion->TextId = $this->objContextInfo->Context->TextId;
             $objSuggestion->SuggestionValue = $this->txtTranslation->Text;
             $objSuggestion->UserId = QApplication::GetUserId();
             $objSuggestion->Save();
             $this->objContextInfo->HasSuggestions = 1;
             $this->objContextInfo->Modified = QDateTime::Now();
             $this->objContextInfo->Save();
             QApplication::ExecuteJavaScript(sprintf('jQuery(\'#%s\').after(\'&nbsp;<small style="padding: 2px;" class="ui-state-highlight ui-corner-all"><span style="width:16px; height: 16px; display:inline-block" class="ui-icon ui-icon-info"></span>&nbsp;%s.</small>\')', $this->btnHelp->ControlId, t('Translation saved')));
             if ($this->dtgTranslation) {
                 $this->dtgTranslation->MarkAsModified();
             }
         }
         if ($this->ParentControl->ParentControl->chkApprove->Checked == true) {
             $this->btnApprove_Click($strFormId, $strControlId, $objSuggestion->SuggestionId);
         } else {
             if ($this->ParentControl->ParentControl->chkRefresh->Checked && $strControlId != $this->btnKeepUntranslated->ControlId) {
                 $this->ParentControl->ParentControl->btnSearch_Click();
             }
             $this->btnHelp_Update();
             foreach ($this->Form->GetAllControls() as $ctl) {
                 if ($ctl instanceof NarroContextInfoEditor) {
                     if ($ctl->Text->Text == $this->lblText->Text) {
                         $ctl->btnHelp_Click($this->Form->FormId, $ctl->btnHelp->ControlId, '1');
                     }
                 }
             }
         }
         $this->chkChanged->Checked = false;
         $this->lblMessage->Text = '';
     } elseif ($this->txtTranslation->Text == '' && $this->objContextInfo->ValidSuggestionId) {
         $this->objContextInfo->ValidSuggestionId = null;
         $this->objContextInfo->ValidatorUserId = null;
         $this->objContextInfo->Save();
         if ($this->dtgTranslation) {
             $this->dtgTranslation->MarkAsModified();
         }
     }
     if (isset($objSuggestion)) {
         return $objSuggestion;
     } else {
         return true;
     }
 }
示例#2
0
 /**
  * A translation here consists of the project, file, text, translation, context, approval, ignore equals
  *
  * @param string $strOriginal the original text
  * @param string $strOriginalAccKey access key for the original text
  * @param string $strTranslation the translated text from the import file (can be empty)
  * @param string $strOriginalAccKey access key for the translated text
  * @param string $strContext the context where the text/translation appears in the file
  * @param string $strComment a comment from the imported file
  * @param string $strOriginalCmdKey command key for the original text
  * @param string $strTranslationCmdKey command key for the translated text
  */
 protected function AddTranslation($strOriginal, $strOriginalAccKey = null, $strTranslation, $strTranslationAccKey = null, $strContext = '', $strComment = null, $strOriginalCmdKey = null, $strTranslationCmdKey = null)
 {
     if ($strOriginal == '') {
         return false;
     }
     $blnContextInfoChanged = false;
     $blnContextChanged = false;
     $objText = $this->GetText($strOriginal);
     if (!$objText instanceof NarroText) {
         $objText = NarroText::LoadByTextValueMd5(md5($strOriginal));
     }
     if (!$this->blnOnlySuggestions && !$objText instanceof NarroText) {
         $objText = new NarroText();
         $objText->TextValue = $strOriginal;
         try {
             $objText->Save();
             // NarroLogger::LogDebug(sprintf('Added text "%s" from the file "%s"', $strOriginal, $this->objFile->FileName));
             NarroImportStatistics::$arrStatistics['Imported texts']++;
         } catch (Exception $objExc) {
             NarroLogger::LogError(sprintf('Error while adding "%s": %s', $strOriginal, $objExc->getMessage()));
             NarroImportStatistics::$arrStatistics['Skipped contexts']++;
             NarroImportStatistics::$arrStatistics['Skipped suggestions']++;
             NarroImportStatistics::$arrStatistics['Skipped texts']++;
             NarroImportStatistics::$arrStatistics['Texts that had errors while adding']++;
             /**
              * If there's no text, there's no context and no suggestion
              */
             return false;
         }
     } elseif (!$objText instanceof NarroText) {
         // NarroLogger::LogDebug('No text found');
         /**
          * If there's no text, there's no context and no suggestion
          */
         return false;
     }
     $objContext = $this->GetContext($strOriginal, $strContext, $strComment);
     if (!$objContext) {
         // NarroLogger::LogDebug('No context found, trying database');
         $objContext = NarroContext::LoadByTextIdContextMd5FileIdCommentMd5($objText->TextId, md5($strContext), $this->objFile->FileId, md5($strComment));
     }
     if (!$this->blnOnlySuggestions && !$objContext instanceof NarroContext) {
         // NarroLogger::LogDebug('Still no context, creating one');
         $objContext = new NarroContext();
         $objContext->TextId = $objText->TextId;
         $objContext->ProjectId = $this->objProject->ProjectId;
         $objContext->Context = $strContext;
         $objContext->ContextMd5 = md5($strContext);
         $objContext->FileId = $this->objFile->FileId;
         $objContext->Active = 1;
         $objContext->Modified = QDateTime::Now();
         $objContext->Created = QDateTime::Now();
         $objContext->Comment = $strComment;
         $objContext->CommentMd5 = md5($strComment);
         try {
             $objContext->Save();
         } catch (Exception $objException) {
             NarroLogger::LogError(sprintf('An error occurred while saving the context: %s. Skipping the text "%s"', $objException->getMessage(), $strOriginal));
             return false;
         }
         // NarroLogger::LogDebug(sprintf('Added the context "%s" from the file "%s"', nl2br($strContext), $this->objFile->FileName));
         NarroImportStatistics::$arrStatistics['Imported contexts']++;
         $this->arrContextId[$objContext->ContextId] = $objContext->ContextId;
     } elseif ($objContext instanceof NarroContext) {
         // NarroLogger::LogDebug('Found context');
         $this->arrContextId[$objContext->ContextId] = $objContext->ContextId;
         NarroImportStatistics::$arrStatistics['Reused contexts']++;
     } else {
         // NarroLogger::LogDebug(sprintf('Cannot add the context for text "%s" from the file "%s" because the importer is running with the option to import only suggestions.', $objText->TextValue, $this->objFile->FilePath));
         return false;
     }
     /**
      * load the context info
      */
     $objContextInfo = $this->GetContextInfo($strOriginal, $strContext, $strComment);
     if (!$objContextInfo) {
         // NarroLogger::LogDebug('No context info found, trying database');
         $objContextInfo = NarroContextInfo::LoadByContextIdLanguageId($objContext->ContextId, $this->objTargetLanguage->LanguageId);
     }
     /**
      * Add context infos even if only suggestion is selected to allow users that have permissions only on one language to approve suggestions
      */
     if (!$objContextInfo instanceof NarroContextInfo) {
         // NarroLogger::LogDebug('Still no context info, creating one');
         $objContextInfo = new NarroContextInfo();
         $objContextInfo->ContextId = $objContext->ContextId;
         $objContextInfo->LanguageId = $this->objTargetLanguage->LanguageId;
         $objContextInfo->HasSuggestions = 0;
         $objContextInfo->Created = QDateTime::Now();
         $blnContextInfoChanged = true;
     } elseif ($objContextInfo instanceof NarroContextInfo) {
         // NarroLogger::LogDebug('Found context info');
         NarroImportStatistics::$arrStatistics['Reused context informations']++;
     }
     if ($objContext instanceof NarroContext && $objContextInfo instanceof NarroContextInfo) {
         /**
          * this lies outside the if/else if reusing contexts is activated, so if a context was moved in another file, we'll just update the file_id
          */
         if ($objContext->FileId != $this->objFile->FileId) {
             // NarroLogger::LogDebug('Context changed file');
             $blnContextChanged = true;
             $objContext->FileId = $this->objFile->FileId;
         }
         if ($objContext->Active == false) {
             // NarroLogger::LogDebug('Context is inactive');
             $blnContextChanged = true;
             $objContext->Active = true;
         }
         if ($objContext->TextAccessKey != $strOriginalAccKey) {
             // NarroLogger::LogDebug('Text access key changed for this context');
             $blnContextChanged = true;
             $objContext->TextAccessKey = $strOriginalAccKey;
         }
         if ($objContext->TextCommandKey != $strOriginalCmdKey) {
             // NarroLogger::LogDebug('Text command key changed for this context');
             $blnContextChanged = true;
             $objContext->TextCommandKey = $strOriginalCmdKey;
         }
     }
     if ($strTranslation == '') {
         // NarroLogger::LogDebug('No translation');
         /**
          * just ignore, used for import without suggestions
          */
         NarroImportStatistics::$arrStatistics['Texts without suggestions']++;
     } elseif ($this->blnCheckEqual && $strOriginal == $strTranslation) {
         // NarroLogger::LogDebug(sprintf('Skipped "%s" because "%s" has the same value. From "%s".', $strOriginal, $strTranslation, $this->objFile->FileName));
         NarroImportStatistics::$arrStatistics['Skipped suggestions']++;
         NarroImportStatistics::$arrStatistics['Suggestions that kept the original text']++;
     } elseif ($this->blnImportSuggestions) {
         /**
          * See if a suggestion already exists, fetch it
          */
         $objSuggestion = $this->GetSuggestion($strOriginal, $strTranslation);
         if (!$objSuggestion) {
             // NarroLogger::LogDebug('No translation, querying the database');
             $objSuggestion = NarroSuggestion::LoadByTextIdLanguageIdSuggestionValueMd5($objText->TextId, $this->objTargetLanguage->LanguageId, md5($strTranslation));
         }
         if (!$objSuggestion instanceof NarroSuggestion) {
             // NarroLogger::LogDebug('Still no translation, creating one');
             $objSuggestion = new NarroSuggestion();
             $objSuggestion->IsImported = 1;
             $objSuggestion->UserId = $this->objUser->UserId;
             $objSuggestion->TextId = $objText->TextId;
             $objSuggestion->LanguageId = $this->objTargetLanguage->LanguageId;
             $objSuggestion->SuggestionValue = $strTranslation;
             try {
                 $objSuggestion->Save();
             } catch (Exception $objException) {
                 NarroLogger::LogError(sprintf('An error occurred while adding the suggestion "%s": %s. Skipping the text "%s"', $strTranslation, $objException->getMessage(), $strOriginal));
                 return false;
             }
             /**
              * update the HasSuggestions if it was 0 and we added a suggestion
              */
             if ($objContextInfo instanceof NarroContextInfo && $objContextInfo->HasSuggestions == 0 && $objSuggestion instanceof NarroSuggestion) {
                 $objContextInfo->HasSuggestions = 1;
                 $blnContextInfoChanged = true;
             }
             NarroImportStatistics::$arrStatistics['Imported suggestions']++;
         } else {
             // NarroLogger::LogDebug('Found translation in the database');
             NarroImportStatistics::$arrStatistics['Reused suggestions']++;
         }
         if ($objContextInfo instanceof NarroContextInfo && $this->blnApprove && (is_null($objContextInfo->ValidSuggestionId) || $this->blnApproveAlreadyApproved) && $objContextInfo->ValidSuggestionId != $objSuggestion->SuggestionId) {
             // NarroLogger::LogDebug('Approving translation');
             $objContextInfo->ValidSuggestionId = $objSuggestion->SuggestionId;
             $objContextInfo->ValidatorUserId = QApplication::GetUserId();
             $blnContextInfoChanged = true;
             NarroImportStatistics::$arrStatistics['Approved suggestions']++;
         }
         if ($objContextInfo instanceof NarroContextInfo && !is_null($strTranslationAccKey) && $objContextInfo->SuggestionAccessKey != $strTranslationAccKey) {
             // NarroLogger::LogDebug('Translation access key changed');
             $blnContextInfoChanged = true;
             $objContextInfo->SuggestionAccessKey = $strTranslationAccKey;
         }
         if ($objContextInfo instanceof NarroContextInfo && !is_null($strTranslationCmdKey) && $objContextInfo->SuggestionCommandKey != $strTranslationCmdKey) {
             // NarroLogger::LogDebug('Translation command key changed');
             $blnContextInfoChanged = true;
             $objContextInfo->SuggestionCommandKey = $strTranslationCmdKey;
         }
     }
     if ($blnContextInfoChanged && $objContextInfo instanceof NarroContextInfo) {
         // NarroLogger::LogDebug('Context info modified, saving...');
         $objContextInfo->Modified = QDateTime::Now();
         try {
             $objContextInfo->Save();
         } catch (Exception $objExc) {
             NarroLogger::LogError(sprintf('Error while saving context info for context %s: %s', $strContext, $objExc->getMessage()));
             NarroImportStatistics::$arrStatistics['Skipped context infos']++;
         }
     }
     if ($blnContextChanged && $objContext instanceof NarroContext) {
         // NarroLogger::LogDebug('Context modified, saving...');
         $objContext->Modified = QDateTime::Now();
         try {
             $objContext->Save();
         } catch (Exception $objExc) {
             NarroLogger::LogError(sprintf('Error while saving context %s: %s', $strContext, $objExc->getMessage()));
             NarroImportStatistics::$arrStatistics['Skipped contexts']++;
         }
     }
     return true;
 }
 public function btnReplace_Click()
 {
     if (!QApplication::HasPermissionForThisLang('Can approve')) {
         return false;
     }
     if ($this->txtReplace->Display == false) {
         $this->txtReplace->Display = true;
     } else {
         if ($this->txtSearch->Text == '') {
             return false;
         }
         if ($this->txtReplace->Text == '') {
             return false;
         }
         $strQuery = NarroContextInfo::GetQueryForConditions($objQueryBuilder, QQ::AndCondition($this->arrConditions), $this->arrClauses);
         $objDbResult = NarroContextInfo::GetDatabase()->Query($strQuery);
         if ($objDbResult) {
             $intReplaceCnt = 0;
             $intApproved = 0;
             $intTranslations = 0;
             $arrProcessed = array();
             while ($objDbRow = $objDbResult->GetNextRow()) {
                 $objContextInfo = NarroContextInfo::InstantiateDbRow($objDbRow, null, null, null, $objQueryBuilder->ColumnAliasArray);
                 if (in_array($objContextInfo->ContextInfoId, $arrProcessed)) {
                     continue;
                 } else {
                     $arrProcessed[] = $objContextInfo->ContextInfoId;
                 }
                 $objReplaceSuggestion = null;
                 if (preg_match("/^'.*'\$/", $this->txtSearch->Text)) {
                     $strToReplace = substr($this->txtSearch->Text, 1, -1);
                 } else {
                     $strToReplace = $this->txtSearch->Text;
                 }
                 if ($strToReplace == $objContextInfo->Context->Text->TextValue) {
                     $objReplaceSuggestion = NarroSuggestion::LoadByTextIdLanguageIdSuggestionValueMd5($objContextInfo->Context->TextId, QApplication::GetLanguageId(), md5($this->txtReplace->Text));
                     if (!$objReplaceSuggestion) {
                         $objSuggestion = new NarroSuggestion();
                         $objSuggestion->UserId = QApplication::GetUserId();
                         $objSuggestion->TextId = $objContextInfo->Context->TextId;
                         $objSuggestion->LanguageId = QApplication::GetLanguageId();
                         $objSuggestion->SuggestionValue = $this->txtReplace->Text;
                         $objSuggestion->HasComments = 0;
                         $objSuggestion->IsImported = 0;
                         $objSuggestion->Created = QDateTime::Now();
                         $objSuggestion->Modified = null;
                         $objSuggestion->Save();
                         $intTranslations++;
                         $objReplaceSuggestion = $objSuggestion;
                     }
                 }
                 if ($objReplaceSuggestion instanceof NarroSuggestion) {
                     $intReplaceCnt++;
                     if ($objContextInfo->ValidSuggestionId != $objReplaceSuggestion->SuggestionId || $this->chkApprove->Checked) {
                         if ($objContextInfo->ValidSuggestionId == null) {
                             $intApproved++;
                         }
                         $objContextInfo->ValidSuggestionId = $objReplaceSuggestion->SuggestionId;
                         $objContextInfo->ValidatorUserId = QApplication::GetUserId();
                         $objContextInfo->Modified = QDateTime::Now();
                         $objContextInfo->Save();
                     }
                 }
             }
             $this->dtrText->RemoveChildControls(true);
             $this->btnReplace->Refresh();
             QApplication::ExecuteJavaScript(sprintf('jQuery(\'#%s\').after(\'&nbsp;<small style="padding: 2px;" class="ui-state-highlight ui-corner-all"><span style="width:16px; height: 16px; display:inline-block" class="ui-icon ui-icon-info"></span>&nbsp;%s.</small>\')', $this->btnReplace->ControlId, sprintf(t('%d occurences of "%s" translated with "%s", out of which %d were already approved, %d translations added.'), $intReplaceCnt, $strToReplace, $this->txtReplace->Text, $intApproved, $intTranslations)));
         }
     }
 }
 /**
  * This Form_Validate event handler allows you to specify any custom Form Validation rules.
  * It will also Blink() on all invalid controls, as well as Focus() on the top-most invalid control.
  */
 protected function Form_Validate()
 {
     // By default, we report the result of validation from the parent
     $blnToReturn = parent::Form_Validate();
     // Custom Validation Rules
     // TODO: Be sure to set $blnToReturn to false if any custom validation fails!
     // Check for records that may violate Unique Clauses
     if (($objNarroSuggestion = NarroSuggestion::LoadByTextIdLanguageIdSuggestionValueMd5($this->lstText->SelectedValue, $this->lstLanguage->SelectedValue, $this->txtSuggestionValueMd5->Text)) && $objNarroSuggestion->SuggestionId != $this->mctNarroSuggestion->NarroSuggestion->SuggestionId) {
         $blnToReturn = false;
         $this->lstText->Warning = QApplication::Translate("Already in Use");
         $this->lstLanguage->Warning = QApplication::Translate("Already in Use");
         $this->txtSuggestionValueMd5->Warning = QApplication::Translate("Already in Use");
     }
     $blnFocused = false;
     foreach ($this->GetErrorControls() as $objControl) {
         // Set Focus to the top-most invalid control
         if (!$blnFocused) {
             $objControl->Focus();
             $blnFocused = true;
         }
         // Blink on ALL invalid controls
         $objControl->Blink();
     }
     return $blnToReturn;
 }