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