* "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,
$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';
}
* @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);
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),
);
}
}
+ 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'])) {
'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'];
$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