Attachment api - add custom field handling
authorColeman Watts <coleman@civicrm.org>
Tue, 3 Feb 2015 21:27:36 +0000 (16:27 -0500)
committerColeman Watts <coleman@civicrm.org>
Tue, 3 Feb 2015 21:27:36 +0000 (16:27 -0500)
api/v3/Attachment.php
tests/phpunit/api/v3/AttachmentTest.php

index 455fd96517e2ccbf1368a895333360b3176668cd..2924813cc2fcf3cc5c2a4feef40df21acda53a89 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,
@@ -78,7 +94,6 @@ 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';
 }
@@ -93,6 +108,12 @@ function _civicrm_api3_attachment_create_spec(&$spec) {
  * @throws API_Exception validation errors
  */
 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 +166,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),
   );
@@ -341,6 +370,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 +465,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'];
index 76fc5eca2617379dbb1253081b609699422be0a3..940bef352a7b6b25b274e5842591e7048d577100 100644 (file)
@@ -552,6 +552,41 @@ class api_v3_AttachmentTest extends CiviUnitTestCase {
     $this->assertAttachmentExistence(FALSE, $createResults['delme']['second']);
   }
 
+  /**
+   *
+   */
+  public function testCustomFileField() {
+    $customGroup = $this->customGroupCreate(array('title' => 'attachment_test_group'));
+    $params = array(
+      'custom_group_id' => $customGroup['id'],
+      'name' => 'test_file_attachment',
+      'label' => 'test_file_attachment',
+      'html_type' => 'File',
+      'data_type' => 'File',
+      'is_active' => 1,
+    );
+    $customField = $this->callAPISuccess('custom_field', 'create', $params);
+    $cfId = 'custom_' . $customField['id'];
+
+    $cid = $this->individualCreate();
+
+    $attachment = $this->callAPISuccess('attachment', 'create', array(
+      'name' => self::getFilePrefix() . 'testCustomFileField.txt',
+      'mime_type' => 'text/plain',
+      'content' => 'My test content',
+      'field_name' => $cfId,
+      'entity_id' => $cid,
+    ));
+    $this->assertAttachmentExistence(TRUE, $attachment);
+
+    $result = $this->callAPISuccess('contact', 'getsingle', array(
+      'id' => $cid,
+      'return' => $cfId,
+    ));
+
+    $this->assertEquals($attachment['id'], $result[$cfId]);
+  }
+
   /**
    * @param $exists
    * @param array $apiResult