From f1fd7c0341f64b91c7879cbe47a0b95269c3d57d Mon Sep 17 00:00:00 2001 From: "deb.monish" Date: Wed, 18 Apr 2018 17:33:46 +0530 Subject: [PATCH] set default created_id value to BAO --- CRM/Core/BAO/File.php | 21 +++++++++++++++++++++ CRM/Upgrade/Incremental/php/FiveTwo.php | 4 ++-- api/v3/Attachment.php | 10 +++++----- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/CRM/Core/BAO/File.php b/CRM/Core/BAO/File.php index 424d40682f71..360a1b24ce08 100644 --- a/CRM/Core/BAO/File.php +++ b/CRM/Core/BAO/File.php @@ -40,6 +40,27 @@ class CRM_Core_BAO_File extends CRM_Core_DAO_File { static $_signableFields = array('entityTable', 'entityID', 'fileID'); + /** + * Takes an associative array and creates a File object. + * + * @param array $params + * (reference ) an assoc array of name/value pairs. + * + * @return CRM_Core_BAO_File + */ + public static function create($params) { + $fileDAO = new CRM_Core_DAO_File(); + $fileDAO->copyValues($params); + + if (empty($params['id']) && empty($params['created_id'])) { + $fileDAO->created_id = CRM_Core_Session::getLoggedInContactID(); + } + + $fileDAO->save(); + + return $fileDAO; + } + /** * @param int $fileID * @param int $entityID diff --git a/CRM/Upgrade/Incremental/php/FiveTwo.php b/CRM/Upgrade/Incremental/php/FiveTwo.php index 85949e553f4c..45fbb73918e4 100644 --- a/CRM/Upgrade/Incremental/php/FiveTwo.php +++ b/CRM/Upgrade/Incremental/php/FiveTwo.php @@ -84,8 +84,8 @@ public static function addFileCreatedIdColumn(CRM_Queue_TaskContext $ctx) { ON UPDATE CASCADE; "); - return TRUE; - } + return TRUE; + } /* * Important! All upgrade functions MUST add a 'runSql' task. diff --git a/api/v3/Attachment.php b/api/v3/Attachment.php index e889cb3272a6..70aa4c6a72aa 100644 --- a/api/v3/Attachment.php +++ b/api/v3/Attachment.php @@ -117,7 +117,8 @@ function civicrm_api3_attachment_create($params) { $entityFileDao = new CRM_Core_DAO_EntityFile(); if ($id) { - $fileDao->id = $id; + $file['id'] = $fileDao->id = $id; + if (!$fileDao->find(TRUE)) { throw new API_Exception("Invalid ID"); } @@ -144,11 +145,11 @@ function civicrm_api3_attachment_create($params) { throw new API_Exception("Cannot modify name"); } - $fileDao->copyValues($file); if (!$id) { - $fileDao->uri = CRM_Utils_File::makeFileName($name); + $file['uri'] = CRM_Utils_File::makeFileName($name); } - $fileDao->save(); + $fileDao = CRM_Core_BAO_File::create($file); + $fileDao->find(TRUE); $entityFileDao->copyValues($entityFile); $entityFileDao->file_id = $fileDao->id; @@ -499,7 +500,6 @@ function _civicrm_api3_attachment_getfields() { 'title' => 'Created By Contact ID', 'type' => CRM_Utils_Type::T_INT, 'description' => 'FK to civicrm_contact, who uploaded this file', - 'api.default' => 'user_contact_id', ); return $spec;