/** * A static function wrapper that deletes the various objects that are * connected to a file object (i.e. file, entityFile and customValue */ public static function deleteFileReferences($fileID, $entityID, $fieldID) { $fileDAO = new CRM_Core_DAO_File(); $fileDAO->id = $fileID; if (!$fileDAO->find(TRUE)) { CRM_Core_Error::fatal(); } // lets call a pre hook before the delete, so attachments hooks can get the info before things // disappear CRM_Utils_Hook::pre('delete', 'File', $fileID, $fileDAO); // get the table and column name list($tableName, $columnName, $groupID) = CRM_Core_BAO_CustomField::getTableColumnGroup($fieldID); $entityFileDAO = new CRM_Core_DAO_EntityFile(); $entityFileDAO->file_id = $fileID; $entityFileDAO->entity_id = $entityID; $entityFileDAO->entity_table = $tableName; if (!$entityFileDAO->find(TRUE)) { CRM_Core_Error::fatal(); } $entityFileDAO->delete(); $fileDAO->delete(); // also set the value to null of the table and column $query = "UPDATE {$tableName} SET {$columnName} = null WHERE {$columnName} = %1"; $params = array(1 => array($fileID, 'Integer')); CRM_Core_DAO::executeQuery($query, $params); }
/** * Returns the list of fields that can be exported * * @param bool $prefix * * @return array */ static function &export($prefix = false) { if (!self::$_export) { self::$_export = array(); $fields = self::fields(); foreach ($fields as $name => $field) { if (CRM_Utils_Array::value('export', $field)) { if ($prefix) { self::$_export['file'] =& $fields[$name]; } else { self::$_export[$name] =& $fields[$name]; } } } } return self::$_export; }
/** * Build the entity-specific custom data into the group tree on a per-field basis * * @param object $dao * Object representing the custom field to be populated into the groupTree. * @param array $groupTree * (reference) the group tree being build. * @param string $table * Table name. * @param int $groupID * Custom group ID. * @param int $fieldID * Custom field ID. */ public static function buildCustomFieldData($dao, &$groupTree, $table, $groupID, $fieldID) { $column = $groupTree[$groupID]['fields'][$fieldID]['column_name']; $idName = "{$table}_id"; $fieldName = "{$table}_{$column}"; $dataType = $groupTree[$groupID]['fields'][$fieldID]['data_type']; if ($dataType == 'File') { if (isset($dao->{$fieldName})) { $config = CRM_Core_Config::singleton(); $fileDAO = new CRM_Core_DAO_File(); $fileDAO->id = $dao->{$fieldName}; if ($fileDAO->find(TRUE)) { $entityIDName = "{$table}_entity_id"; $customValue['id'] = $dao->{$idName}; $customValue['data'] = $fileDAO->uri; $customValue['fid'] = $fileDAO->id; $customValue['fileURL'] = CRM_Utils_System::url('civicrm/file', "reset=1&id={$fileDAO->id}&eid={$dao->{$entityIDName}}"); $customValue['displayURL'] = NULL; $deleteExtra = ts('Are you sure you want to delete attached file.'); $deleteURL = array(CRM_Core_Action::DELETE => array('name' => ts('Delete Attached File'), 'url' => 'civicrm/file', 'qs' => 'reset=1&id=%%id%%&eid=%%eid%%&fid=%%fid%%&action=delete', 'extra' => 'onclick = "if (confirm( \'' . $deleteExtra . '\' ) ) this.href+=\'&confirmed=1\'; else return false;"')); $customValue['deleteURL'] = CRM_Core_Action::formLink($deleteURL, CRM_Core_Action::DELETE, array('id' => $fileDAO->id, 'eid' => $dao->{$entityIDName}, 'fid' => $fieldID), ts('more'), FALSE, 'file.manage.delete', 'File', $fileDAO->id); $customValue['deleteURLArgs'] = CRM_Core_BAO_File::deleteURLArgs($table, $dao->{$entityIDName}, $fileDAO->id); $customValue['fileName'] = CRM_Utils_File::cleanFileName(basename($fileDAO->uri)); if ($fileDAO->mime_type == "image/jpeg" || $fileDAO->mime_type == "image/pjpeg" || $fileDAO->mime_type == "image/gif" || $fileDAO->mime_type == "image/x-png" || $fileDAO->mime_type == "image/png") { $customValue['displayURL'] = $customValue['fileURL']; $entityId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_EntityFile', $fileDAO->id, 'entity_id', 'file_id'); $customValue['imageURL'] = str_replace('persist/contribute', 'custom', $config->imageUploadURL) . $fileDAO->uri; list($path) = CRM_Core_BAO_File::path($fileDAO->id, $entityId, NULL, NULL); if ($path && file_exists($path)) { list($imageWidth, $imageHeight) = getimagesize($path); list($imageThumbWidth, $imageThumbHeight) = CRM_Contact_BAO_Contact::getThumbSize($imageWidth, $imageHeight); $customValue['imageThumbWidth'] = $imageThumbWidth; $customValue['imageThumbHeight'] = $imageThumbHeight; } } } } else { $customValue = array('id' => $dao->{$idName}, 'data' => ''); } } else { $customValue = array('id' => $dao->{$idName}, 'data' => $dao->{$fieldName}); } if (!array_key_exists('customValue', $groupTree[$groupID]['fields'][$fieldID])) { $groupTree[$groupID]['fields'][$fieldID]['customValue'] = array(); } if (empty($groupTree[$groupID]['fields'][$fieldID]['customValue'])) { $groupTree[$groupID]['fields'][$fieldID]['customValue'] = array(1 => $customValue); } else { $groupTree[$groupID]['fields'][$fieldID]['customValue'][] = $customValue; } }
public function delete($useWhere = FALSE) { list($fileID, $entityID, $fieldID) = func_get_args(); // get the table and column name list($tableName, $columnName, $groupID) = CRM_Core_BAO_CustomField::getTableColumnGroup($fieldID); $entityFileDAO = new CRM_Core_DAO_EntityFile(); $entityFileDAO->file_id = $fileID; $entityFileDAO->entity_id = $entityID; $entityFileDAO->entity_table = $tableName; if ($entityFileDAO->find(TRUE)) { $entityFileDAO->delete(); } else { CRM_Core_Error::fatal(); } $fileDAO = new CRM_Core_DAO_File(); $fileDAO->id = $fileID; if ($fileDAO->find(TRUE)) { $fileDAO->delete(); } else { CRM_Core_Error::fatal(); } // also set the value to null of the table and column $query = "UPDATE {$tableName} SET {$columnName} = null WHERE {$columnName} = %1"; $params = array(1 => array($fileID, 'Integer')); CRM_Core_DAO::executeQuery($query, $params); }
/** * Get custom groups/fields for type of entity. * * An array containing all custom groups and their custom fields is returned. * * @param string $entityType - of the contact whose contact type is needed * @param int $entityId - optional - id of entity if we need to populate the tree with custom values. * @param int $groupId - optional group id (if we need it for a single group only) * - if groupId is 0 it gets for inline groups only * - if groupId is -1 we get for all groups * * @return array $groupTree - array consisting of all groups and fields and optionally populated with custom data values. * * @access public * * @static * */ public static function &getTree($entityType, &$form, $entityID = null, $groupID = null, $subType = null, $subName = null) { if ($entityID) { $entityID = CRM_Utils_Type::escape($entityID, 'Integer'); } require_once 'CRM/Core/Action.php'; // create a new tree $groupTree = array(); $strSelect = $strFrom = $strWhere = $orderBy = ''; $tableData = array(); // using tableData to build the queryString $tableData = array('civicrm_custom_field' => array('id', 'label', 'column_name', 'data_type', 'html_type', 'default_value', 'attributes', 'is_required', 'is_view', 'help_pre', 'help_post', 'options_per_line', 'start_date_years', 'end_date_years', 'date_format', 'time_format', 'option_group_id'), 'civicrm_custom_group' => array('id', 'name', 'table_name', 'title', 'help_pre', 'help_post', 'collapse_display', 'is_multiple', 'extends', 'extends_entity_column_id', 'extends_entity_column_value', 'max_multiple')); // create select $select = array(); foreach ($tableData as $tableName => $tableColumn) { foreach ($tableColumn as $columnName) { $alias = $tableName . "_" . $columnName; $select[] = "{$tableName}.{$columnName} as {$tableName}_{$columnName}"; } } $strSelect = "SELECT " . implode(', ', $select); // from, where, order by $strFrom = "\nFROM civicrm_custom_group\nLEFT JOIN civicrm_custom_field ON (civicrm_custom_field.custom_group_id = civicrm_custom_group.id)\n"; // if entity is either individual, organization or household pls get custom groups for 'contact' too. if ($entityType == "Individual" || $entityType == 'Organization' || $entityType == 'Household') { $in = "'{$entityType}', 'Contact'"; } else { if (strpos($entityType, "'") !== false) { // this allows the calling function to send in multiple entity types $in = $entityType; } else { // quote it $in = "'{$entityType}'"; } } if ($subType) { $subType = CRM_Core_DAO::VALUE_SEPARATOR . trim($subType, CRM_Core_DAO::VALUE_SEPARATOR) . CRM_Core_DAO::VALUE_SEPARATOR; $strWhere = "\nWHERE civicrm_custom_group.is_active = 1 \n AND civicrm_custom_field.is_active = 1 \n AND civicrm_custom_group.extends IN ({$in})\n AND ( civicrm_custom_group.extends_entity_column_value LIKE '%{$subType}%'\n OR civicrm_custom_group.extends_entity_column_value IS NULL )\n"; if ($subName) { $strWhere .= " AND civicrm_custom_group.extends_entity_column_id = {$subName} "; } } else { $strWhere = "\nWHERE civicrm_custom_group.is_active = 1 \n AND civicrm_custom_field.is_active = 1 \n AND civicrm_custom_group.extends IN ({$in})\n AND civicrm_custom_group.extends_entity_column_value IS NULL\n"; } $params = array(); if ($groupID > 0) { // since we want a specific group id we add it to the where clause $strWhere .= " AND civicrm_custom_group.id = %1"; $params[1] = array($groupID, 'Integer'); } else { if (!$groupID) { // since groupID is false we need to show all Inline groups $strWhere .= " AND civicrm_custom_group.style = 'Inline'"; } } require_once 'CRM/Core/Permission.php'; // ensure that the user has access to these custom groups $strWhere .= " AND " . CRM_Core_Permission::customGroupClause(CRM_Core_Permission::VIEW, 'civicrm_custom_group.'); $orderBy = "\nORDER BY civicrm_custom_group.weight,\n civicrm_custom_group.title,\n civicrm_custom_field.weight,\n civicrm_custom_field.label\n"; // final query string $queryString = "{$strSelect} {$strFrom} {$strWhere} {$orderBy}"; //crm_core_error::debug('$queryString', $queryString ); // dummy dao needed $crmDAO =& CRM_Core_DAO::executeQuery($queryString, $params); $customValueTables = array(); // process records while ($crmDAO->fetch()) { // get the id's $groupID = $crmDAO->civicrm_custom_group_id; $fieldId = $crmDAO->civicrm_custom_field_id; // create an array for groups if it does not exist if (!array_key_exists($groupID, $groupTree)) { $groupTree[$groupID] = array(); $groupTree[$groupID]['id'] = $groupID; // populate the group information foreach ($tableData['civicrm_custom_group'] as $fieldName) { $fullFieldName = "civicrm_custom_group_{$fieldName}"; if ($fieldName == 'id' || is_null($crmDAO->{$fullFieldName})) { continue; } if ($fieldName == 'extends_entity_column_value' && $subType) { // CRM-5507 $groupTree[$groupID]['subtype'] = trim($subType, CRM_Core_DAO::VALUE_SEPARATOR); } $groupTree[$groupID][$fieldName] = $crmDAO->{$fullFieldName}; } $groupTree[$groupID]['fields'] = array(); $customValueTables[$crmDAO->civicrm_custom_group_table_name] = array(); } // add the fields now (note - the query row will always contain a field) // we only reset this once, since multiple values come is as multiple rows if (!array_key_exists($fieldId, $groupTree[$groupID]['fields'])) { $groupTree[$groupID]['fields'][$fieldId] = array(); } $customValueTables[$crmDAO->civicrm_custom_group_table_name][$crmDAO->civicrm_custom_field_column_name] = 1; $groupTree[$groupID]['fields'][$fieldId]['id'] = $fieldId; // populate information for a custom field foreach ($tableData['civicrm_custom_field'] as $fieldName) { $fullFieldName = "civicrm_custom_field_{$fieldName}"; if ($fieldName == 'id' || is_null($crmDAO->{$fullFieldName})) { continue; } $groupTree[$groupID]['fields'][$fieldId][$fieldName] = $crmDAO->{$fullFieldName}; } } require_once 'CRM/Core/Action.php'; // now that we have all the groups and fields, lets get the values // since we need to know the table and field names // add info to groupTree if (!empty($customValueTables)) { $groupTree['info'] = array('tables' => $customValueTables); $select = $from = $where = array(); foreach ($groupTree['info']['tables'] as $table => $fields) { $from[] = $table; $select[] = "{$table}.id as {$table}_id"; $select[] = "{$table}.entity_id as {$table}_entity_id"; foreach ($fields as $column => $dontCare) { $select[] = "{$table}.{$column} as {$table}_{$column}"; } if ($entityID) { $where[] = "{$table}.entity_id = {$entityID}"; } } $groupTree['info']['select'] = $select; $groupTree['info']['from'] = $from; $groupTree['info']['where'] = null; if ($entityID) { $groupTree['info']['where'] = $where; $select = implode(', ', $select); // this is a hack to find a table that has some values for this // entityID to make the below LEFT JOIN work (CRM-2518) $firstTable = null; foreach ($from as $table) { $query = "\nSELECT id\nFROM {$table}\nWHERE entity_id = {$entityID}\n"; $recordExists = CRM_Core_DAO::singleValueQuery($query); if ($recordExists) { $firstTable = $table; break; } } if ($firstTable) { $fromSQL = $firstTable; foreach ($from as $table) { if ($table != $firstTable) { $fromSQL .= "\nLEFT JOIN {$table} USING (entity_id)"; } } $query = "\nSELECT {$select}\n FROM {$fromSQL}\n WHERE {$firstTable}.entity_id = {$entityID}\n"; $dao = CRM_Core_DAO::executeQuery($query); while ($dao->fetch()) { foreach ($groupTree as $groupID => $group) { if ($groupID === 'info') { continue; } $table = $groupTree[$groupID]['table_name']; foreach ($group['fields'] as $fieldID => $dontCare) { $column = $groupTree[$groupID]['fields'][$fieldID]['column_name']; $idName = "{$table}_id"; $fieldName = "{$table}_{$column}"; $dataType = $groupTree[$groupID]['fields'][$fieldID]['data_type']; if ($dataType == 'File') { if (isset($dao->{$fieldName})) { require_once 'CRM/Core/DAO/File.php'; $config = CRM_Core_Config::singleton(); $fileDAO = new CRM_Core_DAO_File(); $fileDAO->id = $dao->{$fieldName}; if ($fileDAO->find(true)) { $entityIDName = "{$table}_entity_id"; $customValue['id'] = $dao->{$idName}; $customValue['data'] = $fileDAO->uri; $customValue['fid'] = $fileDAO->id; $customValue['fileURL'] = CRM_Utils_System::url('civicrm/file', "reset=1&id={$fileDAO->id}&eid={$dao->{$entityIDName}}"); $customValue['displayURL'] = null; $deleteExtra = ts('Are you sure you want to delete attached file.'); $deleteURL = array(CRM_Core_Action::DELETE => array('name' => ts('Delete Attached File'), 'url' => 'civicrm/file', 'qs' => 'reset=1&id=%%id%%&eid=%%eid%%&fid=%%fid%%&action=delete', 'extra' => 'onclick = "if (confirm( \'' . $deleteExtra . '\' ) ) this.href+=\'&confirmed=1\'; else return false;"')); $customValue['deleteURL'] = CRM_Core_Action::formLink($deleteURL, CRM_Core_Action::DELETE, array('id' => $fileDAO->id, 'eid' => $dao->{$entityIDName}, 'fid' => $fieldID)); $customValue['fileName'] = CRM_Utils_File::cleanFileName(basename($fileDAO->uri)); if ($fileDAO->mime_type == "image/jpeg" || $fileDAO->mime_type == "image/pjpeg" || $fileDAO->mime_type == "image/gif" || $fileDAO->mime_type == "image/x-png" || $fileDAO->mime_type == "image/png") { $customValue['displayURL'] = $customValue['fileURL']; $entityId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_EntityFile', $fileDAO->id, 'entity_id', 'id'); require_once 'CRM/Core/BAO/File.php'; list($path) = CRM_Core_BAO_File::path($fileDAO->id, $entityId, null, null); list($imageWidth, $imageHeight) = getimagesize($path); require_once 'CRM/Contact/BAO/Contact.php'; list($imageThumbWidth, $imageThumbHeight) = CRM_Contact_BAO_Contact::getThumbSize($imageWidth, $imageHeight); $customValue['imageThumbWidth'] = $imageThumbWidth; $customValue['imageThumbHeight'] = $imageThumbHeight; } } } else { $customValue = array('id' => $dao->{$idName}, 'data' => ''); } } else { $customValue = array('id' => $dao->{$idName}, 'data' => $dao->{$fieldName}); } if (!array_key_exists('customValue', $groupTree[$groupID]['fields'][$fieldID])) { $groupTree[$groupID]['fields'][$fieldID]['customValue'] = array(); } if (empty($groupTree[$groupID]['fields'][$fieldID]['customValue'])) { $groupTree[$groupID]['fields'][$fieldID]['customValue'] = array(1 => $customValue); } else { $groupTree[$groupID]['fields'][$fieldID]['customValue'][] = $customValue; } } } } } } } return $groupTree; }
/** * 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; }
/** * 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; }
/** * 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.'); }
/** * Get a file. * * This api is used for finding an existing file. * Required parameters : id OR file_type_id of a file * * @param array $params an associative array of name/value property values of civicrm_file * * @return Array of all found file object property values. * @access public */ function crm_get_file($params) { if (!is_array($params)) { return _crm_error('params is not an array.'); } if (!isset($params['id']) && !isset($params['file_type_id'])) { return _crm_error('Required parameters missing.'); } 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]; } } if ($fileDAO->find()) { $file = array(); while ($fileDAO->fetch()) { _crm_object_to_array(clone $fileDAO, $file); $files[$fileDAO->id] = $file; } } else { return _crm_error('Exact match not found'); } return $files; }
/** * Attachment getfields helper. * * @return array * list of fields (indexed by name) */ function _civicrm_api3_attachment_getfields() { $fileFields = CRM_Core_DAO_File::fields(); $entityFileFields = CRM_Core_DAO_EntityFile::fields(); $spec = array(); $spec['id'] = $fileFields['id']; $spec['name'] = array('title' => 'Name (write-once)', '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']; $spec['entity_table'] = $entityFileFields['entity_table']; // Would be hard to securely handle changes. $spec['entity_table']['title'] = CRM_Utils_Array::value('title', $spec['entity_table'], 'Entity Table') . ' (write-once)'; $spec['entity_id'] = $entityFileFields['entity_id']; $spec['entity_id']['title'] = CRM_Utils_Array::value('title', $spec['entity_id'], 'Entity ID') . ' (write-once)'; // would be hard to securely handle changes $spec['url'] = array('title' => 'URL (read-only)', 'description' => 'URL for downloading the file (not searchable, expire-able)', 'type' => CRM_Utils_Type::T_STRING); $spec['path'] = array('title' => 'Path (read-only)', 'description' => 'Local file path (not searchable, local-only)', 'type' => CRM_Utils_Type::T_STRING); $spec['content'] = array('title' => 'Content', 'description' => 'File content (not searchable, not returned by default)', 'type' => CRM_Utils_Type::T_STRING); return $spec; }
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; }
public function delete($fileID, $entityID, $fieldID) { // get the table and column name require_once 'CRM/Core/BAO/CustomField.php'; list($tableName, $columnName, $groupID) = CRM_Core_BAO_CustomField::getTableColumnGroup($fieldID); require_once "CRM/Core/DAO/EntityFile.php"; $entityFileDAO = new CRM_Core_DAO_EntityFile(); $entityFileDAO->file_id = $fileID; $entityFileDAO->entity_id = $entityID; $entityFileDAO->entity_table = $tableName; if ($entityFileDAO->find(true)) { $entityFileDAO->delete(); } else { CRM_Core_Error::fatal(); } require_once "CRM/Core/DAO/File.php"; $fileDAO = new CRM_Core_DAO_File(); $fileDAO->id = $fileID; if ($fileDAO->find(true)) { $fileDAO->delete(); } else { CRM_Core_Error::fatal(); } // also set the value to null of the table and column $query = "UPDATE {$tableName} SET {$columnName} = null WHERE {$columnName} = %1"; $params = array(1 => array($fileID, 'Integer')); CRM_Core_DAO::executeQuery($query, $params); }
/** * 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; }
/** * 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; }