-
Notifications
You must be signed in to change notification settings - Fork 168
/
BaseModel.php
executable file
·9008 lines (8057 loc) · 353 KB
/
BaseModel.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/** ---------------------------------------------------------------------
* app/lib/core/BaseModel.php :
* ----------------------------------------------------------------------
* CollectiveAccess
* Open-source collections management software
* ----------------------------------------------------------------------
*
* Software by Whirl-i-Gig (http://www.whirl-i-gig.com)
* Copyright 2000-2012 Whirl-i-Gig
*
* For more information visit http://www.CollectiveAccess.org
*
* This program is free software; you may redistribute it and/or modify it under
* the terms of the provided license as published by Whirl-i-Gig
*
* CollectiveAccess is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTIES whatsoever, including any implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* This source code is free and modifiable under the terms of
* GNU General Public License. (http://www.gnu.org/copyleft/gpl.html). See
* the "license.txt" file for details, or visit the CollectiveAccess web site at
* http://www.CollectiveAccess.org
*
* @package CollectiveAccess
* @subpackage BaseModel
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License version 3
*
* ----------------------------------------------------------------------
*/
/**
*
*/
# ------------------------------------------------------------------------------------
# --- Field type constants
# ------------------------------------------------------------------------------------
define("FT_NUMBER",0);
define("FT_TEXT", 1);
define("FT_TIMESTAMP", 2);
define("FT_DATETIME", 3);
define("FT_HISTORIC_DATETIME", 4);
define("FT_DATERANGE", 5);
define("FT_HISTORIC_DATERANGE", 6);
define("FT_BIT", 7);
define("FT_FILE", 8);
define("FT_MEDIA", 9);
define("FT_PASSWORD", 10);
define("FT_VARS", 11);
define("FT_TIMECODE", 12);
define("FT_DATE", 13);
define("FT_HISTORIC_DATE", 14);
define("FT_TIME", 15);
define("FT_TIMERANGE", 16);
# ------------------------------------------------------------------------------------
# --- Display type constants
# ------------------------------------------------------------------------------------
define("DT_SELECT", 0);
define("DT_LIST", 1);
define("DT_LIST_MULTIPLE", 2);
define("DT_CHECKBOXES", 3);
define("DT_RADIO_BUTTONS", 4);
define("DT_FIELD", 5);
define("DT_HIDDEN", 6);
define("DT_OMIT", 7);
define("DT_TEXT", 8);
define("DT_PASSWORD", 9);
define("DT_COLORPICKER", 10);
define("DT_TIMECODE", 12);
define("DT_COUNTRY_LIST", 13);
define("DT_STATEPROV_LIST", 14);
# ------------------------------------------------------------------------------------
# --- Access mode constants
# ------------------------------------------------------------------------------------
define("ACCESS_READ", 0);
define("ACCESS_WRITE", 1);
# ------------------------------------------------------------------------------------
# --- Text-markup constants
# ------------------------------------------------------------------------------------
define("__CA_MT_HTML__", 0);
define("__CA_MT_TEXT_ONLY__", 1);
# ------------------------------------------------------------------------------------
# --- Hierarchy type constants
# ------------------------------------------------------------------------------------
define("__CA_HIER_TYPE_SIMPLE_MONO__", 1);
define("__CA_HIER_TYPE_MULTI_MONO__", 2);
define("__CA_HIER_TYPE_ADHOC_MONO__", 3);
define("__CA_HIER_TYPE_MULTI_POLY__", 4);
# ----------------------------------------------------------------------
# --- Import classes
# ----------------------------------------------------------------------
require_once(__CA_LIB_DIR__."/core/BaseObject.php");
require_once(__CA_LIB_DIR__."/core/Error.php");
require_once(__CA_LIB_DIR__."/core/Configuration.php");
require_once(__CA_LIB_DIR__."/core/Datamodel.php");
require_once(__CA_LIB_DIR__."/core/ApplicationChangeLog.php");
require_once(__CA_LIB_DIR__."/core/Parsers/TimeExpressionParser.php");
require_once(__CA_LIB_DIR__."/core/Parsers/TimecodeParser.php");
require_once(__CA_LIB_DIR__."/core/Db.php");
require_once(__CA_LIB_DIR__."/core/Media.php");
require_once(__CA_LIB_DIR__."/core/Media/MediaVolumes.php");
require_once(__CA_LIB_DIR__."/core/File.php");
require_once(__CA_LIB_DIR__."/core/File/FileVolumes.php");
require_once(__CA_LIB_DIR__."/core/Utils/Timer.php");
require_once(__CA_LIB_DIR__."/core/Utils/Unicode.php");
require_once(__CA_LIB_DIR__."/core/Search/SearchIndexer.php");
require_once(__CA_LIB_DIR__."/core/Db/Transaction.php");
require_once(__CA_LIB_DIR__."/core/Media/MediaProcessingSettings.php");
require_once(__CA_APP_DIR__."/helpers/utilityHelpers.php");
require_once(__CA_APP_DIR__."/helpers/gisHelpers.php");
require_once(__CA_LIB_DIR__."/ca/ApplicationPluginManager.php");
require_once(__CA_LIB_DIR__."/core/Parsers/htmlpurifier/HTMLPurifier.standalone.php");
/**
* Base class for all database table classes. Implements database insert/update/delete
* functionality and a whole lot more, including automatic generation of HTML form
* widgets, layering of additional field types and data processing functionality
* (media upload fields using the Media class; date/time and date range fields
* with natural language input using the TimeExpressionParser class; serialized
* variable stores using php serialize(), automatic management of hierarchies, etc.)
* Each table in your database should have a class that extends BaseModel.
*/
class BaseModel extends BaseObject {
# --------------------------------------------------------------------------------
# --- Properties
# --------------------------------------------------------------------------------
/**
* representation of the access mode of the current instance.
* 0 (ACCESS_READ) means read-only,
* 1 (ACCESS_WRITE) characterizes a BaseModel instance that is able to write to the database.
*
* @access private
*/
private $ACCESS_MODE;
/**
* local Db object for database access
*
* @access private
*/
private $o_db;
/**
* debug mode state
*
* @access private
*/
var $debug = 0;
/**
* @access private
*/
var $_FIELD_VALUES;
/**
* @access protected
*/
protected $_FIELD_VALUE_CHANGED;
/**
* @access private
*/
var $_FIELD_VALUES_OLD;
/**
* @access private
*/
private $_FILES;
/**
* @access private
*/
private $_SET_FILES;
/**
* @access private
*/
private $_FILES_CLEAR;
/**
* object-oriented access to media volume information
*
* @access private
*/
private $_MEDIA_VOLUMES;
/**
* object-oriented access to file volume information
*
* @access private
*/
private $_FILE_VOLUMES;
/**
* if true, DATETIME fields take Unix date/times,
* not parseable date/time expressions
*
* @access private
*/
private $DIRECT_DATETIMES = 0;
/**
* local Configuration object representation
*
* @access protected
*/
protected $_CONFIG;
/**
* local Datamodel object representation
*
* @access protected
*/
protected $_DATAMODEL = null;
/**
* contains current Transaction object
*
* @access protected
*/
protected $_TRANSACTION = null;
/**
* The current locale. Used to determine which set of localized error messages to use. Default is US English ("en_us")
*
* @access private
*/
var $ops_locale = "en_us"; #
/**
* prepared change log statement (primary log entry)
*
* @access private
*/
private $opqs_change_log;
/**
* prepared change log statement (log subject entries)
*
* @access private
*/
private $opqs_change_log_subjects;
/**
* prepared statement to get change log
*
* @access private
*/
private $opqs_get_change_log;
/**
* prepared statement to get change log
*
* @access private
*/
private $opqs_get_change_log_subjects; #
/**
* array containing parsed version string from
*
* @access private
*/
private $opa_php_version; #
# --------------------------------------------------------------------------------
# --- Error handling properties
# --------------------------------------------------------------------------------
/**
* Array of error objects
*
* @access public
*/
public $errors;
/**
* If true, on error error message is printed and execution halted
*
* @access private
*/
private $error_output;
/**
* List of fields that had conflicts with existing data during last update()
* (ie. someone else had already saved to this field while the user of this instance was working)
*
* @access private
*/
private $field_conflicts;
/**
* Single instance of search indexer used by all models
*/
static public $search_indexer;
/**
* Single instance of HTML Purifier used by all models
*/
static public $html_purifier;
/**
* If set, all field values passed through BaseModel::set() are run through HTML Purifier before being stored
*/
private $opb_purify_input = false;
/**
* Array of model definitions, keyed on table name
*/
static $s_ca_models_definitions;
static $s_instance_cache = array();
/**
* Constructor
* In general you should not call this constructor directly. Any table in your database
* should be represented by an extension of this class.
*
* @param int $pn_id primary key identifier of the table row represented by this object
* if omitted, an empty object is created which can be used to create a new row in the database.
* @return BaseModel
*/
public function __construct($pn_id=null, $pb_use_cache=true) {
$vs_table_name = $this->tableName();
if (!$this->FIELDS =& BaseModel::$s_ca_models_definitions[$vs_table_name]['FIELDS']) {
die("Field definitions not found for {$vs_table_name}");
}
$this->NAME_SINGULAR =& BaseModel::$s_ca_models_definitions[$vs_table_name]['NAME_SINGULAR'];
$this->NAME_PLURAL =& BaseModel::$s_ca_models_definitions[$vs_table_name]['NAME_PLURAL'];
$this->errors = array();
$this->error_output = 0; # don't halt on error
$this->field_conflicts = array();
$this->_CONFIG = Configuration::load();
$this->_DATAMODEL = Datamodel::load();
$this->_FILES_CLEAR = array();
$this->_SET_FILES = array();
$this->_MEDIA_VOLUMES = MediaVolumes::load();
$this->_FILE_VOLUMES = FileVolumes::load();
$this->_FIELD_VALUE_CHANGED = array();
if ($vs_locale = $this->_CONFIG->get("locale")) {
$this->ops_locale = $vs_locale;
}
$this->opo_app_plugin_manager = new ApplicationPluginManager();
$this->setMode(ACCESS_READ);
if ($pn_id) { $this->load($pn_id, $pb_use_cache);}
}
/**
* Get Db object
* If a transaction is pending, the Db object representation is taken from the Transaction object,
* if not, a new connection is established, stored in a local property and returned.
*
* @return Db
*/
public function getDb() {
if ($this->inTransaction()) {
$this->o_db = $this->getTransaction()->getDb();
} else {
if (!$this->o_db) {
$this->o_db = new Db();
$this->o_db->dieOnError(false);
}
}
return $this->o_db;
}
/**
* Convenience method to return application configuration object. This is the same object
* you'd get if you instantiated a Configuration() object without any parameters
*
* @return Configuration
*/
public function getAppConfig() {
return $this->_CONFIG;
}
/**
* Convenience method to return application datamodel object. This is the same object
* you'd get if you instantiated a Datamodel() object
*
* @return Configuration
*/
public function getAppDatamodel() {
return $this->_DATAMODEL;
}
/**
* Get character set from configuration file. Defaults to
* UTF8 if configuration parameter "character_set" is
* not found.
*
* @return string the character set
*/
public function getCharacterSet() {
if (!($vs_charset = $this->_CONFIG->get("character_set"))) {
$vs_charset = "UTF-8";
}
return $vs_charset;
}
# --------------------------------------------------------------------------------
# --- Transactions
# --------------------------------------------------------------------------------
/**
* Sets up local transaction property and refreshes local db property to
* reflect the db connection of that transaction. After setting it, you
* can perform common actions.
*
* @see BaseModel::getTransaction()
* @see BaseModel::inTransaction()
* @see BaseModel::removeTransaction()
*
* @param Transaction $transaction
* @return bool success state
*/
public function setTransaction(&$transaction) {
if (is_object($transaction)) {
$this->_TRANSACTION = $transaction;
$this->getDb(); // refresh $o_db property to reflect db connection of transaction
return true;
} else {
return false;
}
}
/**
* Get transaction property.
*
* @see BaseModel::setTransaction()
* @see BaseModel::inTransaction()
* @see BaseModel::removeTransaction()
*
* @return Transaction
*/
public function getTransaction() {
return isset($this->_TRANSACTION) ? $this->_TRANSACTION : null;
}
/**
* Is there a pending transaction?
*
* @return bool
*/
public function inTransaction() {
if (isset($this->_TRANSACTION)) { return ($this->_TRANSACTION) ? true : false; }
return false;
}
/**
* Remove transaction property.
*
* @param bool $ps_commit If true, the transaction is committed, if false, the transaction is rollback.
* Defaults to true.
* @return bool success state
*/
public function removeTransaction($ps_commit=true) {
if ($this->inTransaction()) {
if ($ps_commit) {
$this->_TRANSACTION->commit();
} else {
$this->_TRANSACTION->rollback();
}
unset($this->_TRANSACTION);
return true;
} else {
return false;
}
}
# --------------------------------------------------------------------------------
/**
* Sets whether BaseModel::set() input is run through HTML purifier or not
*
* @param bool $pb_purify If true, all input passed via BaseModel::set() is run through HTML Purifier prior to being stored. If false, input is stored as-is. If null or omitted, no change is made to current setting
* @return bool Returns current purification settings. If you simply want to get the current setting call BaseModel::purify without any parameters.
*/
public function purify($pb_purify=null) {
if (!is_null($pb_purify)) {
$this->opb_purify_input = (bool)$pb_purify;
}
return $this->opb_purify_input;
}
# --------------------------------------------------------------------------------
# Set/get values
# --------------------------------------------------------------------------------
/**
* Get all field values of the current row that is represented by this BaseModel object.
*
* @see BaseModel::getChangedFieldValuesArray()
* @return array associative array: field name => field value
*/
public function getFieldValuesArray() {
return $this->_FIELD_VALUES;
}
/**
* Set alls field values of the current row that is represented by this BaseModel object
* by passing an associative array as follows: field name => field value
*
* @param array $pa_values associative array: field name => field value
* @return void
*/
public function setFieldValuesArray($pa_values) {
$this->_FIELD_VALUES = $pa_values;
}
/**
* Get an associative array of the field values that changed since instantiation of
* this BaseModel object.
*
* @return array associative array: field name => field value
*/
public function getChangedFieldValuesArray() {
$va_fieldnames = array_keys($this->_FIELD_VALUES);
$va_changed_field_values_array = array();
foreach($va_fieldnames as $vs_fieldname) {
if($this->changed($vs_fieldname)) {
$va_changed_field_values_array[$vs_fieldname] = $this->_FIELD_VALUES[$vs_fieldname];
}
}
return $va_changed_field_values_array;
}
/**
* What was the original value of a field?
*
* @param string $ps_field field name
* @return mixed original field value
*/
public function getOriginalValue($ps_field) {
return $this->_FIELD_VALUES_OLD[$ps_field];
}
/**
* Check if the content of a field has changed.
*
* @param string $ps_field field name
* @return bool
*/
public function changed($ps_field) {
return isset($this->_FIELD_VALUE_CHANGED[$ps_field]) ? $this->_FIELD_VALUE_CHANGED[$ps_field] : null;
}
/**
* Force field to be considered changed
*
* @param string $ps_field field name
* @return bool Always true
*/
public function setAsChanged($ps_field) {
$this->_FIELD_VALUE_CHANGED[$ps_field] = true;
return true;
}
/**
* Returns value of primary key
*
* @param bool $vb_quoted Set to true if you want the method to return the value in quotes. Default is false.
* @return mixed value of primary key
*/
public function getPrimaryKey ($vb_quoted=false) {
if (!isset($this->_FIELD_VALUES[$this->PRIMARY_KEY])) return null;
$vm_pk = $this->_FIELD_VALUES[$this->PRIMARY_KEY];
if ($vb_quoted) {
$vm_pk = $this->quote($this->PRIMARY_KEY, $vm_pk);
}
return $vm_pk;
}
/**
* Get a field value of the table row that is represented by this object.
*
* @param string $ps_field field name
* @param array $pa_options options array; can be omitted.
* It should be an associative array of boolean (except one of the options) flags. In case that some of the options are not set, they are treated as 'false'.
* Possible options (keys) are:
* BINARY: return field value as is
* FILTER_HTML_SPECIAL_CHARS: convert all applicable chars to their html entities
* DONT_PROCESS_GLOSSARY_TAGS: ?
* CONVERT_HTML_BREAKS: similar to nl2br()
* convertLineBreaks: same as CONVERT_HTML_BREAKS
* GET_DIRECT_DATE: return raw date value from database if $ps_field adresses a date field, otherwise the value will be parsed using the TimeExpressionParser::getText() method
* GET_DIRECT_TIME: return raw time value from database if $ps_field adresses a time field, otherwise the value will be parsed using the TimeExpressionParser::getText() method
* TIMECODE_FORMAT: set return format for fields representing time ranges possible (string) values: COLON_DELIMITED, HOURS_MINUTES_SECONDS, RAW; data will be passed through floatval() by default
* QUOTE: set return value into quotes
* URL_ENCODE: value will be passed through urlencode()
* ESCAPE_FOR_XML: convert <, >, &, ' and " characters for XML use
* DONT_STRIP_SLASHES: if set to true, return value will not be passed through stripslashes()
* template: formatting string to use for returned value; ^<fieldname> placeholder is used to represent field value in template
* returnAsArray: if true, fields that can return multiple values [currently only <table_name>.children.<field>] will return values in an indexed array; default is false
* returnAllLocales:
* delimiter: if set, value is used as delimiter when fields that can return multiple fields are returned as strings; default is a single space
* convertCodesToDisplayText: if set, id values refering to foreign keys are returned as preferred label text in the current locale
* returnURL: if set then url is returned for media, otherwise an HTML tag for display is returned
* dateFormat: format to return created or lastModified dates in. Valid values are iso8601 or text
* checkAccess = array of access values to filter results by; if defined only items with the specified access code(s) are returned. Only supported for table that have an "access" field.
* sort = optional field to sort returned values on. The field specifiers are fields with or without table name specified.
* sort_direction = direction to sort results by, either 'asc' for ascending order or 'desc' for descending order; default is 'asc'
*/
public function get($ps_field, $pa_options=null) {
if (!$ps_field) return null;
if (!is_array($pa_options)) { $pa_options = array();}
$vb_return_as_array = (isset($pa_options['returnAsArray'])) ? (bool)$pa_options['returnAsArray'] : false;
$vb_return_all_locales = (isset($pa_options['returnAllLocales'])) ? (bool)$pa_options['returnAllLocales'] : false;
$vs_delimiter = (isset($pa_options['delimiter'])) ? $pa_options['delimiter'] : ' ';
if ($vb_return_all_locales && !$vb_return_as_array) { $vb_return_as_array = true; }
$vn_row_id = $this->getPrimaryKey();
if ($vb_return_as_array && $vb_return_as_array) {
$vn_locale_id = ($this->hasField('locale_id')) ? $this->get('locale_id') : null;
}
$va_tmp = explode('.', $ps_field);
if (sizeof($va_tmp) > 1) {
if ($va_tmp[0] === $this->tableName()) {
//
// Return created on or last modified
//
if (in_array($va_tmp[1], array('created', 'lastModified'))) {
if ($vb_return_as_array) {
return ($va_tmp[1] == 'lastModified') ? $this->getLastChangeTimestamp() : $this->getCreationTimestamp();
} else {
$va_data = ($va_tmp[1] == 'lastModified') ? $this->getLastChangeTimestamp() : $this->getCreationTimestamp();
$vs_subfield = (isset($va_tmp[2]) && isset($va_data[$va_tmp[2]])) ? $va_tmp[2] : 'timestamp';
$vm_val = $va_data[$vs_subfield];
if ($vs_subfield == 'timestamp') {
$o_tep = new TimeExpressionParser();
$o_tep->setUnixTimestamps($vm_val, $vm_val);
$vm_val = $o_tep->getText($pa_options);
}
return $vm_val;
}
}
//
// Generalized get
//
switch(sizeof($va_tmp)) {
case 2:
// support <table_name>.<field_name> syntax
$ps_field = $va_tmp[1];
break;
default: // > 2 elements
// media field?
$va_field_info = $this->getFieldInfo($va_tmp[1]);
if (($va_field_info['FIELD_TYPE'] === FT_MEDIA) && (!isset($pa_options['returnAsArray'])) && !$pa_options['returnAsArray']) {
$o_media_settings = new MediaProcessingSettings($va_tmp[0], $va_tmp[1]);
$va_versions = $o_media_settings->getMediaTypeVersions('*');
$vs_version = $va_tmp[2];
if (!isset($va_versions[$vs_version])) {
$vs_version = array_shift(array_keys($va_versions));
}
if (isset($pa_options['returnURL']) && $pa_options['returnURL']) {
return $this->getMediaUrl($va_tmp[1], $vs_version);
} else {
return $this->getMediaTag($va_tmp[1], $vs_version);
}
}
if (($va_tmp[1] == 'parent') && ($this->isHierarchical()) && ($vn_parent_id = $this->get($this->getProperty('HIERARCHY_PARENT_ID_FLD')))) {
$t_instance = $this->getAppDatamodel()->getInstanceByTableNum($this->tableNum());
if (!$t_instance->load($vn_parent_id)) {
return ($vb_return_as_array) ? array() : null;
} else {
unset($va_tmp[1]);
$va_tmp = array_values($va_tmp);
if ($vb_return_as_array) {
if ($vb_return_all_locales) {
return array(
$vn_row_id => array( // this row_id
$vn_locale_id => array( // base model fields aren't translate-able
$t_instance->get(join('.', $va_tmp))
)
)
);
} else {
return array(
$vn_row_id => $t_instance->get(join('.', $va_tmp))
);
}
} else {
return $t_instance->get(join('.', $va_tmp));
}
}
} else {
if (($va_tmp[1] == 'children') && ($this->isHierarchical())) {
$vb_check_access = is_array($pa_options['checkAccess']) && $this->hasField('access');
$vs_sort = isset($pa_options['sort']) ? $pa_options['sort'] : null;
$vs_sort_direction = (isset($pa_options['sort_direction']) && in_array(strtolower($pa_options['sort_direction']), array('asc', 'desc'))) ? strtolower($pa_options['sort_direction']) : 'asc';
unset($va_tmp[1]); // remove 'children' from field path
$va_tmp = array_values($va_tmp);
$vs_childless_path = join('.', $va_tmp);
$va_data = array();
$va_children_ids = $this->getHierarchyChildren(null, array('idsOnly' => true));
if (is_array($va_children_ids) && sizeof($va_children_ids)) {
$t_instance = $this->getAppDatamodel()->getInstanceByTableNum($this->tableNum());
if (($va_tmp[1] == $this->primaryKey()) && !$vs_sort) {
foreach($va_children_ids as $vn_child_id) {
$va_data[$vn_child_id] = $vn_child_id;
}
} else {
if (method_exists($this, "makeSearchResult")) {
// Use SearchResult lazy loading when available
$vs_table = $this->tableName();
$vs_pk = $vs_table.'.'.$this->primaryKey();
$qr_children = $this->makeSearchResult($this->tableName(), $va_children_ids);
while($qr_children->nextHit()) {
if ($vb_check_access && !in_array($qr_children->get("{$vs_table}.access"), $pa_options['checkAccess'])) { continue; }
$vn_child_id = $qr_children->get($vs_pk);
$vs_sort_key = ($vs_sort) ? $qr_children->get($vs_sort) : 0;
if(!is_array($va_data[$vs_sort_key])) { $va_data[$vs_sort_key] = array(); }
if ($vb_return_as_array) {
$va_data[$vs_sort_key][$vn_child_id] = array_shift($qr_children->get($vs_childless_path, array_merge($pa_options, array('returnAsArray' => $vb_return_as_array, 'returnAllLocales' => $vb_return_all_locales))));
} else {
$va_data[$vs_sort_key][$vn_child_id] = $qr_children->get($vs_childless_path, array_merge($pa_options, array('returnAsArray' => false, 'returnAllLocales' => false)));
}
}
ksort($va_data);
if ($vs_sort_direction && ($vs_sort_direction == 'desc')) { $va_data = array_reverse($va_data); }
$va_sorted_data = array();
foreach($va_data as $vs_sort_key => $va_items) {
foreach($va_items as $vs_k => $vs_v) {
$va_sorted_data[] = $vs_v;
}
}
$va_data = $va_sorted_data;
} else {
// Fall-back to loading records row-by-row (slow)
foreach($va_children_ids as $vn_child_id) {
if ($t_instance->load($vn_child_id)) {
if ($vb_check_access && !in_array($t_instance->get("access"), $pa_options['checkAccess'])) { continue; }
$vs_sort_key = ($vs_sort) ? $t_instance->get($vs_sort) : 0;
if(!is_array($va_data[$vs_sort_key])) { $va_data[$vs_sort_key] = array(); }
if ($vb_return_as_array) {
$va_data[$vs_sort_key][$vn_child_id] = array_shift($t_instance->get($vs_childless_path, array_merge($pa_options, array('returnAsArray' => $vb_return_as_array, 'returnAllLocales' => $vb_return_all_locales))));
} else {
$va_data[$vs_sort_key][$vn_child_id] = $t_instance->get($vs_childless_path, array_merge($pa_options, array('returnAsArray' => false, 'returnAllLocales' => false)));
}
}
}
ksort($va_data);
if ($vs_sort_direction && $vs_sort_direction == 'desc') { $va_data = array_reverse($va_data); }
$va_sorted_data = array();
foreach($va_data as $vs_sort_key => $va_items) {
foreach($va_items as $vs_k => $vs_v) {
$va_sorted_data[] = $vs_v;
}
}
$va_data = $va_sorted_data;
}
}
}
if ($vb_return_as_array) {
return $va_data;
} else {
return join($vs_delimiter, $va_data);
}
}
}
break;
}
} else {
// can't pull fields from other tables!
return $vb_return_as_array ? array() : null;
}
}
if (isset($pa_options["BINARY"]) && $pa_options["BINARY"]) {
return $this->_FIELD_VALUES[$ps_field];
}
if (array_key_exists($ps_field,$this->FIELDS)) {
$ps_field_type = $this->getFieldInfo($ps_field,"FIELD_TYPE");
if ($this->getFieldInfo($ps_field, 'IS_LIFESPAN')) {
$pa_options['isLifespan'] = true;
}
switch ($ps_field_type) {
case (FT_BIT):
$vs_prop = isset($this->_FIELD_VALUES[$ps_field]) ? $this->_FIELD_VALUES[$ps_field] : "";
if (isset($pa_options['convertCodesToDisplayText']) && $pa_options['convertCodesToDisplayText']) {
$vs_prop = (bool)$vs_prop ? _t('yes') : _t('no');
}
return $vs_prop;
break;
case (FT_TEXT):
case (FT_NUMBER):
case (FT_PASSWORD):
$vs_prop = isset($this->_FIELD_VALUES[$ps_field]) ? $this->_FIELD_VALUES[$ps_field] : null;
if (isset($pa_options["FILTER_HTML_SPECIAL_CHARS"]) && ($pa_options["FILTER_HTML_SPECIAL_CHARS"])) {
$vs_prop = htmlentities(html_entity_decode($vs_prop));
}
//
// Convert foreign keys and choice list values to display text is needed
//
if (isset($pa_options['convertCodesToDisplayText']) && $pa_options['convertCodesToDisplayText'] && ($vs_list_code = $this->getFieldInfo($ps_field,"LIST_CODE"))) {
$t_list = new ca_lists();
$vs_prop = $t_list->getItemFromListForDisplayByItemID($vs_list_code, $vs_prop);
} else {
if (isset($pa_options['convertCodesToDisplayText']) && $pa_options['convertCodesToDisplayText'] && ($vs_list_code = $this->getFieldInfo($ps_field,"LIST"))) {
$t_list = new ca_lists();
if (!($vs_tmp = $t_list->getItemFromListForDisplayByItemValue($vs_list_code, $vs_prop))) {
if ($vs_tmp = $this->getChoiceListValue($ps_field, $vs_prop)) {
$vs_prop = $vs_tmp;
}
} else {
$vs_prop = $vs_tmp;
}
} else {
if (isset($pa_options['convertCodesToDisplayText']) && $pa_options['convertCodesToDisplayText'] && ($ps_field === 'locale_id') && ((int)$vs_prop > 0)) {
$t_locale = new ca_locales($vs_prop);
$vs_prop = $t_locale->getName();
} else {
if (isset($pa_options['convertCodesToDisplayText']) && $pa_options['convertCodesToDisplayText'] && (is_array($va_list = $this->getFieldInfo($ps_field,"BOUNDS_CHOICE_LIST")))) {
foreach($va_list as $vs_option => $vs_value) {
if ($vs_value == $vs_prop) {
$vs_prop = $vs_option;
break;
}
}
}
}
}
}
if (
(isset($pa_options["CONVERT_HTML_BREAKS"]) && ($pa_options["CONVERT_HTML_BREAKS"]))
||
(isset($pa_options["convertLineBreaks"]) && ($pa_options["convertLineBreaks"]))
) {
$vs_prop = caConvertLineBreaks($vs_prop);
}
break;
case (FT_DATETIME):
case (FT_TIMESTAMP):
case (FT_HISTORIC_DATETIME):
case (FT_HISTORIC_DATE):
case (FT_DATE):
if (isset($pa_options["GET_DIRECT_DATE"]) && $pa_options["GET_DIRECT_DATE"]) {
$vs_prop = $this->_FIELD_VALUES[$ps_field];
} else {
$o_tep = new TimeExpressionParser();
$vn_timestamp = isset($this->_FIELD_VALUES[$ps_field]) ? $this->_FIELD_VALUES[$ps_field] : 0;
if (($ps_field_type == FT_HISTORIC_DATETIME) || ($ps_field_type == FT_HISTORIC_DATE)) {
$o_tep->setHistoricTimestamps($vn_timestamp, $vn_timestamp);
} else {
$o_tep->setUnixTimestamps($vn_timestamp, $vn_timestamp);
}
if (($ps_field_type == FT_DATE) || ($ps_field_type == FT_HISTORIC_DATE)) {
$vs_prop = $o_tep->getText(array_merge(array('timeOmit' => true), $pa_options));
} else {
$vs_prop = $o_tep->getText($pa_options);
}
}
break;
case (FT_TIME):
if (isset($pa_options["GET_DIRECT_TIME"]) && $pa_options["GET_DIRECT_TIME"]) {
$vs_prop = $this->_FIELD_VALUES[$ps_field];
} else {
$o_tep = new TimeExpressionParser();
$vn_timestamp = isset($this->_FIELD_VALUES[$ps_field]) ? $this->_FIELD_VALUES[$ps_field] : 0;
$o_tep->setTimes($vn_timestamp, $vn_timestamp);
$vs_prop = $o_tep->getText($pa_options);
}
break;
case (FT_DATERANGE):
case (FT_HISTORIC_DATERANGE):
$vs_start_field_name = $this->getFieldInfo($ps_field,"START");
$vs_end_field_name = $this->getFieldInfo($ps_field,"END");
$vn_start_date = isset($this->_FIELD_VALUES[$vs_start_field_name]) ? $this->_FIELD_VALUES[$vs_start_field_name] : null;
$vn_end_date = isset($this->_FIELD_VALUES[$vs_end_field_name]) ? $this->_FIELD_VALUES[$vs_end_field_name] : null;
if (!isset($pa_options["GET_DIRECT_DATE"]) || !$pa_options["GET_DIRECT_DATE"]) {
$o_tep = new TimeExpressionParser();
if ($ps_field_type == FT_HISTORIC_DATERANGE) {
$o_tep->setHistoricTimestamps($vn_start_date, $vn_end_date);
} else {
$o_tep->setUnixTimestamps($vn_start_date, $vn_end_date);
}
$vs_prop = $o_tep->getText($pa_options);
} else {
$vs_prop = array($vn_start_date, $vn_end_date);
}
break;
case (FT_TIMERANGE):
$vs_start_field_name = $this->getFieldInfo($ps_field,"START");
$vs_end_field_name = $this->getFieldInfo($ps_field,"END");
$vn_start_date = isset($this->_FIELD_VALUES[$vs_start_field_name]) ? $this->_FIELD_VALUES[$vs_start_field_name] : null;
$vn_end_date = isset($this->_FIELD_VALUES[$vs_end_field_name]) ? $this->_FIELD_VALUES[$vs_end_field_name] : null;
if (!isset($pa_options["GET_DIRECT_TIME"]) || !$pa_options["GET_DIRECT_TIME"]) {
$o_tep = new TimeExpressionParser();
$o_tep->setTimes($vn_start_date, $vn_end_date);
$vs_prop = $o_tep->getText($pa_options);
} else {
$vs_prop = array($vn_start_date, $vn_end_date);
}
break;
case (FT_TIMECODE):
$o_tp = new TimecodeParser();
$o_tp->setParsedValueInSeconds($this->_FIELD_VALUES[$ps_field]);
$vs_prop = $o_tp->getText(isset($pa_options["TIMECODE_FORMAT"]) ? $pa_options["TIMECODE_FORMAT"] : null);
break;
case (FT_MEDIA):
case (FT_FILE):
if (isset($pa_options["USE_MEDIA_FIELD_VALUES"]) && $pa_options["USE_MEDIA_FIELD_VALUES"]) {
$vs_prop = $this->_FIELD_VALUES[$ps_field];
} else {
$vs_prop = (isset($this->_SET_FILES[$ps_field]['tmp_name']) && $this->_SET_FILES[$ps_field]['tmp_name']) ? $this->_SET_FILES[$ps_field]['tmp_name'] : $this->_FIELD_VALUES[$ps_field];
}
break;
case (FT_VARS):
$vs_prop = (isset($this->_FIELD_VALUES[$ps_field]) && $this->_FIELD_VALUES[$ps_field]) ? $this->_FIELD_VALUES[$ps_field] : null;
break;
}
if (isset($pa_options["QUOTE"]) && $pa_options["QUOTE"])
$vs_prop = $this->quote($ps_field, $vs_prop);
} else {
$this->postError(710,_t("'%1' does not exist in this object", $ps_field),"BaseModel->get()");
return $vb_return_as_array ? array() : null;
}
if (isset($pa_options["URL_ENCODE"]) && $pa_options["URL_ENCODE"]) {
$vs_prop = urlEncode($vs_prop);
}
if (isset($pa_options["ESCAPE_FOR_XML"]) && $pa_options["ESCAPE_FOR_XML"]) {
$vs_prop = escapeForXML($vs_prop);
}
if (!(isset($pa_options["DONT_STRIP_SLASHES"]) && $pa_options["DONT_STRIP_SLASHES"])) {
if (is_string($vs_prop)) { $vs_prop = stripSlashes($vs_prop); }
}
if ((isset($pa_options["template"]) && $pa_options["template"])) {
$vs_template_with_substitution = str_replace("^".$ps_field, $vs_prop, $pa_options["template"]);
$vs_prop = str_replace("^".$this->tableName().".".$ps_field, $vs_prop, $vs_template_with_substitution);
}
if ($vb_return_as_array) {
if ($vb_return_all_locales) {
return array(
$vn_row_id => array( // this row_id
$vn_locale_id => array($vs_prop)
)
);
} else {
return array(
$vn_row_id => $vs_prop
);
}
} else {
return $vs_prop;
}
}
/**
* Set field value(s) for the table row represented by this object
*
* @param string|array string $pa_fields representation of a field name
* or array of string representations of field names
* @param mixed $pm_value value to set the given field(s) to
* @param array $pa_options associative array of options
* possible options (keys):