View source
<?php
define('FE_PATHS_OVERRIDE_NEVER', 0);
define('FE_PATHS_OVERRIDE_WITHIN_ENTITY_TYPE', 1);
define('FE_PATHS_OVERRIDE_WITHIN_ENTITY', 2);
define('FE_PATHS_OVERRIDE_ALWAYS', 3);
function fe_paths_permission() {
return array(
'administer_fe_paths' => array(
'title' => t('Administer File Entity Paths'),
'description' => t('Adminsiter File Entity Paths configuration.'),
),
);
}
function fe_paths_menu() {
$items['admin/config/media/fe-paths'] = array(
'title' => 'File Entity Paths settings',
'description' => 'Configure custom paths for file entities',
'type' => MENU_NORMAL_ITEM,
'access arguments' => array(
'administer_fe_paths',
),
'page callback' => 'fe_paths_global_page',
'file' => 'fe_paths.admin.inc',
);
$items['admin/config/media/fe-paths/global'] = array(
'title' => 'Global',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -1,
);
$items['admin/config/media/fe-paths/add'] = array(
'title' => 'Add config',
'type' => MENU_LOCAL_TASK,
'access arguments' => array(
'administer_fe_paths',
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'fe_paths_entity_edit_form',
),
'file' => 'fe_paths.admin.inc',
);
$items['admin/config/media/fe-paths/%fe_paths_config/edit'] = array(
'title' => 'Edit configuration',
'type' => MENU_NORMAL_ITEM,
'access arguments' => array(
'administer_fe_paths',
),
'page callback' => 'fe_paths_config_edit_page',
'page arguments' => array(
4,
),
'file' => 'fe_paths.admin.inc',
);
$items['admin/config/media/fe-paths/%fe_paths_config/delete'] = array(
'title' => 'Edit configuration',
'type' => MENU_NORMAL_ITEM,
'access arguments' => array(
'administer_fe_paths',
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'fe_paths_config_delete_confirm',
4,
),
'file' => 'fe_paths.admin.inc',
);
return $items;
}
function fe_paths_form_alter(&$form, &$form_state, $form_id) {
}
function fe_paths_theme() {
return array(
'fe_paths_config_table' => array(
'render element' => 'element',
'file' => 'fe_paths.admin.inc',
),
'fe_paths_config_data' => array(
'variables' => array(
'config' => NULL,
),
),
);
}
function fe_paths_entity_info() {
$return = array(
'fe_paths_config' => array(
'label' => t('File entity paths configuration'),
'entity class' => 'Entity',
'controller class' => 'EntityAPIControllerExportable',
'base table' => 'fe_paths_config',
'fieldable' => FALSE,
'entity keys' => array(
'id' => 'id',
'name' => 'machine_name',
'label' => 'label',
),
'module' => 'fe_paths',
'exportable' => TRUE,
'label callback' => 'entity_class_label',
'uri callback' => 'entity_class_uri',
),
);
if (module_exists('entitycache')) {
$return['fe_paths_config']['entity cache'] = TRUE;
}
return $return;
}
function fe_paths_entity_presave($entity, $type) {
if ($type == 'file') {
if (empty($entity->origname)) {
$entity->origname = $entity->filename;
}
if (!isset($entity->fe_paths_processed) && isset($entity->fid)) {
drupal_register_shutdown_function('fe_paths_file_process', $entity->fid, $entity, $type);
$entity->fe_paths_processed = TRUE;
}
}
if (!isset($entity->fe_paths_processed) && array_key_exists($type, fe_paths_get_fieldable_entities())) {
$entity_info = entity_get_info($type);
$bundle = NULL;
if (!empty($entity_info['entity keys']['bundle'])) {
$bundle = $entity->{$entity_info['entity keys']['bundle']};
}
else {
$bundle = $type;
}
$fields = field_info_instances($type, $bundle);
$allowed_types = fe_paths_get_allowed_widget_types($entity);
foreach ($fields as $field) {
if (in_array($field['widget']['type'], $allowed_types) && ($files = field_get_items($type, $entity, $field['field_name']))) {
foreach ($files as $file) {
drupal_register_shutdown_function('fe_paths_file_process', $file['fid'], $entity, $type, $field['field_name']);
}
}
if (!empty($field['settings']['text_processing']) && ($field_items = field_get_items($type, $entity, $field['field_name']))) {
foreach ($field_items as $field_item) {
preg_match_all('/\\[\\[.*?\\]\\]/s', $field_item['value'], $matches);
foreach ($matches[0] as $tag) {
$tag = str_replace(array(
'[[',
']]',
), '', $tag);
$tag_info = drupal_json_decode($tag);
if (isset($tag_info['fid'])) {
drupal_register_shutdown_function('fe_paths_file_process', $tag_info['fid'], $entity, $type, $field['field_name']);
}
}
}
}
}
$entity->fe_paths_processed = TRUE;
}
}
function fe_paths_file_delete($file) {
fe_paths_usage_delete($file->fid);
}
function fe_paths_file_process($fid, $entity, $type, $field_name = NULL) {
$file = file_load($fid);
if ($file !== FALSE && fe_paths_file_process_available($file)) {
$configs = fe_paths_config_load_multiple();
fe_paths_add_global_settings_to_config($configs, $file);
$entity_info = entity_get_info($type);
$match = FALSE;
$fe_paths_usage = fe_paths_usage($file->fid);
$file_config = FALSE;
$entity_key = $entity_info['entity keys']['id'];
$entity_id = $entity->{$entity_key};
$bundle_key = isset($entity_info['entity keys']['bundle']) ? $entity_info['entity keys']['bundle'] : 'type';
$bundle = isset($entity->{$bundle_key}) ? $entity->{$bundle_key} : $type;
if ($fe_paths_usage) {
$file_config = $configs[$fe_paths_usage->id];
if (!$file_config->data['other_config']) {
if ($file_config->data['override_options'] === FE_PATHS_OVERRIDE_NEVER) {
return;
}
if ($file_config->data['override_options'] === FE_PATHS_OVERRIDE_WITHIN_ENTITY_TYPE && $fe_paths_usage->entity_type != $type) {
return;
}
if ($file_config->data['override_options'] === FE_PATHS_OVERRIDE_WITHIN_ENTITY && $fe_paths_usage->entity_id != $entity_id) {
return;
}
}
}
foreach ($configs as $config) {
if ($match) {
break;
}
$this_file_config = $config == $file_config;
$data = $config->data;
if ($file_config && !$file_config->data['other_config'] && !$this_file_config) {
continue;
}
if ($this_file_config) {
$match = TRUE;
if ($file_config->data['override_options'] === FE_PATHS_OVERRIDE_NEVER) {
return;
}
if ($file_config->data['override_options'] === FE_PATHS_OVERRIDE_WITHIN_ENTITY_TYPE && $fe_paths_usage->entity_type != $type) {
return;
}
if ($file_config->data['override_options'] === FE_PATHS_OVERRIDE_WITHIN_ENTITY && $fe_paths_usage->entity_id != $entity_id) {
return;
}
}
if (isset($data['file_entity'][$file->type]) && $data['file_entity'][$file->type] !== $file->type) {
continue;
}
if (isset($data['entity']) && $data['entity'] != $type && $data['entity'] != 'global') {
continue;
}
if (isset($data['bundle']) && !isset($data['bundle'][$bundle])) {
continue;
}
if (!is_null($field_name)) {
if (!isset($data['bundle'][$bundle][$field_name])) {
continue;
}
if (!$data['bundle'][$bundle][$field_name]) {
continue;
}
}
if (fe_paths_move_file($file, $config->path, $config->filename, $type, $data, $entity)) {
$match = TRUE;
fe_paths_usage_add($file->fid, $type, $entity->{$entity_key}, $config->id);
}
else {
$match = FALSE;
}
}
}
}
function fe_paths_move_file($file, $replace_path, $replace_filename, $token_type, $config_data, $entity = NULL) {
$scheme = file_uri_scheme($file->uri);
$old_dir_uri = str_replace('/' . $file->filename, '', $file->uri);
$path = token_replace($replace_path, array(
$token_type => $entity,
$token_type,
'file' => $file,
'file',
), array(
'clear' => TRUE,
));
$filename = token_replace($replace_filename, array(
$token_type => $entity,
$token_type,
'file' => $file,
'file',
), array(
'clear' => TRUE,
));
if (isset($config_data['transliteration']) && $config_data['transliteration'] && module_exists('transliteration')) {
$parts = array();
foreach (explode('/', $path) as $part) {
$parts[] = transliteration_clean_filename($part);
}
$path = implode('/', $parts);
$filename = transliteration_clean_filename($filename);
}
if (isset($config_data['pathauto']) && $config_data['pathauto'] && module_exists('pathauto')) {
module_load_include('inc', 'pathauto');
$parts = array();
foreach (explode('/', $path) as $part) {
$parts[] = pathauto_cleanstring($part);
}
$path = implode('/', $parts);
$parts = array();
foreach (explode('.', $filename) as $part) {
$parts[] = pathauto_cleanstring($part);
}
$filename = implode('.', $parts);
}
$new_uri = $scheme . '://' . $path . '/' . $filename;
$directory = $scheme . '://' . $path;
if ($new_uri == $file->uri) {
return $file;
}
file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
if (file_move($file, $new_uri)) {
$files = file_scan_directory($old_dir_uri, '/.*/');
if (file_exists($old_dir_uri) && empty($files)) {
drupal_rmdir($old_dir_uri);
}
$file->filename = $filename;
$file->uri = $new_uri;
return $file;
}
else {
return FALSE;
}
}
function fe_paths_file_process_available($file) {
$return = TRUE;
$processed =& drupal_static(__FUNCTION__, array());
if (!file_entity_file_is_local($file)) {
return FALSE;
}
if (!isset($file->type)) {
watchdog('File Entity devel helper', '<em>!isset($file->type)</em> condition was marked as removable in File Entity Paths module. If you still see this message in log, please let me know in <a href="http://drupal.org/node/1783934">Check whether all condition need in the fe_paths_file_process_available()</a> issue.');
return FALSE;
}
if (!array_key_exists($file->origname, $processed)) {
$processed[$file->origname] = TRUE;
}
else {
$return = FALSE;
}
$clone = clone $file;
drupal_alter('fe_paths_file_process_available', $return, $clone);
return $return;
}
function fe_paths_config_load_multiple($ids = array(
'all',
), $conditions = array(), $reset = FALSE) {
if (in_array('all', $ids)) {
$ids = array();
$result = db_select('fe_paths_config', 'fe')
->fields('fe', array(
'machine_name',
'id',
))
->execute()
->fetchAllAssoc('id');
foreach ($result as $key => $value) {
$ids[] = $key;
}
}
$entities = entity_load('fe_paths_config', $ids, $conditions, $reset);
$weights = array();
foreach ($entities as $id => $config) {
$weights[$id] = $config->weight;
}
asort($weights);
$return = array();
foreach ($weights as $key => $weight) {
$return[$key] = $entities[$key];
}
return $return;
}
function fe_paths_config_load($id, $reset = FALSE) {
$object = fe_paths_config_load_multiple(array(
$id,
), array(), $reset);
return isset($object[$id]) ? $object[$id] : FALSE;
}
function fe_paths_config_delete_multiple(array $ids) {
return entity_delete_multiple('fe_paths_config', $ids);
}
function fe_paths_config_delete($id) {
return fe_paths_config_delete_multiple(array(
$id,
));
}
function fe_paths_config_save($config) {
if (is_array($config)) {
$config = (object) $config;
}
return entity_save('fe_paths_config', $config);
}
function fe_paths_prepare_config(&$config) {
$info = entity_get_info('fe_paths_config');
foreach ($config as $key => $value) {
if (!in_array($key, $info['schema_fields_sql']['base table'])) {
unset($config[$key]);
}
}
$config = (object) $config;
if (!isset($config->status)) {
$config->status = 1;
}
}
function fe_paths_usage_add($fid = NULL, $entity_type, $entity_id, $id) {
$data = array(
'fid' => $fid,
'entity_type' => $entity_type,
'entity_id' => $entity_id,
'id' => $id,
);
if (!fe_paths_usage($fid)) {
drupal_write_record('fe_paths_usage', $data);
}
else {
db_update('fe_paths_usage')
->fields(array(
'entity_type' => $entity_type,
'entity_id' => $entity_id,
'id' => $id,
))
->condition('fid', $fid)
->execute();
}
}
function fe_paths_usage_delete($fid) {
db_delete('fe_paths_usage')
->condition('fid', $fid)
->execute();
}
function fe_paths_usage($fid, $reset = FALSE) {
$usage = drupal_static(__FUNCTION__);
if (isset($usage[$fid]) && !$reset) {
return $usage[$fid];
}
$result = db_select('fe_paths_usage', 'fep')
->fields('fep')
->condition('fid', $fid)
->execute()
->fetchAllAssoc('fid');
if (!$result) {
return FALSE;
}
else {
$usage[$fid] = $result[$fid];
}
return $usage[$fid];
}
function theme_fe_paths_config_data($vars) {
$data = $vars['config']->data;
$items = array();
if (isset($data['file_entity'])) {
$enabled_types = array();
foreach ($data['file_entity'] as $key => $val) {
if ($key === $val) {
$enabled_types[] = $val;
}
}
$items[] = t('File types processed: @types', array(
'@types' => !empty($enabled_types) ? implode(', ', $enabled_types) : ' - ',
));
}
if (isset($data['entity'])) {
$entities = fe_paths_get_fieldable_entities();
$bundles = fe_paths_get_bundle_names($data['entity']);
$items[] = t('Entity: @entity', array(
'@entity' => $entities[$data['entity']],
));
$fields_text = array();
if (isset($data['bundle'])) {
foreach ($data['bundle'] as $bundle_key => $bundle) {
foreach ($bundle as $field_name => $field) {
if ($bundle[$field_name]) {
$fields_text[] = $bundles[$bundle_key] . ': ' . $field_name;
}
}
}
if (!empty($fields_text)) {
$items[] = t('In fields: !bundle', array(
'!bundle' => theme('item_list', array(
'items' => $fields_text,
)),
));
}
}
}
return theme('item_list', array(
'items' => $items,
));
}
function fe_paths_get_allowed_schemes() {
return array(
'public',
'private',
);
}
function fe_paths_get_allowed_widget_types($entity = NULL) {
$allowed = array(
'image_image',
'file_file',
'media_generic',
);
drupal_alter($allowed, $entity);
return $allowed;
}
function fe_paths_get_settings($file_type = NULL) {
$settings =& drupal_static(__FUNCTION__);
if (!isset($settings[$file_type])) {
$entity_info = entity_get_info('file');
foreach ($entity_info['bundles'] as $type => $bundle_info) {
$defaults = array();
if (isset($bundle_info['admin'])) {
foreach (file_get_stream_wrappers() as $scheme => $wrapper) {
$defaults[$scheme] = array(
'path' => '',
'filename' => '[file:name-only-original].[file:extension-original]',
);
}
$settings[$type] = variable_get("fep_{$type}", $defaults);
}
}
}
return is_null($file_type) ? $settings : $settings[$file_type];
}
function fe_paths_add_global_settings_to_config(&$config, $file) {
$settings = fe_paths_get_settings($file->type);
$scheme = file_uri_scheme($file->uri);
$object = new stdClass();
$object->id = 0;
$object->path = $settings[$scheme]['path'];
$object->filename = $settings[$scheme]['filename'];
$object->data = array(
'other_config' => 1,
);
$config[0] = $object;
}
function fe_paths_check_machine_name($machine_name) {
return db_select('fe_paths_config', 'fe')
->fields('fe', array(
'machine_name',
))
->condition('machine_name', $machine_name)
->execute()
->rowCount();
}
function fe_paths_get_fieldable_entities() {
$return = array();
foreach (entity_get_info() as $name => $entity) {
if ($entity['fieldable']) {
$return[$name] = $entity['label'];
}
}
return $return;
}
function fe_paths_get_bundle_names($entity_type) {
$entity = entity_get_info($entity_type);
$bundles = array();
foreach ($entity['bundles'] as $key => $bundle) {
$bundles[$key] = $bundle['label'];
}
return $bundles;
}
function fe_paths_get_override_options() {
return array(
FE_PATHS_OVERRIDE_NEVER => t('Never'),
FE_PATHS_OVERRIDE_WITHIN_ENTITY_TYPE => t('Within the same entity type'),
FE_PATHS_OVERRIDE_WITHIN_ENTITY => t('Within the same entity'),
FE_PATHS_OVERRIDE_ALWAYS => t('Always'),
);
}
function fe_paths_have_valid_bundle($entity_type) {
$bundles = fe_paths_get_bundle_names($entity_type);
return !(count($bundles) == 1 && array_key_exists($entity_type, $bundles));
}
function fe_paths_get_available_fields($entity_type = NULL, $bundle = NULL) {
$instances = field_info_instances();
$allowed_types = fe_paths_get_allowed_widget_types();
$return = array();
foreach ($instances as $entity_name => $type_bundles) {
if (is_null($entity_type) || $entity_name == $entity_type) {
foreach ($type_bundles as $bundle_name => $bundle_instances) {
if (is_null($bundle) || $bundle_name == $bundle) {
foreach ($bundle_instances as $field_name => $instance) {
if (in_array($instance['widget']['type'], $allowed_types) || !empty($instance['settings']['text_processing'])) {
$return[$field_name] = $instance['label'] . ' (' . $field_name . ')';
}
}
}
}
}
}
return $return;
}