/** * Select interfacename * * URL: http://guifi.net/guifi/js/select-device-interfacename/% */ function guifi_ahah_select_device_interfacename($delete = false) { $cid = 'form_' . $_POST['form_build_id']; $cache = cache_get($cid, 'cache_form'); $ids = explode('-', arg(3)); $device = explode('-', $_POST['ipv4'][$ids[0]]['subnet'][$ids[1]]['did']); guifi_log(GUIFILOG_TRACE, sprintf('guifi_ahah_select_device_interfacename (ids=%s did=%d)', arg(3), $device[0])); $device_interfaces = guifi_get_device_allinterfaces($device[0]); $rIpv4 = $_POST['ipv4'][$ids[0]]['subnet'][$ids[1]]; if ($cache) { $form = $cache->data; if (!$delete) { $form['ipv4'][$ids[0]]['subnet'][$ids[1]]['iid']['#options'] = $device_interfaces; $form['ipv4'][$ids[0]]['subnet'][$ids[1]]['did']['#value'] = guifi_get_devicename($device[0], 'large'); $form['ipv4'][$ids[0]]['subnet'][$ids[1]]['ipv4']['#value'] = $_POST['ipv4'][$ids[0]]['subnet'][$ids[1]]['ipv4']; } else { if ($form['ipv4'][$ids[0]]['subnet'][$ids[1]]['new']['#value']) { unset($form['ipv4'][$ids[0]]['subnet'][$ids[1]]); } else { $form['ipv4'][$ids[0]]['subnet'][$ids[1]]['deleted']['#type'] = 'hidden'; $form['ipv4'][$ids[0]]['subnet'][$ids[1]]['deleted']['#value'] = true; $form['ipv4'][$ids[0]]['subnet'][$ids[1]]['ipv4']['#type'] = 'hidden'; $form['ipv4'][$ids[0]]['subnet'][$ids[1]]['iid']['#type'] = 'hidden'; $form['ipv4'][$ids[0]]['subnet'][$ids[1]]['did']['#type'] = 'hidden'; $form['ipv4'][$ids[0]]['subnet'][$ids[1]]['deletedmsg'] = array('#type' => 'item', '#value' => t('Address %addr will be DELETED', array('%addr' => $_POST['ipv4'][$ids[0]]['subnet'][$ids[1]]['ipv4']))); drupal_set_message(t('Press "Reset" to discard changes.'), 'warning'); } } cache_set($cid, $form, 'cache_form', $cache->expire); // Build and render the new select element, then return it in JSON format. $form_state = array(); $form['#post'] = $_POST; $form = form_builder($form['form_id']['#value'], $form, $form_state); // $output = drupal_render($form['ipv4'][$ids[0]]['subnet'][$ids[1]]['iid']); $output = theme('status_messages') . drupal_render($form['ipv4'][$ids[0]]['subnet'][$ids[1]]); drupal_json(array('status' => TRUE, 'data' => $output)); } else { drupal_json(array('status' => FALSE, 'data' => '')); } exit; }
function guifi_ipv4subnet_form($ipv4, $k, $view = false) { define('MAXNIPS', 12); guifi_log(GUIFILOG_TRACE, sprintf('guifi_ipv4subnet_form (id=%d)', $k), $ipv4); $ipc = _ipcalc($ipv4[ipv4], $ipv4[netmask]); if (!empty($ipv4[subnet])) { $subnet = $ipv4[subnet]; } else { $subnet = $ipv4[snet]; } guifi_log(GUIFILOG_FULL, sprintf('guifi_ipv4subnet_form (id=%d)', $k), $subnet); $ips = array(ip2long($ipv4[ipv4])); foreach ($subnet as $ki => $ip) { if (empty($ip[ipv4]) and !empty($ip[ipv4value])) { $subnet[$ki][ipv4] = $ip[ipv4value]; } if (ip2long($ip[ipv4])) { $ips[] = ip2long($ip[ipv4]); } } sort($ips); $form = array('#type' => !$view ? 'hidden' : 'fieldset', '#type' => 'fieldset', '#title' => t('Subnet') . ' ' . $ipc['netid'] . '/' . $ipc['maskbits'] . ' - ' . count($ips) . ' address(es)', '#prefix' => '<div class="ipv4-subnet" id="fieldset-ipv4subnet-' . $k . '">', '#suffix' => '</div>', '#collapsible' => true, '#collapsed' => $view ? false : true); // foreach ($subnet as $ks => $snet) { $nIps = 0; $newIps = 0; for ($ks = 0; $ks < $ipc['hosts']; $ks++) { $snet = $subnet[$ks]; $form[$ks] = array('#type' => !empty($snet[ipv4]) ? 'fieldset' : 'hidden', '#attributes' => array('class' => 'fieldset-interface-port'), '#prefix' => '<div id="fieldset-ipv4subnet-' . $k . '-' . $ks . '">', '#suffix' => '</div>'); $form[$ks][ipv4value] = array('#type' => 'hidden', '#value' => $snet[ipv4]); if (isset($snet['id'])) { $form[$ks]['id'] = array('#type' => 'hidden', '#value' => $snet['id']); } if (isset($snet[interface_id])) { $form[$ks][interface_id] = array('#type' => 'hidden', '#value' => $snet[interface_id]); } $form[$ks][ipv4] = array('#type' => 'textfield', '#default_value' => $snet[ipv4], '#size' => 24, '#maxlength' => 16); if ($snet['new']) { $form[$ks][ipv4]['#type'] = 'hidden'; $form[$ks][ipv4]['#value'] = $snet[ipv4]; $form[$ks][ipv4txt] = array('#type' => 'textfield', '#value' => $snet[ipv4], '#disabled' => true, '#size' => 24); $newIps++; $form[$ks]['new'] = array('#type' => 'hidden', '#value' => true); } $form[$ks][did] = array('#type' => ($view and !empty($snet[ipv4])) ? 'textfield' : 'hidden', '#type' => 'textfield', '#default_value' => guifi_get_devicename($snet['did'], 'large'), '#autocomplete_path' => 'guifi/js/select-node-device', '#size' => 60, '#maxlength' => 128, '#element_validate' => array('guifi_devicename_validate'), '#ahah' => array('event' => 'blur', 'path' => 'guifi/js/select-device-interfacename/' . $k . '-' . $ks, 'wrapper' => 'fieldset-ipv4subnet-' . $k . '-' . $ks, 'method' => 'replace', 'effect' => 'fade')); // if (!empty($snet['ipv4'])) { $dinterfaces = guifi_get_device_allinterfaces($snet['did']); $form[$ks]['iid'] = array('#type' => 'select', '#value' => $snet['iid'], '#required' => true, '#options' => $dinterfaces); // } if (!($snet['deleted'] == true)) { $form[$ks]['deleted'] = array('#type' => 'image_button', '#src' => drupal_get_path('module', 'guifi') . '/icons/drop.png', '#attributes' => array('title' => t('Delete address')), '#ahah' => array('path' => 'guifi/js/delete-ipv4/' . $k . '-' . $ks, 'wrapper' => 'fieldset-ipv4subnet-' . $k . '-' . $ks, 'method' => 'replace', 'effect' => 'fade')); } else { $form[$ks][deleted] = array('#type' => 'hidden', '#value' => true); $form[$ks][ipv4]['#type'] = 'hidden'; $form[$ks][ipv4]['#value'] = $snet[ipv4]; $form[$ks][did]['#type'] = 'hidden'; $form[$ks][iid]['#type'] = 'hidden'; $form[$ks][deletedmsg] = array('#type' => 'item', '#value' => t('Address %addr will be DELETED', array('%addr' => $snet[ipv4]))); } // force save if new addresses at the in-memory form is too high if (empty($snet['ipv4'])) { $nIps++; } if ($nIps >= MAXNIPS) { break; } } if (count($ips) < $ipc['hosts']) { if ($newIps < MAXNIPS) { $form['add-ipv4'] = array('#type' => 'image_button', '#title' => $first_port ? t('Add') : false, '#src' => drupal_get_path('module', 'guifi') . '/icons/ipv4-new.png', '#attributes' => array('title' => t('Add new ipv4 address to the subnetwork')), '#weight' => 100, '#ahah' => array('path' => 'guifi/js/add-remoteipv4/' . $k, 'wrapper' => 'fieldset-ipv4subnet-' . $k, 'method' => 'replace', 'effect' => 'fade')); } else { $form['add-ipv4'] = array('#type' => 'item', '#title' => $first_port ? t('Add') : false, '#value' => t('To many new addresses added. Is not safe to add more values, please confirm or discard changes'), '#weight' => 100); } } return $form; }