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