CRM-15905 fix - API: problem sorting contacts on ID
[civicrm-core.git] / api / v3 / File.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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
37 * @copyright CiviCRM LLC (c) 2004-2014
38 * $Id: $
39 */
40
41 /**
42 * Create a file.
43 *
44 * @param array $params
45 * Array per getfields metadata.
46 *
47 * @return array
48 * Array of newly created file property values.
49 */
50 function civicrm_api3_file_create($params) {
51
52 civicrm_api3_verify_mandatory($params, 'CRM_Core_DAO_File', array('uri'));
53
54 if (!isset($params['upload_date'])) {
55 $params['upload_date'] = date("Ymd");
56 }
57
58 $fileDAO = new CRM_Core_DAO_File();
59 $properties = array(
60 'id',
61 'file_type_id',
62 'mime_type',
63 'uri',
64 'document',
65 'description',
66 'upload_date',
67 );
68
69 foreach ($properties as $name) {
70 if (array_key_exists($name, $params)) {
71 $fileDAO->$name = $params[$name];
72 }
73 }
74
75 $fileDAO->save();
76
77 $file = array();
78 _civicrm_api3_object_to_array($fileDAO, $file);
79
80 return civicrm_api3_create_success($file, $params, 'file', 'create', $fileDAO);
81 }
82
83 /**
84 * Get a file.
85 *
86 * @param array $params
87 * Array per getfields metadata.
88 *
89 * @return array
90 * Array of all found file object property values.
91 */
92 function civicrm_api3_file_get($params) {
93 civicrm_api3_verify_one_mandatory($params);
94 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
95 }
96
97 /**
98 * Update an existing file.
99 *
100 * @param array $params
101 * Array per getfields metadata.
102 *
103 * @return array
104 */
105 function civicrm_api3_file_update($params) {
106
107 if (!isset($params['id'])) {
108 return civicrm_api3_create_error('Required parameter missing');
109 }
110
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 * Delete an existing file.
127 *
128 * @param array $params
129 * Array per getfields metadata.
130 *
131 * @return array
132 * API result array
133 */
134 function civicrm_api3_file_delete($params) {
135
136 civicrm_api3_verify_mandatory($params, NULL, array('id'));
137
138 $check = FALSE;
139
140 $entityFileDAO = new CRM_Core_DAO_EntityFile();
141 $entityFileDAO->file_id = $params['id'];
142 if ($entityFileDAO->find()) {
143 $check = $entityFileDAO->delete();
144 }
145
146 $fileDAO = new CRM_Core_DAO_File();
147 $fileDAO->id = $params['id'];
148 if ($fileDAO->find(TRUE)) {
149 $check = $fileDAO->delete();
150 }
151
152 return $check ? NULL : civicrm_api3_create_error('Error while deleting a file.');
153 }