3 * FusionForge Documentation Manager
5 * Copyright 2016,2021, Franck Villaume - TrivialDev
6 * http://fusionforge.org
8 * This file is part of FusionForge. FusionForge is free software;
9 * you can redistribute it and/or modify it under the terms of the
10 * GNU General Public License as published by the Free Software
11 * Foundation; either version 2 of the Licence, or (at your option)
14 * FusionForge is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 require_once $gfcommon.'include/FFError.class.php';
25 require_once $gfcommon.'docman/DocumentStorage.class.php';
27 $DOCUMENTVERSION_OBJ = array();
29 function &documentversion_get_object($ver_id, $docid, $group_id, $res = false) {
30 global $DOCUMENTVERSION_OBJ;
31 if (!isset($DOCUMENTVERSION_OBJ['_'.$ver_id.'-'.$docid.'_'])) {
33 //the db result handle was passed in
35 // data_words is not retrieve. Too much memory consumption.
36 $res = db_query_params('SELECT serial_id, version, docid, current_version, title, updatedate, createdate, created_by, description, filename, filetype, filesize, vcomment FROM doc_data_version WHERE version = $1 AND docid = $2',
37 array($ver_id, $docid));
39 if (!$res || db_numrows($res) < 1) {
40 $DOCUMENTVERSION_OBJ['_'.$ver_id.'-'.$docid.'_'] = false;
42 $DOCUMENTVERSION_OBJ['_'.$ver_id.'-'.$docid.'_'] = new DocumentVersion(document_get_object($docid, $group_id), $ver_id, db_fetch_array($res));
45 return $DOCUMENTVERSION_OBJ['_'.$ver_id.'-'.$docid.'_'];
48 class DocumentVersion extends FFError {
50 * Associative array of data from db.
52 * @var array $data_array.
57 * The Document object.
59 * @var object $Document.
68 function __construct(&$Document, $verid = false, $arr = false) {
69 parent::__construct();
70 if (!$Document || !is_object($Document)) {
71 $this->setError(_('No Valid Document Object'));
74 if ($Document->isError()) {
75 $this->setError(_('Document Version')._(': ').$Document->getErrorMessage());
78 $this->Document =& $Document;
80 if ($arr && is_array($arr) && ($arr['docid'] == $this->Document->getID()) && ($arr['version'] == $verid)) {
81 $this->data_array =& $arr;
83 if (!$this->fetchData($verid)) {
92 return $this->data_array['serial_id'];
95 function getFileName() {
96 return $this->data_array['filename'];
99 function getFileType() {
100 return $this->data_array['filetype'];
103 function getFileSize() {
104 return $this->data_array['filesize'];
107 function getTitle() {
108 return $this->data_array['title'];
111 function getDescription() {
112 return $this->data_array['description'];
115 function getComment() {
116 return $this->data_array['vcomment'];
120 * getFileData - the filedata of this document.
122 * @return string The filedata.
124 function getFileData() {
125 return file_get_contents($this->getFilePath());
129 * getFilePath - the filepath of this document.
131 * @return string The file where the file is stored.
133 function getFilePath() {
134 return DocumentStorage::instance()->get($this->getID());
137 function fetchData($verid) {
138 // everything but data_words. Too much memory consumption.
139 $res = db_query_params('SELECT serial_id, version, docid, current_version, title, updatedate, createdate, created_by, description, filename, filetype, filesize, vcomment FROM doc_data_version WHERE version = $1 AND docid = $2',
140 array($verid, $this->Document->getID()));
141 if (!$res || db_numrows($res) < 1) {
142 $this->setError(_('DocumentVersion')._(': ')._('Invalid version id'));
145 $this->data_array = db_fetch_array($res);
146 db_free_result($res);
151 * create - create a new version of a document
153 * @param int $docid The linked document to which we create a new version
154 * @param string $title The title of the new version
155 * @param string $description The description of the new version
156 * @param int $created_by The user id who creates the version
157 * @param string $filetype The filetype of the content
158 * @param string $filename The name of the file (content)
159 * @param int $filesize The size of the file (content)
160 * @param string $kwords The parsed words of the file (content)
161 * @param int $createtimetamp timestamp of creation of this version
162 * @param int $version The version id to create. Default is 1 (the first version)
163 * @param int $current_version Is it the current version? Defaut is 1 (yes)
164 * @param string $vcomment
165 * @return bool true on success
167 function create($docid, $title, $description, $created_by, $filetype, $filename, $filesize, $kwords, $createtimetamp, $version = 1, $current_version = 1, $vcomment = '') {
169 $res = db_query_params('INSERT INTO doc_data_version (docid, title, description, created_by, filetype, filename, filesize, data_words, version, current_version, createdate, vcomment)
170 VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)',
171 array($docid, htmlspecialchars($title), htmlspecialchars($description), $created_by, $filetype, $filename, $filesize, $kwords, $version, $current_version, $createtimetamp, htmlspecialchars($vcomment)));
172 $serial_id = db_insertid($res, 'doc_data_version', 'serial_id');
173 if (!$res || !$serial_id) {
174 $this->setError(_('Document Version')._(': ')._('Cannot create version.').' '.db_error());
178 if ($current_version) {
179 $res = db_query_params('UPDATE doc_data_version SET current_version = $1 WHERE docid = $2 AND version != $3',
180 array(0, $this->Document->getID(), $version));
182 $this->setOnUpdateError(_('Cannot update current version on creation.').' '.db_error());
192 * delete - delete a specific version
194 * @param int $verid The version to delete
195 * @return bool true on success
197 function delete($verid) {
199 $res = db_query_params('DELETE FROM doc_data_version WHERE docid = $1 and version = $2',
200 array($this->Document->getID(), $verid));
202 $this->setError(_('DocumentVersion')._(': ')._('Invalid version id').' '.db_error());
206 if ($this->getNumberOfVersions() == 1) {
207 $this->getMaxVersionData();
208 $this->update($this->data_array['version'], $this->data_array['title'], $this->data_array['description'], $this->data_array['filetype'],
209 $this->data_array['filename'], $this->data_array['filesize'], $this->data_array['updatedate'], 1, $this->data_array['vcomment']);
212 db_free_result($res);
217 * getVersion - retrieve the data of a specific version
219 * @param int $version The version to retrive
220 * @return array the content of a version
222 function getVersion($version) {
223 $this->fetchData($version);
224 return $this->data_array;
228 * getMaxVersionID - get the highest value of version of a document
230 * @return int The max value.
232 function getMaxVersionID() {
233 $res = db_query_params('SELECT MAX(version) FROM doc_data_version WHERE docid = $1',
234 array($this->Document->getID()));
236 $arr = db_fetch_array($res);
243 * getMaxVersionData - retrieve the content of the highest version of the document
245 * @return array the content of a version.
247 function getMaxVersionData() {
248 return $this->fetchData($this->getMaxVersionID());
252 * getNumberOfVersions - get the number of versions of this document
254 * @return int Number of versions
256 function getNumberOfVersions() {
257 $res = db_query_params('SELECT COUNT(version) FROM doc_data_version WHERE docid = $1',
258 array($this->Document->getID()));
260 $arr = db_fetch_array($res);
266 * isURL - whether this document is a URL and not a local file.
268 * @return bool is_url.
271 return ($this->data_array['filetype'] == 'URL');
274 function isCurrent() {
275 return $this->data_array['current_version'];
279 * update - Update an existing version of a document
281 * @param int $version The version id to update
282 * @param string $title The new title
283 * @param string $description The new description
284 * @param string $filetype The new filetype
285 * @param string $filename The new filename
286 * @param int $filesize The new filesize
287 * @param int $updatetimestamp timestamp of this update
288 * @param int $current_version Is the current version to set? Default is yes.
289 * @param string $vcomment
290 * @return bool true on success
292 function update($version, $title, $description, $filetype, $filename, $filesize, $updatetimestamp, $current_version = 1, $vcomment = '') {
294 $colArr = array('title', 'description', 'filetype', 'filename', 'filesize', 'current_version', 'updatedate', 'vcomment');
295 $valArr = array(htmlspecialchars($title), htmlspecialchars($description), $filetype, $filename, $filesize, $current_version, $updatetimestamp, htmlspecialchars($vcomment));
296 if (!$this->setValueinDB($version, $colArr, $valArr)) {
300 if ($current_version) {
301 $res = db_query_params('UPDATE doc_data_version SET current_version = $1 WHERE docid = $2 AND version != $3',
302 array(0, $this->Document->getID(), $version));
304 $this->setOnUpdateError(_('Cannot set current_version.').' '.db_error());
314 * updateDataWords - update the indexation of content of a version of the document
316 * @param int $version The version to update
317 * @param string $data_words The content of the document as parsed
318 * @return bool true on success
320 function updateDataWords($version, $data_words) {
322 $colArr = array('data_words');
323 $valArr = array($data_words);
324 if (!$this->setValueinDB($version, $colArr, $valArr)) {
333 * setValueinDB - private function to update columns in db
335 * @param int $version the version id to update
336 * @param array $colArr the columns to update in array form array('col1', col2')
337 * @param array $valArr the values to store in array form array('val1', 'val2')
338 * @return bool success or not
340 private function setValueinDB($version, $colArr, $valArr) {
341 if ((count($colArr) != count($valArr)) || !count($colArr) || !count($valArr)) {
342 $this->setOnUpdateError(_('wrong parameters'));
346 $qpa = db_construct_qpa(false, 'UPDATE doc_data_version SET ');
347 for ($i = 0; $i < count($colArr); $i++) {
349 switch ($colArr[$i]) {
357 case 'current_version':
362 $qpa = db_construct_qpa($qpa, $qpa_string.$colArr[$i].' = $1 ', array($valArr[$i]));
366 $this->setOnUpdateError(_('wrong column name'));
371 $qpa = db_construct_qpa($qpa, ' WHERE version = $1 AND docid = $2',
372 array($version, $this->Document->getID()));
373 $res = db_query_qpa($qpa);
374 if (!$res || db_affected_rows($res) < 1) {
375 $this->setOnUpdateError(db_error());
378 for ($i = 0; $i < count($colArr); $i++) {
379 switch ($colArr[$i]) {
380 // we do not store data_words in memory!
387 case 'current_version':
389 $this->data_array[$colArr[$i]] = $valArr[$i];
396 function hasValidatedReview() {
397 $res = db_query_params('SELECT statusid FROM doc_review, doc_review_version
398 WHERE statusid = $1 AND doc_review.revid = doc_review_version.revid AND doc_review_version.serialid = $2',
399 array(4, $this->getID()));
400 if ($res && (db_numrows($res) > 0)) {