summaryrefslogtreecommitdiff
path: root/trunk/reprap/web/part-lister/models/rawpart.php
blob: 17a7a4b39dbab0b335b23168be29d87ad8499e21 (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
<?
	class RawPart extends Model
	{
		public function __construct($id = null)
		{
			parent::__construct($id, "raw_parts");
		}
		
		public function getSafeName()
		{
			$name = str_replace(" ", "_", $this->name);
			$name = str_replace('"', '', $name);
			$name = preg_replace("/[^a-zA-Z0-9_.']/", "", $name);
		
			return $name;
		}
		
		public function lookupUnique($name = null, $type = null)
		{
			if ($name === null)
				$name = $this->getLookupName();
			if ($type === null)
				$type = $this->get('type');
			
			$sql = "
				SELECT id
				FROM unique_parts
				WHERE name = '" . db()->safe($name) . "'
					AND type = '$type'
			";
			$partId = db()->getValue($sql);
			
			if ($partId)
				return new UniquePart($partId);
				
			if ($type != 'assembly')
				echo "<b>Couldn't find unique part for $name ($type)</b><br/>";
			
			return false;
		}
		
		public function getLookupName()
		{
			switch ($this->get('type'))
			{
				case 'belt':
					if (preg_match("/\((.+)\) x (\d+)/", $this->get('raw_text'), $matches))
						return $matches[1];
					break;
					
				case 'rp':
					return "Printing Service";
					break;

				case 'rod':
					if (preg_match("/(\d+)mm\D+(\d+)/", $this->get('raw_text'), $matches))
						return "{$matches[1]}mm";
					if (preg_match("/M(\d+)\D+(\d+)/", $this->get('raw_text'), $matches))
						return "{$matches[1]}mm";
					break;
					
				case 'stud':
					if (preg_match("/M(\d+)\D+(\d+)/", $this->get('raw_text'), $matches))
						return "M{$matches[1]}";
					if (preg_match('|1/4"-20\D+(\d+)|', $this->get('raw_text', $matches)))
						return '1/4"-20';
					break;

				case 'wire':
					if (preg_match("/AWG/i", $this->get('raw_text')))
					{
						if (preg_match("/(\d+) AWG/", $this->get('raw_text'), $matches))
								return "$matches[1] AWG";
						if (preg_match("/(\d+)/", $this->get('raw_text'), $matches))
								return '22 AWG';
					}
					break;
			}
			
			return $this->get('raw_text');
		}
		
		public function getDisplayName()
		{
			
		}
		
		public function getRealQuantity()
		{
			switch ($this->get('type'))
			{
				case 'belt':
					if (preg_match("/\((.+)\) x (\d+)/", $this->get('raw_text'), $matches))
						return $matches[2] * $this->get('quantity');
					break;

				case 'rod':
				case 'stud':
					if (preg_match("/M(\d+)\D+(\d+)/", $this->get('raw_text'), $matches))
						return $matches[2] * $this->get('quantity');
					if (preg_match('|1/4"-20\D+(\d+)|', $this->get('raw_text'), $matches))
						return $matches[1] * $this->get('quantity');;
					break;
			}
			
			return $this->get('quantity');
		}
		
		public function getChildren()
		{
			$sql = "
				SELECT id
				FROM raw_parts
				WHERE parent_id = $this->id
				ORDER BY type, raw_text
			";
			
			//echo $sql;
			
			return new Collection($sql, array('RawPart' => 'id'));
		}
		
		public function getComponents($deep = false)
		{
			$data = array();
			
			//get our kids.
			$kids = $this->getChildren()->getAll();
			foreach ($kids AS $row)
			{
				$part = $row['RawPart'];
				
				//echo "Got component: " . $part->get('raw_text') . "\n";

				//assemblies are easy... raw -> raw.
				if ($part->get('type') == 'assembly')
				{
					//echo 'Getting kids for assembly.';
					$kids = $part->getComponents($deep);
					foreach ($kids AS $kid)
					{
						$kid->set('quantity', $kid->get('quantity') * $part->get('quantity'));
						$data[] = $kid;
					}
					//echo 'Done with assembly kids.';
				}
				//modules are based on the unique part... raw -> unique -> raw
				else if ($part->get('type') == 'module')
				{
					//if we're deep, load up our kids
					if ($deep)
					{
						//echo 'Getting kids for module';
						$module = new UniquePart($part->get('part_id'));
						$kids = $module->getRawComponents(true);
						foreach ($kids AS $kid)
						{
							$kid->set('quantity', $kid->get('quantity') * $part->get('quantity'));
							$data[] = $kid;
						}
						//echo 'Done with module kids.';						
					}
					//otherwise just add the part.
					else
						$data[] = $part;
				}
				//just add the part in.
				else
					$data[] = $part;
			}
			
			return $data;
		}
	}
?>