BMLT Root Server
c_comdef_formats.class.php
Go to the documentation of this file.
1 <?php
2 /***********************************************************************/
3 /** \file c_comdef_formats.class.php
4  \brief The class file for the c_comdef_formats class.
5 
6  This file is part of the Basic Meeting List Toolbox (BMLT).
7 
8  Find out more at: https://bmlt.app
9 
10  BMLT is free software: you can redistribute it and/or modify
11  it under the terms of the MIT License.
12 
13  BMLT is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  MIT License for more details.
17 
18  You should have received a copy of the MIT License along with this code.
19  If not, see <https://opensource.org/licenses/MIT>.
20 */
21 
22 defined('BMLT_EXEC') or die('Cannot Execute Directly'); // Makes sure that this file is in the correct context.
23 
24 /// Include the format class.
25 require_once(dirname(__FILE__)."/c_comdef_format.class.php");
26 require_once(dirname(__FILE__)."/../shared/classes/comdef_utilityclasses.inc.php");
27 
28 /***********************************************************************/
29 /** \class c_comdef_formats
30  \brief A Class for Aggregating Format Codes
31 
32  This class acts as an array aggregator for c_comdef_format objects.
33  It is a class unto itself, because we want to have a couple of
34  additional filtering options (like the ability to get a bunch of
35  formats that share a _shared_id_bigint code).
36 
37  The formats are kept in a simple two-dimensional array:
38 
39  \code
40  $_local_copy_of_array = array[$lang_enum][$shared_id_bigint]
41  \endcode
42 
43  The combination of shared ID and language needs to be unique. This is how
44  we match formats to languages. The same shared ID is used throughout
45  the system, and we use the localization to return its key and description
46  in the appropriate language.
47 ***********************************************************************/
48 // phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace
49 // phpcs:disable Squiz.Classes.ValidClassName.NotCamelCaps
51 // phpcs:enable PSR1.Classes.ClassDeclaration.MissingNamespace
52 // phpcs:enable Squiz.Classes.ValidClassName.NotCamelCaps
53 {
54  /// This is the parent (container) object that holds this instance.
55  private $_local_id_parent_obj = null;
56 
57  /*******************************************************************/
58  /** We keep a local copy of the simple array, because we can instantly
59  access it, as opposed to having to instantiate iterators.
60  */
61  private $_local_copy_of_array = null;
62 
63  /*******************************************************************/
64  /** \brief Constructor. Sets the parent object.
65  */
66  public function __construct(
67  $in_parent_obj,
68  $in_array
69  ) {
70  $this->SetParentObj($in_parent_obj);
71  $this->_local_copy_of_array = $in_array;
72  }
73 
74  /*******************************************************************/
75  /** \brief Accessor -Get the format object array.
76 
77  \returns a reference to an array of c_comdef_format objects.
78  */
79  // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
80  public function GetFormatsArray()
81  {
82  // phpcs:enable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
83  return $this->_local_copy_of_array;
84  }
85 
86  /*******************************************************************/
87  /** \brief Return a reference to a single object, by format key and
88  language.
89 
90  You do not need to provide a language, in which case, the server's
91  local language is used.
92 
93  \returns A reference to the single selected object.
94  */
95  // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
96  public function GetFormatByKeyAndLanguage(
97  $in_format_key, ///< This is the shared ID code.
98  $in_lang_enum = null ///< This is the code for the desired language. If not given, the server localization will be used.
99  ) {
100  // phpcs:enable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
101  /// If no language is given, we use the server's native language.
102  if (null == $in_lang_enum) {
103  $in_lang_enum = $this->GetParentObj()->GetLocalLang();
104  }
105 
106  if (!$in_lang_enum) {
107  $in_lang_enum = c_comdef_server::GetServer()->GetLocalLang();
108  }
109 
110  // Should never happen.
111  if (!$in_lang_enum) {
112  $in_lang_enum = "en";
113  }
114 
115  if (isset($this->_local_copy_of_array[$in_lang_enum]) && is_array($this->_local_copy_of_array[$in_lang_enum]) && count($this->_local_copy_of_array[$in_lang_enum])) {
116  foreach ($this->_local_copy_of_array[$in_lang_enum] as &$format) {
117  if ($in_format_key == $format->GetKey()) {
118  return $format;
119  };
120  }
121  }
122 
123  return null;
124  }
125 
126  /*******************************************************************/
127  /** \brief Return a reference to a single object, by shared ID and
128  language.
129 
130  You do not need to provide a language, in which case, the server's
131  local language is used.
132 
133  \returns A reference to the single selected object.
134  */
135  // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
137  $in_shared_id_bigint, ///< This is the shared ID code.
138  $in_lang_enum = null ///< This is the code for the desired language. If not given, the server localization will be used.
139  ) {
140  // phpcs:enable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
141  /// If no language is given, we use the server's native language.
142  if (null == $in_lang_enum) {
143  $in_lang_enum = $this->GetParentObj()->GetLocalLang();
144  }
145 
146  if (!$in_lang_enum) {
147  $in_lang_enum = c_comdef_server::GetServer()->GetLocalLang();
148  }
149 
150  // Should never happen.
151  if (!$in_lang_enum) {
152  $in_lang_enum = "en";
153  }
154 
155  if (isset($this->_local_copy_of_array[$in_lang_enum][$in_shared_id_bigint])) {
156  return $this->_local_copy_of_array[$in_lang_enum][$in_shared_id_bigint];
157  }
158  }
159 
160  /*******************************************************************/
161  /** \brief Return all the formats that share a given shared ID code.
162 
163  \returns A simple array of references to the objects for the formats
164  in all languages for the given shared ID. Null if no formats fit the language.
165  */
166  // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
167  public function GetFormatsBySharedIDCode(
168  $in_shared_id_bigint ///< This is the shared ID code.
169  ) {
170  // phpcs:enable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
171  $ret = null;
172 
173  foreach ($this->_local_copy_of_array as $lang) {
174  foreach ($lang as $id) {
175  if ($id->GetSharedID() == $in_shared_id_bigint) {
176  $ret[$id->GetLocalLang()] = $id;
177  }
178  }
179  }
180 
181  return $ret;
182  }
183 
184  /*******************************************************************/
185  /** \brief Return all the formats that share a given language.
186 
187  You do not need to provide a language, in which case, the server's
188  local language is used.
189 
190  \returns A simple array of references to the objects for the formats
191  in the given language. Null if no formats fit the language.
192  */
193  // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
194  public function GetFormatsByLanguage(
195  $in_lang_enum = null ///< This is the code for the desired language. If not given, the server localization will be used.
196  ) {
197  // phpcs:enable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
198  $ret = null;
199 
200  /// If no language is given, we use the server's native language.
201  if (null == $in_lang_enum) {
202  $in_lang_enum = $this->GetParentObj()->GetLocalLang();
203  }
204 
205  if (isset($this->_local_copy_of_array) && is_array($this->_local_copy_of_array) && isset($this->_local_copy_of_array[$in_lang_enum]) && is_array($this->_local_copy_of_array)) {
206  $ret = $this->_local_copy_of_array[$in_lang_enum];
207  }
208 
209  return $ret;
210  }
211 
212  /*******************************************************************/
213  /** \brief Return all the formats that contain the given string.
214 
215  If the language is English or Spanish, this does a metaphone search of all
216  all the strings in the "en" or "es" formats. If it is any other language, then
217  a simple string search is performed.
218 
219  \returns A simple array of references to the objects for the formats
220  in all languages for the given string. Null if no formats fit.
221  */
222  // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
223  public function GetFormatsByString(
224  $in_string, ///< This is the string to search for.
225  $in_lang_enum = null ///< This is the code for the desired language. If not given, the server localization will be used.
226  ) {
227  // phpcs:enable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
228  $ret = null;
229 
230  /// If no language is given, we use the server's native language.
231  if (null == $in_lang_enum) {
232  $in_lang_enum = $this->GetParentObj()->GetLocalLang();
233  }
234 
235  /// We force the search to happen in lowercase. This is a very basic search.
236  $in_string = strtolower($in_string);
237 
238  /// If we will use metaphone, we convert our search criteria to metaphone keys.
239  $in_string_comp = SplitIntoMetaphone($in_string, $in_lang_enum);
240 
241  $count = 0;
242  $iterating_target = $this->GetFormatsByLanguage($in_lang_enum);
243  foreach ($iterating_target as $id) {
244  $found = false;
245 
246  /// We will look in each of the text fields for our string.
247  $string = $id->GetKey();
248 
249  $comp = SplitIntoMetaphone($string, $in_lang_enum);
250 
251  foreach ($in_string_comp as $test) {
252  if (array_search($test, $comp)) {
253  $found = true;
254  break;
255  }
256  }
257 
258  if (!$found) {
259  $string = $id->GetLocalName();
260 
261  $comp = SplitIntoMetaphone($string, $in_lang_enum);
262 
263  foreach ($in_string_comp as $test) {
264  if (array_search($test, $comp)) {
265  $found = true;
266  break;
267  }
268  }
269  }
270 
271  if (!$found) {
272  $string = $id->GetLocalDescription();
273 
274  $comp = SplitIntoMetaphone($string, $in_lang_enum);
275 
276  foreach ($in_string_comp as $test) {
277  if (array_search($test, $comp)) {
278  $found = true;
279  break;
280  }
281  }
282  }
283 
284  if ($found) {
285  $ret[$count++] = $id;
286  }
287  }
288 
289  return $ret;
290  }
291 
292  /*******************************************************************/
293  /** \brief Set the parent object of this instance.
294  */
295  // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
296  public function SetParentObj(
297  $in_parent_obj ///< A reference to the parent object.
298  ) {
299  // phpcs:enable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
300  $this->_local_id_parent_obj = null;
301  $this->_local_id_parent_obj = $in_parent_obj;
302  }
303 
304  /*******************************************************************/
305  /** \brief Return a reference to the parent object of this instance.
306 
307  \returns a reference to the parent instance of the object.
308  */
309  // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
310  public function GetParentObj()
311  {
312  // phpcs:enable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
313  return $this->_local_id_parent_obj;
314  }
315 }
GetFormatsByString($in_string, $in_lang_enum=null)
Return all the formats that contain the given string.
$ret
Definition: contact.php:226
__construct($in_parent_obj, $in_array)
Constructor. Sets the parent object.
GetFormatsByLanguage($in_lang_enum=null)
Return all the formats that share a given language.
GetFormatByKeyAndLanguage($in_format_key, $in_lang_enum=null)
Return a reference to a single object, by format key and language.
GetFormatsBySharedIDCode($in_shared_id_bigint)
Return all the formats that share a given shared ID code.
A Class for Format Codes.
Include the format class.
GetParentObj()
Return a reference to the parent object of this instance.
SetParentObj($in_parent_obj)
Set the parent object of this instance.
SplitIntoMetaphone($in_string, $in_lang_enum=null, $in_literal=false)
Converts a string into an array of metaphone entities.
GetFormatBySharedIDCodeAndLanguage($in_shared_id_bigint, $in_lang_enum=null)
Return a reference to a single object, by shared ID and language.
Simply declares an interface for having a "container" object.
defined('BMLT_EXEC') or define('BMLT_EXEC'
Definition: index.php:3
GetFormatsArray()
Accessor -Get the format object array.