/api/v3/File.php

https://github.com/pcoughlin/CiviCRM-SR · PHP · 343 lines · 185 code · 44 blank · 114 comment · 25 complexity · 0f85a463e6d89ae0e86e2c4e130b3a4b MD5 · raw file

  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 4.0 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2011 |
  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. * Definition of the Tag of the CRM API.
  30. * More detailed documentation can be found
  31. * {@link http://objectledge.org/confluence/display/CRM/CRM+v1.0+Public+APIs
  32. * here}
  33. *
  34. * @package CiviCRM_APIv3
  35. * @subpackage API_File
  36. * @copyright CiviCRM LLC (c) 2004-2011
  37. * $Id: $
  38. *
  39. */
  40. /**
  41. * Files required for this package
  42. */
  43. require_once 'api/v3/utils.php';
  44. /**
  45. * Create a file
  46. *
  47. * This API is used for creating a file
  48. *
  49. * @param array $params an associative array of name/value property values of civicrm_file
  50. * @return array of newly created file property values.
  51. * @access public
  52. */
  53. function civicrm_api3_file_create( $params )
  54. {
  55. _civicrm_api3_initialize(true);
  56. try{
  57. civicrm_api3_verify_mandatory($params,'CRM_Core_DAO_File',array('file_type_id'));
  58. if ( !isset($params['upload_date']) ) {
  59. $params['upload_date'] = date("Ymd");
  60. }
  61. require_once 'CRM/Core/DAO/File.php';
  62. $fileDAO = new CRM_Core_DAO_File();
  63. $properties = array('id', 'file_type_id', 'mime_type', 'uri', 'document', 'description', 'upload_date');
  64. foreach ($properties as $name) {
  65. if (array_key_exists($name, $params)) {
  66. $fileDAO->$name = $params[$name];
  67. }
  68. }
  69. $fileDAO->save();
  70. $file = array();
  71. _civicrm_api3_object_to_array($fileDAO, $file);
  72. return civicrm_create_success($file,$params,$fileDAO);
  73. } catch (PEAR_Exception $e) {
  74. return civicrm_api3_create_error( $e->getMessage() );
  75. } catch (Exception $e) {
  76. return civicrm_api3_create_error( $e->getMessage() );
  77. }
  78. }
  79. /**
  80. * Get a file.
  81. *
  82. * This api is used for finding an existing file.
  83. * Required parameters : id OR file_type_id of a file
  84. *
  85. * @param array $params an associative array of name/value property values of civicrm_file
  86. *
  87. * @return Array of all found file object property values.
  88. * @access public
  89. */
  90. function civicrm_api3_file_get($params)
  91. {
  92. _civicrm_api3_initialize(true);
  93. try{
  94. civicrm_api3_verify_one_mandatory($params, null,array('file_type_id','id'));
  95. require_once 'CRM/Core/DAO/File.php';
  96. $fileDAO = new CRM_Core_DAO_File();
  97. $properties = array('id', 'file_type_id', 'mime_type', 'uri', 'document', 'description', 'upload_date');
  98. foreach ( $properties as $name) {
  99. if (array_key_exists($name, $params)) {
  100. $fileDAO->$name = $params[$name];
  101. }
  102. }
  103. if ( $fileDAO->find() ) {
  104. $file = array();
  105. while ( $fileDAO->fetch() ) {
  106. _civicrm_api3_object_to_array( clone($fileDAO), $file );
  107. $files[$fileDAO->id] = $file;
  108. }
  109. } else {
  110. return civicrm_api3_create_error('Exact match not found');
  111. }
  112. return $files;
  113. } catch (PEAR_Exception $e) {
  114. return civicrm_api3_create_error( $e->getMessage() );
  115. } catch (Exception $e) {
  116. return civicrm_api3_create_error( $e->getMessage() );
  117. }
  118. }
  119. /**
  120. * Update an existing file
  121. *
  122. * This api is used for updating an existing file.
  123. * Required parrmeters : id of a file
  124. *
  125. * @param Array $params an associative array of name/value property values of civicrm_file
  126. *
  127. * @return array of updated file object property values
  128. * @access public
  129. */
  130. function &civicrm_api3_file_update( $params ) {
  131. if ( !is_array( $params ) ) {
  132. return civicrm_api3_create_error( 'Input variable `params` is not an array' );
  133. }
  134. if ( !isset($params['id']) ) {
  135. return civicrm_api3_create_error( 'Required parameter missing' );
  136. }
  137. require_once 'CRM/Core/DAO/File.php';
  138. $fileDAO = new CRM_Core_DAO_File( );
  139. $fileDAO->id = $params['id'];
  140. if ($fileDAO->find(true)) {
  141. $fileDAO->copyValues( $params );
  142. if ( !$params['upload_date'] && !$fileDAO->upload_date) {
  143. $fileDAO->upload_date = date("Ymd");
  144. }
  145. $fileDAO->save();
  146. }
  147. $file = array();
  148. _civicrm_api3_object_to_array( clone($fileDAO), $file );
  149. return $file;
  150. }
  151. /**
  152. * Deletes an existing file
  153. *
  154. * This API is used for deleting a file
  155. * Required parameters : id of a file
  156. *
  157. * @param Int $fileId Id of the file to be deleted
  158. *
  159. * @return null if successfull, object of CRM_Core_Error otherwise
  160. * @access public
  161. * @todo doesn't take array
  162. */
  163. function civicrm_api3_file_delete( $params ) {
  164. _civicrm_api3_initialize(true);
  165. try{
  166. civicrm_api3_verify_mandatory($params,null,array('id'));
  167. $check = false;
  168. require_once 'CRM/Core/DAO/EntityFile.php';
  169. $entityFileDAO = new CRM_Core_DAO_EntityFile( );
  170. $entityFileDAO->file_id = $params['id'];
  171. if ($entityFileDAO->find()) {
  172. $check = $entityFileDAO->delete();
  173. }
  174. require_once 'CRM/Core/DAO/File.php';
  175. $fileDAO = new CRM_Core_DAO_File( );
  176. $fileDAO->id = $params['id'];
  177. if ($fileDAO->find(true)) {
  178. $check = $fileDAO->delete();
  179. }
  180. return $check ? null : civicrm_api3_create_error('Error while deleting a file.');
  181. } catch (PEAR_Exception $e) {
  182. return civicrm_api3_create_error( $e->getMessage() );
  183. } catch (Exception $e) {
  184. return civicrm_api3_create_error( $e->getMessage() );
  185. }
  186. }
  187. /**
  188. * Assigns an entity to a file
  189. *
  190. * @param object $file id of a file
  191. * @param object $entity id of a entity
  192. * @param string $entity_table
  193. *
  194. * @return array of newly created entity-file object properties
  195. * @access public
  196. */
  197. function civicrm_api3_entity_file_create( $params )
  198. {
  199. _civicrm_api3_initialize(true);
  200. try{
  201. require_once 'CRM/Core/DAO/EntityFile.php';
  202. civicrm_api3_verify_one_mandatory($params,null,array('file_id','entity_id'));
  203. if (empty($params['entity_table'])){
  204. $params['entity_table'] = 'civicrm_contact';
  205. }
  206. $entityFileDAO = new CRM_Core_DAO_EntityFile( );
  207. $entityFileDAO->copyValues( $params );
  208. $entityFileDAO->save( );
  209. $entityFile = array();
  210. _civicrm_api3_object_to_array( $entityFileDAO, $entityFile );
  211. return civicrm_create_success($entityFile,$params,$entityFileDAO);
  212. } catch (PEAR_Exception $e) {
  213. return civicrm_api3_create_error( $e->getMessage() );
  214. } catch (Exception $e) {
  215. return civicrm_api3_create_error( $e->getMessage() );
  216. }
  217. }
  218. /**
  219. * Returns all files assigned to a single entity instance.
  220. *
  221. * @param object $entityID id of the supported entity.
  222. * @param string $entity_table
  223. *
  224. * @return array nested array of entity-file property values.
  225. * @access public
  226. */
  227. function civicrm_api3_files_by_entity_get($params) {
  228. _civicrm_api3_initialize ( true );
  229. try {
  230. civicrm_api3_verify_mandatory ( $params, null, array ('entity_id' ) );
  231. if (empty ( $entityTable )) {
  232. $entityTable = 'civicrm_contact';
  233. }
  234. require_once 'CRM/Core/DAO/EntityFile.php';
  235. require_once 'CRM/Core/DAO/File.php';
  236. $entityFileDAO = new CRM_Core_DAO_EntityFile ();
  237. $entityFileDAO->entity_table = $entityTable;
  238. $entityFileDAO->entity_id = $params ['entity_id'];
  239. if ($fileID) {
  240. $entityFileDAO->file_id = $params ['file_id'];
  241. }
  242. if ($entityFileDAO->find ()) {
  243. $entityFile = array ();
  244. while ( $entityFileDAO->fetch () ) {
  245. _civicrm_api3_object_to_array ( $entityFileDAO, $entityFile );
  246. $files [$entityFileDAO->file_id] = $entityFile;
  247. if (array_key_exists ( 'file_id', $files [$entityFileDAO->file_id] )) {
  248. $fileDAO = new CRM_Core_DAO_File ();
  249. $fileDAO->id = $entityFile ['file_id'];
  250. $fileDAO->find ( true );
  251. _civicrm_api3_object_to_array ( $fileDAO, $files [$entityFileDAO->file_id] );
  252. }
  253. if (CRM_Utils_Array::value ( 'file_type_id', $files [$entityFileDAO->file_id] )) {
  254. $files [$entityFileDAO->file_id] ['file_type'] = CRM_Core_OptionGroup::getLabel ( 'file_type', $files [$entityFileDAO->file_id] ['file_type_id'] );
  255. }
  256. }
  257. } else {
  258. return civicrm_api3_create_error ( 'Exact match not found' );
  259. }
  260. return civicrm_api3_create_success ( $files, $params, $entityFileDAO );
  261. } catch ( PEAR_Exception $e ) {
  262. return civicrm_create_error ( $e->getMessage () );
  263. } catch ( Exception $e ) {
  264. return civicrm_create_error ( $e->getMessage () );
  265. }
  266. }
  267. /**
  268. * Deletes an existing entity file assignment.
  269. * Required parameters : 1. id of an entity-file
  270. * 2. entity_id and entity_table of an entity-file
  271. *
  272. * @param array $params an associative array of name/value property values of civicrm_entity_file.
  273. *
  274. * @return null if successfull, object of CRM_Core_Error otherwise
  275. * @access public
  276. */
  277. function civicrm_api3_entity_file_delete($params) {
  278. _civicrm_api3_initialize ( true );
  279. try {
  280. civicrm_api3_verify_mandatory ( $params );
  281. require_once 'CRM/Core/DAO/EntityFile.php';
  282. //if ( ! isset($params['id']) && ( !isset($params['entity_id']) || !isset($params['entity_file']) ) ) {
  283. if (! isset ( $params ['id'] ) && (! isset ( $params ['entity_id'] ) || ! isset ( $params ['entity_table'] ))) {
  284. return civicrm_api3_create_error ( 'Required parameters missing' );
  285. }
  286. $entityFileDAO = new CRM_Core_DAO_EntityFile ();
  287. $properties = array ('id', 'entity_id', 'entity_table', 'file_id' );
  288. foreach ( $properties as $name ) {
  289. if (array_key_exists ( $name, $params )) {
  290. $entityFileDAO->$name = $params [$name];
  291. }
  292. }
  293. return $entityFileDAO->delete () ? null : civicrm_api3_create_error ( 'Error while deleting' );
  294. } catch ( PEAR_Exception $e ) {
  295. return civicrm_create_error ( $e->getMessage () );
  296. } catch ( Exception $e ) {
  297. return civicrm_create_error ( $e->getMessage () );
  298. }
  299. }