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