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;
59 public static $_nullArray = [];
61 public static $_dbColumnValueCache = NULL;
62 const NOT_NULL
= 1, IS_NULL
= 2,
64 VALUE_SEPARATOR
= "\ 1",
65 BULK_INSERT_COUNT
= 200,
66 BULK_INSERT_HIGH_COUNT
= 200,
67 QUERY_FORMAT_WILDCARD
= 1,
68 QUERY_FORMAT_NO_QUOTES
= 2,
71 * Serialized string separated by and bookended with VALUE_SEPARATOR
73 SERIALIZE_SEPARATOR_BOOKEND
= 1,
75 * @deprecated format separated by VALUE_SEPARATOR
77 SERIALIZE_SEPARATOR_TRIMMED
= 2,
79 * Recommended serialization format
83 * @deprecated format using php serialize()
87 * Comma separated string, no quotes, no spaces
92 * Define entities that shouldn't be created or deleted when creating/ deleting
93 * test objects - this prevents world regions, countries etc from being added / deleted
96 public static $_testEntitiesToSkip = [];
98 * The factory class for this application.
101 public static $_factory = NULL;
103 public static $_checkedSqlFunctionsExist = FALSE;
106 * https://issues.civicrm.org/jira/browse/CRM-17748
107 * internal variable for DAO to hold per-query settings
110 protected $_options = [];
115 * @return \CRM_Core_DAO
117 public function __construct() {
119 $this->__table
= $this->getTableName();
123 * Returns localized title of this entity.
126 public static function getEntityTitle() {
127 $className = static::class;
128 Civi
::log()->warning("$className needs to be regeneraged. Missing getEntityTitle method.", ['civi.tag' => 'deprecated']);
129 return CRM_Core_DAO_AllCoreTables
::getBriefName($className);
132 public function __clone() {
133 if (!empty($this->_DB_resultid
)) {
134 $this->resultCopies++
;
141 public function __destruct() {
142 if ($this->resultCopies
=== 0) {
145 $this->resultCopies
--;
149 * Empty definition for virtual function.
151 public static function getTableName() {
156 * Initialize the DAO object.
159 * The database connection string.
161 public static function init($dsn) {
162 Civi
::$statics[__CLASS__
]['init'] = 1;
163 $options = &PEAR
::getStaticProperty('DB_DataObject', 'options');
164 $options['database'] = $dsn;
165 $options['quote_identifiers'] = TRUE;
166 if (defined('CIVICRM_DAO_DEBUG')) {
167 self
::DebugLevel(CIVICRM_DAO_DEBUG
);
169 $factory = new CRM_Contact_DAO_Factory();
170 CRM_Core_DAO
::setFactory($factory);
171 $currentModes = CRM_Utils_SQL
::getSqlModes();
172 if (CRM_Utils_Constant
::value('CIVICRM_MYSQL_STRICT', CRM_Utils_System
::isDevelopment())) {
173 if (CRM_Utils_SQL
::supportsFullGroupBy() && !in_array('ONLY_FULL_GROUP_BY', $currentModes) && CRM_Utils_SQL
::isGroupByModeInDefault()) {
174 $currentModes[] = 'ONLY_FULL_GROUP_BY';
176 if (!in_array('STRICT_TRANS_TABLES', $currentModes)) {
177 $currentModes = array_merge(['STRICT_TRANS_TABLES'], $currentModes);
179 CRM_Core_DAO
::executeQuery("SET SESSION sql_mode = %1", [1 => [implode(',', $currentModes), 'String']]);
181 CRM_Core_DAO
::executeQuery('SET NAMES utf8mb4');
182 CRM_Core_DAO
::executeQuery('SET @uniqueID = %1', [1 => [CRM_Utils_Request
::id(), 'String']]);
188 public static function getConnection() {
189 global $_DB_DATAOBJECT;
190 $dao = new CRM_Core_DAO();
191 return $_DB_DATAOBJECT['CONNECTIONS'][$dao->_database_dsn_md5
];
195 * Disables usage of the ONLY_FULL_GROUP_BY Mode if necessary
197 public static function disableFullGroupByMode() {
198 $currentModes = CRM_Utils_SQL
::getSqlModes();
199 if (in_array('ONLY_FULL_GROUP_BY', $currentModes) && CRM_Utils_SQL
::isGroupByModeInDefault()) {
200 $key = array_search('ONLY_FULL_GROUP_BY', $currentModes);
201 unset($currentModes[$key]);
202 CRM_Core_DAO
::executeQuery("SET SESSION sql_mode = %1", [1 => [implode(',', $currentModes), 'String']]);
207 * Re-enables ONLY_FULL_GROUP_BY sql_mode as necessary..
209 public static function reenableFullGroupByMode() {
210 $currentModes = CRM_Utils_SQL
::getSqlModes();
211 if (!in_array('ONLY_FULL_GROUP_BY', $currentModes) && CRM_Utils_SQL
::isGroupByModeInDefault()) {
212 $currentModes[] = 'ONLY_FULL_GROUP_BY';
213 CRM_Core_DAO
::executeQuery("SET SESSION sql_mode = %1", [1 => [implode(',', $currentModes), 'String']]);
218 * @param string $fieldName
220 * @param array $params
222 protected function assignTestFK($fieldName, $fieldDef, $params) {
223 $required = $fieldDef['required'] ??
NULL;
224 $FKClassName = $fieldDef['FKClassName'] ??
NULL;
225 $dbName = $fieldDef['name'];
226 $daoName = str_replace('_BAO_', '_DAO_', get_class($this));
228 // skip the FK if it is not required
229 // if it's contact id we should create even if not required
230 // we'll have a go @ fetching first though
231 // we WILL create campaigns though for so tests with a campaign pseudoconstant will complete
232 if ($FKClassName === 'CRM_Campaign_DAO_Campaign' && $daoName != $FKClassName) {
235 if (!$required && $dbName != 'contact_id') {
236 $fkDAO = new $FKClassName();
237 if ($fkDAO->find(TRUE)) {
238 $this->$dbName = $fkDAO->id
;
242 elseif (in_array($FKClassName, CRM_Core_DAO
::$_testEntitiesToSkip)) {
243 $depObject = new $FKClassName();
244 $depObject->find(TRUE);
245 $this->$dbName = $depObject->id
;
247 elseif ($daoName == 'CRM_Member_DAO_MembershipType' && $fieldName == 'member_of_contact_id') {
248 // FIXME: the fields() metadata is not specific enough
249 $depObject = CRM_Core_DAO
::createTestObject($FKClassName, ['contact_type' => 'Organization']);
250 $this->$dbName = $depObject->id
;
253 //if it is required we need to generate the dependency object first
254 $depObject = CRM_Core_DAO
::createTestObject($FKClassName, CRM_Utils_Array
::value($dbName, $params, 1));
255 $this->$dbName = $depObject->id
;
260 * Generate and assign an arbitrary value to a field of a test object.
262 * @param string $fieldName
263 * @param array $fieldDef
264 * @param int $counter
265 * The globally-unique ID of the test object.
267 * @throws \CRM_Core_Exception
269 protected function assignTestValue($fieldName, &$fieldDef, $counter) {
270 $dbName = $fieldDef['name'];
271 $daoName = get_class($this);
274 if (!$handled && $dbName == 'contact_sub_type') {
275 //coming up with a rule to set this is too complex let's not set it
279 // Pick an option value if needed
280 if (!$handled && $fieldDef['type'] !== CRM_Utils_Type
::T_BOOLEAN
) {
281 $options = $daoName::buildOptions($dbName, 'create');
283 $this->$dbName = key($options);
289 switch ($fieldDef['type']) {
290 case CRM_Utils_Type
::T_INT
:
291 case CRM_Utils_Type
::T_FLOAT
:
292 case CRM_Utils_Type
::T_MONEY
:
293 if (isset($fieldDef['precision'])) {
294 // $object->$dbName = CRM_Utils_Number::createRandomDecimal($value['precision']);
295 $this->$dbName = CRM_Utils_Number
::createTruncatedDecimal($counter, $fieldDef['precision']);
298 $this->$dbName = $counter;
302 case CRM_Utils_Type
::T_BOOLEAN
:
303 if (isset($fieldDef['default'])) {
304 $this->$dbName = $fieldDef['default'];
306 elseif ($fieldDef['name'] == 'is_deleted' ||
$fieldDef['name'] == 'is_test') {
314 case CRM_Utils_Type
::T_DATE
:
315 case CRM_Utils_Type
::T_DATE + CRM_Utils_Type
::T_TIME
:
316 $this->$dbName = '19700101';
317 if ($dbName == 'end_date') {
318 // put this in the future
319 $this->$dbName = '20200101';
323 case CRM_Utils_Type
::T_TIMESTAMP
:
324 $this->$dbName = '19700201000000';
327 case CRM_Utils_Type
::T_TIME
:
328 throw new CRM_Core_Exception('T_TIME shouldn\'t be used.');
330 case CRM_Utils_Type
::T_CCNUM
:
331 $this->$dbName = '4111 1111 1111 1111';
334 case CRM_Utils_Type
::T_URL
:
335 $this->$dbName = 'http://www.civicrm.org';
338 case CRM_Utils_Type
::T_STRING
:
339 case CRM_Utils_Type
::T_BLOB
:
340 case CRM_Utils_Type
::T_MEDIUMBLOB
:
341 case CRM_Utils_Type
::T_TEXT
:
342 case CRM_Utils_Type
::T_LONGTEXT
:
343 case CRM_Utils_Type
::T_EMAIL
:
345 // WAS: if (isset($value['enumValues'])) {
346 // TODO: see if this works with all pseudoconstants
347 if (isset($fieldDef['pseudoconstant'], $fieldDef['pseudoconstant']['callback'])) {
348 if (isset($fieldDef['default'])) {
349 $this->$dbName = $fieldDef['default'];
352 $options = CRM_Core_PseudoConstant
::get($daoName, $fieldName);
353 if (is_array($options)) {
354 $this->$dbName = $options[0];
357 $defaultValues = explode(',', $options);
358 $this->$dbName = $defaultValues[0];
363 $this->$dbName = $dbName . '_' . $counter;
364 $maxlength = $fieldDef['maxlength'] ??
NULL;
365 if ($maxlength > 0 && strlen($this->$dbName) > $maxlength) {
366 $this->$dbName = substr($this->$dbName, 0, $fieldDef['maxlength']);
374 * Reset the DAO object.
376 * DAO is kinda crappy in that there is an unwritten rule of one query per DAO.
378 * We attempt to get around this crappy restriction by resetting some of DAO's internal fields. Use this with caution
380 public function reset() {
382 foreach (array_keys($this->table()) as $field) {
383 unset($this->$field);
387 * reset the various DB_DAO structures manually
396 * @param string $tableName
400 public static function getLocaleTableName($tableName) {
403 $tables = CRM_Core_I18n_Schema
::schemaStructureTables();
404 if (in_array($tableName, $tables)) {
405 return $tableName . $dbLocale;
412 * Execute a query by the current DAO, localizing it along the way (if needed).
414 * @param string $query
415 * The SQL query for execution.
416 * @param bool $i18nRewrite
417 * Whether to rewrite the query.
420 * the current DAO object after the query execution
422 public function query($query, $i18nRewrite = TRUE) {
423 // rewrite queries that should use $dbLocale-based views for multi-language installs
424 global $dbLocale, $_DB_DATAOBJECT;
426 if (empty($_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5
])) {
427 // Will force connection to be populated per CRM-20541.
431 $conn = &$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5
];
432 $orig_options = $conn->options
;
433 $this->_setDBOptions($this->_options
);
435 if ($i18nRewrite and $dbLocale) {
436 $query = CRM_Core_I18n_Schema
::rewriteQuery($query);
439 $ret = parent
::query($query);
441 $this->_setDBOptions($orig_options);
446 * Static function to set the factory instance for this class.
448 * @param object $factory
449 * The factory application object.
451 public static function setFactory(&$factory) {
452 self
::$_factory = &$factory;
456 * Factory method to instantiate a new object from a table name.
458 * @param string $table
459 * @return \DataObject|\PEAR_Error
461 public function factory($table = '') {
462 if (!isset(self
::$_factory)) {
463 return parent
::factory($table);
466 return self
::$_factory->create($table);
470 * Initialization for all DAO objects. Since we access DB_DO programatically
471 * we need to set the links manually.
473 public function initialize() {
475 if (empty(Civi
::$statics[__CLASS__
]['init'])) {
476 // CRM_Core_DAO::init() must be called before CRM_Core_DAO->initialize().
477 // This occurs very early in bootstrap - error handlers may not be wired up.
478 echo "Inconsistent system initialization sequence. Premature access of (" . get_class($this) . ")";
479 CRM_Utils_System
::civiExit();
484 * Defines the default key as 'id'.
488 public function keys() {
497 * Tells DB_DataObject which keys use autoincrement.
498 * 'id' is autoincrementing by default.
503 public function sequenceKey() {
504 static $sequenceKeys;
505 if (!isset($sequenceKeys)) {
506 $sequenceKeys = ['id', TRUE];
508 return $sequenceKeys;
512 * Returns list of FK relationships.
516 * Array of CRM_Core_Reference_Interface
518 public static function getReferenceColumns() {
523 * Returns all the column names of this table.
528 public static function &fields() {
534 * Returns all usable fields, indexed by name.
536 * This function differs from fields() in that it indexes by name rather than unique_name.
538 * It excludes fields not added yet by pending upgrades.
539 * This avoids problems with trying to SELECT a field that exists in code but has not yet been added to the db.
541 * @param bool $checkPermissions
542 * Filter by field permissions.
545 public static function getSupportedFields($checkPermissions = FALSE) {
546 $fields = array_column((array) static::fields(), NULL, 'name');
548 // Exclude fields yet not added by pending upgrades
549 $dbVer = \CRM_Core_BAO_Domain
::version();
550 if ($fields && version_compare($dbVer, \CRM_Utils_System
::version()) < 0) {
551 $fields = array_filter($fields, function($field) use ($dbVer) {
552 $add = $field['add'] ??
'1.0.0';
553 if (substr_count($add, '.') < 2) {
556 return version_compare($dbVer, $add, '>=');
560 // Exclude fields the user does not have permission for
561 if ($checkPermissions) {
562 $fields = array_filter($fields, function($field) {
563 return empty($field['permission']) || CRM_Core_Permission
::check($field['permission']);
571 * Get/set an associative array of table columns
576 public function table() {
577 $fields = $this->fields();
581 foreach ($fields as $name => $value) {
582 $table[$value['name']] = $value['type'];
583 if (!empty($value['required'])) {
584 $table[$value['name']] +
= self
::DB_DAO_NOTNULL
;
597 * @return CRM_Core_DAO
599 public function save($hook = TRUE) {
600 if (!empty($this->id
)) {
602 $preEvent = new \Civi\Core\DAO\Event\
PreUpdate($this);
603 \Civi
::dispatcher()->dispatch("civi.dao.preUpdate", $preEvent);
606 $result = $this->update();
609 $event = new \Civi\Core\DAO\Event\
PostUpdate($this, $result);
610 \Civi
::dispatcher()->dispatch("civi.dao.postUpdate", $event);
612 $this->clearDbColumnValueCache();
616 $preEvent = new \Civi\Core\DAO\Event\
PreUpdate($this);
617 \Civi
::dispatcher()->dispatch("civi.dao.preInsert", $preEvent);
620 $result = $this->insert();
623 $event = new \Civi\Core\DAO\Event\
PostUpdate($this, $result);
624 \Civi
::dispatcher()->dispatch("civi.dao.postInsert", $event);
630 CRM_Utils_Hook
::postSave($this);
637 * Deletes items from table which match current objects variables.
639 * Returns the true on success
643 * Designed to be extended
645 * $object = new mytable();
647 * echo $object->delete(); // builds a conditon
649 * $object = new mytable();
650 * $object->whereAdd('age > 12');
652 * $object->orderBy('age DESC');
653 * $object->delete(true); // dont use object vars, use the conditions, limit and order.
655 * @param bool $useWhere (optional) If DB_DATAOBJECT_WHEREADD_ONLY is passed in then
656 * we will build the condition only using the whereAdd's. Default is to
657 * build the condition only using the object parameters.
659 * * @return mixed Int (No. of rows affected) on success, false on failure, 0 on no data affected
661 public function delete($useWhere = FALSE) {
662 $preEvent = new \Civi\Core\DAO\Event\
PreDelete($this);
663 \Civi
::dispatcher()->dispatch("civi.dao.preDelete", $preEvent);
665 $result = parent
::delete($useWhere);
667 $event = new \Civi\Core\DAO\Event\
PostDelete($this, $result);
668 \Civi
::dispatcher()->dispatch("civi.dao.postDelete", $event);
671 $this->clearDbColumnValueCache();
677 * @param bool $created
679 public function log($created = FALSE) {
682 if (!$this->getLog()) {
687 $session = CRM_Core_Session
::singleton();
688 $cid = $session->get('userID');
691 // return is we dont have handle to FK
696 $dao = new CRM_Core_DAO_Log();
697 $dao->entity_table
= $this->getTableName();
698 $dao->entity_id
= $this->id
;
699 $dao->modified_id
= $cid;
700 $dao->modified_date
= date("YmdHis");
705 * Given an associative array of name/value pairs, extract all the values
706 * that belong to this object and initialize the object with said values
708 * @param array $params
709 * Array of name/value pairs to save.
712 * Did we copy all null values into the object
714 public function copyValues($params) {
716 foreach ($this->fields() as $uniqueName => $field) {
717 $dbName = $field['name'];
718 if (array_key_exists($dbName, $params)) {
719 $value = $params[$dbName];
722 elseif (array_key_exists($uniqueName, $params)) {
723 $value = $params[$uniqueName];
730 // if there is no value then make the variable NULL
733 $this->$dbName = 'null';
735 elseif (is_array($value) && !empty($field['serialize'])) {
736 $this->$dbName = CRM_Core_DAO
::serializeField($value, $field['serialize']);
740 $maxLength = $field['maxlength'] ??
NULL;
741 if (!is_array($value) && $maxLength && mb_strlen($value) > $maxLength && empty($field['pseudoconstant'])) {
742 Civi
::log()->warning(ts('A string for field $dbName has been truncated. The original string was %1', [CRM_Utils_Type
::escape($value, 'String')]));
743 // The string is too long - what to do what to do? Well losing data is generally bad so lets' truncate
744 $value = CRM_Utils_String
::ellipsify($value, $maxLength);
746 $this->$dbName = $value;
755 * Store all the values from this object in an associative array
756 * this is a destructive store, calling function is responsible
757 * for keeping sanity of id's.
759 * @param object $object
760 * The object that we are extracting data from.
761 * @param array $values
762 * (reference ) associative array of name/value pairs.
764 public static function storeValues(&$object, &$values) {
765 $fields = $object->fields();
766 foreach ($fields as $name => $value) {
767 $dbName = $value['name'];
768 if (isset($object->$dbName) && $object->$dbName !== 'null') {
769 $values[$dbName] = $object->$dbName;
770 if ($name != $dbName) {
771 $values[$name] = $object->$dbName;
778 * Create an attribute for this specific field. We only do this for strings and text
780 * @param array $field
781 * The field under task.
784 * the attributes for the object
786 public static function makeAttribute($field) {
788 if (CRM_Utils_Array
::value('type', $field) == CRM_Utils_Type
::T_STRING
) {
789 $maxLength = $field['maxlength'] ??
NULL;
790 $size = $field['size'] ??
NULL;
791 if ($maxLength ||
$size) {
794 $attributes['maxlength'] = $maxLength;
797 $attributes['size'] = $size;
802 elseif (CRM_Utils_Array
::value('type', $field) == CRM_Utils_Type
::T_TEXT
) {
803 $rows = $field['rows'] ??
NULL;
807 $cols = $field['cols'] ??
NULL;
813 $attributes['rows'] = $rows;
814 $attributes['cols'] = $cols;
817 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
) {
818 $attributes['size'] = 6;
819 $attributes['maxlength'] = 14;
827 * Get the size and maxLength attributes for this text field.
828 * (or for all text fields) in the DAO object.
830 * @param string $class
832 * @param string $fieldName
833 * Field that i'm interested in or null if.
834 * you want the attributes for all DAO text fields
837 * assoc array of name => attribute pairs
839 public static function getAttribute($class, $fieldName = NULL) {
840 $object = new $class();
841 $fields = $object->fields();
842 if ($fieldName != NULL) {
843 $field = $fields[$fieldName] ??
NULL;
844 return self
::makeAttribute($field);
848 foreach ($fields as $name => $field) {
849 $attribute = self
::makeAttribute($field);
851 $attributes[$name] = $attribute;
855 if (!empty($attributes)) {
863 * Create or update a record from supplied params.
865 * If 'id' is supplied, an existing record will be updated
866 * Otherwise a new record will be created.
868 * @param array $record
869 * @return CRM_Core_DAO
870 * @throws CRM_Core_Exception
872 public static function writeRecord(array $record) {
873 $hook = empty($record['id']) ?
'create' : 'edit';
874 $className = CRM_Core_DAO_AllCoreTables
::getCanonicalClassName(static::class);
875 if ($className === 'CRM_Core_DAO') {
876 throw new CRM_Core_Exception('Function writeRecord must be called on a subclass of CRM_Core_DAO');
878 $entityName = CRM_Core_DAO_AllCoreTables
::getBriefName($className);
880 \CRM_Utils_Hook
::pre($hook, $entityName, $record['id'] ??
NULL, $record);
881 $instance = new $className();
882 $instance->copyValues($record);
884 \CRM_Utils_Hook
::post($hook, $entityName, $instance->id
, $instance);
890 * Delete a record from supplied params.
892 * @param array $record
894 * @return CRM_Core_DAO
895 * @throws CRM_Core_Exception
897 public static function deleteRecord(array $record) {
898 $className = CRM_Core_DAO_AllCoreTables
::getCanonicalClassName(static::class);
899 if ($className === 'CRM_Core_DAO') {
900 throw new CRM_Core_Exception('Function deleteRecord must be called on a subclass of CRM_Core_DAO');
902 $entityName = CRM_Core_DAO_AllCoreTables
::getBriefName($className);
903 if (empty($record['id'])) {
904 throw new CRM_Core_Exception("Cannot delete {$entityName} with no id.");
907 CRM_Utils_Hook
::pre('delete', $entityName, $record['id'], $record);
908 $instance = new $className();
909 $instance->id
= $record['id'];
910 if (!$instance->delete()) {
911 throw new CRM_Core_Exception("Could not delete {$entityName} id {$record['id']}");
913 CRM_Utils_Hook
::post('delete', $entityName, $record['id'], $instance);
919 * Check if there is a record with the same name in the db.
921 * @param string $value
922 * The value of the field we are checking.
923 * @param string $daoName
924 * The dao object name.
925 * @param string $daoID
926 * The id of the object being updated. u can change your name.
927 * as long as there is no conflict
928 * @param string $fieldName
929 * The name of the field in the DAO.
931 * @param string $domainID
932 * The id of the domain. Object exists only for the given domain.
935 * true if object exists
937 public static function objectExists($value, $daoName, $daoID, $fieldName = 'name', $domainID = NULL) {
938 $object = new $daoName();
939 $object->$fieldName = $value;
941 $object->domain_id
= $domainID;
944 if ($object->find(TRUE)) {
945 return $daoID && $object->id
== $daoID;
953 * Check if there is a given column in a specific table.
956 * @see CRM_Core_BAO_SchemaHandler::checkIfFieldExists
958 * @param string $tableName
959 * @param string $columnName
960 * @param bool $i18nRewrite
961 * Whether to rewrite the query on multilingual setups.
964 * true if exists, else false
966 public static function checkFieldExists($tableName, $columnName, $i18nRewrite = TRUE) {
967 CRM_Core_Error
::deprecatedFunctionWarning('CRM_Core_BAO_SchemaHandler::checkIfFieldExists');
968 return CRM_Core_BAO_SchemaHandler
::checkIfFieldExists($tableName, $columnName, $i18nRewrite);
972 * Scans all the tables using a slow query and table name.
976 public static function getTableNames() {
977 $dao = CRM_Core_DAO
::executeQuery(
979 FROM information_schema.TABLES
980 WHERE TABLE_SCHEMA = '" . CRM_Core_DAO
::getDatabaseName() . "'
981 AND TABLE_NAME LIKE 'civicrm_%'
982 AND TABLE_NAME NOT LIKE 'civicrm_import_job_%'
983 AND TABLE_NAME NOT LIKE '%_temp%'
986 while ($dao->fetch()) {
987 $values[] = $dao->TABLE_NAME
;
993 * @param int $maxTablesToCheck
997 public static function isDBMyISAM($maxTablesToCheck = 10) {
998 return CRM_Core_DAO
::singleValueQuery(
1000 FROM information_schema.TABLES
1001 WHERE ENGINE = 'MyISAM'
1002 AND TABLE_SCHEMA = '" . CRM_Core_DAO
::getDatabaseName() . "'
1003 AND TABLE_NAME LIKE 'civicrm_%'
1004 AND TABLE_NAME NOT LIKE 'civicrm_import_job_%'
1005 AND TABLE_NAME NOT LIKE '%_temp%'
1006 AND TABLE_NAME NOT LIKE 'civicrm_tmp_%'
1011 * Get the name of the CiviCRM database.
1015 public static function getDatabaseName() {
1016 $daoObj = new CRM_Core_DAO();
1017 return $daoObj->database();
1021 * Checks if a constraint exists for a specified table.
1023 * @param string $tableName
1024 * @param string $constraint
1027 * true if constraint exists, false otherwise
1029 * @throws \CRM_Core_Exception
1031 public static function checkConstraintExists($tableName, $constraint) {
1034 if (!array_key_exists($tableName, $show)) {
1035 $query = "SHOW CREATE TABLE $tableName";
1036 $dao = CRM_Core_DAO
::executeQuery($query, [], TRUE, NULL, FALSE, FALSE);
1038 if (!$dao->fetch()) {
1039 throw new CRM_Core_Exception('query failed');
1042 $show[$tableName] = $dao->Create_Table
;
1045 return (bool) preg_match("/\b$constraint\b/i", $show[$tableName]);
1049 * Checks if CONSTRAINT keyword exists for a specified table.
1051 * @param array $tables
1053 * @throws CRM_Core_Exception
1056 * true if CONSTRAINT keyword exists, false otherwise
1058 public static function schemaRequiresRebuilding($tables = ["civicrm_contact"]) {
1060 foreach ($tables as $tableName) {
1061 if (!array_key_exists($tableName, $show)) {
1062 $query = "SHOW CREATE TABLE $tableName";
1063 $dao = CRM_Core_DAO
::executeQuery($query, [], TRUE, NULL, FALSE, FALSE);
1065 if (!$dao->fetch()) {
1066 throw new CRM_Core_Exception('Show create table failed.');
1069 $show[$tableName] = $dao->Create_Table
;
1072 $result = (bool) preg_match("/\bCONSTRAINT\b\s/i", $show[$tableName]);
1073 if ($result == TRUE) {
1084 * Checks if the FK constraint name is in the format 'FK_tableName_columnName'
1085 * for a specified column of a table.
1087 * @param string $tableName
1088 * @param string $columnName
1091 * true if in format, false otherwise
1093 * @throws \CRM_Core_Exception
1095 public static function checkFKConstraintInFormat($tableName, $columnName) {
1098 if (!array_key_exists($tableName, $show)) {
1099 $query = "SHOW CREATE TABLE $tableName";
1100 $dao = CRM_Core_DAO
::executeQuery($query);
1102 if (!$dao->fetch()) {
1103 throw new CRM_Core_Exception('query failed');
1106 $show[$tableName] = $dao->Create_Table
;
1108 $constraint = "`FK_{$tableName}_{$columnName}`";
1109 $pattern = "/\bCONSTRAINT\b\s+%s\s+\bFOREIGN\s+KEY\b\s/i";
1110 return (bool) preg_match(sprintf($pattern, $constraint), $show[$tableName]);
1114 * Check whether a specific column in a specific table has always the same value.
1116 * @param string $tableName
1117 * @param string $columnName
1118 * @param string $columnValue
1121 * true if the value is always $columnValue, false otherwise
1123 public static function checkFieldHasAlwaysValue($tableName, $columnName, $columnValue) {
1124 $query = "SELECT * FROM $tableName WHERE $columnName != '$columnValue'";
1125 $dao = CRM_Core_DAO
::executeQuery($query);
1126 $result = $dao->fetch() ?
FALSE : TRUE;
1131 * Check whether a specific column in a specific table is always NULL.
1133 * @param string $tableName
1134 * @param string $columnName
1137 * true if if the value is always NULL, false otherwise
1139 public static function checkFieldIsAlwaysNull($tableName, $columnName) {
1140 $query = "SELECT * FROM $tableName WHERE $columnName IS NOT NULL";
1141 $dao = CRM_Core_DAO
::executeQuery($query);
1142 $result = $dao->fetch() ?
FALSE : TRUE;
1147 * Check if there is a given table in the database.
1149 * @param string $tableName
1152 * true if exists, else false
1154 public static function checkTableExists($tableName) {
1159 $params = [1 => [$tableName, 'String']];
1161 $dao = CRM_Core_DAO
::executeQuery($query, $params);
1162 return (bool) $dao->fetch();
1166 * Check if a given table has data.
1168 * @param string $tableName
1170 * TRUE if $tableName has at least one record.
1172 public static function checkTableHasData($tableName) {
1173 $c = CRM_Core_DAO
::singleValueQuery(sprintf('SELECT count(*) c FROM `%s`', $tableName));
1182 public function checkVersion($version) {
1187 $dbVersion = CRM_Core_DAO
::singleValueQuery($query);
1188 return trim($version) == trim($dbVersion);
1192 * Find a DAO object for the given ID and return it.
1195 * Id of the DAO object being searched for.
1197 * @return CRM_Core_DAO
1198 * Object of the type of the class that called this function.
1202 public static function findById($id) {
1203 $object = new static();
1205 if (!$object->find(TRUE)) {
1206 throw new Exception("Unable to find a " . get_called_class() . " with id {$id}.");
1212 * Returns all results as array-encoded records.
1216 public function fetchAll($k = FALSE, $v = FALSE, $method = FALSE) {
1218 while ($this->fetch()) {
1219 $result[] = $this->toArray();
1225 * Return the results as PHP generator.
1227 * @param string $type
1228 * Whether the generator yields 'dao' objects or 'array's.
1230 public function fetchGenerator($type = 'dao') {
1231 while ($this->fetch()) {
1238 yield
$this->toArray();
1242 throw new \
RuntimeException("Invalid record type ($type)");
1248 * Returns a singular value.
1250 * @return mixed|NULL
1252 public function fetchValue() {
1253 $result = $this->getDatabaseResult();
1254 $row = $result->fetchRow();
1264 * Get all the result records as mapping between columns.
1266 * @param string $keyColumn
1268 * @param string $valueColumn
1271 * Ex: ["foo" => "The Foo Bar", "baz" => "The Baz Qux"]
1273 public function fetchMap($keyColumn, $valueColumn) {
1275 while ($this->fetch()) {
1276 $result[$this->{$keyColumn}] = $this->{$valueColumn};
1282 * Given a DAO name, a column name and a column value, find the record and GET the value of another column in that record
1284 * @param string $daoName
1285 * Name of the DAO (Example: CRM_Contact_DAO_Contact to retrieve value from a contact).
1286 * @param int $searchValue
1287 * Value of the column you want to search by.
1288 * @param string $returnColumn
1289 * Name of the column you want to GET the value of.
1290 * @param string $searchColumn
1291 * Name of the column you want to search by.
1292 * @param bool $force
1293 * Skip use of the cache.
1295 * @return string|int|null
1296 * Value of $returnColumn in the retrieved record
1298 * @throws \CRM_Core_Exception
1300 public static function getFieldValue($daoName, $searchValue, $returnColumn = 'name', $searchColumn = 'id', $force = FALSE) {
1302 empty($searchValue) ||
1303 trim(strtolower($searchValue)) == 'null'
1305 // adding this here since developers forget to check for an id
1306 // or for the 'null' (which is a bad DAO kludge)
1307 // and hence we get the first value in the db
1308 throw new CRM_Core_Exception('getFieldValue failed');
1311 self
::$_dbColumnValueCache = self
::$_dbColumnValueCache ??
[];
1313 while (strpos($daoName, '_BAO_') !== FALSE) {
1314 $daoName = get_parent_class($daoName);
1318 empty(self
::$_dbColumnValueCache[$daoName][$searchColumn][$searchValue]) ||
1319 !array_key_exists($returnColumn, self
::$_dbColumnValueCache[$daoName][$searchColumn][$searchValue])
1321 $object = new $daoName();
1322 $object->$searchColumn = $searchValue;
1323 $object->selectAdd();
1324 $object->selectAdd($returnColumn);
1327 if ($object->find(TRUE)) {
1328 $result = $object->$returnColumn;
1331 self
::$_dbColumnValueCache[$daoName][$searchColumn][$searchValue][$returnColumn] = $result;
1333 return self
::$_dbColumnValueCache[$daoName][$searchColumn][$searchValue][$returnColumn];
1337 * Given a DAO name, a column name and a column value, find the record and SET the value of another column in that record
1339 * @param string $daoName
1340 * Name of the DAO (Example: CRM_Contact_DAO_Contact to retrieve value from a contact).
1341 * @param int $searchValue
1342 * Value of the column you want to search by.
1343 * @param string $setColumn
1344 * Name of the column you want to SET the value of.
1345 * @param string $setValue
1346 * SET the setColumn to this value.
1347 * @param string $searchColumn
1348 * Name of the column you want to search by.
1351 * true if we found and updated the object, else false
1353 public static function setFieldValue($daoName, $searchValue, $setColumn, $setValue, $searchColumn = 'id') {
1354 $object = new $daoName();
1355 $object->selectAdd();
1356 $object->selectAdd("$searchColumn, $setColumn");
1357 $object->$searchColumn = $searchValue;
1359 if ($object->find(TRUE)) {
1360 $object->$setColumn = $setValue;
1361 if ($object->save()) {
1372 * @param array|object $sort either array or CRM_Utils_Sort
1373 * @param string $default
1374 * Default sort value.
1378 public static function getSortString($sort, $default = NULL) {
1379 // check if sort is of type CRM_Utils_Sort
1380 if (is_a($sort, 'CRM_Utils_Sort')) {
1381 return $sort->orderBy();
1386 // is it an array specified as $field => $sortDirection ?
1388 foreach ($sort as $k => $v) {
1389 $sortString .= "$k $v,";
1391 return rtrim($sortString, ',');
1397 * Fetch object based on array of properties.
1399 * @param string $daoName
1400 * Name of the dao object.
1401 * @param array $params
1402 * (reference ) an assoc array of name/value pairs.
1403 * @param array $defaults
1404 * (reference ) an assoc array to hold the flattened values.
1405 * @param array $returnProperities
1406 * An assoc array of fields that need to be returned, eg array( 'first_name', 'last_name').
1409 * an object of type referenced by daoName
1411 public static function commonRetrieve($daoName, &$params, &$defaults, $returnProperities = NULL) {
1412 $object = new $daoName();
1413 $object->copyValues($params);
1415 // return only specific fields if returnproperties are sent
1416 if (!empty($returnProperities)) {
1417 $object->selectAdd();
1418 $object->selectAdd(implode(',', $returnProperities));
1421 if ($object->find(TRUE)) {
1422 self
::storeValues($object, $defaults);
1429 * Delete the object records that are associated with this contact.
1431 * @param string $daoName
1432 * Name of the dao object.
1433 * @param int $contactId
1434 * Id of the contact to delete.
1436 public static function deleteEntityContact($daoName, $contactId) {
1437 $object = new $daoName();
1439 $object->entity_table
= 'civicrm_contact';
1440 $object->entity_id
= $contactId;
1445 * Execute an unbuffered query.
1447 * This is a wrapper around new functionality exposed with CRM-17748.
1449 * @param string $query query to be executed
1451 * @param array $params
1452 * @param bool $abort
1453 * @param null $daoName
1454 * @param bool $freeDAO
1455 * @param bool $i18nRewrite
1456 * @param bool $trapException
1458 * @return CRM_Core_DAO
1459 * Object that points to an unbuffered result set
1461 public static function executeUnbufferedQuery(
1467 $i18nRewrite = TRUE,
1468 $trapException = FALSE
1471 return self
::executeQuery(
1479 ['result_buffering' => 0]
1486 * @param string $query
1487 * Query to be executed.
1489 * @param array $params
1490 * @param bool $abort
1491 * @param null $daoName
1492 * @param bool $freeDAO
1493 * @param bool $i18nRewrite
1494 * @param bool $trapException
1495 * @param array $options
1497 * @return CRM_Core_DAO|object
1498 * object that holds the results of the query
1499 * NB - if this is defined as just returning a DAO phpstorm keeps pointing
1500 * out all the properties that are not part of the DAO
1502 public static function &executeQuery(
1508 $i18nRewrite = TRUE,
1509 $trapException = FALSE,
1512 $queryStr = self
::composeQuery($query, $params, $abort);
1515 $dao = new CRM_Core_DAO();
1518 $dao = new $daoName();
1521 if ($trapException) {
1522 $errorScope = CRM_Core_TemporaryErrorScope
::ignoreException();
1525 if ($dao->isValidOption($options)) {
1526 $dao->setOptions($options);
1529 $result = $dao->query($queryStr, $i18nRewrite);
1531 // since it is unbuffered, ($dao->N==0) is true. This blocks the standard fetch() mechanism.
1532 if (CRM_Utils_Array
::value('result_buffering', $options) === 0) {
1536 if (is_a($result, 'DB_Error')) {
1544 * Wrapper to validate internal DAO options before passing to DB_mysql/DB_Common level
1546 * @param array $options
1549 * Provided options are valid
1551 public function isValidOption($options) {
1560 if (empty($options)) {
1564 foreach (array_keys($options) as $option) {
1565 if (!in_array($option, $validOptions)) {
1575 * Execute a query and get the single result.
1577 * @param string $query
1578 * Query to be executed.
1579 * @param array $params
1580 * @param bool $abort
1581 * @param bool $i18nRewrite
1582 * @return string|null
1583 * the result of the query if any
1586 public static function &singleValueQuery(
1592 $queryStr = self
::composeQuery($query, $params, $abort);
1594 static $_dao = NULL;
1597 $_dao = new CRM_Core_DAO();
1600 $_dao->query($queryStr, $i18nRewrite);
1602 $result = $_dao->getDatabaseResult();
1605 $row = $result->fetchRow();
1615 * Compose the query by merging the parameters into it.
1617 * @param string $query
1618 * @param array $params
1619 * @param bool $abort
1622 * @throws CRM_Core_Exception
1624 public static function composeQuery($query, $params = [], $abort = TRUE) {
1626 foreach ($params as $key => $item) {
1627 if (is_numeric($key)) {
1628 if (CRM_Utils_Type
::validate($item[0], $item[1]) !== NULL) {
1629 $item[0] = self
::escapeString($item[0]);
1630 if ($item[1] == 'String' ||
1631 $item[1] == 'Memo' ||
1634 // Support class constants stipulating wildcard characters and/or
1635 // non-quoting of strings. Also support legacy code which may be
1636 // passing in TRUE or 1 for $item[2], which used to indicate the
1637 // use of wildcard characters.
1638 if (!empty($item[2])) {
1639 if ($item[2] & CRM_Core_DAO
::QUERY_FORMAT_WILDCARD ||
$item[2] === TRUE) {
1640 $item[0] = "'%{$item[0]}%'";
1642 elseif (!($item[2] & CRM_Core_DAO
::QUERY_FORMAT_NO_QUOTES
)) {
1643 $item[0] = "'{$item[0]}'";
1647 $item[0] = "'{$item[0]}'";
1651 if (($item[1] == 'Date' ||
$item[1] == 'Timestamp') &&
1652 strlen($item[0]) == 0
1657 $tr['%' . $key] = $item[0];
1660 throw new CRM_Core_Exception("{$item[0]} is not of type {$item[1]}");
1665 return strtr($query, $tr);
1671 public static function freeResult($ids = NULL) {
1672 global $_DB_DATAOBJECT;
1675 if (!$_DB_DATAOBJECT ||
1676 !isset($_DB_DATAOBJECT['RESULTS'])
1680 $ids = array_keys($_DB_DATAOBJECT['RESULTS']);
1683 foreach ($ids as $id) {
1684 if (isset($_DB_DATAOBJECT['RESULTS'][$id])) {
1685 $_DB_DATAOBJECT['RESULTS'][$id]->free();
1686 unset($_DB_DATAOBJECT['RESULTS'][$id]);
1689 if (isset($_DB_DATAOBJECT['RESULTFIELDS'][$id])) {
1690 unset($_DB_DATAOBJECT['RESULTFIELDS'][$id]);
1696 * Make a shallow copy of an object and all the fields in the object.
1698 * @param string $daoName
1700 * @param array $criteria
1701 * Array of all the fields & values.
1702 * on which basis to copy
1703 * @param array $newData
1704 * Array of all the fields & values.
1705 * to be copied besides the other fields
1706 * @param string $fieldsFix
1707 * Array of fields that you want to prefix/suffix/replace.
1708 * @param string $blockCopyOfDependencies
1709 * Fields that you want to block from.
1711 * @param bool $blockCopyofCustomValues
1712 * Case when you don't want to copy the custom values set in a
1713 * template as it will override/ignore the submitted custom values
1715 * @return CRM_Core_DAO|bool
1716 * the newly created copy of the object. False if none created.
1718 public static function copyGeneric($daoName, $criteria, $newData = NULL, $fieldsFix = NULL, $blockCopyOfDependencies = NULL, $blockCopyofCustomValues = FALSE) {
1719 $object = new $daoName();
1722 $object->id
= $criteria['id'];
1725 foreach ($criteria as $key => $value) {
1726 $object->$key = $value;
1731 while ($object->fetch()) {
1733 // all the objects except with $blockCopyOfDependencies set
1734 // be copied - addresses #CRM-1962
1736 if ($blockCopyOfDependencies && $object->$blockCopyOfDependencies) {
1740 $newObject = new $daoName();
1742 $fields = $object->fields();
1743 if (!is_array($fieldsFix)) {
1744 $fieldsToPrefix = [];
1745 $fieldsToSuffix = [];
1746 $fieldsToReplace = [];
1748 if (!empty($fieldsFix['prefix'])) {
1749 $fieldsToPrefix = $fieldsFix['prefix'];
1751 if (!empty($fieldsFix['suffix'])) {
1752 $fieldsToSuffix = $fieldsFix['suffix'];
1754 if (!empty($fieldsFix['replace'])) {
1755 $fieldsToReplace = $fieldsFix['replace'];
1758 foreach ($fields as $name => $value) {
1759 if ($name == 'id' ||
$value['name'] == 'id') {
1760 // copy everything but the id!
1764 $dbName = $value['name'];
1765 $type = CRM_Utils_Type
::typeToString($value['type']);
1766 $newObject->$dbName = $object->$dbName;
1767 if (isset($fieldsToPrefix[$dbName])) {
1768 $newObject->$dbName = $fieldsToPrefix[$dbName] . $newObject->$dbName;
1770 if (isset($fieldsToSuffix[$dbName])) {
1771 $newObject->$dbName .= $fieldsToSuffix[$dbName];
1773 if (isset($fieldsToReplace[$dbName])) {
1774 $newObject->$dbName = $fieldsToReplace[$dbName];
1777 if ($type == 'Timestamp' ||
$type == 'Date') {
1778 $newObject->$dbName = CRM_Utils_Date
::isoToMysql($newObject->$dbName);
1782 $newObject->copyValues($newData);
1786 if (!$blockCopyofCustomValues) {
1787 $newObject->copyCustomFields($object->id
, $newObject->id
);
1789 CRM_Utils_Hook
::post('create', CRM_Core_DAO_AllCoreTables
::getBriefName($daoName), $newObject->id
, $newObject);
1796 * Method that copies custom fields values from an old entity to a new one.
1798 * Fixes bug CRM-19302,
1799 * where if a custom field of File type was present, left both events using the same file,
1800 * breaking download URL's for the old event.
1802 * @todo the goal here is to clean this up so that it works for any entity. Copy Generic already DOES some custom field stuff
1803 * but it seems to be bypassed & perhaps less good than this (or this just duplicates it...)
1805 * @param int $entityID
1806 * @param int $newEntityID
1808 public function copyCustomFields($entityID, $newEntityID) {
1809 $entity = CRM_Core_DAO_AllCoreTables
::getBriefName(get_class($this));
1810 $tableName = CRM_Core_DAO_AllCoreTables
::getTableForClass(get_class($this));
1811 // Obtain custom values for old event
1812 $customParams = $htmlType = [];
1813 $customValues = CRM_Core_BAO_CustomValueTable
::getEntityValues($entityID, $entity);
1815 // If custom values present, we copy them
1816 if (!empty($customValues)) {
1817 // Get Field ID's and identify File type attributes, to handle file copying.
1818 $fieldIds = implode(', ', array_keys($customValues));
1819 $sql = "SELECT id FROM civicrm_custom_field WHERE html_type = 'File' AND id IN ( {$fieldIds} )";
1820 $result = CRM_Core_DAO
::executeQuery($sql);
1822 // Build array of File type fields
1823 while ($result->fetch()) {
1824 $htmlType[] = $result->id
;
1827 // Build params array of custom values
1828 foreach ($customValues as $field => $value) {
1829 if ($value !== NULL) {
1830 // Handle File type attributes
1831 if (in_array($field, $htmlType)) {
1832 $fileValues = CRM_Core_BAO_File
::path($value, $entityID);
1833 $customParams["custom_{$field}_-1"] = [
1834 'name' => CRM_Utils_File
::duplicate($fileValues[0]),
1835 'type' => $fileValues[1],
1838 // Handle other types
1840 $customParams["custom_{$field}_-1"] = $value;
1845 // Save Custom Fields for new Event
1846 CRM_Core_BAO_CustomValueTable
::postProcess($customParams, $tableName, $newEntityID, $entity);
1849 // copy activity attachments ( if any )
1850 CRM_Core_BAO_File
::copyEntityFile($tableName, $entityID, $tableName, $newEntityID);
1854 * Cascade update through related entities.
1856 * @param string $daoName
1859 * @param array $newData
1861 * @return CRM_Core_DAO|null
1863 public static function cascadeUpdate($daoName, $fromId, $toId, $newData = []) {
1864 $object = new $daoName();
1865 $object->id
= $fromId;
1867 if ($object->find(TRUE)) {
1868 $newObject = new $daoName();
1869 $newObject->id
= $toId;
1871 if ($newObject->find(TRUE)) {
1872 $fields = $object->fields();
1873 foreach ($fields as $name => $value) {
1874 if ($name == 'id' ||
$value['name'] == 'id') {
1875 // copy everything but the id!
1879 $colName = $value['name'];
1880 $newObject->$colName = $object->$colName;
1882 if (substr($name, -5) == '_date' ||
1883 substr($name, -10) == '_date_time'
1885 $newObject->$colName = CRM_Utils_Date
::isoToMysql($newObject->$colName);
1888 foreach ($newData as $k => $v) {
1889 $newObject->$k = $v;
1899 * Given the component id, compute the contact id
1900 * since its used for things like send email
1902 * @param $componentIDs
1903 * @param string $tableName
1904 * @param string $idField
1908 public static function getContactIDsFromComponent($componentIDs, $tableName, $idField = 'id') {
1911 if (empty($componentIDs)) {
1915 $IDs = implode(',', $componentIDs);
1919 WHERE $idField IN ( $IDs )
1922 $dao = CRM_Core_DAO
::executeQuery($query);
1923 while ($dao->fetch()) {
1924 $contactIDs[] = $dao->contact_id
;
1930 * Fetch object based on array of properties.
1932 * @param string $daoName
1933 * Name of the dao object.
1934 * @param string $fieldIdName
1935 * @param int $fieldId
1937 * @param array $returnProperities
1938 * An assoc array of fields that need to be returned, eg array( 'first_name', 'last_name').
1941 * an object of type referenced by daoName
1943 public static function commonRetrieveAll($daoName, $fieldIdName = 'id', $fieldId, &$details, $returnProperities = NULL) {
1944 require_once str_replace('_', DIRECTORY_SEPARATOR
, $daoName) . ".php";
1945 $object = new $daoName();
1946 $object->$fieldIdName = $fieldId;
1948 // return only specific fields if returnproperties are sent
1949 if (!empty($returnProperities)) {
1950 $object->selectAdd();
1951 $object->selectAdd('id');
1952 $object->selectAdd(implode(',', $returnProperities));
1956 while ($object->fetch()) {
1958 self
::storeValues($object, $defaults);
1959 $details[$object->id
] = $defaults;
1966 * Drop all CiviCRM tables.
1968 * @throws \CRM_Core_Exception
1970 public static function dropAllTables() {
1972 // first drop all the custom tables we've created
1973 CRM_Core_BAO_CustomGroup
::dropAllTables();
1975 // drop all multilingual views
1976 CRM_Core_I18n_Schema
::dropAllViews();
1978 CRM_Utils_File
::sourceSQLFile(CIVICRM_DSN
,
1979 dirname(__FILE__
) . DIRECTORY_SEPARATOR
.
1980 '..' . DIRECTORY_SEPARATOR
.
1981 '..' . DIRECTORY_SEPARATOR
.
1982 'sql' . DIRECTORY_SEPARATOR
.
1983 'civicrm_drop.mysql'
1992 public static function escapeString($string) {
1993 static $_dao = NULL;
1995 // If this is an atypical case (e.g. preparing .sql file before CiviCRM
1996 // has been installed), then we fallback DB-less str_replace escaping, as
1997 // we can't use mysqli_real_escape_string, as there is no DB connection.
1998 // Note: In typical usage, escapeString() will only check one conditional
1999 // ("if !$_dao") rather than two conditionals ("if !defined(DSN)")
2000 if (!defined('CIVICRM_DSN')) {
2001 // See http://php.net/manual/en/mysqli.real-escape-string.php for the
2002 // list of characters mysqli_real_escape_string escapes.
2003 $search = ["\\", "\x00", "\n", "\r", "'", '"', "\x1a"];
2004 $replace = ["\\\\", "\\0", "\\n", "\\r", "\'", '\"', "\\Z"];
2005 return str_replace($search, $replace, $string);
2007 $_dao = new CRM_Core_DAO();
2009 return $_dao->escape($string);
2013 * Escape a list of strings for use with "WHERE X IN (...)" queries.
2015 * @param array $strings
2016 * @param string $default
2017 * the value to use if $strings has no elements.
2019 * eg "abc","def","ghi"
2021 public static function escapeStrings($strings, $default = NULL) {
2022 static $_dao = NULL;
2024 $_dao = new CRM_Core_DAO();
2027 if (empty($strings)) {
2031 $escapes = array_map([$_dao, 'escape'], $strings);
2032 return '"' . implode('","', $escapes) . '"';
2040 public static function escapeWildCardString($string) {
2042 // ensure we escape the single characters % and _ which are mysql wild
2043 // card characters and could come in via sortByCharacter
2044 // note that mysql does not escape these characters
2045 if ($string && in_array($string,
2046 ['%', '_', '%%', '_%']
2049 return '\\' . $string;
2052 return self
::escapeString($string);
2056 * Creates a test object, including any required objects it needs via recursion
2057 * createOnly: only create in database, do not store or return the objects (useful for perf testing)
2058 * ONLY USE FOR TESTING
2060 * @param string $daoName
2061 * @param array $params
2062 * @param int $numObjects
2063 * @param bool $createOnly
2065 * @return object|array|NULL
2066 * NULL if $createOnly. A single object if $numObjects==1. Otherwise, an array of multiple objects.
2068 public static function createTestObject(
2074 //this is a test function also backtrace is set for the test suite it sometimes unsets itself
2075 // so we re-set here in case
2076 $config = CRM_Core_Config
::singleton();
2077 $config->backtrace
= TRUE;
2079 static $counter = 0;
2080 CRM_Core_DAO
::$_testEntitiesToSkip = [
2081 'CRM_Core_DAO_Worldregion',
2082 'CRM_Core_DAO_StateProvince',
2083 'CRM_Core_DAO_Country',
2084 'CRM_Core_DAO_Domain',
2085 'CRM_Financial_DAO_FinancialType',
2086 //because valid ones exist & we use pick them due to pseudoconstant can't reliably create & delete these
2089 // Prefer to instantiate BAO's instead of DAO's (when possible)
2090 // so that assignTestValue()/assignTestFK() can be overloaded.
2091 $baoName = str_replace('_DAO_', '_BAO_', $daoName);
2092 if ($baoName === 'CRM_Financial_BAO_FinancialTrxn') {
2093 // OMG OMG OMG this is so incredibly bad. The BAO is insanely named.
2094 // @todo create a new class called what the BAO SHOULD be
2095 // that extends BAO-crazy-name.... migrate.
2096 $baoName = 'CRM_Core_BAO_FinancialTrxn';
2098 if (class_exists($baoName)) {
2099 $daoName = $baoName;
2102 for ($i = 0; $i < $numObjects; ++
$i) {
2105 /** @var CRM_Core_DAO $object */
2106 $object = new $daoName();
2108 $fields = $object->fields();
2109 foreach ($fields as $fieldName => $fieldDef) {
2110 $dbName = $fieldDef['name'];
2111 $FKClassName = $fieldDef['FKClassName'] ??
NULL;
2113 if (isset($params[$dbName]) && !is_array($params[$dbName])) {
2114 $object->$dbName = $params[$dbName];
2117 elseif ($dbName != 'id') {
2118 if ($FKClassName != NULL) {
2119 $object->assignTestFK($fieldName, $fieldDef, $params);
2123 $object->assignTestValue($fieldName, $fieldDef, $counter);
2131 $objects[$i] = $object;
2141 elseif ($numObjects == 1) {
2150 * Deletes the this object plus any dependent objects that are associated with it.
2151 * ONLY USE FOR TESTING
2153 * @param string $daoName
2154 * @param array $params
2156 public static function deleteTestObjects($daoName, $params = []) {
2157 //this is a test function also backtrace is set for the test suite it sometimes unsets itself
2158 // so we re-set here in case
2159 $config = CRM_Core_Config
::singleton();
2160 $config->backtrace
= TRUE;
2162 $object = new $daoName();
2163 $object->id
= $params['id'] ??
NULL;
2165 // array(array(0 => $daoName, 1 => $daoParams))
2167 if ($object->find(TRUE)) {
2169 $fields = $object->fields();
2170 foreach ($fields as $name => $value) {
2172 $dbName = $value['name'];
2174 $FKClassName = $value['FKClassName'] ??
NULL;
2175 $required = $value['required'] ??
NULL;
2176 if ($FKClassName != NULL
2178 && !in_array($FKClassName, CRM_Core_DAO
::$_testEntitiesToSkip)
2179 && ($required ||
$dbName == 'contact_id')
2180 //I'm a bit stuck on this one - we might need to change the singleValueAlter so that the entities don't share a contact
2181 // to make this test process pass - line below makes pass for now
2182 && $dbName != 'member_of_contact_id'
2185 $deletions[] = [$FKClassName, ['id' => $object->$dbName]];
2192 foreach ($deletions as $deletion) {
2193 CRM_Core_DAO
::deleteTestObjects($deletion[0], $deletion[1]);
2198 * Set defaults when creating new entity.
2199 * (don't call this set defaults as already in use with different signature in some places)
2201 * @param array $params
2204 public static function setCreateDefaults(&$params, $defaults) {
2205 if (!empty($params['id'])) {
2208 foreach ($defaults as $key => $value) {
2209 if (!array_key_exists($key, $params) ||
$params[$key] === NULL) {
2210 $params[$key] = $value;
2216 * @param string $prefix
2217 * @param bool $addRandomString
2218 * @param null $string
2222 * @see CRM_Utils_SQL_TempTable
2224 public static function createTempTableName($prefix = 'civicrm', $addRandomString = TRUE, $string = NULL) {
2225 CRM_Core_Error
::deprecatedFunctionWarning('Use CRM_Utils_SQL_TempTable interface to create temporary tables');
2226 $tableName = $prefix . "_temp";
2228 if ($addRandomString) {
2230 $tableName .= "_" . $string;
2233 $tableName .= "_" . md5(uniqid('', TRUE));
2241 * @param bool $trigger
2245 public static function checkTriggerViewPermission($view = TRUE, $trigger = TRUE) {
2246 if (\Civi
::settings()->get('logging_no_trigger_permission')) {
2249 // test for create view and trigger permissions and if allowed, add the option to go multilingual
2251 // I'm not sure why we use the getStaticProperty for an error, rather than checking for DB_Error
2252 CRM_Core_TemporaryErrorScope
::ignoreException();
2253 $dao = new CRM_Core_DAO();
2255 $result = $dao->query('CREATE OR REPLACE VIEW civicrm_domain_view AS SELECT * FROM civicrm_domain');
2256 if (PEAR
::getStaticProperty('DB_DataObject', 'lastError') ||
is_a($result, 'DB_Error')) {
2262 $result = $dao->query('CREATE TRIGGER civicrm_domain_trigger BEFORE INSERT ON civicrm_domain FOR EACH ROW BEGIN END');
2263 if (PEAR
::getStaticProperty('DB_DataObject', 'lastError') ||
is_a($result, 'DB_Error')) {
2265 $dao->query('DROP VIEW IF EXISTS civicrm_domain_view');
2270 $dao->query('DROP TRIGGER IF EXISTS civicrm_domain_trigger');
2271 if (PEAR
::getStaticProperty('DB_DataObject', 'lastError')) {
2273 $dao->query('DROP VIEW IF EXISTS civicrm_domain_view');
2280 $dao->query('DROP VIEW IF EXISTS civicrm_domain_view');
2281 if (PEAR
::getStaticProperty('DB_DataObject', 'lastError')) {
2290 * @param null $message
2291 * @param bool $printDAO
2293 public static function debugPrint($message = NULL, $printDAO = TRUE) {
2294 CRM_Utils_System
::xMemory("{$message}: ");
2297 global $_DB_DATAOBJECT;
2299 foreach (array_keys($_DB_DATAOBJECT['RESULTS']) as $id) {
2300 $q[] = $_DB_DATAOBJECT['RESULTS'][$id]->query
;
2302 CRM_Core_Error
::debug('_DB_DATAOBJECT', $q);
2307 * Build a list of triggers via hook and add them to (err, reconcile them
2308 * with) the database.
2310 * @param string $tableName
2311 * the specific table requiring a rebuild; or NULL to rebuild all tables.
2312 * @param bool $force
2317 public static function triggerRebuild($tableName = NULL, $force = FALSE) {
2318 Civi
::service('sql_triggers')->rebuild($tableName, $force);
2322 * Because sql functions are sometimes lost, esp during db migration, we check here to avoid numerous support requests
2323 * @see http://issues.civicrm.org/jira/browse/CRM-13822
2324 * TODO: Alternative solutions might be
2325 * * Stop using functions and find another way to strip numeric characters from phones
2326 * * Give better error messages (currently a missing fn fatals with "unknown error")
2328 public static function checkSqlFunctionsExist() {
2329 if (!self
::$_checkedSqlFunctionsExist) {
2330 self
::$_checkedSqlFunctionsExist = TRUE;
2331 $dao = CRM_Core_DAO
::executeQuery("SHOW function status WHERE db = database() AND name = 'civicrm_strip_non_numeric'");
2332 if (!$dao->fetch()) {
2333 self
::triggerRebuild();
2339 * Wrapper function to drop triggers.
2341 * @param string $tableName
2342 * the specific table requiring a rebuild; or NULL to rebuild all tables.
2345 public static function dropTriggers($tableName = NULL) {
2346 Civi
::service('sql_triggers')->dropTriggers($tableName);
2350 * @param array $info
2351 * per hook_civicrm_triggerInfo.
2352 * @param string $onlyTableName
2353 * the specific table requiring a rebuild; or NULL to rebuild all tables.
2356 public static function createTriggers(&$info, $onlyTableName = NULL) {
2357 Civi
::service('sql_triggers')->createTriggers($info, $onlyTableName);
2361 * Given a list of fields, create a list of references.
2363 * @param string $className
2364 * BAO/DAO class name.
2365 * @return array<CRM_Core_Reference_Interface>
2367 public static function createReferenceColumns($className) {
2369 $fields = $className::fields();
2370 foreach ($fields as $field) {
2371 if (isset($field['pseudoconstant'], $field['pseudoconstant']['optionGroupName'])) {
2372 $result[] = new CRM_Core_Reference_OptionValue(
2373 $className::getTableName(),
2375 'civicrm_option_value',
2376 CRM_Utils_Array
::value('keyColumn', $field['pseudoconstant'], 'value'),
2377 $field['pseudoconstant']['optionGroupName']
2385 * Find all records which refer to this entity.
2388 * Array of objects referencing this
2390 public function findReferences() {
2391 $links = self
::getReferencesToTable(static::getTableName());
2394 foreach ($links as $refSpec) {
2395 /** @var $refSpec CRM_Core_Reference_Interface */
2396 $daoName = CRM_Core_DAO_AllCoreTables
::getClassForTable($refSpec->getReferenceTable());
2397 $result = $refSpec->findReferences($this);
2399 while ($result->fetch()) {
2400 $obj = new $daoName();
2401 $obj->id
= $result->id
;
2402 $occurrences[] = $obj;
2407 return $occurrences;
2412 * each item has keys:
2416 * - table: string|null SQL table name
2417 * - key: string|null SQL column name
2419 public function getReferenceCounts() {
2420 $links = self
::getReferencesToTable(static::getTableName());
2423 foreach ($links as $refSpec) {
2424 /** @var $refSpec CRM_Core_Reference_Interface */
2425 $count = $refSpec->getReferenceCount($this);
2426 if ($count['count'] != 0) {
2431 foreach (CRM_Core_Component
::getEnabledComponents() as $component) {
2432 /** @var $component CRM_Core_Component_Info */
2433 $counts = array_merge($counts, $component->getReferenceCounts($this));
2435 CRM_Utils_Hook
::referenceCounts($this, $counts);
2441 * List all tables which have hard foreign keys to this table.
2443 * For now, this returns a description of every entity_id/entity_table
2445 * TODO: filter dynamic entity references on the $tableName, based on
2446 * schema metadata in dynamicForeignKey which enumerates a restricted
2447 * set of possible entity_table's.
2449 * @param string $tableName
2450 * Table referred to.
2453 * structure of table and column, listing every table with a
2454 * foreign key reference to $tableName, and the column where the key appears.
2456 public static function getReferencesToTable($tableName) {
2458 foreach (CRM_Core_DAO_AllCoreTables
::getClasses() as $daoClassName) {
2459 $links = $daoClassName::getReferenceColumns();
2461 foreach ($links as $refSpec) {
2462 /** @var $refSpec CRM_Core_Reference_Interface */
2463 if ($refSpec->matchesTargetTable($tableName)) {
2464 $refsFound[] = $refSpec;
2472 * Get all references to contact table.
2474 * This includes core tables, custom group tables, tables added by the merge
2475 * hook and the entity_tag table.
2477 * Refer to CRM-17454 for information on the danger of querying the information
2478 * schema to derive this.
2480 * @throws \CiviCRM_API3_Exception
2482 public static function getReferencesToContactTable() {
2483 if (isset(\Civi
::$statics[__CLASS__
]) && isset(\Civi
::$statics[__CLASS__
]['contact_references'])) {
2484 return \Civi
::$statics[__CLASS__
]['contact_references'];
2486 $contactReferences = [];
2487 $coreReferences = CRM_Core_DAO
::getReferencesToTable('civicrm_contact');
2488 foreach ($coreReferences as $coreReference) {
2489 if (!is_a($coreReference, 'CRM_Core_Reference_Dynamic')) {
2490 $contactReferences[$coreReference->getReferenceTable()][] = $coreReference->getReferenceKey();
2493 self
::appendCustomTablesExtendingContacts($contactReferences);
2494 self
::appendCustomContactReferenceFields($contactReferences);
2495 \Civi
::$statics[__CLASS__
]['contact_references'] = $contactReferences;
2496 return \Civi
::$statics[__CLASS__
]['contact_references'];
2500 * Get all dynamic references to the given table.
2502 * @param string $tableName
2506 public static function getDynamicReferencesToTable($tableName) {
2507 if (!isset(\Civi
::$statics[__CLASS__
]['contact_references_dynamic'][$tableName])) {
2508 \Civi
::$statics[__CLASS__
]['contact_references_dynamic'][$tableName] = [];
2509 $coreReferences = CRM_Core_DAO
::getReferencesToTable($tableName);
2510 foreach ($coreReferences as $coreReference) {
2511 if ($coreReference instanceof \CRM_Core_Reference_Dynamic
) {
2512 \Civi
::$statics[__CLASS__
]['contact_references_dynamic'][$tableName][$coreReference->getReferenceTable()][] = [$coreReference->getReferenceKey(), $coreReference->getTypeColumn()];
2516 return \Civi
::$statics[__CLASS__
]['contact_references_dynamic'][$tableName];
2520 * Add custom tables that extend contacts to the list of contact references.
2522 * CRM_Core_BAO_CustomGroup::getAllCustomGroupsByBaseEntity seems like a safe-ish
2523 * function to be sure all are retrieved & we don't miss subtypes or inactive or multiples
2524 * - the down side is it is not cached.
2526 * Further changes should be include tests in the CRM_Core_MergerTest class
2527 * to ensure that disabled, subtype, multiple etc groups are still captured.
2529 * @param array $cidRefs
2531 public static function appendCustomTablesExtendingContacts(&$cidRefs) {
2532 $customValueTables = CRM_Core_BAO_CustomGroup
::getAllCustomGroupsByBaseEntity('Contact');
2533 $customValueTables->find();
2534 while ($customValueTables->fetch()) {
2535 $cidRefs[$customValueTables->table_name
][] = 'entity_id';
2540 * Add custom ContactReference fields to the list of contact references
2542 * This includes active and inactive fields/groups
2544 * @param array $cidRefs
2546 * @throws \CiviCRM_API3_Exception
2548 public static function appendCustomContactReferenceFields(&$cidRefs) {
2549 $fields = civicrm_api3('CustomField', 'get', [
2550 'return' => ['column_name', 'custom_group_id.table_name'],
2551 'data_type' => 'ContactReference',
2552 'options' => ['limit' => 0],
2554 foreach ($fields as $field) {
2555 $cidRefs[$field['custom_group_id.table_name']][] = $field['column_name'];
2560 * Lookup the value of a MySQL global configuration variable.
2562 * @param string $name
2563 * E.g. "thread_stack".
2564 * @param mixed $default
2567 public static function getGlobalSetting($name, $default = NULL) {
2568 // Alternatively, SELECT @@GLOBAL.thread_stack, but
2569 // that has been reported to fail under MySQL 5.0 for OS X
2570 $escapedName = self
::escapeString($name);
2571 $dao = CRM_Core_DAO
::executeQuery("SHOW VARIABLES LIKE '$escapedName'");
2572 if ($dao->fetch()) {
2581 * Update the fields array to also hold keys for pseudoconstant fields that relate to contained fields.
2583 * This is relevant where we want to offer both the ID field and the label field
2584 * as an option, e.g. search builder.
2586 * It is currently limited for optionGroupName & id+ name+ FK combos for purposes keeping the scope of the
2587 * change small, but is appropriate for other sorts of pseudoconstants.
2589 * @param array $fields
2591 public static function appendPseudoConstantsToFields(&$fields) {
2592 foreach ($fields as $fieldUniqueName => $field) {
2593 if (!empty($field['pseudoconstant'])) {
2594 $pseudoConstant = $field['pseudoconstant'];
2595 if (!empty($pseudoConstant['optionGroupName'])) {
2596 $fields[$pseudoConstant['optionGroupName']] = [
2597 'title' => CRM_Core_BAO_OptionGroup
::getTitleByName($pseudoConstant['optionGroupName']),
2598 'name' => $pseudoConstant['optionGroupName'],
2599 'data_type' => CRM_Utils_Type
::T_STRING
,
2600 'is_pseudofield_for' => $fieldUniqueName,
2603 // We restrict to id + name + FK as we are extending this a bit, but cautiously.
2605 !empty($field['FKClassName'])
2606 && CRM_Utils_Array
::value('keyColumn', $pseudoConstant) === 'id'
2607 && CRM_Utils_Array
::value('labelColumn', $pseudoConstant) === 'name'
2609 $pseudoFieldName = str_replace('_' . $pseudoConstant['keyColumn'], '', $field['name']);
2610 // This if is just an extra caution when adding change.
2611 if (!isset($fields[$pseudoFieldName])) {
2612 $daoName = $field['FKClassName'];
2613 $fkFields = $daoName::fields();
2614 foreach ($fkFields as $fkField) {
2615 if ($fkField['name'] === $pseudoConstant['labelColumn']) {
2616 $fields[$pseudoFieldName] = [
2617 'name' => $pseudoFieldName,
2618 'is_pseudofield_for' => $field['name'],
2619 'title' => $fkField['title'],
2620 'data_type' => $fkField['type'],
2621 'where' => $field['where'],
2632 * Get options for the called BAO object's field.
2634 * This function can be overridden by each BAO to add more logic related to context.
2635 * The overriding function will generally call the lower-level CRM_Core_PseudoConstant::get
2637 * @param string $fieldName
2638 * @param string $context
2639 * @see CRM_Core_DAO::buildOptionsContext
2640 * @param array $props
2641 * Raw field values; whatever is known about this bao object.
2643 * Note: $props can contain unsanitized input and should not be passed directly to CRM_Core_PseudoConstant::get
2645 * @return array|bool
2647 public static function buildOptions($fieldName, $context = NULL, $props = []) {
2648 // If a given bao does not override this function
2649 $baoName = get_called_class();
2650 return CRM_Core_PseudoConstant
::get($baoName, $fieldName, [], $context);
2654 * Populate option labels for this object's fields.
2656 * @throws exception if called directly on the base class
2658 public function getOptionLabels() {
2659 $fields = $this->fields();
2660 if ($fields === NULL) {
2661 throw new Exception('Cannot call getOptionLabels on CRM_Core_DAO');
2663 foreach ($fields as $field) {
2664 $name = $field['name'] ??
NULL;
2665 if ($name && isset($this->$name)) {
2666 $label = CRM_Core_PseudoConstant
::getLabel(get_class($this), $name, $this->$name);
2667 if ($label !== FALSE) {
2668 // Append 'label' onto the field name
2669 $labelName = $name . '_label';
2670 $this->$labelName = $label;
2677 * Provides documentation and validation for the buildOptions $context param
2679 * @param string $context
2681 * @throws CRM_Core_Exception
2684 public static function buildOptionsContext($context = NULL) {
2686 'get' => "get: all options are returned, even if they are disabled; labels are translated.",
2687 'create' => "create: options are filtered appropriately for the object being created/updated; labels are translated.",
2688 'search' => "search: searchable options are returned; labels are translated.",
2689 'validate' => "validate: all options are returned, even if they are disabled; machine names are used in place of labels.",
2690 'abbreviate' => "abbreviate: enabled options are returned; labels are replaced with abbreviations.",
2691 'match' => "match: enabled options are returned using machine names as keys; labels are translated.",
2693 // Validation: enforce uniformity of this param
2694 if ($context !== NULL && !isset($contexts[$context])) {
2695 throw new CRM_Core_Exception("'$context' is not a valid context for buildOptions.");
2701 * @param string $fieldName
2702 * @return bool|array
2704 public function getFieldSpec($fieldName) {
2705 $fields = $this->fields();
2706 $fieldKeys = $this->fieldKeys();
2708 // Support "unique names" as well as sql names
2709 $fieldKey = $fieldName;
2710 if (empty($fields[$fieldKey])) {
2711 $fieldKey = $fieldKeys[$fieldName] ??
NULL;
2713 // If neither worked then this field doesn't exist. Return false.
2714 if (empty($fields[$fieldKey])) {
2717 return $fields[$fieldKey];
2721 * Get SQL where clause for SQL filter syntax input parameters.
2723 * SQL version of api function to assign filters to the DAO based on the syntax
2724 * $field => array('IN' => array(4,6,9))
2726 * $field => array('LIKE' => array('%me%))
2729 * @param string $fieldName
2731 * @param array $filter
2732 * filter to be applied indexed by operator.
2733 * @param string $type
2734 * type of field (not actually used - nor in api @todo ).
2735 * @param string $alias
2736 * alternative field name ('as') @todo- not actually used.
2737 * @param bool $returnSanitisedArray
2738 * Return a sanitised array instead of a clause.
2739 * this is primarily so we can add filters @ the api level to the Query object based fields
2743 * @return NULL|string|array
2744 * a string is returned if $returnSanitisedArray is not set, otherwise and Array or NULL
2745 * depending on whether it is supported as yet
2747 public static function createSQLFilter($fieldName, $filter, $type = NULL, $alias = NULL, $returnSanitisedArray = FALSE) {
2748 foreach ($filter as $operator => $criteria) {
2749 if (in_array($operator, self
::acceptedSQLOperators(), TRUE)) {
2750 switch ($operator) {
2754 if (!$returnSanitisedArray) {
2755 return (sprintf('%s %s', $fieldName, $operator));
2758 return (sprintf('%s %s ', $fieldName, $operator));
2762 // ternary operators
2765 if ((empty($criteria[0]) && !in_array($criteria[0], ['0', 0]))||
(empty($criteria[1]) && !in_array($criteria[1], ['0', 0]))) {
2766 throw new Exception("invalid criteria for $operator");
2768 if (!$returnSanitisedArray) {
2769 return (sprintf('%s ' . $operator . ' "%s" AND "%s"', $fieldName, CRM_Core_DAO
::escapeString($criteria[0]), CRM_Core_DAO
::escapeString($criteria[1])));
2772 // not yet implemented (tests required to implement)
2780 if (empty($criteria)) {
2781 throw new Exception("invalid criteria for $operator");
2783 $escapedCriteria = array_map([
2787 if (!$returnSanitisedArray) {
2788 return (sprintf('%s %s ("%s")', $fieldName, $operator, implode('", "', $escapedCriteria)));
2790 return $escapedCriteria;
2795 if (!$returnSanitisedArray) {
2796 return (sprintf('%s %s "%s"', $fieldName, $operator, CRM_Core_DAO
::escapeString($criteria)));
2799 // not yet implemented (tests required to implement)
2808 * @see http://issues.civicrm.org/jira/browse/CRM-9150
2809 * support for other syntaxes is discussed in ticket but being put off for now
2812 public static function acceptedSQLOperators() {
2833 * SQL has a limit of 64 characters on various names:
2834 * table name, trigger name, column name ...
2836 * For custom groups and fields we generated names from user entered input
2837 * which can be longer than this length, this function helps with creating
2838 * strings that meet various criteria.
2840 * @param string $string
2841 * The string to be shortened.
2842 * @param int $length
2843 * The max length of the string.
2845 * @param bool $makeRandom
2849 public static function shortenSQLName($string, $length = 60, $makeRandom = FALSE) {
2850 // early return for strings that meet the requirements
2851 if (strlen($string) <= $length) {
2855 // easy return for calls that dont need a randomized uniq string
2857 return substr($string, 0, $length);
2860 // the string is longer than the length and we need a uniq string
2861 // for the same tablename we need the same uniq string every time
2862 // hence we use md5 on the string, which is not random
2863 // we'll append 8 characters to the end of the tableName
2864 $md5string = substr(md5($string), 0, 8);
2865 return substr($string, 0, $length - 8) . "_{$md5string}";
2869 * https://issues.civicrm.org/jira/browse/CRM-17748
2870 * Sets the internal options to be used on a query
2872 * @param array $options
2875 public function setOptions($options) {
2876 if (is_array($options)) {
2877 $this->_options
= $options;
2882 * https://issues.civicrm.org/jira/browse/CRM-17748
2883 * wrapper to pass internal DAO options down to DB_mysql/DB_Common level
2885 * @param array $options
2888 protected function _setDBOptions($options) {
2889 global $_DB_DATAOBJECT;
2891 if (is_array($options) && count($options)) {
2892 $conn = &$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5
];
2893 foreach ($options as $option_name => $option_value) {
2894 $conn->setOption($option_name, $option_value);
2901 * @param array $params
2903 public function setApiFilter(&$params) {
2907 * Generates acl clauses suitable for adding to WHERE or ON when doing an api.get for this entity
2909 * Return format is in the form of fieldname => clauses starting with an operator. e.g.:
2912 * 'location_type_id' => array('IS NOT NULL', 'IN (1,2,3)')
2916 * Note that all array keys must be actual field names in this entity. Use subqueries to filter on other tables e.g. custom values.
2920 public function addSelectWhereClause() {
2922 $fields = $this->fields();
2923 foreach ($fields as $fieldName => $field) {
2924 // Clause for contact-related entities like Email, Relationship, etc.
2925 if (strpos($fieldName, 'contact_id') === 0 && CRM_Utils_Array
::value('FKClassName', $field) == 'CRM_Contact_DAO_Contact') {
2926 $clauses[$fieldName] = CRM_Utils_SQL
::mergeSubquery('Contact');
2928 // Clause for an entity_table/entity_id combo
2929 if ($fieldName == 'entity_id' && isset($fields['entity_table'])) {
2930 $relatedClauses = [];
2931 $relatedEntities = $this->buildOptions('entity_table', 'get');
2932 foreach ((array) $relatedEntities as $table => $ent) {
2934 $ent = CRM_Core_DAO_AllCoreTables
::getBriefName(CRM_Core_DAO_AllCoreTables
::getClassForTable($table));
2935 $subquery = CRM_Utils_SQL
::mergeSubquery($ent);
2937 $relatedClauses[] = "(entity_table = '$table' AND entity_id " . implode(' AND entity_id ', $subquery) . ")";
2940 $relatedClauses[] = "(entity_table = '$table')";
2944 if ($relatedClauses) {
2945 $clauses['id'] = 'IN (SELECT id FROM `' . $this->tableName() . '` WHERE (' . implode(') OR (', $relatedClauses) . '))';
2949 CRM_Utils_Hook
::selectWhereClause($this, $clauses);
2954 * This returns the final permissioned query string for this entity
2956 * With acls from related entities + additional clauses from hook_civicrm_selectWhereClause
2958 * @param string $tableAlias
2961 public static function getSelectWhereClause($tableAlias = NULL) {
2962 $bao = new static();
2963 if ($tableAlias === NULL) {
2964 $tableAlias = $bao->tableName();
2967 foreach ((array) $bao->addSelectWhereClause() as $field => $vals) {
2968 $clauses[$field] = NULL;
2970 $clauses[$field] = "(`$tableAlias`.`$field` IS NULL OR (`$tableAlias`.`$field` " . implode(" AND `$tableAlias`.`$field` ", (array) $vals) . '))';
2977 * ensure database name is 'safe', i.e. only contains word characters (includes underscores)
2978 * and dashes, and contains at least one [a-z] case insenstive.
2984 public static function requireSafeDBName($database) {
2987 "/^[\w\-]*[a-z]+[\w\-]*$/i",
2991 if (empty($matches)) {
2998 * Transform an array to a serialized string for database storage.
3000 * @param array|null $value
3001 * @param int $serializationType
3002 * @return string|null
3004 * @throws \Exception
3006 public static function serializeField($value, $serializationType) {
3007 if ($value === NULL) {
3010 switch ($serializationType) {
3011 case self
::SERIALIZE_SEPARATOR_BOOKEND
:
3012 return $value === [] ?
'' : CRM_Utils_Array
::implodePadded($value);
3014 case self
::SERIALIZE_SEPARATOR_TRIMMED
:
3015 return is_array($value) ?
implode(self
::VALUE_SEPARATOR
, $value) : $value;
3017 case self
::SERIALIZE_JSON
:
3018 return is_array($value) ?
json_encode($value) : $value;
3020 case self
::SERIALIZE_PHP
:
3021 return is_array($value) ?
serialize($value) : $value;
3023 case self
::SERIALIZE_COMMA
:
3024 return is_array($value) ?
implode(',', $value) : $value;
3027 throw new Exception('Unknown serialization method for field.');
3032 * Transform a serialized string from the database into an array.
3034 * @param string|null $value
3035 * @param $serializationType
3037 * @return array|null
3038 * @throws CRM_Core_Exception
3040 public static function unSerializeField($value, $serializationType) {
3041 if ($value === NULL) {
3044 if ($value === '') {
3047 switch ($serializationType) {
3048 case self
::SERIALIZE_SEPARATOR_BOOKEND
:
3049 return (array) CRM_Utils_Array
::explodePadded($value);
3051 case self
::SERIALIZE_SEPARATOR_TRIMMED
:
3052 return explode(self
::VALUE_SEPARATOR
, trim($value));
3054 case self
::SERIALIZE_JSON
:
3055 return strlen($value) ?
json_decode($value, TRUE) : [];
3057 case self
::SERIALIZE_PHP
:
3058 return strlen($value) ? CRM_Utils_String
::unserialize($value) : [];
3060 case self
::SERIALIZE_COMMA
:
3061 return explode(',', trim(str_replace(', ', '', $value)));
3064 throw new CRM_Core_Exception('Unknown serialization method for field.');
3071 public static function getEntityRefFilters() {
3076 * Get exportable fields with pseudoconstants rendered as an extra field.
3078 * @param string $baoClass
3082 public static function getExportableFieldsWithPseudoConstants($baoClass) {
3083 if (method_exists($baoClass, 'exportableFields')) {
3084 $fields = $baoClass::exportableFields();
3087 $fields = $baoClass::export();
3089 CRM_Core_DAO
::appendPseudoConstantsToFields($fields);
3094 * Remove item from static cache during update/delete operations
3096 private function clearDbColumnValueCache() {
3097 $daoName = get_class($this);
3098 while (strpos($daoName, '_BAO_') !== FALSE) {
3099 $daoName = get_parent_class($daoName);
3101 if (isset($this->id
)) {
3102 unset(self
::$_dbColumnValueCache[$daoName]['id'][$this->id
]);
3104 if (isset($this->name
)) {
3105 unset(self
::$_dbColumnValueCache[$daoName]['name'][$this->name
]);