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->setError('DocumentManager:: '. _('No Valid Group Object'));
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 $result = db_query_params('select docid FROM doc_data WHERE stateid=$1 and group_id=$2', array('2', $this->Group->getID()));
111 $emptyFile = db_query_params('DELETE FROM doc_data WHERE stateid=$1 and group_id=$2', array('2', $this->Group->getID()));
116 $emptyDir = db_query_params('DELETE FROM doc_groups WHERE stateid=$1 and group_id=$2 and groupname !=$3', array('2', $this->Group->getID(), '.trash'));
121 while ($arr = db_fetch_array($result)) {
122 DocumentStorage::instance()->delete($arr['docid'])->commit();
131 * getTree - display recursively the content of the doc_group. Only doc_groups within doc_groups.
133 * @param integer the selected directory
134 * @param string the type of link in the menu
135 * @param integer the doc_group to start: default 0
137 function getTree($selecteddir, $linkmenu, $docGroupId = 0) {
138 global $g; // the master group of all the groups .... anyway.
139 $dg = new DocumentGroup($this->Group);
141 case "listtrashfile": {
150 $subGroupIdArr = $dg->getSubgroup($docGroupId, $stateId);
151 if (sizeof($subGroupIdArr)) {
152 foreach ($subGroupIdArr as $subGroupIdValue) {
153 $localDg = new DocumentGroup($this->Group, $subGroupIdValue);
154 $liclass = 'docman_li_treecontent';
155 if ($selecteddir == $localDg->getID()) {
156 $liclass = 'docman_li_treecontent_selected';
158 if ($this->Group->getID() != $g->getID()) {
159 $link = '/docman/?group_id='.$g->getID().'&view='.$linkmenu.'&dirid='.$localDg->getID().'&childgroup_id='.$this->Group->getID();
161 $link = '/docman/?group_id='.$this->Group->getID().'&view='.$linkmenu.'&dirid='.$localDg->getID();
164 $nbDocs = $localDg->getNumberOfDocuments($stateId);
165 if ($stateId == 1 && forge_check_perm('docman', $this->Group->getID(), 'approve')) {
166 $nbDocsPending = $localDg->getNumberOfDocuments(3);
167 $nbDocsHidden = $localDg->getNumberOfDocuments(4);
168 $nbDocsPrivate = $localDg->getNumberOfDocuments(5);
171 if ($stateId == 2 && forge_check_perm('docman', $this->Group->getID(), 'approve')) {
172 $nbDocsTrashed = $localDg->getNumberOfDocuments(2);
175 if ($nbDocs && (!isset($nbDocsPending) || $nbDocsPending == 0) && (!isset($nbDocsHidden) || $nbDocsHidden == 0) && (!isset($nbDocsPrivate) || $nbDocsPrivate) && (!isset($nbDocsTrashed) || $nbDocsTrashed)) {
176 $nbDocsLabel = '<span class="tabtitle-nw" title="'._('Number of documents in this folder').'" >('.$nbDocs.')</span>';
178 if (isset($nbDocsPending) && isset($nbDocsHidden) && isset($nbDocsPrivate)) {
179 $nbDocsLabel = '<span class="tabtitle-nw" title="'._('Number of documents in this folder per status. active/pending/hidden/private').'" >('.$nbDocs.'/'.$nbDocsPending.'/'.$nbDocsHidden.'/'.$nbDocsPrivate.')</span>';
181 if (isset($nbDocsTrashed)) {
182 $nbDocsLabel = '<span class="tabtitle-nw" title="'._('Number of deleted documents in this folder').'" >('.$nbDocsTrashed.')</span>';
184 if ($localDg->getName() != '.trash') {
185 $user = user_get_object($localDg->getCreated_by());
186 $lititle = _('Created_by:').$user->getRealName()._('; Last modified:').date(_('Y-m-d H:i'), $localDg->getLastModifyDate());
187 echo '<li id="leaf-'.$subGroupIdValue.'" class="'.$liclass.'">'.util_make_link($link, $localDg->getName(), array('class'=>'tabtitle-nw', 'title'=>$lititle)).$nbDocsLabel;
189 echo '<li id="leaf-'.$subGroupIdValue.'" class="'.$liclass.'">'.util_make_link($link, $localDg->getName()).$nbDocsLabel;
191 if ($dg->getSubgroup($subGroupIdValue, $stateId)) {
193 $this->getTree($selecteddir, $linkmenu, $subGroupIdValue);
202 * getStatusNameList - get all status for documents
204 * @param string format of the return values. json returns : { name: id, }. Default is DB object.
205 * @param string skipped status id
207 function getStatusNameList($format = '', $removedval = '') {
208 if (!empty($removedval)) {
209 $stateQuery = db_query_params('select * from doc_states where stateid not in ($1) order by stateid', array($removedval));
211 $stateQuery = db_query_params('select * from doc_states order by stateid', array());
216 while ($stateArr = db_fetch_array($stateQuery)) {
217 $returnString .= util_html_secure($stateArr['name']).': \''.$stateArr['stateid'].'\',';
219 $returnString .= '}';
220 return $returnString;
229 function getDocGroupList($nested_groups, $format = '', $allow_none = true, $selected_id = 0, $dont_display = array()) {
231 $text_array = array();
232 $this->buildArrays($nested_groups, $id_array, $text_array, $dont_display);
233 $rows = count($id_array);
237 for ($i=0; $i<$rows; $i++) {
238 $returnString .= '\''.util_html_secure($text_array[$i]).'\':'.$id_array[$i].',';
240 $returnString .= '}';
244 return $returnString;
248 * buildArrays - Build the arrays to call html_build_select_box_from_arrays()
250 * @param array Array of groups.
251 * @param array Reference to the array of ids that will be build
252 * @param array Reference to the array of group names
253 * @param array Array of IDs of groups that should not be displayed
254 * @param int The ID of the parent whose childs are being showed (0 for root groups)
255 * @param int The current level
257 function buildArrays($group_arr, &$id_array, &$text_array, &$dont_display, $parent = 0, $level = 0) {
258 if (!is_array($group_arr) || !array_key_exists("$parent", $group_arr)) return;
260 $child_count = count($group_arr["$parent"]);
261 for ($i = 0; $i < $child_count; $i++) {
262 $doc_group =& $group_arr["$parent"][$i];
264 // Should we display this element?
265 if (in_array($doc_group->getID(), $dont_display)) continue;
267 $margin = str_repeat("--", $level);
269 $id_array[] = $doc_group->getID();
270 $text_array[] = $margin.$doc_group->getName();
272 // Show childs (if any)
273 $this->buildArrays($group_arr, $id_array, $text_array, $dont_display, $doc_group->getID(), $level+1);
278 * getActivity - return the number of searched actions per sections between two dates
280 * @param array Sections to search for activity
281 * @param int the start date time format time()
282 * @param int the end date time format time()
283 * @return array number per section of activities found between begin and end values
285 function getActivity($sections, $begin, $end) {
286 $qpa = db_construct_qpa(false);
287 for ($i = 0; $i < count($sections); $i++) {
289 if (count($sections) >= 1 && $i != count($sections) -1) {
292 $qpa = db_construct_qpa($qpa, 'SELECT count(*) FROM activity_vw WHERE activity_date BETWEEN $1 AND $2
293 AND group_id = $3 AND section = $4 ',
296 $this->getGroup()->getID(),
299 $qpa = db_construct_qpa($qpa, ' UNION ALL ', array());
302 $res = db_query_qpa($qpa);
305 while ($arr = db_fetch_array($res)) {
306 $results[$sections[$j]] = $arr['0'];