Merge pull request #17522 from seamuslee001/remove_deprecated_methods
[civicrm-core.git] / api / v3 / File.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * This api is a simple wrapper of the CiviCRM file DAO.
14 *
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.
17 *
18 * @fixme no unit tests
19 * @package CiviCRM_APIv3
20 */
21
22 /**
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.
26 *
27 * @param array $params
28 * Array per getfields metadata.
29 *
30 * @return array
31 */
32 function civicrm_api3_file_create($params) {
33
34 civicrm_api3_verify_mandatory($params, 'CRM_Core_DAO_File', ['uri']);
35
36 if (!isset($params['upload_date'])) {
37 $params['upload_date'] = date("Ymd");
38 }
39
40 $fileDAO = new CRM_Core_DAO_File();
41 $properties = [
42 'id',
43 'file_type_id',
44 'mime_type',
45 'uri',
46 'document',
47 'description',
48 'upload_date',
49 ];
50
51 foreach ($properties as $name) {
52 if (array_key_exists($name, $params)) {
53 $fileDAO->$name = $params[$name];
54 }
55 }
56
57 $fileDAO->save();
58
59 $file = [];
60 _civicrm_api3_object_to_array($fileDAO, $file);
61
62 return civicrm_api3_create_success($file, $params, 'File', 'create', $fileDAO);
63 }
64
65 /**
66 * Get a File.
67 *
68 * @param array $params
69 * Array per getfields metadata.
70 *
71 * @return array
72 * Array of all found file object property values.
73 */
74 function civicrm_api3_file_get($params) {
75 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
76 }
77
78 /**
79 * Update an existing File.
80 *
81 * @param array $params
82 * Array per getfields metadata.
83 *
84 * @return array
85 */
86 function civicrm_api3_file_update($params) {
87
88 if (!isset($params['id'])) {
89 return civicrm_api3_create_error('Required parameter missing');
90 }
91
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");
98 }
99 $fileDAO->save();
100 }
101 $file = [];
102 $cloneDAO = clone($fileDAO);
103 _civicrm_api3_object_to_array($cloneDAO, $file);
104 return $file;
105 }
106
107 /**
108 * Delete an existing File.
109 *
110 * @param array $params
111 * Array per getfields metadata.
112 * @return array API Result Array
113 * @throws API_Exception
114 */
115 function civicrm_api3_file_delete($params) {
116
117 civicrm_api3_verify_mandatory($params, NULL, ['id']);
118 if (CRM_Core_BAO_File::deleteEntityFile('*', $params['id'])) {
119 return civicrm_api3_create_success();
120 }
121 else {
122 throw new API_Exception('Error while deleting a file.');
123 }
124 }