static function copyEntityFile($oldEntityTable, $oldEntityId, $newEntityTable, $newEntityId) { $oldEntityFile = new CRM_Core_DAO_EntityFile(); $oldEntityFile->entity_id = $oldEntityId; $oldEntityFile->entity_table = $oldEntityTable; $oldEntityFile->find(); while ($oldEntityFile->fetch()) { $newEntityFile = new CRM_Core_DAO_EntityFile(); $newEntityFile->entity_id = $newEntityId; $newEntityFile->entity_table = $newEntityTable; $newEntityFile->file_id = $oldEntityFile->file_id; $newEntityFile->save(); } }
/** * @param array $customParams * * @throws Exception */ public static function create(&$customParams) { if (empty($customParams) || !is_array($customParams)) { return; } foreach ($customParams as $tableName => $tables) { foreach ($tables as $index => $fields) { $sqlOP = NULL; $hookID = NULL; $hookOP = NULL; $entityID = NULL; $isMultiple = FALSE; $set = array(); $params = array(); $count = 1; foreach ($fields as $field) { if (!$sqlOP) { $entityID = $field['entity_id']; $hookID = $field['custom_group_id']; $isMultiple = $field['is_multiple']; if (array_key_exists('id', $field)) { $sqlOP = "UPDATE {$tableName} "; $where = " WHERE id = %{$count}"; $params[$count] = array($field['id'], 'Integer'); $count++; $hookOP = 'edit'; } else { $sqlOP = "INSERT INTO {$tableName} "; $where = NULL; $hookOP = 'create'; } } // fix the value before we store it $value = $field['value']; $type = $field['type']; switch ($type) { case 'StateProvince': $type = 'Integer'; if (is_array($value)) { $value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $value) . CRM_Core_DAO::VALUE_SEPARATOR; $type = 'String'; } elseif (!is_numeric($value) && !strstr($value, CRM_Core_DAO::VALUE_SEPARATOR)) { //fix for multi select state, CRM-3437 $mulValues = explode(',', $value); $validStates = array(); foreach ($mulValues as $key => $stateVal) { $states = array(); $states['state_province'] = trim($stateVal); CRM_Utils_Array::lookupValue($states, 'state_province', CRM_Core_PseudoConstant::stateProvince(), TRUE); if (empty($states['state_province_id'])) { CRM_Utils_Array::lookupValue($states, 'state_province', CRM_Core_PseudoConstant::stateProvinceAbbreviation(), TRUE); } $validStates[] = CRM_Utils_Array::value('state_province_id', $states); } $value = implode(CRM_Core_DAO::VALUE_SEPARATOR, $validStates); $type = 'String'; } elseif (!$value) { // CRM-3415 // using type of timestamp allows us to sneak in a null into db // gross but effective hack $value = NULL; $type = 'Timestamp'; } else { $type = 'String'; } break; case 'Country': $type = 'Integer'; $mulValues = explode(',', $value); if (is_array($value)) { $value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $value) . CRM_Core_DAO::VALUE_SEPARATOR; $type = 'String'; } elseif (!is_numeric($value) && !strstr($value, CRM_Core_DAO::VALUE_SEPARATOR)) { //fix for multi select country, CRM-3437 $mulValues = explode(',', $value); $validCountries = array(); foreach ($mulValues as $key => $countryVal) { $countries = array(); $countries['country'] = trim($countryVal); CRM_Utils_Array::lookupValue($countries, 'country', CRM_Core_PseudoConstant::country(), TRUE); if (empty($countries['country_id'])) { CRM_Utils_Array::lookupValue($countries, 'country', CRM_Core_PseudoConstant::countryIsoCode(), TRUE); } $validCountries[] = CRM_Utils_Array::value('country_id', $countries); } $value = implode(CRM_Core_DAO::VALUE_SEPARATOR, $validCountries); $type = 'String'; } elseif (!$value) { // CRM-3415 // using type of timestamp allows us to sneak in a null into db // gross but effective hack $value = NULL; $type = 'Timestamp'; } else { $type = 'String'; } break; case 'File': if (!$field['file_id']) { CRM_Core_Error::fatal(); } // need to add/update civicrm_entity_file $entityFileDAO = new CRM_Core_DAO_EntityFile(); $entityFileDAO->file_id = $field['file_id']; $entityFileDAO->find(TRUE); $entityFileDAO->entity_table = $field['table_name']; $entityFileDAO->entity_id = $field['entity_id']; $entityFileDAO->file_id = $field['file_id']; $entityFileDAO->save(); $entityFileDAO->free(); $value = $field['file_id']; $type = 'String'; break; case 'Date': $value = CRM_Utils_Date::isoToMysql($value); break; case 'Int': if (is_numeric($value)) { $type = 'Integer'; } else { $type = 'Timestamp'; } break; case 'ContactReference': if ($value == NULL) { $type = 'Timestamp'; } else { $type = 'Integer'; } break; case 'RichTextEditor': $type = 'String'; break; case 'Boolean': //fix for CRM-3290 $value = CRM_Utils_String::strtoboolstr($value); if ($value === FALSE) { $type = 'Timestamp'; } break; default: break; } if (strtolower($value) === "null") { // when unsetting a value to null, we don't need to validate the type // https://projectllr.atlassian.net/browse/VGQBMP-20 $set[$field['column_name']] = $value; } else { $set[$field['column_name']] = "%{$count}"; $params[$count] = array($value, $type); $count++; } } if (!empty($set)) { $setClause = array(); foreach ($set as $n => $v) { $setClause[] = "{$n} = {$v}"; } $setClause = implode(',', $setClause); if (!$where) { // do this only for insert $set['entity_id'] = "%{$count}"; $params[$count] = array($entityID, 'Integer'); $count++; $fieldNames = implode(',', array_keys($set)); $fieldValues = implode(',', array_values($set)); $query = "{$sqlOP} ( {$fieldNames} ) VALUES ( {$fieldValues} )"; // for multiple values we dont do on duplicate key update if (!$isMultiple) { $query .= " ON DUPLICATE KEY UPDATE {$setClause}"; } } else { $query = "{$sqlOP} SET {$setClause} {$where}"; } $dao = CRM_Core_DAO::executeQuery($query, $params); CRM_Utils_Hook::custom($hookOP, $hookID, $entityID, $fields); } } } }
/** * Delete an existing File. * * @param array $params * Array per getfields metadata. * * @return array * API result array */ function civicrm_api3_file_delete($params) { civicrm_api3_verify_mandatory($params, NULL, array('id')); $check = FALSE; $entityFileDAO = new CRM_Core_DAO_EntityFile(); $entityFileDAO->file_id = $params['id']; if ($entityFileDAO->find()) { $check = $entityFileDAO->delete(); } $fileDAO = new CRM_Core_DAO_File(); $fileDAO->id = $params['id']; if ($fileDAO->find(TRUE)) { $check = $fileDAO->delete(); } return $check ? NULL : civicrm_api3_create_error('Error while deleting a file.'); }
/** * Create an Attachment. * * @param array $params * * @return array * @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); $fileDao = new CRM_Core_BAO_File(); $entityFileDao = new CRM_Core_DAO_EntityFile(); if ($id) { $fileDao->id = $id; if (!$fileDao->find(TRUE)) { throw new API_Exception("Invalid ID"); } $entityFileDao->file_id = $id; if (!$entityFileDao->find(TRUE)) { throw new API_Exception("Cannot modify orphaned file"); } } if (!$id && !is_string($content) && !is_string($moveFile)) { throw new API_Exception("Mandatory key(s) missing from params array: 'id' or 'content' or 'options.move-file'"); } if (!$isTrusted && $moveFile) { throw new API_Exception("options.move-file is only supported on secure calls"); } if (is_string($content) && is_string($moveFile)) { throw new API_Exception("'content' and 'options.move-file' are mutually exclusive"); } if ($id && !$isTrusted && isset($file['upload_date']) && $file['upload_date'] != CRM_Utils_Date::isoToMysql($fileDao->upload_date)) { throw new API_Exception("Cannot modify upload_date" . var_export(array($file['upload_date'], $fileDao->upload_date, CRM_Utils_Date::isoToMysql($fileDao->upload_date)), TRUE)); } if ($id && $name && $name != CRM_Utils_File::cleanFileName($fileDao->uri)) { throw new API_Exception("Cannot modify name"); } $fileDao->copyValues($file); if (!$id) { $fileDao->uri = CRM_Utils_File::makeFileName($name); } $fileDao->save(); $entityFileDao->copyValues($entityFile); $entityFileDao->file_id = $fileDao->id; $entityFileDao->save(); $path = $config->customFileUploadDir . DIRECTORY_SEPARATOR . $fileDao->uri; if (is_string($content)) { file_put_contents($path, $content); } elseif (is_string($moveFile)) { // CRM-17432 Do not use rename() since it will break file permissions. // Also avoid move_uplaoded_file() because the API can use options.move-file. copy($moveFile, $path); unlink($moveFile); } // 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)); return civicrm_api3_create_success($result, $params, 'Attachment', 'create'); }
/** * Process the form submission. */ public function postProcess() { if ($this->_action & CRM_Core_Action::DELETE) { CRM_Core_BAO_MessageTemplate::del($this->_id); } elseif ($this->_action & CRM_Core_Action::VIEW) { // currently, the above action is used solely for previewing default workflow templates CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/messageTemplates', 'selectedChild=workflow&reset=1')); } else { $params = array(); // store the submitted values in an array $params = $this->controller->exportValues($this->_name); if ($this->_action & CRM_Core_Action::UPDATE) { $params['id'] = $this->_id; } if (!empty($params['file_type'])) { unset($params['msg_html']); unset($params['msg_text']); CRM_Utils_File::formatFile($params, 'file_id'); } elseif (!empty($this->_id)) { $entityFileDAO = new CRM_Core_DAO_EntityFile(); $entityFileDAO->entity_id = $this->_id; $entityFileDAO->entity_table = 'civicrm_msg_template'; if ($entityFileDAO->find(TRUE)) { $fileDAO = new CRM_Core_DAO_File(); $fileDAO->id = $entityFileDAO->file_id; $fileDAO->find(TRUE); $entityFileDAO->delete(); $fileDAO->delete(); } } if ($this->_workflow_id) { $params['workflow_id'] = $this->_workflow_id; $params['is_active'] = TRUE; } $messageTemplate = CRM_Core_BAO_MessageTemplate::add($params); CRM_Core_Session::setStatus(ts('The Message Template \'%1\' has been saved.', array(1 => $messageTemplate->msg_title)), ts('Saved'), 'success'); if ($this->_workflow_id) { CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/messageTemplates', 'selectedChild=workflow&reset=1')); } else { CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/messageTemplates', 'selectedChild=user&reset=1')); } } }
/** * Returns all files assigned to a single entity instance. * * @param object $entityID id of the supported entity. * @param string $entity_table * * @return array nested array of entity-file property values. * @access public */ function civicrm_files_by_entity_get($entityID, $entityTable = 'civicrm_contact', $fileID = NULL) { if (!$entityID) { return civicrm_create_error('Required parameters missing'); } require_once 'CRM/Core/DAO/EntityFile.php'; require_once 'CRM/Core/DAO/File.php'; $entityFileDAO = new CRM_Core_DAO_EntityFile(); $entityFileDAO->entity_table = $entityTable; $entityFileDAO->entity_id = $entityID; if ($fileID) { $entityFileDAO->file_id = $fileID; } if ($entityFileDAO->find()) { $entityFile = array(); while ($entityFileDAO->fetch()) { _civicrm_object_to_array($entityFileDAO, $entityFile); $files[$entityFileDAO->file_id] = $entityFile; if (array_key_exists('file_id', $files[$entityFileDAO->file_id])) { $fileDAO = new CRM_Core_DAO_File(); $fileDAO->id = $entityFile['file_id']; $fileDAO->find(TRUE); _civicrm_object_to_array($fileDAO, $files[$entityFileDAO->file_id]); } if (CRM_Utils_Array::value('file_type_id', $files[$entityFileDAO->file_id])) { $files[$entityFileDAO->file_id]['file_type'] = CRM_Core_OptionGroup::getLabel('file_type', $files[$entityFileDAO->file_id]['file_type_id']); } } } else { return civicrm_create_error('Exact match not found'); } return $files; }