Merge pull request #133 from pradpnayak/CRM-12061
[civicrm-core.git] / api / v3 / 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_APIv3
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 'CRM/Core/DAO/File.php';
48require_once 'CRM/Core/BAO/File.php';
49
50/**
51 * Create a file
52 *
53 * This API is used for creating a file
54 *
55 * @param array $params an associative array of name/value property values of civicrm_file
56 *
57 * @return array of newly created file property values.
58 * @access public
59 */
60function civicrm_api3_file_create($params) {
61
62 civicrm_api3_verify_mandatory($params, 'CRM_Core_DAO_File', array('uri'));
63
64 if (!isset($params['upload_date'])) {
65 $params['upload_date'] = date("Ymd");
66 }
67
68 require_once 'CRM/Core/DAO/File.php';
69
70 $fileDAO = new CRM_Core_DAO_File();
71 $properties = array('id', 'file_type_id', 'mime_type', 'uri', 'document', 'description', 'upload_date');
72
73 foreach ($properties as $name) {
74 if (array_key_exists($name, $params)) {
75 $fileDAO->$name = $params[$name];
76 }
77 }
78
79 $fileDAO->save();
80
81 $file = array();
82 _civicrm_api3_object_to_array($fileDAO, $file);
83
84 return civicrm_api3_create_success($file, $params, 'file', 'create', $fileDAO);
85}
86
87/**
88 * Get a file.
89 *
90 * This api is used for finding an existing file.
91 * Required parameters : id OR file_type_id of a file
92 *
93 * @param array $params an associative array of name/value property values of civicrm_file
94 *
95 * @return Array of all found file object property values.
96 * @access public
97 */
98function civicrm_api3_file_get($params) {
99 civicrm_api3_verify_one_mandatory($params);
100 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
101}
102
103/**
104 * Update an existing file
105 *
106 * This api is used for updating an existing file.
107 * Required parrmeters : id of a file
108 *
109 * @param Array $params an associative array of name/value property values of civicrm_file
110 *
111 * @return array of updated file object property values
112 * @access public
113 */
114function &civicrm_api3_file_update($params) {
115
116 if (!isset($params['id'])) {
117 return civicrm_api3_create_error('Required parameter missing');
118 }
119
120 require_once 'CRM/Core/DAO/File.php';
121 $fileDAO = new CRM_Core_DAO_File();
122 $fileDAO->id = $params['id'];
123 if ($fileDAO->find(TRUE)) {
124 $fileDAO->copyValues($params);
125 if (!$params['upload_date'] && !$fileDAO->upload_date) {
126 $fileDAO->upload_date = date("Ymd");
127 }
128 $fileDAO->save();
129 }
130 $file = array();
131 _civicrm_api3_object_to_array(clone($fileDAO), $file);
132 return $file;
133}
134
135/**
136 * Deletes an existing file
137 *
138 * This API is used for deleting a file
139 * Required parameters : id of a file
140 *
141 * @param Int $fileId Id of the file to be deleted
142 *
143 * @return null if successfull, object of CRM_Core_Error otherwise
144 * @access public
145
146 */
147function civicrm_api3_file_delete($params) {
148
149 civicrm_api3_verify_mandatory($params, NULL, array('id'));
150
151 $check = FALSE;
152
153 require_once 'CRM/Core/DAO/EntityFile.php';
154 $entityFileDAO = new CRM_Core_DAO_EntityFile();
155 $entityFileDAO->file_id = $params['id'];
156 if ($entityFileDAO->find()) {
157 $check = $entityFileDAO->delete();
158 }
159
160 require_once 'CRM/Core/DAO/File.php';
161 $fileDAO = new CRM_Core_DAO_File();
162 $fileDAO->id = $params['id'];
163 if ($fileDAO->find(TRUE)) {
164 $check = $fileDAO->delete();
165 }
166
167 return $check ? NULL : civicrm_api3_create_error('Error while deleting a file.');
168}