View source
<?php
define('ENTITYFORM_STATUS_CLOSED', 'ENTITYFORM_CLOSED');
define('ENTITYFORM_STATUS_OPEN', 'ENTITYFORM_OPEN');
function entityform_menu() {
$items = array();
$entity_info = entity_get_info('entityform_type');
$submissions_path = $entity_info['admin ui']['path'] . "/manage/%entityform_type/submissions";
$path_count = count(explode('/', $submissions_path));
$items['admin/config/content/entityform'] = array(
'title' => 'Entityform Settings',
'description' => '',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'entityform_settings',
),
'access arguments' => array(
'administer site configuration',
),
'file' => 'entityform_type.admin.inc',
);
$items[$submissions_path] = array(
'title callback' => 'entityform_type_page_title',
'title arguments' => array(
1,
'submissions report',
),
'page callback' => 'entityform_submission_page',
'page arguments' => array(
$path_count - 2,
$path_count,
'admins',
),
'access arguments' => array(
'view any entityform',
),
'type' => MENU_LOCAL_TASK,
'weight' => 100,
);
$items['admin/reports/entityforms/submissions/%entityform_type'] = array(
'title callback' => 'entityform_type_page_title',
'title arguments' => array(
1,
'submissions report',
),
'page callback' => 'entityform_submission_page',
'page arguments' => array(
4,
5,
'admins',
),
'access arguments' => array(
'view any entityform',
),
'type' => MENU_LOCAL_TASK,
);
return $items;
}
function entityform_entity_info() {
$return['entityform'] = array(
'label' => t('Entityform'),
'entity class' => 'Entityform',
'controller class' => 'EntityformController',
'base table' => 'entityform',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'entityform_id',
'bundle' => 'type',
),
'bundles' => array(),
'bundle keys' => array(
'bundle' => 'type',
),
'view modes' => array(
'full' => array(
'label' => t('Full content'),
'custom settings' => FALSE,
),
'email' => array(
'label' => t('Email'),
'custom settings' => FALSE,
),
'confirmation' => array(
'label' => t('Confirmation'),
'custom settings' => FALSE,
),
'download' => array(
'label' => t('Downloads'),
'custom settings' => FALSE,
),
'table' => array(
'label' => t('Submissions Table'),
'custom settings' => FALSE,
),
),
'label callback' => 'entity_class_label',
'uri callback' => 'entityform_uri',
'creation callback' => 'entityform_create',
'access callback' => 'entityform_access',
'module' => 'entityform',
'admin ui' => array(
'path' => 'admin/structure/entityforms/list',
'front path' => 'entityform',
'file' => 'entityform.admin.inc',
'controller class' => 'EntityformUIController',
'menu wildcard' => '%entityform',
),
'metadata controller class' => 'EntityformMetadataController',
'metatags' => FALSE,
);
$types = db_select('entityform_type', 'e')
->fields('e')
->execute()
->fetchAllAssoc('type');
foreach ($types as $type => $info) {
$return['entityform']['bundles'][$type] = array(
'label' => $info->label,
'admin' => array(
'path' => 'admin/structure/entityform_types/manage/%entityform_type',
'real path' => 'admin/structure/entityform_types/manage/' . $type,
'bundle argument' => 4,
'access arguments' => array(
'administer entityform types',
),
),
);
}
$return['entityform_type'] = array(
'label' => t('Entityform Type'),
'entity class' => 'EntityformType',
'controller class' => 'EntityformTypeController',
'base table' => 'entityform_type',
'fieldable' => FALSE,
'bundle of' => 'entityform',
'exportable' => TRUE,
'entity keys' => array(
'id' => 'id',
'name' => 'type',
'label' => 'label',
),
'view modes' => array(
'full' => array(
'label' => t('Full content'),
'custom settings' => FALSE,
),
),
'access callback' => 'entityform_type_access',
'module' => 'entityform',
'admin ui' => array(
'path' => 'admin/structure/entityform_types',
'file' => 'entityform_type.admin.inc',
'controller class' => 'EntityformTypeUIController',
),
'metadata controller class' => 'EntityformTypeMetadataController',
);
return $return;
}
function entityform_token_info_alter(&$data) {
$data['tokens']['entityform']['entityform-submittd-data'] = array(
'name' => t('Submitted data'),
'description' => t('A summary of submitted data.'),
);
}
function entityform_tokens($type, $tokens, array $data = array(), array $options = array()) {
$replacements = array();
if ($type == 'entityform' && !empty($data['entityform'])) {
$entityform = $data['entityform'];
foreach ($tokens as $name => $original) {
switch ($name) {
case 'entityform-submittd-data':
$instances = field_info_instances('entityform', $entityform->type);
$view_mode = 'email';
$autofields = array();
foreach ($instances as $instance) {
$field_display = isset($instance['display'][$view_mode]) ? $instance['display'][$view_mode] : $instance['display']['default'];
if ($field_display['type'] != 'hidden') {
$items = field_get_items('entityform', $entityform, $instance['field_name']);
if (!empty($items)) {
$autofields[$field_display['weight']] = array(
'field_name' => $instance['field_name'],
'options' => array(
'type' => $field_display['type'],
'settings' => $field_display['settings'],
),
);
$autofields[$field_display['weight']] = field_view_value('entityform', $entityform, $instance['field_name'], $items[0], $field_display);
$autofields[$field_display['weight']]['#title'] = $instance['label'];
}
}
}
ksort($autofields);
$replacements[$original] = theme('entityform_submittd_data', array(
'instances' => $instances,
'fields' => $autofields,
'entityform' => $entityform,
));
break;
}
}
}
return $replacements;
}
function entityform_permission() {
$permissions = array(
'administer entityform types' => array(
'title' => t('Administer entityform types'),
'description' => t('Create and delete fields for entityform types, and set their permissions.'),
),
'edit any entityform' => array(
'title' => t('Edit any entityform submission'),
),
'view any entityform' => array(
'title' => t('View any entityform submission'),
),
'delete any entityform' => array(
'title' => t('Delete any entityform submission'),
),
'edit own entityform' => array(
'title' => t('Edit own entityform submission'),
),
'view own entityform' => array(
'title' => t('View own entityform submission'),
),
'delete own entityform' => array(
'title' => t('Delete own entityform submission'),
),
);
return $permissions;
}
function entityform_access($op, $entityform = NULL, $account = NULL) {
global $user;
if (!isset($account)) {
$account = $user;
}
if ($account->uid == 1) {
return TRUE;
}
if (!empty($entityform)) {
if (is_object($entityform)) {
$type_name = $entityform->type;
}
else {
$type_name = $entityform;
}
$entityform_type = entityform_type_load($type_name);
}
switch ($op) {
case 'update':
$op = 'edit';
break;
case 'create':
$op = 'submit';
break;
}
global $user;
if ($op == 'submit' || $op == 'confirm') {
if (isset($entityform_type) && is_object($entityform_type) && is_array($entityform_type->data) && array_intersect($entityform_type->data['roles'], array_keys($user->roles))) {
$can_submit = TRUE;
}
else {
$can_submit = FALSE;
}
if ($op == 'submit') {
if (!isset($entityform_type->data['form_status']) || $entityform_type->data['form_status'] != ENTITYFORM_STATUS_CLOSED) {
return $can_submit;
}
return FALSE;
}
$entityform_id = $_GET['entityform_id'];
if (empty($user->uid)) {
if (!isset($_SESSION['entityform_submission'])) {
return FALSE;
}
$match = $_SESSION['entityform_submission'] == $entityform_id;
unset($_SESSION['entityform_submission']);
return $match;
}
else {
if (!($entityform = entityform_load($entityform_id))) {
return FALSE;
}
return $entityform->uid == $user->uid;
}
}
if (isset($entityform) && $type_name && is_object($entityform)) {
if (user_access("{$op} any entityform", $account)) {
return TRUE;
}
elseif (!empty($user->uid) && $entityform->uid == $user->uid && user_access("{$op} own entityform", $account)) {
return TRUE;
}
}
return FALSE;
}
function entityform_type_access($op, $type = NULL, $account = NULL) {
if (isset($type) && is_object($type) && get_class($type) == 'EntityformType' && $op == 'view') {
return entityform_access('submit', $type, $account);
}
return user_access('administer entityform types', $account);
}
function _entityform_get_entityform_views_options() {
$views = views_get_enabled_views();
$options = array();
foreach ($views as $view) {
if ($view->base_table == 'entityform') {
foreach ($view->display as $display) {
if ($display->display_plugin == 'page') {
$options[$view->name] = $view->human_name;
}
}
}
}
return $options;
}
function entityform_get_types($type_name = NULL) {
static $drupal_static_fast;
$cache_key = 'entityform_types';
if (!isset($drupal_static_fast)) {
$drupal_static_fast[$cache_key] =& drupal_static(__FUNCTION__);
}
$entityform_types =& $drupal_static_fast[$cache_key];
if (empty($entityform_types)) {
if ($cache = cache_get($cache_key)) {
$entityform_types = $cache->data;
}
}
if (empty($entityform_types) || isset($type_name) && empty($entityform_types[$type_name])) {
if (!isset($type_name)) {
$entityform_types = entity_load_multiple_by_name('entityform_type', FALSE);
}
else {
$types = entity_load_multiple_by_name('entityform_type', array(
$type_name,
));
if (empty($types)) {
return FALSE;
}
$entityform_types[$type_name] = array_shift($types);
}
if (!isset($type_name)) {
cache_set($cache_key, $entityform_types);
}
}
return isset($type_name) ? $entityform_types[$type_name] : $entityform_types;
}
function entityform_get_submissions($type = NULL, $uid = NULL, $draft = 0) {
$submissions = array();
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', "entityform");
if ($type) {
$query
->propertyCondition('type', $type);
}
if (isset($uid)) {
$query
->propertyCondition('uid', $uid);
}
$query
->propertyCondition('draft', $draft);
$result = $query
->execute();
if (isset($result['entityform'])) {
$submissions = $result['entityform'];
}
return $submissions;
}
function entityform_user_draft($type, $uid = NULL) {
return entityform_user_previous_submission($type, $uid, 1);
}
function entityform_user_previous_submission($type, $uid = NULL, $draft = 0) {
if (!$uid) {
global $user;
$uid = $user->uid;
}
if ($uid) {
$submissions = entityform_get_submissions($type, $uid, $draft);
$submission = array_shift($submissions);
if (!$submission) {
return NULL;
}
else {
return entityform_load($submission->entityform_id);
}
}
return NULL;
}
function entityform_user_submitted($type, $uid = NULL) {
if (!$uid) {
global $user;
$uid = $user->uid;
}
$submissions = entityform_get_submissions($type, $uid);
return !empty($submissions);
}
function entityform_type_load($type) {
$type = str_replace('-', '_', $type);
$types = entityform_get_types();
return isset($types[$type]) ? $types[$type] : FALSE;
}
function entityform_load($entityform_id, $reset = FALSE) {
$entityforms = entityform_load_multiple(array(
$entityform_id,
), array(), $reset);
return reset($entityforms);
}
function entityform_load_multiple($entityform_ids = array(), $conditions = array(), $reset = FALSE) {
return entity_load('entityform', $entityform_ids, $conditions, $reset);
}
function entityform_delete(Entityform $entityform) {
$entityform
->delete();
}
function entityform_delete_multiple(array $entityform_ids) {
entity_get_controller('entityform')
->delete($entityform_ids);
}
function entityform_create($values = array()) {
return entity_get_controller('entityform')
->create($values);
}
function entityform_save(Entityform $entityform) {
return $entityform
->save();
}
function entityform_empty_load($type) {
$submit_404 =& drupal_static(__FUNCTION__);
$type = str_replace("-", "_", $type);
$empty_entityform = entityform_create(array(
'type' => $type,
));
if (empty($empty_entityform)) {
if (strpos(current_path(), 'eform/submit/') === 0) {
if ($submit_404) {
return NULL;
}
$submit_404 = TRUE;
drupal_not_found();
}
}
return $empty_entityform;
}
function entityform_type_save(EntityformType $type) {
$type
->save();
}
function entityform_type_delete(EntityformType $type) {
$type
->delete();
}
function entityform_type_cache_reset() {
cache_clear_all('entityform_types', 'cache');
drupal_static_reset('entityform_get_types');
}
function entityform_uri(Entityform $entityform) {
return array(
'path' => 'entityform/' . $entityform->entityform_id,
);
}
function entityform_page_title($entityform, $op = 'view') {
if (!empty($entityform)) {
$entityform_type = entityform_get_types($entityform->type);
switch ($op) {
case 'submit':
return $entityform_type->label;
default:
return t('Form Submission') . ': ' . $entityform_type->label;
}
}
}
function entityform_page_view($entityform, $view_mode = 'full') {
$controller = entity_get_controller('entityform');
$content = $controller
->view(array(
$entityform->entityform_id => $entityform,
), $view_mode);
return $content;
}
function entityform_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'entityform') . '/views',
);
}
function entityform_theme() {
return array(
'entityform_add_list' => array(
'variables' => array(
'content' => array(),
),
'file' => 'entityform.admin.inc',
),
'entityform' => array(
'render element' => 'elements',
'template' => 'entityform',
),
'entityform_submittd_data' => array(
'render element' => 'elements',
'template' => 'entityform-submittd-data',
),
);
}
function template_preprocess_entityform_submittd_data(&$variables) {
$entityform = $variables['entityform'];
$variables['date'] = format_date($entityform->created);
$variables['name'] = theme('username', array(
'account' => $entityform,
));
$uri = entity_uri('entityform', $entityform);
$uri['options']['absolute'] = TRUE;
$variables['url'] = url($uri['path'], $uri['options']);
}
function entityform_draft_page($entityform_type) {
$args = func_get_args();
array_shift($args);
if (empty($entityform_type->data['draft_save_text'])) {
$draft_text = t('Your submission for the form, @form_name, has been saved.', array(
'@form_name' => $entityform_type->label,
));
}
else {
$draft_text = $entityform_type->data['draft_save_text'];
}
$render_array = array(
'submit_text' => array(
'#type' => 'markup',
'#prefix' => '<div class="draft-text">',
'#markup' => _entityform_format_text($draft_text, array(
'entityform_type' => $entityform_type,
)),
'#suffix' => '</div>',
),
);
drupal_alter(array(
'entityform_draft_page',
"entityform_{$entityform_type->type}_draft_page",
), $render_array, $entityform_type, $args);
return $render_array;
}
function entityform_confirm_page($entityform_type) {
$entityform_id = $_GET['entityform_id'];
$entityform = entityform_load($entityform_id);
$render_array = array(
'submit_text' => array(
'#type' => 'markup',
'#prefix' => '<div class="submission-text">',
'#markup' => _entityform_format_text($entityform_type->data['submission_text'], array(
'entityform_type' => $entityform_type,
'entityform' => $entityform,
)),
'#suffix' => '</div>',
),
);
if ($entityform_type->data['submission_show_submitted']) {
if (empty($entityform)) {
return MENU_NOT_FOUND;
}
$controller = entity_get_controller('entityform');
$content = $controller
->view(array(
$entityform->entityform_id => $entityform,
), 'confirmation', NULL, TRUE);
$render_array['submission_data'] = $content;
}
drupal_alter(array(
'entityform_confirm_page',
"entityform_{$entityform_type->type}_confirm_page",
), $render_array, $entityform_type, $entityform_id);
return $render_array;
}
function entityform_type_page_title($entityform_type, $op) {
if ($op == 'confirm') {
if (!isset($entityform_type->data['submission_page_title']) || empty($entityform_type->data['submission_page_title'])) {
$default_values = variable_get('entityform_type_defaults', array());
if (empty($default_values['data']['submission_page_title'])) {
return 'Thank You';
}
return $default_values['data']['submission_page_title'];
}
return $entityform_type->data['submission_page_title'];
}
if ($op == 'submissions report') {
return t('Form Submissions') . (is_object($entityform_type) ? ': ' . $entityform_type->label : '');
}
if ($op == 'view submissions') {
return t('Your Submissions') . (is_object($entityform_type) ? ': ' . $entityform_type->label : '');
}
}
function entityform_entity_presave($entity, $type) {
if ($type == 'entityform_type') {
if (module_exists('path')) {
if (isset($entity->paths)) {
$original_entity = $entity->original;
foreach ($entity->paths as $key => $path) {
$path = $entity->paths[$key];
$path['alias'] = trim($path['alias']);
if (isset($original_entity->paths[$key])) {
$original_path = $original_entity->paths[$key];
if (!empty($original_path['alias']) && empty($path['alias']) || $path['alias'] != $original_path['alias']) {
path_delete($original_path);
}
}
if (!empty($path['alias'])) {
switch ($key) {
case 'submit':
$path['source'] = _entityform_type_get_submit_url($entity->type);
break;
case 'confirm':
$path['source'] = _entityform_type_get_confirm_url($entity->type);
break;
}
if (!empty($original_path['alias']) && $original_path['alias'] == $path['alias'] && $path['source'] == $original_path['source']) {
continue;
}
$path['language'] = LANGUAGE_NONE;
if (_entityform_alias_is_used($path)) {
watchdog('entityform', "Could not create alias @alias for EntityformType entity '@type'", array(
'@alias' => $path['alias'],
'@type' => $entity->type,
), WATCHDOG_WARNING);
}
else {
path_save($path);
}
}
}
}
}
if (module_exists('menu')) {
if (isset($entity->menu)) {
$link =& $entity->menu;
if ($link['enabled']) {
$link = $entity->menu;
$link['link_path'] = _entityform_type_get_submit_url($entity->type);
list($link['menu_name'], $link['plid']) = explode(':', $link['parent']);
if (trim($link['description'])) {
$link['options']['attributes']['title'] = trim($link['description']);
}
else {
unset($link['options']['attributes']['title']);
}
if (!menu_link_save($link)) {
drupal_set_message(t('There was an error saving the menu link.'), 'error');
}
}
else {
if (!empty($link['mlid'])) {
menu_link_delete($link['mlid']);
}
}
}
}
}
}
function _entityform_alias_is_used($path) {
$query = db_select('url_alias')
->condition('alias', $path['alias'])
->condition('language', $path['language']);
if (!empty($path['source'])) {
$query
->condition('source', $path['source'], '<>');
}
$query
->addExpression('1');
$query
->range(0, 1);
if ($query
->execute()
->fetchField()) {
return TRUE;
}
return FALSE;
}
function entityform_submission_page($entityform_type, $show_display_id = NULL, $mode) {
$output = array();
if ($mode == 'admins') {
$submission_view = _entityform_get_type_data_setting($entityform_type, 'submissions_view');
}
else {
$submission_view = _entityform_get_type_data_setting($entityform_type, 'user_submissions_view');
$output['return_link'] = array(
'#theme' => 'link',
'#path' => "eform/submit/{$entityform_type->type}",
'#text' => t('Return to form'),
'#options' => array(
'attributes' => array(),
'html' => FALSE,
),
);
}
$parts = explode(':', $submission_view);
$view_name = $parts[0];
$view = views_get_view($view_name);
$display_ids = array_keys($view->display);
array_shift($display_ids);
$links = '';
foreach ($display_ids as $display_id) {
$enabled = !(isset($view->display[$display_id]->display_options['enabled']) && $view->display[$display_id]->display_options['enabled'] === FALSE);
if ($enabled && $view->display[$display_id]->display_plugin != 'views_data_export') {
$links[] = array(
'href' => _entityform_get_submissions_view_path($display_id),
'title' => $view->display[$display_id]->display_title,
);
}
if (empty($show_display_id)) {
$show_display_id = $display_id;
}
}
if (count($links) > 1) {
$output['links'] = array(
'#theme' => 'links',
'#links' => $links,
'#attributes' => array(
'class' => 'tabs secondary',
),
);
}
$output['view'] = array(
'#type' => 'markup',
'#markup' => entityform_embed_view($view_name, $show_display_id, $entityform_type->type),
);
return $output;
}
function _entityform_get_submissions_view_path($display_id) {
$menu_item = menu_get_item();
$menu_path = $menu_item['path'];
$menu_path_parts = explode('/', $menu_path);
$path_parts = explode('/', current_path());
while (count($path_parts) > count($menu_path_parts)) {
array_pop($path_parts);
}
$path_parts[] = $display_id;
return implode('/', $path_parts);
}
function entityform_embed_view($name, $display_id = 'default') {
$args = func_get_args();
array_shift($args);
if (count($args)) {
array_shift($args);
}
$view = views_get_view($name);
$view->override_path = _entityform_get_submissions_view_path($display_id);
if (!$view || !$view
->access($display_id)) {
return;
}
return $view
->preview($display_id, $args);
}
function _entityform_get_type_data_setting($entityform_type, $setting, $default_value = NULL) {
static $empty_type;
if (!empty($entityform_type->data[$setting]) && $entityform_type->data[$setting] != 'default' && $entityform_type->data[$setting] != 'default_view') {
return $entityform_type->data[$setting];
}
else {
if ($default_value !== NULL) {
return $default_value;
}
if (empty($empty_type)) {
$empty_type = entity_get_controller('entityform_type')
->create();
}
if (isset($empty_type->data[$setting])) {
return $empty_type->data[$setting];
}
return NULL;
}
}
function entityform_menu_local_tasks_alter(&$data, $router_item, $root_path) {
if ($root_path == 'admin/structure/entityforms') {
$item = menu_get_item('admin/structure/entityforms/add');
if ($item['access']) {
$data['actions']['output'][] = array(
'#theme' => 'menu_local_action',
'#link' => $item,
);
}
}
}
function entityform_render_to_text($render_array, $print_empty_title = FALSE, $indent_text = '') {
$tab_text = " ";
$output = '';
$markup = '';
$title = '';
if (!empty($render_array['#markup'])) {
$markup .= "{$indent_text}{$tab_text}" . htmlspecialchars_decode(strip_tags($render_array['#markup']));
$markup = trim($markup);
}
if (isset($render_array['#title'])) {
$title .= htmlspecialchars_decode($render_array['#title']) . ":";
$title = trim($title);
}
if (!empty($markup) && empty($title)) {
$output .= "\n" . $indent_text . $markup . "\n";
}
elseif (!empty($markup) && !empty($title)) {
$output .= "\n" . $indent_text . $title . "\n" . $indent_text . $tab_text . $markup;
}
elseif (empty($markup) && !empty($title)) {
$output .= "\n" . $indent_text . $title;
}
if (!empty($output)) {
$indent_text .= $tab_text;
}
foreach (element_children($render_array) as $child) {
$child_text = entityform_render_to_text($render_array[$child], $print_empty_title, $indent_text);
if (isset($render_array[$child]['#weight'])) {
$child_array[$render_array[$child]['#weight']] = $child_text;
}
else {
$child_array[] = $child_text;
}
}
if (!empty($child_array)) {
ksort($child_array);
$output .= implode('', $child_array);
}
return $output;
}
function _entityform_get_submit_info($entityform) {
$account = user_load($entityform->uid);
return t("Submitted by !name on !date", array(
'!name' => theme('username', array(
'account' => $account,
)),
'!date' => format_date($entityform->created),
));
}
class Entityform extends Entity {
public $uid;
public function __construct($values = array()) {
parent::__construct($values, 'entityform');
}
protected function defaultLabel() {
$entityform_type = entityform_type_load($this->type);
$label = $entityform_type->label;
$submit_user = NULL;
if (!empty($this->uid)) {
$submit_user = user_load($this->uid);
}
$label .= ' - ' . format_username($submit_user);
$label .= ' - ' . format_date($this->created, 'short');
return $label;
}
protected function defaultUri() {
return array(
'path' => 'entityform/' . $this->entityform_id,
);
}
public function getTypeName() {
$entityform_type = entityform_type_load($this->type);
return $entityform_type->label;
}
public function user() {
return user_load($this->uid);
}
public function setUser($account) {
$this->uid = is_object($account) ? $account->uid : $account;
}
}
class EntityformType extends Entity {
public $type;
public $label;
public $data;
public function __construct($values = array()) {
parent::__construct($values, 'entityform_type');
}
public function get_redirect_path($entityform = NULL) {
if ($this->data['redirect_path'] == '<front>') {
return $this->data['redirect_path'];
}
return _entityform_format_text($this->data['redirect_path'], array(
'entityform_type' => $this,
'entityform' => $entityform,
));
}
}
class EntityformController extends EntityAPIController {
public function __construct($entityType) {
parent::__construct($entityType);
}
public function create(array $values = array()) {
if (isset($values['type'])) {
$type = entityform_get_types($values['type']);
}
if (empty($type)) {
return NULL;
}
$values += array(
'entityform_id' => '',
'is_new' => TRUE,
'title' => '',
'created' => '',
'changed' => '',
'data' => '',
);
$entityform = parent::create($values);
return $entityform;
}
public function buildContent($entity, $view_mode = 'full', $langcode = NULL, $content = array()) {
$content = parent::buildContent($entity, $view_mode, $langcode, $content);
$content['info']['user'] = array(
'#markup' => _entityform_get_submit_info($entity),
'#weight' => -100,
);
$content['info']['#weight'] = -99;
return $content;
}
}
class EntityformTypeController extends EntityAPIControllerExportable {
public function __construct($entityType) {
parent::__construct($entityType);
}
public function create(array $values = array()) {
$default_values = variable_get('entityform_type_defaults', array());
$values += $default_values;
$values += array(
'id' => '',
'is_new' => TRUE,
'data' => array(
'submissions_view' => 'entityforms',
'user_submissions_view' => 'user_entityforms',
),
);
$entityform_type = parent::create($values);
return $entityform_type;
}
public function view($entities, $view_mode = 'full', $langcode = NULL, $page = NULL) {
$view = parent::view($entities, $view_mode, $langcode, $page);
foreach ($entities as $entity_id => $entity) {
module_load_include('inc', 'entityform', 'entityform.admin');
$view[$this->entityType][$entity->type]['form'] = entityform_form_wrapper(entityform_empty_load($entity->type), 'submit', 'embedded');
}
return $view;
}
public function load($ids = array(), $conditions = array()) {
$entityform_types = parent::load($ids, $conditions);
foreach ($entityform_types as $entityform_type) {
if (module_exists('path')) {
$entityform_type->paths = array();
$path_types = _entityform_type_get_path_types($entityform_type->type);
foreach ($path_types as $key => $path_type) {
$conditions = array(
'source' => $path_type['default_path'],
);
$path = path_load($conditions);
if ($path !== FALSE) {
$entityform_type->paths[$key] = $path;
}
}
}
}
return $entityform_types;
}
public function save($entity, DatabaseTransaction $transaction = NULL) {
$return = parent::save($entity, $transaction);
entityform_type_cache_reset();
field_info_cache_clear();
return $return;
}
public function delete($ids, DatabaseTransaction $transaction = NULL) {
$entities = $ids ? $this
->load($ids) : FALSE;
if ($entities) {
if (module_exists('path')) {
foreach ($entities as $id => $entity) {
try {
$path_types = _entityform_type_get_path_types($entity->type);
foreach ($path_types as $path_type) {
path_delete(array(
'source' => $path_type['default_path'],
));
}
} catch (Exception $e) {
}
}
}
parent::delete($ids, $transaction);
field_info_cache_clear();
entityform_type_cache_reset();
}
}
public function export($entity, $prefix = '') {
if (module_exists('path')) {
$path_types = _entityform_type_get_path_types($entity->type);
foreach ($path_types as $key => $path_type) {
unset($entity->paths[$key]['pid']);
}
}
return parent::export($entity, $prefix);
}
}
function _entityform_type_get_path_types($type) {
return array(
'submit' => array(
'default_path' => _entityform_type_get_submit_url($type),
'title' => 'Submit URL alias',
'description' => 'Optionally specify an alternative URL by which the form submit page can be accessed.',
),
'confirm' => array(
'default_path' => _entityform_type_get_confirm_url($type),
'title' => 'Confirm URL alias',
'description' => 'Optionally specify an alternative URL by which the form confirmation page(after the form has been submitted) can be accessed.',
),
);
}
function entityform_views_pre_view(&$view, &$display_id, &$args) {
if ($view->base_table == 'entityform' && strpos($display_id, 'autofields_') === 0) {
$type = $view->args[0];
_entityform_view_add_all_fields($view, $display_id, $type);
}
return;
}
function _entityform_view_add_all_fields(&$view, $display_id, $bundle_name) {
$instances = field_info_instances('entityform', $bundle_name);
switch ($display_id) {
case 'autofields_csv':
case 'autofields_xml':
$view_mode = 'download';
break;
case 'autofields_table':
$view_mode = 'table';
break;
default:
$view_mode = 'default';
}
$autofields = array();
foreach ($instances as $instance) {
$field_display = isset($instance['display'][$view_mode]) ? $instance['display'][$view_mode] : $instance['display']['default'];
if ($field_display['type'] != 'hidden') {
$autofields[] = array(
'field_name' => $instance['field_name'],
'options' => array(
'type' => $field_display['type'],
'settings' => $field_display['settings'],
),
'weight' => $field_display['weight'],
);
}
}
uasort($autofields, 'drupal_sort_weight');
foreach ($autofields as $field) {
$view
->add_item($display_id, 'field', "field_data_{$field['field_name']}", $field['field_name'], $field['options']);
}
}
function entityform_help($path, $arg) {
$output = '';
$view_mode_text = 'This View Mode is used to controls how fields will displayed ';
switch ($path) {
case 'admin/structure/entityform_types/manage/%/display/download':
$output = t("{$view_mode_text} in the CSV and XML downloads and their order.");
break;
case 'admin/structure/entityform_types/manage/%/display/table':
$output = t("{$view_mode_text} in submissions table.");
break;
case 'admin/structure/entityform_types/manage/%/display/confirmation':
$output = t("{$view_mode_text} on the submission conformation page.");
break;
case 'admin/config/content/entityform':
$output = t('Set the default settings for new Entityform Types. This will be used for new Entityform Types.');
break;
case 'admin/structure/entityform_types/manage/%/submissions':
$display_id = $arg[6];
case 'admin/reports/entityforms/submissions/%':
if ($path == 'admin/reports/entityforms/submissions/%') {
$display_id = $arg[5];
}
if (user_access('administer entityform types')) {
if ($display_id == 'autofields_table') {
$output = t('The fields on this table can be controlled via the "Table" view mode under Manage Display for this Entityform Type.');
}
}
case 'admin/structure/entityform_types/access_rules/manage/%':
$rule_object = menu_get_object('rules_config', 5);
if (is_subclass_of($rule_object, 'RulesActionContainer')) {
$output = t('This Rule must set the provided variable %var to TRUE for the Entityform to show.', array(
'%var' => 'show_form',
));
}
elseif (is_subclass_of($rule_object, 'RulesConditionContainer')) {
$output = t('This Rule must evaluate to TRUE for the Entityform to show.', array(
'%var' => 'show_form',
));
}
break;
case 'admin/structure/entityform_types/validation_rules/manage/%':
$output = t('If this Rule sets the provided variable %var to FALSE then the Entityform submission will not validae.', array(
'%var' => 'validate_form',
));
$output .= ' ' . t('The Rule should also display a message to the user about the validation error.');
}
return $output;
}
function entityformtype_metadata_get_properties($entityform_type, array $options, $name, $entity_type) {
if (isset($entityform_type->data[$name])) {
return $entityform_type->data[$name];
}
return NULL;
}
function entityform_invoke_rules($entityform, $rule_setting) {
$entityform_type = entityform_type_load($entityform->type);
$return_values = array();
if (isset($entityform_type->data[$rule_setting]) && is_array($entityform_type->data[$rule_setting])) {
foreach ($entityform_type->data[$rule_setting] as $rule_name) {
if (rules_get_cache('comp_' . $rule_name)) {
$return_values[] = rules_invoke_component($rule_name, $entityform, $entityform_type);
}
else {
$msg = t('Entityform Type %form references non-existing Rule component %component', array(
'%form' => $entityform->type,
'%component' => $rule_name,
));
watchdog('entityform', $msg);
if (user_access('administer entityform types')) {
drupal_set_message($msg, 'warning');
}
}
}
}
return $return_values;
}
function _entityform_rules_all_pass($rule_returns) {
$pass = FALSE;
if (empty($rule_returns)) {
return TRUE;
}
foreach ($rule_returns as $rule_return) {
if ($rule_return === FALSE) {
return FALSE;
}
if (is_array($rule_return)) {
if (isset($rule_return[0]) && $rule_return[0] == FALSE) {
return FALSE;
}
}
}
return TRUE;
}
function _entityform_remove_fieldsets_from_tree(&$elements, $parents) {
foreach (element_children($elements) as $key) {
if (strrpos($key, '_set') === drupal_strlen($key) - drupal_strlen('_set')) {
foreach (element_children($elements[$key]) as $sub_key) {
$new_parents = $parents;
$new_parents[] = $sub_key;
$elements[$key][$sub_key]['#parents'] = $new_parents;
}
}
}
}
function entityform_forms() {
$types = entityform_get_types();
$forms = array();
foreach ($types as $machine_name => $type) {
$forms[$machine_name . '_entityform_edit_form'] = array(
'callback' => 'entityform_edit_form',
);
}
$rule_types = _entityform_get_rule_types();
foreach ($rule_types as $rule_type) {
$forms["entityform_add_{$rule_type}_rule_form"] = array(
'callback' => 'rules_admin_add_component',
);
}
return $forms;
}
function _entityform_get_rule_types() {
$rule_types = array(
'access',
'submission',
'validation',
);
return $rule_types;
}
function entityform_mollom_form_list() {
$forms = array();
$types = entityform_get_types();
$forms = array();
foreach ($types as $machine_name => $type) {
$forms[$machine_name . '_entityform_edit_form'] = array(
'title' => t('Entityform: @name form', array(
'@name' => $type->label,
)),
'entity' => 'entityform',
'bundle' => $type->type,
'delete form' => 'entityform_delete_form',
'delete form file' => array(
'name' => 'entityform.admin.inc',
),
'report access' => array(
'administer entityform types',
),
);
}
return $forms;
}
function entityform_mollom_form_info($form_id) {
$entityform_type = drupal_substr($form_id, 0, -21);
$type = entityform_type_load($entityform_type);
$form_info = array(
'bypass access' => array(
'bypass entityform access',
),
'bundle' => $type->type,
'bypass access' => array(
'edit any entityform',
'delete any entityform',
),
);
mollom_form_info_add_fields($form_info, 'entityform', $type->type);
return $form_info;
}
function _entityform_format_text($text_value, $token_types = array()) {
$is_filtered = FALSE;
if (is_array($text_value)) {
$text = $text_value['value'];
$format = $text_value['format'];
$is_filtered = TRUE;
}
else {
$text = check_plain($text_value);
}
if ($token_types !== FALSE) {
$token_types[] = 'global';
$text = token_replace($text, $token_types);
}
if ($is_filtered) {
$text = check_markup($text, $format);
}
return $text;
}
function entityform_views_query_alter(&$view, &$query) {
global $user;
if (!empty($view->query->tags) && in_array('user_access', $view->query->tags) && $view->base_table == 'entityform' && !user_access('view any entityform')) {
$table_alias = 'entityform';
if (user_access('view own entityform')) {
$uid = $user->uid;
}
else {
$uid = -1;
}
$query->where[] = array(
'conditions' => array(
array(
'field' => "{$table_alias}.uid",
'value' => $uid,
'operator' => '=',
),
),
'type' => 'AND',
'args' => array(),
);
}
}
function _entityform_type_get_submit_url($type) {
return 'eform/submit/' . str_replace('_', '-', $type);
}
function _entityform_type_get_confirm_url($type) {
return 'eform/' . str_replace('_', '-', $type) . '/confirm';
}
function entityform_form_entityform_add_access_rule_form_alter(&$form, $form_state, $form_id) {
_entityform_add_rules_component_alter($form, 'entityform access', array(
'and',
'or',
'rule',
'rule set',
));
if (!empty($form['plugin_name']['#default_value'])) {
if (in_array($form['plugin_name']['#default_value'], array(
'rule',
'rule set',
))) {
$form['settings']['vars']['#value']['items'][] = array(
'type' => 'boolean',
'label' => 'Show form',
'name' => 'show_form',
'usage' => '01',
'weight' => 3,
);
}
}
}
function entityform_form_entityform_add_validation_rule_form_alter(&$form, $form_state, $form_id) {
_entityform_add_rules_component_alter($form, 'entityform validation', array(
'rule',
'rule set',
));
if (!empty($form['plugin_name']['#default_value'])) {
if (in_array($form['plugin_name']['#default_value'], array(
'rule',
'rule set',
))) {
$form['settings']['vars']['#value']['items'][] = array(
'type' => 'boolean',
'label' => 'Form Validates',
'name' => 'validate_form',
'usage' => '01',
'weight' => 3,
);
}
}
}
function _entityform_add_rules_component_alter(&$form, $tags, $component_types = NULL) {
$form['settings']['tags'] = array(
'#type' => 'value',
'#value' => $tags,
);
if ($component_types && !empty($form['plugin_name']['#options'])) {
foreach ($form['plugin_name']['#options'] as $key => $value) {
if (!in_array($key, $component_types)) {
unset($form['plugin_name']['#options'][$key]);
}
}
}
$var_items[] = array(
'type' => 'entityform',
'label' => 'Submitted Entityform',
'name' => 'entityform',
'usage' => '10',
'weight' => 1,
);
$var_items[] = array(
'type' => 'entityform_type',
'label' => 'Entityform Type',
'name' => 'entityform_type',
'usage' => '10',
'weight' => 2,
);
$form['settings']['vars'] = array(
'#type' => 'value',
'#value' => array(
'items' => $var_items,
),
);
}
function entityform_entity_update($entity, $type) {
if ($type == 'entityform' && empty($entity->draft)) {
entityform_invoke_rules($entity, 'submission_rules');
}
}
function entityform_entity_insert($entity, $type) {
if ($type == 'entityform' && empty($entity->draft)) {
entityform_invoke_rules($entity, 'submission_rules');
}
}
function entityform_form_entityform_add_submission_rule_form_alter(&$form, $form_state, $form_id) {
_entityform_add_rules_component_alter($form, 'entityform submission', array(
'rule',
'rule set',
));
}
function entityform_load_required_entityform_admin($form, &$form_state) {
module_load_include('inc', 'entityform', 'entityform.admin');
return $form;
}