aa8c527a5fb5deb1469f6ed337429440717f7793
[gosa.git] / gosa-plugins / groupware / admin / groups / GroupwareSharedFolder / class_GroupwareSharedFolder.inc
1 <?php
3 # Load Groupware defintions and ACLs
4 new GroupwareDefinitions();
6 /*! \brief Allows to update groupware shared-folders
7 * using the jsonRPC backend.
8 */
9 class GroupwareSharedFolder extends plugin
10 {
11 public $view_logged = FALSE;
13 // Error hanlding related attributes.
14 private $initialized = FALSE;
15 private $rpcError = FALSE;
16 private $rpcErrorMessage = "";
18 // Attribute definition
19 public $attributes = array('folderList');
20 public $folderList = array();
22 // Feature handling
23 private $featuresEnabled = array();
24 private $FolderWidget = NULL;
25 private $folderPrefix = "shared/";
28 /*! \brief Constructs the plugin, loads required parent values
29 * and initiates the initialization.
30 */
31 function __construct($config, $dn, $attrs = NULL)
32 {
33 plugin::plugin($config, $dn, $attrs);
35 // Get attributes from parent object
36 $this->cn = "";
37 if(isset($this->attrs['cn'])){
38 $this->cn = $this->attrs['cn'][0];
39 }
40 $this->orig_cn = $this->cn;
42 // Initialize the distribution list using the gosa-ng backend
43 $this->init();
44 }
48 /*! \brief Check whether a feature is enabled or not.
49 * @param The feature name to check for
50 * @return TRUE on success else FALSE
51 */
52 function featureEnabled($name)
53 {
54 return(isset($this->featuresEnabled[$name]) && $this->featuresEnabled[$name]);
55 }
58 /*! \brief Try to initialize the plugin using a json-rpc connection
59 * to the gosa-ng server.
60 */
61 function init()
62 {
63 // Detect supported capabilities
64 $rpc = $this->config->getRpcHandle();
65 $capabilities = $rpc->gwGetCapabilities();
66 if(!$rpc->success()){
67 $this->rpcError = TRUE;
68 $this->rpcErrorMessage = $rpc->get_error();
69 $message = sprintf(_("Failed to load supported capabilities from server! Error was: '%s'."),
70 $rpc->get_error());
71 msg_dialog::display(_("Error"),msgPool::rpcError($message), ERROR_DIALOG);
72 return;
73 }
75 // Detect features we can use
76 $map['folder'] = array("folderList","folderAdd","folderDel","folderExists");
77 $map['members'] = array("folderGetMembers","folderSetMembers","folderAddMember","folderDelMember");
78 foreach($map as $name => $required){
79 $this->featuresEnabled[$name] = TRUE;
80 foreach($required as $func){
81 $this->featuresEnabled[$name] &= isset($capabilities[$func]) && $capabilities[$func];
82 }
83 }
85 // If we're creating a new ogroup, then we definately have no extension yet.
86 $this->rpcError = FALSE;
87 $folderList = array();
88 $is_account = FALSE;
89 if($this->cn == "" || $this->dn == "new"){
90 $is_account = FALSE;
91 }else{
93 // Load folders for the given folderPrefix
94 $this->folderPrefix = "shared/{$this->cn}";
95 $folders = $rpc->gwFolderList($this->folderPrefix);
96 if(!$rpc->success()){
97 $this->rpcError = TRUE;
98 $this->rpcErrorMessage = $rpc->get_error();
99 $message = sprintf(_("Failed to load folder list! Error was: '%s'."),
100 $rpc->get_error());
101 msg_dialog::display(_("Error"),msgPool::rpcError($message), ERROR_DIALOG);
102 return;
103 }
105 // We've avalid account, if we've received at least one matching folder.
106 $is_account = count($folders) != 0;
108 // Prepare folders to be used in the folderWidget
109 if($is_account){
110 $folderList = array();
111 foreach($folders as $path){
112 $name = preg_replace("/^.*\//","",$path);
113 $entry = array();
114 $entry['name'] = $name;
115 $entry['status'] = '';
116 $entry['acls'] = array();
117 $acls = array();
118 $acls = $rpc->gwFolderGetMembers($path);
119 if(!$rpc->success()){
120 $this->rpcError = TRUE;
121 $this->rpcErrorMessage = $rpc->get_error();
122 $message = sprintf(_("Failed to load permissions for folder '%s'! Error was: '%s'."),
123 $path,$rpc->get_error());
124 msg_dialog::display(_("Error"),msgPool::rpcError($message), ERROR_DIALOG);
125 return;
126 }
127 foreach($acls as $name => $acl){
128 $entry['acls'][] = array('type' => 'user', 'acl' => $acl, 'name' => $name);
129 }
130 $folderList[$path] = $entry;
131 }
132 }
133 }
135 // We do not have a valid account, use an initial set of folders
136 // to be able to use the folderWidget.
137 // {%cn} will be replaced by the groups cn when save() is called.
138 if(!$is_account){
139 $this->folderPrefix = "shared/{%cn}";
140 $intialFolderEntry = array('name' => '{%cn}', 'status' => 'added', 'acls' => array());
141 $folderList = array();
142 $folderList[$this->folderPrefix] = $intialFolderEntry;
143 }
145 // Store values as current and initial values (saved_attributes)
146 // to be able to keep track och changes.
147 $this->is_account = $this->initially_was_account = $is_account;
148 $this->saved_attributes = array();
149 $this->folderList = $this->saved_attributes['folderList'] = $folderList;
150 $this->capabilities = $capabilities;
151 $this->initialized = TRUE;
152 }
155 function execute()
156 {
157 plugin::execute();
159 // Initialization failed - Display a stripped template which allows
160 // to retry initialization
161 if(!$this->initialized){
162 $smarty = get_smarty();
163 $smarty->assign('rpcError' , $this->rpcError);
164 $smarty->assign('rpcErrorMessage' , $this->rpcErrorMessage);
165 return($smarty->fetch(get_template_path('GroupwareSharedFolder/initFailed.tpl', TRUE)));
166 }
168 // Log account access
169 if($this->is_account && !$this->view_logged){
170 $this->view_logged = TRUE;
171 new log("view","ogroups/".get_class($this),$this->dn);
172 }
174 // Allow to add or remove the distribution list extension
175 if(isset($_POST['modify_state'])){
176 if($this->is_account && $this->acl_is_removeable()){
177 $this->is_account= FALSE;
178 }elseif(!$this->is_account && $this->acl_is_createable()){
179 $this->is_account= TRUE;
180 }
181 }
183 // Show account status-changer
184 if ($this->parent !== NULL){
185 if ($this->is_account){
186 $display= $this->show_disable_header(_("Remove shared folder"),
187 msgPool::featuresEnabled(_("Shared folder")));
188 } else {
189 $display= $this->show_enable_header(_("Create shared folder"),
190 msgPool::featuresDisabled(_("Shared folder")));
191 return ($display);
192 }
193 }
195 /****************
196 Folder editor
197 ****************/
200 if(!$this->FolderWidget){
201 $this->FolderWidget = new FolderWidget($this->config,$this->folderList, $this->folderPrefix);
202 $this->FolderWidget->setPermissions(GroupwareDefinitions::getPermissions());
203 $this->FolderWidget->acl_base = $this->acl_base;
204 $this->FolderWidget->acl_category = $this->acl_category;
205 $this->FolderWidget->showButtons(FALSE);
206 }
207 $this->folderList = $this->FolderWidget->save();
208 $str = $this->FolderWidget->execute();
209 $this->dialog = $this->FolderWidget->dialogOpened();
210 return($str);
213 # ONCE WE HAVE MORE FEATURE THAN JUST THE FOLDER SETTINGS,
214 # ENABLE THIS AGAIN AN REMOVE THE ABOVE LINES.
215 #
216 # if(isset($_POST['FolderWidget_cancel'])) $this->FolderWidget = NULL;
217 # if(isset($_POST['FolderWidget_ok'])){
218 # $this->FolderWidget->save_object();
219 # $msgs = $this->FolderWidget->check();
220 # if(count($msgs)){
221 # msg_dialog::displayChecks($msgs);
222 # }else{
223 # $this->folderList = $this->FolderWidget->save();
224 # $this->FolderWidget = NULL;
225 # }
226 # }
227 # if(isset($_POST['configureFolder'])){
228 # $this->FolderWidget = new FolderWidget($this->config,$this->folderList, $this->folderPrefix);
229 # $this->FolderWidget->setPermissions(GroupwareDefinitions::getPermissions());
230 # $this->FolderWidget->acl_base = $this->acl_base;
231 # $this->FolderWidget->acl_category = $this->acl_category;
232 # }
233 # $this->dialog = FALSE;
234 # if($this->FolderWidget instanceOf FolderWidget){
235 # $this->FolderWidget->save_object();
236 # $this->dialog = TRUE;
237 # return($this->FolderWidget->execute());
238 # }
239 #
241 /****************
242 Generate HTML output
243 ****************/
245 $plInfo = $this->plInfo();
247 $smarty = get_smarty();
249 foreach($plInfo['plProvidedAcls'] as $name => $translation){
250 $smarty->assign("{$name}ACL", $this->getacl($name));
251 }
253 $smarty->assign('rpcError' , $this->rpcError);
254 $smarty->assign('rpcErrorMessage' , $this->rpcErrorMessage);
255 return($display.$smarty->fetch(get_template_path('GroupwareSharedFolder/generic.tpl', TRUE)));
256 }
259 /*! \brief Get posted values and check which are interesting for us.
260 */
261 function save_object()
262 {
263 if(isset($_POST['retryInit'])){
264 $this->init();
265 }
266 if(isset($_POST['GroupwareSharedFolder_posted'])){
267 plugin::save_object();
268 }
269 if($this->FolderWidget) $this->FolderWidget->save_object();
270 }
273 /*! \brief Removes the distribution list extension for the current
274 * object group.
275 */
276 function remove_from_parent()
277 {
278 echo "<br>Remove all folders";
279 }
282 /*! \brief Saves the distribution list extension for the current
283 * object group.
284 */
285 function save()
286 {
287 $this->folderList = $this->FolderWidget->save();
288 $rpc = $this->config->getRpcHandle();
290 // Walk through folders and perform requested actions
291 foreach($this->folderList as $path => $folder){
293 // Update folder names for newly created groupware extensions.
294 $path = preg_replace("/\{%cn\}/", $this->cn, $path);
295 $folder['name'] = preg_replace("/\{%cn\}/", $this->cn, $folder['name']);
297 // Remove shared folder.
298 if($folder['status'] == 'removed'){
299 $rpc->gwFolderDel($path);
300 if(!$rpc->success()){
301 $message = sprintf(_("Failed to remove shared folder '%s'! Error was: '%s'."),
302 $path, $rpc->get_error());
303 msg_dialog::display(_("Error"),msgPool::rpcError($message), ERROR_DIALOG);
304 }
306 echo "<br>Removed {$path}";
307 continue;
308 }
310 // Add newly created folders
311 if($folder['status'] == 'added'){
312 $rpc->gwFolderAdd($path);
313 if(!$rpc->success()){
314 $message = sprintf(_("Failed to create shared folder '%s'! Error was: '%s'."),
315 $path, $rpc->get_error());
316 msg_dialog::display(_("Error"),msgPool::rpcError($message), ERROR_DIALOG);
317 }
319 echo "<br>Added {$path}";
320 }
322 // Collect old and new members and their acls, to be able to compare them later.
323 $oldList = $this->saved_attributes['folderList'];
324 $oldAcls = array();
325 $newAcls = array();
326 $oldMembers = array();
327 $newMembers = array();
328 if(isset($oldList[$path])){
329 foreach($oldList[$path]['acls'] as $entry){
330 $oldMembers[] = $entry['name'];
331 $oldAcls[$entry['name']] = $entry['acl'];
332 }
333 }
334 foreach($folder['acls'] as $entry){
335 $newMembers[] = $entry['name'];
336 $newAcls[$entry['name']] = $entry['acl'];
337 }
339 // Check if member list has changed.
340 if(array_differs($newMembers,$oldMembers) || array_differs($oldAcls, $newAcls)){
341 $rpc->gwFolderSetMembers($path, $newAcls);
342 if(!$rpc->success()){
343 $message = sprintf(_("Failed to create shared folder '%s'! Error was: '%s'."),
344 $path, $rpc->get_error());
345 msg_dialog::display(_("Error"),msgPool::rpcError($message), ERROR_DIALOG);
346 }
347 echo "<br>Updating members for {$path} ";
348 }
349 }
350 }
353 function check()
354 {
355 $messages = plugin::check();
357 // Get current object-group name maybe it is invalid for us.
358 if(isset($this->parent->by_object['group']->cn)){
359 $this->cn = &$this->parent->by_object['group']->cn;
360 }
362 // Check if group name has changed, this is not allowed while a groupware extension is active.
363 if($this->initially_was_account && $this->cn != $this->orig_cn && $this->orig_cn != ""){
364 $messages[] = sprintf(
365 _("Groups cannot be renamed, while their groupware extension is active, please use the intial name '%s' again!"),
366 $this->orig_cn);
367 }
369 // Merge Folder messages.
370 if($this->FolderWidget instanceOf FolderWidget){
371 $messages = array_merge($messages,$this->FolderWidget->check());
372 }
374 return($messages);
375 }
378 /*! \brief ACL settings
379 */
380 static function plInfo()
381 {
382 return (array(
383 "plShortName" => _("Shared folder"),
384 "plDescription" => _("Groupware shared folder"),
385 "plSelfModify" => FALSE,
386 "plDepends" => array("ogroup"), // This plugin depends on
387 "plPriority" => 4, // Position in tabs
388 "plSection" => array("administration"),
389 "plCategory" => array("groups"),
390 "plOptions" => array(),
391 "plProvidedAcls" => array(
392 "folderList" => _("Shared folder"))
393 ));
394 }
395 }
396 ?>