You are here

function vkxp_nodeapi in VK CrossPoster 6

Same name and namespace in other branches
  1. 6.3 vkxp.module \vkxp_nodeapi()
  2. 6.2 vkxp.module \vkxp_nodeapi()

Implementation of hook_nodeapi

File

./vkxp.module, line 106

Code

function vkxp_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  if ($op == 'insert' || $op == 'update') {

    // If access token is recieved, if crossposting enabled, if user has access to crosspost nodes, if user checked this node to post:
    if (variable_get('vkxp_access_token', '') && variable_get('vkxp_enable', 0) && user_access('post to vkontakte') && $node->vkxp_post_this_node) {
      $selected_types = _vkxp_get_selected_node_types();

      // If current node related to selected types list - crosspost it
      if (in_array($node->type, $selected_types)) {

        // Clear cached data if user wants to add link to this page
        // If cache will not be cleared, vkontakte may not post this node due to unaccessable node url
        if (variable_get('vkxp_add_link', 0) && CACHE_DISABLED !== variable_get('cache', CACHE_DISABLED)) {
          cache_clear_all(NULL, 'cache_page');
        }

        // Get node url
        $url = url('node/' . $node->nid, array(
          'absolute' => TRUE,
        ));

        // Get message from node
        if (variable_get('vkxp_post_object', 'body') == 'body') {
          $message = trim(strip_tags($node->body));
        }
        elseif (variable_get('vkxp_post_object', 'body') == 'title') {
          $message = trim(check_plain($node->title));
        }
        else {
          $message = trim(check_plain($node->title)) . "\n\n" . trim(strip_tags($node->body));
        }

        // Trim message if needed
        if (variable_get('vkxp_cut_body', 1)) {
          $length = variable_get('vkxp_cut_body_length', 255);
          if (drupal_strlen($message) > $length) {
            $message = drupal_substr($message, 0, $length - 3) . '...';
          }
        }

        // Decode special symbols
        $message = htmlspecialchars_decode(html_entity_decode($message, ENT_QUOTES, 'UTF-8'));

        // Post node to vk
        $upload_url = _vkxp_get_upload_server();
        if ($upload_url) {
          $images = _vkxp_get_node_images($node);
          $image_ids = _vkxp_upload_images($upload_url, $images);
          _vkxp_post_to_wall($message, $image_ids, $url, $node);
        }
      }
    }
  }
}