CMS  Version 3.9
document_library.inc
Go to the documentation of this file.
1 <?php
7 /**************************************************************
8 
9  Copyright (c) 2010 Sonjara, Inc
10 
11  Permission is hereby granted, free of charge, to any person
12  obtaining a copy of this software and associated documentation
13  files (the "Software"), to deal in the Software without
14  restriction, including without limitation the rights to use,
15  copy, modify, merge, publish, distribute, sublicense, and/or sell
16  copies of the Software, and to permit persons to whom the
17  Software is furnished to do so, subject to the following
18  conditions:
19 
20  The above copyright notice and this permission notice shall be
21  included in all copies or substantial portions of the Software.
22 
23  Except as contained in this notice, the name(s) of the above
24  copyright holders shall not be used in advertising or otherwise
25  to promote the sale, use or other dealings in this Software
26  without prior written authorization.
27 
28  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
30  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
32  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
33  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
34  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
35  OTHER DEALINGS IN THE SOFTWARE.
36 
37 *****************************************************************/
38 
39 Fakoli::using("fileshare");
40 
41 class DocumentLibrary extends DataItem
42 {
43  // Fields
44 
45  var $fields = array("document_library_id" => Number,
46  "name" => String,
47  "identifier" => String,
48  "description" => Text,
49  "hidden" => Boolean,
50  "allow_comments" => Boolean,
51  "owner_id" => Number,
52  "allow_access" => String,
53  "manage_folders" => String,
54  "upload_files" => String,
55  "delete_files" => String,
56  "create_date" => Timestamp,
57  "last_modified" => Timestamp);
58 
59  var $relations = array( "Owner" => "",
60  "Documents" => Document,
61  "Folders" => DocumentFolder,
62  "FileshareUsers" => FileshareUserXref
63 
64  );
65 
66 
67  function Owner()
68  {
69  if(!$this->owner_id)
70  return null;
71 
72  $mgr = new UserManager();
73  return $mgr->getUser($this->owner_id);
74  }
75 
76  function isOwner()
77  {
78  global $user;
79 
80  if(!$user)
81  return false;
82 
83  $owner = $this->Owner();
84 
85  return ($owner->user_id == $user->user_id) ? true : false;
86  }
87 
88 
89  function Documents($constraint = "")
90  {
91  return $this->getRelatedList(Document, "", $constraint);
92  }
93 
94  function Folders($constraint = "")
95  {
96  return $this->getRelatedList(DocumentFolder, "", $constraint);
97  }
98 
99  function getOwnerName()
100  {
101  $owner = $this->Owner();
102 
103  return ($owner) ? $owner->getFullName() : "";
104  }
105 
107  {
108  global $config;
109 
110  $d = $config['uploadbase'] . DIRECTORY_SEPARATOR . "library_{$this->document_library_id}";
111 
112  if (!is_dir($d) && !file_exists($d))
113  {
114  $docs = $d.DIRECTORY_SEPARATOR."documents";
115  mkdir($d);
116  mkdir($docs);
117  chmod($docs, 0777);
118  }
119 
120  return $d;
121  }
122 
123  function getFolders($constraint = "")
124  {
125  return reindexList($this->Folders($constraint), "path");
126  }
127 
128  function countDocuments()
129  {
130  return queryValue(Document, "COUNT(1)", "WHERE document_library_id={$this->document_library_id}");
131  }
132 
133  static function findByIdentifier($identifier)
134  {
135  $match = Query::create(DocumentLibrary, "WHERE identifier=:id")
136  ->bind(":id", $identifier)
137  ->executeSingle();
138 
139  return $match;
140  }
141 
143  {
144  return $this->getRelatedList(FileshareUserXref, "document_library_id", $constraint);
145  }
146 
148  {
149  $xrefs = $this->FileshareUsers("WHERE user_id = $user_id");
150  return (count($xrefs) > 0) ? $xrefs[0] : null;
151  }
152 
153  /*
154  * Check fileshare user-level access
155  */
156  function allowAccess()
157  {
158  global $user;
159 
160  if(!$user)
161  $user = FileshareManager::setVisitor();
162 
163  if($this->isOwner() || checkRole($this->allow_access) || $this->isFileshareMember($user->user_id))
164  {
165  return true;
166  }
167  return false;
168  }
169 
175  function allowDelete()
176  {
177  if(!$this->document_library_id) return false;
178 
179  return (!count($this->Documents()) && checkRole($this->delete_files)) ? true : false;
180  }
181 
182  function allowManageFiles()
183  {
184  global $user;
185 
186  if(!$user)
187  return false;
188 
189  if($this->isOwner() || checkRole($this->manage_folders) || $this->isFileshareMember())
190  {
191  return true;
192  }
193  return false;
194 
195  }
196 
198  {
199  global $user;
200 
201  if(!$user)
202  return false;
203 
204  if($this->isOwner() || checkRole($this->upload_files) || $this->isFileshareMember())
205  return true;
206  else
207  return false;
208  }
209 
210 
211  function isFileshareMember()
212  {
213  global $user;
214 
215  $member = $this->getFileshareUser($user->user_id);
216 
217  return ($member) ? true : false;
218  }
219 
220 
221  /*
222  * Set admin role as default for full access rights to
223  * new libraries. If creating a library in the fileshare
224  * space, also allow "fileshare" role full
225  * access rights.
226  */
227  static function getDefaultRole($fileshare = false)
228  {
229  $identifier = $_REQUEST["identifier"];
230 
231  if(preg_match("/fileshare/i", $identifier) || $fileshare)
232  $constraint = "WHERE role like 'admin%' or role like 'fileshare%'";
233  else
234  $constraint = "WHERE role like 'admin%'";
235 
236  $roles = Query::create(SiteRole, $constraint)->execute();
237 
238  if(count($roles) > 0)
239  $role = formatItems($roles, "{role}", ",");
240 
241  return $role;
242  }
243 
245  {
246  $path = $this->getLibraryDirectory() . DIRECTORY_SEPARATOR . $document->file;
247 
248  if (file_exists($path))
249  {
250  return getScaledSize(filesize($path));
251  }
252 
253  return "";
254  }
255 
256  function DocumentLibrary()
257  {
258  $this->primary_key = "document_library_id";
259  $this->table = "document_library";
260 
261  $this->default_format = "{name}";
262  $this->cacheLocal = true;
263 
264  // Patch in the user class, since this can be overridden by the application
265  $mgr = new UserManager();
266  $this->relations["Owner"] = $mgr->getUserClass();
267 
268  $this->DataItem(func_get_args());
269  }
270 }?>
$constraint
$user_id
$calendar owner_id
if(! $page) $path
Definition: page.inc:57
This class maps the publication table.
Definition: document.inc:43
static findByIdentifier($identifier)
FileshareUsers($constraint="")
getFileshareUser($user_id)
getFolders($constraint="")
Documents($constraint="")
getFileSize($document)
allowDelete()
Can only delete an empty library.
Folders($constraint="")
static getDefaultRole($fileshare=false)
static using()
Import the datamodels, views and manifest for the specified component(s).
Definition: core.inc:116
Provides the interface to the user model for the application.
global $user
if(! $document_id) $document
Definition: delete.inc:43
if(!checkRole($library->allow_access) &&! $library->allowAccess()) if(!checkRole($document->allow_access)) $d
Definition: download.inc:66
global $config
Definition: import.inc:4
$identifier
$table column("Redirect From", "<a href='redirect_form?redirect_id={redirect_id}'>{redirect_from}</a>", true, "width: 30%") -> column("Redirect To", "<a href='{redirect_to}' target='_blank'>{redirect_to}</a>", true, "width: 30%") ->column("Last Modified", "{last_modified}", true, "width: 20%; text-align: center") ->column("Override", "{ override true
Definition: redirects.inc:9
$role
Definition: role_form.inc:41