Subversion Repositories oidplus

Rev

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