CRM-13822 - Ensure mysql phone function exists
authorColeman Watts <coleman@civicrm.org>
Mon, 25 Nov 2013 04:53:14 +0000 (20:53 -0800)
committerColeman Watts <coleman@civicrm.org>
Mon, 25 Nov 2013 04:53:33 +0000 (20:53 -0800)
CRM/Core/BAO/Phone.php
CRM/Core/DAO.php

index 14572de4e2d1a81a5058688c54ebb76d154d0218..e12d84c43581eb86019bad6ddbdef050430af999 100644 (file)
@@ -45,6 +45,9 @@ class CRM_Core_BAO_Phone extends CRM_Core_DAO_Phone {
    * @param array $params input parameters
    */
   static function create($params) {
+    // Ensure mysql phone function exists
+    CRM_Core_DAO::checkSqlFunctionsExist();
+
     if (is_numeric(CRM_Utils_Array::value('is_primary', $params)) ||
       // if id is set & is_primary isn't we can assume no change
       empty($params['id'])
@@ -66,6 +69,9 @@ class CRM_Core_BAO_Phone extends CRM_Core_DAO_Phone {
    * @static
    */
   static function add(&$params) {
+    // Ensure mysql phone function exists
+    CRM_Core_DAO::checkSqlFunctionsExist();
+
     $hook = empty($params['id']) ? 'create' : 'edit';
     CRM_Utils_Hook::pre($hook, 'Phone', CRM_Utils_Array::value('id', $params), $params);
 
@@ -229,6 +235,8 @@ ORDER BY ph.is_primary DESC, phone_id ASC ";
     if (!$optionId) {
       return;
     }
+    // Ensure mysql phone function exists
+    CRM_Core_DAO::checkSqlFunctionsExist();
 
     $tables = array(
       'civicrm_phone',
@@ -252,6 +260,8 @@ ORDER BY ph.is_primary DESC, phone_id ASC ";
    * Call common delete function
    */
   static function del($id) {
+    // Ensure mysql phone function exists
+    CRM_Core_DAO::checkSqlFunctionsExist();
     return CRM_Contact_BAO_Contact::deleteObjectWithPrimary('Phone', $id);
   }
 }
index c03f87f9ee2fb1d2c780af0f8a572de62cb40ba2..214a9378464f76f56e1ddc6a31a354efbc318fef 100644 (file)
@@ -67,6 +67,8 @@ class CRM_Core_DAO extends DB_DataObject {
    */
   static $_factory = NULL;
 
+  static $_checkedSqlFunctionsExist = FALSE;
+
   /**
    * Class constructor
    *
@@ -1558,6 +1560,23 @@ SELECT contact_id
     self::createTriggers($info);
   }
 
+  /**
+   * Because sql functions are sometimes lost, esp during db migration, we check here to avoid numerous support requests
+   * @see http://issues.civicrm.org/jira/browse/CRM-13822
+   * TODO: Alternative solutions might be
+   *  * Stop using functions and find another way to strip numeric characters from phones
+   *  * Give better error messages (currently a missing fn fatals with "unknown error")
+   */
+  static function checkSqlFunctionsExist() {
+    if (!self::$_checkedSqlFunctionsExist) {
+      self::$_checkedSqlFunctionsExist = TRUE;
+      $dao = CRM_Core_DAO::executeQuery("SHOW function status WHERE db = database() AND name = 'civicrm_strip_non_numeric'");
+      if (!$dao->fetch()) {
+        self::triggerRebuild();
+      }
+    }
+  }
+
   /**
    * Wrapper function to drop triggers
    *