| 1 |
<?php
|
| 2 |
// $Id: ec_location.module,v 1.1 2007/05/18 16:06:21 darrenoh Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Implementation of hook_menu().
|
| 6 |
*/
|
| 7 |
function ec_location_menu($may_cache) {
|
| 8 |
$items = array();
|
| 9 |
if ($may_cache) {
|
| 10 |
$items[] = array(
|
| 11 |
'path' => 'admin/ecsettings/ec_location',
|
| 12 |
'title' => t('Location module integration'),
|
| 13 |
'description' => t('Configure location module settings for product types.'),
|
| 14 |
'access' => user_access('administer products'),
|
| 15 |
'callback' => 'ec_location_product_types_page',
|
| 16 |
);
|
| 17 |
}
|
| 18 |
else {
|
| 19 |
$ptypes = product_get_ptypes();
|
| 20 |
foreach ($ptypes as $ptype => $name) {
|
| 21 |
$items[] = array(
|
| 22 |
'path' => "admin/ecsettings/ec_location/$ptype",
|
| 23 |
'title' => $name,
|
| 24 |
'description' => t('@type location settings', array('@type' => $name)),
|
| 25 |
'callback' => 'drupal_get_form',
|
| 26 |
'callback arguments' => array('ec_location_product_type_form', $ptype),
|
| 27 |
'type' => MENU_LOCAL_TASK,
|
| 28 |
);
|
| 29 |
}
|
| 30 |
}
|
| 31 |
return $items;
|
| 32 |
}
|
| 33 |
|
| 34 |
/**
|
| 35 |
* Menu callback; product types overview.
|
| 36 |
* @return HTML string
|
| 37 |
*/
|
| 38 |
function ec_location_product_types_page() {
|
| 39 |
$output = '<p>'. t('Configure location module settings for product types.') .'</p>';
|
| 40 |
$output .= '<ul>';
|
| 41 |
$ptypes = product_get_ptypes();
|
| 42 |
foreach ($ptypes as $ptype => $name) {
|
| 43 |
$output .= '<li>'. l($name, "admin/ecsettings/ec_location/$ptype") .'</li>';
|
| 44 |
}
|
| 45 |
$output .= '</ul>';
|
| 46 |
return $output;
|
| 47 |
}
|
| 48 |
|
| 49 |
/**
|
| 50 |
* Menu callback; return product type configuration form.
|
| 51 |
* @param $ptype: String composed of the product type name
|
| 52 |
* @return Product type configuration form
|
| 53 |
*/
|
| 54 |
function ec_location_product_type_form($ptype) {
|
| 55 |
$form = array();
|
| 56 |
if (module_hook('location', 'form_alter')) {
|
| 57 |
$form['#node_type']->type = "product_$ptype";
|
| 58 |
$form['identity']['type'] = array(
|
| 59 |
'#type' => 'value',
|
| 60 |
'#value' => "product_$ptype",
|
| 61 |
);
|
| 62 |
location_form_alter('node_type_form', $form);
|
| 63 |
foreach ($form['location'] as $element => $value) {
|
| 64 |
if (strpos($element, '#') === 0) {
|
| 65 |
unset($form['location'][$element]);
|
| 66 |
}
|
| 67 |
}
|
| 68 |
$form['submit'] = array(
|
| 69 |
'#type' => 'submit',
|
| 70 |
'#value' => t('Save configuration'),
|
| 71 |
'#weight' => 40,
|
| 72 |
);
|
| 73 |
$form['reset'] = array(
|
| 74 |
'#type' => 'submit',
|
| 75 |
'#value' => t('Reset to defaults'),
|
| 76 |
'#weight' => 50,
|
| 77 |
);
|
| 78 |
}
|
| 79 |
else {
|
| 80 |
$form['location']['#value'] = t('The Location module is not enabled.');
|
| 81 |
}
|
| 82 |
return $form;
|
| 83 |
}
|
| 84 |
|
| 85 |
function ec_location_product_type_form_submit($form_id, $form_values) {
|
| 86 |
$ptype = $form_values['type'];
|
| 87 |
unset($form_values['type'], $form_values['form_token'], $form_values['op'], $form_values['submit'], $form_values['delete'], $form_values['reset'], $form_values['form_id']);
|
| 88 |
foreach ($form_values as $key => $value) {
|
| 89 |
$key .= '_'. $ptype;
|
| 90 |
if ($op == t('Reset to defaults')) {
|
| 91 |
variable_del($key);
|
| 92 |
}
|
| 93 |
else {
|
| 94 |
if (is_array($value)) {
|
| 95 |
$value = array_keys(array_filter($value));
|
| 96 |
}
|
| 97 |
variable_set($key, $value);
|
| 98 |
|
| 99 |
if ($type->old_type != $type->type) {
|
| 100 |
$key = str_replace($type->type, $type->old_type, $key);
|
| 101 |
variable_del($key);
|
| 102 |
}
|
| 103 |
}
|
| 104 |
}
|
| 105 |
}
|
| 106 |
|
| 107 |
/**
|
| 108 |
* Implementation of hook_form_alter().
|
| 109 |
*/
|
| 110 |
function ec_location_form_alter($form_id, &$form) {
|
| 111 |
switch ($form_id) {
|
| 112 |
case 'product_node_form':
|
| 113 |
if (!variable_get('location_maxnum_product', 0) && variable_get('location_maxnum_product_'. $form['product']['ptype']['#value'], 0)) {
|
| 114 |
$temp['type']['#value'] = $form['type']['#value'];
|
| 115 |
$form['type']['#value'] .= '_'. $form['product']['ptype']['#value'];
|
| 116 |
$temp['#node']->type = $form['#node']->type;
|
| 117 |
$form['#node']->type .= '_'. $form['product']['ptype']['#value'];
|
| 118 |
location_form_alter($form['type']['#value'] .'_node_form', $form);
|
| 119 |
$form['type']['#value'] = $temp['type']['#value'];
|
| 120 |
$form['#node']->type = $temp['#node']->type;
|
| 121 |
$form['body_filter']['#weight'] = 0;
|
| 122 |
$form['locations']['#weight'] = 0;
|
| 123 |
}
|
| 124 |
break;
|
| 125 |
}
|
| 126 |
}
|
| 127 |
|
| 128 |
/**
|
| 129 |
* Implementation of hook_nodeapi().
|
| 130 |
*/
|
| 131 |
function ec_location_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
|
| 132 |
if (module_hook('location', 'nodeapi') && variable_get('location_maxnum_product_'. $node->ptype, 0) > 0) {
|
| 133 |
$type = $node->type;
|
| 134 |
$node->type .= '_'. $node->ptype;
|
| 135 |
$locations = location_nodeapi($node, $op, $a3, $a4);
|
| 136 |
$node->type = $type;
|
| 137 |
return $locations;
|
| 138 |
}
|
| 139 |
}
|
| 140 |
|