Index: branches/5.1.x/core/kernel/languages/phrases_cache.php =================================================================== diff -u -N -r12453 -r12657 --- branches/5.1.x/core/kernel/languages/phrases_cache.php (.../phrases_cache.php) (revision 12453) +++ branches/5.1.x/core/kernel/languages/phrases_cache.php (.../phrases_cache.php) (revision 12657) @@ -1,6 +1,6 @@ Conn =& $this->Application->GetADODBConnection(); + + if (defined('DEBUG_MODE') && DEBUG_MODE && $this->Application->IsAdmin()) { + // only has effect in admin, because on front-end phrases are translated in "Content Mode" + $this->_editMissing = defined('DBG_PHRASES') && DBG_PHRASES; + } + + // now we use admin phrase editing template even on front-end + $this->_phraseEditTemplate = 'regional/phrases_edit'; } + /** + * Sets phrase editing mode, that corresponds current editing mode + * + */ + function setPhraseEditing() + { + if (!$this->Application->IsAdmin() && (EDITING_MODE == EDITING_MODE_CONTENT)) { + $this->_editExisting = true; + $this->_editMissing = true; + $this->_simpleEditingMode = true; + $this->_translateHtmlTag = 'span'; + } + } + function Init($prefix, $special = '') { if (constOn('IS_INSTALL')) { @@ -62,11 +128,13 @@ function GetCachedIds() { - $query = sprintf("SELECT PhraseList, ConfigVariables FROM %s WHERE Template = %s", - TABLE_PREFIX.'PhraseCache', - $this->Conn->Qstr(md5($this->Application->GetVar('t').$this->Application->GetVar('m_theme').$this->Application->GetVar('m_lang')))); - $res = $this->Conn->GetRow($query); + $cache_key = md5($this->Application->GetVar('t') . $this->Application->GetVar('m_theme') . $this->Application->GetVar('m_lang')); + $sql = 'SELECT PhraseList, ConfigVariables + FROM ' . TABLE_PREFIX . 'PhraseCache + WHERE Template = ' . $this->Conn->qstr($cache_key); + $res = $this->Conn->GetRow($sql); + if ($res && $res['ConfigVariables']) { $this->Application->OriginalConfigCacheIds = explode(',', $res['ConfigVariables']); $this->Application->ConfigCacheIds = $this->Application->OriginalConfigCacheIds; @@ -77,24 +145,30 @@ function LoadPhrases($ids) { - if ( !is_array($ids) || !implode('', $ids) ) return; - $query = sprintf("SELECT Translation,UPPER(Phrase) AS Phrase FROM %s WHERE LanguageId = %s AND PhraseId IN (%s)", - TABLE_PREFIX.'Phrase', - $this->LanguageId, - join(',', $ids)); - $this->Phrases = $this->Conn->GetCol($query,'Phrase'); + if ( !is_array($ids) || !implode('', $ids) ) { + return; + } + + $sql = 'SELECT Translation, UPPER(Phrase) AS Phrase + FROM ' . TABLE_PREFIX . 'Phrase + WHERE (LanguageId = ' . $this->LanguageId . ') AND PhraseId IN (' . implode(',', $ids) . ')'; + $this->Phrases = $this->Conn->GetCol($sql, 'Phrase'); + /*foreach($phrases as $phrase => $tanslation) { $this->AddCachedPhrase(mb_strtoupper($phrase), $tanslation); }*/ + $this->Ids = $ids; $this->OriginalIds = $ids; } - function AddCachedPhrase($label, $value) + function AddCachedPhrase($label, $value, $allow_editing = true) { - $label = mb_strtoupper($label); - $this->Phrases[$label] = $value; + // uppercase phrase name for cases, when this method is called outside this class + $cache_key = ($allow_editing ? '' : 'NE:') . mb_strtoupper($label); + + $this->Phrases[$cache_key] = $value; } function NeedsCacheUpdate() @@ -125,11 +199,13 @@ } } - function GetPhrase($label) + function GetPhrase($label, $allow_editing = true) { - if (!isset($this->LanguageId)) return 'impossible case'; - //actually possible when custom field contains references to language labels and its being rebuilt in OnAfterConfigRead - //which is triggered by Sections rebuild, which in turn read all the configs and all of that happens BEFORE seeting the language... + if (!isset($this->LanguageId)) { + //actually possible when custom field contains references to language labels and its being rebuilt in OnAfterConfigRead + //which is triggered by Sections rebuild, which in turn read all the configs and all of that happens BEFORE seeting the language... + return 'impossible case'; + } // cut exclamation marks - depricated form of passing phrase name from templates $label = preg_replace('/^!(.*)!$/', '\\1', $label); @@ -140,81 +216,64 @@ $original_label = $label; $label = mb_strtoupper($label); - if(isset($this->Phrases[$label])) { - $translated_label = $this->Phrases[$label]; - // debug mode is checked directly to improve performance - if (defined('DEBUG_MODE') && DEBUG_MODE && constOn('DBG_PHRASES_HIGHLIGHT')) { - if (!$this->Application->isDebugMode()) return $translated_label; - if ($this->Application->IsAdmin()) { - $sql = 'SELECT Module - FROM '.TABLE_PREFIX.'Phrase - WHERE (LanguageId = '.$this->LanguageId.') AND (Phrase = '.$this->Conn->qstr($label).')'; - $this->Application->Debugger->appendHTML('Phrase: '.$label.'; Module: '.$this->Conn->GetOne($sql).''); -// $translated_label = $translated_label.' [m: '.$this->Conn->GetOne($sql).'; l: '.$label.']'; - } - else { - // highlight text created via translated phrase (used to detect if text on screen is phrase or not) - $translated_label = ''.$translated_label.' '.$original_label.''; - } + $cache_key = ($allow_editing ? '' : 'NE:') . $label; - } + if (array_key_exists($cache_key, $this->Phrases)) { + $translated_label = $this->Phrases[$cache_key]; - if (defined('DEBUG_MODE') && DEBUG_MODE && - (($this->Application->IsAdmin() && constOn('DBG_PHRASES_EDIT_ADMIN')) || (!$this->Application->IsAdmin() && constOn('DBG_PHRASES_EDIT_FRONT')) ) ) { + if ($this->_editExisting && $allow_editing && !array_key_exists($label, $this->_missingPhrases)) { // option to change translation for Labels - list($edit_tpl, $index_file) = $this->Application->IsAdmin() ? Array('regional/phrases_edit', 'index.php') : Array('phrases_edit', 'index.php'); + $edit_url = 'javascript:translate_phrase(\'' . addslashes($original_label) . '\', \'' . $this->_phraseEditTemplate . '\', {event: \'OnPrepareUpdate\', simple_mode: ' . ($this->_simpleEditingMode ? 'true' : 'false') . '});'; + $translated_label = '<' . $this->_translateHtmlTag . ' href="' . $edit_url . '" name="cms-translate-phrase" title="Edit translation">' . $translated_label . '_translateHtmlTag . '>'; - if ($this->Application->IsAdmin() && $this->Application->ConfigValue('UsePopups')) { - // link to popup when using popups (only in admin) - $edit_url = 'javascript:translate_phrase(\''.addslashes($translated_label).'\', \''.$edit_tpl.'\');'; + if ($this->fromTag) { + $translated_label = $this->escapeTagReserved($translated_label); } - else { - // direct link, when not using popups OR on frontend - $edit_url = $this->Application->HREF($edit_tpl,'',Array('m_opener'=>'d','phrases_label'=>$original_label,'phrases_event'=>'OnPrepareUpdate', 'pass'=>'all,phrases'), $index_file ); - } - $translated_label = '!'.$translated_label.'!'; } return $translated_label; } - $this->LoadPhraseByLabel($label, $original_label); - return $this->GetPhrase($label); + $this->LoadPhraseByLabel($label, $original_label, $allow_editing); + + return $this->GetPhrase($label, $allow_editing); } - function LoadPhraseByLabel($label, $original_label) + function LoadPhraseByLabel($label, $original_label, $allow_editing = true) { - $query = sprintf("SELECT PhraseId, Translation FROM %s WHERE LanguageId = %s AND UPPER(Phrase) = UPPER(%s)", - TABLE_PREFIX.'Phrase', - $this->LanguageId, - $this->Conn->qstr($label)); - $res = $this->Conn->GetRow($query); - if ($res === false || count($res) == 0) - { - $translation = '!'.$label.'!'; - if($this->Application->isDebugMode() && constOn('DBG_PHRASES')) { - list($edit_tpl, $index_file) = $this->Application->IsAdmin() ? Array('regional/phrases_edit', 'index.php') : Array('phrases_edit', 'index.php'); + // bug: MySQL don't use index on Phrase column, when function is used on it's value (e.g. UPPER, like in this case) + $sql = 'SELECT PhraseId, Translation + FROM ' . TABLE_PREFIX . 'Phrase + WHERE (LanguageId = ' . $this->LanguageId . ') AND (PhraseKey = ' . $this->Conn->qstr($label) . ')'; + $res = $this->Conn->GetRow($sql); - if ($this->Application->IsAdmin() && $this->Application->ConfigValue('UsePopups')) { - // link to popup when using popups (only in admin) - $edit_url = 'javascript:translate_phrase(\''.addslashes($original_label).'\', \''.$edit_tpl.'\');'; + if ($res === false || count($res) == 0) { + $translation = '!' . $label . '!'; + + if ($this->_editMissing && $allow_editing) { + $edit_url = 'javascript:translate_phrase(\'' . addslashes($original_label) . '\', \'' . $this->_phraseEditTemplate . '\', {event: \'OnNew\', simple_mode: ' . ($this->_simpleEditingMode ? 'true' : 'false') . '});'; + $translation = '<' . $this->_translateHtmlTag . ' href="' . $edit_url . '" name="cms-translate-phrase" title="Translate">!' . $label . '!_translateHtmlTag . '>'; + + if ($this->fromTag) { + $translation = $this->escapeTagReserved($translation); } - else { - // direct link, when not using popups OR on frontend - $edit_url = $this->Application->HREF($edit_tpl,'',Array('m_opener'=>'d','phrases_label'=>$original_label,'phrases_event'=>'OnNew', 'pass'=>'all,phrases'), $index_file ); - } - $translation = '!'.$label.'!'; - if($this->fromTag) $translation = $this->escapeTagReserved($translation); + $this->_missingPhrases[$label] = true; // add as key for faster accessing } - $this->AddCachedPhrase($label, $translation); //add it as already cached, as long as we dont need to cache not found phrase + + // add it as already cached, as long as we dont need to cache not found phrase + $this->AddCachedPhrase($label, $translation, $allow_editing); + return false; } - $this->Phrases[$label] = $res['Translation']; + $cache_key = ($allow_editing ? '' : 'NE:') . $label; + $this->Phrases[$cache_key] = $res['Translation']; + array_push($this->Ids, $res['PhraseId']); - $this->Ids = array_unique($this->Ids); //just to make sure + $this->Ids = array_unique($this->Ids); // just to make sure + return true; } @@ -242,23 +301,29 @@ * @return string * @access public */ - function ReplaceLanguageTags($text,$forse_escaping=null) + function ReplaceLanguageTags($text, $forse_escaping = null) { $this->fromTag = true; - if( isset($forse_escaping) ) $this->fromTag = $forse_escaping; + if( isset($forse_escaping) ) { + $this->fromTag = $forse_escaping; + } + preg_match_all("(!(la|lu)[^!]+!)", $text, $res, PREG_PATTERN_ORDER); $language_tags = $res[0]; - uasort($language_tags, Array(&$this, 'CmpParams') ); + uasort($language_tags, Array(&$this, 'CmpParams')); - $values = Array(); $i = 0; + $values = Array(); + foreach ($language_tags as $label) { array_push($values, $this->GetPhrase($label) ); //array_push($values, $this->Application->Phrase($label) ); $language_tags[$i] = '/' . $language_tags[$i] . '/'; $i++; } + $this->fromTag = false; + return preg_replace($language_tags, $values, $text); } @@ -276,7 +341,4 @@ return str_replace($reserved,$replacement,$text); } -} - - -?> \ No newline at end of file +} \ No newline at end of file