b0867261e88486b405cd99edd67b3b0b04d36b67
[civicrm-core.git] / api / v3 / File.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 /**
29 *
30 * Definition of the Tag of the CRM API.
31 * More detailed documentation can be found
32 * {@link http://objectledge.org/confluence/display/CRM/CRM+v1.0+Public+APIs
33 * here}
34 *
35 * @package CiviCRM_APIv3
36 * @subpackage API_File
37 * @copyright CiviCRM LLC (c) 2004-2014
38 * $Id: $
39 *
40 */
41
42 /**
43 * Create a file
44 *
45 * This API is used for creating a file
46 *
47 * @param array $params
48 * An associative array of name/value property values of civicrm_file.
49 *
50 * @return array
51 * Array of newly created file property values.
52 * @access public
53 */
54 function civicrm_api3_file_create($params) {
55
56 civicrm_api3_verify_mandatory($params, 'CRM_Core_DAO_File', array('uri'));
57
58 if (!isset($params['upload_date'])) {
59 $params['upload_date'] = date("Ymd");
60 }
61
62 $fileDAO = new CRM_Core_DAO_File();
63 $properties = array('id', 'file_type_id', 'mime_type', 'uri', 'document', 'description', 'upload_date');
64
65 foreach ($properties as $name) {
66 if (array_key_exists($name, $params)) {
67 $fileDAO->$name = $params[$name];
68 }
69 }
70
71 $fileDAO->save();
72
73 $file = array();
74 _civicrm_api3_object_to_array($fileDAO, $file);
75
76 return civicrm_api3_create_success($file, $params, 'file', 'create', $fileDAO);
77 }
78
79 /**
80 * Get a file.
81 *
82 * This api is used for finding an existing file.
83 * Required parameters : id OR file_type_id of a file
84 *
85 * @param array $params
86 * An associative array of name/value property values of civicrm_file.
87 *
88 * @return array
89 * Array of all found file object property values.
90 * @access public
91 */
92 function civicrm_api3_file_get($params) {
93 civicrm_api3_verify_one_mandatory($params);
94 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
95 }
96
97 /**
98 * Update an existing file
99 *
100 * This api is used for updating an existing file.
101 * Required parameters : id of a file
102 *
103 * @param array $params
104 * @return array
105 */
106 function civicrm_api3_file_update($params) {
107
108 if (!isset($params['id'])) {
109 return civicrm_api3_create_error('Required parameter missing');
110 }
111
112 $fileDAO = new CRM_Core_DAO_File();
113 $fileDAO->id = $params['id'];
114 if ($fileDAO->find(TRUE)) {
115 $fileDAO->copyValues($params);
116 if (!$params['upload_date'] && !$fileDAO->upload_date) {
117 $fileDAO->upload_date = date("Ymd");
118 }
119 $fileDAO->save();
120 }
121 $file = array();
122 _civicrm_api3_object_to_array(clone($fileDAO), $file);
123 return $file;
124 }
125
126 /**
127 * Deletes an existing file
128 *
129 * This API is used for deleting a file
130 * Required parameters : id of a file
131 *
132 * @param array $params
133 *
134 * @return array
135 * API result array
136 * @access public
137 */
138 function civicrm_api3_file_delete($params) {
139
140 civicrm_api3_verify_mandatory($params, NULL, array('id'));
141
142 $check = FALSE;
143
144 $entityFileDAO = new CRM_Core_DAO_EntityFile();
145 $entityFileDAO->file_id = $params['id'];
146 if ($entityFileDAO->find()) {
147 $check = $entityFileDAO->delete();
148 }
149
150 $fileDAO = new CRM_Core_DAO_File();
151 $fileDAO->id = $params['id'];
152 if ($fileDAO->find(TRUE)) {
153 $check = $fileDAO->delete();
154 }
155
156 return $check ? NULL : civicrm_api3_create_error('Error while deleting a file.');
157 }