[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 {
288 if(!$this->initialized){
289 echo "Nothing to save, plugin init failed!";
290 return;
291 }
293 $this->folderList = $this->FolderWidget->save();
294 $rpc = $this->config->getRpcHandle();
296 // Walk through folders and perform requested actions
297 foreach($this->folderList as $path => $folder){
299 // Update folder names for newly created groupware extensions.
300 $path = preg_replace("/\{%cn\}/", $this->cn, $path);
301 $folder['name'] = preg_replace("/\{%cn\}/", $this->cn, $folder['name']);
303 // Remove shared folder.
304 if($folder['status'] == 'removed'){
305 $rpc->gwFolderDel($path);
306 if(!$rpc->success()){
307 $message = sprintf(_("Failed to remove shared folder '%s'! Error was: '%s'."),
308 $path, $rpc->get_error());
309 msg_dialog::display(_("Error"),msgPool::rpcError($message), ERROR_DIALOG);
310 }
312 echo "<br>Removed {$path}";
313 continue;
314 }
316 // Add newly created folders
317 if($folder['status'] == 'added'){
318 $rpc->gwFolderAdd($path);
319 if(!$rpc->success()){
320 $message = sprintf(_("Failed to create shared folder '%s'! Error was: '%s'."),
321 $path, $rpc->get_error());
322 msg_dialog::display(_("Error"),msgPool::rpcError($message), ERROR_DIALOG);
323 }
325 echo "<br>Added {$path}";
326 }
328 // Collect old and new members and their acls, to be able to compare them later.
329 $oldList = $this->saved_attributes['folderList'];
330 $oldAcls = array();
331 $newAcls = array();
332 $oldMembers = array();
333 $newMembers = array();
334 if(isset($oldList[$path])){
335 foreach($oldList[$path]['acls'] as $entry){
336 $oldMembers[] = $entry['name'];
337 $oldAcls[$entry['name']] = $entry['acl'];
338 }
339 }
340 foreach($folder['acls'] as $entry){
341 $newMembers[] = $entry['name'];
342 $newAcls[$entry['name']] = $entry['acl'];
343 }
345 // Check if member list has changed.
346 if(array_differs($newMembers,$oldMembers) || array_differs($oldAcls, $newAcls)){
347 $rpc->gwFolderSetMembers($path, $newAcls);
348 if(!$rpc->success()){
349 $message = sprintf(_("Failed to create shared folder '%s'! Error was: '%s'."),
350 $path, $rpc->get_error());
351 msg_dialog::display(_("Error"),msgPool::rpcError($message), ERROR_DIALOG);
352 }
353 echo "<br>Updating members for {$path} ";
354 }
355 }
356 }
359 function check()
360 {
361 $messages = plugin::check();
363 // Get current object-group name maybe it is invalid for us.
364 if(isset($this->parent->by_object['group']->cn)){
365 $this->cn = &$this->parent->by_object['group']->cn;
366 }
368 // Check if group name has changed, this is not allowed while a groupware extension is active.
369 if($this->initially_was_account && $this->cn != $this->orig_cn && $this->orig_cn != ""){
370 $messages[] = sprintf(
371 _("Groups cannot be renamed, while their groupware extension is active, please use the intial name '%s' again!"),
372 $this->orig_cn);
373 }
375 // Merge Folder messages.
376 if($this->FolderWidget instanceOf FolderWidget){
377 $messages = array_merge($messages,$this->FolderWidget->check());
378 }
380 return($messages);
381 }
384 /*! \brief ACL settings
385 */
386 static function plInfo()
387 {
388 return (array(
389 "plShortName" => _("Shared folder"),
390 "plDescription" => _("Groupware shared folder"),
391 "plSelfModify" => FALSE,
392 "plDepends" => array("ogroup"), // This plugin depends on
393 "plPriority" => 4, // Position in tabs
394 "plSection" => array("administration"),
395 "plCategory" => array("groups"),
396 "plOptions" => array(),
397 "plProvidedAcls" => array(
398 "folderList" => _("Shared folder"))
399 ));
400 }
401 }
402 ?>