View source
<?php
define('MERCI_RESTRICTIONS', 'merci_restrictions');
define('MERCI_OVERRIDE_RESTRICTIONS', 'merci_override_restrictions');
define('MERCI_CHECKOUT_MAX_LENGTH', 'field_max_length_of_checkout');
define('MERCI_CHECKOUT_OFFSITE', 'field_checkout_offsite');
define('MERCI_DEFAULT_MAX_LENGTH', '');
define('MERCI_DEFAULT_ALLOW_OVERNIGHT', TRUE);
define('MERCI_RESTRICTIONS_TYPE_PATH', MERCI_ADMIN_CONFIG_PATH . '/merci-restrictions');
define('MERCI_RESTRICTIONS_PATH', MERCI_RESTRICTIONS_TYPE_PATH);
function merci_restrictions_field_widget_form_alter(&$element, &$form_state, $context) {
if ($context['field']['field_name'] == MERCI_OVERRIDE_RESTRICTIONS) {
$line_item = $element['target_id']['#entity'];
if (property_exists($line_item, MERCI_OVERRIDE_RESTRICTIONS) and !empty($line_item->{MERCI_OVERRIDE_RESTRICTIONS}['und'])) {
return;
}
$element['#attributes']['class'][] = 'myclass';
$element['override_password'] = array(
'#type' => 'password',
'#title' => t('Password'),
);
$element['#type'] = 'fieldset';
$element['#title'] = 'Override restrictions';
$element['#element_validate'][] = 'merci_restrictions_override_validate';
}
}
function merci_restrictions_override_validate($element, &$form_state, $form) {
if (!empty($element['target_id']['#value'])) {
$line_item = $element['target_id']['#entity'];
if ($line_item->is_new) {
if (preg_match("/.+\\((\\d+)\\)/", $element['target_id']['#value'], $matches)) {
$value = $matches[1];
$account = user_load($value);
$name = $account->name;
}
else {
$name = $element['target_id']['#value'];
}
$password = $element['override_password']['#value'];
if (!user_authenticate($name, $password)) {
form_error($element, t('Password for override operator incorrect.'));
}
}
}
}
function merci_restrictions_permission() {
$perms = array();
$perms['administer all merci restrictions'] = array(
'title' => t('Administer all merci restrictions'),
'description' => t('Perform all administration tasks for merci restrictions.'),
);
return $perms;
}
function merci_restrictions_redirect($type = NULL) {
drupal_goto(MERCI_RESTRICTIONS_TYPE_PATH . '/fields');
}
function merci_restrictions_menu() {
$items = array();
$items[MERCI_RESTRICTIONS_TYPE_PATH] = array(
'title' => 'Merci Restrictions',
'description' => 'Manage fields for merci restrictions',
'page callback' => 'merci_restrictions_redirect',
'access callback' => 'user_access',
'access arguments' => array(
'administer MERCI',
),
'type' => MENU_NORMAL_ITEM,
);
$items = array();
$items['admin/merci/config/merci-restrictions/settings'] = array(
'title' => 'Sitewide merci restrictions',
'description' => "Site's default merci restrictions.",
'page callback' => 'drupal_get_form',
'page arguments' => array(
'merci_restrictions_sitewide_settings',
),
'access arguments' => array(
'administer all merci restrictions',
),
'access callback' => 'user_access',
'file' => 'merci_restrictions.admin.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 5,
);
return $items;
}
function merci_restrictions_page() {
return "Default page.";
}
function merci_restrictions_entity_info() {
$info = array();
$info['merci_restrictions'] = array(
'label' => t('Merci restrictions'),
'plural label' => t('Merci restrictions'),
'module' => 'merci_restrictions',
'base table' => 'merci_restrictions',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'id',
'bundle' => 'type',
'label' => 'title',
),
'bundle keys' => array(
'bundle' => 'type',
),
'bundles' => array(
'merci_restrictions' => array(
'label' => t('Merci restrictions'),
'admin' => array(
'path' => MERCI_RESTRICTIONS_TYPE_PATH,
),
),
),
'admin ui' => array(
'path' => 'merci_restrictions',
'file' => 'merci_restrictions.pages.inc',
'controller class' => 'MerciRestrictionsUIController',
),
'entity class' => 'MerciRestrictions',
'controller class' => 'EntityAPIController',
'exportable' => TRUE,
'access callback' => 'merci_restrictions_access',
'inline entity form' => array(
'controller' => 'MerciRestrictionInlineEntityFormController',
),
);
return $info;
}
function merci_restrictions_add_page($entity_type) {
$merci_restrictions_types = merci_restrictions_types();
if (count($merci_restrictions_types) == 1) {
$info = entity_get_info($entity_type);
$type = reset($merci_restrictions_types);
drupal_goto($info['admin ui']['path'] . '/add/' . $type->type);
}
return entity_ui_bundle_add_page($entity_type);
}
function merci_restrictions_get_action_title($op, $entity_type, $bundle_name = NULL) {
$info = entity_get_info($entity_type);
$merci_restrictions_types = merci_restrictions_types();
if (count($merci_restrictions_types) == 1) {
$info = entity_get_info($entity_type);
$type = reset($merci_restrictions_types);
$bundle_name = isset($bundle_name) ? $bundle_name : $type->type;
switch ($op) {
case 'add':
if (isset($bundle_name) && $bundle_name != $entity_type) {
return t('Add @bundle_name', array(
'@bundle_name' => drupal_strtolower($info['bundles'][$bundle_name]['label']),
));
}
else {
return t('Add @entity_type', array(
'@entity_type' => drupal_strtolower($info['label']),
));
}
case 'import':
return t('Import @entity_type', array(
'@entity_type' => drupal_strtolower($info['label']),
));
}
}
return entity_ui_get_action_title($op, $entity_type, $bundle_name);
}
function merci_restrictions_access($op, $entity = NULL, $account = NULL) {
if ($op == 'view') {
return TRUE;
}
return user_access('administer all merci restrictions');
}
function merci_restrictions_load($name = NULL) {
$wc = entity_load_multiple_by_name('merci_restrictions', isset($name) ? array(
$name,
) : FALSE);
return isset($name) ? reset($wc) : $wc;
}
function merci_restrictions_form_element($include_default = FALSE, $default_value = NULL) {
$options = array();
if ($include_default && variable_get('merci_restrictions_default', FALSE)) {
$options[0] = t('-- use site default --');
}
$options += db_select('merci_restrictions', 'wc')
->fields('wc', array(
'name',
'label',
))
->execute()
->fetchAllKeyed(0, 1);
$element = array(
'#type' => 'select',
'#title' => t('Choose merci restrictions'),
'#default_value' => $default_value,
'#options' => $options,
);
return $element;
}
function merci_restrictions_merci_fields_info() {
$fields = array();
module_load_include('inc', 'merci_restrictions', 'merci_restrictions_fields');
$field_bases = merci_restrictions_fields();
$instances = merci_restrictions_instances();
foreach ($field_bases as $field_name => $field) {
$fields[$field_name] = array(
'field' => $field,
'instance' => $instances[$field_name],
);
}
return $fields;
}