3 * FusionForge Documentation Manager
5 * Copyright 2016, 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 function &documentversion_get_object($ver_id, $docid, $group_id, $res = false) {
28 global $DOCUMENTVERSION_OBJ;
29 if (!isset($DOCUMENTVERSION_OBJ['_'.$ver_id.'_'])) {
31 //the db result handle was passed in
33 // data_words is not retrieve. Too much memory consumption.
34 $res = db_query_params('SELECT serial_id, version, docid, current_version, title, updatedate, createdate, created_by, description, filename, filetype, filesize FROM doc_data_version WHERE version = $1 AND docid = $2',
35 array($ver_id, $docid));
37 if (!$res || db_numrows($res) < 1) {
38 $DOCUMENTVERSION_OBJ['_'.$ver_id.'_'] = false;
40 $DOCUMENTVERSION_OBJ['_'.$ver_id.'_'] = new DocumentVersion(document_get_object($docid, $group_id), $ver_id, db_fetch_array($res));
43 return $DOCUMENTVERSION_OBJ['_'.$ver_id.'_'];
46 class DocumentVersion extends FFError {
48 * Associative array of data from db.
50 * @var array $data_array.
55 * The Document object.
57 * @var object $Document.
65 * @internal param \The $object Document object to which this version is associated.
66 * @internal param \The $int verid.
67 * @internal param \The $array associative array of data.
69 function __construct(&$Document, $verid = false, $arr = false) {
70 parent::__construct();
71 if (!$Document || !is_object($Document)) {
72 $this->setError(_('No Valid Document Object'));
75 if ($Document->isError()) {
76 $this->setError(_('Document Version')._(': ').$Document->getErrorMessage());
79 $this->Document =& $Document;
81 if ($arr && is_array($arr) && ($arr['docid'] == $this->Document->getID()) && ($arr['version'] == $verid)) {
82 $this->data_array =& $arr;
84 if (!$this->fetchData($verid)) {
93 return $this->data_array['serial_id'];
96 function getFileName() {
97 return $this->data_array['filename'];
100 function getFileType() {
101 return $this->data_array['filetype'];
104 function getFileSize() {
105 return $this->data_array['filesize'];
108 function getTitle() {
109 return $this->data_array['title'];
112 function getDescription() {
113 return $this->data_array['description'];
117 * getFileData - the filedata of this document.
119 * @param boolean $download update the download flag or not. default is true
120 * @return string The filedata.
122 function getFileData() {
123 return file_get_contents($this->getFilePath());
127 * getFilePath - the filepath of this document.
129 * @return string The file where the file is stored.
131 function getFilePath() {
132 return DocumentStorage::instance()->get($this->getID());
135 function fetchData($verid) {
136 // everything but data_words. Too much memory consumption.
137 $res = db_query_params('SELECT serial_id, version, docid, current_version, title, updatedate, createdate, created_by, description, filename, filetype, filesize FROM doc_data_version WHERE version = $1 AND docid = $2',
138 array($verid, $this->Document->getID()));
139 if (!$res || db_numrows($res) < 1) {
140 $this->setError(_('DocumentVersion')._(': ')._('Invalid version id'));
143 $this->data_array = db_fetch_array($res);
144 db_free_result($res);
149 * create - create a new version of a document
151 * @param int $docid The linked document to which we create a new version
152 * @param string $title The title of the new version
153 * @param string $description The description of the new version
154 * @param int $created_by The user id who creates the version
155 * @param string $filetype The filetype of the content
156 * @param string $filename The name of the file (content)
157 * @param int $filesize The size of the file (content)
158 * @param string $kwords The parsed words of the file (content)
159 * @param int $version The version id to create. Default is 1 (the first version)
160 * @param int $current_version Is it the current version? Defaut is 1 (yes)
161 * @param int $createtimetamp timestamp of creation of this version
162 * return bool true on success
164 function create($docid, $title, $description, $created_by, $filetype, $filename, $filesize, $kwords, $version = 1, $current_version = 1, $createtimetamp) {
166 $res = db_query_params('INSERT INTO doc_data_version (docid, title, description, created_by, filetype, filename, filesize, data_words, version, current_version, createdate)
167 VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)',
168 array($docid, htmlspecialchars($title), htmlspecialchars($description), $created_by, $filetype, $filename, $filesize, $kwords, $version, $current_version, $createtimetamp));
169 $serial_id = db_insertid($res, 'doc_data_version', 'serial_id');
170 if (!$res || !$serial_id) {
171 $this->setError(_('Document Version')._(': ')._('Cannot create version.').' '.db_error());
175 if ($current_version) {
176 $res = db_query_params('UPDATE doc_data_version SET current_version = $1 WHERE docid = $2 AND version != $3',
177 array(0, $this->Document->getID(), $version));
179 $this->setOnUpdateError(_('Cannot update current version on creation.').' '.db_error());
189 * delete - delete a specific version
191 * @param int $verid The version to delete
192 * @return bool true on success
194 function delete($verid) {
196 $res = db_query_params('DELETE FROM doc_data_version WHERE docid = $1 and version = $2',
197 array($this->Document->getID(), $verid));
199 $this->setError(_('DocumentVersion')._(': ')._('Invalid version id').' '.db_error());
203 if ($this->getNumberOfVersions() == 1) {
204 $this->getMaxVersionData();
205 $this->update($this->data_array['version'], $this->data_array['title'], $this->data_array['description'], $this->data_array['filetype'], $this->data_array['filename'], $this->data_array['filesize'], 1);
208 db_free_result($res);
213 * getVersion - retrieve the data of a specific version
215 * @param int $version The version to retrive
216 * @return array the content of a version
218 function getVersion($version) {
219 $this->fetchData($version);
220 return $this->data_array;
224 * getMaxVersionID - get the highest value of version of a document
226 * @return int The max value.
228 function getMaxVersionID() {
229 $res = db_query_params('SELECT MAX(version) FROM doc_data_version WHERE docid = $1',
230 array($this->Document->getID()));
232 $arr = db_fetch_array($res);
239 * getMaxVersionData - retrieve the content of the highest version of the document
241 * @return array the content of a version.
243 function getMaxVersionData() {
244 return $this->fetchData($this->getMaxVersionID());
248 * getNumberOfVersions - get the number of versions of this document
250 * @return int Number of versions
252 function getNumberOfVersions() {
253 $res = db_query_params('SELECT COUNT(version) FROM doc_data_version WHERE docid = $1',
254 array($this->Document->getID()));
256 $arr = db_fetch_array($res);
262 * isURL - whether this document is a URL and not a local file.
264 * @return boolean is_url.
267 return ($this->data_array['filetype'] == 'URL');
270 function isCurrent() {
271 return $this->data_array['current_version'];
275 * update - Update an existing version of a document
277 * @param int $version The version id to update
278 * @param string $title The new title
279 * @param string $description The new description
280 * @param string $filetype The new filetype
281 * @param string $filename The new filename
282 * @param int $filesize The new filesize
283 * @param int $current_version Is the current version to set? Default is yes.
284 * @param int $updatetimestamp timestamp of this update
285 * @return bool true on success
287 function update($version, $title, $description, $filetype, $filename, $filesize, $current_version = 1, $updatetimestamp) {
289 $colArr = array('title', 'description', 'filetype', 'filename', 'filesize', 'current_version', 'updatedate');
290 $valArr = array(htmlspecialchars($title), htmlspecialchars($description), $filetype, $filename, $filesize, $current_version, $updatetimestamp);
291 if (!$this->setValueinDB($version, $colArr, $valArr)) {
295 if ($current_version) {
296 $res = db_query_params('UPDATE doc_data_version SET current_version = $1 WHERE docid = $2 AND version != $3',
297 array(0, $this->Document->getID(), $version));
299 $this->setOnUpdateError(_('Cannot set current_version.').' '.db_error());
309 * updateDataWords - update the indexation of content of a version of the document
311 * @param int $version The version to update
312 * @param string $data_words The content of the document as parsed
313 * @return bool true on success
315 function updateDataWords($version, $data_words) {
317 $colArr = array('data_words');
318 $valArr = array($data_words);
319 if (!$this->setValueinDB($version, $colArr, $valArr)) {
328 * setValueinDB - private function to update columns in db
330 * @param int $version the version id to update
331 * @param array $colArr the columns to update in array form array('col1', col2')
332 * @param array $valArr the values to store in array form array('val1', 'val2')
333 * @return boolean success or not
336 private function setValueinDB($version, $colArr, $valArr) {
337 if ((count($colArr) != count($valArr)) || !count($colArr) || !count($valArr)) {
338 $this->setOnUpdateError(_('wrong parameters'));
342 $qpa = db_construct_qpa(false, 'UPDATE doc_data_version SET ');
343 for ($i = 0; $i < count($colArr); $i++) {
344 switch ($colArr[$i]) {
352 case 'current_version': {
354 $qpa = db_construct_qpa($qpa, ',');
356 $qpa = db_construct_qpa($qpa, $colArr[$i]);
357 $qpa = db_construct_qpa($qpa, ' = $1 ', array($valArr[$i]));
361 $this->setOnUpdateError(_('wrong column name'));
366 $qpa = db_construct_qpa($qpa, ' WHERE version = $1 AND docid = $2',
367 array($version, $this->Document->getID()));
368 $res = db_query_qpa($qpa);
369 if (!$res || db_affected_rows($res) < 1) {
370 $this->setOnUpdateError(db_error());
373 for ($i = 0; $i < count($colArr); $i++) {
374 switch ($colArr[$i]) {
381 case 'current_version': {
382 $this->data_array[$colArr[$i]] = $valArr[$i];