summaryrefslogtreecommitdiff
path: root/trunk/reprap/web/james-bom/db.php
blob: 922df6492d7d1d862dca13e30f115986762e1752 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?php
/*
Bill Of Materials Viewer and Editor

An outgrowth of the RepRap Project.  See reprap.org for details.

Copyright 2007 James Vasile 
Released under Version 1 of the Affero GPL
See LICENSE and <http://www.affero.org/oagpl.html> for details.
*/

class mydb {
  var $db; // database_object;
  var $db_name;
  var $source_id_abbreviation='';

  function mydb($args=array()) {
    // note: set $args['no_use_db'] if you just want to connect to mysql without selecting the bom db

    // Needed to switch from DB.php to mdb2.php for licensing reasons.
    // Thanks to http://www.phpied.com/db-2-mdb2/ for making it easy
    require_once('MDB2.php');

    // included with this distribution in case you don't have it.
    require_once('lib/JSON.php');

    if (!$auth = file('db_auth.php')) {
      die ("Couldn't read db_auth.php!");
    }

    $json = new Services_JSON();
    $auth =  $json->decode(join('', array_slice($auth, 1)));

    if (is_readable($auth->alternate_path)) {
      // if an alternate db_auth file is specified, read it.  If you
      // can't find it, try to use the auth credentials in the
      // original file
      $auth = file($auth->alternate_path);
      $auth =  $json->decode(join('', array_slice($auth, 1)));
    }

    $this->db_name = $auth->db_name;

    $this->db =& MDB2::factory('mysql://'.$auth->db_user.':'.$auth->db_pass.'@'.$auth->host.($args['no_use_db'] ? '' : '/'.$auth->db_name));
    if (MDB2::isError($this->db)) {
      echo ($this->db->getMessage().' - '.$this->db->getUserinfo());
    }

    //$this->db->setFetchMode(MDB2_FETCHMODE_ORDERED);
    $this->db->setFetchMode(MDB2_FETCHMODE_ASSOC);
  }

  function query($sql) {

    if (!preg_match("/[a-z]/i", $sql)) return; // make sure the query isn't blank

    // run the query and get a result handler
    $result = $this->db->query($sql);

    // check if the query was executed properly
    if (MDB2::isError($result)) {
      die ($result->getMessage().' - '.$result->getUserinfo());
    }

    return $result;
  }

  function queries($sql) {
    $queries = explode(";", $sql);
    foreach ($queries as $query) {    
      $this->query($query.";");
    }
  }

  function get_source_abbreviation_id_hash() {
    // get hash of source abbreviations and id

    if ($this->source_id_abbreviation) { return $this->source_id_abbreviation; }

    $q = $this->query("SELECT abbreviation, id FROM source");
    while ($row = $q->fetchRow()) {
      $sid[$row['abbreviation']] = $row['id'];
    }
    $this->source_id_abbreviation = $sid;

    return $sid;
  }

  function get_tags($q) {
    $tag='';
    while ($row = $q->fetchRow(MDB2_FETCHMODE_ORDERED)) {
      $tag .= $this->make_tag_href($row[1], $row[0])."<br />";
    }
    return $tag;
  }
  
  function get_module_tags($id) {
    global $db;
    return $this->get_tags($db->query("SELECT t.name, t.id from tag t, module m, module_tag tm WHERE tm.module_id=m.id AND t.id=tm.tag_id and m.id=$id"));
  }
  
  function get_part_tags($id) {
    global $db;
    return $this->get_tags( $db->query("SELECT t.name, t.id FROM tag t, part_tag pt WHERE pt.part_id=".$id." AND t.id=pt.tag_id;") );
  }

  function get_source_id_by_name ($name) {
    $name = preg_replace('/"/', '\\"', $name);
    $q = $this->query("SELECT id from source WHERE name=\"$name\";");
    $row = $q->fetchRow();
    if ($row['id'] == NULL) { return -1; }
    return $row['id'];
  }

  function get_model_name_by_id ($id) {
    $q = $this->query("SELECT module.name from model, module WHERE module.id=model.id AND model.id=$id;");
    $row = $q->fetchRow();
    if ($row['name'] == NULL) { return -1; }
    return $row['name'];
  }

  function is_model($module_id) {
    // if module is a model, return 1, else 0
    $q = $this->query("SELECT * from model WHERE module_id=$module_id;");
    $row = $q->fetchRow();
    if ($row['id'] == NULL) { return 0; }
    return 1;
  }

  function get_top_modules_by_model($model) {
    // returns top-level module id, name and quantities
    $q = $this->query("SELECT module.id, module.name, mm.quantity FROM module, model, module_module mm ".
                      "WHERE mm.supermodule_id=model.id AND mm.submodule_id=module.id AND model.id=$model;");
    $row = $q->fetchAll();
    return $row;
  }

  function get_module_name_by_id ($id) {
    $q = $this->query("SELECT name from module WHERE id=$id;");
    $row = $q->fetchRow();
    if ($row['name'] == NULL) { return -1; }
    return $row['name'];
  }

  function get_part_id_by_name ($name) {
    $name = preg_replace('/"/', '\\"', $name);
    $q = $this->query("SELECT id from part WHERE name=\"$name\";");
    $row = $q->fetchRow();
    if ($row['id'] == NULL) { return -1; }
    return $row['id'];
  }

  function get_module_id_by_name ($name) {
    // returns -1 if not found
    // returns 0 if $name is blank
    // otherwise returns module id

    if ($name == NULL) { return 0;}
    $q = $this->query("SELECT id from module WHERE name=\"$name\";");
    $row = $q->fetchRow();
    if ($row['id'] == NULL) { return -1; }
    return $row['id'];
  }


  function get_tag_name_by_id ($id) {
    // returns 0 if $id is 0 or not found
    // otherwise returns tag id

    if (!$id) { return 0; }
    $q = $this->query("SELECT name from tag WHERE id=$id;");
    $row = $q->fetchRow();
    if (!$row['name']) { return 0; }
    return $row['name'];
  }

  function get_tag_id_by_name ($name) {
    // returns -1 if not found
    // returns 0 if $name is blank
    // otherwise returns tag id

    if (!$name) { return 0; }
    $q = $this->query("SELECT id from tag WHERE name=\"$name\";");
    $row = $q->fetchRow();
    if (!$row['id']) { return -1; }
    return $row['id'];
  }

  function create_source($args) {
    // assume only one region.  TODO multiple regions
    $this->query("INSERT INTO source (name, description, url, part_url_prefix, region, abbreviation)".
                 'VALUES ("'. $args['name'] .'", '.
                          '"'.$args['description'].'", '.
                          '"'.$args['url'].'", '.
                          '"'.$args['part_url_prefix'].'", '.
                          '"'.$args['region'].'", '.
                          '"'.$args['abbreviation'].'" '.
                 ');');
    return $this->db->lastInsertID();
  }

  function create_model ($model) {
    // returns module id and model id
    $module_id = $this->create_module($model);
    $this->query("INSERT INTO model (module_id) VALUES ($module_id);");
    return array('model_id' => $this->db->lastInsertID(),
                 'module_id' => $module_id);
  }

  function create_tag ($name, $description='') {
    $this->query("INSERT into tag (name, description) VALUES (\"$name\", \"$description\")");
    return $this->db->lastInsertID();
  }


  function escape_quotes ($str) {
    $str = preg_replace('/"/', '\"', $str);
    return $str;
  }

  function create_part ($name, $description='', $notes='') {
    $name = $this->escape_quotes($name);
    $description = $this->escape_quotes($description);
    $notes = $this->escape_quotes($notes);
    $this->query("INSERT into part (name, description, notes) VALUES (\"$name\", \"$description\", \"$notes\")");
    return $this->db->lastInsertID('part', 'id');
  }
  
  function create_module ($name, $parent_module_id=-1, $quantity=1, $notes='') {
    $q = $this->query ("INSERT into module (name) VALUES (\"".$name."\");");
    $module_id = $this->get_module_id_by_name($name);
    if ($parent_module_id != -1) {
      $this->query ("INSERT into module_module (supermodule_id, submodule_id, quantity, notes) VALUES ($parent_module_id, $module_id, $quantity, \"$notes\");");
    }
    return $module_id;
  }

  function tag_model ($model_id, $tag) {
    if (!$tag) { return; }
    $tag_id = $this->get_tag_id_by_name($tag);
    if ($tag_id == -1) {
      $tag_id = $this->create_tag($tag);
    }
    $this->query("INSERT into model_tag (model_id, tag_id) VALUE ($model_id, $tag_id);");
  }

  function tag_module ($module_id, $tag) {
    if (!$tag) { return; }
    $tag_id = $this->get_tag_id_by_name($tag);
    if ($tag_id == -1) {
      $tag_id = $this->create_tag($tag);
    }
    $this->query("INSERT into module_tag (module_id, tag_id) VALUE ($module_id, $tag_id);");
  }

  function tag_part($part_id, $tag) {
    if (!$tag) { return; }
    $tag_id = $this->get_tag_id_by_name($tag);
    if ($tag_id == -1) {
      $tag_id = $this->create_tag($tag);
    }
    $this->query("INSERT into part_tag (part_id, tag_id) VALUE ($part_id, $tag_id);");
  }

  function make_tag_href($id, $text) {
    global $model;
    return '<a href="?model='.$model.'&tag='.$id.'">'.$text.'</a>';
  }

  function make_module_href($id, $text) {
    global $model;
    //  return $text;
    return '<a href="?model='.$model.'&module='.$id.'">'.$text.'</a>';
  }

  function make_part_href($id, $text) {
    global $model;
    return '<a href="?model='.$model.'&part='.$id.'">'.$text.'</a>';
  }

}

?>