aboutsummaryrefslogtreecommitdiff
path: root/isis.php
blob: 4dd0ee2948dc799efdc543414534f3e01aea14e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
/**
 * Database procedures.
 */

/**
 * Generic interface for reading Isis databases.
 */
interface IsisDb {
  // Constructor.
  public function __construct($schema);

  // Return field data for a given entry.
  public function fields($id = NULL);

  // Return subfield data for a given entry.
  public function subfields($id = NULL);

  // Read an entry.
  public function read($id);

  // Return number of rows in the database.
  public function rows();

  // Return a default example schema.
  public function default_schema();
}

/**
 * Malete implementation of IsisDb.
 */
class MaleteDb implements IsisDb {
  var $fdt;
  var $db;
  var $format;

  public function __construct($schema) {
    // Save db schema.
    $this->format = $schema;

    // Setup $fdt used by malete.
    foreach ($schema['fields'] as $field => $info) {
      $this->fdt[$field] = $info['name'];
    }

    // Open a database connection.
    $this->db = new Isis_Db($this->fdt, $schema['db']['name'], new Isis_Server());
    if (!$this->db->srv->sock) {
      return FALSE;
    }
  }

  public function fields($id = NULL) {
  }

  public function subfields($id = NULL) {
  }

  public function read($id) {
    if (!is_numeric($id)) {
      return FALSE;
    }
    $results = $this->db->read($id);
    return $this->tag($results);
  }

  public function rows() {
  }

  /**
   * Schema format example.
   */
  public function default_schema() {
    $schema = array(
      'db'             => array(
        'name'         => 'dbname',
      ),
      'fields'         => array(
        1              => array(
          'name'       => 'field_name',
          'size'       => 1000,
          'format'     => 'numeric',
          'repeat'     => TRUE,
          'subfields'  => array(
            'a'        => 'test',
            'b'        => 'test2',
          ),
        ),
      ),
    );

    return $schema;
  }

  // Tag results of a db query.
  function tag($results) {
    foreach ($results->val as $key => $value) {
      $field = $results->tag[$key];
      $name  = $this->format['fields'][$field]['name'];

      // Handles field repetition.
      if ($this->format['fields'][$field]['repeat']) {
        $data[$name][] = $value;
      }
      else {
        $data[$name] = $value;
      }
    }
    return $data;
  }
}

/**
 * PHP-Isis implementation of IsisDb.
 */
class PhpIsis implements IsisDb {
}