Merge pull request #5185 from colemanw/revertExamples
[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/**
b081365f
CW
29 * This api is a simple wrapper of the CiviCRM file DAO.
30 *
c28e1768
CW
31 * Creating and updating files is a complex process and this api is usually insufficient.
32 * Use the "Attachment" api instead for more robust file handling.
6a488035 33 *
c28e1768 34 * @fixme no unit tests
6a488035 35 * @package CiviCRM_APIv3
6a488035
TO
36 */
37
6a488035 38/**
c28e1768 39 * Create a file record.
b081365f 40 * @note This is only one of several steps needed to create a file in CiviCRM.
c28e1768 41 * Use the "Attachment" api to better handle all steps.
6a488035 42 *
cf470720 43 * @param array $params
2e66abf8 44 * Array per getfields metadata.
6a488035 45 *
a6c01b45 46 * @return array
16b10e64 47 * Array of newly created file property values.
6a488035
TO
48 */
49function civicrm_api3_file_create($params) {
50
51 civicrm_api3_verify_mandatory($params, 'CRM_Core_DAO_File', array('uri'));
52
53 if (!isset($params['upload_date'])) {
54 $params['upload_date'] = date("Ymd");
55 }
56
6a488035 57 $fileDAO = new CRM_Core_DAO_File();
9d32e6f7
EM
58 $properties = array(
59 'id',
60 'file_type_id',
61 'mime_type',
62 'uri',
63 'document',
64 'description',
65 'upload_date',
66 );
6a488035
TO
67
68 foreach ($properties as $name) {
69 if (array_key_exists($name, $params)) {
70 $fileDAO->$name = $params[$name];
71 }
72 }
73
74 $fileDAO->save();
75
76 $file = array();
77 _civicrm_api3_object_to_array($fileDAO, $file);
78
79 return civicrm_api3_create_success($file, $params, 'file', 'create', $fileDAO);
80}
81
82/**
83 * Get a file.
84 *
cf470720 85 * @param array $params
2e66abf8 86 * Array per getfields metadata.
6a488035 87 *
16b10e64
CW
88 * @return array
89 * Array of all found file object property values.
6a488035
TO
90 */
91function civicrm_api3_file_get($params) {
92 civicrm_api3_verify_one_mandatory($params);
93 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
94}
95
96/**
9d32e6f7 97 * Update an existing file.
6a488035 98 *
645ee340 99 * @param array $params
2e66abf8 100 * Array per getfields metadata.
9d32e6f7 101 *
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/**
9d32e6f7 125 * Delete an existing file.
6a488035 126 *
c490a46a 127 * @param array $params
2e66abf8 128 * Array per getfields metadata.
77b97be7 129 *
a6c01b45 130 * @return array
72b3a70c 131 * API result array
6a488035
TO
132 */
133function civicrm_api3_file_delete($params) {
134
135 civicrm_api3_verify_mandatory($params, NULL, array('id'));
136
137 $check = FALSE;
138
6a488035
TO
139 $entityFileDAO = new CRM_Core_DAO_EntityFile();
140 $entityFileDAO->file_id = $params['id'];
141 if ($entityFileDAO->find()) {
142 $check = $entityFileDAO->delete();
143 }
144
6a488035
TO
145 $fileDAO = new CRM_Core_DAO_File();
146 $fileDAO->id = $params['id'];
147 if ($fileDAO->find(TRUE)) {
148 $check = $fileDAO->delete();
149 }
150
151 return $check ? NULL : civicrm_api3_create_error('Error while deleting a file.');
22fd1690 152}