You are here

function gallery_assist_insert in Gallery Assist 6

Implementation of hook_insert().

2 calls to gallery_assist_insert()
gallery_assist_form_submit in ./gallery_assist.module
Prepare some input keys for save.
gallery_assist_nodeapi in ./gallery_assist.module
Implementation of hook_nodeapi().

File

./gallery_assist.module, line 2459
Drupal content type with gallery functionality.

Code

function gallery_assist_insert(&$node) {
  $count = db_result(db_query("SELECT count(gid) FROM {gallery_assist} WHERE nid=%d", $node->nid));
  if ($count > 0) {
    return;
  }
  else {
    $my = array();
    $av_translation = FALSE;

    //    if ($node->is_new) {
    //      $info_text = 'You successfully created your gallery !name. Now you can set various options for this gallery, and add images.<br />When you choose images what you want, you need to press Update button for loading them. <br />When you will ne finished and satisfied with number and description of your images, you need to press button Save on bottom of form, to save all this your work.';
    //      $box = array(
    //        '#type' => 'fieldset',
    //        '#title' => t('Info'),
    //        '#weight' => 1000,
    //        '#collapsible' => TRUE
    //      );
    //      $box['info_text'] = array(
    //        '#type' => 'markup',
    //        '#value' => $info_text
    //      );
    //      drupal_set_message(drupal_render($box), 'status');
    //    }
    // Compare and set settings from default.
    $data = variable_get('gallery_assist_' . $node->type . '_data', FALSE);
    $node->show_title = $data['show_title'];
    $settings = array(
      'ga_align' => $data['ga_align'],
      'gap_align' => $data['gap_align'],
      't_ga_float' => $data['t_ga_float'],
      'p_ga_float' => $data['p_ga_float'],
      'show_layout' => $data['show_layout'],
      'gallery_items_shadow' => $data['gallery_items_shadow'],
      'show_download_link' => $data['show_download_link'],
      'ga_db_order_field' => $data['ga_db_order_field'],
      'ga_db_order' => $data['ga_db_order'],
      'ga_item_title_type' => $data['ga_item_title_type'],
      't_thm_link_format' => $data['t_thm_link_format'],
      'ga_public_status' => $data['ga_public_status'],
    );
    $new_data = serialize($settings);
    if (!empty($node->translation_source->nid) || !empty($node->tnid) && $node->tnid > 0) {

      // Get ref by tranlation and check and upgrate the translation table
      // a exist item will be duplicated with the new language entrie
      if (!empty($node->translation_source->nid) || empty($node->tnid)) {
        $orig = $node->translation_source;
      }
      elseif (!empty($node->tnid) || empty($node->translation_source->nid)) {
        $count = db_result(db_query("SELECT count(gid) FROM {gallery_assist} WHERE nid=%d", $node->tnid));
        if ($count == 0) {
          return;
        }
        $orig = node_load($node->tnid);
      }
      $my['lang'] = $_GET['language'] ? $_GET['language'] : $node->language;
      if (empty($my['lang']) || $my['lang'] == '') {
        $my['lang'] = '';
      }
      if ($node->translation_source->gallconf[$node->type]['translation_behavior'] == 1) {
        foreach ($settings as $t_k => $t_v) {
          if ($t_k == 'ga_align' || $t_k == 'gap_align' || $t_k == 't_ga_float' || $t_k == 'p_ga_float') {
            $t_settings[$t_k] = $node->translation_source->gallconf[$node->type][$t_k];
          }
          else {
            $t_settings[$t_k] = $node->translation_source->gallconf[$node->type][$t_k];
          }
        }
        if (isset($node->translation_source->gallery_items_shadow)) {
          $t_settings['gallery_items_shadow'] = $node->translation_source->gallery_items_shadow;
        }
        if (isset($node->translation_source->gallconf[$node->type]['ga_db_order_field'])) {
          $t_settings['ga_db_order_field'] = $node->translation_source->gallconf[$node->type]['ga_db_order_field'];
        }
        if (isset($node->translation_source->gallconf[$node->type]['ga_db_order'])) {
          $t_settings['ga_db_order'] = $node->translation_source->gallconf[$node->type]['ga_db_order'];
        }
        if (isset($node->translation_source->gallconf[$node->type]['hide_in_teaser'])) {
          $t_settings['hide_in_teaser'] = $node->translation_source->gallconf[$node->type]['hide_in_teaser'];
        }
        if (isset($node->translation_source->gallconf[$node->type]['hide_in_teaser'])) {
          $t_settings['translation_behavior'] = $node->translation_source->gallconf[$node->type]['translation_behavior'];
        }
        $new_data = serialize($t_settings);
      }
      if (count($orig->gallitems) > 0) {
        foreach ($orig->gallitems as $pid => $item) {
          $insert_translated_data = array(
            'nid' => $node->nid,
            'gid' => $item->gid,
            'gref' => $item->gref,
            'pid' => $pid,
            'lang' => $my['lang'],
            'ptitle' => $item->ptitle,
            'palt' => $item->ptitle,
            'pdescription' => $item->pdescription,
          );
          drupal_write_record('gallery_assist_translated', $insert_translated_data);
        }
      }
      $my['gref'] = $orig->gref ? $orig->gref : $orig->gid;
      $node->in_profile = $orig->in_profile;
      $node->ga_public_status = $orig->ga_public_status;
      $node->show_title = $orig->show_title;
      $av_translation = TRUE;
      $cq = "DELETE FROM {cache_gallery_assist_data} WHERE cid = '%s'";
      db_query($cq, $node->tnid);
    }

    // Create a new gallery assist as new node or as referenced tranlated node.
    // are normaly nodes
    $my['ref'] = $orig->ref ? $orig->ref : $node->nid;
    if ($node->sepparated == 1) {
      $my['lang'] = $_GET['language'] ? $_GET['language'] : $node->language;
      $my['ganame'] = 'gallery_assist' . $node->nid;
      $my['ref'] = $node->nid;
      $node->in_profile = '';
      $node->ga_public_status = '';
      $node->show_title = '';
    }
    else {
      $my['lang'] = $_GET['language'] ? $_GET['language'] : $node->language;
      $my['ganame'] = $node->ganame ? $node->ganame : 'gallery_assist' . $my['ref'];
    }
    $insert_gallery_data = array(
      'ref' => $my['ref'],
      'gref' => isset($my['gref']) ? $my['gref'] : (int) 0,
      'lang' => $my['lang'],
      'nid' => $node->nid,
      'uid' => $node->uid,
      'ganame' => $my['ganame'],
      'in_profile' => isset($node->in_profile) ? $node->in_profile : $data['in_profile'],
      'ga_public_status' => $data['ga_public_status'],
      'show_title' => isset($node->show_title) ? $node->show_title : $data['show_title'],
      'show_in_homepage_block' => isset($node->show_in_homepage_block) ? $node->show_in_homepage_block : $data['show_in_homepage_block'],
      'gallery_assist_weight' => $data['gallery_assist_weight'] ? $data['gallery_assist_weight'] : $node->gallery_assist_weight,
      'data' => $new_data,
    );
    if ($node->translation_source->gallconf[$node->type]['translation_behavior'] == 1) {
      if (isset($node->translation_source->gallery_assist_weight)) {
        $insert_gallery_data['gallery_assist_weight'] = $node->translation_source->gallery_assist_weight;
      }
      if (isset($node->translation_source->show_in_homepage_block)) {
        $insert_gallery_data['show_in_homepage_block'] = $node->translation_source->show_in_homepage_block;
      }
    }
    drupal_write_record('gallery_assist', $insert_gallery_data);

    // Get the gid for other operations.
    $my['last_gid'] = db_last_insert_id('gallery_assist', 'gid');
    if ($my['ref'] == $node->nid) {

      // If realy new gallery reference self. Important in case the site will be multilingual, will be translated.
      $update_gallery_data = array(
        'gref' => $my['last_gid'],
        'nid' => $node->nid,
      );
      drupal_write_record('gallery_assist', $update_gallery_data, 'nid');
    }
    if ($node->translation_source->gallconf[$node->type]['translation_behavior'] == 1) {

      // Check if exists settings from other submodules.
      if (count($node->translation_source->gallconf[$node->type]['modules']) > 0) {
        foreach ($node->translation_source->gallconf[$node->type]['modules'] as $module => $settings) {
          $insert_gallery_data;
          $settings_record = array(
            'nid' => $node->nid,
            'ref' => $node->translation_source->ref,
            'gid' => $my['last_gid'],
            'gref' => $node->translation_source->gref,
            'module' => $module,
            'data' => $node->translation_source->gallconf[$node->type]['modules'][$module]['data'],
          );
          db_query("insert into {gallery_assist_modules_settings} (nid, ref, gid, gref, module, data)\n               VALUES (%d, %d, %d, %d, '%s', '%s')", $settings_record['nid'], $settings_record['ref'], $settings_record['gid'], $settings_record['gref'], $settings_record['module'], $settings_record['data']);
        }
      }
    }
    if ($av_translation) {
      $cq = "DELETE FROM {cache_gallery_assist_data} WHERE cid = '%s'";
      db_query($cq, $node->nid);
    }
    unset($my);
  }
}