You are here

vkxp.admin.inc in VK CrossPoster 7

Contains vkxp settings forms.

File

vkxp.admin.inc
View source
<?php

/**
 * @file
 * Contains vkxp settings forms.
 */

/**
 * Page callback.
 * Return form with VKXP main settings.
 */
function vkxp_admin_main_settings($form, &$form_state) {

  // Application settings.
  $form['vkxp_application'] = array(
    '#type' => 'fieldset',
    '#title' => t('Application settings'),
    '#collapsible' => TRUE,
    '#description' => t('Note: You have to !create standalone vkontakte application to make this module work.', array(
      '!create' => l(t('create'), 'http://vk.com/editapp?act=create', array(
        'attributes' => array(
          'target' => '_blank',
        ),
      )),
    )),
  );
  $form['vkxp_application']['vkxp_app_id'] = array(
    '#type' => 'textfield',
    '#title' => t('Application ID'),
    '#required' => TRUE,
    '#default_value' => variable_get('vkxp_app_id', ''),
    '#description' => t('ID of vk application.'),
  );
  $form['vkxp_application']['vkxp_app_secret'] = array(
    '#type' => 'textfield',
    '#title' => t('Application secret code'),
    '#required' => TRUE,
    '#default_value' => variable_get('vkxp_app_secret', ''),
    '#description' => t('Secret code of vk application.'),
  );

  // Owner settings.
  $form['vkxp_owner'] = array(
    '#type' => 'fieldset',
    '#title' => t('Owner settings'),
    '#description' => t('Set user or group to which data will be transferred.'),
    '#collapsible' => TRUE,
  );
  $form['vkxp_owner']['vkxp_group_id'] = array(
    '#type' => 'textfield',
    '#title' => t('Owner ID'),
    '#required' => TRUE,
    '#default_value' => variable_get('vkxp_group_id', ''),
    '#description' => t('User ID or Group ID.'),
  );
  $form['vkxp_owner']['vkxp_wall_owner'] = array(
    '#type' => 'select',
    '#title' => t('Select owner type'),
    '#options' => array(
      'group' => t('Group'),
      'user' => t('User'),
    ),
    '#default_value' => variable_get('vkxp_wall_owner', 'group'),
    '#description' => t('Who owns the above ID.'),
  );

  // Additional settings.
  $form['vkxp_additional'] = array(
    '#type' => 'fieldset',
    '#title' => t('Additional settings'),
    '#collapsible' => TRUE,
  );
  $form['vkxp_additional']['vkxp_enabled_default'] = array(
    '#type' => 'checkbox',
    '#title' => t('Checkbox "Post this node to vk.com" are checked by default'),
    '#default_value' => variable_get('vkxp_enabled_default', 0),
    '#description' => t('Check this if you want checkbox "Post this node to vk.com" in node forms was checked by default.'),
  );
  $form['vkxp_additional']['vkxp_official'] = array(
    '#type' => 'checkbox',
    '#title' => t('Write from group name'),
    '#default_value' => variable_get('vkxp_official', 1),
    '#description' => t('Check this if you want to post messages from group name.'),
  );
  $form['vkxp_additional']['vkxp_add_link'] = array(
    '#type' => 'checkbox',
    '#title' => t('Add link on wall to posted page'),
    '#default_value' => variable_get('vkxp_add_link', 0),
    '#description' => t('Check this if you want to post node url on vk wall.'),
  );

  // Submitters.
  $form['actions'] = array(
    '#type' => 'action',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save settings'),
  );
  $form['actions']['reset'] = array(
    '#type' => 'submit',
    '#value' => t('Recieve new access token'),
  );
  return $form;
}

/**
 * Submit callback for vkxp main settings form.
 */
function vkxp_admin_main_settings_submit($form, &$form_state) {
  $values =& $form_state['values'];

  // Trim values just in case.
  $values['vkxp_app_id'] = trim($values['vkxp_app_id']);
  $values['vkxp_app_secret'] = trim($values['vkxp_app_secret']);

  // If application ID or application secret key was changed we should get new access token.
  $app_id_changed = variable_get('vkxp_app_id', 0) != $values['vkxp_app_id'];
  $secret_changed = variable_get('vkxp_app_secret', 0) != $values['vkxp_app_secret'];
  $reset_clicked = $values['op'] == $values['reset'];

  // Save form state values.
  system_settings_form_submit($form, $form_state);
  if ($app_id_changed || $secret_changed || $reset_clicked) {

    // Build params for http://vk.com to get new access token.
    $params = array();
    $params['client_id'] = $form_state['values']['vkxp_app_id'];
    $params['scope'] = VKXP_AUTHORIZE_SCOPE;
    $params['redirect_uri'] = url(VKXP_AUTHORIZE_REDIRECT_URI, array(
      'absolute' => TRUE,
    ));
    $params['response_type'] = VKXP_AUTHORIZE_RESPONSE_TYPE;
    $params['display'] = VKXP_AUTHORIZE_DISPLAY;

    // Redirect user to authorize page.
    drupal_goto(VKXP_AUTHORIZE_URI . '?' . drupal_http_build_query($params));
  }
}

/**
 * Page callback.
 * Return form with node settings.
 */
function vkxp_admin_node_settings() {
  $form = array();
  $form['node_types'] = array(
    '#type' => 'fieldset',
    '#title' => t('Node types'),
  );
  $form['node_types']['vkxp_node_types'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Node types to crosspost'),
    '#options' => node_type_get_names(),
    '#default_value' => variable_get('vkxp_node_types', array()),
  );
  $form['vkxp_body'] = array(
    '#type' => 'fieldset',
    '#title' => t('Messages'),
  );
  $form['vkxp_body']['vkxp_post_object'] = array(
    '#type' => 'select',
    '#title' => t('Message body'),
    '#description' => t('Select what to post as wall message'),
    '#options' => array(
      'title' => t('Node title'),
      'body' => t('Node body'),
      'title_body' => t('Node title and node body'),
    ),
    '#default_value' => variable_get('vkxp_post_object', 'body'),
  );
  $form['vkxp_body']['vkxp_cut_message'] = array(
    '#type' => 'checkbox',
    '#title' => t('Cut message'),
    '#default_value' => variable_get('vkxp_cut_message', 1),
  );
  $form['vkxp_body']['vkxp_cut_message_length'] = array(
    '#type' => 'textfield',
    '#title' => t('Cut if message length more than'),
    '#field_suffix' => t('symbols'),
    '#size' => 4,
    '#default_value' => variable_get('vkxp_cut_message_length', 255),
  );
  return system_settings_form($form);
}

/**
 * Return form width vkxp image settings.
 */
function vkxp_admin_images_settings() {
  $form = array();

  // Get node types that could be crossposted.
  $enabled_node_types = variable_get('vkxp_node_types', array());
  $selected_types = array_filter($enabled_node_types);
  if (!$selected_types) {
    $form['image'] = array(
      '#markup' => t('Please, select content types to see their image fiels'),
    );
    return $form;
  }
  if (module_exists('image')) {
    $image_fields = array();
    $fields_info = field_info_fields();
    foreach ($selected_types as $node_type) {

      // Build fieldset for every selected node type.
      $node_types = node_type_get_names();
      $form['vkxp_node_type_' . $node_type] = array(
        '#type' => 'fieldset',
        '#title' => $node_types[$node_type],
      );
      foreach ($fields_info as $field) {
        if ($field['type'] == 'image' && isset($field['bundles']['node'])) {
          $bundles = $field['bundles']['node'];
          if (in_array($node_type, $bundles)) {
            $image_fields[$node_type][$field['field_name']] = $field['field_name'];
          }
        }
      }

      // If imagefield for this node type found - build settings form for it.
      if (isset($image_fields[$node_type])) {
        $form['vkxp_node_type_' . $node_type]['vkxp_' . $node_type . '_image_amount'] = array(
          '#type' => 'select',
          '#title' => t('Select amount of images'),
          '#options' => drupal_map_assoc(array(
            0,
            1,
            2,
            3,
            4,
            5,
            6,
            7,
            8,
            9,
            10,
          )),
          '#default_value' => variable_get('vkxp_' . $node_type . '_image_amount', 0),
          '#description' => t('Amount of images which will be posted to vk.com'),
        );
        $form['vkxp_node_type_' . $node_type]['vkxp_' . $node_type . '_image_field'] = array(
          '#type' => 'select',
          '#title' => t('Select imagefield'),
          '#options' => $image_fields[$node_type],
          '#default_value' => variable_get('vkxp_' . $node_type . '_image_field', ''),
          '#description' => t('Selected field will be posted with message to vk.com'),
        );
      }
      else {
        $form['vkxp_node_type_' . $node_type]['empty_value'] = array(
          '#markup' => t('This node types does not contain image fields'),
        );
      }
    }
  }
  else {
    $form['image'] = array(
      '#markup' => t("You can't post images until IMAGE module is installed."),
    );
    return $form;
  }
  return system_settings_form($form);
}

Functions

Namesort descending Description
vkxp_admin_images_settings Return form width vkxp image settings.
vkxp_admin_main_settings Page callback. Return form with VKXP main settings.
vkxp_admin_main_settings_submit Submit callback for vkxp main settings form.
vkxp_admin_node_settings Page callback. Return form with node settings.