Login

Modify and Delete Record Operations - Drupal 7 Multi-Step Form - Source Lisitings - Report and Object Model

The blog post explaining the following source code is located here.

Table Report Source.

function folks_db_report() {
 
  $header = array(
      'id' => array('data' => t('Id'), 'field' => 'u.id'),
      'first_name' => array('data' => t('First Name'), 'field' => 'u.first_name'),
      'last_name' => array('data' => t('Last Name'), 'field' => 'u.last_name'),
      'color' => array('data' => t('Favorite Color'), 'field' => 'u.color'),
      'operations' => array('data' => t('Operations'), 'colspan' => 2),
  );
 
  $query = db_select('folk', 'u');
 
  $count_query = clone $query;
  $count_query->addExpression('COUNT(u.id)');
 
  $query = $query->extend('PagerDefault')->extend('TableSort');
 
  $query
          ->fields('u', array('id', 'first_name', 'last_name', 'color'))
          ->limit(4)
          ->orderByHeader($header)
          ->setCountQuery($count_query);
 
 
  $result = $query->execute();
 
  $rows = array();
  foreach ($result as $folk) {
 
    $rows[$folk->id] = array(
        'id' => toHtml('span', $folk->id),
        'first_name' => toHtml('span', $folk->first_name),
        'last_name' => toHtml('span', $folk->last_name),
        'color' => toHtml('span', $folk->color),
        'modify' => l('Modify', 'test/multi_step_form/modify/' . $folk->id, array('html' => TRUE)),
        'delete' => l('Delete', 'test/multi_step_form/delete/' . $folk->id, array('html' => TRUE)),
    );
  }
 
  $vars['header'] = $header;
  $vars['empty'] = 'Empty List - No Entries';
  $vars['rows'] = $rows;
 
  $render_array['table'] = array(
      '#theme' => 'table',
      '#rows' => $rows,
      '#empty' => 'Empty List - No Entries',
      '#header' => $header,
  );
  $render_array['pager'] = array(
      '#theme' => 'pager',
  );
 
  return $render_array;
}

Our data objects and database access objects:

interface KeyedEntityInterface {
 
  public function getName();
 
  public function setName($name);
 
  public function getKey();
 
  public function setKey($key);
}
 
class KeyedEntity implements KeyedEntityInterface {
 
  protected $name;
  protected $key;
 
  public function getName() {
    return $this->name;
  }
 
  public function setName($name) {
    $this->name = $name;
  }
 
  public function getKey() {
    return $this->key;
  }
 
  public function setKey($index) {
    $this->key = $index;
  }
 
}
 
 
interface FolkInterface {
 
  public function getFirst_name();
 
  public function setFirst_name($first_name);
 
  public function getLast_name();
 
  public function setLast_name($last_name);
 
  public function getColor();
 
  public function setColor($color);
}
 
class Folk extends KeyedEntity implements FolkInterface {
 
  const FIRST_NAME = 'first_name';
  const LAST_NAME = 'last_name';
  const COLOR = 'color';
  const ID = 'id';
  const FOLK_TABLE_NAME = 'folk';
 
 
  protected $first_name;
  protected $last_name;
  protected $color;
 
  public static function create() {
    return new Folk();
  }
 
  public function getFirst_name() {
    return $this->first_name;
  }
 
  public function setFirst_name($first_name) {
    $this->first_name = $first_name;
    return $this;
  }
 
  public function getLast_name() {
    return $this->last_name;
  }
 
  public function setLast_name($last_name) {
    $this->last_name = $last_name;
    return $this;
  }
 
  public function getColor() {
    return $this->color;
  }
 
  public function setColor($color) {
    $this->color = $color;
    return $this;
  }
 
  public function toArray() {
    return array('first_name' => $this->first_name,
        'last_name' => $this->last_name,
        'color' => $this->color,);
  }
 
}
 
class DbFolk {
 
  function get($id) {
    try {
      $fields = array(Folk::ID,
          Folk::FIRST_NAME,
          Folk::LAST_NAME,
          Folk::COLOR);
 
      $result = db_select(Folk::FOLK_TABLE_NAME, 'n')
                      ->fields('n', $fields)
                      ->condition(Folk::ID, $id)->execute()->fetch();
      $folk = new Folk();
      $folk->setKey($result->id);
      $folk->setFirst_name($result->first_name);
      $folk->setLast_name($result->last_name);
      $folk->setColor($result->color);
    } catch (Exception $e) {
      $databaseException = new FolkDatabaseException($e->getMessage(), 'Get Folk Record',
                      t('Index ID: @index', array('@index' => $id)), TRUE);
      $databaseException->handle();
    }
    return $folk;
  }
 
  function add($first_name, $last_name, $color) {
    try {
      $fields = array(Folk::FIRST_NAME, Folk::LAST_NAME,
          Folk::COLOR);
 
      $insert = db_insert(Folk::FOLK_TABLE_NAME)->fields($fields);
      $insert->values(array($first_name, $last_name, $color));
      $insert->execute();
    } catch (Exception $e) {
      $databaseException = new FolkDatabaseException($e->getMessage(), 'Add',
                      t('Folk First Name: @first_name, Last Name: @last_name, Color: @color', 
                           array('@first_name' => $first_name,
                          '@last_name' => $last_name,
                          '@color' => $color)), TRUE);
      $databaseException->handle();
    }
  }
 
  function set(FolkInterface $folk) {
    try {
      $query = db_update('folk');
      $query->condition('id', $folk->getKey(), '=');
      $query->fields(array(
          'first_name' => $folk->getFirst_name(),
          'last_name' => $folk->getLast_name(),
          'color' => $folk->getColor())
      );
      $query->execute();
    } catch (Exception $e) {
      $databaseException = new FolkDatabaseException($e->getMessage(), 'Update',
                      t('Folk Id: @id', array('@id' => $folk->getKey())), TRUE);
      $databaseException->handle();
    }
  }
 
  function delete($id) {
    try {
      $query = db_delete('folk');
      $query->condition('id', $id, '=');
      $query->execute();
    } catch (Exception $e) {
      $databaseException = new FolkDatabaseException($e->getMessage(),
                      'Delete', t('Folk Id: @id ', array('@id' => $id)), TRUE);
      $databaseException->handle();
    }
  }
 
}

A custom exception handler.

class FolkDatabaseException extends Exception {
 
  private $databaseOperation;
  private $operationParamenters;
  private $displayMessage;
  protected $message;
 
  function __construct($message, $databaseOperation, $operationParamenters, $displayMessage = TRUE) {
    $this->message = $message;
    $this->databaseOperation = $databaseOperation;
    $this->operationParamenters = $operationParamenters;
    $this->displayMessage = $displayMessage;
  }
 
  function handle() {
    if ($this->displayMessage) {
      $output =
              t('Database Exception : @operation : @parameters : @system', array(
          '@operation' => $this->databaseOperation,
          '@parameters' => $this->operationParamenters,
          '@system' => $this->message
              )
      );
      drupal_set_message($output, 'error');
    }
  }
 
}