Monday, February 20, 2012

Upload & Process uploaded file drupal

function test_import($form, &$form_state) {
  $form['csv_file'] = array(
      '#title'          => t('Upload a csv file'),
      '#type'         => 'file',
      '#description'  => t('Must be smaller than 5MB in size.'),
     );
  $form[] = array(
      '#type'  => 'submit',
      '#value'  => 'Process',
    );

  return $form;
}

function test_import_validate($form, &$form_state) {
  $validators = array(
     'file_validate_extensions' => array( 'csv' ),
     'file_validate_size' => array(variable_get('btn_file_size', '5') * 1024),
  );
  // the others validators are http://api.drupal.org/api/drupal/includes!file.inc/group/file/7

  $destination = FALSE;
  // $destination = 'public://testimport/';

  $fileObj = file_save_upload('csv_file', $validators, $destination);
  if (!$fileObj) {
    form_set_error('csv_file', t('Oh Unable to access file or file is missing.'));
  }

}

function test_import_submit($form, &$form_state) {

  $fileObj = file_save_upload( 'csv_file');
  if (!$fileObj) {
    form_set_error('csv_file', t('Unable to access file or file is missing.'));
  }

  ini_set('auto_detect_line_endings', true);
  $file = fopen( drupal_realpath($fileObj->uri), 'r');

  while(!feof($file)) {
    $line = fgets($file);
    $data = explode("\t", $line);
    dpm($data[0]);
  }

  fclose($file);

}


related :
http://api.drupal.org/api/drupal/developer%21topics%21forms_api_reference.html/7#managed_file
http://api.drupal.org/api/examples/image_example%21image_example.pages.inc/7

Friday, February 10, 2012

clickable table row on drupal's tableselect

YOUR.inc
=========
function a_form($form, &$formstate){
  drupal_add_js(drupal_get_path('module', 'YOUR-MODULE-NAME') . '/js/selectable-row.js', 'file');
 
  $header = array
  (
    'name' => 'name',
    'surname' => 'surname',
    'action' => 'action',
  );

  $options['1'] = array(
    'name' => 'name1',
    'surname' => 'surname1',
    'action' => '<a href="http://...">link</a>',
  );

  $options['2'] = array(
    'name' => 'name2',
    'surname' => 'surname2',
    'action' => '<a href="http://...">link</a>',
  );

  $form['tableselect_field'] = array(
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $options,
    '#multiple' => FALSE,
    '#ajax' => array(
      'callback' => 'an_example_callback',
      'wrapper' => 'a-div',
      'method' => 'replace',
      'effect' => 'fade',
    ),
    '#attributes' => array('class' => array('selectable-row')),
  );

  $form['ajax_div'] = array(
    '#type' => 'markup',
    '#prefix' => '<div id="a-div">',
    '#markup' => date('y-m-d h:i:s'),
    '#suffix' => '</div>',
  );

  return $form;
}

function an_example_callback($form, $form_state) {
  return $form['ajax_div'];
}


selectable-row.js
===================
(function ($) {

  $(document).ready(function() {

    var selectableRow = $("table.selectable-row tbody").children();

    selectableRow.hover(function () {
      $(this).addClass("highlight");
    }, function () {
      $(this).removeClass("highlight");
    });

    selectableRow.click(function() {
      $(this).siblings().removeClass("selected-row");
      $(this).addClass("selected-row");
      $(this).find(":radio").attr("checked","checked");
      $(this).find(":radio").change();
    });

    // snippet with some modification from
    // http://thephuse.com/design-and-usability/clickable-table-rows-with-jquery/
    selectableRow.find('a').hover( function() {
        $(this).parents('tr').unbind('click');
    }, function() {
        $(this).parents('tr').click( function() {
          $(this).siblings().removeClass("selected-row");
          $(this).addClass("selected-row");
          $(this).find(":radio").attr("checked","checked");
          $(this).find(":radio").change();
        });
    });

  });
})(jQuery)


YOUR.css
=========
tr.highlight {
  background-color: #0000EE;
  color: #FFF;
}

tr.selected-row {
  background-color: #00AAEE;
  color: #FE0;
}

Wednesday, February 8, 2012

Postgresql File System Level Backup Debian

In order to perform postgresql backup on debain, there are some modification on
http://www.postgresql.org/docs/9.1/interactive/backup-file.html


sudo service apache2 stop
sudo service postgresql stop

#%y%m%d_%H-%M
sudo tar -cvpjf /a/path/postgres-`date +%y%m%d`.tar.bz2 -C /var/lib/postgresql/9.1/main .

# Prevent parent directories from being tarred
#   -C, --directory DIR
#           change to directory DIR

# -p, --preserve-permissions, --same-permissions
#           extract information about file permissions (default for superuser)


sudo service postgresql start
sudo service apache2 start

# when extract
#sudo tar -xvjf postgres-xxxxxx.tar.bz2