Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
a30c801b | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
a30c801b TO |
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 | | |
6a488035 TO |
9 | +--------------------------------------------------------------------+ |
10 | */ | |
11 | ||
12 | /** | |
b081365f CW |
13 | * This api is a simple wrapper of the CiviCRM file DAO. |
14 | * | |
c28e1768 CW |
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. | |
6a488035 | 17 | * |
c28e1768 | 18 | * @fixme no unit tests |
6a488035 | 19 | * @package CiviCRM_APIv3 |
6a488035 TO |
20 | */ |
21 | ||
6a488035 | 22 | /** |
c28e1768 | 23 | * Create a file record. |
b081365f | 24 | * @note This is only one of several steps needed to create a file in CiviCRM. |
c28e1768 | 25 | * Use the "Attachment" api to better handle all steps. |
6a488035 | 26 | * |
cf470720 | 27 | * @param array $params |
2e66abf8 | 28 | * Array per getfields metadata. |
6a488035 | 29 | * |
a6c01b45 | 30 | * @return array |
6a488035 TO |
31 | */ |
32 | function civicrm_api3_file_create($params) { | |
33 | ||
cf8f0fff | 34 | civicrm_api3_verify_mandatory($params, 'CRM_Core_DAO_File', ['uri']); |
6a488035 TO |
35 | |
36 | if (!isset($params['upload_date'])) { | |
37 | $params['upload_date'] = date("Ymd"); | |
38 | } | |
39 | ||
6a488035 | 40 | $fileDAO = new CRM_Core_DAO_File(); |
cf8f0fff | 41 | $properties = [ |
9d32e6f7 EM |
42 | 'id', |
43 | 'file_type_id', | |
44 | 'mime_type', | |
45 | 'uri', | |
46 | 'document', | |
47 | 'description', | |
48 | 'upload_date', | |
cf8f0fff | 49 | ]; |
6a488035 TO |
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 | ||
cf8f0fff | 59 | $file = []; |
6a488035 TO |
60 | _civicrm_api3_object_to_array($fileDAO, $file); |
61 | ||
244bbdd8 | 62 | return civicrm_api3_create_success($file, $params, 'File', 'create', $fileDAO); |
6a488035 TO |
63 | } |
64 | ||
65 | /** | |
244bbdd8 | 66 | * Get a File. |
6a488035 | 67 | * |
cf470720 | 68 | * @param array $params |
2e66abf8 | 69 | * Array per getfields metadata. |
6a488035 | 70 | * |
16b10e64 CW |
71 | * @return array |
72 | * Array of all found file object property values. | |
6a488035 TO |
73 | */ |
74 | function civicrm_api3_file_get($params) { | |
6a488035 TO |
75 | return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params); |
76 | } | |
77 | ||
78 | /** | |
244bbdd8 | 79 | * Update an existing File. |
6a488035 | 80 | * |
645ee340 | 81 | * @param array $params |
2e66abf8 | 82 | * Array per getfields metadata. |
9d32e6f7 | 83 | * |
a6c01b45 | 84 | * @return array |
6a488035 | 85 | */ |
7c285037 | 86 | function civicrm_api3_file_update($params) { |
6a488035 TO |
87 | |
88 | if (!isset($params['id'])) { | |
89 | return civicrm_api3_create_error('Required parameter missing'); | |
90 | } | |
91 | ||
6a488035 TO |
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 | } | |
cf8f0fff | 101 | $file = []; |
ceef521a CB |
102 | $cloneDAO = clone($fileDAO); |
103 | _civicrm_api3_object_to_array($cloneDAO, $file); | |
6a488035 TO |
104 | return $file; |
105 | } | |
106 | ||
107 | /** | |
244bbdd8 | 108 | * Delete an existing File. |
6a488035 | 109 | * |
c490a46a | 110 | * @param array $params |
2e66abf8 | 111 | * Array per getfields metadata. |
8089541a | 112 | * @return array API Result Array |
8089541a | 113 | * @throws API_Exception |
6a488035 TO |
114 | */ |
115 | function civicrm_api3_file_delete($params) { | |
116 | ||
cf8f0fff | 117 | civicrm_api3_verify_mandatory($params, NULL, ['id']); |
7b8bc7a1 SB |
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 | } | |
22fd1690 | 124 | } |