Subversion Repositories oidplus

Rev

Rev 11 | 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
 
20
class OIDplusGs1 extends OIDplusObject {
21
        private $number;
22
 
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() {
35
                return "GS1 Based IDs (GLN/GTIN/SSCC/...)";
36
        }
37
 
38
        public static function objectTypeTitleShort() {
39
                return "GS1";
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
 
54
        public function nodeId() {
55
                return 'gs1:'.$this->number;
56
        }
57
 
58
        public function addString($str) {
59
                if (!preg_match('@^\\d+$@', $str, $m)) {
60
                        throw new Exception('GS1 value needs to be numeric');
61
                }
62
 
63
                return $this->nodeId() . $str;
64
        }
65
 
66
        public function crudShowId(OIDplusObject $parent) {
67
                return $this->chunkedNotation(false);
68
        }
69
 
70
        public function crudInsertPrefix() {
16 daniel-mar 71
                return $this->isRoot() ? '' : $this->chunkedNotation(false);
2 daniel-mar 72
        }
73
 
74
        public function jsTreeNodeName(OIDplusObject $parent = null) {
75
                if ($parent == null) return $this->objectTypeTitle();
76
                return substr($this->nodeId(), strlen($parent->nodeId()));
77
        }
78
 
79
        public function defaultTitle() {
80
                return $this->number;
81
        }
82
 
16 daniel-mar 83
        public function isLeafNode() {
84
                return !$this->isBaseOnly();
85
        }
86
 
2 daniel-mar 87
        public function getContentPage(&$title, &$content) {
88
                if ($this->isRoot()) {
89
                        $title = OIDplusGs1::objectTypeTitle();
90
 
91
                        $res = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."objects where parent = '".OIDplus::db()->real_escape_string(self::root())."'");
92
                        if (OIDplus::db()->num_rows($res) > 0) {
93
                                $content  = 'Please select an item in the tree view at the left to show its contents.';
94
                        } else {
95
                                $content  = 'Currently, no GS1 based numbers are registered in the system.';
96
                        }
97
 
16 daniel-mar 98
                        if (!$this->isLeafNode()) {
99
                                if (OIDplus::authUtils()::isAdminLoggedIn()) {
100
                                        $content .= '<h2>Manage root objects</h2>';
101
                                } else {
102
                                        $content .= '<h2>Available objects</h2>';
103
                                }
104
                                $content .= '%%CRUD%%';
2 daniel-mar 105
                        }
106
                } else {
16 daniel-mar 107
                        if ($this->isLeafNode()) {
2 daniel-mar 108
                                $chunked = $this->chunkedNotation(true);
109
                                $checkDigit = $this->checkDigit();
110
                                $content = '<h2>'.$chunked.' - <abbr title="check digit">'.$checkDigit.'</abbr></h2>';
111
                                $content .= '<p><a target="_blank" href="https://www.ean-search.org/?q='.htmlentities($this->fullNumber()).'">Lookup in ean-search.org</a></p>';
112
                                $content .= '<img src="plugins/objectTypes/'.basename(__DIR__).'/barcode.php?number='.urlencode($this->fullNumber()).'">';
16 daniel-mar 113
                                $content .= '<h2>Description</h2>%%DESC%%'; // TODO: add more meta information about the object type
2 daniel-mar 114
                        } else {
16 daniel-mar 115
                                $chunked = $this->chunkedNotation(true);
116
                                $content = '<h2>'.$chunked.'</h2>';
117
                                $content .= '<h2>Description</h2>%%DESC%%'; // TODO: add more meta information about the object type
118
                                if ($this->userHasWriteRights()) {
119
                                        $content .= '<h2>Create or change subsequent objects</h2>';
120
                                } else {
121
                                        $content .= '<h2>Subsequent objects</h2>';
122
                                }
123
                                $content .= '%%CRUD%%';
2 daniel-mar 124
                        }
125
                }
126
        }
127
 
128
        # ---
129
 
130
        public function isBaseOnly() {
131
                return strlen($this->number) <= 7;
132
        }
133
 
134
        public function chunkedNotation($withAbbr=true) {
135
                $curid = 'gs1:'.$this->number;
136
 
137
                $res = OIDplus::db()->query("select id, title from ".OIDPLUS_TABLENAME_PREFIX."objects where id = '".OIDplus::db()->real_escape_string($curid)."'");
138
                if (OIDplus::db()->num_rows($res) == 0) return $this->number();
139
 
140
                $hints = array();
141
                $lengths = array(strlen($curid));
142
                while (OIDplus::db()->num_rows($res = OIDplus::db()->query("select parent, title from ".OIDPLUS_TABLENAME_PREFIX."objects where id = '".OIDplus::db()->real_escape_string($curid)."'")) > 0) {
143
                        $row = OIDplus::db()->fetch_array($res);
144
                        $curid = $row['parent'];
145
                        $hints[] = $row['title'];
146
                        $lengths[] = strlen($curid);
147
                }
148
 
149
                array_shift($lengths);
150
                $chunks = array();
151
 
152
                $full = 'gs1:'.$this->number;
153
                foreach ($lengths as $len) {
154
                        $chunks[] = substr($full, $len);
155
                        $full = substr($full, 0, $len);
156
                }
157
 
158
                $hints = array_reverse($hints);
159
                $chunks = array_reverse($chunks);
160
 
161
                $full = array();
162
                foreach ($chunks as $c) {
163
                        $full[] = $withAbbr ? '<abbr title="'.htmlentities(array_shift($hints)).'">'.$c.'</abbr>' : $c;
164
                }
165
                return implode(' ', $full);
166
        }
167
 
168
        public function fullNumber() {
169
                return $this->number . $this->checkDigit();
170
        }
171
 
172
        public function checkDigit() {
173
                $mul = 3;
174
                $sum = 0;
175
                for ($i=strlen($this->number)-1; $i>=0; $i--) {
176
                        $sum += $this->number[$i] * $mul;
177
                        $mul = $mul == 3 ? 1 : 3;
178
                }
179
                return 10 - ($sum % 10);
180
        }
181
}
182
 
183
OIDplusObject::$registeredObjectTypes[] = 'OIDplusGs1';
184