Merge pull request #3679 from yashodha/CRM-14951
[civicrm-core.git] / api / v3 / File.php
CommitLineData
6a488035 1<?php
6a488035
TO
2
3/*
4 +--------------------------------------------------------------------+
731a0992 5 | CiviCRM version 4.5 |
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 *
48 * @param array $params an associative array of name/value property values of civicrm_file
49 *
50 * @return array of newly created file property values.
51 * @access public
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 *
84 * @param array $params an associative array of name/value property values of civicrm_file
85 *
86 * @return Array of all found file object property values.
87 * @access public
88 */
89function civicrm_api3_file_get($params) {
90 civicrm_api3_verify_one_mandatory($params);
91 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
92}
93
94/**
95 * Update an existing file
96 *
97 * This api is used for updating an existing file.
7c285037 98 * Required parameters : id of a file
6a488035 99 *
7c285037 100 * @param Array $params an array of name/value property values of civicrm_file
6a488035
TO
101 *
102 * @return array of updated file object property values
103 * @access public
104 */
7c285037 105function civicrm_api3_file_update($params) {
6a488035
TO
106
107 if (!isset($params['id'])) {
108 return civicrm_api3_create_error('Required parameter missing');
109 }
110
6a488035
TO
111 $fileDAO = new CRM_Core_DAO_File();
112 $fileDAO->id = $params['id'];
113 if ($fileDAO->find(TRUE)) {
114 $fileDAO->copyValues($params);
115 if (!$params['upload_date'] && !$fileDAO->upload_date) {
116 $fileDAO->upload_date = date("Ymd");
117 }
118 $fileDAO->save();
119 }
120 $file = array();
121 _civicrm_api3_object_to_array(clone($fileDAO), $file);
122 return $file;
123}
124
125/**
126 * Deletes an existing file
127 *
128 * This API is used for deleting a file
129 * Required parameters : id of a file
130 *
77b97be7
EM
131 * @param $params
132 *
133 * @internal param Int $fileId Id of the file to be deleted
6a488035
TO
134 *
135 * @return null if successfull, object of CRM_Core_Error otherwise
136 * @access public
6a488035
TO
137 */
138function civicrm_api3_file_delete($params) {
139
140 civicrm_api3_verify_mandatory($params, NULL, array('id'));
141
142 $check = FALSE;
143
6a488035
TO
144 $entityFileDAO = new CRM_Core_DAO_EntityFile();
145 $entityFileDAO->file_id = $params['id'];
146 if ($entityFileDAO->find()) {
147 $check = $entityFileDAO->delete();
148 }
149
6a488035
TO
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.');
22fd1690 157}