3 * FusionForge document manager
5 * Copyright 2011-2012, Franck Villaume - TrivialDev
6 * Copyright (C) 2012 Alain Peyrat - Alcatel-Lucent
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 __construct(&$Group) {
53 if (!$Group || !is_object($Group)) {
54 $this->setNotValidGroupObjectError();
57 if ($Group->isError()) {
58 $this->setError('DocumentManager:: '. $Group->getErrorMessage());
61 $this->Group =& $Group;
65 * getGroup - get the Group object this Document is associated with.
67 * @return Object The Group object.
69 function &getGroup() {
75 * getTrashID - the trash doc_group id for this DocumentManager.
77 * @return integer The trash doc_group id.
79 function getTrashID() {
80 if (isset($this->data_array['trashid']))
81 return $this->data_array['trashid'];
83 $res = db_query_params('SELECT doc_group from doc_groups
87 array('.trash', $this->Group->getID(), '2'));
88 if (db_numrows($res) == 1) {
89 $arr = db_fetch_array($res);
90 $this->data_array['trashid'] = $arr['doc_group'];
91 return $this->data_array['trashid'];
93 $dg = new DocumentGroup($this->Group);
94 $dg->create('.trash');
102 * cleanTrash - delete all items in trash for this DocumentManager
104 * @return boolean true on success
106 function cleanTrash() {
107 $trashId = $this->getTrashID();
108 if ($trashId !== -1) {
110 $emptyFile = db_query_params('DELETE FROM doc_data WHERE stateid=$1 and group_id=$2', array('2', $this->Group->getID()));
115 $emptyDir = db_query_params('DELETE FROM doc_groups WHERE stateid=$1 and group_id=$2 and groupname !=$3', array('2', $this->Group->getID(), '.trash'));
127 * getTree - display recursively the content of the doc_group. Only doc_groups within doc_groups.
129 * @param integer the selected directory
130 * @param string the type of link in the menu
131 * @param integer the doc_group to start: default 0
133 function getTree($selecteddir, $linkmenu, $docGroupId = 0) {
134 global $g; // the master group of all the groups .... anyway.
135 $dg = new DocumentGroup($this->Group);
137 case "listtrashfile": {
146 $subGroupIdArr = $dg->getSubgroup($docGroupId, $stateId);
147 if (sizeof($subGroupIdArr)) {
148 foreach ($subGroupIdArr as $subGroupIdValue) {
149 $localDg = new DocumentGroup($this->Group, $subGroupIdValue);
150 $liclass = 'docman_li_treecontent';
151 if ($selecteddir == $localDg->getID()) {
152 $liclass = 'docman_li_treecontent_selected';
154 if ($this->Group->getID() != $g->getID()) {
155 $link = '/docman/?group_id='.$g->getID().'&view='.$linkmenu.'&dirid='.$localDg->getID().'&childgroup_id='.$this->Group->getID();
157 $link = '/docman/?group_id='.$this->Group->getID().'&view='.$linkmenu.'&dirid='.$localDg->getID();
160 $nbDocs = $localDg->getNumberOfDocuments($stateId);
161 if ($stateId == 1 && forge_check_perm('docman', $this->Group->getID(), 'approve')) {
162 $nbDocsPending = $localDg->getNumberOfDocuments(3);
163 $nbDocsHidden = $localDg->getNumberOfDocuments(4);
164 $nbDocsPrivate = $localDg->getNumberOfDocuments(5);
167 if ($stateId == 2 && forge_check_perm('docman', $this->Group->getID(), 'approve')) {
168 $nbDocsTrashed = $localDg->getNumberOfDocuments(2);
171 if ($nbDocs && (!isset($nbDocsPending) || $nbDocsPending == 0) && (!isset($nbDocsHidden) || $nbDocsHidden == 0) && (!isset($nbDocsPrivate) || $nbDocsPrivate) && (!isset($nbDocsTrashed) || $nbDocsTrashed)) {
172 $nbDocsLabel = '<span class="tabtitle-nw" title="'._('Number of documents in this folder').'" >('.$nbDocs.')</span>';
174 if (isset($nbDocsPending) && isset($nbDocsHidden) && isset($nbDocsPrivate)) {
175 $nbDocsLabel = '<span class="tabtitle-nw" title="'._('Number of documents in this folder per status. active/pending/hidden/private').'" >('.$nbDocs.'/'.$nbDocsPending.'/'.$nbDocsHidden.'/'.$nbDocsPrivate.')</span>';
177 if (isset($nbDocsTrashed)) {
178 $nbDocsLabel = '<span class="tabtitle-nw" title="'._('Number of deleted documents in this folder').'" >('.$nbDocsTrashed.')</span>';
180 $user = user_get_object($localDg->getCreated_by());
181 $lititle = _('Created_by:').$user->getRealName()._('; Last modified:').date(_('Y-m-d H:i'), $localDg->getLastModifyDate());
182 echo '<li id="leaf-'.$subGroupIdValue.'" class="'.$liclass.'">'.util_make_link($link, $localDg->getName(), array('class'=>'tabtitle-nw', 'title'=>$lititle)).$nbDocsLabel;
183 if ($dg->getSubgroup($subGroupIdValue, $stateId)) {
185 $this->getTree($selecteddir, $linkmenu, $subGroupIdValue);
194 * getStatusNameList - get all status for documents
196 * @param string format of the return values. json returns : { name: id, }. Default is DB object.
197 * @param string skipped status id
199 function getStatusNameList($format = '', $removedval = '') {
200 if (!empty($removedval)) {
201 $stateQuery = db_query_params('select * from doc_states where stateid not in ($1) order by stateid', array($removedval));
203 $stateQuery = db_query_params('select * from doc_states order by stateid', array());
208 while ($stateArr = db_fetch_array($stateQuery)) {
209 $returnString .= util_html_secure($stateArr['name']).': \''.$stateArr['stateid'].'\',';
211 $returnString .= '}';
212 return $returnString;
221 function getDocGroupList($nested_groups, $format = '', $allow_none = true, $selected_id = 0, $dont_display = array()) {
223 $text_array = array();
224 $this->buildArrays($nested_groups, $id_array, $text_array, $dont_display);
225 $rows = count($id_array);
229 for ($i=0; $i<$rows; $i++) {
230 $returnString .= '\''.util_html_secure($text_array[$i]).'\':'.$id_array[$i].',';
232 $returnString .= '}';
236 return $returnString;
240 * buildArrays - Build the arrays to call html_build_select_box_from_arrays()
242 * @param array Array of groups.
243 * @param array Reference to the array of ids that will be build
244 * @param array Reference to the array of group names
245 * @param array Array of IDs of groups that should not be displayed
246 * @param int The ID of the parent whose childs are being showed (0 for root groups)
247 * @param int The current level
249 function buildArrays($group_arr, &$id_array, &$text_array, &$dont_display, $parent = 0, $level = 0) {
250 if (!is_array($group_arr) || !array_key_exists("$parent", $group_arr)) return;
252 $child_count = count($group_arr["$parent"]);
253 for ($i = 0; $i < $child_count; $i++) {
254 $doc_group =& $group_arr["$parent"][$i];
256 // Should we display this element?
257 if (in_array($doc_group->getID(), $dont_display)) continue;
259 $margin = str_repeat("--", $level);
261 $id_array[] = $doc_group->getID();
262 $text_array[] = $margin.$doc_group->getName();
264 // Show childs (if any)
265 $this->buildArrays($group_arr, $id_array, $text_array, $dont_display, $doc_group->getID(), $level+1);