Merge pull request #353 from eileenmcnaughton/trunk-kill-eval
[civicrm-core.git] / api / v2 / File.php
CommitLineData
6a488035
TO
1<?php
2// $Id$
3
4/*
5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.3 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2013 |
9 +--------------------------------------------------------------------+
10 | This file is a part of CiviCRM. |
11 | |
12 | CiviCRM is free software; you can copy, modify, and distribute it |
13 | under the terms of the GNU Affero General Public License |
14 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | |
16 | CiviCRM is distributed in the hope that it will be useful, but |
17 | WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
19 | See the GNU Affero General Public License for more details. |
20 | |
21 | You should have received a copy of the GNU Affero General Public |
22 | License and the CiviCRM Licensing Exception along |
23 | with this program; if not, contact CiviCRM LLC |
24 | at info[AT]civicrm[DOT]org. If you have questions about the |
25 | GNU Affero General Public License or the licensing of CiviCRM, |
26 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
27 +--------------------------------------------------------------------+
28*/
29
30/**
31 *
32 * Definition of the Tag of the CRM API.
33 * More detailed documentation can be found
34 * {@link http://objectledge.org/confluence/display/CRM/CRM+v1.0+Public+APIs
35 * here}
36 *
37 * @package CiviCRM_APIv2
38 * @subpackage API_File
39 * @copyright CiviCRM LLC (c) 2004-2013
40 * $Id: $
41 *
42 */
43
44/**
45 * Files required for this package
46 */
47require_once 'api/v2/utils.php';
48
49/**
50 * Create a file
51 *
52 * This API is used for creating a file
53 *
54 * @param array $params an associative array of name/value property values of civicrm_file
55 *
56 * @return array of newly created file property values.
57 * @access public
58 */
59function civicrm_file_create($params) {
60 if (!is_array($params)) {
61 return civicrm_create_error('Params is not an array.');
62 }
63
64 if (!isset($params['file_type_id'])) {
65 return civicrm_create_error('Required parameter missing.');
66 }
67
68 if (!isset($params['upload_date'])) {
69 $params['upload_date'] = date("Ymd");
70 }
71
72 require_once 'CRM/Core/DAO/File.php';
73
74 $fileDAO = new CRM_Core_DAO_File();
75 $properties = array('id', 'file_type_id', 'mime_type', 'uri', 'document', 'description', 'upload_date');
76
77 foreach ($properties as $name) {
78 if (array_key_exists($name, $params)) {
79 $fileDAO->$name = $params[$name];
80 }
81 }
82
83 $fileDAO->save();
84
85 $file = array();
86 _civicrm_object_to_array($fileDAO, $file);
87
88 return $file;
89}
90
91/**
92 * Get a file.
93 *
94 * This api is used for finding an existing file.
95 * Required parameters : id OR file_type_id of a file
96 *
97 * @param array $params an associative array of name/value property values of civicrm_file
98 *
99 * @return Array of all found file object property values.
100 * @access public
101 */
102function civicrm_file_get($params) {
103 if (!is_array($params)) {
104 return civicrm_create_error('params is not an array.');
105 }
106
107 if (!isset($params['id']) && !isset($params['file_type_id'])) {
108 return civicrm_create_error('Required parameters missing.');
109 }
110
111 require_once 'CRM/Core/DAO/File.php';
112 $fileDAO = new CRM_Core_DAO_File();
113
114 $properties = array('id', 'file_type_id', 'mime_type', 'uri', 'document', 'description', 'upload_date');
115
116 foreach ($properties as $name) {
117 if (array_key_exists($name, $params)) {
118 $fileDAO->$name = $params[$name];
119 }
120 }
121
122 if ($fileDAO->find()) {
123 $file = array();
124 while ($fileDAO->fetch()) {
125 _civicrm_object_to_array(clone($fileDAO), $file);
126 $files[$fileDAO->id] = $file;
127 }
128 }
129 else {
130 return civicrm_create_error('Exact match not found');
131 }
132 return $files;
133}
134
135/**
136 * Update an existing file
137 *
138 * This api is used for updating an existing file.
139 * Required parrmeters : id of a file
140 *
141 * @param Array $params an associative array of name/value property values of civicrm_file
142 *
143 * @return array of updated file object property values
144 * @access public
145 */
146function &civicrm_file_update(&$params) {
147 if (!is_array($params)) {
148 return civicrm_create_error('Params is not an array');
149 }
150
151 if (!isset($params['id'])) {
152 return civicrm_create_error('Required parameter missing');
153 }
154
155 require_once 'CRM/Core/DAO/File.php';
156 $fileDAO = new CRM_Core_DAO_File();
157 $fileDAO->id = $params['id'];
158 if ($fileDAO->find(TRUE)) {
159 $fileDAO->copyValues($params);
160 if (!$params['upload_date'] && !$fileDAO->upload_date) {
161 $fileDAO->upload_date = date("Ymd");
162 }
163 $fileDAO->save();
164 }
165 $file = array();
166 _civicrm_object_to_array(clone($fileDAO), $file);
167 return $file;
168}
169
170/**
171 * Deletes an existing file
172 *
173 * This API is used for deleting a file
174 * Required parameters : id of a file
175 *
176 * @param Int $fileId Id of the file to be deleted
177 *
178 * @return null if successfull, object of CRM_Core_Error otherwise
179 * @access public
180 */
181function &civicrm_file_delete($fileId) {
182 if (empty($fileId)) {
183 return civicrm_create_error('Required parameter missing');
184 }
185
186 $check = FALSE;
187
188 require_once 'CRM/Core/DAO/EntityFile.php';
189 $entityFileDAO = new CRM_Core_DAO_EntityFile();
190 $entityFileDAO->file_id = $fileId;
191 if ($entityFileDAO->find()) {
192 $check = $entityFileDAO->delete();
193 }
194
195 require_once 'CRM/Core/DAO/File.php';
196 $fileDAO = new CRM_Core_DAO_File();
197 $fileDAO->id = $fileId;
198 if ($fileDAO->find(TRUE)) {
199 $check = $fileDAO->delete();
200 }
201
202 return $check ? NULL : civicrm_create_error('Error while deleting a file.');
203}
204
205/**
206 * Assigns an entity to a file
207 *
208 * @param object $file id of a file
209 * @param object $entity id of a entity
210 * @param string $entity_table
211 *
212 * @return array of newly created entity-file object properties
213 * @access public
214 */
215function civicrm_entity_file_create(&$fileID, &$entityID, $entity_table = 'civicrm_contact') {
216 require_once 'CRM/Core/DAO/EntityFile.php';
217
218 if (!$fileID || !$entityID) {
219 return civicrm_create_error('Required parameters missing');
220 }
221
222 $params = array(
223 'entity_id' => $entityID,
224 'file_id' => $fileID,
225 'entity_table' => $entity_table,
226 );
227
228 $entityFileDAO = new CRM_Core_DAO_EntityFile();
229 $entityFileDAO->copyValues($params);
230 $entityFileDAO->save();
231
232 $entityFile = array();
233 _civicrm_object_to_array($entityFileDAO, $entityFile);
234
235 return $entityFile;
236}
237
238/**
239 * Attach a file to a given entity
240 *
241 * @param string $name filename
242 * @param object $entityID id of the supported entity.
243 * @param string $entity_table
244 *
245 * @access public
246 */
247function civicrm_file_by_entity_add($name, $entityID, $entityTable = 'civicrm_contact', $params) {
248 require_once 'CRM/Core/BAO/File.php';
249 CRM_Core_BAO_File::filePostProcess($name, NULL, $entityTable, $entityID, NULL, FALSE, $params);
250}
251
252/**
253 * Returns all files assigned to a single entity instance.
254 *
255 * @param object $entityID id of the supported entity.
256 * @param string $entity_table
257 *
258 * @return array nested array of entity-file property values.
259 * @access public
260 */
261function civicrm_files_by_entity_get($entityID, $entityTable = 'civicrm_contact', $fileID = NULL) {
262 if (!$entityID) {
263 return civicrm_create_error('Required parameters missing');
264 }
265
266 require_once 'CRM/Core/DAO/EntityFile.php';
267 require_once 'CRM/Core/DAO/File.php';
268
269 $entityFileDAO = new CRM_Core_DAO_EntityFile();
270 $entityFileDAO->entity_table = $entityTable;
271 $entityFileDAO->entity_id = $entityID;
272 if ($fileID) {
273 $entityFileDAO->file_id = $fileID;
274 }
275 if ($entityFileDAO->find()) {
276 $entityFile = array();
277 while ($entityFileDAO->fetch()) {
278 _civicrm_object_to_array($entityFileDAO, $entityFile);
279 $files[$entityFileDAO->file_id] = $entityFile;
280
281 if (array_key_exists('file_id', $files[$entityFileDAO->file_id])) {
282 $fileDAO = new CRM_Core_DAO_File();
283 $fileDAO->id = $entityFile['file_id'];
284 $fileDAO->find(TRUE);
285 _civicrm_object_to_array($fileDAO, $files[$entityFileDAO->file_id]);
286 }
287
288 if (CRM_Utils_Array::value('file_type_id', $files[$entityFileDAO->file_id])) {
289 $files[$entityFileDAO->file_id]['file_type'] = CRM_Core_OptionGroup::getLabel('file_type',
290 $files[$entityFileDAO->file_id]['file_type_id']
291 );
292 }
293 }
294 }
295 else {
296 return civicrm_create_error('Exact match not found');
297 }
298
299 return $files;
300}
301
302/**
303 * Deletes an existing entity file assignment.
304 * Required parameters : 1. id of an entity-file
305 * 2. entity_id and entity_table of an entity-file
306 *
307 * @param array $params an associative array of name/value property values of civicrm_entity_file.
308 *
309 * @return null if successfull, object of CRM_Core_Error otherwise
310 * @access public
311 */
312function civicrm_entity_file_delete(&$params) {
313 require_once 'CRM/Core/DAO/EntityFile.php';
314
315 //if ( ! isset($params['id']) && ( !isset($params['entity_id']) || !isset($params['entity_file']) ) ) {
316 if (!isset($params['id']) && (!isset($params['entity_id']) || !isset($params['entity_table']))) {
317 return civicrm_create_error('Required parameters missing');
318 }
319
320 $entityFileDAO = new CRM_Core_DAO_EntityFile();
321
322 $properties = array('id', 'entity_id', 'entity_table', 'file_id');
323 foreach ($properties as $name) {
324 if (array_key_exists($name, $params)) {
325 $entityFileDAO->$name = $params[$name];
326 }
327 }
328
329 return $entityFileDAO->delete() ? NULL : civicrm_create_error('Error while deleting');
330}
331