| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | +--------------------------------------------------------------------+ |
| 5 | | CiviCRM version 4.4 | |
| 6 | +--------------------------------------------------------------------+ |
| 7 | | Copyright CiviCRM LLC (c) 2004-2013 | |
| 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 |
| 38 | * @copyright CiviCRM LLC (c) 2004-2013 |
| 39 | * $Id: $ |
| 40 | * |
| 41 | */ |
| 42 | |
| 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 | */ |
| 53 | function 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 | |
| 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 | */ |
| 89 | function 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. |
| 98 | * Required parrmeters : id of a file |
| 99 | * |
| 100 | * @param Array $params an associative array of name/value property values of civicrm_file |
| 101 | * |
| 102 | * @return array of updated file object property values |
| 103 | * @access public |
| 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 | * Deletes an existing file |
| 127 | * |
| 128 | * This API is used for deleting a file |
| 129 | * Required parameters : id of a file |
| 130 | * |
| 131 | * @param Int $fileId Id of the file to be deleted |
| 132 | * |
| 133 | * @return null if successfull, object of CRM_Core_Error otherwise |
| 134 | * @access public |
| 135 | |
| 136 | */ |
| 137 | function civicrm_api3_file_delete($params) { |
| 138 | |
| 139 | civicrm_api3_verify_mandatory($params, NULL, array('id')); |
| 140 | |
| 141 | $check = FALSE; |
| 142 | |
| 143 | $entityFileDAO = new CRM_Core_DAO_EntityFile(); |
| 144 | $entityFileDAO->file_id = $params['id']; |
| 145 | if ($entityFileDAO->find()) { |
| 146 | $check = $entityFileDAO->delete(); |
| 147 | } |
| 148 | |
| 149 | $fileDAO = new CRM_Core_DAO_File(); |
| 150 | $fileDAO->id = $params['id']; |
| 151 | if ($fileDAO->find(TRUE)) { |
| 152 | $check = $fileDAO->delete(); |
| 153 | } |
| 154 | |
| 155 | return $check ? NULL : civicrm_api3_create_error('Error while deleting a file.'); |
| 156 | } |