3 * FusionForge document manager
5 * Copyright 2011-2012, Franck Villaume - TrivialDev
6 * Copyright (C) 2012 Alain Peyrat - Alcatel-Lucent
7 * Copyright 2013, French Ministry of Education
8 * http://fusionforge.org
10 * This file is part of FusionForge. FusionForge is free software;
11 * you can redistribute it and/or modify it under the terms of the
12 * GNU General Public License as published by the Free Software
13 * Foundation; either version 2 of the Licence, or (at your option)
16 * FusionForge is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 require_once $gfcommon.'include/Error.class.php';
27 require_once $gfcommon.'include/User.class.php';
28 require_once $gfcommon.'docman/DocumentGroup.class.php';
30 class DocumentManager extends Error {
33 * Associative array of data from db.
35 * @var array $data_array.
49 * @param object The Group object to which this document is associated.
50 * @return boolean success.
52 function __construct(&$Group) {
54 if (!$Group || !is_object($Group)) {
55 $this->setError('DocumentManager:: '. _('No Valid Group Object'));
58 if ($Group->isError()) {
59 $this->setError('DocumentManager:: '. $Group->getErrorMessage());
62 $this->Group =& $Group;
66 * getGroup - get the Group object this Document is associated with.
68 * @return Object The Group object.
70 function &getGroup() {
76 * getTrashID - the trash doc_group id for this DocumentManager.
78 * @return integer The trash doc_group id.
80 function getTrashID() {
81 if (isset($this->data_array['trashid']))
82 return $this->data_array['trashid'];
84 $res = db_query_params('SELECT doc_group from doc_groups
88 array('.trash', $this->Group->getID(), '2'));
89 if (db_numrows($res) == 1) {
90 $arr = db_fetch_array($res);
91 $this->data_array['trashid'] = $arr['doc_group'];
92 return $this->data_array['trashid'];
94 $dg = new DocumentGroup($this->Group);
95 $dg->create('.trash');
103 * cleanTrash - delete all items in trash for this DocumentManager
105 * @return boolean true on success
107 function cleanTrash() {
108 $trashId = $this->getTrashID();
109 if ($trashId !== -1) {
111 $result = db_query_params('select docid FROM doc_data WHERE stateid=$1 and group_id=$2', array('2', $this->Group->getID()));
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'));
122 while ($arr = db_fetch_array($result)) {
123 DocumentStorage::instance()->delete($arr['docid'])->commit();
132 * getTree - display recursively the content of the doc_group. Only doc_groups within doc_groups.
134 * @param integer the selected directory
135 * @param string the type of link in the menu
136 * @param integer the doc_group to start: default 0
138 function getTree($selecteddir, $linkmenu, $docGroupId = 0) {
139 global $g; // the master group of all the groups .... anyway.
140 $dg = new DocumentGroup($this->Group);
142 case "listtrashfile": {
151 $subGroupIdArr = $dg->getSubgroup($docGroupId, $stateId);
152 if (sizeof($subGroupIdArr)) {
153 foreach ($subGroupIdArr as $subGroupIdValue) {
154 $localDg = new DocumentGroup($this->Group, $subGroupIdValue);
155 $liclass = 'docman_li_treecontent';
156 if ($selecteddir == $localDg->getID()) {
157 $liclass = 'docman_li_treecontent_selected';
159 if ($this->Group->getID() != $g->getID()) {
160 $link = '/docman/?group_id='.$g->getID().'&view='.$linkmenu.'&dirid='.$localDg->getID().'&childgroup_id='.$this->Group->getID();
162 $link = '/docman/?group_id='.$this->Group->getID().'&view='.$linkmenu.'&dirid='.$localDg->getID();
165 $nbDocs = $localDg->getNumberOfDocuments($stateId);
166 if ($stateId == 1 && forge_check_perm('docman', $this->Group->getID(), 'approve')) {
167 $nbDocsPending = $localDg->getNumberOfDocuments(3);
168 $nbDocsHidden = $localDg->getNumberOfDocuments(4);
169 $nbDocsPrivate = $localDg->getNumberOfDocuments(5);
172 if ($stateId == 2 && forge_check_perm('docman', $this->Group->getID(), 'approve')) {
173 $nbDocsTrashed = $localDg->getNumberOfDocuments(2);
176 if ($nbDocs && (!isset($nbDocsPending) || $nbDocsPending == 0) && (!isset($nbDocsHidden) || $nbDocsHidden == 0) && (!isset($nbDocsPrivate) || $nbDocsPrivate) && (!isset($nbDocsTrashed) || $nbDocsTrashed)) {
177 $nbDocsLabel = '<span class="tabtitle-nw" title="'._('Number of documents in this folder').'" >('.$nbDocs.')</span>';
179 if (isset($nbDocsPending) && isset($nbDocsHidden) && isset($nbDocsPrivate)) {
180 $nbDocsLabel = '<span class="tabtitle-nw" title="'._('Number of documents in this folder per status. active/pending/hidden/private').'" >('.$nbDocs.'/'.$nbDocsPending.'/'.$nbDocsHidden.'/'.$nbDocsPrivate.')</span>';
182 if (isset($nbDocsTrashed)) {
183 $nbDocsLabel = '<span class="tabtitle-nw" title="'._('Number of deleted documents in this folder').'" >('.$nbDocsTrashed.')</span>';
185 if ($localDg->getName() != '.trash') {
186 $user = user_get_object($localDg->getCreated_by());
187 $lititle = _('Created_by:').$user->getRealName()._('; Last modified:').date(_('Y-m-d H:i'), $localDg->getLastModifyDate());
188 echo '<li id="leaf-'.$subGroupIdValue.'" class="'.$liclass.'">'.util_make_link($link, $localDg->getName(), array('class'=>'tabtitle-nw', 'title'=>$lititle)).$nbDocsLabel;
190 echo '<li id="leaf-'.$subGroupIdValue.'" class="'.$liclass.'">'.util_make_link($link, $localDg->getName()).$nbDocsLabel;
192 if ($dg->getSubgroup($subGroupIdValue, $stateId)) {
193 echo '<ul class="simpleTreeMenu" >';
194 $this->getTree($selecteddir, $linkmenu, $subGroupIdValue);
203 * getStatusNameList - get all status for documents
205 * @param string format of the return values. json returns : { name: id, }. Default is DB object.
206 * @param string skipped status id
208 function getStatusNameList($format = '', $removedval = '') {
209 if (!empty($removedval)) {
210 $stateQuery = db_query_params('select * from doc_states where stateid not in ($1) order by stateid', array($removedval));
212 $stateQuery = db_query_params('select * from doc_states order by stateid', array());
217 while ($stateArr = db_fetch_array($stateQuery)) {
218 $returnString .= util_html_secure($stateArr['name']).': \''.$stateArr['stateid'].'\',';
220 $returnString .= '}';
221 return $returnString;
230 function getDocGroupList($nested_groups, $format = '', $allow_none = true, $selected_id = 0, $dont_display = array()) {
232 $text_array = array();
233 $this->buildArrays($nested_groups, $id_array, $text_array, $dont_display);
234 $rows = count($id_array);
238 for ($i=0; $i<$rows; $i++) {
239 $returnString .= '\''.util_html_secure($text_array[$i]).'\':'.$id_array[$i].',';
241 $returnString .= '}';
245 return $returnString;
249 * buildArrays - Build the arrays to call html_build_select_box_from_arrays()
251 * @param array Array of groups.
252 * @param array Reference to the array of ids that will be build
253 * @param array Reference to the array of group names
254 * @param array Array of IDs of groups that should not be displayed
255 * @param int The ID of the parent whose childs are being showed (0 for root groups)
256 * @param int The current level
258 function buildArrays($group_arr, &$id_array, &$text_array, &$dont_display, $parent = 0, $level = 0) {
259 if (!is_array($group_arr) || !array_key_exists("$parent", $group_arr)) return;
261 $child_count = count($group_arr["$parent"]);
262 for ($i = 0; $i < $child_count; $i++) {
263 $doc_group =& $group_arr["$parent"][$i];
265 // Should we display this element?
266 if (in_array($doc_group->getID(), $dont_display)) continue;
268 $margin = str_repeat("--", $level);
270 $id_array[] = $doc_group->getID();
271 $text_array[] = $margin.$doc_group->getName();
273 // Show childs (if any)
274 $this->buildArrays($group_arr, $id_array, $text_array, $dont_display, $doc_group->getID(), $level+1);
279 * getActivity - return the number of searched actions per sections between two dates
281 * @param array Sections to search for activity
282 * @param int the start date time format time()
283 * @param int the end date time format time()
284 * @return array number per section of activities found between begin and end values
286 function getActivity($sections, $begin, $end) {
287 $qpa = db_construct_qpa(false);
288 for ($i = 0; $i < count($sections); $i++) {
290 if (count($sections) >= 1 && $i != count($sections) -1) {
293 $qpa = db_construct_qpa($qpa, 'SELECT count(*) FROM activity_vw WHERE activity_date BETWEEN $1 AND $2
294 AND group_id = $3 AND section = $4 ',
297 $this->getGroup()->getID(),
300 $qpa = db_construct_qpa($qpa, ' UNION ALL ', array());
303 $res = db_query_qpa($qpa);
306 while ($arr = db_fetch_array($res)) {
307 $results[$sections[$j]] = $arr['0'];