Ensure fieldsTo array are defined
[civicrm-core.git] / CRM / Core / DAO.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Base Database Access Object class.
14 *
15 * All DAO classes should inherit from this class.
16 *
17 * @package CRM
18 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 */
20
21 if (!defined('DB_DSN_MODE')) {
22 define('DB_DSN_MODE', 'auto');
23 }
24
25 require_once 'PEAR.php';
26 require_once 'DB/DataObject.php';
27
28 require_once 'CRM/Core/I18n.php';
29
30 /**
31 * Class CRM_Core_DAO
32 */
33 class CRM_Core_DAO extends DB_DataObject {
34
35 /**
36 * How many times has this instance been cloned.
37 *
38 * @var int
39 */
40 protected $resultCopies = 0;
41
42 /**
43 * @var null
44 * @deprecated
45 */
46 public static $_nullObject = NULL;
47
48 /**
49 * Icon associated with this entity.
50 *
51 * @var string
52 */
53 public static $_icon = NULL;
54
55 /**
56 * Field to show when displaying a record.
57 *
58 * @var string
59 */
60 public static $_labelField = NULL;
61
62 /**
63 * @var array
64 * @deprecated
65 */
66 public static $_nullArray = [];
67
68 public static $_dbColumnValueCache = NULL;
69 const NOT_NULL = 1, IS_NULL = 2,
70 DB_DAO_NOTNULL = 128,
71 VALUE_SEPARATOR = "\ 1",
72 BULK_INSERT_COUNT = 200,
73 BULK_INSERT_HIGH_COUNT = 200,
74 QUERY_FORMAT_WILDCARD = 1,
75 QUERY_FORMAT_NO_QUOTES = 2,
76
77 /**
78 * Serialized string separated by and bookended with VALUE_SEPARATOR
79 */
80 SERIALIZE_SEPARATOR_BOOKEND = 1,
81 /**
82 * @deprecated format separated by VALUE_SEPARATOR
83 */
84 SERIALIZE_SEPARATOR_TRIMMED = 2,
85 /**
86 * Recommended serialization format
87 */
88 SERIALIZE_JSON = 3,
89 /**
90 * @deprecated format using php serialize()
91 */
92 SERIALIZE_PHP = 4,
93 /**
94 * Comma separated string, no quotes, no spaces
95 */
96 SERIALIZE_COMMA = 5;
97
98 /**
99 * Define entities that shouldn't be created or deleted when creating/ deleting
100 * test objects - this prevents world regions, countries etc from being added / deleted
101 * @var array
102 */
103 public static $_testEntitiesToSkip = [];
104 /**
105 * The factory class for this application.
106 * @var object
107 */
108 public static $_factory = NULL;
109
110 public static $_checkedSqlFunctionsExist = FALSE;
111
112 /**
113 * https://issues.civicrm.org/jira/browse/CRM-17748
114 * internal variable for DAO to hold per-query settings
115 * @var array
116 */
117 protected $_options = [];
118
119 /**
120 * Class constructor.
121 *
122 * @return \CRM_Core_DAO
123 */
124 public function __construct() {
125 $this->initialize();
126 $this->__table = $this->getTableName();
127 }
128
129 /**
130 * Returns localized title of this entity.
131 *
132 * @return string
133 */
134 public static function getEntityTitle() {
135 $className = static::class;
136 CRM_Core_Error::deprecatedWarning("$className needs to be regenerated. Missing getEntityTitle method.");
137 return CRM_Core_DAO_AllCoreTables::getBriefName($className);
138 }
139
140 public function __clone() {
141 if (!empty($this->_DB_resultid)) {
142 $this->resultCopies++;
143 }
144 }
145
146 /**
147 * Class destructor.
148 */
149 public function __destruct() {
150 if ($this->resultCopies === 0) {
151 $this->free();
152 }
153 $this->resultCopies--;
154 }
155
156 /**
157 * Returns the name of this table
158 *
159 * @return string
160 */
161 public static function getTableName() {
162 return self::getLocaleTableName(static::$_tableName ?? NULL);
163 }
164
165 /**
166 * Returns if this table needs to be logged
167 *
168 * @return bool
169 */
170 public function getLog() {
171 return static::$_log ?? FALSE;
172 }
173
174 /**
175 * Initialize the DAO object.
176 *
177 * @param string $dsn
178 * The database connection string.
179 */
180 public static function init($dsn) {
181 Civi::$statics[__CLASS__]['init'] = 1;
182 $options = &PEAR::getStaticProperty('DB_DataObject', 'options');
183 $dsn = CRM_Utils_SQL::autoSwitchDSN($dsn);
184 $options['database'] = $dsn;
185 $options['quote_identifiers'] = TRUE;
186 if (CRM_Utils_SQL::isSSLDSN($dsn)) {
187 // There are two different options arrays.
188 $other_options = &PEAR::getStaticProperty('DB', 'options');
189 $other_options['ssl'] = TRUE;
190 }
191 if (defined('CIVICRM_DAO_DEBUG')) {
192 self::DebugLevel(CIVICRM_DAO_DEBUG);
193 }
194 $factory = new CRM_Contact_DAO_Factory();
195 CRM_Core_DAO::setFactory($factory);
196 CRM_Core_DAO::executeQuery('SET NAMES utf8mb4');
197 CRM_Core_DAO::executeQuery('SET @uniqueID = %1', [1 => [CRM_Utils_Request::id(), 'String']]);
198 }
199
200 /**
201 * @return DB_common
202 */
203 public static function getConnection() {
204 global $_DB_DATAOBJECT;
205 $dao = new CRM_Core_DAO();
206 return $_DB_DATAOBJECT['CONNECTIONS'][$dao->_database_dsn_md5];
207 }
208
209 /**
210 * Disables usage of the ONLY_FULL_GROUP_BY Mode if necessary
211 */
212 public static function disableFullGroupByMode() {
213 $currentModes = CRM_Utils_SQL::getSqlModes();
214 if (in_array('ONLY_FULL_GROUP_BY', $currentModes) && CRM_Utils_SQL::isGroupByModeInDefault()) {
215 $key = array_search('ONLY_FULL_GROUP_BY', $currentModes);
216 unset($currentModes[$key]);
217 CRM_Core_DAO::executeQuery("SET SESSION sql_mode = %1", [1 => [implode(',', $currentModes), 'String']]);
218 }
219 }
220
221 /**
222 * Re-enables ONLY_FULL_GROUP_BY sql_mode as necessary..
223 */
224 public static function reenableFullGroupByMode() {
225 $currentModes = CRM_Utils_SQL::getSqlModes();
226 if (!in_array('ONLY_FULL_GROUP_BY', $currentModes) && CRM_Utils_SQL::isGroupByModeInDefault()) {
227 $currentModes[] = 'ONLY_FULL_GROUP_BY';
228 CRM_Core_DAO::executeQuery("SET SESSION sql_mode = %1", [1 => [implode(',', $currentModes), 'String']]);
229 }
230 }
231
232 /**
233 * @param string $fieldName
234 * @param $fieldDef
235 * @param array $params
236 */
237 protected function assignTestFK($fieldName, $fieldDef, $params) {
238 $required = $fieldDef['required'] ?? NULL;
239 $FKClassName = $fieldDef['FKClassName'] ?? NULL;
240 $dbName = $fieldDef['name'];
241 $daoName = str_replace('_BAO_', '_DAO_', get_class($this));
242
243 // skip the FK if it is not required
244 // if it's contact id we should create even if not required
245 // we'll have a go @ fetching first though
246 // we WILL create campaigns though for so tests with a campaign pseudoconstant will complete
247 if ($FKClassName === 'CRM_Campaign_DAO_Campaign' && $daoName != $FKClassName) {
248 $required = TRUE;
249 }
250 if (!$required && $dbName != 'contact_id') {
251 $fkDAO = new $FKClassName();
252 if ($fkDAO->find(TRUE)) {
253 $this->$dbName = $fkDAO->id;
254 }
255 }
256
257 elseif (in_array($FKClassName, CRM_Core_DAO::$_testEntitiesToSkip)) {
258 $depObject = new $FKClassName();
259 $depObject->find(TRUE);
260 $this->$dbName = $depObject->id;
261 }
262 elseif ($daoName == 'CRM_Member_DAO_MembershipType' && $fieldName == 'member_of_contact_id') {
263 // FIXME: the fields() metadata is not specific enough
264 $depObject = CRM_Core_DAO::createTestObject($FKClassName, ['contact_type' => 'Organization']);
265 $this->$dbName = $depObject->id;
266 }
267 else {
268 //if it is required we need to generate the dependency object first
269 $depObject = CRM_Core_DAO::createTestObject($FKClassName, CRM_Utils_Array::value($dbName, $params, 1));
270 $this->$dbName = $depObject->id;
271 }
272 }
273
274 /**
275 * Generate and assign an arbitrary value to a field of a test object.
276 *
277 * @param string $fieldName
278 * @param array $fieldDef
279 * @param int $counter
280 * The globally-unique ID of the test object.
281 *
282 * @throws \CRM_Core_Exception
283 */
284 protected function assignTestValue($fieldName, &$fieldDef, $counter) {
285 $dbName = $fieldDef['name'];
286 $daoName = get_class($this);
287 $handled = FALSE;
288
289 if (!$handled && $dbName == 'contact_sub_type') {
290 //coming up with a rule to set this is too complex let's not set it
291 $handled = TRUE;
292 }
293
294 // Pick an option value if needed
295 if (!$handled && $fieldDef['type'] !== CRM_Utils_Type::T_BOOLEAN) {
296 $options = $daoName::buildOptions($dbName, 'create');
297 if ($options) {
298 $this->$dbName = key($options);
299 $handled = TRUE;
300 }
301 }
302
303 if (!$handled) {
304 switch ($fieldDef['type']) {
305 case CRM_Utils_Type::T_INT:
306 case CRM_Utils_Type::T_FLOAT:
307 case CRM_Utils_Type::T_MONEY:
308 if (isset($fieldDef['precision'])) {
309 // $object->$dbName = CRM_Utils_Number::createRandomDecimal($value['precision']);
310 $this->$dbName = CRM_Utils_Number::createTruncatedDecimal($counter, $fieldDef['precision']);
311 }
312 else {
313 $this->$dbName = $counter;
314 }
315 break;
316
317 case CRM_Utils_Type::T_BOOLEAN:
318 if (isset($fieldDef['default'])) {
319 $this->$dbName = $fieldDef['default'];
320 }
321 elseif ($fieldDef['name'] == 'is_deleted' || $fieldDef['name'] == 'is_test') {
322 $this->$dbName = 0;
323 }
324 else {
325 $this->$dbName = 1;
326 }
327 break;
328
329 case CRM_Utils_Type::T_DATE:
330 case CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME:
331 $this->$dbName = '19700101';
332 if ($dbName == 'end_date') {
333 // put this in the future
334 $this->$dbName = '20200101';
335 }
336 break;
337
338 case CRM_Utils_Type::T_TIMESTAMP:
339 $this->$dbName = '19700201000000';
340 break;
341
342 case CRM_Utils_Type::T_TIME:
343 throw new CRM_Core_Exception('T_TIME shouldn\'t be used.');
344
345 case CRM_Utils_Type::T_CCNUM:
346 $this->$dbName = '4111 1111 1111 1111';
347 break;
348
349 case CRM_Utils_Type::T_URL:
350 $this->$dbName = 'http://www.civicrm.org';
351 break;
352
353 case CRM_Utils_Type::T_STRING:
354 case CRM_Utils_Type::T_BLOB:
355 case CRM_Utils_Type::T_MEDIUMBLOB:
356 case CRM_Utils_Type::T_TEXT:
357 case CRM_Utils_Type::T_LONGTEXT:
358 case CRM_Utils_Type::T_EMAIL:
359 default:
360 // WAS: if (isset($value['enumValues'])) {
361 // TODO: see if this works with all pseudoconstants
362 if (isset($fieldDef['pseudoconstant'], $fieldDef['pseudoconstant']['callback'])) {
363 if (isset($fieldDef['default'])) {
364 $this->$dbName = $fieldDef['default'];
365 }
366 else {
367 $options = CRM_Core_PseudoConstant::get($daoName, $fieldName);
368 if (is_array($options)) {
369 $this->$dbName = $options[0];
370 }
371 else {
372 $defaultValues = explode(',', $options);
373 $this->$dbName = $defaultValues[0];
374 }
375 }
376 }
377 else {
378 $this->$dbName = $dbName . '_' . $counter;
379 $maxlength = $fieldDef['maxlength'] ?? NULL;
380 if ($maxlength > 0 && strlen($this->$dbName) > $maxlength) {
381 $this->$dbName = substr($this->$dbName, 0, $fieldDef['maxlength']);
382 }
383 }
384 }
385 }
386 }
387
388 /**
389 * Reset the DAO object.
390 *
391 * DAO is kinda crappy in that there is an unwritten rule of one query per DAO.
392 *
393 * We attempt to get around this crappy restriction by resetting some of DAO's internal fields. Use this with caution
394 */
395 public function reset() {
396
397 foreach (array_keys($this->table()) as $field) {
398 unset($this->$field);
399 }
400
401 /**
402 * reset the various DB_DAO structures manually
403 */
404 $this->_query = [];
405 $this->whereAdd();
406 $this->selectAdd();
407 $this->joinAdd();
408 }
409
410 /**
411 * @param string $tableName
412 *
413 * @return string
414 */
415 public static function getLocaleTableName($tableName) {
416 global $dbLocale;
417 if ($dbLocale) {
418 $tables = CRM_Core_I18n_Schema::schemaStructureTables();
419 if (in_array($tableName, $tables)) {
420 return $tableName . $dbLocale;
421 }
422 }
423 return $tableName;
424 }
425
426 /**
427 * Execute a query by the current DAO, localizing it along the way (if needed).
428 *
429 * @param string $query
430 * The SQL query for execution.
431 * @param bool $i18nRewrite
432 * Whether to rewrite the query.
433 *
434 * @return object
435 * the current DAO object after the query execution
436 */
437 public function query($query, $i18nRewrite = TRUE) {
438 // rewrite queries that should use $dbLocale-based views for multi-language installs
439 global $dbLocale, $_DB_DATAOBJECT;
440
441 if (empty($_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5])) {
442 // Will force connection to be populated per CRM-20541.
443 new CRM_Core_DAO();
444 }
445
446 $conn = &$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5];
447 $orig_options = $conn->options;
448 $this->_setDBOptions($this->_options);
449
450 if ($i18nRewrite and $dbLocale) {
451 $query = CRM_Core_I18n_Schema::rewriteQuery($query);
452 }
453
454 $ret = parent::query($query);
455
456 $this->_setDBOptions($orig_options);
457 return $ret;
458 }
459
460 /**
461 * Static function to set the factory instance for this class.
462 *
463 * @param object $factory
464 * The factory application object.
465 */
466 public static function setFactory(&$factory) {
467 self::$_factory = &$factory;
468 }
469
470 /**
471 * Factory method to instantiate a new object from a table name.
472 *
473 * @param string $table
474 * @return \DataObject|\PEAR_Error
475 */
476 public function factory($table = '') {
477 if (!isset(self::$_factory)) {
478 return parent::factory($table);
479 }
480
481 return self::$_factory->create($table);
482 }
483
484 /**
485 * Initialization for all DAO objects. Since we access DB_DO programatically
486 * we need to set the links manually.
487 */
488 public function initialize() {
489 $this->_connect();
490 if (empty(Civi::$statics[__CLASS__]['init'])) {
491 // CRM_Core_DAO::init() must be called before CRM_Core_DAO->initialize().
492 // This occurs very early in bootstrap - error handlers may not be wired up.
493 echo "Inconsistent system initialization sequence. Premature access of (" . get_class($this) . ")";
494 CRM_Utils_System::civiExit();
495 }
496 }
497
498 /**
499 * Defines the default key as 'id'.
500 *
501 * @return array
502 */
503 public function keys() {
504 static $keys;
505 if (!isset($keys)) {
506 $keys = ['id'];
507 }
508 return $keys;
509 }
510
511 /**
512 * Tells DB_DataObject which keys use autoincrement.
513 * 'id' is autoincrementing by default.
514 *
515 *
516 * @return array
517 */
518 public function sequenceKey() {
519 static $sequenceKeys;
520 if (!isset($sequenceKeys)) {
521 $sequenceKeys = ['id', TRUE];
522 }
523 return $sequenceKeys;
524 }
525
526 /**
527 * Returns list of FK relationships.
528 *
529 *
530 * @return CRM_Core_Reference_Basic[]
531 */
532 public static function getReferenceColumns() {
533 return [];
534 }
535
536 /**
537 * Returns all the column names of this table.
538 *
539 *
540 * @return array
541 */
542 public static function &fields() {
543 $result = NULL;
544 return $result;
545 }
546
547 /**
548 * Returns all usable fields, indexed by name.
549 *
550 * This function differs from fields() in that it indexes by name rather than unique_name.
551 *
552 * It excludes fields not added yet by pending upgrades.
553 * This avoids problems with trying to SELECT a field that exists in code but has not yet been added to the db.
554 *
555 * @param bool $checkPermissions
556 * Filter by field permissions.
557 * @return array
558 */
559 public static function getSupportedFields($checkPermissions = FALSE) {
560 $fields = array_column((array) static::fields(), NULL, 'name');
561
562 // Exclude fields yet not added by pending upgrades
563 $dbVer = \CRM_Core_BAO_Domain::version();
564 $daoExt = defined(static::class . '::EXT') ? constant(static::class . '::EXT') : NULL;
565 if ($fields && $daoExt === 'civicrm' && version_compare($dbVer, \CRM_Utils_System::version()) < 0) {
566 $fields = array_filter($fields, function($field) use ($dbVer) {
567 $add = $field['add'] ?? '1.0.0';
568 if (substr_count($add, '.') < 2) {
569 $add .= '.alpha1';
570 }
571 return version_compare($dbVer, $add, '>=');
572 });
573 }
574
575 // Exclude fields the user does not have permission for
576 if ($checkPermissions) {
577 $fields = array_filter($fields, function($field) {
578 return empty($field['permission']) || CRM_Core_Permission::check($field['permission']);
579 });
580 }
581
582 return $fields;
583 }
584
585 /**
586 * Get/set an associative array of table columns
587 *
588 * @return array
589 * (associative)
590 */
591 public function table() {
592 $fields = $this->fields();
593
594 $table = [];
595 if ($fields) {
596 foreach ($fields as $name => $value) {
597 $table[$value['name']] = $value['type'];
598 if (!empty($value['required'])) {
599 $table[$value['name']] += self::DB_DAO_NOTNULL;
600 }
601 }
602 }
603
604 return $table;
605 }
606
607 /**
608 * Save DAO object.
609 *
610 * @param bool $hook
611 *
612 * @return CRM_Core_DAO
613 */
614 public function save($hook = TRUE) {
615 $eventID = uniqid();
616 if (!empty($this->id)) {
617 if ($hook) {
618 $preEvent = new \Civi\Core\DAO\Event\PreUpdate($this);
619 $preEvent->eventID = $eventID;
620 \Civi::dispatcher()->dispatch("civi.dao.preUpdate", $preEvent);
621 }
622
623 $result = $this->update();
624
625 if ($hook) {
626 $event = new \Civi\Core\DAO\Event\PostUpdate($this, $result);
627 $event->eventID = $eventID;
628 \Civi::dispatcher()->dispatch("civi.dao.postUpdate", $event);
629 }
630 $this->clearDbColumnValueCache();
631 }
632 else {
633 if ($hook) {
634 $preEvent = new \Civi\Core\DAO\Event\PreUpdate($this);
635 $preEvent->eventID = $eventID;
636 \Civi::dispatcher()->dispatch("civi.dao.preInsert", $preEvent);
637 }
638
639 $result = $this->insert();
640
641 if ($hook) {
642 $event = new \Civi\Core\DAO\Event\PostUpdate($this, $result);
643 $event->eventID = $eventID;
644 \Civi::dispatcher()->dispatch("civi.dao.postInsert", $event);
645 }
646 }
647 $this->free();
648
649 if ($hook) {
650 CRM_Utils_Hook::postSave($this);
651 }
652
653 return $this;
654 }
655
656 /**
657 * Deletes items from table which match current objects variables.
658 *
659 * Returns the true on success
660 *
661 * for example
662 *
663 * Designed to be extended
664 *
665 * $object = new mytable();
666 * $object->ID=123;
667 * echo $object->delete(); // builds a conditon
668 *
669 * $object = new mytable();
670 * $object->whereAdd('age > 12');
671 * $object->limit(1);
672 * $object->orderBy('age DESC');
673 * $object->delete(true); // dont use object vars, use the conditions, limit and order.
674 *
675 * @param bool $useWhere (optional) If DB_DATAOBJECT_WHEREADD_ONLY is passed in then
676 * we will build the condition only using the whereAdd's. Default is to
677 * build the condition only using the object parameters.
678 *
679 * * @return mixed Int (No. of rows affected) on success, false on failure, 0 on no data affected
680 */
681 public function delete($useWhere = FALSE) {
682 $preEvent = new \Civi\Core\DAO\Event\PreDelete($this);
683 \Civi::dispatcher()->dispatch("civi.dao.preDelete", $preEvent);
684
685 $result = parent::delete($useWhere);
686
687 $event = new \Civi\Core\DAO\Event\PostDelete($this, $result);
688 \Civi::dispatcher()->dispatch("civi.dao.postDelete", $event);
689 $this->free();
690
691 $this->clearDbColumnValueCache();
692
693 return $result;
694 }
695
696 /**
697 * @param bool $created
698 */
699 public function log($created = FALSE) {
700 static $cid = NULL;
701
702 if (!$this->getLog()) {
703 return;
704 }
705
706 if (!$cid) {
707 $session = CRM_Core_Session::singleton();
708 $cid = $session->get('userID');
709 }
710
711 // return is we dont have handle to FK
712 if (!$cid) {
713 return;
714 }
715
716 $dao = new CRM_Core_DAO_Log();
717 $dao->entity_table = $this->getTableName();
718 $dao->entity_id = $this->id;
719 $dao->modified_id = $cid;
720 $dao->modified_date = date("YmdHis");
721 $dao->insert();
722 }
723
724 /**
725 * Given an associative array of name/value pairs, extract all the values
726 * that belong to this object and initialize the object with said values
727 *
728 * @param array $params
729 * Array of name/value pairs to save.
730 *
731 * @return bool
732 * Did we copy all null values into the object
733 */
734 public function copyValues($params) {
735 $allNull = TRUE;
736 foreach ($this->fields() as $uniqueName => $field) {
737 $dbName = $field['name'];
738 if (array_key_exists($dbName, $params)) {
739 $value = $params[$dbName];
740 $exists = TRUE;
741 }
742 elseif (array_key_exists($uniqueName, $params)) {
743 $value = $params[$uniqueName];
744 $exists = TRUE;
745 }
746 else {
747 $exists = FALSE;
748 }
749
750 // if there is no value then make the variable NULL
751 if ($exists) {
752 if ($value === '') {
753 $this->$dbName = 'null';
754 }
755 elseif (is_array($value) && !empty($field['serialize'])) {
756 $this->$dbName = CRM_Core_DAO::serializeField($value, $field['serialize']);
757 $allNull = FALSE;
758 }
759 else {
760 $maxLength = $field['maxlength'] ?? NULL;
761 if (!is_array($value) && $maxLength && mb_strlen($value) > $maxLength && empty($field['pseudoconstant'])) {
762 // No ts() since this is a sysadmin-y string not seen by general users.
763 Civi::log()->warning('A string for field {dbName} has been truncated. The original string was {value}.', ['dbName' => $dbName, 'value' => $value]);
764 // The string is too long - what to do what to do? Well losing data is generally bad so let's truncate
765 $value = CRM_Utils_String::ellipsify($value, $maxLength);
766 }
767 $this->$dbName = $value;
768 $allNull = FALSE;
769 }
770 }
771 }
772 return $allNull;
773 }
774
775 /**
776 * Store all the values from this object in an associative array
777 * this is a destructive store, calling function is responsible
778 * for keeping sanity of id's.
779 *
780 * @param object $object
781 * The object that we are extracting data from.
782 * @param array $values
783 * (reference ) associative array of name/value pairs.
784 */
785 public static function storeValues(&$object, &$values) {
786 $fields = $object->fields();
787 foreach ($fields as $name => $value) {
788 $dbName = $value['name'];
789 if (isset($object->$dbName) && $object->$dbName !== 'null') {
790 $values[$dbName] = $object->$dbName;
791 if ($name != $dbName) {
792 $values[$name] = $object->$dbName;
793 }
794 }
795 }
796 }
797
798 /**
799 * Create an attribute for this specific field. We only do this for strings and text
800 *
801 * @param array $field
802 * The field under task.
803 *
804 * @return array|null
805 * the attributes for the object
806 */
807 public static function makeAttribute($field) {
808 if ($field) {
809 if (CRM_Utils_Array::value('type', $field) == CRM_Utils_Type::T_STRING) {
810 $maxLength = $field['maxlength'] ?? NULL;
811 $size = $field['size'] ?? NULL;
812 if ($maxLength || $size) {
813 $attributes = [];
814 if ($maxLength) {
815 $attributes['maxlength'] = $maxLength;
816 }
817 if ($size) {
818 $attributes['size'] = $size;
819 }
820 return $attributes;
821 }
822 }
823 elseif (CRM_Utils_Array::value('type', $field) == CRM_Utils_Type::T_TEXT) {
824 $rows = $field['rows'] ?? NULL;
825 if (!isset($rows)) {
826 $rows = 2;
827 }
828 $cols = $field['cols'] ?? NULL;
829 if (!isset($cols)) {
830 $cols = 80;
831 }
832
833 $attributes = [];
834 $attributes['rows'] = $rows;
835 $attributes['cols'] = $cols;
836 return $attributes;
837 }
838 elseif (CRM_Utils_Array::value('type', $field) == CRM_Utils_Type::T_INT || CRM_Utils_Array::value('type', $field) == CRM_Utils_Type::T_FLOAT || CRM_Utils_Array::value('type', $field) == CRM_Utils_Type::T_MONEY) {
839 $attributes['size'] = 6;
840 $attributes['maxlength'] = 14;
841 return $attributes;
842 }
843 }
844 return NULL;
845 }
846
847 /**
848 * Get the size and maxLength attributes for this text field.
849 * (or for all text fields) in the DAO object.
850 *
851 * @param string $class
852 * Name of DAO class.
853 * @param string $fieldName
854 * Field that i'm interested in or null if.
855 * you want the attributes for all DAO text fields
856 *
857 * @return array
858 * assoc array of name => attribute pairs
859 */
860 public static function getAttribute($class, $fieldName = NULL) {
861 $object = new $class();
862 $fields = $object->fields();
863 if ($fieldName != NULL) {
864 $field = $fields[$fieldName] ?? NULL;
865 return self::makeAttribute($field);
866 }
867 else {
868 $attributes = [];
869 foreach ($fields as $name => $field) {
870 $attribute = self::makeAttribute($field);
871 if ($attribute) {
872 $attributes[$name] = $attribute;
873 }
874 }
875
876 if (!empty($attributes)) {
877 return $attributes;
878 }
879 }
880 return NULL;
881 }
882
883 /**
884 * Create or update a record from supplied params.
885 *
886 * If 'id' is supplied, an existing record will be updated
887 * Otherwise a new record will be created.
888 *
889 * @param array $record
890 *
891 * @return $this
892 * @throws \CRM_Core_Exception
893 */
894 public static function writeRecord(array $record): CRM_Core_DAO {
895 $hook = empty($record['id']) ? 'create' : 'edit';
896 $className = CRM_Core_DAO_AllCoreTables::getCanonicalClassName(static::class);
897 if ($className === 'CRM_Core_DAO') {
898 throw new CRM_Core_Exception('Function writeRecord must be called on a subclass of CRM_Core_DAO');
899 }
900 $entityName = CRM_Core_DAO_AllCoreTables::getBriefName($className);
901
902 \CRM_Utils_Hook::pre($hook, $entityName, $record['id'] ?? NULL, $record);
903 $instance = new $className();
904 $instance->copyValues($record);
905 $instance->save();
906 \CRM_Utils_Hook::post($hook, $entityName, $instance->id, $instance);
907
908 return $instance;
909 }
910
911 /**
912 * Delete a record from supplied params.
913 *
914 * @param array $record
915 * 'id' is required.
916 * @return CRM_Core_DAO
917 * @throws CRM_Core_Exception
918 */
919 public static function deleteRecord(array $record) {
920 $className = CRM_Core_DAO_AllCoreTables::getCanonicalClassName(static::class);
921 if ($className === 'CRM_Core_DAO') {
922 throw new CRM_Core_Exception('Function deleteRecord must be called on a subclass of CRM_Core_DAO');
923 }
924 $entityName = CRM_Core_DAO_AllCoreTables::getBriefName($className);
925 if (empty($record['id'])) {
926 throw new CRM_Core_Exception("Cannot delete {$entityName} with no id.");
927 }
928
929 CRM_Utils_Hook::pre('delete', $entityName, $record['id'], $record);
930 $instance = new $className();
931 $instance->id = $record['id'];
932 if (!$instance->delete()) {
933 throw new CRM_Core_Exception("Could not delete {$entityName} id {$record['id']}");
934 }
935 CRM_Utils_Hook::post('delete', $entityName, $record['id'], $instance);
936
937 return $instance;
938 }
939
940 /**
941 * Check if there is a record with the same name in the db.
942 *
943 * @param string $value
944 * The value of the field we are checking.
945 * @param string $daoName
946 * The dao object name.
947 * @param string $daoID
948 * The id of the object being updated. u can change your name.
949 * as long as there is no conflict
950 * @param string $fieldName
951 * The name of the field in the DAO.
952 *
953 * @param string $domainID
954 * The id of the domain. Object exists only for the given domain.
955 *
956 * @return bool
957 * true if object exists
958 */
959 public static function objectExists($value, $daoName, $daoID, $fieldName = 'name', $domainID = NULL) {
960 $object = new $daoName();
961 $object->$fieldName = $value;
962 if ($domainID) {
963 $object->domain_id = $domainID;
964 }
965
966 if ($object->find(TRUE)) {
967 return $daoID && $object->id == $daoID;
968 }
969 else {
970 return TRUE;
971 }
972 }
973
974 /**
975 * Check if there is a given column in a specific table.
976 *
977 * @deprecated
978 * @see CRM_Core_BAO_SchemaHandler::checkIfFieldExists
979 *
980 * @param string $tableName
981 * @param string $columnName
982 * @param bool $i18nRewrite
983 * Whether to rewrite the query on multilingual setups.
984 *
985 * @return bool
986 * true if exists, else false
987 */
988 public static function checkFieldExists($tableName, $columnName, $i18nRewrite = TRUE) {
989 CRM_Core_Error::deprecatedFunctionWarning('CRM_Core_BAO_SchemaHandler::checkIfFieldExists');
990 return CRM_Core_BAO_SchemaHandler::checkIfFieldExists($tableName, $columnName, $i18nRewrite);
991 }
992
993 /**
994 * Scans all the tables using a slow query and table name.
995 *
996 * @return array
997 */
998 public static function getTableNames() {
999 $dao = CRM_Core_DAO::executeQuery(
1000 "SELECT TABLE_NAME
1001 FROM information_schema.TABLES
1002 WHERE TABLE_SCHEMA = '" . CRM_Core_DAO::getDatabaseName() . "'
1003 AND TABLE_NAME LIKE 'civicrm_%'
1004 AND TABLE_NAME NOT LIKE 'civicrm_import_job_%'
1005 AND TABLE_NAME NOT LIKE '%_temp%'
1006 ");
1007
1008 while ($dao->fetch()) {
1009 $values[] = $dao->TABLE_NAME;
1010 }
1011 return $values;
1012 }
1013
1014 /**
1015 * @param int $maxTablesToCheck
1016 *
1017 * @return bool
1018 */
1019 public static function isDBMyISAM($maxTablesToCheck = 10) {
1020 return CRM_Core_DAO::singleValueQuery(
1021 "SELECT count(*)
1022 FROM information_schema.TABLES
1023 WHERE ENGINE = 'MyISAM'
1024 AND TABLE_SCHEMA = '" . CRM_Core_DAO::getDatabaseName() . "'
1025 AND TABLE_NAME LIKE 'civicrm_%'
1026 AND TABLE_NAME NOT LIKE 'civicrm_import_job_%'
1027 AND TABLE_NAME NOT LIKE '%_temp%'
1028 AND TABLE_NAME NOT LIKE 'civicrm_tmp_%'
1029 ");
1030 }
1031
1032 /**
1033 * Get the name of the CiviCRM database.
1034 *
1035 * @return string
1036 */
1037 public static function getDatabaseName() {
1038 $daoObj = new CRM_Core_DAO();
1039 return $daoObj->database();
1040 }
1041
1042 /**
1043 * Checks if a constraint exists for a specified table.
1044 *
1045 * @param string $tableName
1046 * @param string $constraint
1047 *
1048 * @return bool
1049 * true if constraint exists, false otherwise
1050 *
1051 * @throws \CRM_Core_Exception
1052 */
1053 public static function checkConstraintExists($tableName, $constraint) {
1054 static $show = [];
1055
1056 if (!array_key_exists($tableName, $show)) {
1057 $query = "SHOW CREATE TABLE $tableName";
1058 $dao = CRM_Core_DAO::executeQuery($query, [], TRUE, NULL, FALSE, FALSE);
1059
1060 if (!$dao->fetch()) {
1061 throw new CRM_Core_Exception('query failed');
1062 }
1063
1064 $show[$tableName] = $dao->Create_Table;
1065 }
1066
1067 return (bool) preg_match("/\b$constraint\b/i", $show[$tableName]);
1068 }
1069
1070 /**
1071 * Checks if CONSTRAINT keyword exists for a specified table.
1072 *
1073 * @param array $tables
1074 *
1075 * @throws CRM_Core_Exception
1076 *
1077 * @return bool
1078 * true if CONSTRAINT keyword exists, false otherwise
1079 */
1080 public static function schemaRequiresRebuilding($tables = ["civicrm_contact"]) {
1081 $show = [];
1082 foreach ($tables as $tableName) {
1083 if (!array_key_exists($tableName, $show)) {
1084 $query = "SHOW CREATE TABLE $tableName";
1085 $dao = CRM_Core_DAO::executeQuery($query, [], TRUE, NULL, FALSE, FALSE);
1086
1087 if (!$dao->fetch()) {
1088 throw new CRM_Core_Exception('Show create table failed.');
1089 }
1090
1091 $show[$tableName] = $dao->Create_Table;
1092 }
1093
1094 $result = (bool) preg_match("/\bCONSTRAINT\b\s/i", $show[$tableName]);
1095 if ($result == TRUE) {
1096 continue;
1097 }
1098 else {
1099 return FALSE;
1100 }
1101 }
1102 return TRUE;
1103 }
1104
1105 /**
1106 * Checks if the FK constraint name is in the format 'FK_tableName_columnName'
1107 * for a specified column of a table.
1108 *
1109 * @param string $tableName
1110 * @param string $columnName
1111 *
1112 * @return bool
1113 * true if in format, false otherwise
1114 *
1115 * @throws \CRM_Core_Exception
1116 */
1117 public static function checkFKConstraintInFormat($tableName, $columnName) {
1118 static $show = [];
1119
1120 if (!array_key_exists($tableName, $show)) {
1121 $query = "SHOW CREATE TABLE $tableName";
1122 $dao = CRM_Core_DAO::executeQuery($query);
1123
1124 if (!$dao->fetch()) {
1125 throw new CRM_Core_Exception('query failed');
1126 }
1127
1128 $show[$tableName] = $dao->Create_Table;
1129 }
1130 $constraint = "`FK_{$tableName}_{$columnName}`";
1131 $pattern = "/\bCONSTRAINT\b\s+%s\s+\bFOREIGN\s+KEY\b\s/i";
1132 return (bool) preg_match(sprintf($pattern, $constraint), $show[$tableName]);
1133 }
1134
1135 /**
1136 * Check whether a specific column in a specific table has always the same value.
1137 *
1138 * @param string $tableName
1139 * @param string $columnName
1140 * @param string $columnValue
1141 *
1142 * @return bool
1143 * true if the value is always $columnValue, false otherwise
1144 */
1145 public static function checkFieldHasAlwaysValue($tableName, $columnName, $columnValue) {
1146 $query = "SELECT * FROM $tableName WHERE $columnName != '$columnValue'";
1147 $dao = CRM_Core_DAO::executeQuery($query);
1148 $result = $dao->fetch() ? FALSE : TRUE;
1149 return $result;
1150 }
1151
1152 /**
1153 * Check whether a specific column in a specific table is always NULL.
1154 *
1155 * @param string $tableName
1156 * @param string $columnName
1157 *
1158 * @return bool
1159 * true if if the value is always NULL, false otherwise
1160 */
1161 public static function checkFieldIsAlwaysNull($tableName, $columnName) {
1162 $query = "SELECT * FROM $tableName WHERE $columnName IS NOT NULL";
1163 $dao = CRM_Core_DAO::executeQuery($query);
1164 $result = $dao->fetch() ? FALSE : TRUE;
1165 return $result;
1166 }
1167
1168 /**
1169 * Checks if this DAO's table ought to exist.
1170 *
1171 * If there are pending DB updates, this function compares the CiviCRM version of the table to the current schema version.
1172 *
1173 * @return bool
1174 * @throws CRM_Core_Exception
1175 */
1176 public static function tableHasBeenAdded() {
1177 if (CRM_Utils_System::version() === CRM_Core_BAO_Domain::version()) {
1178 return TRUE;
1179 }
1180 $daoExt = defined(static::class . '::EXT') ? constant(static::class . '::EXT') : NULL;
1181 $daoVersion = defined(static::class . '::TABLE_ADDED') ? constant(static::class . '::TABLE_ADDED') : '1.0';
1182 return !($daoExt === 'civicrm' && version_compare(CRM_Core_BAO_Domain::version(), $daoVersion, '<'));
1183 }
1184
1185 /**
1186 * Check if there is a given table in the database.
1187 *
1188 * @param string $tableName
1189 *
1190 * @return bool
1191 * true if exists, else false
1192 */
1193 public static function checkTableExists($tableName) {
1194 $query = "
1195 SHOW TABLES
1196 LIKE %1
1197 ";
1198 $params = [1 => [$tableName, 'String']];
1199
1200 $dao = CRM_Core_DAO::executeQuery($query, $params);
1201 return (bool) $dao->fetch();
1202 }
1203
1204 /**
1205 * Check if a given table has data.
1206 *
1207 * @param string $tableName
1208 * @return bool
1209 * TRUE if $tableName has at least one record.
1210 */
1211 public static function checkTableHasData($tableName) {
1212 $c = CRM_Core_DAO::singleValueQuery(sprintf('SELECT count(*) c FROM `%s`', $tableName));
1213 return $c > 0;
1214 }
1215
1216 /**
1217 * @param $version
1218 * @deprecated
1219 * @return bool
1220 */
1221 public function checkVersion($version) {
1222 CRM_Core_Error::deprecatedFunctionWarning('CRM_Core_BAO_Domain::version');
1223 $dbVersion = CRM_Core_BAO_Domain::version();
1224 return trim($version) == trim($dbVersion);
1225 }
1226
1227 /**
1228 * Find a DAO object for the given ID and return it.
1229 *
1230 * @param int $id
1231 * Id of the DAO object being searched for.
1232 *
1233 * @return CRM_Core_DAO
1234 * Object of the type of the class that called this function.
1235 *
1236 * @throws Exception
1237 */
1238 public static function findById($id) {
1239 $object = new static();
1240 $object->id = $id;
1241 if (!$object->find(TRUE)) {
1242 throw new Exception("Unable to find a " . get_called_class() . " with id {$id}.");
1243 }
1244 return $object;
1245 }
1246
1247 /**
1248 * Returns all results as array-encoded records.
1249 *
1250 * @return array
1251 */
1252 public function fetchAll($k = FALSE, $v = FALSE, $method = FALSE) {
1253 $result = [];
1254 while ($this->fetch()) {
1255 $result[] = $this->toArray();
1256 }
1257 return $result;
1258 }
1259
1260 /**
1261 * Return the results as PHP generator.
1262 *
1263 * @param string $type
1264 * Whether the generator yields 'dao' objects or 'array's.
1265 */
1266 public function fetchGenerator($type = 'dao') {
1267 while ($this->fetch()) {
1268 switch ($type) {
1269 case 'dao':
1270 yield $this;
1271 break;
1272
1273 case 'array':
1274 yield $this->toArray();
1275 break;
1276
1277 default:
1278 throw new \RuntimeException("Invalid record type ($type)");
1279 }
1280 }
1281 }
1282
1283 /**
1284 * Returns a singular value.
1285 *
1286 * @return mixed|NULL
1287 */
1288 public function fetchValue() {
1289 $result = $this->getDatabaseResult();
1290 $row = $result->fetchRow();
1291 $ret = NULL;
1292 if ($row) {
1293 $ret = $row[0];
1294 }
1295 $this->free();
1296 return $ret;
1297 }
1298
1299 /**
1300 * Get all the result records as mapping between columns.
1301 *
1302 * @param string $keyColumn
1303 * Ex: "name"
1304 * @param string $valueColumn
1305 * Ex: "label"
1306 * @return array
1307 * Ex: ["foo" => "The Foo Bar", "baz" => "The Baz Qux"]
1308 */
1309 public function fetchMap($keyColumn, $valueColumn) {
1310 $result = [];
1311 while ($this->fetch()) {
1312 $result[$this->{$keyColumn}] = $this->{$valueColumn};
1313 }
1314 return $result;
1315 }
1316
1317 /**
1318 * Given a DAO name, a column name and a column value, find the record and GET the value of another column in that record
1319 *
1320 * @param string $daoName
1321 * Name of the DAO (Example: CRM_Contact_DAO_Contact to retrieve value from a contact).
1322 * @param int $searchValue
1323 * Value of the column you want to search by.
1324 * @param string $returnColumn
1325 * Name of the column you want to GET the value of.
1326 * @param string $searchColumn
1327 * Name of the column you want to search by.
1328 * @param bool $force
1329 * Skip use of the cache.
1330 *
1331 * @return string|int|null
1332 * Value of $returnColumn in the retrieved record
1333 *
1334 * @throws \CRM_Core_Exception
1335 */
1336 public static function getFieldValue($daoName, $searchValue, $returnColumn = 'name', $searchColumn = 'id', $force = FALSE) {
1337 if (
1338 empty($searchValue) ||
1339 trim(strtolower($searchValue)) == 'null'
1340 ) {
1341 // adding this here since developers forget to check for an id
1342 // or for the 'null' (which is a bad DAO kludge)
1343 // and hence we get the first value in the db
1344 throw new CRM_Core_Exception('getFieldValue failed');
1345 }
1346
1347 self::$_dbColumnValueCache = self::$_dbColumnValueCache ?? [];
1348
1349 while (strpos($daoName, '_BAO_') !== FALSE) {
1350 $daoName = get_parent_class($daoName);
1351 }
1352
1353 if ($force ||
1354 empty(self::$_dbColumnValueCache[$daoName][$searchColumn][$searchValue]) ||
1355 !array_key_exists($returnColumn, self::$_dbColumnValueCache[$daoName][$searchColumn][$searchValue])
1356 ) {
1357 $object = new $daoName();
1358 $object->$searchColumn = $searchValue;
1359 $object->selectAdd();
1360 $object->selectAdd($returnColumn);
1361
1362 $result = NULL;
1363 if ($object->find(TRUE)) {
1364 $result = $object->$returnColumn;
1365 }
1366
1367 self::$_dbColumnValueCache[$daoName][$searchColumn][$searchValue][$returnColumn] = $result;
1368 }
1369 return self::$_dbColumnValueCache[$daoName][$searchColumn][$searchValue][$returnColumn];
1370 }
1371
1372 /**
1373 * Given a DAO name, a column name and a column value, find the record and SET the value of another column in that record
1374 *
1375 * @param string $daoName
1376 * Name of the DAO (Example: CRM_Contact_DAO_Contact to retrieve value from a contact).
1377 * @param int $searchValue
1378 * Value of the column you want to search by.
1379 * @param string $setColumn
1380 * Name of the column you want to SET the value of.
1381 * @param string $setValue
1382 * SET the setColumn to this value.
1383 * @param string $searchColumn
1384 * Name of the column you want to search by.
1385 *
1386 * @return bool
1387 * true if we found and updated the object, else false
1388 */
1389 public static function setFieldValue($daoName, $searchValue, $setColumn, $setValue, $searchColumn = 'id') {
1390 $object = new $daoName();
1391 $object->selectAdd();
1392 $object->selectAdd("$searchColumn, $setColumn");
1393 $object->$searchColumn = $searchValue;
1394 $result = FALSE;
1395 if ($object->find(TRUE)) {
1396 $object->$setColumn = $setValue;
1397 if ($object->save()) {
1398 $result = TRUE;
1399 }
1400 }
1401 $object->free();
1402 return $result;
1403 }
1404
1405 /**
1406 * Get sort string.
1407 *
1408 * @param array|object $sort either array or CRM_Utils_Sort
1409 * @param string $default
1410 * Default sort value.
1411 *
1412 * @return string
1413 */
1414 public static function getSortString($sort, $default = NULL) {
1415 // check if sort is of type CRM_Utils_Sort
1416 if (is_a($sort, 'CRM_Utils_Sort')) {
1417 return $sort->orderBy();
1418 }
1419
1420 $sortString = '';
1421
1422 // is it an array specified as $field => $sortDirection ?
1423 if ($sort) {
1424 foreach ($sort as $k => $v) {
1425 $sortString .= "$k $v,";
1426 }
1427 return rtrim($sortString, ',');
1428 }
1429 return $default;
1430 }
1431
1432 /**
1433 * Fetch object based on array of properties.
1434 *
1435 * @param string $daoName
1436 * Name of the dao object.
1437 * @param array $params
1438 * (reference ) an assoc array of name/value pairs.
1439 * @param array $defaults
1440 * (reference ) an assoc array to hold the flattened values.
1441 * @param array $returnProperities
1442 * An assoc array of fields that need to be returned, eg array( 'first_name', 'last_name').
1443 *
1444 * @return object
1445 * an object of type referenced by daoName
1446 */
1447 public static function commonRetrieve($daoName, &$params, &$defaults, $returnProperities = NULL) {
1448 $object = new $daoName();
1449 $object->copyValues($params);
1450
1451 // return only specific fields if returnproperties are sent
1452 if (!empty($returnProperities)) {
1453 $object->selectAdd();
1454 $object->selectAdd(implode(',', $returnProperities));
1455 }
1456
1457 if ($object->find(TRUE)) {
1458 self::storeValues($object, $defaults);
1459 return $object;
1460 }
1461 return NULL;
1462 }
1463
1464 /**
1465 * Delete the object records that are associated with this contact.
1466 *
1467 * @param string $daoName
1468 * Name of the dao object.
1469 * @param int $contactId
1470 * Id of the contact to delete.
1471 */
1472 public static function deleteEntityContact($daoName, $contactId) {
1473 $object = new $daoName();
1474
1475 $object->entity_table = 'civicrm_contact';
1476 $object->entity_id = $contactId;
1477 $object->delete();
1478 }
1479
1480 /**
1481 * Execute an unbuffered query.
1482 *
1483 * This is a wrapper around new functionality exposed with CRM-17748.
1484 *
1485 * @param string $query query to be executed
1486 *
1487 * @param array $params
1488 * @param bool $abort
1489 * @param null $daoName
1490 * @param bool $freeDAO
1491 * @param bool $i18nRewrite
1492 * @param bool $trapException
1493 *
1494 * @return CRM_Core_DAO
1495 * Object that points to an unbuffered result set
1496 */
1497 public static function executeUnbufferedQuery(
1498 $query,
1499 $params = [],
1500 $abort = TRUE,
1501 $daoName = NULL,
1502 $freeDAO = FALSE,
1503 $i18nRewrite = TRUE,
1504 $trapException = FALSE
1505 ) {
1506
1507 return self::executeQuery(
1508 $query,
1509 $params,
1510 $abort,
1511 $daoName,
1512 $freeDAO,
1513 $i18nRewrite,
1514 $trapException,
1515 ['result_buffering' => 0]
1516 );
1517 }
1518
1519 /**
1520 * Execute a query.
1521 *
1522 * @param string $query
1523 * Query to be executed.
1524 *
1525 * @param array $params
1526 * @param bool $abort
1527 * @param null $daoName
1528 * @param bool $freeDAO
1529 * @param bool $i18nRewrite
1530 * @param bool $trapException
1531 * @param array $options
1532 *
1533 * @return CRM_Core_DAO|object
1534 * object that holds the results of the query
1535 * NB - if this is defined as just returning a DAO phpstorm keeps pointing
1536 * out all the properties that are not part of the DAO
1537 */
1538 public static function &executeQuery(
1539 $query,
1540 $params = [],
1541 $abort = TRUE,
1542 $daoName = NULL,
1543 $freeDAO = FALSE,
1544 $i18nRewrite = TRUE,
1545 $trapException = FALSE,
1546 $options = []
1547 ) {
1548 $queryStr = self::composeQuery($query, $params, $abort);
1549
1550 if (!$daoName) {
1551 $dao = new CRM_Core_DAO();
1552 }
1553 else {
1554 $dao = new $daoName();
1555 }
1556
1557 if ($trapException) {
1558 CRM_Core_Error::deprecatedFunctionWarning('calling functions should handle exceptions');
1559 $errorScope = CRM_Core_TemporaryErrorScope::ignoreException();
1560 }
1561
1562 if ($dao->isValidOption($options)) {
1563 $dao->setOptions($options);
1564 }
1565
1566 $result = $dao->query($queryStr, $i18nRewrite);
1567
1568 // since it is unbuffered, ($dao->N==0) is true. This blocks the standard fetch() mechanism.
1569 if (CRM_Utils_Array::value('result_buffering', $options) === 0) {
1570 $dao->N = TRUE;
1571 }
1572
1573 if (is_a($result, 'DB_Error')) {
1574 CRM_Core_Error::deprecatedFunctionWarning('calling functions should handle exceptions');
1575 return $result;
1576 }
1577
1578 return $dao;
1579 }
1580
1581 /**
1582 * Wrapper to validate internal DAO options before passing to DB_mysql/DB_Common level
1583 *
1584 * @param array $options
1585 *
1586 * @return bool
1587 * Provided options are valid
1588 */
1589 public function isValidOption($options) {
1590 $isValid = FALSE;
1591 $validOptions = [
1592 'result_buffering',
1593 'persistent',
1594 'ssl',
1595 'portability',
1596 ];
1597
1598 if (empty($options)) {
1599 return $isValid;
1600 }
1601
1602 foreach (array_keys($options) as $option) {
1603 if (!in_array($option, $validOptions)) {
1604 return FALSE;
1605 }
1606 $isValid = TRUE;
1607 }
1608
1609 return $isValid;
1610 }
1611
1612 /**
1613 * Execute a query and get the single result.
1614 *
1615 * @param string $query
1616 * Query to be executed.
1617 * @param array $params
1618 * @param bool $abort
1619 * @param bool $i18nRewrite
1620 * @return string|null
1621 * the result of the query if any
1622 *
1623 */
1624 public static function &singleValueQuery(
1625 $query,
1626 $params = [],
1627 $abort = TRUE,
1628 $i18nRewrite = TRUE
1629 ) {
1630 $queryStr = self::composeQuery($query, $params, $abort);
1631
1632 static $_dao = NULL;
1633
1634 if (!$_dao) {
1635 $_dao = new CRM_Core_DAO();
1636 }
1637
1638 $_dao->query($queryStr, $i18nRewrite);
1639
1640 $result = $_dao->getDatabaseResult();
1641 $ret = NULL;
1642 if ($result) {
1643 $row = $result->fetchRow();
1644 if ($row) {
1645 $ret = $row[0];
1646 }
1647 }
1648 $_dao->free();
1649 return $ret;
1650 }
1651
1652 /**
1653 * Compose the query by merging the parameters into it.
1654 *
1655 * @param string $query
1656 * @param array $params
1657 * @param bool $abort
1658 *
1659 * @return string
1660 * @throws CRM_Core_Exception
1661 */
1662 public static function composeQuery($query, $params = [], $abort = TRUE) {
1663 $tr = [];
1664 foreach ($params as $key => $item) {
1665 if (is_numeric($key)) {
1666 if (CRM_Utils_Type::validate($item[0], $item[1]) !== NULL) {
1667 $item[0] = self::escapeString($item[0]);
1668 if ($item[1] == 'String' ||
1669 $item[1] == 'Memo' ||
1670 $item[1] == 'Link'
1671 ) {
1672 // Support class constants stipulating wildcard characters and/or
1673 // non-quoting of strings. Also support legacy code which may be
1674 // passing in TRUE or 1 for $item[2], which used to indicate the
1675 // use of wildcard characters.
1676 if (!empty($item[2])) {
1677 if ($item[2] & CRM_Core_DAO::QUERY_FORMAT_WILDCARD || $item[2] === TRUE) {
1678 $item[0] = "'%{$item[0]}%'";
1679 }
1680 elseif (!($item[2] & CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES)) {
1681 $item[0] = "'{$item[0]}'";
1682 }
1683 }
1684 else {
1685 $item[0] = "'{$item[0]}'";
1686 }
1687 }
1688
1689 if (($item[1] == 'Date' || $item[1] == 'Timestamp') &&
1690 strlen($item[0]) == 0
1691 ) {
1692 $item[0] = 'null';
1693 }
1694
1695 $tr['%' . $key] = $item[0];
1696 }
1697 elseif ($abort) {
1698 throw new CRM_Core_Exception("{$item[0]} is not of type {$item[1]}");
1699 }
1700 }
1701 }
1702
1703 return strtr($query, $tr);
1704 }
1705
1706 /**
1707 * @param null $ids
1708 */
1709 public static function freeResult($ids = NULL) {
1710 global $_DB_DATAOBJECT;
1711
1712 if (!$ids) {
1713 if (!$_DB_DATAOBJECT ||
1714 !isset($_DB_DATAOBJECT['RESULTS'])
1715 ) {
1716 return;
1717 }
1718 $ids = array_keys($_DB_DATAOBJECT['RESULTS']);
1719 }
1720
1721 foreach ($ids as $id) {
1722 if (isset($_DB_DATAOBJECT['RESULTS'][$id])) {
1723 $_DB_DATAOBJECT['RESULTS'][$id]->free();
1724 unset($_DB_DATAOBJECT['RESULTS'][$id]);
1725 }
1726
1727 if (isset($_DB_DATAOBJECT['RESULTFIELDS'][$id])) {
1728 unset($_DB_DATAOBJECT['RESULTFIELDS'][$id]);
1729 }
1730 }
1731 }
1732
1733 /**
1734 * Make a shallow copy of an object and all the fields in the object.
1735 *
1736 * @param string $daoName
1737 * Name of the dao.
1738 * @param array $criteria
1739 * Array of all the fields & values.
1740 * on which basis to copy
1741 * @param array $newData
1742 * Array of all the fields & values.
1743 * to be copied besides the other fields
1744 * @param string $fieldsFix
1745 * Array of fields that you want to prefix/suffix/replace.
1746 * @param string $blockCopyOfDependencies
1747 * Fields that you want to block from.
1748 * getting copied
1749 * @param bool $blockCopyofCustomValues
1750 * Case when you don't want to copy the custom values set in a
1751 * template as it will override/ignore the submitted custom values
1752 *
1753 * @return CRM_Core_DAO|bool
1754 * the newly created copy of the object. False if none created.
1755 */
1756 public static function copyGeneric($daoName, $criteria, $newData = NULL, $fieldsFix = NULL, $blockCopyOfDependencies = NULL, $blockCopyofCustomValues = FALSE) {
1757 $object = new $daoName();
1758 $newObject = FALSE;
1759 if (!$newData) {
1760 $object->id = $criteria['id'];
1761 }
1762 else {
1763 foreach ($criteria as $key => $value) {
1764 $object->$key = $value;
1765 }
1766 }
1767
1768 $object->find();
1769 while ($object->fetch()) {
1770
1771 // all the objects except with $blockCopyOfDependencies set
1772 // be copied - addresses #CRM-1962
1773
1774 if ($blockCopyOfDependencies && $object->$blockCopyOfDependencies) {
1775 break;
1776 }
1777
1778 $newObject = new $daoName();
1779
1780 $fields = $object->fields();
1781 $fieldsToPrefix = [];
1782 $fieldsToSuffix = [];
1783 $fieldsToReplace = [];
1784 if (!empty($fieldsFix['prefix'])) {
1785 $fieldsToPrefix = $fieldsFix['prefix'];
1786 }
1787 if (!empty($fieldsFix['suffix'])) {
1788 $fieldsToSuffix = $fieldsFix['suffix'];
1789 }
1790 if (!empty($fieldsFix['replace'])) {
1791 $fieldsToReplace = $fieldsFix['replace'];
1792 }
1793
1794 $localizableFields = FALSE;
1795 foreach ($fields as $name => $value) {
1796 if ($name == 'id' || $value['name'] == 'id') {
1797 // copy everything but the id!
1798 continue;
1799 }
1800
1801 $dbName = $value['name'];
1802 $type = CRM_Utils_Type::typeToString($value['type']);
1803 $newObject->$dbName = $object->$dbName;
1804 if (isset($fieldsToPrefix[$dbName])) {
1805 $newObject->$dbName = $fieldsToPrefix[$dbName] . $newObject->$dbName;
1806 }
1807 if (isset($fieldsToSuffix[$dbName])) {
1808 $newObject->$dbName .= $fieldsToSuffix[$dbName];
1809 }
1810 if (isset($fieldsToReplace[$dbName])) {
1811 $newObject->$dbName = $fieldsToReplace[$dbName];
1812 }
1813
1814 if ($type == 'Timestamp' || $type == 'Date') {
1815 $newObject->$dbName = CRM_Utils_Date::isoToMysql($newObject->$dbName);
1816 }
1817
1818 if (!empty($value['localizable'])) {
1819 $localizableFields = TRUE;
1820 }
1821
1822 if ($newData) {
1823 $newObject->copyValues($newData);
1824 }
1825 }
1826 $newObject->save();
1827
1828 // ensure we copy all localized fields as well
1829 if (CRM_Core_I18n::isMultilingual() && $localizableFields) {
1830 global $dbLocale;
1831 $locales = CRM_Core_I18n::getMultilingual();
1832 $curLocale = CRM_Core_I18n::getLocale();
1833 // loop on other locales
1834 foreach ($locales as $locale) {
1835 if ($locale != $curLocale) {
1836 // setLocale doesn't seems to be reliable to set dbLocale and we only need to change the db locale
1837 $dbLocale = '_' . $locale;
1838 $newObject->copyLocalizable($object->id, $newObject->id, $fieldsToPrefix, $fieldsToSuffix, $fieldsToReplace);
1839 }
1840 }
1841 // restore dbLocale to starting value
1842 $dbLocale = '_' . $curLocale;
1843 }
1844
1845 if (!$blockCopyofCustomValues) {
1846 $newObject->copyCustomFields($object->id, $newObject->id);
1847 }
1848 CRM_Utils_Hook::post('create', CRM_Core_DAO_AllCoreTables::getBriefName($daoName), $newObject->id, $newObject);
1849 }
1850
1851 return $newObject;
1852 }
1853
1854 /**
1855 * Method that copies localizable fields from an old entity to a new one.
1856 *
1857 * Fixes bug dev/core#2479,
1858 * where non current locale fields are copied from current locale losing translation when copying
1859 *
1860 * @param int $entityID
1861 * @param int $newEntityID
1862 * @param array $fieldsToPrefix
1863 * @param array $fieldsToSuffix
1864 * @param array $fieldsToReplace
1865 */
1866 protected function copyLocalizable($entityID, $newEntityID, $fieldsToPrefix, $fieldsToSuffix, $fieldsToReplace) {
1867 $entity = get_class($this);
1868 $object = new $entity();
1869 $object->id = $entityID;
1870 $object->find();
1871
1872 $newObject = new $entity();
1873 $newObject->id = $newEntityID;
1874
1875 $newObject->find();
1876
1877 if ($object->fetch() && $newObject->fetch()) {
1878
1879 $fields = $object->fields();
1880 foreach ($fields as $name => $value) {
1881
1882 if ($name == 'id' || $value['name'] == 'id') {
1883 // copy everything but the id!
1884 continue;
1885 }
1886
1887 // only copy localizable fields
1888 if (!$value['localizable']) {
1889 continue;
1890 }
1891
1892 $dbName = $value['name'];
1893 $type = CRM_Utils_Type::typeToString($value['type']);
1894 $newObject->$dbName = $object->$dbName;
1895 if (isset($fieldsToPrefix[$dbName])) {
1896 $newObject->$dbName = $fieldsToPrefix[$dbName] . $newObject->$dbName;
1897 }
1898 if (isset($fieldsToSuffix[$dbName])) {
1899 $newObject->$dbName .= $fieldsToSuffix[$dbName];
1900 }
1901 if (isset($fieldsToReplace[$dbName])) {
1902 $newObject->$dbName = $fieldsToReplace[$dbName];
1903 }
1904
1905 if ($type == 'Timestamp' || $type == 'Date') {
1906 $newObject->$dbName = CRM_Utils_Date::isoToMysql($newObject->$dbName);
1907 }
1908
1909 }
1910 $newObject->save();
1911
1912 }
1913 }
1914
1915 /**
1916 * Method that copies custom fields values from an old entity to a new one.
1917 *
1918 * Fixes bug CRM-19302,
1919 * where if a custom field of File type was present, left both events using the same file,
1920 * breaking download URL's for the old event.
1921 *
1922 * @todo the goal here is to clean this up so that it works for any entity. Copy Generic already DOES some custom field stuff
1923 * but it seems to be bypassed & perhaps less good than this (or this just duplicates it...)
1924 *
1925 * @param int $entityID
1926 * @param int $newEntityID
1927 */
1928 public function copyCustomFields($entityID, $newEntityID) {
1929 $entity = CRM_Core_DAO_AllCoreTables::getBriefName(get_class($this));
1930 $tableName = CRM_Core_DAO_AllCoreTables::getTableForClass(get_class($this));
1931 // Obtain custom values for old event
1932 $customParams = $htmlType = [];
1933 $customValues = CRM_Core_BAO_CustomValueTable::getEntityValues($entityID, $entity);
1934
1935 // If custom values present, we copy them
1936 if (!empty($customValues)) {
1937 // Get Field ID's and identify File type attributes, to handle file copying.
1938 $fieldIds = implode(', ', array_keys($customValues));
1939 $sql = "SELECT id FROM civicrm_custom_field WHERE html_type = 'File' AND id IN ( {$fieldIds} )";
1940 $result = CRM_Core_DAO::executeQuery($sql);
1941
1942 // Build array of File type fields
1943 while ($result->fetch()) {
1944 $htmlType[] = $result->id;
1945 }
1946
1947 // Build params array of custom values
1948 foreach ($customValues as $field => $value) {
1949 if ($value !== NULL) {
1950 // Handle File type attributes
1951 if (in_array($field, $htmlType)) {
1952 $fileValues = CRM_Core_BAO_File::path($value, $entityID);
1953 $customParams["custom_{$field}_-1"] = [
1954 'name' => CRM_Utils_File::duplicate($fileValues[0]),
1955 'type' => $fileValues[1],
1956 ];
1957 }
1958 // Handle other types
1959 else {
1960 $customParams["custom_{$field}_-1"] = $value;
1961 }
1962 }
1963 }
1964
1965 // Save Custom Fields for new Event
1966 CRM_Core_BAO_CustomValueTable::postProcess($customParams, $tableName, $newEntityID, $entity);
1967 }
1968
1969 // copy activity attachments ( if any )
1970 CRM_Core_BAO_File::copyEntityFile($tableName, $entityID, $tableName, $newEntityID);
1971 }
1972
1973 /**
1974 * Cascade update through related entities.
1975 *
1976 * @param string $daoName
1977 * @param $fromId
1978 * @param $toId
1979 * @param array $newData
1980 *
1981 * @return CRM_Core_DAO|null
1982 */
1983 public static function cascadeUpdate($daoName, $fromId, $toId, $newData = []) {
1984 $object = new $daoName();
1985 $object->id = $fromId;
1986
1987 if ($object->find(TRUE)) {
1988 $newObject = new $daoName();
1989 $newObject->id = $toId;
1990
1991 if ($newObject->find(TRUE)) {
1992 $fields = $object->fields();
1993 foreach ($fields as $name => $value) {
1994 if ($name == 'id' || $value['name'] == 'id') {
1995 // copy everything but the id!
1996 continue;
1997 }
1998
1999 $colName = $value['name'];
2000 $newObject->$colName = $object->$colName;
2001
2002 if (substr($name, -5) == '_date' ||
2003 substr($name, -10) == '_date_time'
2004 ) {
2005 $newObject->$colName = CRM_Utils_Date::isoToMysql($newObject->$colName);
2006 }
2007 }
2008 foreach ($newData as $k => $v) {
2009 $newObject->$k = $v;
2010 }
2011 $newObject->save();
2012 return $newObject;
2013 }
2014 }
2015 return NULL;
2016 }
2017
2018 /**
2019 * Given the component id, compute the contact id
2020 * since its used for things like send email
2021 *
2022 * @param $componentIDs
2023 * @param string $tableName
2024 * @param string $idField
2025 *
2026 * @return array
2027 */
2028 public static function getContactIDsFromComponent($componentIDs, $tableName, $idField = 'id') {
2029 $contactIDs = [];
2030
2031 if (empty($componentIDs)) {
2032 return $contactIDs;
2033 }
2034
2035 $IDs = implode(',', $componentIDs);
2036 $query = "
2037 SELECT contact_id
2038 FROM $tableName
2039 WHERE $idField IN ( $IDs )
2040 ";
2041
2042 $dao = CRM_Core_DAO::executeQuery($query);
2043 while ($dao->fetch()) {
2044 $contactIDs[] = $dao->contact_id;
2045 }
2046 return $contactIDs;
2047 }
2048
2049 /**
2050 * Fetch object based on array of properties.
2051 *
2052 * @param string $daoName
2053 * Name of the dao object.
2054 * @param string $fieldIdName
2055 * @param int $fieldId
2056 * @param $details
2057 * @param array $returnProperities
2058 * An assoc array of fields that need to be returned, eg array( 'first_name', 'last_name').
2059 *
2060 * @return object
2061 * an object of type referenced by daoName
2062 */
2063 public static function commonRetrieveAll($daoName, $fieldIdName = 'id', $fieldId, &$details, $returnProperities = NULL) {
2064 require_once str_replace('_', DIRECTORY_SEPARATOR, $daoName) . ".php";
2065 $object = new $daoName();
2066 $object->$fieldIdName = $fieldId;
2067
2068 // return only specific fields if returnproperties are sent
2069 if (!empty($returnProperities)) {
2070 $object->selectAdd();
2071 $object->selectAdd('id');
2072 $object->selectAdd(implode(',', $returnProperities));
2073 }
2074
2075 $object->find();
2076 while ($object->fetch()) {
2077 $defaults = [];
2078 self::storeValues($object, $defaults);
2079 $details[$object->id] = $defaults;
2080 }
2081
2082 return $details;
2083 }
2084
2085 /**
2086 * Drop all CiviCRM tables.
2087 *
2088 * @throws \CRM_Core_Exception
2089 */
2090 public static function dropAllTables() {
2091
2092 // first drop all the custom tables we've created
2093 CRM_Core_BAO_CustomGroup::dropAllTables();
2094
2095 // drop all multilingual views
2096 CRM_Core_I18n_Schema::dropAllViews();
2097
2098 CRM_Utils_File::sourceSQLFile(CIVICRM_DSN,
2099 dirname(__FILE__) . DIRECTORY_SEPARATOR .
2100 '..' . DIRECTORY_SEPARATOR .
2101 '..' . DIRECTORY_SEPARATOR .
2102 'sql' . DIRECTORY_SEPARATOR .
2103 'civicrm_drop.mysql'
2104 );
2105 }
2106
2107 /**
2108 * @param $string
2109 *
2110 * @return string
2111 */
2112 public static function escapeString($string) {
2113 static $_dao = NULL;
2114 if (!$_dao) {
2115 // If this is an atypical case (e.g. preparing .sql file before CiviCRM
2116 // has been installed), then we fallback DB-less str_replace escaping, as
2117 // we can't use mysqli_real_escape_string, as there is no DB connection.
2118 // Note: In typical usage, escapeString() will only check one conditional
2119 // ("if !$_dao") rather than two conditionals ("if !defined(DSN)")
2120 if (!defined('CIVICRM_DSN')) {
2121 // See http://php.net/manual/en/mysqli.real-escape-string.php for the
2122 // list of characters mysqli_real_escape_string escapes.
2123 $search = ["\\", "\x00", "\n", "\r", "'", '"', "\x1a"];
2124 $replace = ["\\\\", "\\0", "\\n", "\\r", "\'", '\"', "\\Z"];
2125 return str_replace($search, $replace, $string);
2126 }
2127 $_dao = new CRM_Core_DAO();
2128 }
2129 return $_dao->escape($string);
2130 }
2131
2132 /**
2133 * Escape a list of strings for use with "WHERE X IN (...)" queries.
2134 *
2135 * @param array $strings
2136 * @param string $default
2137 * the value to use if $strings has no elements.
2138 * @return string
2139 * eg "abc","def","ghi"
2140 */
2141 public static function escapeStrings($strings, $default = NULL) {
2142 static $_dao = NULL;
2143 if (!$_dao) {
2144 $_dao = new CRM_Core_DAO();
2145 }
2146
2147 if (empty($strings)) {
2148 return $default;
2149 }
2150
2151 $escapes = array_map([$_dao, 'escape'], $strings);
2152 return '"' . implode('","', $escapes) . '"';
2153 }
2154
2155 /**
2156 * @param $string
2157 *
2158 * @return string
2159 */
2160 public static function escapeWildCardString($string) {
2161 // CRM-9155
2162 // ensure we escape the single characters % and _ which are mysql wild
2163 // card characters and could come in via sortByCharacter
2164 // note that mysql does not escape these characters
2165 if ($string && in_array($string,
2166 ['%', '_', '%%', '_%']
2167 )
2168 ) {
2169 return '\\' . $string;
2170 }
2171
2172 return self::escapeString($string);
2173 }
2174
2175 /**
2176 * Creates a test object, including any required objects it needs via recursion
2177 * createOnly: only create in database, do not store or return the objects (useful for perf testing)
2178 * ONLY USE FOR TESTING
2179 *
2180 * @param string $daoName
2181 * @param array $params
2182 * @param int $numObjects
2183 * @param bool $createOnly
2184 *
2185 * @return object|array|NULL
2186 * NULL if $createOnly. A single object if $numObjects==1. Otherwise, an array of multiple objects.
2187 */
2188 public static function createTestObject(
2189 $daoName,
2190 $params = [],
2191 $numObjects = 1,
2192 $createOnly = FALSE
2193 ) {
2194 //this is a test function also backtrace is set for the test suite it sometimes unsets itself
2195 // so we re-set here in case
2196 $config = CRM_Core_Config::singleton();
2197 $config->backtrace = TRUE;
2198
2199 static $counter = 0;
2200 CRM_Core_DAO::$_testEntitiesToSkip = [
2201 'CRM_Core_DAO_Worldregion',
2202 'CRM_Core_DAO_StateProvince',
2203 'CRM_Core_DAO_Country',
2204 'CRM_Core_DAO_Domain',
2205 'CRM_Financial_DAO_FinancialType',
2206 //because valid ones exist & we use pick them due to pseudoconstant can't reliably create & delete these
2207 ];
2208
2209 // Prefer to instantiate BAO's instead of DAO's (when possible)
2210 // so that assignTestValue()/assignTestFK() can be overloaded.
2211 $baoName = str_replace('_DAO_', '_BAO_', $daoName);
2212 if ($baoName === 'CRM_Financial_BAO_FinancialTrxn') {
2213 // OMG OMG OMG this is so incredibly bad. The BAO is insanely named.
2214 // @todo create a new class called what the BAO SHOULD be
2215 // that extends BAO-crazy-name.... migrate.
2216 $baoName = 'CRM_Core_BAO_FinancialTrxn';
2217 }
2218 if (class_exists($baoName)) {
2219 $daoName = $baoName;
2220 }
2221
2222 for ($i = 0; $i < $numObjects; ++$i) {
2223
2224 ++$counter;
2225 /** @var CRM_Core_DAO $object */
2226 $object = new $daoName();
2227
2228 $fields = $object->fields();
2229 foreach ($fields as $fieldName => $fieldDef) {
2230 $dbName = $fieldDef['name'];
2231 $FKClassName = $fieldDef['FKClassName'] ?? NULL;
2232
2233 if (isset($params[$dbName]) && !is_array($params[$dbName])) {
2234 $object->$dbName = $params[$dbName];
2235 }
2236
2237 elseif ($dbName != 'id') {
2238 if ($FKClassName != NULL) {
2239 $object->assignTestFK($fieldName, $fieldDef, $params);
2240 continue;
2241 }
2242 else {
2243 $object->assignTestValue($fieldName, $fieldDef, $counter);
2244 }
2245 }
2246 }
2247
2248 $object->save();
2249
2250 if (!$createOnly) {
2251 $objects[$i] = $object;
2252 }
2253 else {
2254 unset($object);
2255 }
2256 }
2257
2258 if ($createOnly) {
2259 return NULL;
2260 }
2261 elseif ($numObjects == 1) {
2262 return $objects[0];
2263 }
2264 else {
2265 return $objects;
2266 }
2267 }
2268
2269 /**
2270 * Deletes the this object plus any dependent objects that are associated with it.
2271 * ONLY USE FOR TESTING
2272 *
2273 * @param string $daoName
2274 * @param array $params
2275 */
2276 public static function deleteTestObjects($daoName, $params = []) {
2277 //this is a test function also backtrace is set for the test suite it sometimes unsets itself
2278 // so we re-set here in case
2279 $config = CRM_Core_Config::singleton();
2280 $config->backtrace = TRUE;
2281
2282 $object = new $daoName();
2283 $object->id = $params['id'] ?? NULL;
2284
2285 // array(array(0 => $daoName, 1 => $daoParams))
2286 $deletions = [];
2287 if ($object->find(TRUE)) {
2288
2289 $fields = $object->fields();
2290 foreach ($fields as $name => $value) {
2291
2292 $dbName = $value['name'];
2293
2294 $FKClassName = $value['FKClassName'] ?? NULL;
2295 $required = $value['required'] ?? NULL;
2296 if ($FKClassName != NULL
2297 && $object->$dbName
2298 && !in_array($FKClassName, CRM_Core_DAO::$_testEntitiesToSkip)
2299 && ($required || $dbName == 'contact_id')
2300 //I'm a bit stuck on this one - we might need to change the singleValueAlter so that the entities don't share a contact
2301 // to make this test process pass - line below makes pass for now
2302 && $dbName != 'member_of_contact_id'
2303 ) {
2304 // x
2305 $deletions[] = [$FKClassName, ['id' => $object->$dbName]];
2306 }
2307 }
2308 }
2309
2310 $object->delete();
2311
2312 foreach ($deletions as $deletion) {
2313 CRM_Core_DAO::deleteTestObjects($deletion[0], $deletion[1]);
2314 }
2315 }
2316
2317 /**
2318 * Set defaults when creating new entity.
2319 * (don't call this set defaults as already in use with different signature in some places)
2320 *
2321 * @param array $params
2322 * @param $defaults
2323 */
2324 public static function setCreateDefaults(&$params, $defaults) {
2325 if (!empty($params['id'])) {
2326 return;
2327 }
2328 foreach ($defaults as $key => $value) {
2329 if (!array_key_exists($key, $params) || $params[$key] === NULL) {
2330 $params[$key] = $value;
2331 }
2332 }
2333 }
2334
2335 /**
2336 * @param string $prefix
2337 * @param bool $addRandomString
2338 * @param null $string
2339 *
2340 * @return string
2341 * @deprecated
2342 * @see CRM_Utils_SQL_TempTable
2343 */
2344 public static function createTempTableName($prefix = 'civicrm', $addRandomString = TRUE, $string = NULL) {
2345 CRM_Core_Error::deprecatedFunctionWarning('Use CRM_Utils_SQL_TempTable interface to create temporary tables');
2346 $tableName = $prefix . "_temp";
2347
2348 if ($addRandomString) {
2349 if ($string) {
2350 $tableName .= "_" . $string;
2351 }
2352 else {
2353 $tableName .= "_" . md5(uniqid('', TRUE));
2354 }
2355 }
2356 return $tableName;
2357 }
2358
2359 /**
2360 * @param bool $view
2361 * @param bool $trigger
2362 *
2363 * @return bool
2364 */
2365 public static function checkTriggerViewPermission($view = TRUE, $trigger = TRUE) {
2366 if (\Civi::settings()->get('logging_no_trigger_permission')) {
2367 return TRUE;
2368 }
2369 // test for create view and trigger permissions and if allowed, add the option to go multilingual and logging
2370 $dao = new CRM_Core_DAO();
2371 try {
2372 if ($view) {
2373 $dao->query('CREATE OR REPLACE VIEW civicrm_domain_view AS SELECT * FROM civicrm_domain');
2374 $dao->query('DROP VIEW IF EXISTS civicrm_domain_view');
2375 }
2376
2377 if ($trigger) {
2378 $dao->query('CREATE TRIGGER civicrm_domain_trigger BEFORE INSERT ON civicrm_domain FOR EACH ROW BEGIN END');
2379 $dao->query('DROP TRIGGER IF EXISTS civicrm_domain_trigger');
2380 }
2381 }
2382 catch (Exception $e) {
2383 return FALSE;
2384 }
2385
2386 return TRUE;
2387 }
2388
2389 /**
2390 * @param null $message
2391 * @param bool $printDAO
2392 */
2393 public static function debugPrint($message = NULL, $printDAO = TRUE) {
2394 CRM_Utils_System::xMemory("{$message}: ");
2395
2396 if ($printDAO) {
2397 global $_DB_DATAOBJECT;
2398 $q = [];
2399 foreach (array_keys($_DB_DATAOBJECT['RESULTS']) as $id) {
2400 $q[] = $_DB_DATAOBJECT['RESULTS'][$id]->query;
2401 }
2402 CRM_Core_Error::debug('_DB_DATAOBJECT', $q);
2403 }
2404 }
2405
2406 /**
2407 * Build a list of triggers via hook and add them to (err, reconcile them
2408 * with) the database.
2409 *
2410 * @param string $tableName
2411 * the specific table requiring a rebuild; or NULL to rebuild all tables.
2412 * @param bool $force
2413 * @deprecated
2414 *
2415 * @see CRM-9716
2416 */
2417 public static function triggerRebuild($tableName = NULL, $force = FALSE) {
2418 Civi::service('sql_triggers')->rebuild($tableName, $force);
2419 }
2420
2421 /**
2422 * Because sql functions are sometimes lost, esp during db migration, we check here to avoid numerous support requests
2423 * @see http://issues.civicrm.org/jira/browse/CRM-13822
2424 * TODO: Alternative solutions might be
2425 * * Stop using functions and find another way to strip numeric characters from phones
2426 * * Give better error messages (currently a missing fn fatals with "unknown error")
2427 */
2428 public static function checkSqlFunctionsExist() {
2429 if (!self::$_checkedSqlFunctionsExist) {
2430 self::$_checkedSqlFunctionsExist = TRUE;
2431 $dao = CRM_Core_DAO::executeQuery("SHOW function status WHERE db = database() AND name = 'civicrm_strip_non_numeric'");
2432 if (!$dao->fetch()) {
2433 self::triggerRebuild();
2434 }
2435 }
2436 }
2437
2438 /**
2439 * Wrapper function to drop triggers.
2440 *
2441 * @param string $tableName
2442 * the specific table requiring a rebuild; or NULL to rebuild all tables.
2443 * @deprecated
2444 */
2445 public static function dropTriggers($tableName = NULL) {
2446 Civi::service('sql_triggers')->dropTriggers($tableName);
2447 }
2448
2449 /**
2450 * @param array $info
2451 * per hook_civicrm_triggerInfo.
2452 * @param string $onlyTableName
2453 * the specific table requiring a rebuild; or NULL to rebuild all tables.
2454 * @deprecated
2455 */
2456 public static function createTriggers(&$info, $onlyTableName = NULL) {
2457 Civi::service('sql_triggers')->createTriggers($info, $onlyTableName);
2458 }
2459
2460 /**
2461 * Given a list of fields, create a list of references.
2462 *
2463 * @param string $className
2464 * BAO/DAO class name.
2465 * @return array<CRM_Core_Reference_Interface>
2466 */
2467 public static function createReferenceColumns($className) {
2468 $result = [];
2469 $fields = $className::fields();
2470 foreach ($fields as $field) {
2471 if (isset($field['pseudoconstant'], $field['pseudoconstant']['optionGroupName'])) {
2472 $result[] = new CRM_Core_Reference_OptionValue(
2473 $className::getTableName(),
2474 $field['name'],
2475 'civicrm_option_value',
2476 CRM_Utils_Array::value('keyColumn', $field['pseudoconstant'], 'value'),
2477 $field['pseudoconstant']['optionGroupName']
2478 );
2479 }
2480 }
2481 return $result;
2482 }
2483
2484 /**
2485 * Find all records which refer to this entity.
2486 *
2487 * @return array
2488 * Array of objects referencing this
2489 */
2490 public function findReferences() {
2491 $links = self::getReferencesToTable(static::getTableName());
2492
2493 $occurrences = [];
2494 foreach ($links as $refSpec) {
2495 /** @var $refSpec CRM_Core_Reference_Interface */
2496 $daoName = CRM_Core_DAO_AllCoreTables::getClassForTable($refSpec->getReferenceTable());
2497 $result = $refSpec->findReferences($this);
2498 if ($result) {
2499 while ($result->fetch()) {
2500 $obj = new $daoName();
2501 $obj->id = $result->id;
2502 $occurrences[] = $obj;
2503 }
2504 }
2505 }
2506
2507 return $occurrences;
2508 }
2509
2510 /**
2511 * @return array
2512 * each item has keys:
2513 * - name: string
2514 * - type: string
2515 * - count: int
2516 * - table: string|null SQL table name
2517 * - key: string|null SQL column name
2518 */
2519 public function getReferenceCounts() {
2520 $links = self::getReferencesToTable(static::getTableName());
2521
2522 $counts = [];
2523 foreach ($links as $refSpec) {
2524 /** @var $refSpec CRM_Core_Reference_Interface */
2525 $count = $refSpec->getReferenceCount($this);
2526 if ($count['count'] != 0) {
2527 $counts[] = $count;
2528 }
2529 }
2530
2531 foreach (CRM_Core_Component::getEnabledComponents() as $component) {
2532 /** @var $component CRM_Core_Component_Info */
2533 $counts = array_merge($counts, $component->getReferenceCounts($this));
2534 }
2535 CRM_Utils_Hook::referenceCounts($this, $counts);
2536
2537 return $counts;
2538 }
2539
2540 /**
2541 * List all tables which have hard foreign keys to this table.
2542 *
2543 * For now, this returns a description of every entity_id/entity_table
2544 * reference.
2545 * TODO: filter dynamic entity references on the $tableName, based on
2546 * schema metadata in dynamicForeignKey which enumerates a restricted
2547 * set of possible entity_table's.
2548 *
2549 * @param string $tableName
2550 * Table referred to.
2551 *
2552 * @return array
2553 * structure of table and column, listing every table with a
2554 * foreign key reference to $tableName, and the column where the key appears.
2555 */
2556 public static function getReferencesToTable($tableName) {
2557 $refsFound = [];
2558 foreach (CRM_Core_DAO_AllCoreTables::getClasses() as $daoClassName) {
2559 $links = $daoClassName::getReferenceColumns();
2560
2561 foreach ($links as $refSpec) {
2562 /** @var $refSpec CRM_Core_Reference_Interface */
2563 if ($refSpec->matchesTargetTable($tableName)) {
2564 $refsFound[] = $refSpec;
2565 }
2566 }
2567 }
2568 return $refsFound;
2569 }
2570
2571 /**
2572 * Get all references to contact table.
2573 *
2574 * This includes core tables, custom group tables, tables added by the merge
2575 * hook and the entity_tag table.
2576 *
2577 * Refer to CRM-17454 for information on the danger of querying the information
2578 * schema to derive this.
2579 *
2580 * @throws \CiviCRM_API3_Exception
2581 */
2582 public static function getReferencesToContactTable() {
2583 if (isset(\Civi::$statics[__CLASS__]) && isset(\Civi::$statics[__CLASS__]['contact_references'])) {
2584 return \Civi::$statics[__CLASS__]['contact_references'];
2585 }
2586 $contactReferences = [];
2587 $coreReferences = CRM_Core_DAO::getReferencesToTable('civicrm_contact');
2588 foreach ($coreReferences as $coreReference) {
2589 if (!is_a($coreReference, 'CRM_Core_Reference_Dynamic')) {
2590 $contactReferences[$coreReference->getReferenceTable()][] = $coreReference->getReferenceKey();
2591 }
2592 }
2593 self::appendCustomTablesExtendingContacts($contactReferences);
2594 self::appendCustomContactReferenceFields($contactReferences);
2595 \Civi::$statics[__CLASS__]['contact_references'] = $contactReferences;
2596 return \Civi::$statics[__CLASS__]['contact_references'];
2597 }
2598
2599 /**
2600 * Get all dynamic references to the given table.
2601 *
2602 * @param string $tableName
2603 *
2604 * @return array
2605 */
2606 public static function getDynamicReferencesToTable($tableName) {
2607 if (!isset(\Civi::$statics[__CLASS__]['contact_references_dynamic'][$tableName])) {
2608 \Civi::$statics[__CLASS__]['contact_references_dynamic'][$tableName] = [];
2609 $coreReferences = CRM_Core_DAO::getReferencesToTable($tableName);
2610 foreach ($coreReferences as $coreReference) {
2611 if ($coreReference instanceof \CRM_Core_Reference_Dynamic) {
2612 \Civi::$statics[__CLASS__]['contact_references_dynamic'][$tableName][$coreReference->getReferenceTable()][] = [$coreReference->getReferenceKey(), $coreReference->getTypeColumn()];
2613 }
2614 }
2615 }
2616 return \Civi::$statics[__CLASS__]['contact_references_dynamic'][$tableName];
2617 }
2618
2619 /**
2620 * Add custom tables that extend contacts to the list of contact references.
2621 *
2622 * CRM_Core_BAO_CustomGroup::getAllCustomGroupsByBaseEntity seems like a safe-ish
2623 * function to be sure all are retrieved & we don't miss subtypes or inactive or multiples
2624 * - the down side is it is not cached.
2625 *
2626 * Further changes should be include tests in the CRM_Core_MergerTest class
2627 * to ensure that disabled, subtype, multiple etc groups are still captured.
2628 *
2629 * @param array $cidRefs
2630 */
2631 public static function appendCustomTablesExtendingContacts(&$cidRefs) {
2632 $customValueTables = CRM_Core_BAO_CustomGroup::getAllCustomGroupsByBaseEntity('Contact');
2633 $customValueTables->find();
2634 while ($customValueTables->fetch()) {
2635 $cidRefs[$customValueTables->table_name][] = 'entity_id';
2636 }
2637 }
2638
2639 /**
2640 * Add custom ContactReference fields to the list of contact references
2641 *
2642 * This includes active and inactive fields/groups
2643 *
2644 * @param array $cidRefs
2645 *
2646 * @throws \CiviCRM_API3_Exception
2647 */
2648 public static function appendCustomContactReferenceFields(&$cidRefs) {
2649 $fields = civicrm_api3('CustomField', 'get', [
2650 'return' => ['column_name', 'custom_group_id.table_name'],
2651 'data_type' => 'ContactReference',
2652 'options' => ['limit' => 0],
2653 ])['values'];
2654 foreach ($fields as $field) {
2655 $cidRefs[$field['custom_group_id.table_name']][] = $field['column_name'];
2656 }
2657 }
2658
2659 /**
2660 * Lookup the value of a MySQL global configuration variable.
2661 *
2662 * @param string $name
2663 * E.g. "thread_stack".
2664 * @param mixed $default
2665 * @return mixed
2666 */
2667 public static function getGlobalSetting($name, $default = NULL) {
2668 // Alternatively, SELECT @@GLOBAL.thread_stack, but
2669 // that has been reported to fail under MySQL 5.0 for OS X
2670 $escapedName = self::escapeString($name);
2671 $dao = CRM_Core_DAO::executeQuery("SHOW VARIABLES LIKE '$escapedName'");
2672 if ($dao->fetch()) {
2673 return $dao->Value;
2674 }
2675 else {
2676 return $default;
2677 }
2678 }
2679
2680 /**
2681 * Update the fields array to also hold keys for pseudoconstant fields that relate to contained fields.
2682 *
2683 * This is relevant where we want to offer both the ID field and the label field
2684 * as an option, e.g. search builder.
2685 *
2686 * It is currently limited for optionGroupName & id+ name+ FK combos for purposes keeping the scope of the
2687 * change small, but is appropriate for other sorts of pseudoconstants.
2688 *
2689 * @param array $fields
2690 */
2691 public static function appendPseudoConstantsToFields(&$fields) {
2692 foreach ($fields as $fieldUniqueName => $field) {
2693 if (!empty($field['pseudoconstant'])) {
2694 $pseudoConstant = $field['pseudoconstant'];
2695 if (!empty($pseudoConstant['optionGroupName'])) {
2696 $fields[$pseudoConstant['optionGroupName']] = [
2697 'title' => CRM_Core_BAO_OptionGroup::getTitleByName($pseudoConstant['optionGroupName']),
2698 'name' => $pseudoConstant['optionGroupName'],
2699 'data_type' => CRM_Utils_Type::T_STRING,
2700 'is_pseudofield_for' => $fieldUniqueName,
2701 ];
2702 }
2703 // We restrict to id + name + FK as we are extending this a bit, but cautiously.
2704 elseif (
2705 !empty($field['FKClassName'])
2706 && CRM_Utils_Array::value('keyColumn', $pseudoConstant) === 'id'
2707 && CRM_Utils_Array::value('labelColumn', $pseudoConstant) === 'name'
2708 ) {
2709 $pseudoFieldName = str_replace('_' . $pseudoConstant['keyColumn'], '', $field['name']);
2710 // This if is just an extra caution when adding change.
2711 if (!isset($fields[$pseudoFieldName])) {
2712 $daoName = $field['FKClassName'];
2713 $fkFields = $daoName::fields();
2714 foreach ($fkFields as $fkField) {
2715 if ($fkField['name'] === $pseudoConstant['labelColumn']) {
2716 $fields[$pseudoFieldName] = [
2717 'name' => $pseudoFieldName,
2718 'is_pseudofield_for' => $field['name'],
2719 'title' => $fkField['title'],
2720 'data_type' => $fkField['type'],
2721 'where' => $field['where'],
2722 ];
2723 }
2724 }
2725 }
2726 }
2727 }
2728 }
2729 }
2730
2731 /**
2732 * Get options for the called BAO object's field.
2733 *
2734 * This function can be overridden by each BAO to add more logic related to context.
2735 * The overriding function will generally call the lower-level CRM_Core_PseudoConstant::get
2736 *
2737 * @param string $fieldName
2738 * @param string $context
2739 * @see CRM_Core_DAO::buildOptionsContext
2740 * @param array $props
2741 * Raw field values; whatever is known about this bao object.
2742 *
2743 * Note: $props can contain unsanitized input and should not be passed directly to CRM_Core_PseudoConstant::get
2744 *
2745 * @return array|bool
2746 */
2747 public static function buildOptions($fieldName, $context = NULL, $props = []) {
2748 // If a given bao does not override this function
2749 $baoName = get_called_class();
2750 return CRM_Core_PseudoConstant::get($baoName, $fieldName, [], $context);
2751 }
2752
2753 /**
2754 * Populate option labels for this object's fields.
2755 *
2756 * @throws exception if called directly on the base class
2757 */
2758 public function getOptionLabels() {
2759 $fields = $this->fields();
2760 if ($fields === NULL) {
2761 throw new Exception('Cannot call getOptionLabels on CRM_Core_DAO');
2762 }
2763 foreach ($fields as $field) {
2764 $name = $field['name'] ?? NULL;
2765 if ($name && isset($this->$name)) {
2766 $label = CRM_Core_PseudoConstant::getLabel(get_class($this), $name, $this->$name);
2767 if ($label !== FALSE) {
2768 // Append 'label' onto the field name
2769 $labelName = $name . '_label';
2770 $this->$labelName = $label;
2771 }
2772 }
2773 }
2774 }
2775
2776 /**
2777 * Provides documentation and validation for the buildOptions $context param
2778 *
2779 * @param string $context
2780 *
2781 * @throws CRM_Core_Exception
2782 * @return array
2783 */
2784 public static function buildOptionsContext($context = NULL) {
2785 $contexts = [
2786 'get' => "get: all options are returned, even if they are disabled; labels are translated.",
2787 'create' => "create: options are filtered appropriately for the object being created/updated; labels are translated.",
2788 'search' => "search: searchable options are returned; labels are translated.",
2789 'validate' => "validate: all options are returned, even if they are disabled; machine names are used in place of labels.",
2790 'abbreviate' => "abbreviate: enabled options are returned; labels are replaced with abbreviations.",
2791 'match' => "match: enabled options are returned using machine names as keys; labels are translated.",
2792 ];
2793 // Validation: enforce uniformity of this param
2794 if ($context !== NULL && !isset($contexts[$context])) {
2795 throw new CRM_Core_Exception("'$context' is not a valid context for buildOptions.");
2796 }
2797 return $contexts;
2798 }
2799
2800 /**
2801 * @param string $fieldName
2802 * @return bool|array
2803 */
2804 public function getFieldSpec($fieldName) {
2805 $fields = $this->fields();
2806
2807 // Support "unique names" as well as sql names
2808 $fieldKey = $fieldName;
2809 if (empty($fields[$fieldKey])) {
2810 $fieldKeys = $this->fieldKeys();
2811 $fieldKey = $fieldKeys[$fieldName] ?? NULL;
2812 }
2813 // If neither worked then this field doesn't exist. Return false.
2814 if (empty($fields[$fieldKey])) {
2815 return FALSE;
2816 }
2817 return $fields[$fieldKey];
2818 }
2819
2820 /**
2821 * Get SQL where clause for SQL filter syntax input parameters.
2822 *
2823 * SQL version of api function to assign filters to the DAO based on the syntax
2824 * $field => array('IN' => array(4,6,9))
2825 * OR
2826 * $field => array('LIKE' => array('%me%))
2827 * etc
2828 *
2829 * @param string $fieldName
2830 * Name of fields.
2831 * @param array $filter
2832 * filter to be applied indexed by operator.
2833 * @param string $type
2834 * type of field (not actually used - nor in api @todo ).
2835 * @param string $alias
2836 * alternative field name ('as') @todo- not actually used.
2837 * @param bool $returnSanitisedArray
2838 * Return a sanitised array instead of a clause.
2839 * this is primarily so we can add filters @ the api level to the Query object based fields
2840 *
2841 * @throws Exception
2842 *
2843 * @return NULL|string|array
2844 * a string is returned if $returnSanitisedArray is not set, otherwise and Array or NULL
2845 * depending on whether it is supported as yet
2846 */
2847 public static function createSQLFilter($fieldName, $filter, $type = NULL, $alias = NULL, $returnSanitisedArray = FALSE) {
2848 foreach ($filter as $operator => $criteria) {
2849 if (!CRM_Core_BAO_SchemaHandler::databaseSupportsUTF8MB4()) {
2850 foreach ((array) $criteria as $criterion) {
2851 if (!empty($criterion) && !is_numeric($criterion)
2852 // The first 2 criteria are redundant but are added as they
2853 // seem like they would
2854 // be quicker than this 3rd check.
2855 && max(array_map('ord', str_split($criterion))) >= 240) {
2856 // String contains unsupported emojis.
2857 // We return a clause that resolves to false as an emoji string by definition cannot be saved.
2858 // note that if we return just 0 for false if gets lost in empty checks.
2859 // https://stackoverflow.com/questions/16496554/can-php-detect-4-byte-encoded-utf8-chars
2860 return '0 = 1';
2861 }
2862 }
2863 }
2864
2865 if (in_array($operator, self::acceptedSQLOperators(), TRUE)) {
2866 switch ($operator) {
2867 // unary operators
2868 case 'IS NULL':
2869 case 'IS NOT NULL':
2870 if (!$returnSanitisedArray) {
2871 return (sprintf('%s %s', $fieldName, $operator));
2872 }
2873 else {
2874 return (sprintf('%s %s ', $fieldName, $operator));
2875 }
2876 break;
2877
2878 // ternary operators
2879 case 'BETWEEN':
2880 case 'NOT BETWEEN':
2881 if ((empty($criteria[0]) && !in_array($criteria[0], ['0', 0]))|| (empty($criteria[1]) && !in_array($criteria[1], ['0', 0]))) {
2882 throw new Exception("invalid criteria for $operator");
2883 }
2884 if (!$returnSanitisedArray) {
2885 return (sprintf('%s ' . $operator . ' "%s" AND "%s"', $fieldName, CRM_Core_DAO::escapeString($criteria[0]), CRM_Core_DAO::escapeString($criteria[1])));
2886 }
2887 else {
2888 // not yet implemented (tests required to implement)
2889 return NULL;
2890 }
2891 break;
2892
2893 // n-ary operators
2894 case 'IN':
2895 case 'NOT IN':
2896 if (empty($criteria)) {
2897 throw new Exception("invalid criteria for $operator");
2898 }
2899 $escapedCriteria = array_map([
2900 'CRM_Core_DAO',
2901 'escapeString',
2902 ], $criteria);
2903 if (!$returnSanitisedArray) {
2904 return (sprintf('%s %s ("%s")', $fieldName, $operator, implode('", "', $escapedCriteria)));
2905 }
2906 return $escapedCriteria;
2907
2908 // binary operators
2909
2910 default:
2911 if (!$returnSanitisedArray) {
2912 return (sprintf('%s %s "%s"', $fieldName, $operator, CRM_Core_DAO::escapeString($criteria)));
2913 }
2914 else {
2915 // not yet implemented (tests required to implement)
2916 return NULL;
2917 }
2918 }
2919 }
2920 }
2921 }
2922
2923 /**
2924 * @see http://issues.civicrm.org/jira/browse/CRM-9150
2925 * support for other syntaxes is discussed in ticket but being put off for now
2926 * @return string[]
2927 */
2928 public static function acceptedSQLOperators() {
2929 return [
2930 '=',
2931 '<=',
2932 '>=',
2933 '>',
2934 '<',
2935 'LIKE',
2936 "<>",
2937 "!=",
2938 "NOT LIKE",
2939 'IN',
2940 'NOT IN',
2941 'BETWEEN',
2942 'NOT BETWEEN',
2943 'IS NOT NULL',
2944 'IS NULL',
2945 ];
2946 }
2947
2948 /**
2949 * SQL has a limit of 64 characters on various names:
2950 * table name, trigger name, column name ...
2951 *
2952 * For custom groups and fields we generated names from user entered input
2953 * which can be longer than this length, this function helps with creating
2954 * strings that meet various criteria.
2955 *
2956 * @param string $string
2957 * The string to be shortened.
2958 * @param int $length
2959 * The max length of the string.
2960 *
2961 * @param bool $makeRandom
2962 *
2963 * @return string
2964 */
2965 public static function shortenSQLName($string, $length = 60, $makeRandom = FALSE) {
2966 // early return for strings that meet the requirements
2967 if (strlen($string) <= $length) {
2968 return $string;
2969 }
2970
2971 // easy return for calls that dont need a randomized uniq string
2972 if (!$makeRandom) {
2973 return substr($string, 0, $length);
2974 }
2975
2976 // the string is longer than the length and we need a uniq string
2977 // for the same tablename we need the same uniq string every time
2978 // hence we use md5 on the string, which is not random
2979 // we'll append 8 characters to the end of the tableName
2980 $md5string = substr(md5($string), 0, 8);
2981 return substr($string, 0, $length - 8) . "_{$md5string}";
2982 }
2983
2984 /**
2985 * https://issues.civicrm.org/jira/browse/CRM-17748
2986 * Sets the internal options to be used on a query
2987 *
2988 * @param array $options
2989 *
2990 */
2991 public function setOptions($options) {
2992 if (is_array($options)) {
2993 $this->_options = $options;
2994 }
2995 }
2996
2997 /**
2998 * https://issues.civicrm.org/jira/browse/CRM-17748
2999 * wrapper to pass internal DAO options down to DB_mysql/DB_Common level
3000 *
3001 * @param array $options
3002 *
3003 */
3004 protected function _setDBOptions($options) {
3005 global $_DB_DATAOBJECT;
3006
3007 if (is_array($options) && count($options)) {
3008 $conn = &$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5];
3009 foreach ($options as $option_name => $option_value) {
3010 $conn->setOption($option_name, $option_value);
3011 }
3012 }
3013 }
3014
3015 /**
3016 * @deprecated
3017 * @param array $params
3018 */
3019 public function setApiFilter(&$params) {
3020 }
3021
3022 /**
3023 * Generates acl clauses suitable for adding to WHERE or ON when doing an api.get for this entity
3024 *
3025 * Return format is in the form of fieldname => clauses starting with an operator. e.g.:
3026 * ```
3027 * array(
3028 * 'location_type_id' => array('IS NOT NULL', 'IN (1,2,3)')
3029 * )
3030 * ```
3031 *
3032 * Note that all array keys must be actual field names in this entity. Use subqueries to filter on other tables e.g. custom values.
3033 *
3034 * @return array
3035 */
3036 public function addSelectWhereClause() {
3037 $clauses = [];
3038 $fields = $this->fields();
3039 foreach ($fields as $fieldName => $field) {
3040 // Clause for contact-related entities like Email, Relationship, etc.
3041 if (strpos($fieldName, 'contact_id') === 0 && CRM_Utils_Array::value('FKClassName', $field) == 'CRM_Contact_DAO_Contact') {
3042 $clauses[$fieldName] = CRM_Utils_SQL::mergeSubquery('Contact');
3043 }
3044 // Clause for an entity_table/entity_id combo
3045 if ($fieldName == 'entity_id' && isset($fields['entity_table'])) {
3046 $relatedClauses = [];
3047 $relatedEntities = $this->buildOptions('entity_table', 'get');
3048 foreach ((array) $relatedEntities as $table => $ent) {
3049 if (!empty($ent)) {
3050 $ent = CRM_Core_DAO_AllCoreTables::getBriefName(CRM_Core_DAO_AllCoreTables::getClassForTable($table));
3051 $subquery = CRM_Utils_SQL::mergeSubquery($ent);
3052 if ($subquery) {
3053 $relatedClauses[] = "(entity_table = '$table' AND entity_id " . implode(' AND entity_id ', $subquery) . ")";
3054 }
3055 else {
3056 $relatedClauses[] = "(entity_table = '$table')";
3057 }
3058 }
3059 }
3060 if ($relatedClauses) {
3061 $clauses['id'] = 'IN (SELECT id FROM `' . $this->tableName() . '` WHERE (' . implode(') OR (', $relatedClauses) . '))';
3062 }
3063 }
3064 }
3065 CRM_Utils_Hook::selectWhereClause($this, $clauses);
3066 return $clauses;
3067 }
3068
3069 /**
3070 * This returns the final permissioned query string for this entity
3071 *
3072 * With acls from related entities + additional clauses from hook_civicrm_selectWhereClause
3073 *
3074 * @param string $tableAlias
3075 * @return array
3076 */
3077 public static function getSelectWhereClause($tableAlias = NULL) {
3078 $bao = new static();
3079 if ($tableAlias === NULL) {
3080 $tableAlias = $bao->tableName();
3081 }
3082 $clauses = [];
3083 foreach ((array) $bao->addSelectWhereClause() as $field => $vals) {
3084 $clauses[$field] = NULL;
3085 if ($vals) {
3086 $clauses[$field] = "(`$tableAlias`.`$field` IS NULL OR (`$tableAlias`.`$field` " . implode(" AND `$tableAlias`.`$field` ", (array) $vals) . '))';
3087 }
3088 }
3089 return $clauses;
3090 }
3091
3092 /**
3093 * ensure database name is 'safe', i.e. only contains word characters (includes underscores)
3094 * and dashes, and contains at least one [a-z] case insenstive.
3095 *
3096 * @param $database
3097 *
3098 * @return bool
3099 */
3100 public static function requireSafeDBName($database) {
3101 $matches = [];
3102 preg_match(
3103 "/^[\w\-]*[a-z]+[\w\-]*$/i",
3104 $database,
3105 $matches
3106 );
3107 if (empty($matches)) {
3108 return FALSE;
3109 }
3110 return TRUE;
3111 }
3112
3113 /**
3114 * Transform an array to a serialized string for database storage.
3115 *
3116 * @param array|null $value
3117 * @param int $serializationType
3118 * @return string|null
3119 *
3120 * @throws \Exception
3121 */
3122 public static function serializeField($value, $serializationType) {
3123 if ($value === NULL) {
3124 return NULL;
3125 }
3126 switch ($serializationType) {
3127 case self::SERIALIZE_SEPARATOR_BOOKEND:
3128 return $value === [] ? '' : CRM_Utils_Array::implodePadded($value);
3129
3130 case self::SERIALIZE_SEPARATOR_TRIMMED:
3131 return is_array($value) ? implode(self::VALUE_SEPARATOR, $value) : $value;
3132
3133 case self::SERIALIZE_JSON:
3134 return is_array($value) ? json_encode($value) : $value;
3135
3136 case self::SERIALIZE_PHP:
3137 return is_array($value) ? serialize($value) : $value;
3138
3139 case self::SERIALIZE_COMMA:
3140 return is_array($value) ? implode(',', $value) : $value;
3141
3142 default:
3143 throw new Exception('Unknown serialization method for field.');
3144 }
3145 }
3146
3147 /**
3148 * Transform a serialized string from the database into an array.
3149 *
3150 * @param string|null $value
3151 * @param $serializationType
3152 *
3153 * @return array|null
3154 * @throws CRM_Core_Exception
3155 */
3156 public static function unSerializeField($value, $serializationType) {
3157 if ($value === NULL) {
3158 return NULL;
3159 }
3160 if ($value === '') {
3161 return [];
3162 }
3163 switch ($serializationType) {
3164 case self::SERIALIZE_SEPARATOR_BOOKEND:
3165 return (array) CRM_Utils_Array::explodePadded($value);
3166
3167 case self::SERIALIZE_SEPARATOR_TRIMMED:
3168 return explode(self::VALUE_SEPARATOR, trim($value));
3169
3170 case self::SERIALIZE_JSON:
3171 return strlen($value) ? json_decode($value, TRUE) : [];
3172
3173 case self::SERIALIZE_PHP:
3174 return strlen($value) ? CRM_Utils_String::unserialize($value) : [];
3175
3176 case self::SERIALIZE_COMMA:
3177 return explode(',', trim(str_replace(', ', '', $value)));
3178
3179 default:
3180 throw new CRM_Core_Exception('Unknown serialization method for field.');
3181 }
3182 }
3183
3184 /**
3185 * @return array
3186 */
3187 public static function getEntityRefFilters() {
3188 return [];
3189 }
3190
3191 /**
3192 * Get exportable fields with pseudoconstants rendered as an extra field.
3193 *
3194 * @param string $baoClass
3195 *
3196 * @return array
3197 */
3198 public static function getExportableFieldsWithPseudoConstants($baoClass) {
3199 if (method_exists($baoClass, 'exportableFields')) {
3200 $fields = $baoClass::exportableFields();
3201 }
3202 else {
3203 $fields = $baoClass::export();
3204 }
3205 CRM_Core_DAO::appendPseudoConstantsToFields($fields);
3206 return $fields;
3207 }
3208
3209 /**
3210 * Remove item from static cache during update/delete operations
3211 */
3212 private function clearDbColumnValueCache() {
3213 $daoName = get_class($this);
3214 while (strpos($daoName, '_BAO_') !== FALSE) {
3215 $daoName = get_parent_class($daoName);
3216 }
3217 if (isset($this->id)) {
3218 unset(self::$_dbColumnValueCache[$daoName]['id'][$this->id]);
3219 }
3220 if (isset($this->name)) {
3221 unset(self::$_dbColumnValueCache[$daoName]['name'][$this->name]);
3222 }
3223 }
3224
3225 /**
3226 * Return a mapping from field-name to the corresponding key (as used in fields()).
3227 *
3228 * @return array
3229 * Array(string $name => string $uniqueName).
3230 */
3231 public static function fieldKeys() {
3232 return array_flip(CRM_Utils_Array::collect('name', static::fields()));
3233 }
3234
3235 /**
3236 * Returns system paths related to this entity (as defined in the xml schema)
3237 *
3238 * @return array
3239 */
3240 public static function getEntityPaths() {
3241 return static::$_paths ?? [];
3242 }
3243
3244 }