/** * Create a file * * This API is used for creating a file * * @param array $params an associative array of name/value property values of civicrm_file * @return array of newly created file property values. * @access public */ function crm_create_file($params) { if (!is_array($params)) { return _crm_error('Params is not an array.'); } if (!isset($params['file_type_id'])) { return _crm_error('Required parameter missing.'); } if (!isset($params['upload_date'])) { $params['upload_date'] = date("Ymd"); } require_once 'CRM/Core/DAO/File.php'; $fileDAO = new CRM_Core_DAO_File(); $properties = array('id', 'file_type_id', 'mime_type', 'uri', 'document', 'description', 'upload_date'); foreach ($properties as $name) { if (array_key_exists($name, $params)) { $fileDAO->{$name} = $params[$name]; } } $fileDAO->save(); $file = array(); _crm_object_to_array($fileDAO, $file); return $file; }
static function filePostProcess($data, $fileTypeID, $entityTable, $entityID, $entitySubtype, $overwrite = TRUE, $fileParams = NULL, $uploadName = 'uploadFile', $mimeType = null) { if (!$mimeType) { CRM_Core_Error::fatal(ts('Mime Type is now a required parameter')); } $config = CRM_Core_Config::singleton(); $path = explode('/', $data); $filename = $path[count($path) - 1]; // rename this file to go into the secure directory if ($entitySubtype) { $directoryName = $config->customFileUploadDir . $entitySubtype . DIRECTORY_SEPARATOR . $entityID; } else { $directoryName = $config->customFileUploadDir; } CRM_Utils_File::createDir($directoryName); if (!rename($data, $directoryName . DIRECTORY_SEPARATOR . $filename)) { CRM_Core_Error::fatal(ts('Could not move custom file to custom upload directory')); break; } // to get id's if ($overwrite && $fileTypeID) { list($sql, $params) = self::sql($entityTable, $entityID, $fileTypeID); } else { list($sql, $params) = self::sql($entityTable, $entityID, 0); } $dao = CRM_Core_DAO::executeQuery($sql, $params); $dao->fetch(); $fileDAO = new CRM_Core_DAO_File(); $op = 'create'; if (isset($dao->cfID) && $dao->cfID) { $op = 'edit'; $fileDAO->id = $dao->cfID; unlink($directoryName . DIRECTORY_SEPARATOR . $dao->uri); } if (!empty($fileParams)) { $fileDAO->copyValues($fileParams); } $fileDAO->uri = $filename; $fileDAO->mime_type = $mimeType; $fileDAO->file_type_id = $fileTypeID; $fileDAO->upload_date = date('Ymdhis'); $fileDAO->save(); // need to add/update civicrm_entity_file $entityFileDAO = new CRM_Core_DAO_EntityFile(); if (isset($dao->cefID) && $dao->cefID) { $entityFileDAO->id = $dao->cefID; } $entityFileDAO->entity_table = $entityTable; $entityFileDAO->entity_id = $entityID; $entityFileDAO->file_id = $fileDAO->id; $entityFileDAO->save(); //save static tags if (!empty($fileParams['tag'])) { CRM_Core_BAO_EntityTag::create($fileParams['tag'], 'civicrm_file', $entityFileDAO->id); } //save free tags if (isset($fileParams['attachment_taglist']) && !empty($fileParams['attachment_taglist'])) { CRM_Core_Form_Tag::postProcess($fileParams['attachment_taglist'], $entityFileDAO->id, 'civicrm_file', CRM_Core_DAO::$_nullObject); } // lets call the post hook here so attachments code can do the right stuff CRM_Utils_Hook::post($op, 'File', $fileDAO->id, $fileDAO); }
public function filePostProcess($data, $fileID, $entityTable, $entityID, $entitySubtype, $overwrite = TRUE, $fileParams = NULL, $uploadName = 'uploadFile', $mimeType) { $config = CRM_Core_Config::singleton(); $path = explode('/', $data); $filename = $path[count($path) - 1]; // rename this file to go into the secure directory if ($entitySubtype) { $directoryName = $config->customFileUploadDir . $entitySubtype . DIRECTORY_SEPARATOR . $entityID; } else { $directoryName = $config->customFileUploadDir; } CRM_Utils_File::createDir($directoryName); if (!rename($data, $directoryName . DIRECTORY_SEPARATOR . $filename)) { CRM_Core_Error::fatal(ts('Could not move custom file to custom upload directory')); break; } // to get id's if ($overwrite && $fileID) { list($sql, $params) = self::sql($entityTable, $entityID, $fileID); } else { list($sql, $params) = self::sql($entityTable, $entityID, 0); } $dao = CRM_Core_DAO::executeQuery($sql, $params); $dao->fetch(); if (!$mimeType) { CRM_Core_Error::fatal(); } $fileDAO = new CRM_Core_DAO_File(); if (isset($dao->cfID) && $dao->cfID) { $fileDAO->id = $dao->cfID; unlink($directoryName . DIRECTORY_SEPARATOR . $dao->uri); } if (!empty($fileParams)) { $fileDAO->copyValues($fileParams); } $fileDAO->uri = $filename; $fileDAO->mime_type = $mimeType; $fileDAO->file_type_id = $fileID; $fileDAO->upload_date = date('Ymdhis'); $fileDAO->save(); // need to add/update civicrm_entity_file $entityFileDAO = new CRM_Core_DAO_EntityFile(); if (isset($dao->cefID) && $dao->cefID) { $entityFileDAO->id = $dao->cefID; } $entityFileDAO->entity_table = $entityTable; $entityFileDAO->entity_id = $entityID; $entityFileDAO->file_id = $fileDAO->id; $entityFileDAO->save(); }
/** * Format custom fields before inserting. * * @param int $customFieldId * Custom field id. * @param array $customFormatted * Formatted array. * @param mix $value * Value of custom field. * @param string $customFieldExtend * Custom field extends. * @param int $customValueId * Custom option value id. * @param int $entityId * Entity id (contribution, membership...). * @param bool $inline * Consider inline custom groups only. * @param bool $checkPermission * If false, do not include permissioning clause. * @param bool $includeViewOnly * If true, fields marked 'View Only' are included. Required for APIv3. * * @return array|NULL * formatted custom field array */ public static function formatCustomField($customFieldId, &$customFormatted, $value, $customFieldExtend, $customValueId = NULL, $entityId = NULL, $inline = FALSE, $checkPermission = TRUE, $includeViewOnly = FALSE) { //get the custom fields for the entity //subtype and basic type $customDataSubType = NULL; if ($customFieldExtend) { // This is the case when getFieldsForImport() requires fields // of subtype and its parent.CRM-5143 // CRM-16065 - Custom field set data not being saved if contact has more than one contact sub type $customDataSubType = array_intersect(CRM_Contact_BAO_ContactType::subTypes(), (array) $customFieldExtend); if (!empty($customDataSubType) && is_array($customDataSubType)) { $customFieldExtend = CRM_Contact_BAO_ContactType::getBasicType($customDataSubType); if (is_array($customFieldExtend)) { $customFieldExtend = array_unique(array_values($customFieldExtend)); } } } $customFields = CRM_Core_BAO_CustomField::getFields($customFieldExtend, FALSE, $inline, $customDataSubType, NULL, FALSE, FALSE, $checkPermission); if (!array_key_exists($customFieldId, $customFields)) { return NULL; } // return if field is a 'code' field if (!$includeViewOnly && !empty($customFields[$customFieldId]['is_view'])) { return NULL; } list($tableName, $columnName, $groupID) = self::getTableColumnGroup($customFieldId); if (!$customValueId && !$customFields[$customFieldId]['is_multiple'] && $entityId) { $query = "\nSELECT id\n FROM {$tableName}\n WHERE entity_id={$entityId}"; $customValueId = CRM_Core_DAO::singleValueQuery($query); } //fix checkbox, now check box always submits values if ($customFields[$customFieldId]['html_type'] == 'CheckBox') { if ($value) { // Note that only during merge this is not an array, and you can directly use value if (is_array($value)) { $selectedValues = array(); foreach ($value as $selId => $val) { if ($val) { $selectedValues[] = $selId; } } if (!empty($selectedValues)) { $value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $selectedValues) . CRM_Core_DAO::VALUE_SEPARATOR; } else { $value = ''; } } } } if ($customFields[$customFieldId]['html_type'] == 'Multi-Select' || $customFields[$customFieldId]['html_type'] == 'AdvMulti-Select') { if ($value) { // Note that only during merge this is not an array, // and you can directly use value, CRM-4385 if (is_array($value)) { $value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, array_values($value)) . CRM_Core_DAO::VALUE_SEPARATOR; } } else { $value = ''; } } if (($customFields[$customFieldId]['html_type'] == 'Multi-Select' || $customFields[$customFieldId]['html_type'] == 'AdvMulti-Select' || $customFields[$customFieldId]['html_type'] == 'CheckBox') && $customFields[$customFieldId]['data_type'] == 'String' && !empty($customFields[$customFieldId]['text_length']) && !empty($value)) { // lets make sure that value is less than the length, else we'll // be losing some data, CRM-7481 if (strlen($value) >= $customFields[$customFieldId]['text_length']) { // need to do a few things here // 1. lets find a new length $newLength = $customFields[$customFieldId]['text_length']; $minLength = strlen($value); while ($newLength < $minLength) { $newLength = $newLength * 2; } // set the custom field meta data to have a length larger than value // alter the custom value table column to match this length CRM_Core_BAO_SchemaHandler::alterFieldLength($customFieldId, $tableName, $columnName, $newLength); } } $date = NULL; if ($customFields[$customFieldId]['data_type'] == 'Date') { if (!CRM_Utils_System::isNull($value)) { $format = $customFields[$customFieldId]['date_format']; $date = CRM_Utils_Date::processDate($value, NULL, FALSE, 'YmdHis', $format); } $value = $date; } if ($customFields[$customFieldId]['data_type'] == 'Float' || $customFields[$customFieldId]['data_type'] == 'Money') { if (!$value) { $value = 0; } if ($customFields[$customFieldId]['data_type'] == 'Money') { $value = CRM_Utils_Rule::cleanMoney($value); } } if (($customFields[$customFieldId]['data_type'] == 'StateProvince' || $customFields[$customFieldId]['data_type'] == 'Country') && empty($value)) { // CRM-3415 $value = 0; } $fileId = NULL; if ($customFields[$customFieldId]['data_type'] == 'File') { if (empty($value)) { return; } $config = CRM_Core_Config::singleton(); $fName = $value['name']; $mimeType = $value['type']; $filename = pathinfo($fName, PATHINFO_BASENAME); // rename this file to go into the secure directory if (!rename($fName, $config->customFileUploadDir . $filename)) { CRM_Core_Error::statusBounce(ts('Could not move custom file to custom upload directory')); } if ($customValueId) { $query = "\nSELECT {$columnName}\n FROM {$tableName}\n WHERE id = %1"; $params = array(1 => array($customValueId, 'Integer')); $fileId = CRM_Core_DAO::singleValueQuery($query, $params); } $fileDAO = new CRM_Core_DAO_File(); if ($fileId) { $fileDAO->id = $fileId; } $fileDAO->uri = $filename; $fileDAO->mime_type = $mimeType; $fileDAO->upload_date = date('Ymdhis'); $fileDAO->save(); $fileId = $fileDAO->id; $value = $filename; } if (!is_array($customFormatted)) { $customFormatted = array(); } if (!array_key_exists($customFieldId, $customFormatted)) { $customFormatted[$customFieldId] = array(); } $index = -1; if ($customValueId) { $index = $customValueId; } if (!array_key_exists($index, $customFormatted[$customFieldId])) { $customFormatted[$customFieldId][$index] = array(); } $customFormatted[$customFieldId][$index] = array('id' => $customValueId > 0 ? $customValueId : NULL, 'value' => $value, 'type' => $customFields[$customFieldId]['data_type'], 'custom_field_id' => $customFieldId, 'custom_group_id' => $groupID, 'table_name' => $tableName, 'column_name' => $columnName, 'file_id' => $fileId, 'is_multiple' => $customFields[$customFieldId]['is_multiple']); //we need to sort so that custom fields are created in the order of entry krsort($customFormatted[$customFieldId]); return $customFormatted; }
/** * Update an existing File. * * @param array $params * Array per getfields metadata. * * @return array */ function civicrm_api3_file_update($params) { if (!isset($params['id'])) { return civicrm_api3_create_error('Required parameter missing'); } $fileDAO = new CRM_Core_DAO_File(); $fileDAO->id = $params['id']; if ($fileDAO->find(TRUE)) { $fileDAO->copyValues($params); if (!$params['upload_date'] && !$fileDAO->upload_date) { $fileDAO->upload_date = date("Ymd"); } $fileDAO->save(); } $file = array(); _civicrm_api3_object_to_array(clone $fileDAO, $file); return $file; }
/** * Format custom fields before inserting * * @param int $customFieldId custom field id * @param array $customFormatted formatted array * @param mix $value value of custom field * @param string $customFieldExtend custom field extends * @param int $customValueId custom option value id * @param int $entityId entity id (contribution, membership...) * * @return array $customFormatted formatted custom field array * @static */ static function formatCustomField($customFieldId, &$customFormatted, $value, $customFieldExtend, $customValueId = null, $entityId = null, $inline = false) { //get the custom fields for the entity //subtype and basic type $customDataSubType = null; if (in_array($customFieldExtend, CRM_Contact_BAO_ContactType::subTypes())) { // This is the case when getFieldsForImport() requires fields // of subtype and its parent.CRM-5143 $customDataSubType = $customFieldExtend; $customFieldExtend = CRM_Contact_BAO_ContactType::getBasicType($customDataSubType); } $customFields = CRM_Core_BAO_CustomField::getFields($customFieldExtend, false, $inline, $customDataSubType); if (!array_key_exists($customFieldId, $customFields)) { return; } // return if field is a 'code' field if (CRM_Utils_Array::value('is_view', $customFields[$customFieldId])) { return; } list($tableName, $columnName, $groupID) = self::getTableColumnGroup($customFieldId); if (is_array($customFieldExtend)) { $customFieldExtend = $customFieldExtend[0]; } if (!$customValueId && !$customFields[$customFieldId]['is_multiple'] && $entityId) { //get the entity table for the custom field require_once "CRM/Core/BAO/CustomQuery.php"; $entityTable = CRM_Core_BAO_CustomQuery::$extendsMap[$customFieldExtend]; $query = "\nSELECT id \n FROM {$tableName}\n WHERE entity_id={$entityId}"; $customValueId = CRM_Core_DAO::singleValueQuery($query); } //fix checkbox, now check box always submits values if ($customFields[$customFieldId]['html_type'] == 'CheckBox') { if ($value) { // Note that only during merge this is not an array, and you can directly use value if (is_array($value)) { $selectedValues = null; foreach ($value as $selId => $val) { if ($val) { $selectedValues .= $selId . CRM_Core_BAO_CustomOption::VALUE_SEPERATOR; } } if ($selectedValues) { $value = CRM_Core_BAO_CustomOption::VALUE_SEPERATOR . $selectedValues; } else { $value = ''; } } } } if ($customFields[$customFieldId]['html_type'] == 'Multi-Select' || $customFields[$customFieldId]['html_type'] == 'AdvMulti-Select') { if ($value) { // Note that only during merge this is not an array, // and you can directly use value, CRM-4385 if (is_array($value)) { $value = CRM_Core_BAO_CustomOption::VALUE_SEPERATOR . implode(CRM_Core_BAO_CustomOption::VALUE_SEPERATOR, array_values($value)) . CRM_Core_BAO_CustomOption::VALUE_SEPERATOR; } } else { $value = ''; } } $date = null; if ($customFields[$customFieldId]['data_type'] == 'Date') { if (!CRM_Utils_System::isNull($value)) { $format = $customFields[$customFieldId]['date_format']; if (in_array($format, array('dd-mm', 'mm/dd'))) { $dateTimeArray = explode(' ', $value); $separator = '/'; if ($format == 'dd-mm') { $separator = '-'; } $value = $dateTimeArray[0] . $separator . '1902'; if (array_key_exists(1, $dateTimeArray)) { $value .= ' ' . $dateTimeArray[1]; } } else { if ($format == 'yy') { $value = "01-01-{$value}"; } } $date = CRM_Utils_Date::processDate($value, null, false, 'YmdHis', $format); } $value = $date; } if ($customFields[$customFieldId]['data_type'] == 'Float' || $customFields[$customFieldId]['data_type'] == 'Money') { if (!$value) { $value = 0; } if ($customFields[$customFieldId]['data_type'] == 'Money') { require_once 'CRM/Utils/Rule.php'; $value = CRM_Utils_Rule::cleanMoney($value); } } if (($customFields[$customFieldId]['data_type'] == 'StateProvince' || $customFields[$customFieldId]['data_type'] == 'Country') && empty($value)) { // CRM-3415 $value = 0; } $fileId = null; if ($customFields[$customFieldId]['data_type'] == 'File') { if (empty($value)) { return; } require_once 'CRM/Core/DAO/File.php'; $config =& CRM_Core_Config::singleton(); $fName = $value['name']; $mimeType = $value['type']; $path = explode('/', $fName); $filename = $path[count($path) - 1]; // rename this file to go into the secure directory if (!rename($fName, $config->customFileUploadDir . $filename)) { CRM_Core_Error::statusBounce(ts('Could not move custom file to custom upload directory')); break; } if ($customValueId) { $query = "\nSELECT {$columnName}\n FROM {$tableName}\n WHERE id = %1"; $params = array(1 => array($customValueId, 'Integer')); $fileId = CRM_Core_DAO::singleValueQuery($query, $params); } $fileDAO = new CRM_Core_DAO_File(); if ($fileId) { $fileDAO->id = $fileId; } $fileDAO->uri = $filename; $fileDAO->mime_type = $mimeType; $fileDAO->upload_date = date('Ymdhis'); $fileDAO->save(); $fileId = $fileDAO->id; $value = $filename; } if (!is_array($customFormatted)) { $customFormatted = array(); } if (!array_key_exists($customFieldId, $customFormatted)) { $customFormatted[$customFieldId] = array(); } $index = -1; if ($customValueId) { $index = $customValueId; } if (!array_key_exists($index, $customFormatted[$customFieldId])) { $customFormatted[$customFieldId][$index] = array(); } $customFormatted[$customFieldId][$index] = array('id' => $customValueId > 0 ? $customValueId : null, 'value' => $value, 'type' => $customFields[$customFieldId]['data_type'], 'custom_field_id' => $customFieldId, 'custom_group_id' => $groupID, 'table_name' => $tableName, 'column_name' => $columnName, 'file_id' => $fileId, 'is_multiple' => $customFields[$customFieldId]['is_multiple']); //we need to sort so that custom fields are created in the order of entry krsort($customFormatted[$customFieldId]); return $customFormatted; }
function saveDocument($contact_id, $filename, $mimetype, $filetype, $date, $date_from, $date_to, $comment) { $docs = get_docs_table(); $file = new CRM_Core_DAO_File(); $file->mime_type = $mimetype; $file->uri = $filename; $file->upload_date = date('Ymd'); $file->save(); $entityFile = new CRM_Core_DAO_EntityFile(); $entityFile->file_id = $file->id; $entityFile->entity_id = $contact_id; $entityFile->entity_table = $docs['table']; $entityFile->save(); $stmt = "INSERT INTO {$docs['table']}\n SET {$docs['field_filetype']} = '{$filetype}'\n , {$docs['field_date']} = '{$date}'\n , {$docs['field_from']} = '{$date_from}'\n , {$docs['field_to']} = '{$date_to}'\n , {$docs['field_comment']} = '{$comment}'\n , {$docs['field_file']} = {$file->id}\n , entity_id = {$contact_id}\n "; $res =& CRM_Core_DAO::executeQuery($stmt); return $file->id; }
/** * Update an existing file * * This api is used for updating an existing file. * Required parrmeters : id of a file * * @param Array $params an associative array of name/value property values of civicrm_file * * @return array of updated file object property values * @access public */ function &civicrm_file_update(&$params) { if (!is_array($params)) { return civicrm_create_error('Params is not an array'); } if (!isset($params['id'])) { return civicrm_create_error('Required parameter missing'); } require_once 'CRM/Core/DAO/File.php'; $fileDAO = new CRM_Core_DAO_File(); $fileDAO->id = $params['id']; if ($fileDAO->find(TRUE)) { $fileDAO->copyValues($params); if (!$params['upload_date'] && !$fileDAO->upload_date) { $fileDAO->upload_date = date("Ymd"); } $fileDAO->save(); } $file = array(); _civicrm_object_to_array(clone $fileDAO, $file); return $file; }