3 * FusionForge document manager
5 * Copyright 2011-2012, Franck Villaume - TrivialDev
6 * Copyright 2013, French Ministry of Education
7 * http://fusionforge.org
9 * This file is part of FusionForge. FusionForge is free software;
10 * you can redistribute it and/or modify it under the terms of the
11 * GNU General Public License as published by the Free Software
12 * Foundation; either version 2 of the Licence, or (at your option)
15 * FusionForge is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 require_once $gfcommon.'include/Error.class.php';
26 require_once $gfcommon.'include/User.class.php';
27 require_once $gfcommon.'docman/DocumentGroup.class.php';
29 class DocumentManager extends Error {
32 * Associative array of data from db.
34 * @var array $data_array.
48 * @param object The Group object to which this document is associated.
49 * @return boolean success.
51 function DocumentManager(&$Group) {
53 if (!$Group || !is_object($Group)) {
54 $this->setError('DocumentManager:: '. _('No Valid Group Object'));
57 if ($Group->isError()) {
58 $this->setError('DocumentManager:: '. $Group->getErrorMessage());
61 $this->Group =& $Group;
67 * getGroup - get the Group object this Document is associated with.
69 * @return Object The Group object.
71 function &getGroup() {
77 * getTrashID - the trash doc_group id for this DocumentManager.
79 * @return integer The trash doc_group id.
81 function getTrashID() {
82 if (isset($this->data_array['trashid']))
83 return $this->data_array['trashid'];
85 $res = db_query_params('SELECT doc_group from doc_groups
89 array('.trash', $this->Group->getID(), '2'));
90 if (db_numrows($res) == 1) {
91 $arr = db_fetch_array($res);
92 $this->data_array['trashid'] = $arr['doc_group'];
93 return $this->data_array['trashid'];
95 $dg = new DocumentGroup($this->Group);
96 $dg->create('.trash');
104 * cleanTrash - delete all items in trash for this DocumentManager
106 * @return boolean true on success
108 function cleanTrash() {
109 $trashId = $this->getTrashID();
110 if ($trashId !== -1) {
112 $emptyFile = db_query_params('DELETE FROM doc_data WHERE stateid=$1 and group_id=$2', array('2', $this->Group->getID()));
117 $emptyDir = db_query_params('DELETE FROM doc_groups WHERE stateid=$1 and group_id=$2 and groupname !=$3', array('2', $this->Group->getID(), '.trash'));
129 * getTree - display recursively the content of the doc_group. Only doc_groups within doc_groups.
131 * @param integer the selected directory
132 * @param string the type of link in the menu
133 * @param integer the doc_group to start: default 0
135 function getTree($selecteddir, $linkmenu, $docGroupId = 0) {
136 global $g; // the master group of all the groups .... anyway.
137 $dg = new DocumentGroup($this->Group);
139 case "listtrashfile": {
148 $subGroupIdArr = $dg->getSubgroup($docGroupId, $stateId);
149 if (sizeof($subGroupIdArr)) {
150 foreach ($subGroupIdArr as $subGroupIdValue) {
151 $localDg = new DocumentGroup($this->Group, $subGroupIdValue);
152 $liclass = 'docman_li_treecontent';
153 if ($selecteddir == $localDg->getID()) {
154 $liclass = 'docman_li_treecontent_selected';
156 if ($this->Group->getID() != $g->getID()) {
157 $link = '/docman/?group_id='.$g->getID().'&view='.$linkmenu.'&dirid='.$localDg->getID().'&childgroup_id='.$this->Group->getID();
159 $link = '/docman/?group_id='.$this->Group->getID().'&view='.$linkmenu.'&dirid='.$localDg->getID();
162 $nbDocs = $localDg->getNumberOfDocuments($stateId);
163 if ($stateId == 1 && forge_check_perm('docman', $this->Group->getID(), 'approve')) {
164 $nbDocsPending = $localDg->getNumberOfDocuments(3);
165 $nbDocsHidden = $localDg->getNumberOfDocuments(4);
166 $nbDocsPrivate = $localDg->getNumberOfDocuments(5);
169 if ($stateId == 2 && forge_check_perm('docman', $this->Group->getID(), 'approve')) {
170 $nbDocsTrashed = $localDg->getNumberOfDocuments(2);
173 if ($nbDocs && (!isset($nbDocsPending) || $nbDocsPending == 0) && (!isset($nbDocsHidden) || $nbDocsHidden == 0) && (!isset($nbDocsPrivate) || $nbDocsPrivate) && (!isset($nbDocsTrashed) || $nbDocsTrashed)) {
174 $nbDocsLabel = '<span class="tabtitle-nw" title="'._('Number of documents in this folder').'" >('.$nbDocs.')</span>';
176 if (isset($nbDocsPending) && isset($nbDocsHidden) && isset($nbDocsPrivate)) {
177 $nbDocsLabel = '<span class="tabtitle-nw" title="'._('Number of documents in this folder per status. active/pending/hidden/private').'" >('.$nbDocs.'/'.$nbDocsPending.'/'.$nbDocsHidden.'/'.$nbDocsPrivate.')</span>';
179 if (isset($nbDocsTrashed)) {
180 $nbDocsLabel = '<span class="tabtitle-nw" title="'._('Number of deleted documents in this folder').'" >('.$nbDocsTrashed.')</span>';
182 if ($localDg->getName() != '.trash') {
183 $user = user_get_object($localDg->getCreated_by());
184 $lititle = _('Created_by:').$user->getRealName()._('; Last modified:').date(_('Y-m-d H:i'), $localDg->getLastModifyDate());
185 echo '<li id="leaf-'.$subGroupIdValue.'" class="'.$liclass.'">'.util_make_link($link, $localDg->getName(), array('class'=>'tabtitle-nw', 'title'=>$lititle)).$nbDocsLabel;
187 echo '<li id="leaf-'.$subGroupIdValue.'" class="'.$liclass.'">'.util_make_link($link, $localDg->getName()).$nbDocsLabel;
189 if ($dg->getSubgroup($subGroupIdValue, $stateId)) {
190 echo '<ul class="simpleTreeMenu" >';
191 $this->getTree($selecteddir, $linkmenu, $subGroupIdValue);
200 * getStatusNameList - get all status for documents
202 * @param string format of the return values. json returns : { name: id, }. Default is DB object.
203 * @param string skipped status id
205 function getStatusNameList($format = '', $removedval = '') {
206 if (!empty($removedval)) {
207 $stateQuery = db_query_params('select * from doc_states where stateid not in ($1) order by stateid', array($removedval));
209 $stateQuery = db_query_params('select * from doc_states order by stateid', array());
214 while ($stateArr = db_fetch_array($stateQuery)) {
215 $returnString .= util_html_secure($stateArr['name']).': \''.$stateArr['stateid'].'\',';
217 $returnString .= '}';
218 return $returnString;
227 function getDocGroupList($nested_groups, $format = '', $allow_none = true, $selected_id = 0, $dont_display = array()) {
229 $text_array = array();
230 $this->buildArrays($nested_groups, $id_array, $text_array, $dont_display);
231 $rows = count($id_array);
235 for ($i=0; $i<$rows; $i++) {
236 $returnString .= '\''.util_html_secure($text_array[$i]).'\':'.$id_array[$i].',';
238 $returnString .= '}';
242 return $returnString;
246 * buildArrays - Build the arrays to call html_build_select_box_from_arrays()
248 * @param array Array of groups.
249 * @param array Reference to the array of ids that will be build
250 * @param array Reference to the array of group names
251 * @param array Array of IDs of groups that should not be displayed
252 * @param int The ID of the parent whose childs are being showed (0 for root groups)
253 * @param int The current level
255 function buildArrays($group_arr, &$id_array, &$text_array, &$dont_display, $parent = 0, $level = 0) {
256 if (!is_array($group_arr) || !array_key_exists("$parent", $group_arr)) return;
258 $child_count = count($group_arr["$parent"]);
259 for ($i = 0; $i < $child_count; $i++) {
260 $doc_group =& $group_arr["$parent"][$i];
262 // Should we display this element?
263 if (in_array($doc_group->getID(), $dont_display)) continue;
265 $margin = str_repeat("--", $level);
267 $id_array[] = $doc_group->getID();
268 $text_array[] = $margin.$doc_group->getName();
270 // Show childs (if any)
271 $this->buildArrays($group_arr, $id_array, $text_array, $dont_display, $doc_group->getID(), $level+1);