Merge pull request #4896 from totten/master-movedep
[civicrm-core.git] / api / v3 / File.php
CommitLineData
6a488035 1<?php
6a488035
TO
2
3/*
4 +--------------------------------------------------------------------+
39de6fd5 5 | CiviCRM version 4.6 |
6a488035 6 +--------------------------------------------------------------------+
731a0992 7 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28
29/**
30 *
31 * Definition of the Tag of the CRM API.
32 * More detailed documentation can be found
33 * {@link http://objectledge.org/confluence/display/CRM/CRM+v1.0+Public+APIs
34 * here}
35 *
36 * @package CiviCRM_APIv3
37 * @subpackage API_File
731a0992 38 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
39 * $Id: $
40 *
41 */
42
6a488035
TO
43/**
44 * Create a file
45 *
46 * This API is used for creating a file
47 *
cf470720
TO
48 * @param array $params
49 * An associative array of name/value property values of civicrm_file.
6a488035 50 *
a6c01b45
CW
51 * @return array
52 * of newly created file property values.
6a488035
TO
53 * @access public
54 */
55function civicrm_api3_file_create($params) {
56
57 civicrm_api3_verify_mandatory($params, 'CRM_Core_DAO_File', array('uri'));
58
59 if (!isset($params['upload_date'])) {
60 $params['upload_date'] = date("Ymd");
61 }
62
6a488035
TO
63 $fileDAO = new CRM_Core_DAO_File();
64 $properties = array('id', 'file_type_id', 'mime_type', 'uri', 'document', 'description', 'upload_date');
65
66 foreach ($properties as $name) {
67 if (array_key_exists($name, $params)) {
68 $fileDAO->$name = $params[$name];
69 }
70 }
71
72 $fileDAO->save();
73
74 $file = array();
75 _civicrm_api3_object_to_array($fileDAO, $file);
76
77 return civicrm_api3_create_success($file, $params, 'file', 'create', $fileDAO);
78}
79
80/**
81 * Get a file.
82 *
83 * This api is used for finding an existing file.
84 * Required parameters : id OR file_type_id of a file
85 *
cf470720
TO
86 * @param array $params
87 * An associative array of name/value property values of civicrm_file.
6a488035 88 *
a6c01b45
CW
89 * @return Array
90 * of all found file object property values.
6a488035
TO
91 * @access public
92 */
93function civicrm_api3_file_get($params) {
94 civicrm_api3_verify_one_mandatory($params);
95 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
96}
97
98/**
99 * Update an existing file
100 *
101 * This api is used for updating an existing file.
7c285037 102 * Required parameters : id of a file
6a488035 103 *
35671d00 104 * @param array $paramsAn array of name/value property values of civicrm_file.
cf470720 105 * An array of name/value property values of civicrm_file.
6a488035 106 *
a6c01b45
CW
107 * @return array
108 * of updated file object property values
6a488035
TO
109 * @access public
110 */
7c285037 111function civicrm_api3_file_update($params) {
6a488035
TO
112
113 if (!isset($params['id'])) {
114 return civicrm_api3_create_error('Required parameter missing');
115 }
116
6a488035
TO
117 $fileDAO = new CRM_Core_DAO_File();
118 $fileDAO->id = $params['id'];
119 if ($fileDAO->find(TRUE)) {
120 $fileDAO->copyValues($params);
121 if (!$params['upload_date'] && !$fileDAO->upload_date) {
122 $fileDAO->upload_date = date("Ymd");
123 }
124 $fileDAO->save();
125 }
126 $file = array();
127 _civicrm_api3_object_to_array(clone($fileDAO), $file);
128 return $file;
129}
130
131/**
132 * Deletes an existing file
133 *
134 * This API is used for deleting a file
135 * Required parameters : id of a file
136 *
c490a46a 137 * @param array $params
77b97be7 138 *
a6c01b45
CW
139 * @return array
140 * API result array
6a488035 141 * @access public
6a488035
TO
142 */
143function civicrm_api3_file_delete($params) {
144
145 civicrm_api3_verify_mandatory($params, NULL, array('id'));
146
147 $check = FALSE;
148
6a488035
TO
149 $entityFileDAO = new CRM_Core_DAO_EntityFile();
150 $entityFileDAO->file_id = $params['id'];
151 if ($entityFileDAO->find()) {
152 $check = $entityFileDAO->delete();
153 }
154
6a488035
TO
155 $fileDAO = new CRM_Core_DAO_File();
156 $fileDAO->id = $params['id'];
157 if ($fileDAO->find(TRUE)) {
158 $check = $fileDAO->delete();
159 }
160
161 return $check ? NULL : civicrm_api3_create_error('Error while deleting a file.');
22fd1690 162}