3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
13 * Base Database Access Object class.
15 * All DAO classes should inherit from this class.
18 * @copyright CiviCRM LLC https://civicrm.org/licensing
21 if (!defined('DB_DSN_MODE')) {
22 define('DB_DSN_MODE', 'auto');
25 require_once 'PEAR.php';
26 require_once 'DB/DataObject.php';
28 require_once 'CRM/Core/I18n.php';
33 class CRM_Core_DAO
extends DB_DataObject
{
36 * How many times has this instance been cloned.
40 protected $resultCopies = 0;
46 public static $_nullObject = NULL;
49 * Icon associated with this entity.
53 public static $_icon = NULL;
56 * Field to show when displaying a record.
60 public static $_labelField = NULL;
66 public static $_nullArray = [];
68 public static $_dbColumnValueCache = NULL;
69 const NOT_NULL
= 1, IS_NULL
= 2,
71 VALUE_SEPARATOR
= "\ 1",
72 BULK_INSERT_COUNT
= 200,
73 BULK_INSERT_HIGH_COUNT
= 200,
74 QUERY_FORMAT_WILDCARD
= 1,
75 QUERY_FORMAT_NO_QUOTES
= 2,
78 * Serialized string separated by and bookended with VALUE_SEPARATOR
80 SERIALIZE_SEPARATOR_BOOKEND
= 1,
82 * @deprecated format separated by VALUE_SEPARATOR
84 SERIALIZE_SEPARATOR_TRIMMED
= 2,
86 * Recommended serialization format
90 * @deprecated format using php serialize()
94 * Comma separated string, no quotes, no spaces
99 * Define entities that shouldn't be created or deleted when creating/ deleting
100 * test objects - this prevents world regions, countries etc from being added / deleted
103 public static $_testEntitiesToSkip = [];
105 * The factory class for this application.
108 public static $_factory = NULL;
110 public static $_checkedSqlFunctionsExist = FALSE;
113 * https://issues.civicrm.org/jira/browse/CRM-17748
114 * internal variable for DAO to hold per-query settings
117 protected $_options = [];
122 * @return \CRM_Core_DAO
124 public function __construct() {
126 $this->__table
= $this->getTableName();
130 * Returns localized title of this entity.
134 public static function getEntityTitle() {
135 $className = static::class;
136 CRM_Core_Error
::deprecatedWarning("$className needs to be regenerated. Missing getEntityTitle method.");
137 return CRM_Core_DAO_AllCoreTables
::getBriefName($className);
141 * Returns user-friendly description of this entity.
143 * @return string|null
145 public static function getEntityDescription() {
149 public function __clone() {
150 if (!empty($this->_DB_resultid
)) {
151 $this->resultCopies++
;
158 public function __destruct() {
159 if ($this->resultCopies
=== 0) {
162 $this->resultCopies
--;
166 * Returns the name of this table
170 public static function getTableName() {
171 return self
::getLocaleTableName(static::$_tableName ??
NULL);
175 * Returns if this table needs to be logged
179 public function getLog() {
180 return static::$_log ??
FALSE;
184 * Initialize the DAO object.
187 * The database connection string.
189 public static function init($dsn) {
190 Civi
::$statics[__CLASS__
]['init'] = 1;
191 $options = &PEAR
::getStaticProperty('DB_DataObject', 'options');
192 $dsn = CRM_Utils_SQL
::autoSwitchDSN($dsn);
193 $options['database'] = $dsn;
194 $options['quote_identifiers'] = TRUE;
195 if (CRM_Utils_SQL
::isSSLDSN($dsn)) {
196 // There are two different options arrays.
197 $other_options = &PEAR
::getStaticProperty('DB', 'options');
198 $other_options['ssl'] = TRUE;
200 if (defined('CIVICRM_DAO_DEBUG')) {
201 self
::DebugLevel(CIVICRM_DAO_DEBUG
);
203 $factory = new CRM_Contact_DAO_Factory();
204 CRM_Core_DAO
::setFactory($factory);
205 CRM_Core_DAO
::executeQuery('SET NAMES utf8mb4');
206 CRM_Core_DAO
::executeQuery('SET @uniqueID = %1', [1 => [CRM_Utils_Request
::id(), 'String']]);
212 public static function getConnection() {
213 global $_DB_DATAOBJECT;
214 $dao = new CRM_Core_DAO();
215 return $_DB_DATAOBJECT['CONNECTIONS'][$dao->_database_dsn_md5
];
219 * Disables usage of the ONLY_FULL_GROUP_BY Mode if necessary
221 public static function disableFullGroupByMode() {
222 $currentModes = CRM_Utils_SQL
::getSqlModes();
223 if (in_array('ONLY_FULL_GROUP_BY', $currentModes) && CRM_Utils_SQL
::isGroupByModeInDefault()) {
224 $key = array_search('ONLY_FULL_GROUP_BY', $currentModes);
225 unset($currentModes[$key]);
226 CRM_Core_DAO
::executeQuery("SET SESSION sql_mode = %1", [1 => [implode(',', $currentModes), 'String']]);
231 * Re-enables ONLY_FULL_GROUP_BY sql_mode as necessary..
233 public static function reenableFullGroupByMode() {
234 $currentModes = CRM_Utils_SQL
::getSqlModes();
235 if (!in_array('ONLY_FULL_GROUP_BY', $currentModes) && CRM_Utils_SQL
::isGroupByModeInDefault()) {
236 $currentModes[] = 'ONLY_FULL_GROUP_BY';
237 CRM_Core_DAO
::executeQuery("SET SESSION sql_mode = %1", [1 => [implode(',', $currentModes), 'String']]);
242 * @param string $fieldName
244 * @param array $params
246 protected function assignTestFK($fieldName, $fieldDef, $params) {
247 $required = $fieldDef['required'] ??
NULL;
248 $FKClassName = $fieldDef['FKClassName'] ??
NULL;
249 $dbName = $fieldDef['name'];
250 $daoName = str_replace('_BAO_', '_DAO_', get_class($this));
252 // skip the FK if it is not required
253 // if it's contact id we should create even if not required
254 // we'll have a go @ fetching first though
255 // we WILL create campaigns though for so tests with a campaign pseudoconstant will complete
256 if ($FKClassName === 'CRM_Campaign_DAO_Campaign' && $daoName != $FKClassName) {
259 if (!$required && $dbName != 'contact_id') {
260 $fkDAO = new $FKClassName();
261 if ($fkDAO->find(TRUE)) {
262 $this->$dbName = $fkDAO->id
;
266 elseif (in_array($FKClassName, CRM_Core_DAO
::$_testEntitiesToSkip)) {
267 $depObject = new $FKClassName();
268 $depObject->find(TRUE);
269 $this->$dbName = $depObject->id
;
271 elseif ($daoName == 'CRM_Member_DAO_MembershipType' && $fieldName == 'member_of_contact_id') {
272 // FIXME: the fields() metadata is not specific enough
273 $depObject = CRM_Core_DAO
::createTestObject($FKClassName, ['contact_type' => 'Organization']);
274 $this->$dbName = $depObject->id
;
277 //if it is required we need to generate the dependency object first
278 $depObject = CRM_Core_DAO
::createTestObject($FKClassName, CRM_Utils_Array
::value($dbName, $params, 1));
279 $this->$dbName = $depObject->id
;
284 * Generate and assign an arbitrary value to a field of a test object.
286 * @param string $fieldName
287 * @param array $fieldDef
288 * @param int $counter
289 * The globally-unique ID of the test object.
291 * @throws \CRM_Core_Exception
293 protected function assignTestValue($fieldName, &$fieldDef, $counter) {
294 $dbName = $fieldDef['name'];
295 $daoName = get_class($this);
298 if (!$handled && $dbName == 'contact_sub_type') {
299 //coming up with a rule to set this is too complex let's not set it
303 // Pick an option value if needed
304 if (!$handled && $fieldDef['type'] !== CRM_Utils_Type
::T_BOOLEAN
) {
305 $options = $daoName::buildOptions($dbName, 'create');
307 $this->$dbName = key($options);
313 switch ($fieldDef['type']) {
314 case CRM_Utils_Type
::T_INT
:
315 case CRM_Utils_Type
::T_FLOAT
:
316 case CRM_Utils_Type
::T_MONEY
:
317 if (isset($fieldDef['precision'])) {
318 // $object->$dbName = CRM_Utils_Number::createRandomDecimal($value['precision']);
319 $this->$dbName = CRM_Utils_Number
::createTruncatedDecimal($counter, $fieldDef['precision']);
322 $this->$dbName = $counter;
326 case CRM_Utils_Type
::T_BOOLEAN
:
327 if (isset($fieldDef['default'])) {
328 $this->$dbName = $fieldDef['default'];
330 elseif ($fieldDef['name'] == 'is_deleted' ||
$fieldDef['name'] == 'is_test') {
338 case CRM_Utils_Type
::T_DATE
:
339 case CRM_Utils_Type
::T_DATE + CRM_Utils_Type
::T_TIME
:
340 $this->$dbName = '19700101';
341 if ($dbName == 'end_date') {
342 // put this in the future
343 $this->$dbName = '20200101';
347 case CRM_Utils_Type
::T_TIMESTAMP
:
348 $this->$dbName = '19700201000000';
351 case CRM_Utils_Type
::T_TIME
:
352 throw new CRM_Core_Exception('T_TIME shouldn\'t be used.');
354 case CRM_Utils_Type
::T_CCNUM
:
355 $this->$dbName = '4111 1111 1111 1111';
358 case CRM_Utils_Type
::T_URL
:
359 $this->$dbName = 'http://www.civicrm.org';
362 case CRM_Utils_Type
::T_STRING
:
363 case CRM_Utils_Type
::T_BLOB
:
364 case CRM_Utils_Type
::T_MEDIUMBLOB
:
365 case CRM_Utils_Type
::T_TEXT
:
366 case CRM_Utils_Type
::T_LONGTEXT
:
367 case CRM_Utils_Type
::T_EMAIL
:
369 // WAS: if (isset($value['enumValues'])) {
370 // TODO: see if this works with all pseudoconstants
371 if (isset($fieldDef['pseudoconstant'], $fieldDef['pseudoconstant']['callback'])) {
372 if (isset($fieldDef['default'])) {
373 $this->$dbName = $fieldDef['default'];
376 $options = CRM_Core_PseudoConstant
::get($daoName, $fieldName);
377 if (is_array($options)) {
378 $this->$dbName = $options[0];
381 $defaultValues = explode(',', $options);
382 $this->$dbName = $defaultValues[0];
387 $this->$dbName = $dbName . '_' . $counter;
388 $maxlength = $fieldDef['maxlength'] ??
NULL;
389 if ($maxlength > 0 && strlen($this->$dbName) > $maxlength) {
390 $this->$dbName = substr($this->$dbName, 0, $fieldDef['maxlength']);
398 * Reset the DAO object.
400 * DAO is kinda crappy in that there is an unwritten rule of one query per DAO.
402 * We attempt to get around this crappy restriction by resetting some of DAO's internal fields. Use this with caution
404 public function reset() {
406 foreach (array_keys($this->table()) as $field) {
407 unset($this->$field);
411 * reset the various DB_DAO structures manually
420 * @param string $tableName
424 public static function getLocaleTableName($tableName) {
427 $tables = CRM_Core_I18n_Schema
::schemaStructureTables();
428 if (in_array($tableName, $tables)) {
429 return $tableName . $dbLocale;
436 * Execute a query by the current DAO, localizing it along the way (if needed).
438 * @param string $query
439 * The SQL query for execution.
440 * @param bool $i18nRewrite
441 * Whether to rewrite the query.
444 * the current DAO object after the query execution
446 public function query($query, $i18nRewrite = TRUE) {
447 // rewrite queries that should use $dbLocale-based views for multi-language installs
448 global $dbLocale, $_DB_DATAOBJECT;
450 if (empty($_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5
])) {
451 // Will force connection to be populated per CRM-20541.
455 $conn = &$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5
];
456 $orig_options = $conn->options
;
457 $this->_setDBOptions($this->_options
);
459 if ($i18nRewrite and $dbLocale) {
460 $query = CRM_Core_I18n_Schema
::rewriteQuery($query);
463 $ret = parent
::query($query);
465 $this->_setDBOptions($orig_options);
470 * Static function to set the factory instance for this class.
472 * @param object $factory
473 * The factory application object.
475 public static function setFactory(&$factory) {
476 self
::$_factory = &$factory;
480 * Factory method to instantiate a new object from a table name.
482 * @param string $table
483 * @return \DataObject|\PEAR_Error
485 public function factory($table = '') {
486 if (!isset(self
::$_factory)) {
487 return parent
::factory($table);
490 return self
::$_factory->create($table);
494 * Initialization for all DAO objects. Since we access DB_DO programatically
495 * we need to set the links manually.
497 public function initialize() {
499 if (empty(Civi
::$statics[__CLASS__
]['init'])) {
500 // CRM_Core_DAO::init() must be called before CRM_Core_DAO->initialize().
501 // This occurs very early in bootstrap - error handlers may not be wired up.
502 echo "Inconsistent system initialization sequence. Premature access of (" . get_class($this) . ")";
503 CRM_Utils_System
::civiExit();
508 * Defines the default key as 'id'.
512 public function keys() {
521 * Tells DB_DataObject which keys use autoincrement.
522 * 'id' is autoincrementing by default.
527 public function sequenceKey() {
528 static $sequenceKeys;
529 if (!isset($sequenceKeys)) {
530 $sequenceKeys = ['id', TRUE];
532 return $sequenceKeys;
536 * Returns list of FK relationships.
538 * @return CRM_Core_Reference_Basic[]
540 public static function getReferenceColumns() {
541 if (!isset(Civi
::$statics[static::class]['links'])) {
542 Civi
::$statics[static::class]['links'] = static::createReferenceColumns(static::class);
543 CRM_Core_DAO_AllCoreTables
::invoke(static::class, 'links_callback', Civi
::$statics[static::class]['links']);
545 return Civi
::$statics[static::class]['links'];
549 * Returns all the column names of this table.
554 public static function &fields() {
560 * Returns all usable fields, indexed by name.
562 * This function differs from fields() in that it indexes by name rather than unique_name.
564 * It excludes fields not added yet by pending upgrades.
565 * This avoids problems with trying to SELECT a field that exists in code but has not yet been added to the db.
567 * @param bool $checkPermissions
568 * Filter by field permissions.
571 public static function getSupportedFields($checkPermissions = FALSE) {
572 $fields = array_column((array) static::fields(), NULL, 'name');
574 // Exclude fields yet not added by pending upgrades
575 $dbVer = \CRM_Core_BAO_Domain
::version();
576 $daoExt = defined(static::class . '::EXT') ?
constant(static::class . '::EXT') : NULL;
577 if ($fields && $daoExt === 'civicrm' && version_compare($dbVer, \CRM_Utils_System
::version()) < 0) {
578 $fields = array_filter($fields, function($field) use ($dbVer) {
579 $add = $field['add'] ??
'1.0.0';
580 if (substr_count($add, '.') < 2) {
583 return version_compare($dbVer, $add, '>=');
587 // Exclude fields the user does not have permission for
588 if ($checkPermissions) {
589 $fields = array_filter($fields, function($field) {
590 return empty($field['permission']) || CRM_Core_Permission
::check($field['permission']);
598 * Get/set an associative array of table columns
603 public function table() {
604 $fields = $this->fields();
608 foreach ($fields as $name => $value) {
609 $table[$value['name']] = $value['type'];
610 if (!empty($value['required'])) {
611 $table[$value['name']] +
= self
::DB_DAO_NOTNULL
;
624 * @return CRM_Core_DAO
626 public function save($hook = TRUE) {
628 if (!empty($this->id
)) {
630 $preEvent = new \Civi\Core\DAO\Event\
PreUpdate($this);
631 $preEvent->eventID
= $eventID;
632 \Civi
::dispatcher()->dispatch("civi.dao.preUpdate", $preEvent);
635 $result = $this->update();
638 $event = new \Civi\Core\DAO\Event\
PostUpdate($this, $result);
639 $event->eventID
= $eventID;
640 \Civi
::dispatcher()->dispatch("civi.dao.postUpdate", $event);
642 $this->clearDbColumnValueCache();
646 $preEvent = new \Civi\Core\DAO\Event\
PreUpdate($this);
647 $preEvent->eventID
= $eventID;
648 \Civi
::dispatcher()->dispatch("civi.dao.preInsert", $preEvent);
651 $result = $this->insert();
654 $event = new \Civi\Core\DAO\Event\
PostUpdate($this, $result);
655 $event->eventID
= $eventID;
656 \Civi
::dispatcher()->dispatch("civi.dao.postInsert", $event);
662 CRM_Utils_Hook
::postSave($this);
669 * Deletes items from table which match current objects variables.
671 * Returns the true on success
675 * Designed to be extended
677 * $object = new mytable();
679 * echo $object->delete(); // builds a conditon
681 * $object = new mytable();
682 * $object->whereAdd('age > 12');
684 * $object->orderBy('age DESC');
685 * $object->delete(true); // dont use object vars, use the conditions, limit and order.
687 * @param bool $useWhere (optional) If DB_DATAOBJECT_WHEREADD_ONLY is passed in then
688 * we will build the condition only using the whereAdd's. Default is to
689 * build the condition only using the object parameters.
691 * * @return mixed Int (No. of rows affected) on success, false on failure, 0 on no data affected
693 public function delete($useWhere = FALSE) {
694 $preEvent = new \Civi\Core\DAO\Event\
PreDelete($this);
695 \Civi
::dispatcher()->dispatch("civi.dao.preDelete", $preEvent);
697 $result = parent
::delete($useWhere);
699 $event = new \Civi\Core\DAO\Event\
PostDelete($this, $result);
700 \Civi
::dispatcher()->dispatch("civi.dao.postDelete", $event);
703 $this->clearDbColumnValueCache();
709 * @param bool $created
711 public function log($created = FALSE) {
714 if (!$this->getLog()) {
719 $session = CRM_Core_Session
::singleton();
720 $cid = $session->get('userID');
723 // return is we dont have handle to FK
728 $dao = new CRM_Core_DAO_Log();
729 $dao->entity_table
= $this->getTableName();
730 $dao->entity_id
= $this->id
;
731 $dao->modified_id
= $cid;
732 $dao->modified_date
= date("YmdHis");
737 * Given an associative array of name/value pairs, extract all the values
738 * that belong to this object and initialize the object with said values
740 * @param array $params
741 * Array of name/value pairs to save.
744 * Did we copy all null values into the object
746 public function copyValues($params) {
748 foreach ($this->fields() as $uniqueName => $field) {
749 $dbName = $field['name'];
750 if (array_key_exists($dbName, $params)) {
751 $value = $params[$dbName];
754 elseif (array_key_exists($uniqueName, $params)) {
755 $value = $params[$uniqueName];
762 // if there is no value then make the variable NULL
765 $this->$dbName = 'null';
767 elseif (is_array($value) && !empty($field['serialize'])) {
768 $this->$dbName = CRM_Core_DAO
::serializeField($value, $field['serialize']);
772 $maxLength = $field['maxlength'] ??
NULL;
773 if (!is_array($value) && $maxLength && mb_strlen($value) > $maxLength && empty($field['pseudoconstant'])) {
774 // No ts() since this is a sysadmin-y string not seen by general users.
775 Civi
::log()->warning('A string for field {dbName} has been truncated. The original string was {value}.', ['dbName' => $dbName, 'value' => $value]);
776 // The string is too long - what to do what to do? Well losing data is generally bad so let's truncate
777 $value = CRM_Utils_String
::ellipsify($value, $maxLength);
779 $this->$dbName = $value;
788 * Store all the values from this object in an associative array
789 * this is a destructive store, calling function is responsible
790 * for keeping sanity of id's.
792 * @param object $object
793 * The object that we are extracting data from.
794 * @param array $values
795 * (reference ) associative array of name/value pairs.
797 public static function storeValues(&$object, &$values) {
798 $fields = $object->fields();
799 foreach ($fields as $name => $value) {
800 $dbName = $value['name'];
801 if (isset($object->$dbName) && $object->$dbName !== 'null') {
802 $values[$dbName] = $object->$dbName;
803 if ($name != $dbName) {
804 $values[$name] = $object->$dbName;
811 * Create an attribute for this specific field. We only do this for strings and text
813 * @param array $field
814 * The field under task.
817 * the attributes for the object
819 public static function makeAttribute($field) {
821 if (CRM_Utils_Array
::value('type', $field) == CRM_Utils_Type
::T_STRING
) {
822 $maxLength = $field['maxlength'] ??
NULL;
823 $size = $field['size'] ??
NULL;
824 if ($maxLength ||
$size) {
827 $attributes['maxlength'] = $maxLength;
830 $attributes['size'] = $size;
835 elseif (CRM_Utils_Array
::value('type', $field) == CRM_Utils_Type
::T_TEXT
) {
836 $rows = $field['rows'] ??
NULL;
840 $cols = $field['cols'] ??
NULL;
846 $attributes['rows'] = $rows;
847 $attributes['cols'] = $cols;
850 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
) {
851 $attributes['size'] = 6;
852 $attributes['maxlength'] = 14;
860 * Get the size and maxLength attributes for this text field.
861 * (or for all text fields) in the DAO object.
863 * @param string $class
865 * @param string $fieldName
866 * Field that i'm interested in or null if.
867 * you want the attributes for all DAO text fields
870 * assoc array of name => attribute pairs
872 public static function getAttribute($class, $fieldName = NULL) {
873 $object = new $class();
874 $fields = $object->fields();
875 if ($fieldName != NULL) {
876 $field = $fields[$fieldName] ??
NULL;
877 return self
::makeAttribute($field);
881 foreach ($fields as $name => $field) {
882 $attribute = self
::makeAttribute($field);
884 $attributes[$name] = $attribute;
888 if (!empty($attributes)) {
896 * Create or update a record from supplied params.
898 * If 'id' is supplied, an existing record will be updated
899 * Otherwise a new record will be created.
901 * @param array $record
904 * @throws \CRM_Core_Exception
906 public static function writeRecord(array $record): CRM_Core_DAO
{
907 $hook = empty($record['id']) ?
'create' : 'edit';
908 $className = CRM_Core_DAO_AllCoreTables
::getCanonicalClassName(static::class);
909 if ($className === 'CRM_Core_DAO') {
910 throw new CRM_Core_Exception('Function writeRecord must be called on a subclass of CRM_Core_DAO');
912 $entityName = CRM_Core_DAO_AllCoreTables
::getBriefName($className);
914 \CRM_Utils_Hook
::pre($hook, $entityName, $record['id'] ??
NULL, $record);
915 $instance = new $className();
916 $instance->copyValues($record);
918 \CRM_Utils_Hook
::post($hook, $entityName, $instance->id
, $instance);
924 * Bulk save multiple records
926 * @param array[] $records
928 * @throws CRM_Core_Exception
930 public static function writeRecords(array $records) {
932 foreach ($records as $record) {
933 $results[] = static::writeRecord($record);
939 * Delete a record from supplied params.
941 * @param array $record
944 * @throws CRM_Core_Exception
946 public static function deleteRecord(array $record) {
947 $className = CRM_Core_DAO_AllCoreTables
::getCanonicalClassName(static::class);
948 if ($className === 'CRM_Core_DAO') {
949 throw new CRM_Core_Exception('Function deleteRecord must be called on a subclass of CRM_Core_DAO');
951 $entityName = CRM_Core_DAO_AllCoreTables
::getBriefName($className);
952 if (empty($record['id'])) {
953 throw new CRM_Core_Exception("Cannot delete {$entityName} with no id.");
956 CRM_Utils_Hook
::pre('delete', $entityName, $record['id'], $record);
957 $instance = new $className();
958 $instance->id
= $record['id'];
959 if (!$instance->delete()) {
960 throw new CRM_Core_Exception("Could not delete {$entityName} id {$record['id']}");
962 CRM_Utils_Hook
::post('delete', $entityName, $record['id'], $instance);
968 * Bulk delete multiple records.
970 * @param array[] $records
972 * @throws CRM_Core_Exception
974 public static function deleteRecords(array $records) {
976 foreach ($records as $record) {
977 $results[] = static::deleteRecord($record);
983 * Check if there is a record with the same name in the db.
985 * @param string $value
986 * The value of the field we are checking.
987 * @param string $daoName
988 * The dao object name.
989 * @param string $daoID
990 * The id of the object being updated. u can change your name.
991 * as long as there is no conflict
992 * @param string $fieldName
993 * The name of the field in the DAO.
995 * @param string $domainID
996 * The id of the domain. Object exists only for the given domain.
999 * true if object exists
1001 public static function objectExists($value, $daoName, $daoID, $fieldName = 'name', $domainID = NULL) {
1002 $object = new $daoName();
1003 $object->$fieldName = $value;
1005 $object->domain_id
= $domainID;
1008 if ($object->find(TRUE)) {
1009 return $daoID && $object->id
== $daoID;
1017 * Check if there is a given column in a specific table.
1020 * @see CRM_Core_BAO_SchemaHandler::checkIfFieldExists
1022 * @param string $tableName
1023 * @param string $columnName
1024 * @param bool $i18nRewrite
1025 * Whether to rewrite the query on multilingual setups.
1028 * true if exists, else false
1030 public static function checkFieldExists($tableName, $columnName, $i18nRewrite = TRUE) {
1031 CRM_Core_Error
::deprecatedFunctionWarning('CRM_Core_BAO_SchemaHandler::checkIfFieldExists');
1032 return CRM_Core_BAO_SchemaHandler
::checkIfFieldExists($tableName, $columnName, $i18nRewrite);
1036 * Scans all the tables using a slow query and table name.
1040 public static function getTableNames() {
1041 $dao = CRM_Core_DAO
::executeQuery(
1043 FROM information_schema.TABLES
1044 WHERE TABLE_SCHEMA = '" . CRM_Core_DAO
::getDatabaseName() . "'
1045 AND TABLE_NAME LIKE 'civicrm_%'
1046 AND TABLE_NAME NOT LIKE 'civicrm_import_job_%'
1047 AND TABLE_NAME NOT LIKE '%_temp%'
1050 while ($dao->fetch()) {
1051 $values[] = $dao->TABLE_NAME
;
1057 * @param int $maxTablesToCheck
1061 public static function isDBMyISAM($maxTablesToCheck = 10) {
1062 return CRM_Core_DAO
::singleValueQuery(
1064 FROM information_schema.TABLES
1065 WHERE ENGINE = 'MyISAM'
1066 AND TABLE_SCHEMA = '" . CRM_Core_DAO
::getDatabaseName() . "'
1067 AND TABLE_NAME LIKE 'civicrm_%'
1068 AND TABLE_NAME NOT LIKE 'civicrm_import_job_%'
1069 AND TABLE_NAME NOT LIKE '%_temp%'
1070 AND TABLE_NAME NOT LIKE 'civicrm_tmp_%'
1075 * Get the name of the CiviCRM database.
1079 public static function getDatabaseName() {
1080 $daoObj = new CRM_Core_DAO();
1081 return $daoObj->database();
1085 * Checks if a constraint exists for a specified table.
1087 * @param string $tableName
1088 * @param string $constraint
1091 * true if constraint exists, false otherwise
1093 * @throws \CRM_Core_Exception
1095 public static function checkConstraintExists($tableName, $constraint) {
1098 if (!array_key_exists($tableName, $show)) {
1099 $query = "SHOW CREATE TABLE $tableName";
1100 $dao = CRM_Core_DAO
::executeQuery($query, [], TRUE, NULL, FALSE, FALSE);
1102 if (!$dao->fetch()) {
1103 throw new CRM_Core_Exception('query failed');
1106 $show[$tableName] = $dao->Create_Table
;
1109 return (bool) preg_match("/\b$constraint\b/i", $show[$tableName]);
1113 * Checks if CONSTRAINT keyword exists for a specified table.
1115 * @param array $tables
1117 * @throws CRM_Core_Exception
1120 * true if CONSTRAINT keyword exists, false otherwise
1122 public static function schemaRequiresRebuilding($tables = ["civicrm_contact"]) {
1124 foreach ($tables as $tableName) {
1125 if (!array_key_exists($tableName, $show)) {
1126 $query = "SHOW CREATE TABLE $tableName";
1127 $dao = CRM_Core_DAO
::executeQuery($query, [], TRUE, NULL, FALSE, FALSE);
1129 if (!$dao->fetch()) {
1130 throw new CRM_Core_Exception('Show create table failed.');
1133 $show[$tableName] = $dao->Create_Table
;
1136 $result = (bool) preg_match("/\bCONSTRAINT\b\s/i", $show[$tableName]);
1137 if ($result == TRUE) {
1148 * Checks if the FK constraint name is in the format 'FK_tableName_columnName'
1149 * for a specified column of a table.
1151 * @param string $tableName
1152 * @param string $columnName
1155 * true if in format, false otherwise
1157 * @throws \CRM_Core_Exception
1159 public static function checkFKConstraintInFormat($tableName, $columnName) {
1162 if (!array_key_exists($tableName, $show)) {
1163 $query = "SHOW CREATE TABLE $tableName";
1164 $dao = CRM_Core_DAO
::executeQuery($query);
1166 if (!$dao->fetch()) {
1167 throw new CRM_Core_Exception('query failed');
1170 $show[$tableName] = $dao->Create_Table
;
1172 $constraint = "`FK_{$tableName}_{$columnName}`";
1173 $pattern = "/\bCONSTRAINT\b\s+%s\s+\bFOREIGN\s+KEY\b\s/i";
1174 return (bool) preg_match(sprintf($pattern, $constraint), $show[$tableName]);
1178 * Check whether a specific column in a specific table has always the same value.
1180 * @param string $tableName
1181 * @param string $columnName
1182 * @param string $columnValue
1185 * true if the value is always $columnValue, false otherwise
1187 public static function checkFieldHasAlwaysValue($tableName, $columnName, $columnValue) {
1188 $query = "SELECT * FROM $tableName WHERE $columnName != '$columnValue'";
1189 $dao = CRM_Core_DAO
::executeQuery($query);
1190 $result = $dao->fetch() ?
FALSE : TRUE;
1195 * Check whether a specific column in a specific table is always NULL.
1197 * @param string $tableName
1198 * @param string $columnName
1201 * true if if the value is always NULL, false otherwise
1203 public static function checkFieldIsAlwaysNull($tableName, $columnName) {
1204 $query = "SELECT * FROM $tableName WHERE $columnName IS NOT NULL";
1205 $dao = CRM_Core_DAO
::executeQuery($query);
1206 $result = $dao->fetch() ?
FALSE : TRUE;
1211 * Checks if this DAO's table ought to exist.
1213 * If there are pending DB updates, this function compares the CiviCRM version of the table to the current schema version.
1216 * @throws CRM_Core_Exception
1218 public static function tableHasBeenAdded() {
1219 if (CRM_Utils_System
::version() === CRM_Core_BAO_Domain
::version()) {
1222 $daoExt = defined(static::class . '::EXT') ?
constant(static::class . '::EXT') : NULL;
1223 $daoVersion = defined(static::class . '::TABLE_ADDED') ?
constant(static::class . '::TABLE_ADDED') : '1.0';
1224 return !($daoExt === 'civicrm' && version_compare(CRM_Core_BAO_Domain
::version(), $daoVersion, '<'));
1228 * Check if there is a given table in the database.
1230 * @param string $tableName
1233 * true if exists, else false
1235 public static function checkTableExists($tableName) {
1240 $params = [1 => [$tableName, 'String']];
1242 $dao = CRM_Core_DAO
::executeQuery($query, $params);
1243 return (bool) $dao->fetch();
1247 * Check if a given table has data.
1249 * @param string $tableName
1251 * TRUE if $tableName has at least one record.
1253 public static function checkTableHasData($tableName) {
1254 $c = CRM_Core_DAO
::singleValueQuery(sprintf('SELECT count(*) c FROM `%s`', $tableName));
1263 public function checkVersion($version) {
1264 CRM_Core_Error
::deprecatedFunctionWarning('CRM_Core_BAO_Domain::version');
1265 $dbVersion = CRM_Core_BAO_Domain
::version();
1266 return trim($version) == trim($dbVersion);
1270 * Find a DAO object for the given ID and return it.
1273 * Id of the DAO object being searched for.
1275 * @return CRM_Core_DAO
1276 * Object of the type of the class that called this function.
1280 public static function findById($id) {
1281 $object = new static();
1283 if (!$object->find(TRUE)) {
1284 throw new Exception("Unable to find a " . get_called_class() . " with id {$id}.");
1290 * Returns all results as array-encoded records.
1294 public function fetchAll($k = FALSE, $v = FALSE, $method = FALSE) {
1296 while ($this->fetch()) {
1297 $result[] = $this->toArray();
1303 * Return the results as PHP generator.
1305 * @param string $type
1306 * Whether the generator yields 'dao' objects or 'array's.
1308 public function fetchGenerator($type = 'dao') {
1309 while ($this->fetch()) {
1316 yield
$this->toArray();
1320 throw new \
RuntimeException("Invalid record type ($type)");
1326 * Returns a singular value.
1328 * @return mixed|NULL
1330 public function fetchValue() {
1331 $result = $this->getDatabaseResult();
1332 $row = $result->fetchRow();
1342 * Get all the result records as mapping between columns.
1344 * @param string $keyColumn
1346 * @param string $valueColumn
1349 * Ex: ["foo" => "The Foo Bar", "baz" => "The Baz Qux"]
1351 public function fetchMap($keyColumn, $valueColumn) {
1353 while ($this->fetch()) {
1354 $result[$this->{$keyColumn}] = $this->{$valueColumn};
1360 * Given a DAO name, a column name and a column value, find the record and GET the value of another column in that record
1362 * @param string $daoName
1363 * Name of the DAO (Example: CRM_Contact_DAO_Contact to retrieve value from a contact).
1364 * @param int $searchValue
1365 * Value of the column you want to search by.
1366 * @param string $returnColumn
1367 * Name of the column you want to GET the value of.
1368 * @param string $searchColumn
1369 * Name of the column you want to search by.
1370 * @param bool $force
1371 * Skip use of the cache.
1373 * @return string|int|null
1374 * Value of $returnColumn in the retrieved record
1376 * @throws \CRM_Core_Exception
1378 public static function getFieldValue($daoName, $searchValue, $returnColumn = 'name', $searchColumn = 'id', $force = FALSE) {
1380 empty($searchValue) ||
1381 trim(strtolower($searchValue)) == 'null'
1383 // adding this here since developers forget to check for an id
1384 // or for the 'null' (which is a bad DAO kludge)
1385 // and hence we get the first value in the db
1386 throw new CRM_Core_Exception('getFieldValue failed');
1389 self
::$_dbColumnValueCache = self
::$_dbColumnValueCache ??
[];
1391 while (strpos($daoName, '_BAO_') !== FALSE) {
1392 $daoName = get_parent_class($daoName);
1396 empty(self
::$_dbColumnValueCache[$daoName][$searchColumn][$searchValue]) ||
1397 !array_key_exists($returnColumn, self
::$_dbColumnValueCache[$daoName][$searchColumn][$searchValue])
1399 $object = new $daoName();
1400 $object->$searchColumn = $searchValue;
1401 $object->selectAdd();
1402 $object->selectAdd($returnColumn);
1405 if ($object->find(TRUE)) {
1406 $result = $object->$returnColumn;
1409 self
::$_dbColumnValueCache[$daoName][$searchColumn][$searchValue][$returnColumn] = $result;
1411 return self
::$_dbColumnValueCache[$daoName][$searchColumn][$searchValue][$returnColumn];
1415 * Given a DAO name, a column name and a column value, find the record and SET the value of another column in that record
1417 * @param string $daoName
1418 * Name of the DAO (Example: CRM_Contact_DAO_Contact to retrieve value from a contact).
1419 * @param int $searchValue
1420 * Value of the column you want to search by.
1421 * @param string $setColumn
1422 * Name of the column you want to SET the value of.
1423 * @param string $setValue
1424 * SET the setColumn to this value.
1425 * @param string $searchColumn
1426 * Name of the column you want to search by.
1429 * true if we found and updated the object, else false
1431 public static function setFieldValue($daoName, $searchValue, $setColumn, $setValue, $searchColumn = 'id') {
1432 $object = new $daoName();
1433 $object->selectAdd();
1434 $object->selectAdd("$searchColumn, $setColumn");
1435 $object->$searchColumn = $searchValue;
1437 if ($object->find(TRUE)) {
1438 $object->$setColumn = $setValue;
1439 if ($object->save()) {
1450 * @param array|object $sort either array or CRM_Utils_Sort
1451 * @param string $default
1452 * Default sort value.
1456 public static function getSortString($sort, $default = NULL) {
1457 // check if sort is of type CRM_Utils_Sort
1458 if (is_a($sort, 'CRM_Utils_Sort')) {
1459 return $sort->orderBy();
1464 // is it an array specified as $field => $sortDirection ?
1466 foreach ($sort as $k => $v) {
1467 $sortString .= "$k $v,";
1469 return rtrim($sortString, ',');
1475 * Fetch object based on array of properties.
1477 * @param string $daoName
1478 * Name of the dao object.
1479 * @param array $params
1480 * (reference ) an assoc array of name/value pairs.
1481 * @param array $defaults
1482 * (reference ) an assoc array to hold the flattened values.
1483 * @param array $returnProperities
1484 * An assoc array of fields that need to be returned, eg array( 'first_name', 'last_name').
1487 * an object of type referenced by daoName
1489 public static function commonRetrieve($daoName, &$params, &$defaults, $returnProperities = NULL) {
1490 $object = new $daoName();
1491 $object->copyValues($params);
1493 // return only specific fields if returnproperties are sent
1494 if (!empty($returnProperities)) {
1495 $object->selectAdd();
1496 $object->selectAdd(implode(',', $returnProperities));
1499 if ($object->find(TRUE)) {
1500 self
::storeValues($object, $defaults);
1507 * Delete the object records that are associated with this contact.
1509 * @param string $daoName
1510 * Name of the dao object.
1511 * @param int $contactId
1512 * Id of the contact to delete.
1514 public static function deleteEntityContact($daoName, $contactId) {
1515 $object = new $daoName();
1517 $object->entity_table
= 'civicrm_contact';
1518 $object->entity_id
= $contactId;
1523 * Execute an unbuffered query.
1525 * This is a wrapper around new functionality exposed with CRM-17748.
1527 * @param string $query query to be executed
1529 * @param array $params
1530 * @param bool $abort
1531 * @param null $daoName
1532 * @param bool $freeDAO
1533 * @param bool $i18nRewrite
1534 * @param bool $trapException
1536 * @return CRM_Core_DAO
1537 * Object that points to an unbuffered result set
1539 public static function executeUnbufferedQuery(
1545 $i18nRewrite = TRUE,
1546 $trapException = FALSE
1549 return self
::executeQuery(
1557 ['result_buffering' => 0]
1564 * @param string $query
1565 * Query to be executed.
1567 * @param array $params
1568 * @param bool $abort
1569 * @param null $daoName
1570 * @param bool $freeDAO
1571 * @param bool $i18nRewrite
1572 * @param bool $trapException
1573 * @param array $options
1575 * @return CRM_Core_DAO|object
1576 * object that holds the results of the query
1577 * NB - if this is defined as just returning a DAO phpstorm keeps pointing
1578 * out all the properties that are not part of the DAO
1580 public static function &executeQuery(
1586 $i18nRewrite = TRUE,
1587 $trapException = FALSE,
1590 $queryStr = self
::composeQuery($query, $params, $abort);
1593 $dao = new CRM_Core_DAO();
1596 $dao = new $daoName();
1599 if ($trapException) {
1600 CRM_Core_Error
::deprecatedFunctionWarning('calling functions should handle exceptions');
1601 $errorScope = CRM_Core_TemporaryErrorScope
::ignoreException();
1604 if ($dao->isValidOption($options)) {
1605 $dao->setOptions($options);
1608 $result = $dao->query($queryStr, $i18nRewrite);
1610 // since it is unbuffered, ($dao->N==0) is true. This blocks the standard fetch() mechanism.
1611 if (CRM_Utils_Array
::value('result_buffering', $options) === 0) {
1615 if (is_a($result, 'DB_Error')) {
1616 CRM_Core_Error
::deprecatedFunctionWarning('calling functions should handle exceptions');
1624 * Wrapper to validate internal DAO options before passing to DB_mysql/DB_Common level
1626 * @param array $options
1629 * Provided options are valid
1631 public function isValidOption($options) {
1640 if (empty($options)) {
1644 foreach (array_keys($options) as $option) {
1645 if (!in_array($option, $validOptions)) {
1655 * Execute a query and get the single result.
1657 * @param string $query
1658 * Query to be executed.
1659 * @param array $params
1660 * @param bool $abort
1661 * @param bool $i18nRewrite
1662 * @return string|null
1663 * the result of the query if any
1666 public static function &singleValueQuery(
1672 $queryStr = self
::composeQuery($query, $params, $abort);
1674 static $_dao = NULL;
1677 $_dao = new CRM_Core_DAO();
1680 $_dao->query($queryStr, $i18nRewrite);
1682 $result = $_dao->getDatabaseResult();
1685 $row = $result->fetchRow();
1695 * Compose the query by merging the parameters into it.
1697 * @param string $query
1698 * @param array $params
1699 * @param bool $abort
1702 * @throws CRM_Core_Exception
1704 public static function composeQuery($query, $params = [], $abort = TRUE) {
1706 foreach ($params as $key => $item) {
1707 if (is_numeric($key)) {
1708 if (CRM_Utils_Type
::validate($item[0], $item[1]) !== NULL) {
1709 $item[0] = self
::escapeString($item[0]);
1710 if ($item[1] == 'String' ||
1711 $item[1] == 'Memo' ||
1714 // Support class constants stipulating wildcard characters and/or
1715 // non-quoting of strings. Also support legacy code which may be
1716 // passing in TRUE or 1 for $item[2], which used to indicate the
1717 // use of wildcard characters.
1718 if (!empty($item[2])) {
1719 if ($item[2] & CRM_Core_DAO
::QUERY_FORMAT_WILDCARD ||
$item[2] === TRUE) {
1720 $item[0] = "'%{$item[0]}%'";
1722 elseif (!($item[2] & CRM_Core_DAO
::QUERY_FORMAT_NO_QUOTES
)) {
1723 $item[0] = "'{$item[0]}'";
1727 $item[0] = "'{$item[0]}'";
1731 if (($item[1] == 'Date' ||
$item[1] == 'Timestamp') &&
1732 strlen($item[0]) == 0
1737 $tr['%' . $key] = $item[0];
1740 throw new CRM_Core_Exception("{$item[0]} is not of type {$item[1]}");
1745 return strtr($query, $tr);
1751 public static function freeResult($ids = NULL) {
1752 global $_DB_DATAOBJECT;
1755 if (!$_DB_DATAOBJECT ||
1756 !isset($_DB_DATAOBJECT['RESULTS'])
1760 $ids = array_keys($_DB_DATAOBJECT['RESULTS']);
1763 foreach ($ids as $id) {
1764 if (isset($_DB_DATAOBJECT['RESULTS'][$id])) {
1765 $_DB_DATAOBJECT['RESULTS'][$id]->free();
1766 unset($_DB_DATAOBJECT['RESULTS'][$id]);
1769 if (isset($_DB_DATAOBJECT['RESULTFIELDS'][$id])) {
1770 unset($_DB_DATAOBJECT['RESULTFIELDS'][$id]);
1776 * Make a shallow copy of an object and all the fields in the object.
1778 * @param string $daoName
1780 * @param array $criteria
1781 * Array of all the fields & values.
1782 * on which basis to copy
1783 * @param array $newData
1784 * Array of all the fields & values.
1785 * to be copied besides the other fields
1786 * @param string $fieldsFix
1787 * Array of fields that you want to prefix/suffix/replace.
1788 * @param string $blockCopyOfDependencies
1789 * Fields that you want to block from.
1791 * @param bool $blockCopyofCustomValues
1792 * Case when you don't want to copy the custom values set in a
1793 * template as it will override/ignore the submitted custom values
1795 * @return CRM_Core_DAO|bool
1796 * the newly created copy of the object. False if none created.
1798 public static function copyGeneric($daoName, $criteria, $newData = NULL, $fieldsFix = NULL, $blockCopyOfDependencies = NULL, $blockCopyofCustomValues = FALSE) {
1799 $object = new $daoName();
1802 $object->id
= $criteria['id'];
1805 foreach ($criteria as $key => $value) {
1806 $object->$key = $value;
1811 while ($object->fetch()) {
1813 // all the objects except with $blockCopyOfDependencies set
1814 // be copied - addresses #CRM-1962
1816 if ($blockCopyOfDependencies && $object->$blockCopyOfDependencies) {
1820 $newObject = new $daoName();
1822 $fields = $object->fields();
1823 $fieldsToPrefix = [];
1824 $fieldsToSuffix = [];
1825 $fieldsToReplace = [];
1826 if (!empty($fieldsFix['prefix'])) {
1827 $fieldsToPrefix = $fieldsFix['prefix'];
1829 if (!empty($fieldsFix['suffix'])) {
1830 $fieldsToSuffix = $fieldsFix['suffix'];
1832 if (!empty($fieldsFix['replace'])) {
1833 $fieldsToReplace = $fieldsFix['replace'];
1836 $localizableFields = FALSE;
1837 foreach ($fields as $name => $value) {
1838 if ($name == 'id' ||
$value['name'] == 'id') {
1839 // copy everything but the id!
1843 $dbName = $value['name'];
1844 $type = CRM_Utils_Type
::typeToString($value['type']);
1845 $newObject->$dbName = $object->$dbName;
1846 if (isset($fieldsToPrefix[$dbName])) {
1847 $newObject->$dbName = $fieldsToPrefix[$dbName] . $newObject->$dbName;
1849 if (isset($fieldsToSuffix[$dbName])) {
1850 $newObject->$dbName .= $fieldsToSuffix[$dbName];
1852 if (isset($fieldsToReplace[$dbName])) {
1853 $newObject->$dbName = $fieldsToReplace[$dbName];
1856 if ($type == 'Timestamp' ||
$type == 'Date') {
1857 $newObject->$dbName = CRM_Utils_Date
::isoToMysql($newObject->$dbName);
1860 if (!empty($value['localizable'])) {
1861 $localizableFields = TRUE;
1865 $newObject->copyValues($newData);
1870 // ensure we copy all localized fields as well
1871 if (CRM_Core_I18n
::isMultilingual() && $localizableFields) {
1873 $locales = CRM_Core_I18n
::getMultilingual();
1874 $curLocale = CRM_Core_I18n
::getLocale();
1875 // loop on other locales
1876 foreach ($locales as $locale) {
1877 if ($locale != $curLocale) {
1878 // setLocale doesn't seems to be reliable to set dbLocale and we only need to change the db locale
1879 $dbLocale = '_' . $locale;
1880 $newObject->copyLocalizable($object->id
, $newObject->id
, $fieldsToPrefix, $fieldsToSuffix, $fieldsToReplace);
1883 // restore dbLocale to starting value
1884 $dbLocale = '_' . $curLocale;
1887 if (!$blockCopyofCustomValues) {
1888 $newObject->copyCustomFields($object->id
, $newObject->id
);
1890 CRM_Utils_Hook
::post('create', CRM_Core_DAO_AllCoreTables
::getBriefName($daoName), $newObject->id
, $newObject);
1897 * Method that copies localizable fields from an old entity to a new one.
1899 * Fixes bug dev/core#2479,
1900 * where non current locale fields are copied from current locale losing translation when copying
1902 * @param int $entityID
1903 * @param int $newEntityID
1904 * @param array $fieldsToPrefix
1905 * @param array $fieldsToSuffix
1906 * @param array $fieldsToReplace
1908 protected function copyLocalizable($entityID, $newEntityID, $fieldsToPrefix, $fieldsToSuffix, $fieldsToReplace) {
1909 $entity = get_class($this);
1910 $object = new $entity();
1911 $object->id
= $entityID;
1914 $newObject = new $entity();
1915 $newObject->id
= $newEntityID;
1919 if ($object->fetch() && $newObject->fetch()) {
1921 $fields = $object->fields();
1922 foreach ($fields as $name => $value) {
1924 if ($name == 'id' ||
$value['name'] == 'id') {
1925 // copy everything but the id!
1929 // only copy localizable fields
1930 if (!$value['localizable']) {
1934 $dbName = $value['name'];
1935 $type = CRM_Utils_Type
::typeToString($value['type']);
1936 $newObject->$dbName = $object->$dbName;
1937 if (isset($fieldsToPrefix[$dbName])) {
1938 $newObject->$dbName = $fieldsToPrefix[$dbName] . $newObject->$dbName;
1940 if (isset($fieldsToSuffix[$dbName])) {
1941 $newObject->$dbName .= $fieldsToSuffix[$dbName];
1943 if (isset($fieldsToReplace[$dbName])) {
1944 $newObject->$dbName = $fieldsToReplace[$dbName];
1947 if ($type == 'Timestamp' ||
$type == 'Date') {
1948 $newObject->$dbName = CRM_Utils_Date
::isoToMysql($newObject->$dbName);
1958 * Method that copies custom fields values from an old entity to a new one.
1960 * Fixes bug CRM-19302,
1961 * where if a custom field of File type was present, left both events using the same file,
1962 * breaking download URL's for the old event.
1964 * @todo the goal here is to clean this up so that it works for any entity. Copy Generic already DOES some custom field stuff
1965 * but it seems to be bypassed & perhaps less good than this (or this just duplicates it...)
1967 * @param int $entityID
1968 * @param int $newEntityID
1970 public function copyCustomFields($entityID, $newEntityID) {
1971 $entity = CRM_Core_DAO_AllCoreTables
::getBriefName(get_class($this));
1972 $tableName = CRM_Core_DAO_AllCoreTables
::getTableForClass(get_class($this));
1973 // Obtain custom values for old event
1974 $customParams = $htmlType = [];
1975 $customValues = CRM_Core_BAO_CustomValueTable
::getEntityValues($entityID, $entity);
1977 // If custom values present, we copy them
1978 if (!empty($customValues)) {
1979 // Get Field ID's and identify File type attributes, to handle file copying.
1980 $fieldIds = implode(', ', array_keys($customValues));
1981 $sql = "SELECT id FROM civicrm_custom_field WHERE html_type = 'File' AND id IN ( {$fieldIds} )";
1982 $result = CRM_Core_DAO
::executeQuery($sql);
1984 // Build array of File type fields
1985 while ($result->fetch()) {
1986 $htmlType[] = $result->id
;
1989 // Build params array of custom values
1990 foreach ($customValues as $field => $value) {
1991 if ($value !== NULL) {
1992 // Handle File type attributes
1993 if (in_array($field, $htmlType)) {
1994 $fileValues = CRM_Core_BAO_File
::path($value, $entityID);
1995 $customParams["custom_{$field}_-1"] = [
1996 'name' => CRM_Utils_File
::duplicate($fileValues[0]),
1997 'type' => $fileValues[1],
2000 // Handle other types
2002 $customParams["custom_{$field}_-1"] = $value;
2007 // Save Custom Fields for new Event
2008 CRM_Core_BAO_CustomValueTable
::postProcess($customParams, $tableName, $newEntityID, $entity);
2011 // copy activity attachments ( if any )
2012 CRM_Core_BAO_File
::copyEntityFile($tableName, $entityID, $tableName, $newEntityID);
2016 * Cascade update through related entities.
2018 * @param string $daoName
2021 * @param array $newData
2023 * @return CRM_Core_DAO|null
2025 public static function cascadeUpdate($daoName, $fromId, $toId, $newData = []) {
2026 $object = new $daoName();
2027 $object->id
= $fromId;
2029 if ($object->find(TRUE)) {
2030 $newObject = new $daoName();
2031 $newObject->id
= $toId;
2033 if ($newObject->find(TRUE)) {
2034 $fields = $object->fields();
2035 foreach ($fields as $name => $value) {
2036 if ($name == 'id' ||
$value['name'] == 'id') {
2037 // copy everything but the id!
2041 $colName = $value['name'];
2042 $newObject->$colName = $object->$colName;
2044 if (substr($name, -5) == '_date' ||
2045 substr($name, -10) == '_date_time'
2047 $newObject->$colName = CRM_Utils_Date
::isoToMysql($newObject->$colName);
2050 foreach ($newData as $k => $v) {
2051 $newObject->$k = $v;
2061 * Given the component id, compute the contact id
2062 * since its used for things like send email
2064 * @param $componentIDs
2065 * @param string $tableName
2066 * @param string $idField
2070 public static function getContactIDsFromComponent($componentIDs, $tableName, $idField = 'id') {
2073 if (empty($componentIDs)) {
2077 $IDs = implode(',', $componentIDs);
2081 WHERE $idField IN ( $IDs )
2084 $dao = CRM_Core_DAO
::executeQuery($query);
2085 while ($dao->fetch()) {
2086 $contactIDs[] = $dao->contact_id
;
2092 * Fetch object based on array of properties.
2094 * @param string $daoName
2095 * Name of the dao object.
2096 * @param string $fieldIdName
2097 * @param int $fieldId
2099 * @param array $returnProperities
2100 * An assoc array of fields that need to be returned, eg array( 'first_name', 'last_name').
2103 * an object of type referenced by daoName
2105 public static function commonRetrieveAll($daoName, $fieldIdName, $fieldId, &$details, $returnProperities = NULL) {
2106 require_once str_replace('_', DIRECTORY_SEPARATOR
, $daoName) . ".php";
2107 $object = new $daoName();
2108 $object->$fieldIdName = $fieldId;
2110 // return only specific fields if returnproperties are sent
2111 if (!empty($returnProperities)) {
2112 $object->selectAdd();
2113 $object->selectAdd('id');
2114 $object->selectAdd(implode(',', $returnProperities));
2118 while ($object->fetch()) {
2120 self
::storeValues($object, $defaults);
2121 $details[$object->id
] = $defaults;
2128 * Drop all CiviCRM tables.
2130 * @throws \CRM_Core_Exception
2132 public static function dropAllTables() {
2134 // first drop all the custom tables we've created
2135 CRM_Core_BAO_CustomGroup
::dropAllTables();
2137 // drop all multilingual views
2138 CRM_Core_I18n_Schema
::dropAllViews();
2140 CRM_Utils_File
::sourceSQLFile(CIVICRM_DSN
,
2141 dirname(__FILE__
) . DIRECTORY_SEPARATOR
.
2142 '..' . DIRECTORY_SEPARATOR
.
2143 '..' . DIRECTORY_SEPARATOR
.
2144 'sql' . DIRECTORY_SEPARATOR
.
2145 'civicrm_drop.mysql'
2154 public static function escapeString($string) {
2155 static $_dao = NULL;
2157 // If this is an atypical case (e.g. preparing .sql file before CiviCRM
2158 // has been installed), then we fallback DB-less str_replace escaping, as
2159 // we can't use mysqli_real_escape_string, as there is no DB connection.
2160 // Note: In typical usage, escapeString() will only check one conditional
2161 // ("if !$_dao") rather than two conditionals ("if !defined(DSN)")
2162 if (!defined('CIVICRM_DSN')) {
2163 // See http://php.net/manual/en/mysqli.real-escape-string.php for the
2164 // list of characters mysqli_real_escape_string escapes.
2165 $search = ["\\", "\x00", "\n", "\r", "'", '"', "\x1a"];
2166 $replace = ["\\\\", "\\0", "\\n", "\\r", "\'", '\"', "\\Z"];
2167 return str_replace($search, $replace, $string);
2169 $_dao = new CRM_Core_DAO();
2171 return $_dao->escape($string);
2175 * Escape a list of strings for use with "WHERE X IN (...)" queries.
2177 * @param array $strings
2178 * @param string $default
2179 * the value to use if $strings has no elements.
2181 * eg "abc","def","ghi"
2183 public static function escapeStrings($strings, $default = NULL) {
2184 static $_dao = NULL;
2186 $_dao = new CRM_Core_DAO();
2189 if (empty($strings)) {
2193 $escapes = array_map([$_dao, 'escape'], $strings);
2194 return '"' . implode('","', $escapes) . '"';
2202 public static function escapeWildCardString($string) {
2204 // ensure we escape the single characters % and _ which are mysql wild
2205 // card characters and could come in via sortByCharacter
2206 // note that mysql does not escape these characters
2207 if ($string && in_array($string,
2208 ['%', '_', '%%', '_%']
2211 return '\\' . $string;
2214 return self
::escapeString($string);
2218 * Creates a test object, including any required objects it needs via recursion
2219 * createOnly: only create in database, do not store or return the objects (useful for perf testing)
2220 * ONLY USE FOR TESTING
2222 * @param string $daoName
2223 * @param array $params
2224 * @param int $numObjects
2225 * @param bool $createOnly
2227 * @return object|array|NULL
2228 * NULL if $createOnly. A single object if $numObjects==1. Otherwise, an array of multiple objects.
2230 public static function createTestObject(
2236 //this is a test function also backtrace is set for the test suite it sometimes unsets itself
2237 // so we re-set here in case
2238 $config = CRM_Core_Config
::singleton();
2239 $config->backtrace
= TRUE;
2241 static $counter = 0;
2242 CRM_Core_DAO
::$_testEntitiesToSkip = [
2243 'CRM_Core_DAO_Worldregion',
2244 'CRM_Core_DAO_StateProvince',
2245 'CRM_Core_DAO_Country',
2246 'CRM_Core_DAO_Domain',
2247 'CRM_Financial_DAO_FinancialType',
2248 //because valid ones exist & we use pick them due to pseudoconstant can't reliably create & delete these
2251 // Prefer to instantiate BAO's instead of DAO's (when possible)
2252 // so that assignTestValue()/assignTestFK() can be overloaded.
2253 $baoName = str_replace('_DAO_', '_BAO_', $daoName);
2254 if ($baoName === 'CRM_Financial_BAO_FinancialTrxn') {
2255 // OMG OMG OMG this is so incredibly bad. The BAO is insanely named.
2256 // @todo create a new class called what the BAO SHOULD be
2257 // that extends BAO-crazy-name.... migrate.
2258 $baoName = 'CRM_Core_BAO_FinancialTrxn';
2260 if (class_exists($baoName)) {
2261 $daoName = $baoName;
2264 for ($i = 0; $i < $numObjects; ++
$i) {
2267 /** @var CRM_Core_DAO $object */
2268 $object = new $daoName();
2270 $fields = $object->fields();
2271 foreach ($fields as $fieldName => $fieldDef) {
2272 $dbName = $fieldDef['name'];
2273 $FKClassName = $fieldDef['FKClassName'] ??
NULL;
2275 if (isset($params[$dbName]) && !is_array($params[$dbName])) {
2276 $object->$dbName = $params[$dbName];
2279 elseif ($dbName != 'id') {
2280 if ($FKClassName != NULL) {
2281 $object->assignTestFK($fieldName, $fieldDef, $params);
2285 $object->assignTestValue($fieldName, $fieldDef, $counter);
2293 $objects[$i] = $object;
2303 elseif ($numObjects == 1) {
2312 * Deletes the this object plus any dependent objects that are associated with it.
2313 * ONLY USE FOR TESTING
2315 * @param string $daoName
2316 * @param array $params
2318 public static function deleteTestObjects($daoName, $params = []) {
2319 //this is a test function also backtrace is set for the test suite it sometimes unsets itself
2320 // so we re-set here in case
2321 $config = CRM_Core_Config
::singleton();
2322 $config->backtrace
= TRUE;
2324 $object = new $daoName();
2325 $object->id
= $params['id'] ??
NULL;
2327 // array(array(0 => $daoName, 1 => $daoParams))
2329 if ($object->find(TRUE)) {
2331 $fields = $object->fields();
2332 foreach ($fields as $name => $value) {
2334 $dbName = $value['name'];
2336 $FKClassName = $value['FKClassName'] ??
NULL;
2337 $required = $value['required'] ??
NULL;
2338 if ($FKClassName != NULL
2340 && !in_array($FKClassName, CRM_Core_DAO
::$_testEntitiesToSkip)
2341 && ($required ||
$dbName == 'contact_id')
2342 //I'm a bit stuck on this one - we might need to change the singleValueAlter so that the entities don't share a contact
2343 // to make this test process pass - line below makes pass for now
2344 && $dbName != 'member_of_contact_id'
2347 $deletions[] = [$FKClassName, ['id' => $object->$dbName]];
2354 foreach ($deletions as $deletion) {
2355 CRM_Core_DAO
::deleteTestObjects($deletion[0], $deletion[1]);
2360 * Set defaults when creating new entity.
2361 * (don't call this set defaults as already in use with different signature in some places)
2363 * @param array $params
2366 public static function setCreateDefaults(&$params, $defaults) {
2367 if (!empty($params['id'])) {
2370 foreach ($defaults as $key => $value) {
2371 if (!array_key_exists($key, $params) ||
$params[$key] === NULL) {
2372 $params[$key] = $value;
2378 * @param string $prefix
2379 * @param bool $addRandomString
2380 * @param null $string
2384 * @see CRM_Utils_SQL_TempTable
2386 public static function createTempTableName($prefix = 'civicrm', $addRandomString = TRUE, $string = NULL) {
2387 CRM_Core_Error
::deprecatedFunctionWarning('Use CRM_Utils_SQL_TempTable interface to create temporary tables');
2388 $tableName = $prefix . "_temp";
2390 if ($addRandomString) {
2392 $tableName .= "_" . $string;
2395 $tableName .= "_" . md5(uniqid('', TRUE));
2403 * @param bool $trigger
2407 public static function checkTriggerViewPermission($view = TRUE, $trigger = TRUE) {
2408 if (\Civi
::settings()->get('logging_no_trigger_permission')) {
2411 // test for create view and trigger permissions and if allowed, add the option to go multilingual and logging
2412 $dao = new CRM_Core_DAO();
2415 $dao->query('CREATE OR REPLACE VIEW civicrm_domain_view AS SELECT * FROM civicrm_domain');
2416 $dao->query('DROP VIEW IF EXISTS civicrm_domain_view');
2420 $dao->query('CREATE TRIGGER civicrm_domain_trigger BEFORE INSERT ON civicrm_domain FOR EACH ROW BEGIN END');
2421 $dao->query('DROP TRIGGER IF EXISTS civicrm_domain_trigger');
2424 catch (Exception
$e) {
2432 * @param null $message
2433 * @param bool $printDAO
2435 public static function debugPrint($message = NULL, $printDAO = TRUE) {
2436 CRM_Utils_System
::xMemory("{$message}: ");
2439 global $_DB_DATAOBJECT;
2441 foreach (array_keys($_DB_DATAOBJECT['RESULTS']) as $id) {
2442 $q[] = $_DB_DATAOBJECT['RESULTS'][$id]->query
;
2444 CRM_Core_Error
::debug('_DB_DATAOBJECT', $q);
2449 * Build a list of triggers via hook and add them to (err, reconcile them
2450 * with) the database.
2452 * @param string $tableName
2453 * the specific table requiring a rebuild; or NULL to rebuild all tables.
2454 * @param bool $force
2459 public static function triggerRebuild($tableName = NULL, $force = FALSE) {
2460 Civi
::service('sql_triggers')->rebuild($tableName, $force);
2464 * Because sql functions are sometimes lost, esp during db migration, we check here to avoid numerous support requests
2465 * @see http://issues.civicrm.org/jira/browse/CRM-13822
2466 * TODO: Alternative solutions might be
2467 * * Stop using functions and find another way to strip numeric characters from phones
2468 * * Give better error messages (currently a missing fn fatals with "unknown error")
2470 public static function checkSqlFunctionsExist() {
2471 if (!self
::$_checkedSqlFunctionsExist) {
2472 self
::$_checkedSqlFunctionsExist = TRUE;
2473 $dao = CRM_Core_DAO
::executeQuery("SHOW function status WHERE db = database() AND name = 'civicrm_strip_non_numeric'");
2474 if (!$dao->fetch()) {
2475 self
::triggerRebuild();
2481 * Wrapper function to drop triggers.
2483 * @param string $tableName
2484 * the specific table requiring a rebuild; or NULL to rebuild all tables.
2487 public static function dropTriggers($tableName = NULL) {
2488 Civi
::service('sql_triggers')->dropTriggers($tableName);
2492 * @param array $info
2493 * per hook_civicrm_triggerInfo.
2494 * @param string $onlyTableName
2495 * the specific table requiring a rebuild; or NULL to rebuild all tables.
2498 public static function createTriggers(&$info, $onlyTableName = NULL) {
2499 Civi
::service('sql_triggers')->createTriggers($info, $onlyTableName);
2503 * Given a list of fields, create a list of references.
2505 * @param string $className
2506 * BAO/DAO class name.
2507 * @return array<CRM_Core_Reference_Interface>
2509 public static function createReferenceColumns($className) {
2511 $fields = $className::fields();
2512 foreach ($fields as $field) {
2513 if (isset($field['pseudoconstant'], $field['pseudoconstant']['optionGroupName'])) {
2514 $result[] = new CRM_Core_Reference_OptionValue(
2515 $className::getTableName(),
2517 'civicrm_option_value',
2518 CRM_Utils_Array
::value('keyColumn', $field['pseudoconstant'], 'value'),
2519 $field['pseudoconstant']['optionGroupName']
2527 * Find all records which refer to this entity.
2530 * Array of objects referencing this
2532 public function findReferences() {
2533 $links = self
::getReferencesToTable(static::getTableName());
2536 foreach ($links as $refSpec) {
2537 /** @var $refSpec CRM_Core_Reference_Interface */
2538 $daoName = CRM_Core_DAO_AllCoreTables
::getClassForTable($refSpec->getReferenceTable());
2539 $result = $refSpec->findReferences($this);
2541 while ($result->fetch()) {
2542 $obj = new $daoName();
2543 $obj->id
= $result->id
;
2544 $occurrences[] = $obj;
2549 return $occurrences;
2554 * each item has keys:
2558 * - table: string|null SQL table name
2559 * - key: string|null SQL column name
2561 public function getReferenceCounts() {
2562 $links = self
::getReferencesToTable(static::getTableName());
2565 foreach ($links as $refSpec) {
2566 /** @var $refSpec CRM_Core_Reference_Interface */
2567 $count = $refSpec->getReferenceCount($this);
2568 if ($count['count'] != 0) {
2573 foreach (CRM_Core_Component
::getEnabledComponents() as $component) {
2574 /** @var $component CRM_Core_Component_Info */
2575 $counts = array_merge($counts, $component->getReferenceCounts($this));
2577 CRM_Utils_Hook
::referenceCounts($this, $counts);
2583 * List all tables which have hard foreign keys to this table.
2585 * For now, this returns a description of every entity_id/entity_table
2587 * TODO: filter dynamic entity references on the $tableName, based on
2588 * schema metadata in dynamicForeignKey which enumerates a restricted
2589 * set of possible entity_table's.
2591 * @param string $tableName
2592 * Table referred to.
2594 * @return CRM_Core_Reference_Interface[]
2595 * structure of table and column, listing every table with a
2596 * foreign key reference to $tableName, and the column where the key appears.
2598 public static function getReferencesToTable($tableName) {
2600 foreach (CRM_Core_DAO_AllCoreTables
::getClasses() as $daoClassName) {
2601 $links = $daoClassName::getReferenceColumns();
2603 foreach ($links as $refSpec) {
2604 /** @var $refSpec CRM_Core_Reference_Interface */
2605 if ($refSpec->matchesTargetTable($tableName)) {
2606 $refsFound[] = $refSpec;
2614 * Get all references to contact table.
2616 * This includes core tables, custom group tables, tables added by the merge
2617 * hook and the entity_tag table.
2619 * Refer to CRM-17454 for information on the danger of querying the information
2620 * schema to derive this.
2622 * @throws \CiviCRM_API3_Exception
2624 public static function getReferencesToContactTable() {
2625 if (isset(\Civi
::$statics[__CLASS__
]) && isset(\Civi
::$statics[__CLASS__
]['contact_references'])) {
2626 return \Civi
::$statics[__CLASS__
]['contact_references'];
2628 $contactReferences = [];
2629 $coreReferences = CRM_Core_DAO
::getReferencesToTable('civicrm_contact');
2630 foreach ($coreReferences as $coreReference) {
2632 // Exclude option values
2633 !is_a($coreReference, 'CRM_Core_Reference_Dynamic') &&
2634 // Exclude references to other columns
2635 $coreReference->getTargetKey() === 'id'
2637 $contactReferences[$coreReference->getReferenceTable()][] = $coreReference->getReferenceKey();
2640 self
::appendCustomTablesExtendingContacts($contactReferences);
2641 self
::appendCustomContactReferenceFields($contactReferences);
2642 \Civi
::$statics[__CLASS__
]['contact_references'] = $contactReferences;
2643 return \Civi
::$statics[__CLASS__
]['contact_references'];
2647 * Get all dynamic references to the given table.
2649 * @param string $tableName
2653 public static function getDynamicReferencesToTable($tableName) {
2654 if (!isset(\Civi
::$statics[__CLASS__
]['contact_references_dynamic'][$tableName])) {
2655 \Civi
::$statics[__CLASS__
]['contact_references_dynamic'][$tableName] = [];
2656 $coreReferences = CRM_Core_DAO
::getReferencesToTable($tableName);
2657 foreach ($coreReferences as $coreReference) {
2658 if ($coreReference instanceof \CRM_Core_Reference_Dynamic
) {
2659 \Civi
::$statics[__CLASS__
]['contact_references_dynamic'][$tableName][$coreReference->getReferenceTable()][] = [$coreReference->getReferenceKey(), $coreReference->getTypeColumn()];
2663 return \Civi
::$statics[__CLASS__
]['contact_references_dynamic'][$tableName];
2667 * Add custom tables that extend contacts to the list of contact references.
2669 * CRM_Core_BAO_CustomGroup::getAllCustomGroupsByBaseEntity seems like a safe-ish
2670 * function to be sure all are retrieved & we don't miss subtypes or inactive or multiples
2671 * - the down side is it is not cached.
2673 * Further changes should be include tests in the CRM_Core_MergerTest class
2674 * to ensure that disabled, subtype, multiple etc groups are still captured.
2676 * @param array $cidRefs
2678 public static function appendCustomTablesExtendingContacts(&$cidRefs) {
2679 $customValueTables = CRM_Core_BAO_CustomGroup
::getAllCustomGroupsByBaseEntity('Contact');
2680 $customValueTables->find();
2681 while ($customValueTables->fetch()) {
2682 $cidRefs[$customValueTables->table_name
][] = 'entity_id';
2687 * Add custom ContactReference fields to the list of contact references
2689 * This includes active and inactive fields/groups
2691 * @param array $cidRefs
2693 * @throws \CiviCRM_API3_Exception
2695 public static function appendCustomContactReferenceFields(&$cidRefs) {
2696 $fields = civicrm_api3('CustomField', 'get', [
2697 'return' => ['column_name', 'custom_group_id.table_name'],
2698 'data_type' => 'ContactReference',
2699 'options' => ['limit' => 0],
2701 foreach ($fields as $field) {
2702 $cidRefs[$field['custom_group_id.table_name']][] = $field['column_name'];
2707 * Lookup the value of a MySQL global configuration variable.
2709 * @param string $name
2710 * E.g. "thread_stack".
2711 * @param mixed $default
2714 public static function getGlobalSetting($name, $default = NULL) {
2715 // Alternatively, SELECT @@GLOBAL.thread_stack, but
2716 // that has been reported to fail under MySQL 5.0 for OS X
2717 $escapedName = self
::escapeString($name);
2718 $dao = CRM_Core_DAO
::executeQuery("SHOW VARIABLES LIKE '$escapedName'");
2719 if ($dao->fetch()) {
2728 * Update the fields array to also hold keys for pseudoconstant fields that relate to contained fields.
2730 * This is relevant where we want to offer both the ID field and the label field
2731 * as an option, e.g. search builder.
2733 * It is currently limited for optionGroupName & id+ name+ FK combos for purposes keeping the scope of the
2734 * change small, but is appropriate for other sorts of pseudoconstants.
2736 * @param array $fields
2738 public static function appendPseudoConstantsToFields(&$fields) {
2739 foreach ($fields as $fieldUniqueName => $field) {
2740 if (!empty($field['pseudoconstant'])) {
2741 $pseudoConstant = $field['pseudoconstant'];
2742 if (!empty($pseudoConstant['optionGroupName'])) {
2743 $fields[$pseudoConstant['optionGroupName']] = [
2744 'title' => CRM_Core_BAO_OptionGroup
::getTitleByName($pseudoConstant['optionGroupName']),
2745 'name' => $pseudoConstant['optionGroupName'],
2746 'data_type' => CRM_Utils_Type
::T_STRING
,
2747 'is_pseudofield_for' => $fieldUniqueName,
2750 // We restrict to id + name + FK as we are extending this a bit, but cautiously.
2752 !empty($field['FKClassName'])
2753 && CRM_Utils_Array
::value('keyColumn', $pseudoConstant) === 'id'
2754 && CRM_Utils_Array
::value('labelColumn', $pseudoConstant) === 'name'
2756 $pseudoFieldName = str_replace('_' . $pseudoConstant['keyColumn'], '', $field['name']);
2757 // This if is just an extra caution when adding change.
2758 if (!isset($fields[$pseudoFieldName])) {
2759 $daoName = $field['FKClassName'];
2760 $fkFields = $daoName::fields();
2761 foreach ($fkFields as $fkField) {
2762 if ($fkField['name'] === $pseudoConstant['labelColumn']) {
2763 $fields[$pseudoFieldName] = [
2764 'name' => $pseudoFieldName,
2765 'is_pseudofield_for' => $field['name'],
2766 'title' => $fkField['title'],
2767 'data_type' => $fkField['type'],
2768 'where' => $field['where'],
2779 * Get options for the called BAO object's field.
2781 * This function can be overridden by each BAO to add more logic related to context.
2782 * The overriding function will generally call the lower-level CRM_Core_PseudoConstant::get
2784 * @param string $fieldName
2785 * @param string $context
2786 * @see CRM_Core_DAO::buildOptionsContext
2787 * @param array $props
2788 * Raw field values; whatever is known about this bao object.
2790 * Note: $props can contain unsanitized input and should not be passed directly to CRM_Core_PseudoConstant::get
2792 * @return array|bool
2794 public static function buildOptions($fieldName, $context = NULL, $props = []) {
2795 // If a given bao does not override this function
2796 $baoName = get_called_class();
2797 return CRM_Core_PseudoConstant
::get($baoName, $fieldName, [], $context);
2801 * Populate option labels for this object's fields.
2803 * @throws exception if called directly on the base class
2805 public function getOptionLabels() {
2806 $fields = $this->fields();
2807 if ($fields === NULL) {
2808 throw new Exception('Cannot call getOptionLabels on CRM_Core_DAO');
2810 foreach ($fields as $field) {
2811 $name = $field['name'] ??
NULL;
2812 if ($name && isset($this->$name)) {
2813 $label = CRM_Core_PseudoConstant
::getLabel(get_class($this), $name, $this->$name);
2814 if ($label !== FALSE) {
2815 // Append 'label' onto the field name
2816 $labelName = $name . '_label';
2817 $this->$labelName = $label;
2824 * Provides documentation and validation for the buildOptions $context param
2826 * @param string $context
2828 * @throws CRM_Core_Exception
2831 public static function buildOptionsContext($context = NULL) {
2833 'get' => "get: all options are returned, even if they are disabled; labels are translated.",
2834 'create' => "create: options are filtered appropriately for the object being created/updated; labels are translated.",
2835 'search' => "search: searchable options are returned; labels are translated.",
2836 'validate' => "validate: all options are returned, even if they are disabled; machine names are used in place of labels.",
2837 'abbreviate' => "abbreviate: enabled options are returned; labels are replaced with abbreviations.",
2838 'match' => "match: enabled options are returned using machine names as keys; labels are translated.",
2840 // Validation: enforce uniformity of this param
2841 if ($context !== NULL && !isset($contexts[$context])) {
2842 throw new CRM_Core_Exception("'$context' is not a valid context for buildOptions.");
2848 * @param string $fieldName
2849 * @return bool|array
2851 public function getFieldSpec($fieldName) {
2852 $fields = $this->fields();
2854 // Support "unique names" as well as sql names
2855 $fieldKey = $fieldName;
2856 if (empty($fields[$fieldKey])) {
2857 $fieldKeys = $this->fieldKeys();
2858 $fieldKey = $fieldKeys[$fieldName] ??
NULL;
2860 // If neither worked then this field doesn't exist. Return false.
2861 if (empty($fields[$fieldKey])) {
2864 return $fields[$fieldKey];
2868 * Get SQL where clause for SQL filter syntax input parameters.
2870 * SQL version of api function to assign filters to the DAO based on the syntax
2871 * $field => array('IN' => array(4,6,9))
2873 * $field => array('LIKE' => array('%me%))
2876 * @param string $fieldName
2878 * @param array $filter
2879 * filter to be applied indexed by operator.
2880 * @param string $type
2881 * type of field (not actually used - nor in api @todo ).
2882 * @param string $alias
2883 * alternative field name ('as') @todo- not actually used.
2884 * @param bool $returnSanitisedArray
2885 * Return a sanitised array instead of a clause.
2886 * this is primarily so we can add filters @ the api level to the Query object based fields
2890 * @return NULL|string|array
2891 * a string is returned if $returnSanitisedArray is not set, otherwise and Array or NULL
2892 * depending on whether it is supported as yet
2894 public static function createSQLFilter($fieldName, $filter, $type = NULL, $alias = NULL, $returnSanitisedArray = FALSE) {
2895 foreach ($filter as $operator => $criteria) {
2896 if (!CRM_Core_BAO_SchemaHandler
::databaseSupportsUTF8MB4()) {
2897 foreach ((array) $criteria as $criterion) {
2898 if (!empty($criterion) && !is_numeric($criterion)
2899 // The first 2 criteria are redundant but are added as they
2900 // seem like they would
2901 // be quicker than this 3rd check.
2902 && max(array_map('ord', str_split($criterion))) >= 240) {
2903 // String contains unsupported emojis.
2904 // We return a clause that resolves to false as an emoji string by definition cannot be saved.
2905 // note that if we return just 0 for false if gets lost in empty checks.
2906 // https://stackoverflow.com/questions/16496554/can-php-detect-4-byte-encoded-utf8-chars
2912 if (in_array($operator, self
::acceptedSQLOperators(), TRUE)) {
2913 switch ($operator) {
2917 if (!$returnSanitisedArray) {
2918 return (sprintf('%s %s', $fieldName, $operator));
2921 return (sprintf('%s %s ', $fieldName, $operator));
2925 // ternary operators
2928 if ((empty($criteria[0]) && !in_array($criteria[0], ['0', 0]))||
(empty($criteria[1]) && !in_array($criteria[1], ['0', 0]))) {
2929 throw new Exception("invalid criteria for $operator");
2931 if (!$returnSanitisedArray) {
2932 return (sprintf('%s ' . $operator . ' "%s" AND "%s"', $fieldName, CRM_Core_DAO
::escapeString($criteria[0]), CRM_Core_DAO
::escapeString($criteria[1])));
2935 // not yet implemented (tests required to implement)
2943 if (empty($criteria)) {
2944 throw new Exception("invalid criteria for $operator");
2946 $escapedCriteria = array_map([
2950 if (!$returnSanitisedArray) {
2951 return (sprintf('%s %s ("%s")', $fieldName, $operator, implode('", "', $escapedCriteria)));
2953 return $escapedCriteria;
2958 if (!$returnSanitisedArray) {
2959 return (sprintf('%s %s "%s"', $fieldName, $operator, CRM_Core_DAO
::escapeString($criteria)));
2962 // not yet implemented (tests required to implement)
2971 * @see http://issues.civicrm.org/jira/browse/CRM-9150
2972 * support for other syntaxes is discussed in ticket but being put off for now
2975 public static function acceptedSQLOperators() {
2996 * SQL has a limit of 64 characters on various names:
2997 * table name, trigger name, column name ...
2999 * For custom groups and fields we generated names from user entered input
3000 * which can be longer than this length, this function helps with creating
3001 * strings that meet various criteria.
3003 * @param string $string
3004 * The string to be shortened.
3005 * @param int $length
3006 * The max length of the string.
3008 * @param bool $makeRandom
3012 public static function shortenSQLName($string, $length = 60, $makeRandom = FALSE) {
3013 // early return for strings that meet the requirements
3014 if (strlen($string) <= $length) {
3018 // easy return for calls that dont need a randomized uniq string
3020 return substr($string, 0, $length);
3023 // the string is longer than the length and we need a uniq string
3024 // for the same tablename we need the same uniq string every time
3025 // hence we use md5 on the string, which is not random
3026 // we'll append 8 characters to the end of the tableName
3027 $md5string = substr(md5($string), 0, 8);
3028 return substr($string, 0, $length - 8) . "_{$md5string}";
3032 * https://issues.civicrm.org/jira/browse/CRM-17748
3033 * Sets the internal options to be used on a query
3035 * @param array $options
3038 public function setOptions($options) {
3039 if (is_array($options)) {
3040 $this->_options
= $options;
3045 * https://issues.civicrm.org/jira/browse/CRM-17748
3046 * wrapper to pass internal DAO options down to DB_mysql/DB_Common level
3048 * @param array $options
3051 protected function _setDBOptions($options) {
3052 global $_DB_DATAOBJECT;
3054 if (is_array($options) && count($options)) {
3055 $conn = &$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5
];
3056 foreach ($options as $option_name => $option_value) {
3057 $conn->setOption($option_name, $option_value);
3064 * @param array $params
3066 public function setApiFilter(&$params) {
3070 * Generates acl clauses suitable for adding to WHERE or ON when doing an api.get for this entity
3072 * Return format is in the form of fieldname => clauses starting with an operator. e.g.:
3075 * 'location_type_id' => array('IS NOT NULL', 'IN (1,2,3)')
3079 * Note that all array keys must be actual field names in this entity. Use subqueries to filter on other tables e.g. custom values.
3083 public function addSelectWhereClause() {
3085 $fields = $this->fields();
3086 foreach ($fields as $fieldName => $field) {
3087 // Clause for contact-related entities like Email, Relationship, etc.
3088 if (strpos($fieldName, 'contact_id') === 0 && CRM_Utils_Array
::value('FKClassName', $field) == 'CRM_Contact_DAO_Contact') {
3089 $clauses[$fieldName] = CRM_Utils_SQL
::mergeSubquery('Contact');
3091 // Clause for an entity_table/entity_id combo
3092 if ($fieldName === 'entity_id' && isset($fields['entity_table'])) {
3093 $relatedClauses = [];
3094 $relatedEntities = $this->buildOptions('entity_table', 'get');
3095 foreach ((array) $relatedEntities as $table => $ent) {
3097 $ent = CRM_Core_DAO_AllCoreTables
::getBriefName(CRM_Core_DAO_AllCoreTables
::getClassForTable($table));
3098 $subquery = CRM_Utils_SQL
::mergeSubquery($ent);
3100 $relatedClauses[] = "(entity_table = '$table' AND entity_id " . implode(' AND entity_id ', $subquery) . ")";
3103 $relatedClauses[] = "(entity_table = '$table')";
3107 if ($relatedClauses) {
3108 $clauses['id'] = 'IN (SELECT id FROM `' . $this->tableName() . '` WHERE (' . implode(') OR (', $relatedClauses) . '))';
3112 CRM_Utils_Hook
::selectWhereClause($this, $clauses);
3117 * This returns the final permissioned query string for this entity
3119 * With acls from related entities + additional clauses from hook_civicrm_selectWhereClause
3121 * @param string $tableAlias
3124 public static function getSelectWhereClause($tableAlias = NULL) {
3125 $bao = new static();
3126 if ($tableAlias === NULL) {
3127 $tableAlias = $bao->tableName();
3130 foreach ((array) $bao->addSelectWhereClause() as $field => $vals) {
3131 $clauses[$field] = NULL;
3133 $clauses[$field] = "(`$tableAlias`.`$field` IS NULL OR (`$tableAlias`.`$field` " . implode(" AND `$tableAlias`.`$field` ", (array) $vals) . '))';
3140 * ensure database name is 'safe', i.e. only contains word characters (includes underscores)
3141 * and dashes, and contains at least one [a-z] case insensitive.
3147 public static function requireSafeDBName($database) {
3150 "/^[\w\-]*[a-z]+[\w\-]*$/i",
3154 if (empty($matches)) {
3161 * Transform an array to a serialized string for database storage.
3163 * @param array|null $value
3164 * @param int $serializationType
3165 * @return string|null
3167 * @throws \Exception
3169 public static function serializeField($value, $serializationType) {
3170 if ($value === NULL) {
3173 switch ($serializationType) {
3174 case self
::SERIALIZE_SEPARATOR_BOOKEND
:
3175 return $value === [] ?
'' : CRM_Utils_Array
::implodePadded($value);
3177 case self
::SERIALIZE_SEPARATOR_TRIMMED
:
3178 return is_array($value) ?
implode(self
::VALUE_SEPARATOR
, $value) : $value;
3180 case self
::SERIALIZE_JSON
:
3181 return is_array($value) ?
json_encode($value) : $value;
3183 case self
::SERIALIZE_PHP
:
3184 return is_array($value) ?
serialize($value) : $value;
3186 case self
::SERIALIZE_COMMA
:
3187 return is_array($value) ?
implode(',', $value) : $value;
3190 throw new Exception('Unknown serialization method for field.');
3195 * Transform a serialized string from the database into an array.
3197 * @param string|null $value
3198 * @param $serializationType
3200 * @return array|null
3201 * @throws CRM_Core_Exception
3203 public static function unSerializeField($value, $serializationType) {
3204 if ($value === NULL) {
3207 if ($value === '') {
3210 switch ($serializationType) {
3211 case self
::SERIALIZE_SEPARATOR_BOOKEND
:
3212 return (array) CRM_Utils_Array
::explodePadded($value);
3214 case self
::SERIALIZE_SEPARATOR_TRIMMED
:
3215 return explode(self
::VALUE_SEPARATOR
, trim($value));
3217 case self
::SERIALIZE_JSON
:
3218 return strlen($value) ?
json_decode($value, TRUE) : [];
3220 case self
::SERIALIZE_PHP
:
3221 return strlen($value) ? CRM_Utils_String
::unserialize($value) : [];
3223 case self
::SERIALIZE_COMMA
:
3224 return explode(',', trim(str_replace(', ', '', $value)));
3227 throw new CRM_Core_Exception('Unknown serialization method for field.');
3234 public static function getEntityRefFilters() {
3239 * Get exportable fields with pseudoconstants rendered as an extra field.
3241 * @param string $baoClass
3245 public static function getExportableFieldsWithPseudoConstants($baoClass) {
3246 if (method_exists($baoClass, 'exportableFields')) {
3247 $fields = $baoClass::exportableFields();
3250 $fields = $baoClass::export();
3252 CRM_Core_DAO
::appendPseudoConstantsToFields($fields);
3257 * Remove item from static cache during update/delete operations
3259 private function clearDbColumnValueCache() {
3260 $daoName = get_class($this);
3261 while (strpos($daoName, '_BAO_') !== FALSE) {
3262 $daoName = get_parent_class($daoName);
3264 if (isset($this->id
)) {
3265 unset(self
::$_dbColumnValueCache[$daoName]['id'][$this->id
]);
3267 if (isset($this->name
)) {
3268 unset(self
::$_dbColumnValueCache[$daoName]['name'][$this->name
]);
3273 * Return a mapping from field-name to the corresponding key (as used in fields()).
3276 * Array(string $name => string $uniqueName).
3278 public static function fieldKeys() {
3279 return array_flip(CRM_Utils_Array
::collect('name', static::fields()));
3283 * Returns system paths related to this entity (as defined in the xml schema)
3287 public static function getEntityPaths() {
3288 return static::$_paths ??
[];