Merge pull request #12748 from compucorp/370-no-new-tasks-and-documents-get-added...
[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 $this->$dbName = $pValue;
700 $allNull = FALSE;
701 }
702 }
703 }
704 return $allNull;
705 }
706
707 /**
708 * Store all the values from this object in an associative array
709 * this is a destructive store, calling function is responsible
710 * for keeping sanity of id's.
711 *
712 * @param object $object
713 * The object that we are extracting data from.
714 * @param array $values
715 * (reference ) associative array of name/value pairs.
716 */
717 public static function storeValues(&$object, &$values) {
718 $fields = $object->fields();
719 foreach ($fields as $name => $value) {
720 $dbName = $value['name'];
721 if (isset($object->$dbName) && $object->$dbName !== 'null') {
722 $values[$dbName] = $object->$dbName;
723 if ($name != $dbName) {
724 $values[$name] = $object->$dbName;
725 }
726 }
727 }
728 }
729
730 /**
731 * Create an attribute for this specific field. We only do this for strings and text
732 *
733 * @param array $field
734 * The field under task.
735 *
736 * @return array|null
737 * the attributes for the object
738 */
739 public static function makeAttribute($field) {
740 if ($field) {
741 if (CRM_Utils_Array::value('type', $field) == CRM_Utils_Type::T_STRING) {
742 $maxLength = CRM_Utils_Array::value('maxlength', $field);
743 $size = CRM_Utils_Array::value('size', $field);
744 if ($maxLength || $size) {
745 $attributes = array();
746 if ($maxLength) {
747 $attributes['maxlength'] = $maxLength;
748 }
749 if ($size) {
750 $attributes['size'] = $size;
751 }
752 return $attributes;
753 }
754 }
755 elseif (CRM_Utils_Array::value('type', $field) == CRM_Utils_Type::T_TEXT) {
756 $rows = CRM_Utils_Array::value('rows', $field);
757 if (!isset($rows)) {
758 $rows = 2;
759 }
760 $cols = CRM_Utils_Array::value('cols', $field);
761 if (!isset($cols)) {
762 $cols = 80;
763 }
764
765 $attributes = array();
766 $attributes['rows'] = $rows;
767 $attributes['cols'] = $cols;
768 return $attributes;
769 }
770 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) {
771 $attributes['size'] = 6;
772 $attributes['maxlength'] = 14;
773 return $attributes;
774 }
775 }
776 return NULL;
777 }
778
779 /**
780 * Get the size and maxLength attributes for this text field.
781 * (or for all text fields) in the DAO object.
782 *
783 * @param string $class
784 * Name of DAO class.
785 * @param string $fieldName
786 * Field that i'm interested in or null if.
787 * you want the attributes for all DAO text fields
788 *
789 * @return array
790 * assoc array of name => attribute pairs
791 */
792 public static function getAttribute($class, $fieldName = NULL) {
793 $object = new $class();
794 $fields = $object->fields();
795 if ($fieldName != NULL) {
796 $field = CRM_Utils_Array::value($fieldName, $fields);
797 return self::makeAttribute($field);
798 }
799 else {
800 $attributes = array();
801 foreach ($fields as $name => $field) {
802 $attribute = self::makeAttribute($field);
803 if ($attribute) {
804 $attributes[$name] = $attribute;
805 }
806 }
807
808 if (!empty($attributes)) {
809 return $attributes;
810 }
811 }
812 return NULL;
813 }
814
815 /**
816 * Check if there is a record with the same name in the db.
817 *
818 * @param string $value
819 * The value of the field we are checking.
820 * @param string $daoName
821 * The dao object name.
822 * @param string $daoID
823 * The id of the object being updated. u can change your name.
824 * as long as there is no conflict
825 * @param string $fieldName
826 * The name of the field in the DAO.
827 *
828 * @param string $domainID
829 * The id of the domain. Object exists only for the given domain.
830 *
831 * @return bool
832 * true if object exists
833 */
834 public static function objectExists($value, $daoName, $daoID, $fieldName = 'name', $domainID = NULL) {
835 $object = new $daoName();
836 $object->$fieldName = $value;
837 if ($domainID) {
838 $object->domain_id = $domainID;
839 }
840
841 if ($object->find(TRUE)) {
842 return ($daoID && $object->id == $daoID) ? TRUE : FALSE;
843 }
844 else {
845 return TRUE;
846 }
847 }
848
849 /**
850 * Check if there is a given column in a specific table.
851 *
852 * @deprecated
853 * @see CRM_Core_BAO_SchemaHandler::checkIfFieldExists
854 *
855 * @param string $tableName
856 * @param string $columnName
857 * @param bool $i18nRewrite
858 * Whether to rewrite the query on multilingual setups.
859 *
860 * @return bool
861 * true if exists, else false
862 */
863 public static function checkFieldExists($tableName, $columnName, $i18nRewrite = TRUE) {
864 return CRM_Core_BAO_SchemaHandler::checkIfFieldExists($tableName, $columnName, $i18nRewrite);
865 }
866
867 /**
868 * Scans all the tables using a slow query and table name.
869 *
870 * @return array
871 */
872 public static function getTableNames() {
873 $dao = CRM_Core_DAO::executeQuery(
874 "SELECT TABLE_NAME
875 FROM information_schema.TABLES
876 WHERE TABLE_SCHEMA = '" . CRM_Core_DAO::getDatabaseName() . "'
877 AND TABLE_NAME LIKE 'civicrm_%'
878 AND TABLE_NAME NOT LIKE 'civicrm_import_job_%'
879 AND TABLE_NAME NOT LIKE '%_temp%'
880 ");
881
882 while ($dao->fetch()) {
883 $values[] = $dao->TABLE_NAME;
884 }
885 $dao->free();
886 return $values;
887 }
888
889 /**
890 * @param int $maxTablesToCheck
891 *
892 * @return bool
893 */
894 public static function isDBMyISAM($maxTablesToCheck = 10) {
895 return CRM_Core_DAO::singleValueQuery(
896 "SELECT count(*)
897 FROM information_schema.TABLES
898 WHERE ENGINE = 'MyISAM'
899 AND TABLE_SCHEMA = '" . CRM_Core_DAO::getDatabaseName() . "'
900 AND TABLE_NAME LIKE 'civicrm_%'
901 AND TABLE_NAME NOT LIKE 'civicrm_import_job_%'
902 AND TABLE_NAME NOT LIKE '%_temp%'
903 ");
904 }
905
906 /**
907 * Get the name of the CiviCRM database.
908 *
909 * @return string
910 */
911 public static function getDatabaseName() {
912 $daoObj = new CRM_Core_DAO();
913 return $daoObj->database();
914 }
915
916 /**
917 * Checks if a constraint exists for a specified table.
918 *
919 * @param string $tableName
920 * @param string $constraint
921 *
922 * @return bool
923 * true if constraint exists, false otherwise
924 */
925 public static function checkConstraintExists($tableName, $constraint) {
926 static $show = array();
927
928 if (!array_key_exists($tableName, $show)) {
929 $query = "SHOW CREATE TABLE $tableName";
930 $dao = CRM_Core_DAO::executeQuery($query);
931
932 if (!$dao->fetch()) {
933 CRM_Core_Error::fatal();
934 }
935
936 $dao->free();
937 $show[$tableName] = $dao->Create_Table;
938 }
939
940 return preg_match("/\b$constraint\b/i", $show[$tableName]) ? TRUE : FALSE;
941 }
942
943 /**
944 * Checks if CONSTRAINT keyword exists for a specified table.
945 *
946 * @param array $tables
947 *
948 * @throws Exception
949 *
950 * @return bool
951 * true if CONSTRAINT keyword exists, false otherwise
952 */
953 public static function schemaRequiresRebuilding($tables = array("civicrm_contact")) {
954 $show = array();
955 foreach ($tables as $tableName) {
956 if (!array_key_exists($tableName, $show)) {
957 $query = "SHOW CREATE TABLE $tableName";
958 $dao = CRM_Core_DAO::executeQuery($query);
959
960 if (!$dao->fetch()) {
961 CRM_Core_Error::fatal();
962 }
963
964 $dao->free();
965 $show[$tableName] = $dao->Create_Table;
966 }
967
968 $result = preg_match("/\bCONSTRAINT\b\s/i", $show[$tableName]) ? TRUE : FALSE;
969 if ($result == TRUE) {
970 continue;
971 }
972 else {
973 return FALSE;
974 }
975 }
976 return TRUE;
977 }
978
979 /**
980 * Checks if the FK constraint name is in the format 'FK_tableName_columnName'
981 * for a specified column of a table.
982 *
983 * @param string $tableName
984 * @param string $columnName
985 *
986 * @return bool
987 * true if in format, false otherwise
988 */
989 public static function checkFKConstraintInFormat($tableName, $columnName) {
990 static $show = array();
991
992 if (!array_key_exists($tableName, $show)) {
993 $query = "SHOW CREATE TABLE $tableName";
994 $dao = CRM_Core_DAO::executeQuery($query);
995
996 if (!$dao->fetch()) {
997 CRM_Core_Error::fatal();
998 }
999
1000 $dao->free();
1001 $show[$tableName] = $dao->Create_Table;
1002 }
1003 $constraint = "`FK_{$tableName}_{$columnName}`";
1004 $pattern = "/\bCONSTRAINT\b\s+%s\s+\bFOREIGN\s+KEY\b\s/i";
1005 return preg_match(sprintf($pattern, $constraint), $show[$tableName]) ? TRUE : FALSE;
1006 }
1007
1008 /**
1009 * Check whether a specific column in a specific table has always the same value.
1010 *
1011 * @param string $tableName
1012 * @param string $columnName
1013 * @param string $columnValue
1014 *
1015 * @return bool
1016 * true if the value is always $columnValue, false otherwise
1017 */
1018 public static function checkFieldHasAlwaysValue($tableName, $columnName, $columnValue) {
1019 $query = "SELECT * FROM $tableName WHERE $columnName != '$columnValue'";
1020 $dao = CRM_Core_DAO::executeQuery($query);
1021 $result = $dao->fetch() ? FALSE : TRUE;
1022 $dao->free();
1023 return $result;
1024 }
1025
1026 /**
1027 * Check whether a specific column in a specific table is always NULL.
1028 *
1029 * @param string $tableName
1030 * @param string $columnName
1031 *
1032 * @return bool
1033 * true if if the value is always NULL, false otherwise
1034 */
1035 public static function checkFieldIsAlwaysNull($tableName, $columnName) {
1036 $query = "SELECT * FROM $tableName WHERE $columnName IS NOT NULL";
1037 $dao = CRM_Core_DAO::executeQuery($query);
1038 $result = $dao->fetch() ? FALSE : TRUE;
1039 $dao->free();
1040 return $result;
1041 }
1042
1043 /**
1044 * Check if there is a given table in the database.
1045 *
1046 * @param string $tableName
1047 *
1048 * @return bool
1049 * true if exists, else false
1050 */
1051 public static function checkTableExists($tableName) {
1052 $query = "
1053 SHOW TABLES
1054 LIKE %1
1055 ";
1056 $params = array(1 => array($tableName, 'String'));
1057
1058 $dao = CRM_Core_DAO::executeQuery($query, $params);
1059 $result = $dao->fetch() ? TRUE : FALSE;
1060 $dao->free();
1061 return $result;
1062 }
1063
1064 /**
1065 * @param $version
1066 *
1067 * @return bool
1068 */
1069 public function checkVersion($version) {
1070 $query = "
1071 SELECT version
1072 FROM civicrm_domain
1073 ";
1074 $dbVersion = CRM_Core_DAO::singleValueQuery($query);
1075 return trim($version) == trim($dbVersion) ? TRUE : FALSE;
1076 }
1077
1078 /**
1079 * Find a DAO object for the given ID and return it.
1080 *
1081 * @param int $id
1082 * Id of the DAO object being searched for.
1083 *
1084 * @return CRM_Core_DAO
1085 * Object of the type of the class that called this function.
1086 *
1087 * @throws Exception
1088 */
1089 public static function findById($id) {
1090 $object = new static();
1091 $object->id = $id;
1092 if (!$object->find(TRUE)) {
1093 throw new Exception("Unable to find a " . get_called_class() . " with id {$id}.");
1094 }
1095 return $object;
1096 }
1097
1098 /**
1099 * Returns all results as array-encoded records.
1100 *
1101 * @return array
1102 */
1103 public function fetchAll() {
1104 $result = array();
1105 while ($this->fetch()) {
1106 $result[] = $this->toArray();
1107 }
1108 return $result;
1109 }
1110
1111 /**
1112 * Return the results as PHP generator.
1113 *
1114 * @param string $type
1115 * Whether the generator yields 'dao' objects or 'array's.
1116 */
1117 public function fetchGenerator($type = 'dao') {
1118 while ($this->fetch()) {
1119 switch ($type) {
1120 case 'dao':
1121 yield $this;
1122 break;
1123
1124 case 'array':
1125 yield $this->toArray();
1126 break;
1127
1128 default:
1129 throw new \RuntimeException("Invalid record type ($type)");
1130 }
1131 }
1132 }
1133
1134 /**
1135 * Returns a singular value.
1136 *
1137 * @return mixed|NULL
1138 */
1139 public function fetchValue() {
1140 $result = $this->getDatabaseResult();
1141 $row = $result->fetchRow();
1142 $ret = NULL;
1143 if ($row) {
1144 $ret = $row[0];
1145 }
1146 $this->free();
1147 return $ret;
1148 }
1149
1150 /**
1151 * Get all the result records as mapping between columns.
1152 *
1153 * @param string $keyColumn
1154 * Ex: "name"
1155 * @param string $valueColumn
1156 * Ex: "label"
1157 * @return array
1158 * Ex: ["foo" => "The Foo Bar", "baz" => "The Baz Qux"]
1159 */
1160 public function fetchMap($keyColumn, $valueColumn) {
1161 $result = array();
1162 while ($this->fetch()) {
1163 $result[$this->{$keyColumn}] = $this->{$valueColumn};
1164 }
1165 return $result;
1166 }
1167
1168 /**
1169 * Given a DAO name, a column name and a column value, find the record and GET the value of another column in that record
1170 *
1171 * @param string $daoName
1172 * Name of the DAO (Example: CRM_Contact_DAO_Contact to retrieve value from a contact).
1173 * @param int $searchValue
1174 * Value of the column you want to search by.
1175 * @param string $returnColumn
1176 * Name of the column you want to GET the value of.
1177 * @param string $searchColumn
1178 * Name of the column you want to search by.
1179 * @param bool $force
1180 * Skip use of the cache.
1181 *
1182 * @return string|null
1183 * Value of $returnColumn in the retrieved record
1184 */
1185 public static function getFieldValue($daoName, $searchValue, $returnColumn = 'name', $searchColumn = 'id', $force = FALSE) {
1186 if (
1187 empty($searchValue) ||
1188 trim(strtolower($searchValue)) == 'null'
1189 ) {
1190 // adding this here since developers forget to check for an id
1191 // or for the 'null' (which is a bad DAO kludge)
1192 // and hence we get the first value in the db
1193 CRM_Core_Error::fatal();
1194 }
1195
1196 $cacheKey = "{$daoName}:{$searchValue}:{$returnColumn}:{$searchColumn}";
1197 if (self::$_dbColumnValueCache === NULL) {
1198 self::$_dbColumnValueCache = array();
1199 }
1200
1201 if (!array_key_exists($cacheKey, self::$_dbColumnValueCache) || $force) {
1202 $object = new $daoName();
1203 $object->$searchColumn = $searchValue;
1204 $object->selectAdd();
1205 $object->selectAdd($returnColumn);
1206
1207 $result = NULL;
1208 if ($object->find(TRUE)) {
1209 $result = $object->$returnColumn;
1210 }
1211 $object->free();
1212
1213 self::$_dbColumnValueCache[$cacheKey] = $result;
1214 }
1215 return self::$_dbColumnValueCache[$cacheKey];
1216 }
1217
1218 /**
1219 * Given a DAO name, a column name and a column value, find the record and SET the value of another column in that record
1220 *
1221 * @param string $daoName
1222 * Name of the DAO (Example: CRM_Contact_DAO_Contact to retrieve value from a contact).
1223 * @param int $searchValue
1224 * Value of the column you want to search by.
1225 * @param string $setColumn
1226 * Name of the column you want to SET the value of.
1227 * @param string $setValue
1228 * SET the setColumn to this value.
1229 * @param string $searchColumn
1230 * Name of the column you want to search by.
1231 *
1232 * @return bool
1233 * true if we found and updated the object, else false
1234 */
1235 public static function setFieldValue($daoName, $searchValue, $setColumn, $setValue, $searchColumn = 'id') {
1236 $object = new $daoName();
1237 $object->selectAdd();
1238 $object->selectAdd("$searchColumn, $setColumn");
1239 $object->$searchColumn = $searchValue;
1240 $result = FALSE;
1241 if ($object->find(TRUE)) {
1242 $object->$setColumn = $setValue;
1243 if ($object->save()) {
1244 $result = TRUE;
1245 }
1246 }
1247 $object->free();
1248 return $result;
1249 }
1250
1251 /**
1252 * Get sort string.
1253 *
1254 * @param array|object $sort either array or CRM_Utils_Sort
1255 * @param string $default
1256 * Default sort value.
1257 *
1258 * @return string
1259 */
1260 public static function getSortString($sort, $default = NULL) {
1261 // check if sort is of type CRM_Utils_Sort
1262 if (is_a($sort, 'CRM_Utils_Sort')) {
1263 return $sort->orderBy();
1264 }
1265
1266 $sortString = '';
1267
1268 // is it an array specified as $field => $sortDirection ?
1269 if ($sort) {
1270 foreach ($sort as $k => $v) {
1271 $sortString .= "$k $v,";
1272 }
1273 return rtrim($sortString, ',');
1274 }
1275 return $default;
1276 }
1277
1278 /**
1279 * Fetch object based on array of properties.
1280 *
1281 * @param string $daoName
1282 * Name of the dao object.
1283 * @param array $params
1284 * (reference ) an assoc array of name/value pairs.
1285 * @param array $defaults
1286 * (reference ) an assoc array to hold the flattened values.
1287 * @param array $returnProperities
1288 * An assoc array of fields that need to be returned, eg array( 'first_name', 'last_name').
1289 *
1290 * @return object
1291 * an object of type referenced by daoName
1292 */
1293 public static function commonRetrieve($daoName, &$params, &$defaults, $returnProperities = NULL) {
1294 $object = new $daoName();
1295 $object->copyValues($params);
1296
1297 // return only specific fields if returnproperties are sent
1298 if (!empty($returnProperities)) {
1299 $object->selectAdd();
1300 $object->selectAdd(implode(',', $returnProperities));
1301 }
1302
1303 if ($object->find(TRUE)) {
1304 self::storeValues($object, $defaults);
1305 return $object;
1306 }
1307 return NULL;
1308 }
1309
1310 /**
1311 * Delete the object records that are associated with this contact.
1312 *
1313 * @param string $daoName
1314 * Name of the dao object.
1315 * @param int $contactId
1316 * Id of the contact to delete.
1317 */
1318 public static function deleteEntityContact($daoName, $contactId) {
1319 $object = new $daoName();
1320
1321 $object->entity_table = 'civicrm_contact';
1322 $object->entity_id = $contactId;
1323 $object->delete();
1324 }
1325
1326 /**
1327 * Execute an unbuffered query.
1328 *
1329 * This is a wrapper around new functionality exposed with CRM-17748.
1330 *
1331 * @param string $query query to be executed
1332 *
1333 * @param array $params
1334 * @param bool $abort
1335 * @param null $daoName
1336 * @param bool $freeDAO
1337 * @param bool $i18nRewrite
1338 * @param bool $trapException
1339 *
1340 * @return CRM_Core_DAO
1341 * Object that points to an unbuffered result set
1342 */
1343 static public function executeUnbufferedQuery(
1344 $query,
1345 $params = array(),
1346 $abort = TRUE,
1347 $daoName = NULL,
1348 $freeDAO = FALSE,
1349 $i18nRewrite = TRUE,
1350 $trapException = FALSE
1351 ) {
1352
1353 return self::executeQuery(
1354 $query,
1355 $params,
1356 $abort,
1357 $daoName,
1358 $freeDAO,
1359 $i18nRewrite,
1360 $trapException,
1361 array('result_buffering' => 0)
1362 );
1363 }
1364
1365 /**
1366 * Execute a query.
1367 *
1368 * @param string $query
1369 * Query to be executed.
1370 *
1371 * @param array $params
1372 * @param bool $abort
1373 * @param null $daoName
1374 * @param bool $freeDAO
1375 * @param bool $i18nRewrite
1376 * @param bool $trapException
1377 * @param array $options
1378 *
1379 * @return CRM_Core_DAO|object
1380 * object that holds the results of the query
1381 * NB - if this is defined as just returning a DAO phpstorm keeps pointing
1382 * out all the properties that are not part of the DAO
1383 */
1384 public static function &executeQuery(
1385 $query,
1386 $params = array(),
1387 $abort = TRUE,
1388 $daoName = NULL,
1389 $freeDAO = FALSE,
1390 $i18nRewrite = TRUE,
1391 $trapException = FALSE,
1392 $options = array()
1393 ) {
1394 $queryStr = self::composeQuery($query, $params, $abort);
1395
1396 if (!$daoName) {
1397 $dao = new CRM_Core_DAO();
1398 }
1399 else {
1400 $dao = new $daoName();
1401 }
1402
1403 if ($trapException) {
1404 $errorScope = CRM_Core_TemporaryErrorScope::ignoreException();
1405 }
1406
1407 if ($dao->isValidOption($options)) {
1408 $dao->setOptions($options);
1409 }
1410
1411 $result = $dao->query($queryStr, $i18nRewrite);
1412
1413 // since it is unbuffered, ($dao->N==0) is true. This blocks the standard fetch() mechanism.
1414 if (CRM_Utils_Array::value('result_buffering', $options) === 0) {
1415 $dao->N = TRUE;
1416 }
1417
1418 if (is_a($result, 'DB_Error')) {
1419 return $result;
1420 }
1421
1422 if ($freeDAO ||
1423 preg_match('/^(insert|update|delete|create|drop|replace)/i', $queryStr)
1424 ) {
1425 // we typically do this for insert/update/delete statements OR if explicitly asked to
1426 // free the dao
1427 $dao->free();
1428 }
1429 return $dao;
1430 }
1431
1432 /**
1433 * Wrapper to validate internal DAO options before passing to DB_mysql/DB_Common level
1434 *
1435 * @param array $options
1436 *
1437 * @return bool
1438 * Provided options are valid
1439 */
1440 public function isValidOption($options) {
1441 $isValid = FALSE;
1442 $validOptions = array(
1443 'result_buffering',
1444 'persistent',
1445 'ssl',
1446 'portability',
1447 );
1448
1449 if (empty($options)) {
1450 return $isValid;
1451 }
1452
1453 foreach (array_keys($options) as $option) {
1454 if (!in_array($option, $validOptions)) {
1455 return FALSE;
1456 }
1457 $isValid = TRUE;
1458 }
1459
1460 return $isValid;
1461 }
1462
1463 /**
1464 * Execute a query and get the single result.
1465 *
1466 * @param string $query
1467 * Query to be executed.
1468 * @param array $params
1469 * @param bool $abort
1470 * @param bool $i18nRewrite
1471 * @return string|null
1472 * the result of the query if any
1473 *
1474 */
1475 public static function &singleValueQuery(
1476 $query,
1477 $params = array(),
1478 $abort = TRUE,
1479 $i18nRewrite = TRUE
1480 ) {
1481 $queryStr = self::composeQuery($query, $params, $abort);
1482
1483 static $_dao = NULL;
1484
1485 if (!$_dao) {
1486 $_dao = new CRM_Core_DAO();
1487 }
1488
1489 $_dao->query($queryStr, $i18nRewrite);
1490
1491 $result = $_dao->getDatabaseResult();
1492 $ret = NULL;
1493 if ($result) {
1494 $row = $result->fetchRow();
1495 if ($row) {
1496 $ret = $row[0];
1497 }
1498 }
1499 $_dao->free();
1500 return $ret;
1501 }
1502
1503 /**
1504 * Compose the query by merging the parameters into it.
1505 *
1506 * @param string $query
1507 * @param array $params
1508 * @param bool $abort
1509 *
1510 * @return string
1511 * @throws Exception
1512 */
1513 public static function composeQuery($query, $params, $abort = TRUE) {
1514 $tr = array();
1515 foreach ($params as $key => $item) {
1516 if (is_numeric($key)) {
1517 if (CRM_Utils_Type::validate($item[0], $item[1]) !== NULL) {
1518 $item[0] = self::escapeString($item[0]);
1519 if ($item[1] == 'String' ||
1520 $item[1] == 'Memo' ||
1521 $item[1] == 'Link'
1522 ) {
1523 // Support class constants stipulating wildcard characters and/or
1524 // non-quoting of strings. Also support legacy code which may be
1525 // passing in TRUE or 1 for $item[2], which used to indicate the
1526 // use of wildcard characters.
1527 if (!empty($item[2])) {
1528 if ($item[2] & CRM_Core_DAO::QUERY_FORMAT_WILDCARD || $item[2] === TRUE) {
1529 $item[0] = "'%{$item[0]}%'";
1530 }
1531 elseif (!($item[2] & CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES)) {
1532 $item[0] = "'{$item[0]}'";
1533 }
1534 }
1535 else {
1536 $item[0] = "'{$item[0]}'";
1537 }
1538 }
1539
1540 if (($item[1] == 'Date' || $item[1] == 'Timestamp') &&
1541 strlen($item[0]) == 0
1542 ) {
1543 $item[0] = 'null';
1544 }
1545
1546 $tr['%' . $key] = $item[0];
1547 }
1548 elseif ($abort) {
1549 CRM_Core_Error::fatal("{$item[0]} is not of type {$item[1]}");
1550 }
1551 }
1552 }
1553
1554 return strtr($query, $tr);
1555 }
1556
1557 /**
1558 * @param null $ids
1559 */
1560 public static function freeResult($ids = NULL) {
1561 global $_DB_DATAOBJECT;
1562
1563 if (!$ids) {
1564 if (!$_DB_DATAOBJECT ||
1565 !isset($_DB_DATAOBJECT['RESULTS'])
1566 ) {
1567 return;
1568 }
1569 $ids = array_keys($_DB_DATAOBJECT['RESULTS']);
1570 }
1571
1572 foreach ($ids as $id) {
1573 if (isset($_DB_DATAOBJECT['RESULTS'][$id])) {
1574 $_DB_DATAOBJECT['RESULTS'][$id]->free();
1575 unset($_DB_DATAOBJECT['RESULTS'][$id]);
1576 }
1577
1578 if (isset($_DB_DATAOBJECT['RESULTFIELDS'][$id])) {
1579 unset($_DB_DATAOBJECT['RESULTFIELDS'][$id]);
1580 }
1581 }
1582 }
1583
1584 /**
1585 * Make a shallow copy of an object and all the fields in the object.
1586 *
1587 * @param string $daoName
1588 * Name of the dao.
1589 * @param array $criteria
1590 * Array of all the fields & values.
1591 * on which basis to copy
1592 * @param array $newData
1593 * Array of all the fields & values.
1594 * to be copied besides the other fields
1595 * @param string $fieldsFix
1596 * Array of fields that you want to prefix/suffix/replace.
1597 * @param string $blockCopyOfDependencies
1598 * Fields that you want to block from.
1599 * getting copied
1600 *
1601 * @return CRM_Core_DAO
1602 * the newly created copy of the object
1603 */
1604 public static function &copyGeneric($daoName, $criteria, $newData = NULL, $fieldsFix = NULL, $blockCopyOfDependencies = NULL) {
1605 $object = new $daoName();
1606 if (!$newData) {
1607 $object->id = $criteria['id'];
1608 }
1609 else {
1610 foreach ($criteria as $key => $value) {
1611 $object->$key = $value;
1612 }
1613 }
1614
1615 $object->find();
1616 while ($object->fetch()) {
1617
1618 // all the objects except with $blockCopyOfDependencies set
1619 // be copied - addresses #CRM-1962
1620
1621 if ($blockCopyOfDependencies && $object->$blockCopyOfDependencies) {
1622 break;
1623 }
1624
1625 $newObject = new $daoName();
1626
1627 $fields = $object->fields();
1628 if (!is_array($fieldsFix)) {
1629 $fieldsToPrefix = array();
1630 $fieldsToSuffix = array();
1631 $fieldsToReplace = array();
1632 }
1633 if (!empty($fieldsFix['prefix'])) {
1634 $fieldsToPrefix = $fieldsFix['prefix'];
1635 }
1636 if (!empty($fieldsFix['suffix'])) {
1637 $fieldsToSuffix = $fieldsFix['suffix'];
1638 }
1639 if (!empty($fieldsFix['replace'])) {
1640 $fieldsToReplace = $fieldsFix['replace'];
1641 }
1642
1643 foreach ($fields as $name => $value) {
1644 if ($name == 'id' || $value['name'] == 'id') {
1645 // copy everything but the id!
1646 continue;
1647 }
1648
1649 $dbName = $value['name'];
1650 $type = CRM_Utils_Type::typeToString($value['type']);
1651 $newObject->$dbName = $object->$dbName;
1652 if (isset($fieldsToPrefix[$dbName])) {
1653 $newObject->$dbName = $fieldsToPrefix[$dbName] . $newObject->$dbName;
1654 }
1655 if (isset($fieldsToSuffix[$dbName])) {
1656 $newObject->$dbName .= $fieldsToSuffix[$dbName];
1657 }
1658 if (isset($fieldsToReplace[$dbName])) {
1659 $newObject->$dbName = $fieldsToReplace[$dbName];
1660 }
1661
1662 if ($type == 'Timestamp' || $type == 'Date') {
1663 $newObject->$dbName = CRM_Utils_Date::isoToMysql($newObject->$dbName);
1664 }
1665
1666 if ($newData) {
1667 foreach ($newData as $k => $v) {
1668 $newObject->$k = $v;
1669 }
1670 }
1671 }
1672 $newObject->save();
1673 }
1674 return $newObject;
1675 }
1676
1677 /**
1678 * Cascade update through related entities.
1679 *
1680 * @param string $daoName
1681 * @param $fromId
1682 * @param $toId
1683 * @param array $newData
1684 *
1685 * @return CRM_Core_DAO|null
1686 */
1687 public static function cascadeUpdate($daoName, $fromId, $toId, $newData = array()) {
1688 $object = new $daoName();
1689 $object->id = $fromId;
1690
1691 if ($object->find(TRUE)) {
1692 $newObject = new $daoName();
1693 $newObject->id = $toId;
1694
1695 if ($newObject->find(TRUE)) {
1696 $fields = $object->fields();
1697 foreach ($fields as $name => $value) {
1698 if ($name == 'id' || $value['name'] == 'id') {
1699 // copy everything but the id!
1700 continue;
1701 }
1702
1703 $colName = $value['name'];
1704 $newObject->$colName = $object->$colName;
1705
1706 if (substr($name, -5) == '_date' ||
1707 substr($name, -10) == '_date_time'
1708 ) {
1709 $newObject->$colName = CRM_Utils_Date::isoToMysql($newObject->$colName);
1710 }
1711 }
1712 foreach ($newData as $k => $v) {
1713 $newObject->$k = $v;
1714 }
1715 $newObject->save();
1716 return $newObject;
1717 }
1718 }
1719 return NULL;
1720 }
1721
1722 /**
1723 * Given the component id, compute the contact id
1724 * since its used for things like send email
1725 *
1726 * @param $componentIDs
1727 * @param string $tableName
1728 * @param string $idField
1729 *
1730 * @return array
1731 */
1732 public static function getContactIDsFromComponent($componentIDs, $tableName, $idField = 'id') {
1733 $contactIDs = array();
1734
1735 if (empty($componentIDs)) {
1736 return $contactIDs;
1737 }
1738
1739 $IDs = implode(',', $componentIDs);
1740 $query = "
1741 SELECT contact_id
1742 FROM $tableName
1743 WHERE $idField IN ( $IDs )
1744 ";
1745
1746 $dao = CRM_Core_DAO::executeQuery($query);
1747 while ($dao->fetch()) {
1748 $contactIDs[] = $dao->contact_id;
1749 }
1750 return $contactIDs;
1751 }
1752
1753 /**
1754 * Fetch object based on array of properties.
1755 *
1756 * @param string $daoName
1757 * Name of the dao object.
1758 * @param string $fieldIdName
1759 * @param int $fieldId
1760 * @param $details
1761 * @param array $returnProperities
1762 * An assoc array of fields that need to be returned, eg array( 'first_name', 'last_name').
1763 *
1764 * @return object
1765 * an object of type referenced by daoName
1766 */
1767 public static function commonRetrieveAll($daoName, $fieldIdName = 'id', $fieldId, &$details, $returnProperities = NULL) {
1768 require_once str_replace('_', DIRECTORY_SEPARATOR, $daoName) . ".php";
1769 $object = new $daoName();
1770 $object->$fieldIdName = $fieldId;
1771
1772 // return only specific fields if returnproperties are sent
1773 if (!empty($returnProperities)) {
1774 $object->selectAdd();
1775 $object->selectAdd('id');
1776 $object->selectAdd(implode(',', $returnProperities));
1777 }
1778
1779 $object->find();
1780 while ($object->fetch()) {
1781 $defaults = array();
1782 self::storeValues($object, $defaults);
1783 $details[$object->id] = $defaults;
1784 }
1785
1786 return $details;
1787 }
1788
1789 /**
1790 * Drop all CiviCRM tables.
1791 *
1792 * @throws \CRM_Exception
1793 */
1794 public static function dropAllTables() {
1795
1796 // first drop all the custom tables we've created
1797 CRM_Core_BAO_CustomGroup::dropAllTables();
1798
1799 // drop all multilingual views
1800 CRM_Core_I18n_Schema::dropAllViews();
1801
1802 CRM_Utils_File::sourceSQLFile(CIVICRM_DSN,
1803 dirname(__FILE__) . DIRECTORY_SEPARATOR .
1804 '..' . DIRECTORY_SEPARATOR .
1805 '..' . DIRECTORY_SEPARATOR .
1806 'sql' . DIRECTORY_SEPARATOR .
1807 'civicrm_drop.mysql'
1808 );
1809 }
1810
1811 /**
1812 * @param $string
1813 *
1814 * @return string
1815 */
1816 public static function escapeString($string) {
1817 static $_dao = NULL;
1818 if (!$_dao) {
1819 // If this is an atypical case (e.g. preparing .sql file before CiviCRM
1820 // has been installed), then we fallback DB-less str_replace escaping, as
1821 // we can't use mysqli_real_escape_string, as there is no DB connection.
1822 // Note: In typical usage, escapeString() will only check one conditional
1823 // ("if !$_dao") rather than two conditionals ("if !defined(DSN)")
1824 if (!defined('CIVICRM_DSN')) {
1825 // See http://php.net/manual/en/mysqli.real-escape-string.php for the
1826 // list of characters mysqli_real_escape_string escapes.
1827 $search = array("\\", "\x00", "\n", "\r", "'", '"', "\x1a");
1828 $replace = array("\\\\", "\\0", "\\n", "\\r", "\'", '\"', "\\Z");
1829 return str_replace($search, $replace, $string);
1830 }
1831 $_dao = new CRM_Core_DAO();
1832 }
1833 return $_dao->escape($string);
1834 }
1835
1836 /**
1837 * Escape a list of strings for use with "WHERE X IN (...)" queries.
1838 *
1839 * @param array $strings
1840 * @param string $default
1841 * the value to use if $strings has no elements.
1842 * @return string
1843 * eg "abc","def","ghi"
1844 */
1845 public static function escapeStrings($strings, $default = NULL) {
1846 static $_dao = NULL;
1847 if (!$_dao) {
1848 $_dao = new CRM_Core_DAO();
1849 }
1850
1851 if (empty($strings)) {
1852 return $default;
1853 }
1854
1855 $escapes = array_map(array($_dao, 'escape'), $strings);
1856 return '"' . implode('","', $escapes) . '"';
1857 }
1858
1859 /**
1860 * @param $string
1861 *
1862 * @return string
1863 */
1864 public static function escapeWildCardString($string) {
1865 // CRM-9155
1866 // ensure we escape the single characters % and _ which are mysql wild
1867 // card characters and could come in via sortByCharacter
1868 // note that mysql does not escape these characters
1869 if ($string && in_array($string,
1870 array('%', '_', '%%', '_%')
1871 )
1872 ) {
1873 return '\\' . $string;
1874 }
1875
1876 return self::escapeString($string);
1877 }
1878
1879 /**
1880 * Creates a test object, including any required objects it needs via recursion
1881 * createOnly: only create in database, do not store or return the objects (useful for perf testing)
1882 * ONLY USE FOR TESTING
1883 *
1884 * @param string $daoName
1885 * @param array $params
1886 * @param int $numObjects
1887 * @param bool $createOnly
1888 *
1889 * @return object|array|NULL
1890 * NULL if $createOnly. A single object if $numObjects==1. Otherwise, an array of multiple objects.
1891 */
1892 public static function createTestObject(
1893 $daoName,
1894 $params = array(),
1895 $numObjects = 1,
1896 $createOnly = FALSE
1897 ) {
1898 //this is a test function also backtrace is set for the test suite it sometimes unsets itself
1899 // so we re-set here in case
1900 $config = CRM_Core_Config::singleton();
1901 $config->backtrace = TRUE;
1902
1903 static $counter = 0;
1904 CRM_Core_DAO::$_testEntitiesToSkip = array(
1905 'CRM_Core_DAO_Worldregion',
1906 'CRM_Core_DAO_StateProvince',
1907 'CRM_Core_DAO_Country',
1908 'CRM_Core_DAO_Domain',
1909 'CRM_Financial_DAO_FinancialType',
1910 //because valid ones exist & we use pick them due to pseudoconstant can't reliably create & delete these
1911 );
1912
1913 // Prefer to instantiate BAO's instead of DAO's (when possible)
1914 // so that assignTestValue()/assignTestFK() can be overloaded.
1915 $baoName = str_replace('_DAO_', '_BAO_', $daoName);
1916 if (class_exists($baoName)) {
1917 $daoName = $baoName;
1918 }
1919
1920 for ($i = 0; $i < $numObjects; ++$i) {
1921
1922 ++$counter;
1923 /** @var CRM_Core_DAO $object */
1924 $object = new $daoName();
1925
1926 $fields = $object->fields();
1927 foreach ($fields as $fieldName => $fieldDef) {
1928 $dbName = $fieldDef['name'];
1929 $FKClassName = CRM_Utils_Array::value('FKClassName', $fieldDef);
1930 $required = CRM_Utils_Array::value('required', $fieldDef);
1931
1932 if (CRM_Utils_Array::value($dbName, $params) !== NULL && !is_array($params[$dbName])) {
1933 $object->$dbName = $params[$dbName];
1934 }
1935
1936 elseif ($dbName != 'id') {
1937 if ($FKClassName != NULL) {
1938 $object->assignTestFK($fieldName, $fieldDef, $params);
1939 continue;
1940 }
1941 else {
1942 $object->assignTestValue($fieldName, $fieldDef, $counter);
1943 }
1944 }
1945 }
1946
1947 $object->save();
1948
1949 if (!$createOnly) {
1950 $objects[$i] = $object;
1951 }
1952 else {
1953 unset($object);
1954 }
1955 }
1956
1957 if ($createOnly) {
1958 return NULL;
1959 }
1960 elseif ($numObjects == 1) {
1961 return $objects[0];
1962 }
1963 else {
1964 return $objects;
1965 }
1966 }
1967
1968 /**
1969 * Deletes the this object plus any dependent objects that are associated with it.
1970 * ONLY USE FOR TESTING
1971 *
1972 * @param string $daoName
1973 * @param array $params
1974 */
1975 public static function deleteTestObjects($daoName, $params = array()) {
1976 //this is a test function also backtrace is set for the test suite it sometimes unsets itself
1977 // so we re-set here in case
1978 $config = CRM_Core_Config::singleton();
1979 $config->backtrace = TRUE;
1980
1981 $object = new $daoName();
1982 $object->id = CRM_Utils_Array::value('id', $params);
1983
1984 $deletions = array(); // array(array(0 => $daoName, 1 => $daoParams))
1985 if ($object->find(TRUE)) {
1986
1987 $fields = $object->fields();
1988 foreach ($fields as $name => $value) {
1989
1990 $dbName = $value['name'];
1991
1992 $FKClassName = CRM_Utils_Array::value('FKClassName', $value);
1993 $required = CRM_Utils_Array::value('required', $value);
1994 if ($FKClassName != NULL
1995 && $object->$dbName
1996 && !in_array($FKClassName, CRM_Core_DAO::$_testEntitiesToSkip)
1997 && ($required || $dbName == 'contact_id')
1998 //I'm a bit stuck on this one - we might need to change the singleValueAlter so that the entities don't share a contact
1999 // to make this test process pass - line below makes pass for now
2000 && $dbName != 'member_of_contact_id'
2001 ) {
2002 $deletions[] = array($FKClassName, array('id' => $object->$dbName)); // x
2003 }
2004 }
2005 }
2006
2007 $object->delete();
2008
2009 foreach ($deletions as $deletion) {
2010 CRM_Core_DAO::deleteTestObjects($deletion[0], $deletion[1]);
2011 }
2012 }
2013
2014 /**
2015 * Set defaults when creating new entity.
2016 * (don't call this set defaults as already in use with different signature in some places)
2017 *
2018 * @param array $params
2019 * @param $defaults
2020 */
2021 public static function setCreateDefaults(&$params, $defaults) {
2022 if (!empty($params['id'])) {
2023 return;
2024 }
2025 foreach ($defaults as $key => $value) {
2026 if (!array_key_exists($key, $params) || $params[$key] === NULL) {
2027 $params[$key] = $value;
2028 }
2029 }
2030 }
2031
2032 /**
2033 * @param string $prefix
2034 * @param bool $addRandomString
2035 * @param null $string
2036 *
2037 * @return string
2038 * @deprecated
2039 * @see CRM_Utils_SQL_TempTable
2040 */
2041 public static function createTempTableName($prefix = 'civicrm', $addRandomString = TRUE, $string = NULL) {
2042 $tableName = $prefix . "_temp";
2043
2044 if ($addRandomString) {
2045 if ($string) {
2046 $tableName .= "_" . $string;
2047 }
2048 else {
2049 $tableName .= "_" . md5(uniqid('', TRUE));
2050 }
2051 }
2052 return $tableName;
2053 }
2054
2055 /**
2056 * @param bool $view
2057 * @param bool $trigger
2058 *
2059 * @return bool
2060 */
2061 public static function checkTriggerViewPermission($view = TRUE, $trigger = TRUE) {
2062 if (\Civi::settings()->get('logging_no_trigger_permission')) {
2063 return TRUE;
2064 }
2065 // test for create view and trigger permissions and if allowed, add the option to go multilingual
2066 // and logging
2067 // I'm not sure why we use the getStaticProperty for an error, rather than checking for DB_Error
2068 CRM_Core_TemporaryErrorScope::ignoreException();
2069 $dao = new CRM_Core_DAO();
2070 if ($view) {
2071 $result = $dao->query('CREATE OR REPLACE VIEW civicrm_domain_view AS SELECT * FROM civicrm_domain');
2072 if (PEAR::getStaticProperty('DB_DataObject', 'lastError') || is_a($result, 'DB_Error')) {
2073 return FALSE;
2074 }
2075 }
2076
2077 if ($trigger) {
2078 $result = $dao->query('CREATE TRIGGER civicrm_domain_trigger BEFORE INSERT ON civicrm_domain FOR EACH ROW BEGIN END');
2079 if (PEAR::getStaticProperty('DB_DataObject', 'lastError') || is_a($result, 'DB_Error')) {
2080 if ($view) {
2081 $dao->query('DROP VIEW IF EXISTS civicrm_domain_view');
2082 }
2083 return FALSE;
2084 }
2085
2086 $dao->query('DROP TRIGGER IF EXISTS civicrm_domain_trigger');
2087 if (PEAR::getStaticProperty('DB_DataObject', 'lastError')) {
2088 if ($view) {
2089 $dao->query('DROP VIEW IF EXISTS civicrm_domain_view');
2090 }
2091 return FALSE;
2092 }
2093 }
2094
2095 if ($view) {
2096 $dao->query('DROP VIEW IF EXISTS civicrm_domain_view');
2097 if (PEAR::getStaticProperty('DB_DataObject', 'lastError')) {
2098 return FALSE;
2099 }
2100 }
2101
2102 return TRUE;
2103 }
2104
2105 /**
2106 * @param null $message
2107 * @param bool $printDAO
2108 */
2109 public static function debugPrint($message = NULL, $printDAO = TRUE) {
2110 CRM_Utils_System::xMemory("{$message}: ");
2111
2112 if ($printDAO) {
2113 global $_DB_DATAOBJECT;
2114 $q = array();
2115 foreach (array_keys($_DB_DATAOBJECT['RESULTS']) as $id) {
2116 $q[] = $_DB_DATAOBJECT['RESULTS'][$id]->query;
2117 }
2118 CRM_Core_Error::debug('_DB_DATAOBJECT', $q);
2119 }
2120 }
2121
2122 /**
2123 * Build a list of triggers via hook and add them to (err, reconcile them
2124 * with) the database.
2125 *
2126 * @param string $tableName
2127 * the specific table requiring a rebuild; or NULL to rebuild all tables.
2128 * @param bool $force
2129 * @deprecated
2130 *
2131 * @see CRM-9716
2132 */
2133 public static function triggerRebuild($tableName = NULL, $force = FALSE) {
2134 Civi::service('sql_triggers')->rebuild($tableName, $force);
2135 }
2136
2137 /**
2138 * Because sql functions are sometimes lost, esp during db migration, we check here to avoid numerous support requests
2139 * @see http://issues.civicrm.org/jira/browse/CRM-13822
2140 * TODO: Alternative solutions might be
2141 * * Stop using functions and find another way to strip numeric characters from phones
2142 * * Give better error messages (currently a missing fn fatals with "unknown error")
2143 */
2144 public static function checkSqlFunctionsExist() {
2145 if (!self::$_checkedSqlFunctionsExist) {
2146 self::$_checkedSqlFunctionsExist = TRUE;
2147 $dao = CRM_Core_DAO::executeQuery("SHOW function status WHERE db = database() AND name = 'civicrm_strip_non_numeric'");
2148 if (!$dao->fetch()) {
2149 self::triggerRebuild();
2150 }
2151 }
2152 }
2153
2154 /**
2155 * Wrapper function to drop triggers.
2156 *
2157 * @param string $tableName
2158 * the specific table requiring a rebuild; or NULL to rebuild all tables.
2159 * @deprecated
2160 */
2161 public static function dropTriggers($tableName = NULL) {
2162 Civi::service('sql_triggers')->dropTriggers($tableName);
2163 }
2164
2165 /**
2166 * @param array $info
2167 * per hook_civicrm_triggerInfo.
2168 * @param string $onlyTableName
2169 * the specific table requiring a rebuild; or NULL to rebuild all tables.
2170 * @deprecated
2171 */
2172 public static function createTriggers(&$info, $onlyTableName = NULL) {
2173 Civi::service('sql_triggers')->createTriggers($info, $onlyTableName);
2174 }
2175
2176 /**
2177 * Given a list of fields, create a list of references.
2178 *
2179 * @param string $className
2180 * BAO/DAO class name.
2181 * @return array<CRM_Core_Reference_Interface>
2182 */
2183 public static function createReferenceColumns($className) {
2184 $result = array();
2185 $fields = $className::fields();
2186 foreach ($fields as $field) {
2187 if (isset($field['pseudoconstant'], $field['pseudoconstant']['optionGroupName'])) {
2188 $result[] = new CRM_Core_Reference_OptionValue(
2189 $className::getTableName(),
2190 $field['name'],
2191 'civicrm_option_value',
2192 CRM_Utils_Array::value('keyColumn', $field['pseudoconstant'], 'value'),
2193 $field['pseudoconstant']['optionGroupName']
2194 );
2195 }
2196 }
2197 return $result;
2198 }
2199
2200 /**
2201 * Find all records which refer to this entity.
2202 *
2203 * @return array
2204 * Array of objects referencing this
2205 */
2206 public function findReferences() {
2207 $links = self::getReferencesToTable(static::getTableName());
2208
2209 $occurrences = array();
2210 foreach ($links as $refSpec) {
2211 /** @var $refSpec CRM_Core_Reference_Interface */
2212 $daoName = CRM_Core_DAO_AllCoreTables::getClassForTable($refSpec->getReferenceTable());
2213 $result = $refSpec->findReferences($this);
2214 if ($result) {
2215 while ($result->fetch()) {
2216 $obj = new $daoName();
2217 $obj->id = $result->id;
2218 $occurrences[] = $obj;
2219 }
2220 }
2221 }
2222
2223 return $occurrences;
2224 }
2225
2226 /**
2227 * @return array
2228 * each item has keys:
2229 * - name: string
2230 * - type: string
2231 * - count: int
2232 * - table: string|null SQL table name
2233 * - key: string|null SQL column name
2234 */
2235 public function getReferenceCounts() {
2236 $links = self::getReferencesToTable(static::getTableName());
2237
2238 $counts = array();
2239 foreach ($links as $refSpec) {
2240 /** @var $refSpec CRM_Core_Reference_Interface */
2241 $count = $refSpec->getReferenceCount($this);
2242 if ($count['count'] != 0) {
2243 $counts[] = $count;
2244 }
2245 }
2246
2247 foreach (CRM_Core_Component::getEnabledComponents() as $component) {
2248 /** @var $component CRM_Core_Component_Info */
2249 $counts = array_merge($counts, $component->getReferenceCounts($this));
2250 }
2251 CRM_Utils_Hook::referenceCounts($this, $counts);
2252
2253 return $counts;
2254 }
2255
2256 /**
2257 * List all tables which have hard foreign keys to this table.
2258 *
2259 * For now, this returns a description of every entity_id/entity_table
2260 * reference.
2261 * TODO: filter dynamic entity references on the $tableName, based on
2262 * schema metadata in dynamicForeignKey which enumerates a restricted
2263 * set of possible entity_table's.
2264 *
2265 * @param string $tableName
2266 * Table referred to.
2267 *
2268 * @return array
2269 * structure of table and column, listing every table with a
2270 * foreign key reference to $tableName, and the column where the key appears.
2271 */
2272 public static function getReferencesToTable($tableName) {
2273 $refsFound = array();
2274 foreach (CRM_Core_DAO_AllCoreTables::getClasses() as $daoClassName) {
2275 $links = $daoClassName::getReferenceColumns();
2276 $daoTableName = $daoClassName::getTableName();
2277
2278 foreach ($links as $refSpec) {
2279 /** @var $refSpec CRM_Core_Reference_Interface */
2280 if ($refSpec->matchesTargetTable($tableName)) {
2281 $refsFound[] = $refSpec;
2282 }
2283 }
2284 }
2285 return $refsFound;
2286 }
2287
2288 /**
2289 * Get all references to contact table.
2290 *
2291 * This includes core tables, custom group tables, tables added by the merge
2292 * hook and the entity_tag table.
2293 *
2294 * Refer to CRM-17454 for information on the danger of querying the information
2295 * schema to derive this.
2296 */
2297 public static function getReferencesToContactTable() {
2298 if (isset(\Civi::$statics[__CLASS__]) && isset(\Civi::$statics[__CLASS__]['contact_references'])) {
2299 return \Civi::$statics[__CLASS__]['contact_references'];
2300 }
2301 $contactReferences = [];
2302 $coreReferences = CRM_Core_DAO::getReferencesToTable('civicrm_contact');
2303 foreach ($coreReferences as $coreReference) {
2304 if (!is_a($coreReference, 'CRM_Core_Reference_Dynamic')) {
2305 $contactReferences[$coreReference->getReferenceTable()][] = $coreReference->getReferenceKey();
2306 }
2307 }
2308 self::appendCustomTablesExtendingContacts($contactReferences);
2309
2310 // FixME for time being adding below line statically as no Foreign key constraint defined for table 'civicrm_entity_tag'
2311 $contactReferences['civicrm_entity_tag'][] = 'entity_id';
2312 \Civi::$statics[__CLASS__]['contact_references'] = $contactReferences;
2313 return \Civi::$statics[__CLASS__]['contact_references'];
2314 }
2315
2316 /**
2317 * Add custom tables that extend contacts to the list of contact references.
2318 *
2319 * CRM_Core_BAO_CustomGroup::getAllCustomGroupsByBaseEntity seems like a safe-ish
2320 * function to be sure all are retrieved & we don't miss subtypes or inactive or multiples
2321 * - the down side is it is not cached.
2322 *
2323 * Further changes should be include tests in the CRM_Core_MergerTest class
2324 * to ensure that disabled, subtype, multiple etc groups are still captured.
2325 *
2326 * @param array $cidRefs
2327 */
2328 public static function appendCustomTablesExtendingContacts(&$cidRefs) {
2329 $customValueTables = CRM_Core_BAO_CustomGroup::getAllCustomGroupsByBaseEntity('Contact');
2330 $customValueTables->find();
2331 while ($customValueTables->fetch()) {
2332 $cidRefs[$customValueTables->table_name] = array('entity_id');
2333 }
2334 }
2335
2336 /**
2337 * Lookup the value of a MySQL global configuration variable.
2338 *
2339 * @param string $name
2340 * E.g. "thread_stack".
2341 * @param mixed $default
2342 * @return mixed
2343 */
2344 public static function getGlobalSetting($name, $default = NULL) {
2345 // Alternatively, SELECT @@GLOBAL.thread_stack, but
2346 // that has been reported to fail under MySQL 5.0 for OS X
2347 $escapedName = self::escapeString($name);
2348 $dao = CRM_Core_DAO::executeQuery("SHOW VARIABLES LIKE '$escapedName'");
2349 if ($dao->fetch()) {
2350 return $dao->Value;
2351 }
2352 else {
2353 return $default;
2354 }
2355 }
2356
2357
2358 /**
2359 * Update the fields array to also hold keys for pseudoconstant fields that relate to contained fields.
2360 *
2361 * This is relevant where we want to offer both the ID field and the label field
2362 * as an option, e.g. search builder.
2363 *
2364 * It is currently limited for optionGroupName for purposes keeping the scope of the
2365 * change small, but is appropriate for other sorts of pseudoconstants.
2366 *
2367 * @param array $fields
2368 */
2369 protected static function appendPseudoConstantsToFields(&$fields) {
2370 foreach ($fields as $field) {
2371 if (!empty($field['pseudoconstant']) && !empty($field['pseudoconstant']['optionGroupName'])) {
2372 $fields[$field['pseudoconstant']['optionGroupName']] = array(
2373 'title' => CRM_Core_BAO_OptionGroup::getTitleByName($field['pseudoconstant']['optionGroupName']),
2374 'name' => $field['pseudoconstant']['optionGroupName'],
2375 'data_type' => CRM_Utils_Type::T_STRING,
2376 );
2377 }
2378 }
2379 }
2380
2381 /**
2382 * Get options for the called BAO object's field.
2383 *
2384 * This function can be overridden by each BAO to add more logic related to context.
2385 * The overriding function will generally call the lower-level CRM_Core_PseudoConstant::get
2386 *
2387 * @param string $fieldName
2388 * @param string $context
2389 * @see CRM_Core_DAO::buildOptionsContext
2390 * @param array $props
2391 * whatever is known about this bao object.
2392 *
2393 * @return array|bool
2394 */
2395 public static function buildOptions($fieldName, $context = NULL, $props = array()) {
2396 // If a given bao does not override this function
2397 $baoName = get_called_class();
2398 return CRM_Core_PseudoConstant::get($baoName, $fieldName, $props, $context);
2399 }
2400
2401 /**
2402 * Populate option labels for this object's fields.
2403 *
2404 * @throws exception if called directly on the base class
2405 */
2406 public function getOptionLabels() {
2407 $fields = $this->fields();
2408 if ($fields === NULL) {
2409 throw new Exception('Cannot call getOptionLabels on CRM_Core_DAO');
2410 }
2411 foreach ($fields as $field) {
2412 $name = CRM_Utils_Array::value('name', $field);
2413 if ($name && isset($this->$name)) {
2414 $label = CRM_Core_PseudoConstant::getLabel(get_class($this), $name, $this->$name);
2415 if ($label !== FALSE) {
2416 // Append 'label' onto the field name
2417 $labelName = $name . '_label';
2418 $this->$labelName = $label;
2419 }
2420 }
2421 }
2422 }
2423
2424 /**
2425 * Provides documentation and validation for the buildOptions $context param
2426 *
2427 * @param string $context
2428 *
2429 * @throws Exception
2430 * @return array
2431 */
2432 public static function buildOptionsContext($context = NULL) {
2433 $contexts = array(
2434 'get' => "get: all options are returned, even if they are disabled; labels are translated.",
2435 'create' => "create: options are filtered appropriately for the object being created/updated; labels are translated.",
2436 'search' => "search: searchable options are returned; labels are translated.",
2437 'validate' => "validate: all options are returned, even if they are disabled; machine names are used in place of labels.",
2438 'abbreviate' => "abbreviate: enabled options are returned; labels are replaced with abbreviations.",
2439 'match' => "match: enabled options are returned using machine names as keys; labels are translated.",
2440 );
2441 // Validation: enforce uniformity of this param
2442 if ($context !== NULL && !isset($contexts[$context])) {
2443 throw new Exception("'$context' is not a valid context for buildOptions.");
2444 }
2445 return $contexts;
2446 }
2447
2448 /**
2449 * @param string $fieldName
2450 * @return bool|array
2451 */
2452 public function getFieldSpec($fieldName) {
2453 $fields = $this->fields();
2454 $fieldKeys = $this->fieldKeys();
2455
2456 // Support "unique names" as well as sql names
2457 $fieldKey = $fieldName;
2458 if (empty($fields[$fieldKey])) {
2459 $fieldKey = CRM_Utils_Array::value($fieldName, $fieldKeys);
2460 }
2461 // If neither worked then this field doesn't exist. Return false.
2462 if (empty($fields[$fieldKey])) {
2463 return FALSE;
2464 }
2465 return $fields[$fieldKey];
2466 }
2467
2468 /**
2469 * Get SQL where clause for SQL filter syntax input parameters.
2470 *
2471 * SQL version of api function to assign filters to the DAO based on the syntax
2472 * $field => array('IN' => array(4,6,9))
2473 * OR
2474 * $field => array('LIKE' => array('%me%))
2475 * etc
2476 *
2477 * @param string $fieldName
2478 * Name of fields.
2479 * @param array $filter
2480 * filter to be applied indexed by operator.
2481 * @param string $type
2482 * type of field (not actually used - nor in api @todo ).
2483 * @param string $alias
2484 * alternative field name ('as') @todo- not actually used.
2485 * @param bool $returnSanitisedArray
2486 * Return a sanitised array instead of a clause.
2487 * this is primarily so we can add filters @ the api level to the Query object based fields
2488 *
2489 * @throws Exception
2490 *
2491 * @return NULL|string|array
2492 * a string is returned if $returnSanitisedArray is not set, otherwise and Array or NULL
2493 * depending on whether it is supported as yet
2494 */
2495 public static function createSQLFilter($fieldName, $filter, $type = NULL, $alias = NULL, $returnSanitisedArray = FALSE) {
2496 foreach ($filter as $operator => $criteria) {
2497 if (in_array($operator, self::acceptedSQLOperators(), TRUE)) {
2498 switch ($operator) {
2499 // unary operators
2500 case 'IS NULL':
2501 case 'IS NOT NULL':
2502 if (!$returnSanitisedArray) {
2503 return (sprintf('%s %s', $fieldName, $operator));
2504 }
2505 else {
2506 return (sprintf('%s %s ', $fieldName, $operator));
2507 }
2508 break;
2509
2510 // ternary operators
2511 case 'BETWEEN':
2512 case 'NOT BETWEEN':
2513 if (empty($criteria[0]) || empty($criteria[1])) {
2514 throw new Exception("invalid criteria for $operator");
2515 }
2516 if (!$returnSanitisedArray) {
2517 return (sprintf('%s ' . $operator . ' "%s" AND "%s"', $fieldName, CRM_Core_DAO::escapeString($criteria[0]), CRM_Core_DAO::escapeString($criteria[1])));
2518 }
2519 else {
2520 return NULL; // not yet implemented (tests required to implement)
2521 }
2522 break;
2523
2524 // n-ary operators
2525 case 'IN':
2526 case 'NOT IN':
2527 if (empty($criteria)) {
2528 throw new Exception("invalid criteria for $operator");
2529 }
2530 $escapedCriteria = array_map(array(
2531 'CRM_Core_DAO',
2532 'escapeString',
2533 ), $criteria);
2534 if (!$returnSanitisedArray) {
2535 return (sprintf('%s %s ("%s")', $fieldName, $operator, implode('", "', $escapedCriteria)));
2536 }
2537 return $escapedCriteria;
2538
2539 // binary operators
2540
2541 default:
2542 if (!$returnSanitisedArray) {
2543 return (sprintf('%s %s "%s"', $fieldName, $operator, CRM_Core_DAO::escapeString($criteria)));
2544 }
2545 else {
2546 return NULL; // not yet implemented (tests required to implement)
2547 }
2548 }
2549 }
2550 }
2551 }
2552
2553 /**
2554 * @see http://issues.civicrm.org/jira/browse/CRM-9150
2555 * support for other syntaxes is discussed in ticket but being put off for now
2556 * @return array
2557 */
2558 public static function acceptedSQLOperators() {
2559 return array(
2560 '=',
2561 '<=',
2562 '>=',
2563 '>',
2564 '<',
2565 'LIKE',
2566 "<>",
2567 "!=",
2568 "NOT LIKE",
2569 'IN',
2570 'NOT IN',
2571 'BETWEEN',
2572 'NOT BETWEEN',
2573 'IS NOT NULL',
2574 'IS NULL',
2575 );
2576 }
2577
2578 /**
2579 * SQL has a limit of 64 characters on various names:
2580 * table name, trigger name, column name ...
2581 *
2582 * For custom groups and fields we generated names from user entered input
2583 * which can be longer than this length, this function helps with creating
2584 * strings that meet various criteria.
2585 *
2586 * @param string $string
2587 * The string to be shortened.
2588 * @param int $length
2589 * The max length of the string.
2590 *
2591 * @param bool $makeRandom
2592 *
2593 * @return string
2594 */
2595 public static function shortenSQLName($string, $length = 60, $makeRandom = FALSE) {
2596 // early return for strings that meet the requirements
2597 if (strlen($string) <= $length) {
2598 return $string;
2599 }
2600
2601 // easy return for calls that dont need a randomized uniq string
2602 if (!$makeRandom) {
2603 return substr($string, 0, $length);
2604 }
2605
2606 // the string is longer than the length and we need a uniq string
2607 // for the same tablename we need the same uniq string every time
2608 // hence we use md5 on the string, which is not random
2609 // we'll append 8 characters to the end of the tableName
2610 $md5string = substr(md5($string), 0, 8);
2611 return substr($string, 0, $length - 8) . "_{$md5string}";
2612 }
2613
2614 /**
2615 * https://issues.civicrm.org/jira/browse/CRM-17748
2616 * Sets the internal options to be used on a query
2617 *
2618 * @param array $options
2619 *
2620 */
2621 public function setOptions($options) {
2622 if (is_array($options)) {
2623 $this->_options = $options;
2624 }
2625 }
2626
2627 /**
2628 * https://issues.civicrm.org/jira/browse/CRM-17748
2629 * wrapper to pass internal DAO options down to DB_mysql/DB_Common level
2630 *
2631 * @param array $options
2632 *
2633 */
2634 protected function _setDBOptions($options) {
2635 global $_DB_DATAOBJECT;
2636
2637 if (is_array($options) && count($options)) {
2638 $conn = &$_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5];
2639 foreach ($options as $option_name => $option_value) {
2640 $conn->setOption($option_name, $option_value);
2641 }
2642 }
2643 }
2644
2645 /**
2646 * @deprecated
2647 * @param array $params
2648 */
2649 public function setApiFilter(&$params) {
2650 }
2651
2652 /**
2653 * Generates acl clauses suitable for adding to WHERE or ON when doing an api.get for this entity
2654 *
2655 * Return format is in the form of fieldname => clauses starting with an operator. e.g.:
2656 * @code
2657 * array(
2658 * 'location_type_id' => array('IS NOT NULL', 'IN (1,2,3)')
2659 * )
2660 * @endcode
2661 *
2662 * Note that all array keys must be actual field names in this entity. Use subqueries to filter on other tables e.g. custom values.
2663 *
2664 * @return array
2665 */
2666 public function addSelectWhereClause() {
2667 $clauses = array();
2668 $fields = $this->fields();
2669 foreach ($fields as $fieldName => $field) {
2670 // Clause for contact-related entities like Email, Relationship, etc.
2671 if (strpos($fieldName, 'contact_id') === 0 && CRM_Utils_Array::value('FKClassName', $field) == 'CRM_Contact_DAO_Contact') {
2672 $clauses[$fieldName] = CRM_Utils_SQL::mergeSubquery('Contact');
2673 }
2674 // Clause for an entity_table/entity_id combo
2675 if ($fieldName == 'entity_id' && isset($fields['entity_table'])) {
2676 $relatedClauses = array();
2677 $relatedEntities = $this->buildOptions('entity_table', 'get');
2678 foreach ((array) $relatedEntities as $table => $ent) {
2679 if (!empty($ent)) {
2680 $ent = CRM_Core_DAO_AllCoreTables::getBriefName(CRM_Core_DAO_AllCoreTables::getClassForTable($table));
2681 $subquery = CRM_Utils_SQL::mergeSubquery($ent);
2682 if ($subquery) {
2683 $relatedClauses[] = "(entity_table = '$table' AND entity_id " . implode(' AND entity_id ', $subquery) . ")";
2684 }
2685 else {
2686 $relatedClauses[] = "(entity_table = '$table')";
2687 }
2688 }
2689 }
2690 if ($relatedClauses) {
2691 $clauses['id'] = 'IN (SELECT id FROM `' . $this->tableName() . '` WHERE (' . implode(') OR (', $relatedClauses) . '))';
2692 }
2693 }
2694 }
2695 CRM_Utils_Hook::selectWhereClause($this, $clauses);
2696 return $clauses;
2697 }
2698
2699 /**
2700 * This returns the final permissioned query string for this entity
2701 *
2702 * With acls from related entities + additional clauses from hook_civicrm_selectWhereClause
2703 *
2704 * @param string $tableAlias
2705 * @return array
2706 */
2707 public static function getSelectWhereClause($tableAlias = NULL) {
2708 $bao = new static();
2709 if ($tableAlias === NULL) {
2710 $tableAlias = $bao->tableName();
2711 }
2712 $clauses = array();
2713 foreach ((array) $bao->addSelectWhereClause() as $field => $vals) {
2714 $clauses[$field] = NULL;
2715 if ($vals) {
2716 $clauses[$field] = "(`$tableAlias`.`$field` IS NULL OR (`$tableAlias`.`$field` " . implode(" AND `$tableAlias`.`$field` ", (array) $vals) . '))';
2717 }
2718 }
2719 return $clauses;
2720 }
2721
2722 /**
2723 * ensure database name is 'safe', i.e. only contains word characters (includes underscores)
2724 * and dashes, and contains at least one [a-z] case insenstive.
2725 *
2726 * @param $database
2727 *
2728 * @return bool
2729 */
2730 public static function requireSafeDBName($database) {
2731 $matches = array();
2732 preg_match(
2733 "/^[\w\-]*[a-z]+[\w\-]*$/i",
2734 $database,
2735 $matches
2736 );
2737 if (empty($matches)) {
2738 return FALSE;
2739 }
2740 return TRUE;
2741 }
2742
2743 /**
2744 * Transform an array to a serialized string for database storage.
2745 *
2746 * @param array|NULL $value
2747 * @param $serializationType
2748 * @return string|NULL
2749 * @throws \Exception
2750 */
2751 public static function serializeField($value, $serializationType) {
2752 if ($value === NULL) {
2753 return NULL;
2754 }
2755 switch ($serializationType) {
2756 case self::SERIALIZE_SEPARATOR_BOOKEND:
2757 return $value === array() ? '' : CRM_Utils_Array::implodePadded($value);
2758
2759 case self::SERIALIZE_SEPARATOR_TRIMMED:
2760 return is_array($value) ? implode(self::VALUE_SEPARATOR, $value) : $value;
2761
2762 case self::SERIALIZE_JSON:
2763 return is_array($value) ? json_encode($value) : $value;
2764
2765 case self::SERIALIZE_PHP:
2766 return is_array($value) ? serialize($value) : $value;
2767
2768 case self::SERIALIZE_COMMA:
2769 return is_array($value) ? implode(',', $value) : $value;
2770
2771 default:
2772 throw new Exception('Unknown serialization method for field.');
2773 }
2774 }
2775
2776 /**
2777 * Transform a serialized string from the database into an array.
2778 *
2779 * @param string|null $value
2780 * @param $serializationType
2781 * @return array|null
2782 * @throws \Exception
2783 */
2784 public static function unSerializeField($value, $serializationType) {
2785 if ($value === NULL) {
2786 return NULL;
2787 }
2788 if ($value === '') {
2789 return array();
2790 }
2791 switch ($serializationType) {
2792 case self::SERIALIZE_SEPARATOR_BOOKEND:
2793 return (array) CRM_Utils_Array::explodePadded($value);
2794
2795 case self::SERIALIZE_SEPARATOR_TRIMMED:
2796 return explode(self::VALUE_SEPARATOR, trim($value));
2797
2798 case self::SERIALIZE_JSON:
2799 return strlen($value) ? json_decode($value, TRUE) : array();
2800
2801 case self::SERIALIZE_PHP:
2802 return strlen($value) ? unserialize($value) : array();
2803
2804 case self::SERIALIZE_COMMA:
2805 return explode(',', trim(str_replace(', ', '', $value)));
2806
2807 default:
2808 throw new Exception('Unknown serialization method for field.');
2809 }
2810 }
2811
2812 }