province abbreviation patch - issue 724
[civicrm-core.git] / CRM / Utils / SQL.php
index 3b9167e6265f5301a81023024ff084d4134dbce2..1ca298e96ad37d881c25c64cde4aeed53709bb33 100644 (file)
@@ -203,4 +203,30 @@ class CRM_Utils_SQL {
     return $dsn;
   }
 
+  /**
+   * Filter out Emojis in where clause if the database (determined by checking the create table for civicrm_contact)
+   * cannot support emojis
+   * @param mixed $criteria - filter criteria to check
+   *
+   * @return bool|string
+   */
+  public static function handleEmojiInQuery($criteria) {
+    if (!CRM_Core_BAO_SchemaHandler::databaseSupportsUTF8MB4()) {
+      foreach ((array) $criteria as $criterion) {
+        if (!empty($criterion) && !is_numeric($criterion)
+          // The first 2 criteria are redundant but are added as they
+          // seem like they would
+          // be quicker than this 3rd check.
+          && max(array_map('ord', str_split($criterion))) >= 240) {
+          // String contains unsupported emojis.
+          // We return a clause that resolves to false as an emoji string by definition cannot be saved.
+          // note that if we return just 0 for false if gets lost in empty checks.
+          // https://stackoverflow.com/questions/16496554/can-php-detect-4-byte-encoded-utf8-chars
+          return '0 = 1';
+        }
+      }
+      return TRUE;
+    }
+  }
+
 }