Ejemplo n.º 1
0
function guifi_device_form($form_state, $params = array())
{
    global $user;
    $form = array();
    //$form['#suffix'] = '<script type="text/javascript">'
    //               . 'jQuery(\'input#edit-funders-0-user\').focus();'
    //               . '</script>';
    guifi_log(GUIFILOG_TRACE, 'function guifi_device_form()', $params);
    // Local javascript validations not actve because of bug in Firefox
    // Errors are not displayed when fieldset folder is collapsed
    // guifi_validate_js("#guifi-device-form");
    // $form['#attributes'] = array('onsubmit' => 'kk');
    if (empty($form_state['values'])) {
        $form_state['values'] = $params;
    }
    $form_state['#redirect'] = FALSE;
    // if new device, initializing variables
    if ($form_state['values']['nid'] == NULL && $params['add'] != NULL) {
        $form_state['values']['nid'] = $params['add'];
        $form_state['values']['new'] = TRUE;
        $form_state['values']['type'] = $params['type'];
        $form_state['values']['links'] = array();
        $form_state['values']['netmask'] = '255.255.255.224';
        if ($form_state['values']['type'] == 'radio') {
            $form_state['values']['variable']['firmware_id'] = '23';
            $form_state['values']['variable']['model_id'] = '70';
        }
    }
    drupal_set_breadcrumb(guifi_node_ariadna($form_state['values']['nid']));
    // Check permissions
    if ($params['edit']) {
        if (!guifi_device_access('update', $params['edit'])) {
            drupal_set_message(t('You are not authorized to edit this device', 'error'));
            return;
        }
    }
    // Loading node & zone where the device belongs to (some information will be used)
    $node = node_load(array('nid' => $form_state['values']['nid']));
    $zone = node_load($node->zone_id);
    // Setting the breadcrumb
    drupal_set_breadcrumb(guifi_node_ariadna($form_state['values']['nid']));
    // if contact is NULL, then get it from the node or the user logged in drupal
    if (is_null($form_state['values']['notification'])) {
        if (guifi_notification_validate($node->notification)) {
            $form_state['values']['notification'] = $node->notification;
        } else {
            drupal_set_message(t('The node has not a valid email address as a contact. Using your email as a default. Change the contact mail address if necessary.'));
            $form_state['values']['notification'] = $user->mail;
        }
    }
    // if nick is NULL, get a default name
    if ($form_state['values']['nick'] == "") {
        $form_state['values']['nick'] = guifi_device_get_default_nick($node, $form_state['values']['type'], $form_state['values']['nid']);
    }
    if (isset($form_state['action'])) {
        guifi_log(GUIFILOG_TRACE, 'action', $form_state['action']);
        if (function_exists($form_state['action'])) {
            if (!call_user_func_array($form_state['action'], array(&$form, &$form_state))) {
                return $form;
            }
        }
    }
    $form_weight = 0;
    if ($form_state['values']['id']) {
        $form['id'] = array('#type' => 'hidden', '#name' => 'id', '#value' => $form_state['values']['id'], '#weight' => $form_weight++);
    } else {
        $form['new'] = array('#type' => 'hidden', '#name' => 'new', '#weight' => $form_weight++, '#value' => TRUE);
    }
    $form['type'] = array('#type' => 'hidden', '#name' => 'type', '#weight' => $form_weight++, '#value' => $form_state['values']['type']);
    if ($params['add'] != NULL) {
        drupal_set_title(t('adding a new %device at %node', array('%node' => $node->nick, '%device' => $form_state['values']['type'])));
    } else {
        drupal_set_title(t('edit device %dname', array('%dname' => $form_state['values']['nick'])));
    }
    // All preprocess is complete, now going to create the form
    $form['main'] = array('#type' => 'fieldset', '#title' => t('Device name, status and main settings') . ' (' . $form_state['values']['nick'] . ') - ' . $form_state['values']['flag'], '#weight' => $form_weight++, '#collapsible' => TRUE, '#attributes' => array('class' => 'fieldset-device-main'), '#collapsed' => is_null($params['edit']));
    $form['main']['movenode'] = array('#type' => 'textfield', '#title' => t('Node'), '#maxlength' => 60, '#weight' => $form_weight++, '#default_value' => $form_state['values']['nid'] . '-' . guifi_get_zone_nick(guifi_get_zone_of_node($form_state['values']['nid'])) . ', ' . guifi_get_nodename($form_state['values']['nid']), '#autocomplete_path' => 'guifi/js/select-node', '#element_validate' => array('guifi_nodename_validate'), '#description' => t('Select the node where the device is.<br />' . 'You can find the node by introducing part of the node id number, ' . 'zone name or node name. A list with all matching values ' . 'with a maximum of 50 values will be created.<br />' . 'You can refine the text to find your choice.'), '#prefix' => '<div class"form-newline">', '#suffix' => '</div>');
    $form['main']['nid'] = array('#type' => 'hidden', '#weight' => $form_weight++, '#value' => $form_state['values']['nid']);
    $form['main']['nick'] = array('#type' => 'textfield', '#size' => 20, '#maxlength' => 128, '#title' => t('nick'), '#required' => TRUE, '#attributes' => array('class' => 'required'), '#default_value' => $form_state['values']['nick'], '#weight' => $form_weight++, '#description' => t('The name of the device.<br />Used as a hostname, SSID, etc...'));
    $form['main']['flag'] = array('#type' => 'select', '#title' => t("Status"), '#required' => TRUE, '#default_value' => $form_state['values']['flag'], '#options' => guifi_types('status'), '#weight' => $form_weight++, '#description' => t("Current status of this device."));
    $form['main']['notification'] = array('#type' => 'textfield', '#size' => 60, '#maxlength' => 1024, '#title' => t('contact'), '#required' => TRUE, '#element_validate' => array('guifi_emails_validate'), '#default_value' => $form_state['values']['notification'], '#weight' => $form_weight++, '#description' => t('Mailid where changes on the device will be notified, ' . 'if many, separated by \',\'<br />' . 'used for network administration.'));
    if (!empty($form_state['values']['ipv4'])) {
        $form['main']['mainipv4'] = array('#type' => 'select', '#title' => t('Main IPv4'), '#options' => guifi_get_currentDeviceIpv4s($form_state['values']), '#default_value' => $form_state['values']['mainipv4'], '#weight' => $form_weight++, '#description' => t('Used for monitoring.<br>Save and continue to refresh available addresses'));
    }
    $form['main']['logserver'] = array('#type' => 'textfield', '#size' => 60, '#maxlength' => 60, '#title' => t('Log Server'), '#default_value' => $form_state['values']['logserver'], '#weight' => $form_weight++, '#description' => t('If you have a log server for mikrotik (dude), add your ip.'));
    $form['main']['graph_server'] = array('#type' => 'select', '#title' => t("Server which collects traffic and availability data"), '#required' => FALSE, '#default_value' => $node->graph_server ? $node->graph_server : 0, '#options' => array('0' => t('Default'), '-1' => t('None')) + guifi_services_select('SNPgraphs'), '#weight' => $form_weight++, '#description' => t("If not specified, inherits zone properties."));
    if (!user_access('administer guifi zones') || $device['user_created'] == $user->uid and $form_state['values']['type'] == 'radio') {
        $form['main']['graph_server']['#disabled'] = true;
        $form['main']['graph_server']['#description'] .= '<br>' . t('To change the value, you are required for maintainer privilege.');
    }
    /*
     * maintainers fieldset
     */
    $form['maintainers'] = guifi_maintainers_form(array2object($form_state['values']), $form_weight);
    guifi_log(GUIFILOG_TRACE, 'function guifi_device_form(maintainers)', $form_state['values']['maintainers']);
    /*
     * funders fieldset
     */
    $form['funders'] = guifi_funders_form(array2object($form_state['values']), $form_weight);
    guifi_log(GUIFILOG_TRACE, 'function guifi_device_form(funders)', $form_state['values']['funders']);
    guifi_log(GUIFILOG_TRACE, sprintf('function guifi_device_form(abans type)'), $form_weight);
    // create the device-type depenedent form
    // looking for a "guifi_"<device_type>"_form()" function
    if (function_exists('guifi_' . $form_state['values']['type'] . '_form')) {
        $form = array_merge($form, call_user_func_array('guifi_' . $form_state['values']['type'] . '_form', array($form_state['values'], &$form_weight)));
    }
    // Cable interfaces/networking
    guifi_log(GUIFILOG_TRACE, sprintf('function guifi_device_form(abans if cable links)'), $form_weight);
    $form['if'] = guifi_interfaces_cable_form($form_state['values'], $form_weight);
    $form['if']['#weight'] = $form_weight++;
    // Cable interfaces/ports
    guifi_log(GUIFILOG_TRACE, sprintf('function guifi_device_form(abans if ports)'), $form_weight);
    if (isset($form_state['values']['interfaces'])) {
        foreach ($form_state['values']['interfaces'] as $k => $v) {
            unset($form_state['values']['interfaces'][$k]['ipv4']);
        }
        guifi_log(GUIFILOG_TRACE, sprintf('function guifi_device_form(abans if ports)'), $form_state['values']['interfaces']);
        $form['interfaces'] = guifi_ports_form($form_state['values'], $form_weight);
        $form['interfaces']['#weight'] = $form_weight++;
    }
    // VLANs (VLans, VRRPs, WDS...)
    guifi_log(GUIFILOG_TRACE, sprintf('function guifi_device_form(abans if vLANs)'), $form_weight);
    $form['vlans'] = guifi_vinterfaces_form('vlans', $form_state['values'], $form_weight);
    $form['vlans']['#weight'] = $form_weight++;
    // Aggregations (Bridges & Bondings)
    guifi_log(GUIFILOG_TRACE, sprintf('function guifi_device_form(abans if Aggregations)'), $form_weight);
    $form['aggregations'] = guifi_vinterfaces_form('aggregations', $form_state['values'], $form_weight);
    $form['aggregations']['#weight'] = $form_weight++;
    // ipv4
    guifi_log(GUIFILOG_TRACE, sprintf('function guifi_device_form(abans if ipv4)'), $form_weight);
    $form['ipv4'] = guifi_ipv4s_form($form_state['values'], $form_weight);
    $form['ipv4']['#weight'] = $form_weight++;
    guifi_log(GUIFILOG_TRACE, sprintf('function guifi_device_form(abans comments)'), $form_weight);
    // Comments
    $form['comment'] = array('#type' => 'textarea', '#title' => t('Comments'), '#default_value' => $form_state['values']['comment'], '#description' => t('This text will be displayed as an information of the device.'), '#cols' => 60, '#rows' => 5, '#weight' => $form_weight++);
    guifi_log(GUIFILOG_TRACE, sprintf('function guifi_device_form(abans buttons)'), $form_weight);
    //  save/validate/reset buttons
    $form['dbuttons'] = guifi_device_buttons(FALSE, '', 0, $form_weight);
    $form['dbuttons']['#weight'] = $form_weight++;
    guifi_log(GUIFILOG_TRACE, sprintf('function guifi_device_form(form_final)'), $form);
    return $form;
}
Ejemplo n.º 2
0
function guifi_user_form($form_state, $params = array())
{
    _user_password_dynamic_validation();
    guifi_log(GUIFILOG_TRACE, 'function guifi_user_form()', $form_state);
    guifi_validate_js("#guifi-user-form");
    if (empty($form_state['values'])) {
        if (is_numeric($params)) {
            $form_state['values'] = guifi_user_load($params);
        } else {
            $form_state['values'] = $params;
        }
    }
    if (isset($form_state['values']['id'])) {
        $f['id'] = array('#type' => 'hidden', '#value' => $form_state['values']['id']);
        drupal_set_title(t('edit user') . ' ' . $form_state['values']['username']);
    } else {
        $f['new'] = array('#type' => 'hidden', '#value' => TRUE);
        drupal_set_title(t('add user') . ' @ ' . guifi_get_nodename($form_state['values']['nid']));
    }
    $f['firstname'] = array('#type' => 'textfield', '#size' => 60, '#maxlength' => 128, '#title' => t('Firstname'), '#required' => TRUE, '#attributes' => array('class' => 'required'), '#default_value' => $form_state['values']['firstname'], '#description' => t('The real user name (Firstname), ' . 'will be used while building the username.<br />' . 'If username results duplicated, add more words ' . '(i.e. middle initial).<br />' . 'Please enter real data, if fake information is entered, ' . 'administrators might <strong>remove</strong> this user.'));
    $f['lastname'] = array('#type' => 'textfield', '#size' => 60, '#maxlength' => 128, '#title' => t('Lastname'), '#required' => TRUE, '#attributes' => array('class' => 'required'), '#default_value' => $form_state['values']['lastname'], '#description' => t('The real user name (Lastname).'));
    if (!empty($form_state['values']['username'])) {
        $f['username'] = array('#type' => 'item', '#value' => $form_state['values']['username'], '#description' => t('The resulting username.'));
    }
    if (user_access('administer guifi users')) {
        $f['status'] = array('#type' => 'select', '#title' => t('Status'), '#options' => guifi_types('user_status'), '#default_value' => $form_state['values']['status']);
        $f['node'] = array('#type' => 'textfield', '#title' => t('Node'), '#maxlength' => 60, '#default_value' => $form_state['values']['nid'] . '-' . guifi_get_zone_nick(guifi_get_zone_of_node($form_state['values']['nid'])) . ', ' . guifi_get_nodename($form_state['values']['nid']), '#autocomplete_path' => 'guifi/js/select-node', '#element_validate' => array('guifi_nodename_validate'), '#description' => t('Select the node where the user is.<br />' . 'You can find the node by introducing part of the node id number, ' . 'zone name or node name. A list with all matching values ' . 'with a maximum of 50 values will be created.<br />' . 'You can refine the text to find your choice.'));
    } else {
        $f['status'] = array('#type' => 'item', '#title' => t('Status'), '#value' => $form_state['values']['status']);
        $f['node'] = array('#type' => 'item', '#title' => t('Node'), '#value' => $form_state['values']['nid'] . '-' . guifi_get_zone_nick(guifi_get_zone_of_node($form_state['values']['nid'])) . ', ' . guifi_get_nodename($form_state['values']['nid']));
        if (!isset($f['new'])) {
            $f['previous_pwd'] = array('#type' => 'password', '#title' => t('Current password'), '#description' => t('To proceed for any change, you have to ' . 'know the current password.'));
        }
        if (!isset($f['new'])) {
            $f['resetPwd'] = array('#type' => 'submit', '#value' => t('Reset password'));
        }
    }
    $f['nid'] = array('#type' => 'hidden', '#value' => $form_state['values']['nid']);
    $f['pass'] = array('#type' => 'password_confirm', '#required' => isset($f['new']), '#title' => t('Set a new password'), '#description' => t('To change/set the current user password, enter the new password in both fields.'), '#size' => 25);
    $f['notification'] = array('#type' => 'textfield', '#size' => 60, '#maxlength' => 1024, '#title' => t('contact'), '#required' => TRUE, '#element_validate' => array('guifi_emails_validate'), '#default_value' => $form_state['values']['notification'], '#description' => t('Mailid where changes on this user will be notified, ' . 'if many, separated by \',\'<br />' . 'Also where the user can be contacted.'));
    // services
    $f['services'] = array('#type' => 'fieldset', '#title' => t('services'), '#collapsible' => TRUE, '#collapsed' => FALSE, '#tree' => TRUE);
    if (user_access('administer guifi users')) {
        $f['services']['proxystr'] = array('#type' => 'textfield', '#title' => t('proxy'), '#maxlength' => 60, '#default_value' => guifi_service_str($form_state['values']['services']['proxy']), '#autocomplete_path' => 'guifi/js/select-service/proxy', '#element_validate' => array('guifi_service_name_validate', 'guifi_user_proxy_validate'));
    } else {
        $f['services']['proxystr'] = array('#type' => 'item', '#title' => t('proxy'), '#value' => guifi_service_str($form_state['values']['services']['proxy']));
    }
    $f['services']['proxy'] = array('#type' => 'hidden', '#value' => $form_state['values']['services']['proxy']);
    $f['services']['filters'] = array('#type' => 'checkboxes', '#parents' => array('content_filters'), '#title' => t('content filters'), '#options' => guifi_types('filter'), '#multiple' => TRUE, '#description' => t('Content to be filtered.<br />Check the type of content ' . 'which will be filtered to this user. ' . 'Note that this filters will work only on those sites ' . 'which have enabled this feature, ' . 'so don\'t think that is safe to rely on this.'));
    if (!empty($form_state['values']['content_filters'])) {
        $f['services']['filters']['#default_value'] = $form_state['values']['content_filters'];
    }
    $f['author'] = array('#type' => 'fieldset', '#access' => user_access('administer guifi users'), '#title' => t('Authoring information'), '#collapsible' => TRUE, '#collapsed' => TRUE);
    $f['author']['username_created'] = array('#type' => 'textfield', '#title' => t('Authored by'), '#maxlength' => 60, '#autocomplete_path' => 'user/autocomplete', '#default_value' => $form_state['values']['username_created'], '#description' => t('Leave blank for %anonymous.', array('%anonymous' => variable_get('anonymous', t('Anonymous')))));
    $f['submit'] = array('#type' => 'submit', '#value' => t('Save'));
    if (!isset($f['new'])) {
        $f['delete'] = array('#type' => 'submit', '#value' => t('Delete'));
    }
    return $f;
}