Rev 360 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 360 | Rev 386 | ||
---|---|---|---|
1 | <?php |
1 | <?php |
2 | 2 | ||
3 | /* |
3 | /* |
4 | * OIDplus 2.0 |
4 | * OIDplus 2.0 |
5 | * Copyright 2019 Daniel Marschall, ViaThinkSoft |
5 | * Copyright 2019 Daniel Marschall, ViaThinkSoft |
6 | * |
6 | * |
7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | * you may not use this file except in compliance with the License. |
8 | * you may not use this file except in compliance with the License. |
9 | * You may obtain a copy of the License at |
9 | * You may obtain a copy of the License at |
10 | * |
10 | * |
11 | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | * |
12 | * |
13 | * Unless required by applicable law or agreed to in writing, software |
13 | * Unless required by applicable law or agreed to in writing, software |
14 | * distributed under the License is distributed on an "AS IS" BASIS, |
14 | * distributed under the License is distributed on an "AS IS" BASIS, |
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | * See the License for the specific language governing permissions and |
16 | * See the License for the specific language governing permissions and |
17 | * limitations under the License. |
17 | * limitations under the License. |
18 | */ |
18 | */ |
19 | 19 | ||
20 | class OIDplusGs1 extends OIDplusObject { |
20 | class OIDplusGs1 extends OIDplusObject { |
21 | private $number; |
21 | private $number; |
22 | 22 | ||
23 | public function __construct($number) { |
23 | public function __construct($number) { |
24 | // TODO: syntax checks |
24 | // TODO: syntax checks |
25 | $this->number = $number; |
25 | $this->number = $number; |
26 | } |
26 | } |
27 | 27 | ||
28 | public static function parse($node_id) { |
28 | public static function parse($node_id) { |
29 | @list($namespace, $number) = explode(':', $node_id, 2); |
29 | @list($namespace, $number) = explode(':', $node_id, 2); |
30 | if ($namespace !== 'gs1') return false; |
30 | if ($namespace !== 'gs1') return false; |
31 | return new self($number); |
31 | return new self($number); |
32 | } |
32 | } |
33 | 33 | ||
34 | public static function objectTypeTitle() { |
34 | public static function objectTypeTitle() { |
35 | return _L('GS1 Based IDs (GLN/GTIN/SSCC/...)'); |
35 | return _L('GS1 Based IDs (GLN/GTIN/SSCC/...)'); |
36 | } |
36 | } |
37 | 37 | ||
38 | public static function objectTypeTitleShort() { |
38 | public static function objectTypeTitleShort() { |
39 | return _L('GS1'); |
39 | return _L('GS1'); |
40 | } |
40 | } |
41 | 41 | ||
42 | public static function ns() { |
42 | public static function ns() { |
43 | return 'gs1'; |
43 | return 'gs1'; |
44 | } |
44 | } |
45 | 45 | ||
46 | public static function root() { |
46 | public static function root() { |
47 | return 'gs1:'; |
47 | return 'gs1:'; |
48 | } |
48 | } |
49 | 49 | ||
50 | public function isRoot() { |
50 | public function isRoot() { |
51 | return $this->number == ''; |
51 | return $this->number == ''; |
52 | } |
52 | } |
53 | 53 | ||
54 | public function nodeId($with_ns=true) { |
54 | public function nodeId($with_ns=true) { |
55 | return $with_ns ? 'gs1:'.$this->number : $this->number; |
55 | return $with_ns ? 'gs1:'.$this->number : $this->number; |
56 | } |
56 | } |
57 | 57 | ||
58 | public function addString($str) { |
58 | public function addString($str) { |
- | 59 | $m = array(); |
|
59 | if (!preg_match('@^\\d+$@', $str, $m)) { |
60 | if (!preg_match('@^\\d+$@', $str, $m)) { |
60 | throw new OIDplusException(_L('GS1 value needs to be numeric')); |
61 | throw new OIDplusException(_L('GS1 value needs to be numeric')); |
61 | } |
62 | } |
62 | 63 | ||
63 | return $this->nodeId() . $str; |
64 | return $this->nodeId() . $str; |
64 | } |
65 | } |
65 | 66 | ||
66 | public function crudShowId(OIDplusObject $parent) { |
67 | public function crudShowId(OIDplusObject $parent) { |
67 | return $this->chunkedNotation(false); |
68 | return $this->chunkedNotation(false); |
68 | } |
69 | } |
69 | 70 | ||
70 | public function crudInsertPrefix() { |
71 | public function crudInsertPrefix() { |
71 | return $this->isRoot() ? '' : $this->chunkedNotation(false); |
72 | return $this->isRoot() ? '' : $this->chunkedNotation(false); |
72 | } |
73 | } |
73 | 74 | ||
74 | public function jsTreeNodeName(OIDplusObject $parent = null) { |
75 | public function jsTreeNodeName(OIDplusObject $parent = null) { |
75 | if ($parent == null) return $this->objectTypeTitle(); |
76 | if ($parent == null) return $this->objectTypeTitle(); |
76 | return substr($this->nodeId(), strlen($parent->nodeId())); |
77 | return substr($this->nodeId(), strlen($parent->nodeId())); |
77 | } |
78 | } |
78 | 79 | ||
79 | public function defaultTitle() { |
80 | public function defaultTitle() { |
80 | return $this->number; |
81 | return $this->number; |
81 | } |
82 | } |
82 | 83 | ||
83 | public function isLeafNode() { |
84 | public function isLeafNode() { |
84 | return !$this->isBaseOnly(); |
85 | return !$this->isBaseOnly(); |
85 | } |
86 | } |
86 | 87 | ||
87 | public function getContentPage(&$title, &$content, &$icon) { |
88 | public function getContentPage(&$title, &$content, &$icon) { |
88 | $icon = file_exists(__DIR__.'/icon_big.png') ? 'plugins/objectTypes/'.basename(__DIR__).'/icon_big.png' : ''; |
89 | $icon = file_exists(__DIR__.'/icon_big.png') ? 'plugins/objectTypes/'.basename(__DIR__).'/icon_big.png' : ''; |
89 | 90 | ||
90 | if ($this->isRoot()) { |
91 | if ($this->isRoot()) { |
91 | $title = OIDplusGs1::objectTypeTitle(); |
92 | $title = OIDplusGs1::objectTypeTitle(); |
92 | 93 | ||
93 | $res = OIDplus::db()->query("select * from ###objects where parent = ?", array(self::root())); |
94 | $res = OIDplus::db()->query("select * from ###objects where parent = ?", array(self::root())); |
94 | if ($res->num_rows() > 0) { |
95 | if ($res->num_rows() > 0) { |
95 | $content = _L('Please select an item in the tree view at the left to show its contents.'); |
96 | $content = _L('Please select an item in the tree view at the left to show its contents.'); |
96 | } else { |
97 | } else { |
97 | $content = _L('Currently, no GS1 based numbers are registered in the system.'); |
98 | $content = _L('Currently, no GS1 based numbers are registered in the system.'); |
98 | } |
99 | } |
99 | 100 | ||
100 | if (!$this->isLeafNode()) { |
101 | if (!$this->isLeafNode()) { |
101 | if (OIDplus::authUtils()::isAdminLoggedIn()) { |
102 | if (OIDplus::authUtils()::isAdminLoggedIn()) { |
102 | $content .= '<h2>'._L('Manage root objects').'</h2>'; |
103 | $content .= '<h2>'._L('Manage root objects').'</h2>'; |
103 | } else { |
104 | } else { |
104 | $content .= '<h2>'._L('Available objects').'</h2>'; |
105 | $content .= '<h2>'._L('Available objects').'</h2>'; |
105 | } |
106 | } |
106 | $content .= '%%CRUD%%'; |
107 | $content .= '%%CRUD%%'; |
107 | } |
108 | } |
108 | } else { |
109 | } else { |
109 | $title = $this->getTitle(); |
110 | $title = $this->getTitle(); |
110 | 111 | ||
111 | if ($this->isLeafNode()) { |
112 | if ($this->isLeafNode()) { |
112 | $chunked = $this->chunkedNotation(true); |
113 | $chunked = $this->chunkedNotation(true); |
113 | $checkDigit = $this->checkDigit(); |
114 | $checkDigit = $this->checkDigit(); |
114 | $content = '<h2>'.$chunked.' - <abbr title="'._L('check digit').'">'.$checkDigit.'</abbr></h2>'; |
115 | $content = '<h2>'.$chunked.' - <abbr title="'._L('check digit').'">'.$checkDigit.'</abbr></h2>'; |
115 | $content .= '<p><a target="_blank" href="https://www.ean-search.org/?q='.htmlentities($this->fullNumber()).'">'._L('Lookup at ean-search.org').'</a></p>'; |
116 | $content .= '<p><a target="_blank" href="https://www.ean-search.org/?q='.htmlentities($this->fullNumber()).'">'._L('Lookup at ean-search.org').'</a></p>'; |
116 | $content .= '<img src="plugins/objectTypes/'.basename(__DIR__).'/barcode.php?number='.urlencode($this->fullNumber()).'">'; |
117 | $content .= '<img src="plugins/objectTypes/'.basename(__DIR__).'/barcode.php?number='.urlencode($this->fullNumber()).'">'; |
117 | $content .= '<h2>'._L('Description').'</h2>%%DESC%%'; // TODO: add more meta information about the object type |
118 | $content .= '<h2>'._L('Description').'</h2>%%DESC%%'; // TODO: add more meta information about the object type |
118 | } else { |
119 | } else { |
119 | $chunked = $this->chunkedNotation(true); |
120 | $chunked = $this->chunkedNotation(true); |
120 | $content = '<h2>'.$chunked.'</h2>'; |
121 | $content = '<h2>'.$chunked.'</h2>'; |
121 | $content .= '<h2>'._L('Description').'</h2>%%DESC%%'; // TODO: add more meta information about the object type |
122 | $content .= '<h2>'._L('Description').'</h2>%%DESC%%'; // TODO: add more meta information about the object type |
122 | if ($this->userHasWriteRights()) { |
123 | if ($this->userHasWriteRights()) { |
123 | $content .= '<h2>'._L('Create or change subsequent objects').'</h2>'; |
124 | $content .= '<h2>'._L('Create or change subsequent objects').'</h2>'; |
124 | } else { |
125 | } else { |
125 | $content .= '<h2>'._L('Subsequent objects').'</h2>'; |
126 | $content .= '<h2>'._L('Subsequent objects').'</h2>'; |
126 | } |
127 | } |
127 | $content .= '%%CRUD%%'; |
128 | $content .= '%%CRUD%%'; |
128 | } |
129 | } |
129 | } |
130 | } |
130 | } |
131 | } |
131 | 132 | ||
132 | # --- |
133 | # --- |
133 | 134 | ||
134 | public function isBaseOnly() { |
135 | public function isBaseOnly() { |
135 | return strlen($this->number) <= 7; |
136 | return strlen($this->number) <= 7; |
136 | } |
137 | } |
137 | 138 | ||
138 | public function chunkedNotation($withAbbr=true) { |
139 | public function chunkedNotation($withAbbr=true) { |
139 | $curid = 'gs1:'.$this->number; |
140 | $curid = 'gs1:'.$this->number; |
140 | 141 | ||
141 | $res = OIDplus::db()->query("select id, title from ###objects where id = ?", array($curid)); |
142 | $res = OIDplus::db()->query("select id, title from ###objects where id = ?", array($curid)); |
142 | if ($res->num_rows() == 0) return $this->number(); |
143 | if ($res->num_rows() == 0) return $this->number(); |
143 | 144 | ||
144 | $hints = array(); |
145 | $hints = array(); |
145 | $lengths = array(strlen($curid)); |
146 | $lengths = array(strlen($curid)); |
146 | while (($res = OIDplus::db()->query("select parent, title from ###objects where id = ?", array($curid)))->num_rows() > 0) { |
147 | while (($res = OIDplus::db()->query("select parent, title from ###objects where id = ?", array($curid)))->num_rows() > 0) { |
147 | $row = $res->fetch_array(); |
148 | $row = $res->fetch_array(); |
148 | $curid = $row['parent']; |
149 | $curid = $row['parent']; |
149 | $hints[] = $row['title']; |
150 | $hints[] = $row['title']; |
150 | $lengths[] = strlen($curid); |
151 | $lengths[] = strlen($curid); |
151 | } |
152 | } |
152 | 153 | ||
153 | array_shift($lengths); |
154 | array_shift($lengths); |
154 | $chunks = array(); |
155 | $chunks = array(); |
155 | 156 | ||
156 | $full = 'gs1:'.$this->number; |
157 | $full = 'gs1:'.$this->number; |
157 | foreach ($lengths as $len) { |
158 | foreach ($lengths as $len) { |
158 | $chunks[] = substr($full, $len); |
159 | $chunks[] = substr($full, $len); |
159 | $full = substr($full, 0, $len); |
160 | $full = substr($full, 0, $len); |
160 | } |
161 | } |
161 | 162 | ||
162 | $hints = array_reverse($hints); |
163 | $hints = array_reverse($hints); |
163 | $chunks = array_reverse($chunks); |
164 | $chunks = array_reverse($chunks); |
164 | 165 | ||
165 | $full = array(); |
166 | $full = array(); |
166 | foreach ($chunks as $c) { |
167 | foreach ($chunks as $c) { |
167 | $full[] = $withAbbr ? '<abbr title="'.htmlentities(array_shift($hints)).'">'.$c.'</abbr>' : $c; |
168 | $full[] = $withAbbr ? '<abbr title="'.htmlentities(array_shift($hints)).'">'.$c.'</abbr>' : $c; |
168 | } |
169 | } |
169 | return implode(' ', $full); |
170 | return implode(' ', $full); |
170 | } |
171 | } |
171 | 172 | ||
172 | public function fullNumber() { |
173 | public function fullNumber() { |
173 | return $this->number . $this->checkDigit(); |
174 | return $this->number . $this->checkDigit(); |
174 | } |
175 | } |
175 | 176 | ||
176 | public function checkDigit() { |
177 | public function checkDigit() { |
177 | $mul = 3; |
178 | $mul = 3; |
178 | $sum = 0; |
179 | $sum = 0; |
179 | for ($i=strlen($this->number)-1; $i>=0; $i--) { |
180 | for ($i=strlen($this->number)-1; $i>=0; $i--) { |
180 | $sum += $this->number[$i] * $mul; |
181 | $sum += $this->number[$i] * $mul; |
181 | $mul = $mul == 3 ? 1 : 3; |
182 | $mul = $mul == 3 ? 1 : 3; |
182 | } |
183 | } |
183 | return 10 - ($sum % 10); |
184 | return 10 - ($sum % 10); |
184 | } |
185 | } |
185 | 186 | ||
186 | public function one_up() { |
187 | public function one_up() { |
187 | return OIDplusObject::parse($this->ns().':'.substr($this->number,0,strlen($this->number)-1)); |
188 | return OIDplusObject::parse($this->ns().':'.substr($this->number,0,strlen($this->number)-1)); |
188 | } |
189 | } |
189 | 190 | ||
190 | private static function distance_($a, $b) { |
191 | private static function distance_($a, $b) { |
191 | $min_len = min(strlen($a), strlen($b)); |
192 | $min_len = min(strlen($a), strlen($b)); |
192 | 193 | ||
193 | for ($i=0; $i<$min_len; $i++) { |
194 | for ($i=0; $i<$min_len; $i++) { |
194 | if ($a[$i] != $b[$i]) return false; |
195 | if ($a[$i] != $b[$i]) return false; |
195 | } |
196 | } |
196 | 197 | ||
197 | return strlen($a) - strlen($b); |
198 | return strlen($a) - strlen($b); |
198 | } |
199 | } |
199 | 200 | ||
200 | public function distance($to) { |
201 | public function distance($to) { |
201 | if (!is_object($to)) $to = OIDplusObject::parse($to); |
202 | if (!is_object($to)) $to = OIDplusObject::parse($to); |
202 | if (!($to instanceof $this)) return false; |
203 | if (!($to instanceof $this)) return false; |
203 | 204 | ||
204 | // This is pretty tricky, because the whois service should accept GS1 numbers with and without checksum |
205 | // This is pretty tricky, because the whois service should accept GS1 numbers with and without checksum |
205 | if ($this->number == $to->number) return 0; |
206 | if ($this->number == $to->number) return 0; |
206 | if ($this->number.$this->checkDigit() == $to->number) return 0; |
207 | if ($this->number.$this->checkDigit() == $to->number) return 0; |
207 | if ($this->number == $to->number.$to->checkDigit()) return 0; |
208 | if ($this->number == $to->number.$to->checkDigit()) return 0; |
208 | 209 | ||
209 | $b = $this->number; |
210 | $b = $this->number; |
210 | $a = $to->number; |
211 | $a = $to->number; |
211 | $tmp = self::distance_($a, $b); |
212 | $tmp = self::distance_($a, $b); |
212 | if ($tmp != false) return $tmp; |
213 | if ($tmp != false) return $tmp; |
213 | 214 | ||
214 | $b = $this->number.$this->checkDigit(); |
215 | $b = $this->number.$this->checkDigit(); |
215 | $a = $to->number; |
216 | $a = $to->number; |
216 | $tmp = self::distance_($a, $b); |
217 | $tmp = self::distance_($a, $b); |
217 | if ($tmp != false) return $tmp; |
218 | if ($tmp != false) return $tmp; |
218 | 219 | ||
219 | $b = $this->number; |
220 | $b = $this->number; |
220 | $a = $to->number.$to->checkDigit(); |
221 | $a = $to->number.$to->checkDigit(); |
221 | $tmp = self::distance_($a, $b); |
222 | $tmp = self::distance_($a, $b); |
222 | if ($tmp != false) return $tmp; |
223 | if ($tmp != false) return $tmp; |
223 | 224 | ||
224 | return null; |
225 | return null; |
225 | } |
226 | } |
226 | } |
227 | } |