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