Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
b081365f CW |
29 | * This api is a simple wrapper of the CiviCRM file DAO. |
30 | * | |
c28e1768 CW |
31 | * Creating and updating files is a complex process and this api is usually insufficient. |
32 | * Use the "Attachment" api instead for more robust file handling. | |
6a488035 | 33 | * |
c28e1768 | 34 | * @fixme no unit tests |
6a488035 | 35 | * @package CiviCRM_APIv3 |
6a488035 TO |
36 | */ |
37 | ||
6a488035 | 38 | /** |
c28e1768 | 39 | * Create a file record. |
b081365f | 40 | * @note This is only one of several steps needed to create a file in CiviCRM. |
c28e1768 | 41 | * Use the "Attachment" api to better handle all steps. |
6a488035 | 42 | * |
cf470720 | 43 | * @param array $params |
2e66abf8 | 44 | * Array per getfields metadata. |
6a488035 | 45 | * |
a6c01b45 | 46 | * @return array |
6a488035 TO |
47 | */ |
48 | function civicrm_api3_file_create($params) { | |
49 | ||
50 | civicrm_api3_verify_mandatory($params, 'CRM_Core_DAO_File', array('uri')); | |
51 | ||
52 | if (!isset($params['upload_date'])) { | |
53 | $params['upload_date'] = date("Ymd"); | |
54 | } | |
55 | ||
6a488035 | 56 | $fileDAO = new CRM_Core_DAO_File(); |
9d32e6f7 EM |
57 | $properties = array( |
58 | 'id', | |
59 | 'file_type_id', | |
60 | 'mime_type', | |
61 | 'uri', | |
62 | 'document', | |
63 | 'description', | |
64 | 'upload_date', | |
65 | ); | |
6a488035 TO |
66 | |
67 | foreach ($properties as $name) { | |
68 | if (array_key_exists($name, $params)) { | |
69 | $fileDAO->$name = $params[$name]; | |
70 | } | |
71 | } | |
72 | ||
73 | $fileDAO->save(); | |
74 | ||
75 | $file = array(); | |
76 | _civicrm_api3_object_to_array($fileDAO, $file); | |
77 | ||
244bbdd8 | 78 | return civicrm_api3_create_success($file, $params, 'File', 'create', $fileDAO); |
6a488035 TO |
79 | } |
80 | ||
81 | /** | |
244bbdd8 | 82 | * Get a File. |
6a488035 | 83 | * |
cf470720 | 84 | * @param array $params |
2e66abf8 | 85 | * Array per getfields metadata. |
6a488035 | 86 | * |
16b10e64 CW |
87 | * @return array |
88 | * Array of all found file object property values. | |
6a488035 TO |
89 | */ |
90 | function civicrm_api3_file_get($params) { | |
6a488035 TO |
91 | return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params); |
92 | } | |
93 | ||
94 | /** | |
244bbdd8 | 95 | * Update an existing File. |
6a488035 | 96 | * |
645ee340 | 97 | * @param array $params |
2e66abf8 | 98 | * Array per getfields metadata. |
9d32e6f7 | 99 | * |
a6c01b45 | 100 | * @return array |
6a488035 | 101 | */ |
7c285037 | 102 | function civicrm_api3_file_update($params) { |
6a488035 TO |
103 | |
104 | if (!isset($params['id'])) { | |
105 | return civicrm_api3_create_error('Required parameter missing'); | |
106 | } | |
107 | ||
6a488035 TO |
108 | $fileDAO = new CRM_Core_DAO_File(); |
109 | $fileDAO->id = $params['id']; | |
110 | if ($fileDAO->find(TRUE)) { | |
111 | $fileDAO->copyValues($params); | |
112 | if (!$params['upload_date'] && !$fileDAO->upload_date) { | |
113 | $fileDAO->upload_date = date("Ymd"); | |
114 | } | |
115 | $fileDAO->save(); | |
116 | } | |
117 | $file = array(); | |
ceef521a CB |
118 | $cloneDAO = clone($fileDAO); |
119 | _civicrm_api3_object_to_array($cloneDAO, $file); | |
6a488035 TO |
120 | return $file; |
121 | } | |
122 | ||
123 | /** | |
244bbdd8 | 124 | * Delete an existing File. |
6a488035 | 125 | * |
c490a46a | 126 | * @param array $params |
2e66abf8 | 127 | * Array per getfields metadata. |
8089541a | 128 | * @return array API Result Array |
8089541a | 129 | * @throws API_Exception |
6a488035 TO |
130 | */ |
131 | function civicrm_api3_file_delete($params) { | |
132 | ||
133 | civicrm_api3_verify_mandatory($params, NULL, array('id')); | |
7b8bc7a1 SB |
134 | if (CRM_Core_BAO_File::deleteEntityFile('*', $params['id'])) { |
135 | return civicrm_api3_create_success(); | |
136 | } | |
137 | else { | |
138 | throw new API_Exception('Error while deleting a file.'); | |
139 | } | |
22fd1690 | 140 | } |