You are here

function _fb_post_node_settings_form in Drupal for Facebook 7.4

Helper for the node submission form and the node type form.

3 calls to _fb_post_node_settings_form()
fb_post_entity_publish_form in ./fb_post.module
fb_post_form_node_form_alter in ./fb_post.module
Implements hook_form_BASE_FORM_ID_alter(). Implements hook_form_node_form_alter().
fb_post_form_node_type_form_alter in ./fb_post.module
Implements hook_form_FORM_ID_alter().

File

./fb_post.module, line 426

Code

function _fb_post_node_settings_form($form, &$form_state, $node, $token) {
  if ($token == fb_get_admin_token(FB_POST_VAR_ADMIN_TOKEN)) {

    // Site-wide defaults.
    $defaults = variable_get('fb_post_settings_' . $node->type . '_array', array(
      'status' => FALSE,
      // Don't post by default.
      'message' => '',
      'feed_id' => NULL,
      'account_id' => NULL,
    ));
    $site_wide = TRUE;
  }
  else {

    // @TODO - user-specific defaults
    $defaults = array(
      'status' => FALSE,
      'message' => '',
      'feed_id' => NULL,
      'account_id' => NULL,
    );
    $site_wide = FALSE;
  }
  try {
    $graph['me'] = fb_graph('me', $token);
    try {

      // me/accounts fails when using a page token.
      // @todo figure out some other way whether token is a page token or not.
      $graph['me/accounts'] = @fb_graph('me/accounts', $token);
    } catch (Exception $e) {
      $graph['me/accounts'] = array();
    }
    $me = $graph['me'];
    $accounts = $graph['me/accounts'];

    // TODO: check permission to post

    /*
      $perms = fb_fql("SELECT publish_actions,manage_pages FROM permissions WHERE uid=$me[id]");
    dpm($perms[0], __FUNCTION__);
    $form['auth'] = array(
      '#type' => 'markup',
      '#markup' => t('TODO'),
    );
    */

    // Build a list of pages the user can post to.
    // Keep track of tokens associated with each page.
    $page_options = array(
      $me['id'] => $me['name'],
    );
    $page_tokens = array(
      $me['id'] => $token,
    );

    /* The accounts data include pages and applications the user can administer.  Applications no longer have an about page, so we don't want to include them in the options. They don't seem to have 'perms' associated with them, so we test for that. */
    if (!empty($accounts)) {
      foreach ($accounts['data'] as $account) {
        if (!empty($account['access_token']) && !empty($account['perms'])) {

          // @TODO: explicitly check for required perms (CREATE_CONTENT? ADMINISTER?)
          $page_options[$account['id']] = $account['name'];
          $page_tokens[$account['id']] = $account['access_token'];
        }
      }
    }

    // TODO defaults
    $form['#tree'] = TRUE;
    $form['status'] = array(
      '#type' => 'checkbox',
      '#title' => t('Post a link to this %type on Facebook.', array(
        '%type' => $node->type,
      )),
      //'#description' => 'Uncheck to not post.',
      '#default_value' => $defaults['status'],
    );

    // Don't publish again, unless explicitly asked.
    if (isset($node->fb_post_graph) && count($node->fb_post_graph)) {
      $form['status']['#title'] = t('Post another link to this %type on Facebook.', array(
        '%type' => $node->type,
      ));
      $form['status']['#default_value'] = FALSE;
      $form['status']['#description'] = t('Already posted %count time(s).', array(
        '%count' => count($node->fb_post_graph),
      ));

      // Debugging...

      /*
        foreach ($node->fb_post_graph as $row) {
        $form['debug'][$row->graph_id] = array(
          '#markup' => l($row->graph_id, url("https://graph.facebook.com/{$row->graph_id}", array(
                                               'query' => array(
                                                 'access_token' => $token,
                                               ),
                                             ))),
        );
      }
      */

      // end debug
    }

    // TODO: does this still work?
    $form['message'] = array(
      '#type' => 'textfield',
      '#title' => 'Message',
      '#default_value' => $defaults['message'],
      '#description' => 'A brief message to precede the link.',
    );

    // Include access tokens as form values (rather than in form_state) so they get passed all the way through to node hooks.

    /*
    $form['access_token'] = array(
      '#type' => 'value',
      '#value' => $token,
      //'#element_validate' => array('fb_post_settings_token_validate'),
    );
    */
    $form['page_tokens'] = array(
      '#type' => 'value',
      '#value' => $page_tokens,
    );
    $form['feed_id'] = array(
      '#type' => 'select',
      '#title' => 'Publish to',
      '#options' => $page_options,
      '#default_value' => $defaults['feed_id'],
      '#description' => 'You may select a user\'s wall, or a managed page.',
    );

    // @TODO: this feature is not working.  Does facebook even support it anymore???

    /*
    $form['account_id'] = array(
      '#type' => 'checkbox',
      '#title' => t('Authored by %name', array('%name' => $me['name'])),
      '#description' => t('Posts to a <em>page</em> are attributed to the page.  Check this to attribute your facebook account instead.'),
      '#return_value' => $me['id'],
      '#default_value' => $defaults['account_id'],
    );
    */
    $form['data'] = array(
      '#tree' => TRUE,
    );

    // Placeholder
    $form['#element_validate'] = array(
      'fb_post_node_settings_validate',
    );
  } catch (Exception $e) {

    //dpm($e, __FUNCTION__);
    if ($site_wide) {
      fb_log_exception($e, 'Post to Facebook settings not available.');
    }
    if (!empty($e->fb_code) && $e->fb_code == 190) {
      if ($site_wide) {

        // TODO: check user access
        drupal_set_message(t('The post to facebook token is no longer valid and should be <a href=!replace_url>replaced</a>.', array(
          '!replace_url' => url(FB_PATH_ADMIN_CONFIG . '/token_replace/' . FB_POST_VAR_ADMIN_TOKEN),
        )));
        $form['error'] = array(
          '#markup' => t('Unable to connect with facebook, possibly authorization has expired.  <a href=!url target=_blank>Reset your authorization</a>, then refresh this form to see all options.', array(
            '!url' => url(FB_POST_PATH_ADMIN),
          )),
        );
      }
      else {
        $form['error'] = array(
          '#markup' => t('Unable to publish to your personal timeline.  You are not currently connected to Facebook.', array()),
        );
      }
    }
    $form_state['fb_post']['settings_invalid'] = TRUE;
  }
  return $form;
}