Update Copywrite year to be 2019
[civicrm-core.git] / CRM / Core / DAO.php
index 3416e61a70d662aa47f4f5ae51f663c8ffcf06af..b130576df270947efb7909e5d1626fb1381b07fa 100644 (file)
@@ -3,7 +3,7 @@
   +--------------------------------------------------------------------+
   | CiviCRM version 5                                                  |
   +--------------------------------------------------------------------+
-  | Copyright CiviCRM LLC (c) 2004-2018                                |
+  | Copyright CiviCRM LLC (c) 2004-2019                                |
   +--------------------------------------------------------------------+
   | This file is a part of CiviCRM.                                    |
   |                                                                    |
@@ -31,7 +31,7 @@
  * All DAO classes should inherit from this class.
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2018
+ * @copyright CiviCRM LLC (c) 2004-2019
  */
 
 if (!defined('DB_DSN_MODE')) {
@@ -696,6 +696,14 @@ class CRM_Core_DAO extends DB_DataObject {
           if (!$serializeArrays && is_array($pValue) && !empty($value['serialize'])) {
             Civi::log()->warning(ts('use copyParams to serialize arrays (' . __CLASS__ . '.' . $name . ')'), ['civi.tag' => 'deprecated']);
           }
+          $maxLength = CRM_Utils_Array::value('maxlength', $value);
+          if (!is_array($pValue) && $maxLength && mb_strlen($pValue) > $maxLength
+            && empty($value['pseudoconstant'])
+          ) {
+            Civi::log()->warning(ts('A string for field $dbName has been truncated. The original string was %1', [CRM_Utils_Type::escape($pValue, 'String')]));
+            // The string is too long - what to do what to do? Well losing data is generally bad so lets' truncate
+            $pValue = CRM_Utils_String::ellipsify($pValue, $maxLength);
+          }
           $this->$dbName = $pValue;
           $allNull = FALSE;
         }
@@ -849,6 +857,9 @@ class CRM_Core_DAO extends DB_DataObject {
   /**
    * Check if there is a given column in a specific table.
    *
+   * @deprecated
+   * @see CRM_Core_BAO_SchemaHandler::checkIfFieldExists
+   *
    * @param string $tableName
    * @param string $columnName
    * @param bool $i18nRewrite
@@ -858,16 +869,7 @@ class CRM_Core_DAO extends DB_DataObject {
    *   true if exists, else false
    */
   public static function checkFieldExists($tableName, $columnName, $i18nRewrite = TRUE) {
-    $query = "
-SHOW COLUMNS
-FROM $tableName
-LIKE %1
-";
-    $params = array(1 => array($columnName, 'String'));
-    $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, NULL, FALSE, $i18nRewrite);
-    $result = $dao->fetch() ? TRUE : FALSE;
-    $dao->free();
-    return $result;
+    return CRM_Core_BAO_SchemaHandler::checkIfFieldExists($tableName, $columnName, $i18nRewrite);
   }
 
   /**
@@ -888,7 +890,6 @@ LIKE %1
     while ($dao->fetch()) {
       $values[] = $dao->TABLE_NAME;
     }
-    $dao->free();
     return $values;
   }
 
@@ -906,6 +907,7 @@ LIKE %1
          AND TABLE_NAME LIKE 'civicrm_%'
          AND TABLE_NAME NOT LIKE 'civicrm_import_job_%'
          AND TABLE_NAME NOT LIKE '%_temp%'
+         AND TABLE_NAME NOT LIKE 'civicrm_tmp_%'
       ");
   }
 
@@ -939,7 +941,6 @@ LIKE %1
         CRM_Core_Error::fatal();
       }
 
-      $dao->free();
       $show[$tableName] = $dao->Create_Table;
     }
 
@@ -967,7 +968,6 @@ LIKE %1
           CRM_Core_Error::fatal();
         }
 
-        $dao->free();
         $show[$tableName] = $dao->Create_Table;
       }
 
@@ -1003,7 +1003,6 @@ LIKE %1
         CRM_Core_Error::fatal();
       }
 
-      $dao->free();
       $show[$tableName] = $dao->Create_Table;
     }
     $constraint = "`FK_{$tableName}_{$columnName}`";
@@ -1025,7 +1024,6 @@ LIKE %1
     $query = "SELECT * FROM $tableName WHERE $columnName != '$columnValue'";
     $dao = CRM_Core_DAO::executeQuery($query);
     $result = $dao->fetch() ? FALSE : TRUE;
-    $dao->free();
     return $result;
   }
 
@@ -1042,7 +1040,6 @@ LIKE %1
     $query = "SELECT * FROM $tableName WHERE $columnName IS NOT NULL";
     $dao = CRM_Core_DAO::executeQuery($query);
     $result = $dao->fetch() ? FALSE : TRUE;
-    $dao->free();
     return $result;
   }
 
@@ -1063,7 +1060,6 @@ LIKE %1
 
     $dao = CRM_Core_DAO::executeQuery($query, $params);
     $result = $dao->fetch() ? TRUE : FALSE;
-    $dao->free();
     return $result;
   }
 
@@ -1430,7 +1426,6 @@ FROM   civicrm_domain
     ) {
       // we typically do this for insert/update/delete statements OR if explicitly asked to
       // free the dao
-      $dao->free();
     }
     return $dao;
   }
@@ -1676,7 +1671,9 @@ FROM   civicrm_domain
         }
       }
       $newObject->save();
+      CRM_Utils_Hook::post('create', CRM_Core_DAO_AllCoreTables::getBriefName($daoName), $newObject->id, $newObject);
     }
+
     return $newObject;
   }