| 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 | * This api is a simple wrapper of the CiviCRM file DAO. |
| 30 | * |
| 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. |
| 33 | * |
| 34 | * @fixme no unit tests |
| 35 | * @package CiviCRM_APIv3 |
| 36 | */ |
| 37 | |
| 38 | /** |
| 39 | * Create a file record. |
| 40 | * @note This is only one of several steps needed to create a file in CiviCRM. |
| 41 | * Use the "Attachment" api to better handle all steps. |
| 42 | * |
| 43 | * @param array $params |
| 44 | * Array per getfields metadata. |
| 45 | * |
| 46 | * @return array |
| 47 | */ |
| 48 | function civicrm_api3_file_create($params) { |
| 49 | |
| 50 | civicrm_api3_verify_mandatory($params, 'CRM_Core_DAO_File', array('uri')); |
| 51 | |
| 52 | if (!isset($params['upload_date'])) { |
| 53 | $params['upload_date'] = date("Ymd"); |
| 54 | } |
| 55 | |
| 56 | $fileDAO = new CRM_Core_DAO_File(); |
| 57 | $properties = array( |
| 58 | 'id', |
| 59 | 'file_type_id', |
| 60 | 'mime_type', |
| 61 | 'uri', |
| 62 | 'document', |
| 63 | 'description', |
| 64 | 'upload_date', |
| 65 | ); |
| 66 | |
| 67 | foreach ($properties as $name) { |
| 68 | if (array_key_exists($name, $params)) { |
| 69 | $fileDAO->$name = $params[$name]; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | $fileDAO->save(); |
| 74 | |
| 75 | $file = array(); |
| 76 | _civicrm_api3_object_to_array($fileDAO, $file); |
| 77 | |
| 78 | return civicrm_api3_create_success($file, $params, 'File', 'create', $fileDAO); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Get a File. |
| 83 | * |
| 84 | * @param array $params |
| 85 | * Array per getfields metadata. |
| 86 | * |
| 87 | * @return array |
| 88 | * Array of all found file object property values. |
| 89 | */ |
| 90 | function 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 | * @param array $params |
| 99 | * Array per getfields metadata. |
| 100 | * |
| 101 | * @return array |
| 102 | */ |
| 103 | function civicrm_api3_file_update($params) { |
| 104 | |
| 105 | if (!isset($params['id'])) { |
| 106 | return civicrm_api3_create_error('Required parameter missing'); |
| 107 | } |
| 108 | |
| 109 | $fileDAO = new CRM_Core_DAO_File(); |
| 110 | $fileDAO->id = $params['id']; |
| 111 | if ($fileDAO->find(TRUE)) { |
| 112 | $fileDAO->copyValues($params); |
| 113 | if (!$params['upload_date'] && !$fileDAO->upload_date) { |
| 114 | $fileDAO->upload_date = date("Ymd"); |
| 115 | } |
| 116 | $fileDAO->save(); |
| 117 | } |
| 118 | $file = array(); |
| 119 | _civicrm_api3_object_to_array(clone($fileDAO), $file); |
| 120 | return $file; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Delete an existing File. |
| 125 | * |
| 126 | * @param array $params |
| 127 | * Array per getfields metadata. |
| 128 | * |
| 129 | * @return array |
| 130 | * API result array |
| 131 | */ |
| 132 | function civicrm_api3_file_delete($params) { |
| 133 | |
| 134 | civicrm_api3_verify_mandatory($params, NULL, array('id')); |
| 135 | |
| 136 | $check = FALSE; |
| 137 | |
| 138 | $entityFileDAO = new CRM_Core_DAO_EntityFile(); |
| 139 | $entityFileDAO->file_id = $params['id']; |
| 140 | if ($entityFileDAO->find()) { |
| 141 | $check = $entityFileDAO->delete(); |
| 142 | } |
| 143 | |
| 144 | $fileDAO = new CRM_Core_DAO_File(); |
| 145 | $fileDAO->id = $params['id']; |
| 146 | if ($fileDAO->find(TRUE)) { |
| 147 | $check = $fileDAO->delete(); |
| 148 | } |
| 149 | |
| 150 | return $check ? NULL : civicrm_api3_create_error('Error while deleting a file.'); |
| 151 | } |