Skip to content

Instantly share code, notes, and snippets.

@xurizaemon
Created February 13, 2015 19:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xurizaemon/4a4e80273f89f0c570d2 to your computer and use it in GitHub Desktop.
Save xurizaemon/4a4e80273f89f0c570d2 to your computer and use it in GitHub Desktop.
Export of CiviCRM view to render contact and custom images via ImageCache External
// For http://forum.civicrm.org/index.php/topic,34929.0.html
//
// Ingredients:
// * CiviCRM + some contact images or custom fields
// * Install Views PHP. Actually don't, use templating to achieve the same effect without Views PHP. This is just for demo.
// * Install Imagecache External.
// * Have the default "thumbnail" image style.
// * Whitelist your own site at admin/config/media/imagecache_external
// * Configure the custom fields in the view - this view contains names from a sample DB which your site won't match. This export contains magic strings which you need to adjust.
// - image_358
// - civicrm_value_contact_details_17
// - $data->civicrm_value_contact_details_17_image_358
$view = new view();
$view->name = 'contact_images';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'civicrm_contact';
$view->human_name = 'Contact Images';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['title'] = 'Contact Images';
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['access']['type'] = 'none';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['pager']['options']['items_per_page'] = '10';
$handler->display->display_options['style_plugin'] = 'table';
$handler->display->display_options['style_options']['columns'] = array(
'id' => 'id',
'display_name' => 'display_name',
'first_name' => 'first_name',
);
$handler->display->display_options['style_options']['default'] = '-1';
$handler->display->display_options['style_options']['info'] = array(
'id' => array(
'sortable' => 0,
'default_sort_order' => 'asc',
'align' => '',
'separator' => '',
'empty_column' => 0,
),
'display_name' => array(
'sortable' => 0,
'default_sort_order' => 'asc',
'align' => '',
'separator' => '',
'empty_column' => 0,
),
'first_name' => array(
'sortable' => 0,
'default_sort_order' => 'asc',
'align' => '',
'separator' => '',
'empty_column' => 0,
),
);
/* Field: CiviCRM Contacts: Display Name */
$handler->display->display_options['fields']['display_name']['id'] = 'display_name';
$handler->display->display_options['fields']['display_name']['table'] = 'civicrm_contact';
$handler->display->display_options['fields']['display_name']['field'] = 'display_name';
$handler->display->display_options['fields']['display_name']['link_to_civicrm_contact'] = 1;
/* Field: CiviCRM Custom: Personal Information: Image */
$handler->display->display_options['fields']['image_358']['id'] = 'image_358';
$handler->display->display_options['fields']['image_358']['table'] = 'civicrm_value_contact_details_17';
$handler->display->display_options['fields']['image_358']['field'] = 'image_358';
$handler->display->display_options['fields']['image_358']['label'] = 'Custom Image';
/* Field: CiviCRM Contacts: Image Url */
$handler->display->display_options['fields']['image_URL']['id'] = 'image_URL';
$handler->display->display_options['fields']['image_URL']['table'] = 'civicrm_contact';
$handler->display->display_options['fields']['image_URL']['field'] = 'image_URL';
/* Field: Global: Custom text */
$handler->display->display_options['fields']['nothing']['id'] = 'nothing';
$handler->display->display_options['fields']['nothing']['table'] = 'views';
$handler->display->display_options['fields']['nothing']['field'] = 'nothing';
$handler->display->display_options['fields']['nothing']['label'] = 'Custom Image URL';
$handler->display->display_options['fields']['nothing']['alter']['text'] = '[image_358]';
/* Field: Global: PHP */
$handler->display->display_options['fields']['php']['id'] = 'php';
$handler->display->display_options['fields']['php']['table'] = 'views';
$handler->display->display_options['fields']['php']['field'] = 'php';
$handler->display->display_options['fields']['php']['label'] = 'Contact Image';
$handler->display->display_options['fields']['php']['use_php_setup'] = 0;
$handler->display->display_options['fields']['php']['php_output'] = '<?php
print theme(\'imagecache_external\', array(
\'path\' => $row->image_URL,
\'style_name\'=> \'thumbnail\',
\'alt\' => \'contact image\',
));
?>';
$handler->display->display_options['fields']['php']['use_php_click_sortable'] = '0';
$handler->display->display_options['fields']['php']['php_click_sortable'] = '';
/* Field: Global: PHP */
$handler->display->display_options['fields']['php_1']['id'] = 'php_1';
$handler->display->display_options['fields']['php_1']['table'] = 'views';
$handler->display->display_options['fields']['php_1']['field'] = 'php';
$handler->display->display_options['fields']['php_1']['label'] = 'Custom Image';
$handler->display->display_options['fields']['php_1']['use_php_setup'] = 1;
$handler->display->display_options['fields']['php_1']['php_setup'] = '// Ripped from civicrm_handler_field_custom_file.inc
function civicrm_handler_field_custom_file_render($file_id) {
$fileType = CRM_Core_DAO::getFieldValue(\'CRM_Core_DAO_File\',
$file_id,
\'mime_type\'
);
if ($fileType && in_array($fileType, array(
"image/jpeg", "image/pjpeg", "image/gif", "image/x-png", "image/png"))) {
$uri = CRM_Core_DAO::getFieldValue(\'CRM_Core_DAO_File\',
$file_id,
\'uri\'
);
$config = CRM_Core_Config::singleton();
return str_replace(\'persist/contribute\', \'custom\', $config->imageUploadURL) . $uri;
}
else {
$entityField = $this->aliases[\'entity_id\'];
return CRM_Utils_System::url(\'civicrm/file\',
"reset=1&id={$file_id}&eid={$values->$entityField}",
TRUE, NULL, TRUE, TRUE
);
}
}';
$handler->display->display_options['fields']['php_1']['php_value'] = '$file_id = $data->civicrm_value_contact_details_17_image_358;
return civicrm_handler_field_custom_file_render($file_id);';
$handler->display->display_options['fields']['php_1']['php_output'] = '<?php
print theme(\'imagecache_external\', array(
\'path\' => $value,
\'style_name\'=> \'thumbnail\',
\'alt\' => \'contact image\',
));
?>';
$handler->display->display_options['fields']['php_1']['use_php_click_sortable'] = '0';
$handler->display->display_options['fields']['php_1']['php_click_sortable'] = '';
/* Filter criterion: CiviCRM Contacts: Image Url */
$handler->display->display_options['filters']['image_URL']['id'] = 'image_URL';
$handler->display->display_options['filters']['image_URL']['table'] = 'civicrm_contact';
$handler->display->display_options['filters']['image_URL']['field'] = 'image_URL';
$handler->display->display_options['filters']['image_URL']['operator'] = 'contains';
$handler->display->display_options['filters']['image_URL']['value'] = '/';
/* Filter criterion: CiviCRM Custom: Personal Information: Image */
$handler->display->display_options['filters']['image_358']['id'] = 'image_358';
$handler->display->display_options['filters']['image_358']['table'] = 'civicrm_value_contact_details_17';
$handler->display->display_options['filters']['image_358']['field'] = 'image_358';
$handler->display->display_options['filters']['image_358']['operator'] = 'not empty';
$handler->display->display_options['filters']['image_358']['value'] = '/';
/* Display: Page */
$handler = $view->new_display('page', 'Page', 'page');
$handler->display->display_options['path'] = 'contact-images';
$translatables['contact_images'] = array(
t('Master'),
t('Contact Images'),
t('more'),
t('Apply'),
t('Reset'),
t('Sort by'),
t('Asc'),
t('Desc'),
t('Items per page'),
t('- All -'),
t('Offset'),
t('« first'),
t('‹ previous'),
t('next ›'),
t('last »'),
t('Display Name'),
t('Custom Image'),
t('Image Url'),
t('Custom Image URL'),
t('[image_358]'),
t('Contact Image'),
t('Page'),
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment