Subversion Repositories oidplus

Rev

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