CMS  Version 3.9
text_lookup.inc
Go to the documentation of this file.
1 <?php
7 /**************************************************************
8 
9  Copyright (c) 2010 Sonjara, Inc
10 
11  Permission is hereby granted, free of charge, to any person
12  obtaining a copy of this software and associated documentation
13  files (the "Software"), to deal in the Software without
14  restriction, including without limitation the rights to use,
15  copy, modify, merge, publish, distribute, sublicense, and/or sell
16  copies of the Software, and to permit persons to whom the
17  Software is furnished to do so, subject to the following
18  conditions:
19 
20  The above copyright notice and this permission notice shall be
21  included in all copies or substantial portions of the Software.
22 
23  Except as contained in this notice, the name(s) of the above
24  copyright holders shall not be used in advertising or otherwise
25  to promote the sale, use or other dealings in this Software
26  without prior written authorization.
27 
28  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
30  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
32  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
33  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
34  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
35  OTHER DEALINGS IN THE SOFTWARE.
36 
37 *****************************************************************/
38 
39 Fakoli::using("email", "settings");
40 
41 class TextLookup extends DataItem
42 {
43  var $primary_key = "text_id";
44  var $table = "text_lookup";
45 
46  var $fields = array(
47  "text_id" => Number,
48  "code" => String,
49  "text" => HTML, // default language English
50  "editable" => Boolean,
51  "category" => String,
52  "class_name" => String,
53  "enabled" => Boolean
54  );
55 
56  var $versioned_fields = array("code", "text", "editable", "enabled");
57 
58  var $relations = array(
59  "Translations" => TextTranslation
60  );
61 
62 
63  function Translations($constraint = "")
64  {
65  return $this->getRelatedList(TextTranslation, "", $constraint);
66  }
67 
85  static function getText($code, $obj = null, $blank = false)
86  {
87  $texts = Query::create(TextLookup, "WHERE code=:c AND enabled=1")
88  ->bind(":c", $code)
89  ->execute();
90 
91  if(count($texts) > 0)
92  {
93  $text = $texts[0];
94 
96 
98  if(isset($obj))
99  {
100  $text = $obj->format($text->text);
102  $out = $mgr->searchAndReplace($text);
103 
104  }
105  else
106  {
107  $out = $text->text;
108  }
109 
110  if ($text->editable && Settings::getValue("settings", "enable_inline_editing") && Settings::checkPermission("settings", "editor_roles"))
111  {
112  $versioningControls = ComponentManager::fireEvent("RenderVersioningControls", $text);
113  //$versioningEnabled = Settings::getValue("settings", "enable_content_versioning") ? "1" : "0";
114 
115  $out .= $text->format("<p id='inline_editing_{code}' class='inline_editor_toolbar'>".
116  "<a href='#' class='edit' onclick='TextLookupManager.editText({text_id}); return false;'>Edit</a>".
117  "{$versioningControls}</p>");
118  //AJG - must wrap text output if inline editing is enabled
119  $out = "<div>{$out}</div>";
120  }
121 
122  return $out;
123  }
124  else if($blank = false) // return the code to signal that no value was found.
125  {
126  return $code;
127  }
128  else
129  {
130  return "";
131  }
132  }
133 
134  static function getValue($code, $obj = null, $blank = false)
135  {
136  return TextLookup::getText($code, $obj, $blank);
137  }
138 
146  static function getPlainText($code, $obj = null, $blank = false)
147  {
148  return trim(stripHTML(TextLookup::getText($code, $obj, $blank)));
149  }
150 
156  {
157  $translations = $this->Translations();
158  if(count($translations) > 0)
159  $indexed = regroupList($translations, "language");
160  else
161  $index = array();
162 
163  $options = array();
164  foreach(TextTranslation::$languageOptions as $language)
165  {
166  if($language != "English" && !array_key_exists($language, $indexed))
167  $options[$language] = $language;
168  }
169 
170  return $options;
171  }
172 
182  static function setDefaultValue($code, $value, $class_name = "")
183  {
184  $texts = Query::create(TextLookup, "WHERE code=:c")
185  ->bind(":c", $code)
186  ->execute();
187 
188  if(count($texts)) return;
189 
191  }
192 
200  static function setValue($code, $value, $class_name = "")
201  {
202  $text = new TextLookup();
203  $text->code = $code;
204  $text->text = $value;
205  $text->class_name = $class_name;
206  $text->save();
207  }
208 }
209 
210 
211 class TextTranslation extends DataItem
212 {
213  var $fields = array(
214  "text_translation_id" => Number,
215  "text_id" => String,
216  "language" => String,
217  "text" => HTML
218  );
219 
220  var $relations = array(
221  "TextLookup" => TextLookup
222  );
223 
224  /*
225  * Used by both user form, for users to specify their
226  * language and in Application Settings => Text Lookup
227  * to set the application default.
228  */
229  static $languageOptions = array(
230  "English" => "English",
231  "French" => "French",
232  "Spanish" => "Spanish",
233  "German" => "German",
234  "Russian" => "Russian"
235  );
236 
237 
238  function TextLookup()
239  {
240  return $this->getRelated(TextLookup);
241  }
242 
243  function TextTranslation()
244  {
245  $this->primary_key = "text_translation_id";
246  $this->table = "text_translation";
247 
248  $this->DataItem(func_get_args());
249  }
250 
251  /*
252  * A language specified in the user's record
253  * takes precedence over the CMS Application Setting
254  * for language.
255  *
256  * If the requested translation is not found, then
257  * return the original English text.
258  */
259  static function getTranslation($text)
260  {
261  global $user;
262 
263  if($user && $user->hasField("language") && $user->language)
264  $language = $user->language;
265  else
266  $language = Settings::getValue("text_lookup", "language");
267 
268  if(!$language || $language == "English")
269  return $text;
270  else
271  {
272  $translations = Query::create(TextTranslation, "WHERE text_id=:text_id AND language=:language")
273  ->bind(":text_id", $text->text_id, ":language", $language)
274  ->execute();
275 
276  if(count($translations) > 0 && $translations[0]->text)
277  {
278  $text->text = $translations[0]->text;
279  }
280  }
281 
282  return $text;
283  }
284 }?>
$constraint
$out
Definition: page.inc:66
static fireEvent($event, $parameter=null, $mustBeConsumed=false)
Fire an event to all subscribers as detailed in their manifests.
static using()
Import the datamodels, views and manifest for the specified component(s).
Definition: core.inc:116
static getValue($component, $name)
Retrieve the value of the specified Setting.
Definition: settings.inc:104
static checkPermission($component, $name, $account=null)
Check whether a user has a specific permission.
Definition: settings.inc:241
static setDefaultValue($code, $value, $class_name="")
Called from a component manager fired event setDefaults to set the default value for a text lookup co...
Translations($constraint="")
Definition: text_lookup.inc:63
static getPlainText($code, $obj=null, $blank=false)
Retrieve the plain text value of the text field, with any HTML and leading or trailing whitespace rem...
static getText($code, $obj=null, $blank=false)
Retrieves text for display on a page, given the code.
Definition: text_lookup.inc:85
static getValue($code, $obj=null, $blank=false)
getTranslationOptions()
Given a text record, find the remaining languages for which this text has NOT yet been translated.
static setValue($code, $value, $class_name="")
Set the string (or html) value of a lookup text code.
static getTranslation($text)
static $languageOptions
static selectDisplayVersion($target, $param="version")
global $user
$code
Definition: hide_hint.inc:36