3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * This api is a simple wrapper of the CiviCRM file DAO.
15 * Creating and updating files is a complex process and this api is usually insufficient.
16 * Use the "Attachment" api instead for more robust file handling.
18 * @fixme no unit tests
19 * @package CiviCRM_APIv3
23 * Create a file record.
24 * @note This is only one of several steps needed to create a file in CiviCRM.
25 * Use the "Attachment" api to better handle all steps.
27 * @param array $params
28 * Array per getfields metadata.
32 function civicrm_api3_file_create($params) {
34 civicrm_api3_verify_mandatory($params, 'CRM_Core_DAO_File', ['uri']);
36 if (!isset($params['upload_date'])) {
37 $params['upload_date'] = date("Ymd");
40 $fileDAO = new CRM_Core_DAO_File();
51 foreach ($properties as $name) {
52 if (array_key_exists($name, $params)) {
53 $fileDAO->$name = $params[$name];
60 _civicrm_api3_object_to_array($fileDAO, $file);
62 return civicrm_api3_create_success($file, $params, 'File', 'create', $fileDAO);
68 * @param array $params
69 * Array per getfields metadata.
72 * Array of all found file object property values.
74 function civicrm_api3_file_get($params) {
75 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__
), $params);
79 * Update an existing File.
81 * @param array $params
82 * Array per getfields metadata.
86 function civicrm_api3_file_update($params) {
88 if (!isset($params['id'])) {
89 return civicrm_api3_create_error('Required parameter missing');
92 $fileDAO = new CRM_Core_DAO_File();
93 $fileDAO->id
= $params['id'];
94 if ($fileDAO->find(TRUE)) {
95 $fileDAO->copyValues($params);
96 if (!$params['upload_date'] && !$fileDAO->upload_date
) {
97 $fileDAO->upload_date
= date("Ymd");
102 $cloneDAO = clone($fileDAO);
103 _civicrm_api3_object_to_array($cloneDAO, $file);
108 * Delete an existing File.
110 * @param array $params
111 * Array per getfields metadata.
112 * @return array API Result Array
113 * @throws API_Exception
115 function civicrm_api3_file_delete($params) {
117 civicrm_api3_verify_mandatory($params, NULL, ['id']);
118 if (CRM_Core_BAO_File
::deleteEntityFile('*', $params['id'])) {
119 return civicrm_api3_create_success();
122 throw new API_Exception('Error while deleting a file.');