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