You are here

function i18nblocks_nodeapi in Internationalization 5

Same name and namespace in other branches
  1. 5.3 i18nblocks/i18nblocks.module \i18nblocks_nodeapi()
  2. 5.2 i18nblocks/i18nblocks.module \i18nblocks_nodeapi()

Implementation of hook_nodeapi

File

i18nblocks/i18nblocks.module, line 123

Code

function i18nblocks_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  if (variable_get("nodeasblock_{$node->type}", 1) && variable_get("i18n_nodeasblock_{$node->type}", 0)) {
    switch ($op) {
      case 'submit':
        if ($node->i18nblock) {

          // Copy values from the form before changing any
          foreach (array(
            'status',
            'region',
            'weight',
            'visibility',
            'pages',
            'custom',
            'info',
          ) as $field) {
            $node->i18nblock['block'][$field] = $node->nodeasblockset[$field];
          }

          // If the block is new, disable the block changing status before it is saved
          if (!$node->nodeasblock) {
            $node->nodeasblockset['status'] = 0;
            $node->nodeasblockset['region'] = BLOCK_REGION_NONE;
          }
        }
        break;
      case 'update':
      case 'insert':

        // Regenerate all translations for this block
        // This will run after i18n, translations and nodeasblock
        if (isset($node->i18nblock) && ($metablock = $node->i18nblock)) {
          $metablock['nids'][$node->nid] = $node->nid;

          // Recreate the translation block. Or delete if no blocks.
          $metablock['i18nblocks'] = array();
          $result = db_query("SELECT * FROM {i18n_node} i INNER JOIN {nodeasblock} n ON i.nid = n.nid WHERE i.nid IN (%s) ", implode(',', $metablock['nids']));
          while ($trn = db_fetch_object($result)) {
            $metablock['i18nblocks'][$trn->language] = "nodeasblock:{$trn->nid}";
          }
          if ($metablock['i18nblocks']) {

            // Set values if new block
            if (!$metablock['info']) {
              $metablock['info'] = t('Translation: !title', array(
                '!title' => $node->title,
              ));
            }
            $metablock = i18nblocks_save($metablock);

            // Only if the block is new, rehash block table
            if (isset($metablock['new'])) {
              _block_rehash();
            }
            if ($block = $metablock['block']) {
              $block['status'] = 1;
              $block['delta'] = $metablock['delta'];
              db_query("UPDATE {blocks} SET status = %d, region = '%s', weight = %d, visibility = %d, pages = '%s', custom = %d WHERE module = 'i18nblocks' AND delta = '%s'", $block['status'], $block['region'], $block['weight'], $block['visibility'], trim($block['pages']), $block['custom'], $block['delta']);
            }
          }
          elseif ($metablock['delta']) {
            i18nblocks_delete($metablock['delta']);
          }
        }
        break;
    }
  }
}