<?php

define('TDEMO_DELIMETER', "\3");

function tdemo_menu() {
  $items['tdemo'] = array(
    'title' => 'Text demo',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('tdemo_form_text'),
    'access arguments' => array('access content'),
  );
  return $items;
}

function tdemo_form_text($form, $form_state) {
  // Display actial data from table.
  $result = db_select('tdemo', 't')->fields('t')->execute();
  $rows = array();
  foreach ($result as $row) {
    $rows[] = array(
      array('data' => array(
        '#theme' => 'item_list',
        '#items' => explode(TDEMO_DELIMETER, $row->source),
      )),
      array('data' => array(
        '#theme' => 'item_list',
        '#items' => explode(TDEMO_DELIMETER, $row->translation),
      )),
    );
  }
  $form['table'] = array(
    '#theme' => 'table',
    '#caption' => 'Database',
    '#header' => array('Source', 'Translations'),
    '#rows' => $rows,
    '#empty' => 'Generate strings',
  );
  $form['text'] = array(
    '#title' => 'Enter text to search',
    '#type' => 'textfield',
  );
  $rows = array();
  if (isset($form_state['#res'])) {
    // Display search results.
    foreach ($form_state['#res'] as $row) {
      $rows[] = array(
        array('data' => array(
          '#theme' => 'item_list',
          '#items' => explode(TDEMO_DELIMETER, $row->source),
        )),
        array('data' => array(
          '#theme' => 'item_list',
          '#items' => explode(TDEMO_DELIMETER, $row->translation),
        )),
      );
    }
  }
  $form['result'] = array(
    '#theme' => 'table',
    '#caption' => 'Search',
    '#header' => array('Source', 'Translations'),
    '#rows' => $rows,
    '#empty' => 'No search result',
  );
  $form['actions'] = array('#type' => 'actions');
  $form['actions']['search'] = array(
    '#type' => 'submit',
    '#value' => 'Search',
  );
  $form['actions']['generate'] = array(
    '#type' => 'submit',
    '#value' => 'Generate',
    '#submit' => array('tdemo_regenerate'),
  );
  return $form;
}

function tdemo_form_text_submit($form, &$form_state) {
  $text = $form_state['values']['text'];
  $result = db_select('tdemo', 't')
    ->fields('t')
    ->condition(db_or()
      ->condition('source', '%' . $text . '%', 'LIKE')
      ->condition('translation', '%' . $text . '%', 'LIKE')
    )
    ->execute();
  $rows = array();
  foreach ($result as $row) {
    $rows[] = $row;
  }
  $form_state['#res'] = $rows;
  $form_state['rebuild'] = TRUE;
}

function tdemo_regenerate($form, $form_state) {
  db_delete('tdemo')->execute();
  $source1 = implode(TDEMO_DELIMETER, array('1 comment', '@count comments'));
  $source2 = implode(TDEMO_DELIMETER, array('1 new comment', '@count new comments'));
  $translation1 = implode(TDEMO_DELIMETER, array('1 комментарий', '@count коментария', '@count коментариев'));
  $translation2 = implode(TDEMO_DELIMETER, array('1 новый комментарий', '@count новых коментария', '@count  коментариев'));
  db_insert('tdemo')
    ->fields(array('source', 'translation'))
    ->values(array($source1, $translation1))
    ->values(array($source2, $translation2))
    ->execute();
  drupal_set_message('Table regenerated');
  $form_state['rebuild'] = TRUE;
}
