Subversion Repositories oidplus

Rev

Go to most recent revision | Details | 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 OIDplusDoi extends OIDplusObject {
21
        private $doi;
22
 
23
        public function __construct($doi) {
24
                $this->doi = $doi;
25
        }
26
 
27
        public static function parse($node_id) {
28
                @list($namespace, $doi) = explode(':', $node_id, 2);
29
                if ($namespace !== 'doi') return false;
30
                return new self($doi);
31
        }
32
 
33
        public static function objectTypeTitle() {
34
                return "Digital Object Identifier (DOI)";
35
        }
36
 
37
        public static function objectTypeTitleShort() {
38
                return "DOI";
39
        }
40
 
41
        public static function ns() {
42
                return 'doi';
43
        }
44
 
45
        public static function root() {
46
                return 'doi:';
47
        }
48
 
49
        public function isRoot() {
50
                return $this->doi == '';
51
        }
52
 
53
        public function nodeId() {
54
                return 'doi:'.$this->doi;
55
        }
56
 
57
        public function addString($str) {
58
                if ($this->isRoot()) {
59
                        // Parent is root, so $str is the base DOI (10.xxxx)
60
                        $base = $str;
61
                        if (!self::validBaseDoi($base)) {
62
                                throw new Exception("Invalid DOI $base . It must have syntax 10.xxxx");
63
                        }
64
                        return 'doi:' . $base;
65
                } else if (self::validBaseDoi($this->doi)) {
66
                        // First level: We add a pubilcation to the base
67
                        return 'doi:' . $this->doi . '/' . $str;
68
                } else {
69
                        // We just add an additional string to the already existing publication, e.g. a graphic reference or chapter
70
                        return 'doi:' . $this->doi . $str;
71
                }
72
        }
73
 
74
        public function crudShowId(OIDplusObject $parent) {
75
                return $this->doi;
76
        }
77
 
78
        public function crudInsertPrefix() {
79
                return $this->isRoot() ? '' : substr($this->addString(''), strlen(self::ns())+1);
80
        }
81
 
82
        public function jsTreeNodeName(OIDplusObject $parent = null) {
83
                if ($parent == null) return $this->objectTypeTitle();
84
                $out = $this->doi;
85
                $ary = explode('/', $out, 2);
86
                if (count($ary) > 1) $out = $ary[1];
87
                return $out;
88
        }
89
 
90
        public function defaultTitle() {
91
                return 'DOI ' . $this->doi;
92
        }
93
 
94
        public function getContentPage(&$title, &$content) {
95
                if ($this->isRoot()) {
96
                        $title = OIDplusDoi::objectTypeTitle();
97
 
98
                        $res = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."objects where parent = '".OIDplus::db()->real_escape_string(self::root())."'");
99
                        if (OIDplus::db()->num_rows($res) > 0) {
100
                                $content = 'Please select an DOI in the tree view at the left to show its contents.';
101
                        } else {
102
                                $content = 'Currently, no DOIs are registered in the system.';
103
                        }
104
 
105
                        if (OIDplus::authUtils()::isAdminLoggedIn()) {
106
                                $content .= '<h2>Manage your DOIs</h2>';
107
                        } else {
108
                                $content .= '<h2>Available DOIs</h2>';
109
                        }
110
                        $content .= '%%CRUD%%';
111
                } else {
112
                        $pure = explode(':',$this->nodeId())[1];
113
                        $content = '<h3><a target="_blank" href="https://dx.doi.org/'.htmlentities($pure).'">Resolve '.htmlentities($pure).'</a></h3>';
114
 
115
                        $content .= '<h2>Description</h2><br><br>%%DESC%%<br><br>'; // TODO: add more meta information about the object type
116
 
117
                        if ($this->userHasWriteRights()) {
118
                                $content .= '<h2>Create or change subsequent objects</h2>';
119
                        } else {
120
                                $content .= '<h2>Subsequent objects</h2>';
121
                        }
122
                        $content .= '%%CRUD%%';
123
                }
124
        }
125
 
126
        # ---
127
 
128
        public static function validBaseDoi($doi) {
129
                return preg_match('@^10\.\d{4}$@', $doi, $m);
130
        }
131
}
132
 
133
OIDplusObject::$registeredObjectTypes[] = 'OIDplusDoi';