You are here

function cpn_block_submit in Code per Node 7

Same name and namespace in other branches
  1. 6 cpn.module \cpn_block_submit()

Block submit callback.

1 string reference to 'cpn_block_submit'
cpn_form_alter in ./cpn.module
Implements of hook_form_alter().

File

./cpn.module, line 632
Primary hook implementations.

Code

function cpn_block_submit($form, &$form_state) {
  if (isset($form_state['values']['cpn'])) {
    $module = $form_state['values']['module'];
    $delta = $form_state['values']['delta'];

    // "Block" block was just created; get delta from "block_custom" table.
    if (empty($delta) and $module == 'block') {
      $delta = db_query("SELECT bid FROM {block_custom} ORDER BY bid DESC LIMIT 1")
        ->fetchField();
    }

    // Update the existing records & files. However, only update anything if the
    // current user has access to edit the records, to avoid accidentally
    // deleting something because a user didn't have access.
    $fields = array();
    if (user_access('administer code per node') || user_access('edit css per block')) {
      $fields['css'] = $form_state['values']['cpn']['css'];
    }
    if (user_access('administer code per node') || user_access('edit javascript per block')) {
      $fields['js'] = $form_state['values']['cpn']['js'];
      $fields['noscript'] = $form_state['values']['cpn']['noscript'];
    }

    // If an updated record was found then do so.
    if (!empty($fields)) {
      db_update('block')
        ->fields($fields)
        ->condition('module', $module)
        ->condition('delta', $delta)
        ->execute();

      // Save the output.
      foreach (array(
        'css',
        'js',
      ) as $type) {

        // Only update records that the visitor had access to edit.
        if (isset($fields[$type])) {

          // Wrap the strings, if needed.
          $form_state['values']['cpn'][$type] = cpn_wrap_output($form_state['values']['cpn'][$type], 'block', $type);

          // Delete existing file.
          cpn_delete_file($module . '-' . $delta . '.' . $type);

          // Save the output to a file.
          // Add the global wrapper code.
          $output = cpn_wrap_output($form_state['values']['cpn'][$type], 'block', $type);

          // Replace the token strings using any available global tokens.
          $output = token_replace($output);

          // Output the file.
          if (!empty($output)) {
            cpn_save_file($output, $module . '-' . $delta . '.' . $type);
          }
        }
      }
    }
  }
}