Rev 1050 | Rev 1116 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
635 | daniel-mar | 1 | <?php |
2 | |||
3 | /* |
||
4 | * OIDplus 2.0 |
||
1086 | daniel-mar | 5 | * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft |
635 | daniel-mar | 6 | * |
7 | * Licensed under the Apache License, Version 2.0 (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 |
||
10 | * |
||
11 | * http://www.apache.org/licenses/LICENSE-2.0 |
||
12 | * |
||
13 | * Unless required by applicable law or agreed to in writing, software |
||
14 | * distributed under the License is distributed on an "AS IS" BASIS, |
||
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
16 | * See the License for the specific language governing permissions and |
||
17 | * limitations under the License. |
||
18 | */ |
||
19 | |||
1050 | daniel-mar | 20 | namespace ViaThinkSoft\OIDplus; |
635 | daniel-mar | 21 | |
1086 | daniel-mar | 22 | // phpcs:disable PSR1.Files.SideEffects |
23 | \defined('INSIDE_OIDPLUS') or die; |
||
24 | // phpcs:enable PSR1.Files.SideEffects |
||
25 | |||
635 | daniel-mar | 26 | class OIDplusGs1 extends OIDplusObject { |
27 | private $number; |
||
28 | |||
29 | public function __construct($number) { |
||
30 | // TODO: syntax checks |
||
31 | $this->number = $number; |
||
32 | } |
||
33 | |||
34 | public static function parse($node_id) { |
||
35 | @list($namespace, $number) = explode(':', $node_id, 2); |
||
860 | daniel-mar | 36 | if ($namespace !== self::ns()) return false; |
635 | daniel-mar | 37 | return new self($number); |
38 | } |
||
39 | |||
40 | public static function objectTypeTitle() { |
||
41 | return _L('GS1 Based IDs (GLN/GTIN/SSCC/...)'); |
||
42 | } |
||
43 | |||
44 | public static function objectTypeTitleShort() { |
||
45 | return _L('GS1'); |
||
46 | } |
||
47 | |||
48 | public static function ns() { |
||
49 | return 'gs1'; |
||
50 | } |
||
51 | |||
52 | public static function root() { |
||
860 | daniel-mar | 53 | return self::ns().':'; |
635 | daniel-mar | 54 | } |
55 | |||
56 | public function isRoot() { |
||
57 | return $this->number == ''; |
||
58 | } |
||
59 | |||
60 | public function nodeId($with_ns=true) { |
||
859 | daniel-mar | 61 | return $with_ns ? self::root().$this->number : $this->number; |
635 | daniel-mar | 62 | } |
63 | |||
64 | public function addString($str) { |
||
65 | $m = array(); |
||
66 | if (!preg_match('@^\\d+$@', $str, $m)) { |
||
67 | throw new OIDplusException(_L('GS1 value needs to be numeric')); |
||
68 | } |
||
69 | |||
70 | return $this->nodeId() . $str; |
||
71 | } |
||
72 | |||
73 | public function crudShowId(OIDplusObject $parent) { |
||
74 | return $this->chunkedNotation(false); |
||
75 | } |
||
76 | |||
77 | public function crudInsertPrefix() { |
||
78 | return $this->isRoot() ? '' : $this->chunkedNotation(false); |
||
79 | } |
||
80 | |||
81 | public function jsTreeNodeName(OIDplusObject $parent = null) { |
||
82 | if ($parent == null) return $this->objectTypeTitle(); |
||
83 | return substr($this->nodeId(), strlen($parent->nodeId())); |
||
84 | } |
||
85 | |||
86 | public function defaultTitle() { |
||
87 | return $this->number; |
||
88 | } |
||
89 | |||
90 | public function isLeafNode() { |
||
91 | return !$this->isBaseOnly(); |
||
92 | } |
||
93 | |||
94 | public function getContentPage(&$title, &$content, &$icon) { |
||
801 | daniel-mar | 95 | $icon = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : ''; |
635 | daniel-mar | 96 | |
97 | if ($this->isRoot()) { |
||
98 | $title = OIDplusGs1::objectTypeTitle(); |
||
99 | |||
100 | $res = OIDplus::db()->query("select * from ###objects where parent = ?", array(self::root())); |
||
790 | daniel-mar | 101 | if ($res->any()) { |
962 | daniel-mar | 102 | $content = '<p>'._L('Please select an item in the tree view at the left to show its contents.').'</p>'; |
635 | daniel-mar | 103 | } else { |
962 | daniel-mar | 104 | $content = '<p>'._L('Currently, no GS1 based numbers are registered in the system.').'</p>'; |
635 | daniel-mar | 105 | } |
106 | |||
107 | if (!$this->isLeafNode()) { |
||
108 | if (OIDplus::authUtils()->isAdminLoggedIn()) { |
||
109 | $content .= '<h2>'._L('Manage root objects').'</h2>'; |
||
110 | } else { |
||
111 | $content .= '<h2>'._L('Available objects').'</h2>'; |
||
112 | } |
||
113 | $content .= '%%CRUD%%'; |
||
114 | } |
||
115 | } else { |
||
116 | $title = $this->getTitle(); |
||
117 | |||
118 | if ($this->isLeafNode()) { |
||
119 | $chunked = $this->chunkedNotation(true); |
||
120 | $checkDigit = $this->checkDigit(); |
||
121 | $content = '<h2>'.$chunked.' - <abbr title="'._L('check digit').'">'.$checkDigit.'</abbr></h2>'; |
||
122 | $content .= '<p><a target="_blank" href="https://www.ean-search.org/?q='.htmlentities($this->fullNumber()).'">'._L('Lookup at ean-search.org').'</a></p>'; |
||
801 | daniel-mar | 123 | $content .= '<img src="'.OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'barcode.php?number='.urlencode($this->fullNumber()).'">'; |
635 | daniel-mar | 124 | $content .= '<h2>'._L('Description').'</h2>%%DESC%%'; // TODO: add more meta information about the object type |
125 | } else { |
||
126 | $chunked = $this->chunkedNotation(true); |
||
127 | $content = '<h2>'.$chunked.'</h2>'; |
||
128 | $content .= '<h2>'._L('Description').'</h2>%%DESC%%'; // TODO: add more meta information about the object type |
||
129 | if ($this->userHasWriteRights()) { |
||
928 | daniel-mar | 130 | $content .= '<h2>'._L('Create or change subordinate objects').'</h2>'; |
635 | daniel-mar | 131 | } else { |
928 | daniel-mar | 132 | $content .= '<h2>'._L('Subordinate objects').'</h2>'; |
635 | daniel-mar | 133 | } |
134 | $content .= '%%CRUD%%'; |
||
135 | } |
||
136 | } |
||
137 | } |
||
138 | |||
139 | # --- |
||
140 | |||
141 | public function isBaseOnly() { |
||
142 | return strlen($this->number) <= 7; |
||
143 | } |
||
144 | |||
145 | public function chunkedNotation($withAbbr=true) { |
||
859 | daniel-mar | 146 | $curid = self::root().$this->number; |
635 | daniel-mar | 147 | |
977 | daniel-mar | 148 | $obj = OIDplusObject::findFitting($curid); |
149 | if (!$obj) return $this->number; |
||
635 | daniel-mar | 150 | |
151 | $hints = array(); |
||
152 | $lengths = array(strlen($curid)); |
||
977 | daniel-mar | 153 | while ($obj = OIDplusObject::findFitting($curid)) { |
154 | $objParent = $obj->getParent(); |
||
155 | if (!$objParent) break; |
||
156 | $curid = $objParent->nodeId(); |
||
157 | $hints[] = $obj->getTitle(); |
||
635 | daniel-mar | 158 | $lengths[] = strlen($curid); |
159 | } |
||
160 | |||
161 | array_shift($lengths); |
||
162 | $chunks = array(); |
||
163 | |||
859 | daniel-mar | 164 | $full = self::root().$this->number; |
635 | daniel-mar | 165 | foreach ($lengths as $len) { |
166 | $chunks[] = substr($full, $len); |
||
167 | $full = substr($full, 0, $len); |
||
168 | } |
||
169 | |||
170 | $hints = array_reverse($hints); |
||
171 | $chunks = array_reverse($chunks); |
||
172 | |||
173 | $full = array(); |
||
174 | foreach ($chunks as $c) { |
||
934 | daniel-mar | 175 | $hint = array_shift($hints); |
176 | $full[] = $withAbbr && ($hint !== '') ? '<abbr title="'.htmlentities($hint).'">'.$c.'</abbr>' : $c; |
||
635 | daniel-mar | 177 | } |
178 | return implode(' ', $full); |
||
179 | } |
||
180 | |||
181 | public function fullNumber() { |
||
182 | return $this->number . $this->checkDigit(); |
||
183 | } |
||
184 | |||
185 | public function checkDigit() { |
||
186 | $mul = 3; |
||
187 | $sum = 0; |
||
188 | for ($i=strlen($this->number)-1; $i>=0; $i--) { |
||
189 | $sum += $this->number[$i] * $mul; |
||
190 | $mul = $mul == 3 ? 1 : 3; |
||
191 | } |
||
192 | return 10 - ($sum % 10); |
||
193 | } |
||
194 | |||
195 | public function one_up() { |
||
713 | daniel-mar | 196 | return OIDplusObject::parse($this->ns().':'.substr($this->number,0,strlen($this->number)-1)); |
635 | daniel-mar | 197 | } |
198 | |||
199 | private static function distance_($a, $b) { |
||
200 | $min_len = min(strlen($a), strlen($b)); |
||
201 | |||
202 | for ($i=0; $i<$min_len; $i++) { |
||
203 | if ($a[$i] != $b[$i]) return false; |
||
204 | } |
||
205 | |||
206 | return strlen($a) - strlen($b); |
||
207 | } |
||
208 | |||
209 | public function distance($to) { |
||
210 | if (!is_object($to)) $to = OIDplusObject::parse($to); |
||
211 | if (!($to instanceof $this)) return false; |
||
212 | |||
213 | // This is pretty tricky, because the whois service should accept GS1 numbers with and without checksum |
||
214 | if ($this->number == $to->number) return 0; |
||
215 | if ($this->number.$this->checkDigit() == $to->number) return 0; |
||
216 | if ($this->number == $to->number.$to->checkDigit()) return 0; |
||
217 | |||
218 | $b = $this->number; |
||
219 | $a = $to->number; |
||
220 | $tmp = self::distance_($a, $b); |
||
221 | if ($tmp != false) return $tmp; |
||
222 | |||
223 | $b = $this->number.$this->checkDigit(); |
||
224 | $a = $to->number; |
||
225 | $tmp = self::distance_($a, $b); |
||
226 | if ($tmp != false) return $tmp; |
||
227 | |||
228 | $b = $this->number; |
||
229 | $a = $to->number.$to->checkDigit(); |
||
230 | $tmp = self::distance_($a, $b); |
||
231 | if ($tmp != false) return $tmp; |
||
232 | |||
233 | return null; |
||
234 | } |
||
235 | |||
945 | daniel-mar | 236 | public function getAltIds() { |
237 | if ($this->isRoot()) return array(); |
||
238 | $ids = parent::getAltIds(); |
||
239 | |||
240 | // (VTS F5) GS1 to AID (PIX allowed) |
||
241 | $gs1 = $this->nodeId(false); |
||
242 | $aid = 'D276000186F5'.$gs1; |
||
243 | if (strlen($aid)%2 == 1) $aid .= 'F'; |
||
244 | $aid_is_ok = aid_canonize($aid); |
||
959 | daniel-mar | 245 | if ($aid_is_ok) $ids[] = new OIDplusAltId('aid', $aid, _L('Application Identifier (ISO/IEC 7816)'), ' ('._L('Optional PIX allowed, with "FF" prefix').')'); |
945 | daniel-mar | 246 | |
247 | return $ids; |
||
248 | } |
||
249 | |||
635 | daniel-mar | 250 | public function getDirectoryName() { |
251 | if ($this->isRoot()) return $this->ns(); |
||
252 | return $this->ns().'_'.$this->nodeId(false); // safe, because there are only numbers |
||
253 | } |
||
800 | daniel-mar | 254 | |
805 | daniel-mar | 255 | public static function treeIconFilename($mode) { |
800 | daniel-mar | 256 | return 'img/'.$mode.'_icon16.png'; |
257 | } |
||
635 | daniel-mar | 258 | } |