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