Subversion Repositories oidplus

Rev

Rev 489 | Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * OIDplus 2.0
  5.  * Copyright 2019 - 2021 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. if (!defined('INSIDE_OIDPLUS')) die();
  21.  
  22. class OIDplusGs1 extends OIDplusObject {
  23.         private $number;
  24.  
  25.         public function __construct($number) {
  26.                 // TODO: syntax checks
  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 _L('GS1 Based IDs (GLN/GTIN/SSCC/...)');
  38.         }
  39.  
  40.         public static function objectTypeTitleShort() {
  41.                 return _L('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($with_ns=true) {
  57.                 return $with_ns ? 'gs1:'.$this->number : $this->number;
  58.         }
  59.  
  60.         public function addString($str) {
  61.                 $m = array();
  62.                 if (!preg_match('@^\\d+$@', $str, $m)) {
  63.                         throw new OIDplusException(_L('GS1 value needs to be numeric'));
  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() {
  74.                 return $this->isRoot() ? '' : $this->chunkedNotation(false);
  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.  
  86.         public function isLeafNode() {
  87.                 return !$this->isBaseOnly();
  88.         }
  89.  
  90.         public function getContentPage(&$title, &$content, &$icon) {
  91.                 $icon = file_exists(__DIR__.'/icon_big.png') ? 'plugins/objectTypes/'.basename(__DIR__).'/icon_big.png' : '';
  92.  
  93.                 if ($this->isRoot()) {
  94.                         $title = OIDplusGs1::objectTypeTitle();
  95.  
  96.                         $res = OIDplus::db()->query("select * from ###objects where parent = ?", array(self::root()));
  97.                         if ($res->num_rows() > 0) {
  98.                                 $content  = _L('Please select an item in the tree view at the left to show its contents.');
  99.                         } else {
  100.                                 $content  = _L('Currently, no GS1 based numbers are registered in the system.');
  101.                         }
  102.  
  103.                         if (!$this->isLeafNode()) {
  104.                                 if (OIDplus::authUtils()::isAdminLoggedIn()) {
  105.                                         $content .= '<h2>'._L('Manage root objects').'</h2>';
  106.                                 } else {
  107.                                         $content .= '<h2>'._L('Available objects').'</h2>';
  108.                                 }
  109.                                 $content .= '%%CRUD%%';
  110.                         }
  111.                 } else {
  112.                         $title = $this->getTitle();
  113.  
  114.                         if ($this->isLeafNode()) {
  115.                                 $chunked = $this->chunkedNotation(true);
  116.                                 $checkDigit = $this->checkDigit();
  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>';
  119.                                 $content .= '<img src="plugins/objectTypes/'.basename(__DIR__).'/barcode.php?number='.urlencode($this->fullNumber()).'">';
  120.                                 $content .= '<h2>'._L('Description').'</h2>%%DESC%%'; // TODO: add more meta information about the object type
  121.                         } else {
  122.                                 $chunked = $this->chunkedNotation(true);
  123.                                 $content = '<h2>'.$chunked.'</h2>';
  124.                                 $content .= '<h2>'._L('Description').'</h2>%%DESC%%'; // TODO: add more meta information about the object type
  125.                                 if ($this->userHasWriteRights()) {
  126.                                         $content .= '<h2>'._L('Create or change subsequent objects').'</h2>';
  127.                                 } else {
  128.                                         $content .= '<h2>'._L('Subsequent objects').'</h2>';
  129.                                 }
  130.                                 $content .= '%%CRUD%%';
  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.  
  144.                 $res = OIDplus::db()->query("select id, title from ###objects where id = ?", array($curid));
  145.                 if ($res->num_rows() == 0) return $this->number;
  146.  
  147.                 $hints = array();
  148.                 $lengths = array(strlen($curid));
  149.                 while (($res = OIDplus::db()->query("select parent, title from ###objects where id = ?", array($curid)))->num_rows() > 0) {
  150.                         $row = $res->fetch_array();
  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.         }
  188.  
  189.         public function one_up() {
  190.                 return  OIDplusObject::parse($this->ns().':'.substr($this->number,0,strlen($this->number)-1));
  191.         }
  192.  
  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.  
  203.         public function distance($to) {
  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.  
  227.                 return null;
  228.         }
  229. }
  230.