summaryrefslogtreecommitdiff
path: root/trunk/reprap/web/part-lister/models/uniquepart.php
blob: c7ee49b40e9db58705a646a45ff0be7e15717565 (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
<?
	class UniquePart extends Model
	{
		public function __construct($id = null)
		{
			parent::__construct($id, "unique_parts");
		}
		
		public static function byName($name, $type)
		{
			$name = db()->safe($name);
			
			$sql = "
				SELECT id
				FROM unique_parts
				WHERE name = '$name'
					AND type = '$type'
			";
			
			return new UniquePart(db()->getValue($sql));
		}

		public static function getModules()
		{
			$sql = "
				SELECT id
				FROM unique_parts
				WHERE type = 'module'
				ORDER BY id
			";
			
			return new Collection($sql, array(
				'UniquePart' => 'id',
			));
		}

		public function getEmbedUrl()
		{
			return "http://" . SITE_HOSTNAME . "/embed/" . $this->get('type') . "/" . urlencode($this->get('name'));
		}
		
		public function getViewUrl()
		{
			return "/part/" . $this->get('type') . "/" . urlencode($this->get('name'));
		}
		
		public function lookupRawPart()
		{
			//get a top level part that corresponds to this one.
			$sql = "
				SELECT id
				FROM raw_parts
				WHERE part_id = '$this->id'
					AND parent_id = 0
			";
			$id = db()->getValue($sql);
			
			return new RawPart($id);
		}
		
		public function getRawComponents($deep = false)
		{
			//find our root node.
			$raw = $this->lookupRawPart();

			//merge it up!
			$components = array();
			if ($raw->id)
				$components = array_merge($components, $raw->getComponents($deep));
			
			return $components;
		}
		
		public function getUniquePartList($deep = false, $quantity = 1)
		{
			$components = $this->getRawComponents($deep);
			
			$list = new UniquePartList();
			foreach ($components AS $part)
			{
				$part->set('quantity', $part->get('quantity') * $quantity);
				$list->addRaw($part, $quantity);
			}
				
			return $list;
		}
		
		public function getUniqueComponents($deep = false)
		{
			$raw_comps = $this->getRawComponents($deep);
			$data = array();
			
			foreach ($raw_comps AS $raw)
			{
				$unique = new UniquePart($raw->get('part_id'));
			
				//do we already have this unique part?
				if ($data[$raw->get('type')][$unique->id] instanceOf UniquePart)
				{
					//simply add the quantity in...
					$qty = $data[$raw->get('type')][$unique->id]->get('quantity') + $raw->get('quantity');
					$data[$raw->get('type')][$unique->id]->set('quantity', $qty);
				}
				//nope, make a new entry.
				else
				{
					//use the initial quantity from our raw part.
					$unique->set('quantity', $raw->get('quantity'));
					$data[$raw->get('type')][$unique->id] = $unique;
				}
			}
			
			return $data;
		}
		
		public function getSupplierParts()
		{
			$sql = "
				SELECT id, supplier_id
				FROM supplier_parts
				WHERE part_id = '$this->id'
			";
			
			return new Collection($sql, array(
				'SupplierPart' => 'id',
				'Supplier' => 'supplier_id'
			));
		}
		
		public function getParentModules()
		{
			$sql = "
				SELECT p.id, p.part_id
				FROM raw_parts c
				INNER JOIN raw_parts p
					ON c.parent_id = p.id
				LEFT OUTER JOIN unique_parts u
					ON u.id = p.part_id
				WHERE c.part_id = '$this->id'
				ORDER BY u.name
			";
			
			return new Collection($sql, array(
				'UniquePart' => 'part_id',
				'RawPart' => 'id'
			));
		}
		
		public static function byType($type)
		{
			$sql = "
				SELECT id
				FROM unique_parts
				WHERE type = '$type'
				ORDER BY name
			";
			
			return new Collection($sql, array(
				'UniquePart' => 'id',
			));
		}
		
		public static function typeToEnglish($type)
		{
			switch ($type)
			{
				case 'belt':
					return 'Toothed Belt';
				
				case 'component':
					return 'Electronic Component';
				
				case 'fast':
					return 'Fastener';
					
				case 'machined':
					return 'Machined Part';
					
				case 'misc':
					return 'Miscellaneous';
					
				case 'module':
					return 'System Module';
					
				case 'motor':
					return 'Electric Motor';
					
				case 'pcb':
					return 'Printed Circuit Board';
					
				case 'pulley':
					return 'Pulley';
					
				case 'rp':
					return 'Printed Part';
					
				case 'rod':
					return 'Smooth Rod';
					
				case 'stud':
					return 'Threaded Rod';
					
				case 'tool':
					return 'Tool';
					
				case 'wire':
					return 'Wire';
					
				default:
					return ucfirst($type);
			}
		}
	}
?>