BMLT Root Server
c_comdef_format.class.php
Go to the documentation of this file.
1 <?php
2 /***********************************************************************/
3 /** \file c_comdef_format.class.php
4  \brief The class file for the c_comdef_format 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 require_once(dirname(__FILE__)."/../shared/classes/base_templates.inc.php");
25 
26 /***********************************************************************/
27 /** \class c_comdef_format
28  \brief A Class for Format Codes
29 
30  This class handles the model for the NA Meeting Format Codes. The
31  codes are stored one code per language per instance of this class.
32  If codes are related (Same code, different languages), then you should
33  give them all the same shared ID.
34 ***********************************************************************/
35 // phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace
36 // phpcs:disable Squiz.Classes.ValidClassName.NotCamelCaps
38 // phpcs:enable PSR1.Classes.ClassDeclaration.MissingNamespace
39 // phpcs:enable Squiz.Classes.ValidClassName.NotCamelCaps
40 {
41  /// The shared ID (for multiple instances of the same format code).
42  public $_shared_id_bigint = null;
43 
44  /** \brief This is the type of format
45  - An enumerated string value, representing the classification of the format:
46  - "FC1" -Meeting Format (Speaker, Book Study, etc.)
47  - "FC2" -Location Code (Wheelchair Accessible, Limited Parking, etc.)
48  - "FC3" -Attendance Restriction (Men Only, Closed, Open, No Children, etc.) */
49  public $_format_type_string = null;
50 
51  /// The Format Key, in Text Form.
52  public $_key_string = null;
53 
54  /// The Format Icon, as a 64-pixel-square Icon, in GIF format.
55  public $_icon_blob = null;
56 
57  /// This will be an array of the format types (localized).
58  public $_format_codes = null;
59 
60  /*******************************************************************/
61  /** \brief Updates the DB to the current values of this instance.
62  (replacing current values of the DB).
63 
64  \throws a PDOException if there is a problem.
65  */
66  // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
67  public function UpdateToDB(
68  $is_rollback = false ///< If true, this is a rollback operation.
69  ) {
70  // phpcs:enable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
71  $ret = false;
72 
74 
75  if ($this->UserCanEdit($user)) {
76  // We take a snapshot of the format as it currently sits in the database as a "before" image.
77  $before = null;
78  $before_id = null;
79  $before_lang = null;
80  $before_obj = c_comdef_server::GetOneFormat($this->GetSharedID(), $this->GetLocalLang());
81 
82  if ($before_obj instanceof c_comdef_format) {
83  $before = $before_obj->SerializeObject();
84  $before_id = $before_obj->GetSharedID();
85  $before_lang = $before_obj->GetLocalLang();
86  $before_obj = null;
87  }
88 
89  try {
90  $this->DeleteFromDB_NoRecord();
91 
92  /// Set the values for this instance, to be stored in the database.
93  $record = array();
94  $record['shared_id_bigint'] = intval($this->GetSharedID());
95  $record['format_type_enum'] = $this->GetFormatType();
96  $record['key_string'] = $this->GetKey();
97  $record['icon_blob'] = $this->GetIcon();
98  $record['worldid_mixed'] = $this->GetWorldID();
99  $record['lang_enum'] = $this->GetLocalLang();
100  $record['name_string'] = $this->GetLocalName();
101  $record['description_string'] = $this->GetLocalDescription();
102 
103  $first = true; ///< This is used to keep track of the first iteration, to prevent a comma before it.
104  $vals = array(); ///< This is the prepared statement value array.
105 
106  /// Create a new entry.
107  $updateSQL = "INSERT INTO `".c_comdef_server::GetFormatTableName_obj()."` (";
108  while (list ( $key, $value ) = each($record)) {
109  if (!$first) {
110  $updateSQL .= ",";
111  } else {
112  $first = false;
113  }
114  $updateSQL .= "`".$key."`";
115  }
116 
117  reset($record);
118 
119  $updateSQL .= ") VALUES (";
120 
121  $first = true;
122  while (list ( $key, $value ) = each($record)) {
123  if (!$first) {
124  $updateSQL .= ",";
125  } else {
126  $first = false;
127  }
128  /// We give the prepared statement a token, that will be filled by a value.
129  $updateSQL .= ":$key";
130  /// We give the value by declaring an associative array element with the token name.
131  $vals[':'.$key] = $value;
132  }
133  $updateSQL .= ")";
134  c_comdef_dbsingleton::preparedExec($updateSQL, $vals);
135 
136  $after = $this->SerializeObject();
137  $after_id = $this->GetSharedID();
138  $after_lang = $this->GetLocalLang();
139 
140  $cType = (true == $is_rollback) ? 'comdef_change_type_rollback' : ((null != $before) ? 'comdef_change_type_change' : 'comdef_change_type_new');
141 
142  c_comdef_server::AddNewChange($user->GetID(), $cType, null, $before, $after, 'c_comdef_format', $before_id, $after_id, $before_lang, $after_lang);
143 
144  $ret = true;
145  } catch (Exception $ex) {
146  global $_COMDEF_DEBUG;
147 
148  if ($_COMDEF_DEBUG) {
149  echo "Exception Thrown in c_comdef_format::UpdateToDB()!<br />";
150  var_dump($ex);
151  }
152  throw ( $ex );
153  }
154  }
155 
156  return $ret;
157  }
158 
159  /*******************************************************************/
160  /** \brief Deletes this instance from the database without recording it.
161 
162  \returns true, if the delete was successful. False, otherwise.
163 
164  \throws a PDOException if there is a problem.
165  */
166  // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
167  public function DeleteFromDB_NoRecord()
168  {
169  // phpcs:enable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
170  $ret = false;
171 
172  if ($this->UserCanEdit()) {
173  try {
174  $sql = "DELETE FROM `".c_comdef_server::GetFormatTableName_obj()."` WHERE shared_id_bigint=? AND lang_enum=?";
175  c_comdef_dbsingleton::preparedExec($sql, array ( $this->GetSharedID(), $this->GetLocalLang() ));
176  $ret = true;
177  } catch (Exception $ex) {
178  global $_COMDEF_DEBUG;
179 
180  if ($_COMDEF_DEBUG) {
181  echo "Exception Thrown in c_comdef_format::DeleteFromDB_NoRecord()!<br />";
182  var_dump($ex);
183  }
184  throw ( $ex );
185  }
186  }
187 
188  return $ret;
189  }
190 
191  /*******************************************************************/
192  /** \brief Deletes this instance from the database.
193 
194  \throws a PDOException if there is a problem.
195  */
196  // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
197  public function DeleteFromDB()
198  {
199  // phpcs:enable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
200  $ret = false;
201 
203 
204  if ($this->UserCanEdit($user)) {
205  // We take a snapshot of the user as it currently sits in the database as a "before" image.
206  $before = null;
207  $before_id = null;
208  $before_lang = null;
209  $before_obj = c_comdef_server::GetOneFormat($this->GetSharedID(), $this->GetLocalLang());
210 
211  if ($before_obj instanceof c_comdef_format) {
212  $before = $before_obj->SerializeObject();
213  $before_id = $before_obj->GetSharedID();
214  $before_lang = $before_obj->GetLocalLang();
215  $before_obj = null;
216  }
217 
218  $ret = $this->DeleteFromDB_NoRecord();
219 
220  if ($ret) {
221  c_comdef_server::AddNewChange($user->GetID(), 'comdef_change_type_delete', $this->GetSharedID(), $before, null, 'c_comdef_format', $before_id, null, $before_lang, null);
222  }
223  }
224 
225  return $ret;
226  }
227 
228  /*******************************************************************/
229  /** \brief Updates this instance to the current values in the DB
230  (replacing current values of the instance).
231 
232  \throws a PDOException if there is a problem.
233  */
234  // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
235  public function RestoreFromDB()
236  {
237  // phpcs:enable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
238  try {
239  $sql = "SELECT * FROM `".c_comdef_server::GetFormatTableName_obj()."` WHERE shared_id_bigint=? AND lang_enum=?";
240 
241  $vals = array ( intval($this->GetLocalID(), $this->GetLocalLang()) );
242 
243  $rows = c_comdef_dbsingleton::preparedQuery($sql, $vals);
244 
245  if (is_array($rows) && count($rows)) {
246  $this->SetSharedID($rs['shared_id_bigint']);
247  $this->SetFormatType($rs['format_type_enum']);
248  $this->SetKey($rs['key_string']);
249  $this->SetIcon($rs['icon_blob']);
250  $this->SetWorldID($rs['worldid_mixed']);
251  $this->SetLocalLang($rs['lang_enum']);
252  $this->SetLocalName($rs['name_string']);
253  $this->SetLocalDescription($rs['description_string']);
254  } else {
255  global $_COMDEF_DEBUG;
256 
257  if ($_COMDEF_DEBUG) {
258  echo "Exception Thrown in c_comdef_format::RestoreFromDB()!<br />";
259  var_dump($ex);
260  }
261  throw ( $ex );
262  }
263  } catch (Exception $ex) {
264  global $_COMDEF_DEBUG;
265 
266  if ($_COMDEF_DEBUG) {
267  echo "Exception Thrown in c_comdef_format::RestoreFromDB()!<br />";
268  var_dump($ex);
269  }
270  throw ( $ex );
271  }
272  }
273 
274  /*******************************************************************/
275  /** \brief The initial setup call for the class. If you send in values,
276  the object will set itself up to use them.
277 
278  */
279  // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
280  public function __construct(
281  $in_parent_obj, ///< The object that "owns" this instance.
282  $in_shared_id_bigint = null, ///< The shared ID (If this is the same as other formats -used for different languages).
283  $in_format_type_string = null,
284  // \brief The format code type
285  // - An enumerated string value, representing the classification of the format:
286  // - "FC1" -Meeting Format (Speaker, Book Study, etc.)
287  // - "FC2" -Location Code (Wheelchair Accessible, Limited Parking, etc.)
288  // - "FC3" -Attendance Restriction (Men Only, Closed, Open, No Children, etc.)
289  $in_key_string = null, ///< The format Key, as a text string (1-3 Characters).
290  $in_icon_blob = null, ///< The format icon, as 64-pixel-square GIF image data.
291  /// The t_comdef_world_type Class:
292  $in_worldid_mixed = null, ///< The NAWS ID for this format (can be the same as other format IDs)
293  /// The t_comdef_local_type Class
294  $in_lang_enum = null, ///< The language to be used for this instance (if null, the server language is used).
295  $in_name_string = null, ///< The name of this instance
296  $in_description_string = null ///< A verbose description
297  ) {
298  // phpcs:enable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
300 
301  /// This is the server language.
302  if (null == $in_lang_enum) {
303  $in_lang_enum = $comdef_global_language;
304  }
305 
306  /// Should never be necessary.
307  if (null == $in_lang_enum) {
308  $in_lang_enum = "en";
309  }
310 
311  $this->SetSharedID($in_shared_id_bigint);
312  $this->SetFormatType($in_format_type_string);
313  $this->SetKey($in_key_string);
314  $this->SetIcon($in_icon_blob);
315 
316  $this->SetParentObj($in_parent_obj);
317 
318  $this->SetLocalLang($in_lang_enum);
319  $this->SetLocalName($in_name_string);
320  $this->SetLocalDescription($in_description_string);
321 
322  $this->SetWorldID($in_worldid_mixed);
323 
324  /// This gets us the format codes, with their localized descriptions.
325 // $file_path = dirname ( __FILE__ ).'/../../local_server/server_admin/lang/'.$in_lang_enum.'/format_codes.inc.php';
326 //
327 // require ( $file_path );
328 //
329 // $this->SetFormatCodes ( $comdef_format_types );
330  }
331 
332  /*******************************************************************/
333  /** \brief Accessor -Set the shared ID
334 
335  The shared ID may be the same for a number of different instances. It is how
336  we link different language versions of the same format together.
337  */
338  // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
339  public function SetSharedID(
340  $in_shared_id_bigint ///< The shared ID (If this is the same as other formats -used for different languages).
341  ) {
342  // phpcs:enable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
343  $this->_shared_id_bigint = null;
344  $this->_shared_id_bigint = $in_shared_id_bigint;
345  }
346 
347  /*******************************************************************/
348  /** \brief Accessor -Get a reference to the shared ID.
349 
350  @returns The _shared_id_bigint data member, as a reference.
351  */
352  // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
353  public function GetSharedID()
354  {
355  // phpcs:enable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
356  return $this->_shared_id_bigint;
357  }
358 
359  /*******************************************************************/
360  /** \brief Accessor -Sets the format type code.
361  */
362  // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
363  public function SetFormatType(
364  $in_format_type_string
365  // - An enumerated string value, representing the classification of the format:
366  // - "FC1" -Meeting Format (Speaker, Book Study, etc.)
367  // - "FC2" -Location Code (Wheelchair Accessible, Limited Parking, etc.)
368  // - "FC3" -Attendance Restriction (Men Only, Closed, Open, No Children, etc.)s
369  ) {
370  // phpcs:enable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
371  $this->_format_type_string = null;
372  $this->_format_type_string = $in_format_type_string;
373  }
374 
375  /*******************************************************************/
376  /** \brief Accessor -Returns a reference to the format type code.
377 
378  @returns The _format_type_string data member, as a reference.
379  */
380  // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
381  public function &GetFormatType()
382  {
383  // phpcs:enable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
384  return $this->_format_type_string;
385  }
386 
387  /*******************************************************************/
388  /** \brief Accessor -Sets the format key (the 1-3 letter code that represents the format).
389  */
390  // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
391  public function SetKey(
392  $in_key_string ///< The format Key, as a text string (1-3 Characters)
393  ) {
394  // phpcs:enable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
395  $this->_key_string = null;
396  $this->_key_string = $in_key_string;
397  }
398 
399  /*******************************************************************/
400  /** \brief Accessor -Returns a reference to the _key_string data member
401 
402  @returns The _key_string data member, as a reference.
403  */
404  // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
405  public function &GetKey()
406  {
407  // phpcs:enable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
408  return $this->_key_string;
409  }
410 
411  /*******************************************************************/
412  /** \brief Accessor -Sets a 64-pixel-square image (as a GIF), to be used to indicate the format.
413  */
414  // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
415  public function SetIcon(
416  $in_icon_blob ///< The GIF image, as a stream of binary data.
417  ) {
418  // phpcs:enable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
419  $this->_icon_blob = null;
420  $this->_icon_blob = $in_icon_blob;
421  }
422 
423  /*******************************************************************/
424  /** \brief Accessor -Returns a reference to the _icon_blob data member
425 
426  @returns The _icon_blob data member, as a reference.
427  */
428  // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
429  public function &GetIcon()
430  {
431  // phpcs:enable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
432  return $this->_icon_blob;
433  }
434 
435  /*******************************************************************/
436  /** \brief Accessor -Sets a brief description string for the format, in whatever language has been selected.
437  */
438  // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
439  public function SetFormatCodes(
440  $in_format_code_array ///< An associative array, with the format codes.
441  ) {
442  // phpcs:enable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
443  $this->_format_codes = null;
444  $this->_format_codes = $in_format_code_array; ///< Make sure we copy it.
445  }
446 
447  /*******************************************************************/
448  /** \brief Accessor -Returns a reference to the _format_codes data member
449 
450  @returns The _format_codes data member, as a reference.
451  */
452  // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
453  public function &GetFormatCodes()
454  {
455  // phpcs:enable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
456  return $this->_format_codes;
457  }
458 
459  /*******************************************************************/
460  /** \brief Returns a storable serialization of the object, as a string.
461 
462  This is only used for the changes, as the serialized string may not
463  be easily searched.
464 
465  \returns an array, containing the object in serialized form.
466  */
467  // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
468  public function SerializeObject()
469  {
470  // phpcs:enable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
471  $s_array['shared_id_bigint'] = $this->GetSharedID();
472  $s_array['format_type_string'] = $this->GetFormatType();
473  $s_array['key_string'] = $this->GetKey();
474  $s_array['icon_blob'] = $this->GetIcon();
475  $s_array['worldid_mixed'] = $this->GetWorldID();
476  $s_array['lang_enum'] = $this->GetLocalLang();
477  $s_array['name_string'] = $this->GetLocalName();
478  $s_array['description_string'] = $this->GetLocalDescription();
479 
480  return serialize($s_array);
481  }
482 
483  /*******************************************************************/
484  /** \brief This takes the serialized data, and instantiates a
485  new object from it.
486 
487  \returns a new instance of c_comdef_format, set up according to
488  the serialized data passed in.
489  */
490  // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
491  public static function UnserializeObject(
492  $in_parent, ///< The parent object.
493  $serialized_array ///< An array containing the serialized data.
494  ) {
495  // phpcs:enable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
496  $s_array = unserialize($serialized_array);
497 
498  return new c_comdef_format(
499  $in_parent,
500  $s_array['shared_id_bigint'],
501  $s_array['format_type_string'],
502  $s_array['key_string'],
503  $s_array['icon_blob'],
504  $s_array['worldid_mixed'],
505  $s_array['lang_enum'],
506  $s_array['name_string'],
507  $s_array['description_string']
508  );
509  }
510 
511  /*******************************************************************/
512  /** \brief Test to see if a user is allowed to edit an instance (change the data).
513 
514  \returns true, if the user is allowed to edit, false, otherwise.
515  */
516  // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
517  public function UserCanEdit(
518  $in_user_object = null ///< A reference to a c_comdef_user object, for the user to be validated. If null, or not supplied, the server current user is tested.
519  ) {
520  // phpcs:enable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
521  $ret = false;
522 
523  // We load the server user if one wasn't supplied.
524  if (null == $in_user_object) {
525  $in_user_object = c_comdef_server::GetCurrentUserObj();
526  }
527 
528  // If it isn't a user object, we fail right there.
529  if ($in_user_object instanceof c_comdef_user) {
530  $in_user_object->RestoreFromDB(); // The reason you do this, is to ensure that the user wasn't modified "live." It's a security precaution.
531  // Only the server admin can edit formats.
533  $ret = true;
534  }
535  }
536 
537  return $ret;
538  }
539 }
static AddNewChange($in_user_id_bigint, $in_change_type, $in_service_body_id_bigint, $in_before_string, $in_after_string, $in_object_class_string, $in_before_obj_id_bigint, $in_after_obj_id_bigint, $in_before_obj_lang_enum=null, $in_after_obj_lang_enum=null, $in_name_string=null, $in_description_string=null, $in_lang_enum=null)
Creates a new change record in the Database.
GetSharedID()
Accessor -Get a reference to the shared ID.
static preparedExec($sql, $params=array())
Wrapper for preparing and executing a PDOStatement that does not return a resultset e...
SetKey($in_key_string)
Accessor -Sets the format key (the 1-3 letter code that represents the format).
DeleteFromDB()
Deletes this instance from the database.
This class handles BMLT users. One instance is created for each user on the server.
$ret
Definition: contact.php:226
& GetFormatType()
Accessor -Returns a reference to the format type code.
UserCanEdit($in_user_object=null)
Test to see if a user is allowed to edit an instance (change the data).
& GetIcon()
Accessor -Returns a reference to the _icon_blob data member.
DeleteFromDB_NoRecord()
Deletes this instance from the database without recording it.
global $comdef_global_language
The local server language enum (Will default to English)
static GetOneFormat($in_id_bigint, $in_lang_enum)
Given an ID and a language for a format, it returns one instance.
static preparedQuery($sql, $params=array(), $fetchKeyPair=false)
Wrapper for preparing and executing a PDOStatement that returns a resultset e.g. SELECT SQL statement...
RestoreFromDB()
Updates this instance to the current values in the DB (replacing current values of the instance)...
UpdateToDB($is_rollback=false)
Updates the DB to the current values of this instance. (replacing current values of the DB)...
SetFormatType($in_format_type_string)
Accessor -Sets the format type code.
A Class for Format Codes.
SetFormatCodes($in_format_code_array)
Accessor -Sets a brief description string for the format, in whatever language has been selected...
function E each("Boolean Number String Function Array Date RegExp Object Error Symbol".split(" "), function(e, t){n["[object "+t+"]"]=t.toLowerCase()})
SetSharedID($in_shared_id_bigint)
Accessor -Set the shared ID.
A very simple class that allows whatever format NAWS will use as an ID to be assigned to the object...
& GetKey()
Accessor -Returns a reference to the _key_string data member.
Interface for entities that can be rendered into serialized form.
static UnserializeObject($in_parent, $serialized_array)
This takes the serialized data, and instantiates a new object from it.
Interface for entities that store themselves in the database.
__construct($in_parent_obj, $in_shared_id_bigint=null, $in_format_type_string=null, $in_key_string=null, $in_icon_blob=null, $in_worldid_mixed=null, $in_lang_enum=null, $in_name_string=null, $in_description_string=null)
The initial setup call for the class. If you send in values, the object will set itself up to use the...
SerializeObject()
Returns a storable serialization of the object, as a string.
defined('BMLT_EXEC') or define('BMLT_EXEC'
Definition: index.php:3
& GetFormatCodes()
Accessor -Returns a reference to the _format_codes data member.
global $_COMDEF_DEBUG
This flag, when set to true, will cause extra debug information to be output.
static GetCurrentUserObj($in_is_ajax=false)
Get the current logged-in user, as a c_comdef_user instance.
static IsUserServerAdmin($in_user_obj=null, $in_is_ajax=false)
Find out if the user is a server admin.
SetIcon($in_icon_blob)
Accessor -Sets a 64-pixel-square image (as a GIF), to be used to indicate the format.
Interface for entities that authenticate users.