Merge remote-tracking branch 'upstream/4.5' into 4.5-master-2015-02-02-18-36-16
[civicrm-core.git] / api / v3 / File.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
731a0992 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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/**
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
731a0992 37 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
38 * $Id: $
39 *
40 */
41
6a488035
TO
42/**
43 * Create a file
44 *
45 * This API is used for creating a file
46 *
cf470720
TO
47 * @param array $params
48 * An associative array of name/value property values of civicrm_file.
6a488035 49 *
a6c01b45 50 * @return array
16b10e64 51 * Array of newly created file property values.
6a488035
TO
52 */
53function civicrm_api3_file_create($params) {
54
55 civicrm_api3_verify_mandatory($params, 'CRM_Core_DAO_File', array('uri'));
56
57 if (!isset($params['upload_date'])) {
58 $params['upload_date'] = date("Ymd");
59 }
60
6a488035
TO
61 $fileDAO = new CRM_Core_DAO_File();
62 $properties = array('id', 'file_type_id', 'mime_type', 'uri', 'document', 'description', 'upload_date');
63
64 foreach ($properties as $name) {
65 if (array_key_exists($name, $params)) {
66 $fileDAO->$name = $params[$name];
67 }
68 }
69
70 $fileDAO->save();
71
72 $file = array();
73 _civicrm_api3_object_to_array($fileDAO, $file);
74
75 return civicrm_api3_create_success($file, $params, 'file', 'create', $fileDAO);
76}
77
78/**
79 * Get a file.
80 *
81 * This api is used for finding an existing file.
82 * Required parameters : id OR file_type_id of a file
83 *
cf470720
TO
84 * @param array $params
85 * An associative array of name/value property values of civicrm_file.
6a488035 86 *
16b10e64
CW
87 * @return array
88 * Array of all found file object property values.
6a488035
TO
89 */
90function civicrm_api3_file_get($params) {
91 civicrm_api3_verify_one_mandatory($params);
92 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
93}
94
95/**
96 * Update an existing file
97 *
98 * This api is used for updating an existing file.
7c285037 99 * Required parameters : id of a file
6a488035 100 *
645ee340 101 * @param array $params
a6c01b45 102 * @return array
6a488035 103 */
7c285037 104function civicrm_api3_file_update($params) {
6a488035
TO
105
106 if (!isset($params['id'])) {
107 return civicrm_api3_create_error('Required parameter missing');
108 }
109
6a488035
TO
110 $fileDAO = new CRM_Core_DAO_File();
111 $fileDAO->id = $params['id'];
112 if ($fileDAO->find(TRUE)) {
113 $fileDAO->copyValues($params);
114 if (!$params['upload_date'] && !$fileDAO->upload_date) {
115 $fileDAO->upload_date = date("Ymd");
116 }
117 $fileDAO->save();
118 }
119 $file = array();
120 _civicrm_api3_object_to_array(clone($fileDAO), $file);
121 return $file;
122}
123
124/**
125 * Deletes an existing file
126 *
127 * This API is used for deleting a file
128 * Required parameters : id of a file
129 *
c490a46a 130 * @param array $params
77b97be7 131 *
a6c01b45 132 * @return array
72b3a70c 133 * API result array
6a488035
TO
134 */
135function civicrm_api3_file_delete($params) {
136
137 civicrm_api3_verify_mandatory($params, NULL, array('id'));
138
139 $check = FALSE;
140
6a488035
TO
141 $entityFileDAO = new CRM_Core_DAO_EntityFile();
142 $entityFileDAO->file_id = $params['id'];
143 if ($entityFileDAO->find()) {
144 $check = $entityFileDAO->delete();
145 }
146
6a488035
TO
147 $fileDAO = new CRM_Core_DAO_File();
148 $fileDAO->id = $params['id'];
149 if ($fileDAO->find(TRUE)) {
150 $check = $fileDAO->delete();
151 }
152
153 return $check ? NULL : civicrm_api3_create_error('Error while deleting a file.');
22fd1690 154}