summaryrefslogtreecommitdiff
path: root/trunk/reprap/web/james-bom/module.php
blob: be4714dbb90f8aa09acc05059e4b0c55928eee3d (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
<?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.
*/
?>

<?php

function dump_module($module_id) {
  global $db;
  global $model_name;
  global $model;

  function html_row($row, $indent, $module=0, $master_row=0, $start_hide_row=0, $end_hide_row=0) {
    if (!count($row)) {return;}

    $str .= '<tr id="bomRow'.$master_row.'"'.$hide.'>';
    for ($l=0; $l < count($row); $l++) {
      $padding='';
      if ($l==0) {
        if ($indent > 0) {$padding = ' style="padding-left:'.($indent * 15).'px"';}
        $img='';
        if ($module) {
          if ($start_hide_row) {
            $img = '<img id="toggle'.$master_row.'" src="'."css/images/minus.png".'" '.
                   'onclick="toggleModule('."$master_row, $start_hide_row, $end_hide_row)\" />";
          }

          if ($indent==0) {
            $class = ' class="topmodule" ';
          } else {
            $class = ' class="submodule" ';
          }
        }
        $str .= '<td'.$padding.$class.'>'.$img.$row[$l].'</td>';
      } else {
        if ($indent==0 && $module) {
          $str .= "<td class=\"topmodule\">$row[$l]</td>";
        } else {
          $str .= "<td class=\"bomBorderLeft\">$row[$l]</td>";
        }

      }
    }
    $str .= "</tr></div>\n";
    return $str;
  }

  function html_parent_modules($module_id) {
    global $db;

    $q = $db->query("SELECT m.name, m.id FROM module_module mm, module m WHERE mm.submodule_id = $module_id AND mm.supermodule_id=m.id AND m.id NOT IN (SELECT module_id FROM model);");
  
    while ($row = $q->fetchRow()) {
      $str .= $db->make_module_href($row['id'], $row['name']).' ';
    }

    if (!$str) { return; }

    return "This module is part of the following modules: \n".$str;
  }

  function recursive_dump_module($module_id, $level=0, $master_row=0) {
    require_once("view.php");

    global $db;
    global $model_name;
    
    // get all parts
    $q = $db->query("SELECT p.id, p.name, mp.quantity, mp.description, mp.schematic, mp.notes ".
                    "FROM part p, module_part mp WHERE mp.module_id=$module_id AND p.id=mp.part_id ORDER BY p.name;");
    while ($row = $q->fetchRow(MDB2_FETCHMODE_ORDERED)) {
      $id = $row[0];
      $out[0] =  $db->make_part_href($id, $row[1]).($row[2] != 1 ? " ($row[2])" : '');
      $out[1] =  combine_description_notes($row[3], $row[5]);
      $out[2] = $row[4];
      $out[3] =  $db->get_part_tags($id);
      $out[4] = html_source_for_part($id);

      $str .= html_row($out,  $level, 0, $master_row++);
    }
    

    // get all modules
    /*    $sort = 'm.name';
    if (!strcmp($_GET['sort'], 'supplier')) {
      $sort = 'm.source???
    }
    */

    $q = $db->query("SELECT m.id, m.name, mm.quantity, m.description, mm.schematic, mm.notes ".
                    "FROM module m, module_module mm WHERE m.id=mm.submodule_id AND mm.supermodule_id=$module_id ORDER BY m.name;");
    while ($row = $q->fetchRow(MDB2_FETCHMODE_ORDERED)) {
      $id = $row[0];
      $out[0] = $db->make_module_href($id, $row[1]).($row[2] != 1 ? " ($row[2])" : '');
      $out[1] = combine_description_notes($row[3], $row[5]);
      $out[2] = $row[4];
      $out[3] = $db->get_module_tags($id);
      $out[4] = '';

      $temp_master_row = $master_row;
      $master_row++;
      $temp = recursive_dump_module($id, $level+1, &$master_row);

      if ($master_row > $temp_master_row+1) {
        $start_row_hide = $temp_master_row + 1;
        $end_row_hide = $master_row-1;
      } else {
        $start_row_hide=0;
        $end_row_hide=0;
      }

      $temp_module_row = html_row($out, $level, 1, $temp_master_row, $start_row_hide, $end_row_hide);
      $str .= $temp_module_row.$temp;
    }
    
    return $str;
  }

  $str .= '<table summary="Darwin Bill of Materials" class="bomT" cellspacing="0">';

  if (!$db->is_model($module_id)) {
    $str .=  '<tr><td colspan="20" class="bomHeader">'.$db->get_module_name_by_id($module_id).'</td></tr>'."\n";
  }

  $str .= '<tr><td class="field">'.
    //join("</td><td class=\"field\">", array('Name', 'Description and Notes', 'Schematic', 'Tags', '<a href="?model='.$model.'&module='.$module_id.'&sort=supplier">Suppliers</a>')).    
    join("</td><td class=\"field\">", array('Name', 'Description and Notes', 'Schematic', 'Tags', 'Suppliers')).
    '</td></tr>'."\n";

  $str .=  recursive_dump_module($module_id).'</table>';

  $str .= html_parent_modules($module_id);


  return array('title' => 'Bill of Materials for '.$model_name, 'body' => $str);
}