CRM-16019 - Expose date/time format preferences to clientside
[civicrm-core.git] / api / v3 / Attachment.php
index 455fd96517e2ccbf1368a895333360b3176668cd..5ca4f68c3950d287d8f31f3832618ed961f05fa2 100644 (file)
  * "Attachment" is a pseudo-entity which represents a record in civicrm_file
  * combined with a record in civicrm_entity_file as well as the underlying
  * file content.
+ * For core fields use "entity_table", for custom fields use "field_name"
  *
  * @code
+ * // Create an attachment for a core field
  * $result = civicrm_api3('Attachment', 'create', array(
  *   'entity_table' => 'civicrm_activity',
  *   'entity_id' => 123,
  * @endcode
  *
  * @code
+ * // Create an attachment for a custom file field
+ * $result = civicrm_api3('Attachment', 'create', array(
+ *   'field_name' => 'custom_6',
+ *   'entity_id' => 123,
+ *   'name' => 'README.txt',
+ *   'mime_type' => 'text/plain',
+ *   'content' => 'Please to read the README',
+ * ));
+ * $attachment = $result['values'][$result['id']];
+ * echo sprintf("<a href='%s'>View %s</a>", $attachment['url'], $attachment['name']);
+ * @endcode
+ *
+ * @code
+ * // Move an existing file and save as an attachment
  * $result = civicrm_api3('Attachment', 'create', array(
  *   'entity_table' => 'civicrm_activity',
  *   'entity_id' => 123,
@@ -63,9 +79,6 @@
  *    "check_permissions"==false)
  *
  * @package CiviCRM_APIv3
- * @subpackage API_Attachment
- * @copyright CiviCRM LLC (c) 2004-2014
- * $Id: $
  */
 
 /**
@@ -78,21 +91,26 @@ function _civicrm_api3_attachment_create_spec(&$spec) {
   $spec = array_merge($spec, _civicrm_api3_attachment_getfields());
   $spec['name']['api.required'] = 1;
   $spec['mime_type']['api.required'] = 1;
-  $spec['entity_table']['api.required'] = 1;
   $spec['entity_id']['api.required'] = 1;
   $spec['upload_date']['api.default'] = 'now';
 }
 
 /**
- * Create an attachment.
+ * Create an Attachment.
  *
  * @param array $params
  *
  * @return array
- *   Array of newly created file property values.
  * @throws API_Exception validation errors
+ * @see Civi\API\Subscriber\DynamicFKAuthorization
  */
 function civicrm_api3_attachment_create($params) {
+
+  if (empty($params['id'])) {
+    // When creating we need either entity_table or field_name
+    civicrm_api3_verify_one_mandatory($params, NULL, array('entity_table', 'field_name'));
+  }
+
   $config = CRM_Core_Config::singleton();
   list($id, $file, $entityFile, $name, $content, $moveFile, $isTrusted, $returnContent) = _civicrm_api3_attachment_parse_params($params);
 
@@ -145,6 +163,14 @@ function civicrm_api3_attachment_create($params) {
     rename($moveFile, $path);
   }
 
+  // Save custom field to entity
+  if (!$id && empty($params['entity_table']) && isset($params['field_name'])) {
+    civicrm_api3('custom_value', 'create', array(
+      'entity_id' => $params['entity_id'],
+      $params['field_name'] => $fileDao->id,
+    ));
+  }
+
   $result = array(
     $fileDao->id => _civicrm_api3_attachment_format_result($fileDao, $entityFileDao, $returnContent, $isTrusted),
   );
@@ -162,7 +188,7 @@ function _civicrm_api3_attachment_get_spec(&$spec) {
 }
 
 /**
- * Get attachment.
+ * Get Attachment.
  *
  * @param array $params
  *
@@ -182,7 +208,7 @@ function civicrm_api3_attachment_get($params) {
 }
 
 /**
- * Adjust metadata for attachment delete action.
+ * Adjust metadata for Attachment delete action.
  *
  * @param $spec
  */
@@ -196,7 +222,7 @@ function _civicrm_api3_attachment_delete_spec(&$spec) {
 }
 
 /**
- * Delete attachment.
+ * Delete Attachment.
  *
  * @param array $params
  *
@@ -341,6 +367,11 @@ function _civicrm_api3_attachment_parse_params($params) {
     }
   }
 
+  if (empty($params['entity_table']) && isset($params['field_name'])) {
+    $tableInfo = CRM_Core_BAO_CustomField::getTableColumnGroup(intval(str_replace('custom_', '', $params['field_name'])));
+    $entityFile['entity_table'] = $tableInfo[0];
+  }
+
   $name = NULL;
   if (array_key_exists('name', $params)) {
     if ($params['name'] != basename($params['name']) || preg_match(':[/\\\\]:', $params['name'])) {
@@ -431,6 +462,11 @@ function _civicrm_api3_attachment_getfields() {
     'description' => 'The logical file name (not searchable)',
     'type' => CRM_Utils_Type::T_STRING,
   );
+  $spec['field_name'] = array(
+    'title' => 'Field Name (write-once)',
+    'description' => 'Alternative to "entity_table" param - sets custom field value.',
+    'type' => CRM_Utils_Type::T_STRING,
+  );
   $spec['mime_type'] = $fileFields['mime_type'];
   $spec['description'] = $fileFields['description'];
   $spec['upload_date'] = $fileFields['upload_date'];