Unify/de-duplicate code for folder form handling

This commit is contained in:
Aleksander Machniak 2018-03-28 15:36:08 +00:00
parent 9f58502839
commit a444b5b801
6 changed files with 108 additions and 329 deletions

View file

@ -2328,20 +2328,11 @@ class kolab_driver extends calendar_driver
// General tab
$form['props'] = array(
'name' => $this->rc->gettext('properties'),
'content' => $formfields,
'name' => $this->rc->gettext('properties'),
'fields' => $formfields,
);
$form_html = '';
// Create form output
foreach ($form as $tab) {
if ($content = $this->get_form_part($tab)) {
$form_html .= html::tag('fieldset', null, html::tag('legend', null, rcube::Q($tab['name'])) . $content) ."\n";
}
}
return $form_html;
return kolab_utils::folder_form($form, '', 'calendar');
}
$this->_read_calendars();
@ -2374,8 +2365,8 @@ class kolab_driver extends calendar_driver
// General tab
$form['props'] = array(
'name' => $this->rc->gettext('properties'),
'content' => array(),
'name' => $this->rc->gettext('properties'),
'fields' => array(),
);
// Disable folder name input
@ -2386,7 +2377,7 @@ class kolab_driver extends calendar_driver
}
// calendar name (default field)
$form['props']['content']['location'] = $formfields['name'];
$form['props']['fields']['location'] = $formfields['name'];
if (!empty($options) && ($options['norename'] || $options['protected'])) {
// prevent user from moving folder
@ -2394,7 +2385,7 @@ class kolab_driver extends calendar_driver
}
else {
$select = kolab_storage::folder_selector('event', array('name' => 'parent', 'id' => 'calendar-parent'), $folder);
$form['props']['content']['path'] = array(
$form['props']['fields']['path'] = array(
'id' => 'calendar-parent',
'label' => $this->cal->gettext('parentcalendar'),
'value' => $select->show(strlen($folder) ? $path_imap : ''),
@ -2402,122 +2393,10 @@ class kolab_driver extends calendar_driver
}
// calendar color (default field)
$form['props']['content']['color'] = $formfields['color'];
$form['props']['content']['alarms'] = $formfields['showalarms'];
$form['props']['fields']['color'] = $formfields['color'];
$form['props']['fields']['alarms'] = $formfields['showalarms'];
if ($action != 'form-new') {
$form['sharing'] = array(
'name' => rcube::Q($this->cal->gettext('tabsharing')),
'content' => $this->folder_acl_form($folder)
);
}
$form_html = '';
if (is_array($hidden_fields)) {
foreach ($hidden_fields as $field) {
$hiddenfield = new html_hiddenfield($field);
$form_html .= $hiddenfield->show() . "\n";
}
}
// Create form output
foreach ($form as $tab) {
if ($content = $this->get_form_part($tab)) {
$form_html .= html::tag('fieldset', null, html::tag('legend', null, rcube::Q($tab['name'])) . $content) ."\n";
}
}
return $form_html;
}
/**
* Returns ACL form
*/
public function folder_acl_form($folder)
{
if (strlen($folder)) {
$this->cal->require_plugin('acl');
$storage = $this->rc->get_storage();
$options = $storage->folder_info($folder);
// get sharing UI from acl plugin
$acl = $this->rc->plugins->exec_hook('folder_form',
array('form' => array(), 'options' => $options, 'name' => $folder));
}
return $acl['form']['sharing']['content'] ?: html::div('hint', $this->cal->gettext('aclnorights'));
}
/**
* Helper function used in calendar_form_content(). Creates a part of the form.
*/
private function get_form_part($form)
{
$content = '';
if (is_array($form['content']) && !empty($form['content'])) {
$table = new html_table(array('cols' => 2, 'class' => 'propform'));
foreach ($form['content'] as $col => $colprop) {
$label = !empty($colprop['label']) ? $colprop['label'] : $this->cal->gettext($col);
$table->add('title', html::label($colprop['id'], rcube::Q($label)));
$table->add(null, $colprop['value']);
}
$content = $table->show();
}
else {
$content = $form['content'];
}
return $content;
}
/**
* Handler to render ACL form for a calendar folder
*/
public function calendar_acl()
{
$this->rc->output->add_handler('folderacl', array($this, 'calendar_acl_form'));
$this->rc->output->send('calendar.kolabacl');
}
/**
* Handler for ACL form template object
*/
public function calendar_acl_form()
{
$calid = rcube_utils::get_input_value('_id', rcube_utils::INPUT_GPC);
if ($calid && ($cal = $this->get_calendar($calid))) {
$folder = $cal->get_realname(); // UTF7
$color = $cal->get_color();
}
else {
$folder = '';
$color = '';
}
$storage = $this->rc->get_storage();
$delim = $storage->get_hierarchy_delimiter();
$form = array();
if (strlen($folder)) {
$path_imap = explode($delim, $folder);
array_pop($path_imap); // pop off name part
$path_imap = implode($path_imap, $delim);
$options = $storage->folder_info($folder);
// Allow plugins to modify the form content (e.g. with ACL form)
$plugin = $this->rc->plugins->exec_hook('calendar_form_kolab',
array('form' => $form, 'options' => $options, 'name' => $folder));
}
if (!$plugin['form']['sharing']['content'])
$plugin['form']['sharing']['content'] = html::div('hint', $this->cal->gettext('aclnorights'));
return $plugin['form']['sharing']['content'];
return kolab_utils::folder_form($form, $folder, 'calendar', $hidden_fields);
}
/**

View file

@ -235,40 +235,7 @@ class kolab_addressbook_ui
);
}
// Allow plugins to modify address book form content (e.g. with ACL form)
$plugin = $this->rc->plugins->exec_hook('addressbook_form',
array('form' => $form, 'options' => $options, 'name' => $folder));
$form = $plugin['form'];
$form_html = '';
if (is_array($hidden_fields)) {
foreach ($hidden_fields as $field) {
$hiddenfield = new html_hiddenfield($field);
$form_html .= $hiddenfield->show() . "\n";
}
}
// create form output
foreach ($form as $tab) {
if (is_array($tab['fields']) && empty($tab['content'])) {
$table = new html_table(array('cols' => 2, 'class' => 'propform'));
foreach ($tab['fields'] as $col => $colprop) {
$label = !empty($colprop['label']) ? $colprop['label'] : $this->plugin->gettext($col);
$table->add('title', html::label($colprop['id'], rcube::Q($label)));
$table->add(null, $colprop['value']);
}
$content = $table->show();
}
else {
$content = $tab['content'];
}
if (!empty($content)) {
$form_html .= html::tag('fieldset', null, html::tag('legend', null, rcube::Q($tab['name'])) . $content) . "\n";
}
}
$form_html = kolab_utils::folder_form($form, $folder, 'calendar', $hidden_fields);
return html::tag('form', $attrib + array('action' => 'plugin.book-save', 'method' => 'post', 'id' => 'bookpropform'), $form_html);
}
@ -286,58 +253,4 @@ class kolab_addressbook_ui
return $p;
}
private function get_form_part($form)
{
$content = '';
if (is_array($form['content']) && !empty($form['content'])) {
$table = new html_table(array('cols' => 2, 'class' => 'propform'));
foreach ($form['content'] as $col => $colprop) {
$colprop['id'] = '_'.$col;
$label = !empty($colprop['label']) ? $colprop['label'] : $this->rc->gettext($col);
$table->add('title', sprintf('<label for="%s">%s</label>', $colprop['id'], rcube::Q($label)));
$table->add(null, $colprop['value']);
}
$content = $table->show();
}
else {
$content = $form['content'];
}
return $content;
}
private function get_form_tags($attrib, $action, $id = null, $hidden = null)
{
$form_start = $form_end = '';
$request_key = $action . (isset($id) ? '.'.$id : '');
$form_start = $this->rc->output->request_form(array(
'name' => 'form',
'method' => 'post',
'task' => $this->rc->task,
'action' => $action,
'request' => $request_key,
'noclose' => true,
) + $attrib);
if (is_array($hidden)) {
foreach ($hidden as $field) {
$hiddenfield = new html_hiddenfield($field);
$form_start .= $hiddenfield->show();
}
}
$form_end = !strlen($attrib['form']) ? '</form>' : '';
$EDIT_FORM = !empty($attrib['form']) ? $attrib['form'] : 'form';
$this->rc->output->add_gui_object('editform', $EDIT_FORM);
return array($form_start, $form_end);
}
}

View file

@ -340,61 +340,8 @@ class kolab_notes_ui
);
}
// add folder ACL tab
if ($this->action != 'form-new') {
$form['sharing'] = array(
'name' => rcube::Q($this->plugin->gettext('tabsharing')),
'content' => $this->folder_acl_form()
);
}
$form_html = '';
if (is_array($hidden_fields)) {
foreach ($hidden_fields as $field) {
$hiddenfield = new html_hiddenfield($field);
$form_html .= $hiddenfield->show() . "\n";
}
}
// create form output
foreach ($form as $tab) {
if (is_array($tab['fields']) && empty($tab['content'])) {
$table = new html_table(array('cols' => 2, 'class' => 'propform'));
foreach ($tab['fields'] as $col => $colprop) {
$label = !empty($colprop['label']) ? $colprop['label'] : $this->plugin->gettext($col);
$table->add('title', html::label($colprop['id'], rcube::Q($label)));
$table->add(null, $colprop['value']);
}
$content = $table->show();
}
else {
$content = $tab['content'];
}
if (!empty($content)) {
$form_html .= html::tag('fieldset', null, html::tag('legend', null, rcube::Q($tab['name'])) . $content) . "\n";
}
}
$form_html = kolab_utils::folder_form($form, $folder_name, 'kolab_notes', $hidden_fields);
return html::tag('form', $attrib + array('action' => '#', 'method' => 'post', 'id' => 'noteslistpropform'), $form_html);
}
/**
* Returns ACL form
*/
public function folder_acl_form()
{
if (strlen($this->folder)) {
$this->plugin->require_plugin('acl');
$storage = $this->rc->get_storage();
$options = $storage->folder_info($this->folder);
// get sharing UI from acl plugin
$acl = $this->rc->plugins->exec_hook('folder_form',
array('form' => array(), 'options' => $options, 'name' => $this->folder));
}
return $acl['form']['sharing']['content'] ?: html::div('hint', $this->plugin->gettext('aclnorights'));
}
}

View file

@ -0,0 +1,93 @@
<?php
/**
* Utility class providing unified functionality for other plugins.
*
* @version @package_version@
* @author Thomas Bruederli <bruederli@kolabsys.com>
* @author Aleksander Machniak <machniak@kolabsys.com>
*
* Copyright (C) 2012-2018, Kolab Systems AG <contact@kolabsys.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
class kolab_utils
{
public static function folder_form($form, $folder, $domain, $hidden_fields = array())
{
$rcmail = rcmail::get_instance();
// add folder ACL tab
if (is_string($folder) && strlen($folder)) {
$form['sharing'] = array(
'name' => rcube::Q($rcmail->gettext('libkolab.tabsharing')),
'content' => self::folder_acl_form($folder),
);
}
$form_html = '';
if (is_array($hidden_fields)) {
foreach ($hidden_fields as $field) {
$hiddenfield = new html_hiddenfield($field);
$form_html .= $hiddenfield->show() . "\n";
}
}
// create form output
foreach ($form as $tab) {
if (is_array($tab['fields']) && empty($tab['content'])) {
$table = new html_table(array('cols' => 2, 'class' => 'propform'));
foreach ($tab['fields'] as $col => $colprop) {
$label = !empty($colprop['label']) ? $colprop['label'] : $rcmail->gettext("$domain.$col");
$table->add('title', html::label($colprop['id'], rcube::Q($label)));
$table->add(null, $colprop['value']);
}
$content = $table->show();
}
else {
$content = $tab['content'];
}
if (!empty($content)) {
$form_html .= html::tag('fieldset', null, html::tag('legend', null, rcube::Q($tab['name'])) . $content) . "\n";
}
}
return $form_html;
}
/**
* Handler for ACL form template object
*/
public static function folder_acl_form($folder)
{
$rcmail = rcmail::get_instance();
$storage = $rcmail->get_storage();
$options = $storage->folder_info($folder);
$rcmail->plugins->load_plugin('acl', true);
// get sharing UI from acl plugin
$acl = $rcmail->plugins->exec_hook('folder_form', array(
'form' => array(),
'options' => $options,
'name' => $folder
));
return $acl['form']['sharing']['content'] ?: html::div('hint', $rcmail->gettext('libkolab.aclnorights'));
}
}

View file

@ -28,3 +28,5 @@ $labels['actionflagclear'] = 'Flag removed';
$labels['showrevision'] = 'Show this version';
$labels['restore'] = 'Restore this version';
$labels['objectchangelognotavailable'] = 'History is not available for this object.';
$labels['tabsharing'] = 'Sharing';
$labels['aclnorights'] = 'You do not have administrator rights for this folder.';

View file

@ -1719,61 +1719,6 @@ class tasklist_kolab_driver extends tasklist_driver
$form['properties']['fields'][$f] = $fieldprop[$f];
}
// add folder ACL tab
if ($action != 'form-new') {
$form['sharing'] = array(
'name' => rcube::Q($this->plugin->gettext('tabsharing')),
'content' => $this->folder_acl_form($folder_name)
);
}
$form_html = '';
if (is_array($hidden_fields)) {
foreach ($hidden_fields as $field) {
$hiddenfield = new html_hiddenfield($field);
$form_html .= $hiddenfield->show() . "\n";
}
}
// create form output
foreach ($form as $tab) {
if (is_array($tab['fields']) && empty($tab['content'])) {
$table = new html_table(array('cols' => 2, 'class' => 'propform'));
foreach ($tab['fields'] as $col => $colprop) {
$label = !empty($colprop['label']) ? $colprop['label'] : $this->plugin->gettext($col);
$table->add('title', html::label($colprop['id'], rcube::Q($label)));
$table->add(null, $colprop['value']);
}
$content = $table->show();
}
else {
$content = $tab['content'];
}
if (!empty($content)) {
$form_html .= html::tag('fieldset', null, html::tag('legend', null, rcube::Q($tab['name'])) . $content) . "\n";
}
}
return $form_html;
}
/**
* Handler for ACL form template object
*/
public function folder_acl_form($folder)
{
if (strlen($folder)) {
$this->plugin->require_plugin('acl');
$storage = $this->rc->get_storage();
$options = $storage->folder_info($folder);
// get sharing UI from acl plugin
$acl = $this->rc->plugins->exec_hook('folder_form',
array('form' => array(), 'options' => $options, 'name' => $folder));
}
return $acl['form']['sharing']['content'] ?: html::div('hint', $this->plugin->gettext('aclnorights'));
return kolab_utils::folder_form($form, $folder_name, 'tasklist', $hidden_fields);
}
}