R26844 - /trunk/admin/inc/class.customfields.inc.php

Author: ralfbecker
New Revision: 26844

URL: http://www.egroupware.org/viewvc/egroupware?rev=3D26844&view=3Drev
Log:
“fixed fatal error: creating custom fields delete application config:
caused by a missing config::read_repository() before calling config::save_r=
epository(), which then only stores the custom fields, but not the rest of =
the config”

Modified:
trunk/admin/inc/class.customfields.inc.php

Modified: trunk/admin/inc/class.customfields.inc.php
URL: http://www.egroupware.org/viewvc/egroupware/trunk/admin/inc/class.cust=
omfields.inc.php?rev=3D26844&r1=3D26843&r2=3D26844&view=3Ddiff
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
— trunk/admin/inc/class.customfields.inc.php (original)
+++ trunk/admin/inc/class.customfields.inc.php Thu Apr 23 13:18:51 2009
@@ -1,381 +1,379 @@

<?php - /************************************************************************= **\ - * eGroupWare - admin: Custom fields = * - * http://www.egroupware.org = * - * -------------------------------------------- = * - * This program is free software; you can redistribute it and/or modify i= t * - * under the terms of the GNU General Public License as published by the = * - * Free Software Foundation; either version 2 of the License, or (at your= * - * option) any later version. = * - \************************************************************************= **/ - - /* $Id$ */ - - /** - * Custiomfields class - manages customfield definitions in egw_config ta= ble +/** + * eGgroupWare admin - UI for adding custom fields + * + * @link http://www.egroupware.org + * @author Ralf Becker + * @author Cornelius Weiss + * @package admin + * @license http://opensource.org/licenses/gpl-license.php GPL - GNU Gener= al Public License + * @version $Id$ + */ + +/** +* Custiomfields class - manages customfield definitions in egw_config tab= le +* +* The repository name (config_name) is 'customfields'. +*/ +class customfields +{ + + /** + * appname of app which want to add / edit its customfields * - * The repository name (config_name) is 'customfields'. + * @var string + */ + var $appname; + + /** + * userdefiened types e.g. type of infolog * - * @license GPL - * @author Ralf Becker - * @author Cornelius Weiss - * @package admin - */ - class customfields - { - = - /** - * appname of app which want to add / edit its customfields - * = - * @var string - */ - var $appname; - = - /** - * userdefiened types e.g. type of infolog - * = - * @var array - */ - var $types2 =3D array(); - var $content_types,$fields; - = - var $public_functions =3D array( - 'edit' =3D> True + * @var array + */ + var $types2 =3D array(); + var $content_types,$fields; + + var $public_functions =3D array( + 'edit' =3D> True + ); + /** + * Instance of etemplate class + * + * @var etemplate + */ + var $tmpl; + + /** + * Constructor + * + * @param string $appname + */ + function __construct($appname=3D'') + { + if (($this->appname =3D $appname)) + { + $this->fields =3D config::get_customfields($this->appname,true); + $this->content_types =3D config::get_content_types($this->appname); + } + } + + /** + * Edit/Create Custom fields with type + * + * @author Ralf Becker + * @param array $content Content from the eTemplate Exec + */ + function edit($content =3D null) + { + $GLOBALS['egw']->translation->add_app('infolog'); // til we move t= he translations + + // determine appname + $this->appname =3D $_GET['appname'] ? $_GET['appname'] : ($content['appn= ame'] ? $content['appname'] : false); + if(!$this->appname) die(lang('Error! No appname found')); + + $GLOBALS['egw']->translation->add_app('infolog'); // til we move the tra= nslations + $this->tmpl =3D new etemplate(); + // do we manage content-types? + if($this->tmpl->read($this->appname.'.admin.types')) $this->manage_conte= nt_types =3D true; + + $this->fields =3D config::get_customfields($this->appname,true); + $this->tmpl->read('admin.customfields'); + + if($this->manage_content_types) $this->content_types =3D $this->get_cont= ent_types(); + else + { + $this->tmpl->children[0]['data'][2]['A']['disabled'] =3D true; + $this->tmpl->children[0]['data'][3]['A']['disabled'] =3D true; + } + + if (is_array($content)) + { + //echo '
'; print_r($content); echo "\n";
+			if($this->manage_content_types)
+			{
+				$this->content_type =3D $content['content_types']['types'];
+			}
+
+			if($content['content_types']['delete']) $this->delete_content_type($con=
tent);
+			elseif($content['content_types']['create']) $this->create_content_type(=
$content);
+			elseif($content['fields']['delete']) $this->delete_field($content);
+			elseif($content['fields']['create']) $this->create_field($content);
+			else
+			{
+				list($action) =3D @each($content['button']);
+				switch($action)
+				{
+					default:
+						if (!$content['fields']['create'] && !$content['fields']['delete'])
+						{
+							break;	// type change
+						}
+					case 'save':
+					case 'apply':
+						$this->update($content);
+						if ($action !=3D 'save')
+						{
+							break;
+						}
+					case 'cancel':
+						$GLOBALS['egw']->redirect_link($content['referer'] ? $content['refer=
er'] : '/admin/index.php');
+						exit;
+				}
+			}
+			$referer =3D $content['referer'];
+		}
+		else
+		{
+			if($this->manage_content_types)
+			{
+				$content_types =3D array_keys($this->content_types);
+				$this->content_type =3D $content_types[0];
+			}
+			$content['use_private'] =3D (boolean)$_GET['use_private'];
+
+			$referer =3D $GLOBALS['egw']->common->get_referer();
+		}
+		$GLOBALS['egw_info']['flags']['app_header'] =3D $GLOBALS['egw_info']['ap=
ps'][$this->appname]['title'].' - '.lang('Custom fields');
+		$readonlys =3D array();
+
+		if($this->manage_content_types)
+		{
+			$content['content_types']['app-name'] =3D $this->appname;
+			foreach($this->content_types as $type =3D> $entry)
+			{
+				$this->types2[$type] =3D $entry['name'];
+			}
+			$content['content_types']['options-types'] =3D $this->types2;
+			$this->tmpl->children[0]['data'][3]['A']['name'] =3D $this->appname.'.a=
dmin.types';
+			$this->tmpl->children[0]['data'][3]['A']['size'] =3D 'content_type_opti=
ons';
+			$content['content_type_options'] =3D $this->content_types[$this->conten=
t_type]['options'];
+			$content['content_type_options']['type'] =3D $this->content_types[$this=
->content_type]['name'];
+			if ($this->content_types[$this->content_type]['non_deletable'])
+			{
+				$content['content_types']['non_deletable'] =3D true;
+			}
+		}
+
+		//echo 'customfields=3D
'; print_r($this=
->fields); echo "
\n"; + $content['fields'] =3D array('use_private' =3D> $content['use_private']); + $n =3D 0; + foreach($this->fields as $name =3D> $data) + { + if(!is_array($data)) + { + $data =3D array(); + $data['label'] =3D $name; + $data['order'] =3D ($n+1) * 10; + } + if (is_array($data['values'])) + { + $values =3D ''; + foreach($data['values'] as $var =3D> $value) + { + $values .=3D (!empty($values) ? "\n" : '').$var.'=3D'.$value; + } + $data['values'] =3D $values; + } + $content['fields'][++$n] =3D (array)$data + array( + 'name' =3D> $name + ); + $preserv_fields[$n]['old_name'] =3D $name; + $readonlys['fields']["create$name"] =3D True; + } + $content['fields'][++$n] =3D array('name'=3D>'','order' =3D> 10 * $n); /= / new line for create + if($this->manage_content_types) $content['fields']['type2'] =3D 'enable'; + + $readonlys['fields']["delete[]"] =3D True; + //echo '

uicustomfields.edit(content =3D

'; print_r($content); echo "
\n"; + //echo 'readonlys =3D
'; print_r($reado=
nlys); echo "
\n"; + $sel_options =3D array( + 'type2' =3D> $this->types2 + array('tmpl' =3D> 'template'), ); - /** - * Instance of the config class for the instancated applications - * - * @var config - */ - var $config; - - function customfields($appname=3D'') - { - $this->config =3D& new config($this->appname=3D$appname); - - if ($appname) - { - $this->fields =3D $this->get_customfields(true); - $this->content_types =3D $this->get_content_types(); - } - } - - /** - * Edit/Create Custom fields with type - * - * @author Ralf Becker - * @param array $content Content from the eTemplate Exec - */ - function edit($content =3D null) - { - $GLOBALS['egw']->translation->add_app('infolog'); // til we move = the translations - - // determine appname - $this->appname =3D $_GET['appname'] ? $_GET['appname'] : ($content['app= name'] ? $content['appname'] : false); - if(!$this->appname) die(lang('Error! No appname found')); - = - $GLOBALS['egw']->translation->add_app('infolog'); // til we move the tr= anslations - $this->tmpl =3D& CreateObject('etemplate.etemplate'); - $this->config =3D& CreateObject('phpgwapi.config',$this->appname); - // do we manage content-types? - if($this->tmpl->read($this->appname.'.admin.types')) $this->manage_cont= ent_types =3D true; - = - $this->fields =3D $this->get_customfields(true); - $this->tmpl->read('admin.customfields'); - = - if($this->manage_content_types) $this->content_types =3D $this->get_con= tent_types(); - else - { - $this->tmpl->children[0]['data'][2]['A']['disabled'] =3D true; - $this->tmpl->children[0]['data'][3]['A']['disabled'] =3D true; - } - = - if (is_array($content)) - { - //echo '
'; print_r($content); echo "<=
/pre>\n";
-				if($this->manage_content_types)
-				{
-					$this->content_type =3D $content['content_types']['types'];
-				}
-
-				if($content['content_types']['delete']) $this->delete_content_type($co=
ntent);
-				elseif($content['content_types']['create']) $this->create_content_type=
($content);
-				elseif($content['fields']['delete']) $this->delete_field($content);
-				elseif($content['fields']['create']) $this->create_field($content);
-				else
-				{
-					list($action) =3D @each($content['button']);
-					switch($action)
-					{
-						default:
-							if (!$content['fields']['create'] && !$content['fields']['delete'])
-							{
-								break;	// type change
-							}
-						case 'save':
-						case 'apply':
-							$this->update($content);
-							if ($action !=3D 'save')
-							{
-								break;
-							}
-						case 'cancel':
-							$GLOBALS['egw']->redirect_link($content['referer'] ? $content['refe=
rer'] : '/admin/index.php');
-							exit;
-					}
-				}
-				$referer =3D $content['referer'];
-			}
-			else
-			{
-				if($this->manage_content_types)
-				{
-					$content_types =3D array_keys($this->content_types);
-					$this->content_type =3D $content_types[0];
-				}
-				$content['use_private'] =3D (boolean)$_GET['use_private'];
-
-				$referer =3D $GLOBALS['egw']->common->get_referer();
-			}
-			$GLOBALS['egw_info']['flags']['app_header'] =3D $GLOBALS['egw_info']['a=
pps'][$this->appname]['title'].' - '.lang('Custom fields');
-			$readonlys =3D array();
-			=

-			if($this->manage_content_types)
-			{
-				$content['content_types']['app-name'] =3D $this->appname;
-				foreach($this->content_types as $type =3D> $entry)
-				{
-					$this->types2[$type] =3D $entry['name'];
-				}
-				$content['content_types']['options-types'] =3D $this->types2;
-				$this->tmpl->children[0]['data'][3]['A']['name'] =3D $this->appname.'.=
admin.types';
-				$this->tmpl->children[0]['data'][3]['A']['size'] =3D 'content_type_opt=
ions';
-				$content['content_type_options'] =3D $this->content_types[$this->conte=
nt_type]['options'];
-				$content['content_type_options']['type'] =3D $this->content_types[$thi=
s->content_type]['name'];
-				if ($this->content_types[$this->content_type]['non_deletable'])
-				{
-					$content['content_types']['non_deletable'] =3D true;
-				}
-			}
-			=

-			//echo 'customfields=3D
'; print_r($thi=
s->fields); echo "
\n"; - $content['fields'] =3D array('use_private' =3D> $content['use_private']= ); - $n =3D 0; - foreach($this->fields as $name =3D> $data) - { - if(!is_array($data)) - { - $data =3D array(); - $data['label'] =3D $name; - $data['order'] =3D ($n+1) * 10; - } - if (is_array($data['values'])) - { - $values =3D ''; - foreach($data['values'] as $var =3D> $value) - { - $values .=3D (!empty($values) ? "\n" : '').$var.'=3D'.$value; - } - $data['values'] =3D $values; - } - $content['fields'][++$n] =3D (array)$data + array( - 'name' =3D> $name - ); - $preserv_fields[$n]['old_name'] =3D $name; - $readonlys['fields']["create$name"] =3D True; - } - $content['fields'][++$n] =3D array('name'=3D>'','order' =3D> 10 * $n); = // new line for create - if($this->manage_content_types) $content['fields']['type2'] =3D 'enable= '; - - $readonlys['fields']["delete[]"] =3D True; - //echo '

uicustomfields.edit(content =3D

'; print_r($content); echo "
\n"; - //echo 'readonlys =3D
'; print_r($read=
onlys); echo "
\n"; - $sel_options =3D array( - 'type2' =3D> $this->types2 + array('tmpl' =3D> 'template'), + $this->tmpl->exec('admin.customfields.edit',$content,$sel_options,$reado= nlys,array( + 'fields' =3D> $preserv_fields, + 'appname' =3D> $this->appname, + 'referer' =3D> $referer, + 'use_private' =3D> $content['use_private'], + )); + } + + function update_fields(&$content) + { + foreach($content['fields'] as $field) + { + $name =3D trim($field['name']); + $old_name =3D $field['old_name']; + + if (!empty($delete) && $delete =3D=3D $old_name) + { + unset($this->fields[$old_name]); + continue; + } + if (isset($field['old_name'])) + { + if (empty($name)) // empty name not allowed + { + $content['error_msg'] =3D lang('Name must not be empty !!!'); + $name =3D $old_name; + } + if (!empty($name) && $old_name !=3D $name) // renamed + { + unset($this->fields[$old_name]); + } + } + elseif (empty($name)) // new item and empty =3D=3D> ignore it + { + continue; + } + $values =3D array(); + if (!empty($field['values'])) + { + foreach(explode("\n",$field['values']) as $line) + { + list($var,$value) =3D split('=3D',trim($line),2); + $var =3D trim($var); + $values[$var] =3D empty($value) ? $var : $value; + } + } + $this->fields[$name] =3D array( + 'type' =3D> $field['type'], + 'type2' =3D> $field['type2'], + 'label' =3D> empty($field['label']) ? $name : $field['label'], + 'help' =3D> $field['help'], + 'values'=3D> $values, + 'len' =3D> $field['len'], + 'rows' =3D> (int)$field['rows'], + 'order' =3D> (int)$field['order'], + 'private' =3D> $field['private'], ); - $this->tmpl->exec('admin.customfields.edit',$content,$sel_options,$read= onlys,array( - 'fields' =3D> $preserv_fields, - 'appname' =3D> $this->appname, - 'referer' =3D> $referer, - 'use_private' =3D> $content['use_private'], - )); - } - - function update_fields(&$content) - { - foreach($content['fields'] as $field) - { - $name =3D trim($field['name']); - $old_name =3D $field['old_name']; - - if (!empty($delete) && $delete =3D=3D $old_name) - { - unset($this->fields[$old_name]); - continue; - } - if (isset($field['old_name'])) - { - if (empty($name)) // empty name not allowed - { - $content['error_msg'] =3D lang('Name must not be empty !!!'); - $name =3D $old_name; - } - if (!empty($name) && $old_name !=3D $name) // renamed - { - unset($this->fields[$old_name]); - } - } - elseif (empty($name)) // new item and empty =3D=3D> ignore it - { - continue; - } - $values =3D array(); - if (!empty($field['values'])) - { - foreach(explode("\n",$field['values']) as $line) - { - list($var,$value) =3D split('=3D',trim($line),2); - $var =3D trim($var); - $values[$var] =3D empty($value) ? $var : $value; - } - } - $this->fields[$name] =3D array( - 'type' =3D> $field['type'], - 'type2' =3D> $field['type2'], - 'label' =3D> empty($field['label']) ? $name : $field['label'], - 'help' =3D> $field['help'], - 'values'=3D> $values, - 'len' =3D> $field['len'], - 'rows' =3D> (int)$field['rows'], - 'order' =3D> (int)$field['order'], - 'private' =3D> $field['private'], - ); - if(!$this->fields[$name]['type2'] && $this->manage_content_types) - { - $this->fields[$name]['type2'] =3D (string)0; - } - } - if (!function_exists('sort_by_order')) - { - function sort_by_order($arr1,$arr2) - { - return $arr1['order'] - $arr2['order']; - } - } - uasort($this->fields,sort_by_order); - - $n =3D 0; - foreach($this->fields as $name =3D> $data) - { - $this->fields[$name]['order'] =3D ($n +=3D 10); - } - } - - - function update(&$content) - { - $this->update_fields($content); - $this->content_types[$this->content_type]['options'] =3D $content['cont= ent_type_options']; - // save changes to repository + if(!$this->fields[$name]['type2'] && $this->manage_content_types) + { + $this->fields[$name]['type2'] =3D (string)0; + } + } + if (!function_exists('sort_by_order')) + { + function sort_by_order($arr1,$arr2) + { + return $arr1['order'] - $arr2['order']; + } + } + uasort($this->fields,sort_by_order); + + $n =3D 0; + foreach($this->fields as $name =3D> $data) + { + $this->fields[$name]['order'] =3D ($n +=3D 10); + } + } + + + function update(&$content) + { + $this->update_fields($content); + $this->content_types[$this->content_type]['options'] =3D $content['conte= nt_type_options']; + // save changes to repository + $this->save_repository(); + } + + /** + * deletes custom field from customfield definitions + */ + function delete_field(&$content) + { + unset($this->fields[key($content['fields']['delete'])]); + // save changes to repository + $this->save_repository(); + } + + function delete_content_type(&$content) + { + unset($this->content_types[$content['content_types']['types']]); + // save changes to repository + $this->save_repository(); + } + + /** + * create a new custom field + */ + function create_field(&$content) + { + $new_name =3D trim($content['fields'][count($content['fields'])-1]['name= ']); + if (empty($new_name) || isset($this->fields[$new_name])) + { + $content['error_msg'] .=3D empty($new_name) ? + lang('You have to enter a name, to create a new field!!!') : + lang("Field '%1' already exists !!!",$new_name); + } + else + { + $this->fields[$new_name] =3D $content['fields'][count($content['fields'= ])-1]; + if(!$this->fields[$new_name]['label']) $this->fields[$new_name]['label'= ] =3D $this->fields[$new_name]['name']; $this->save_repository(); } - - /** - * deletes custom field from customfield definitions - */ - function delete_field(&$content) - { - unset($this->fields[key($content['fields']['delete'])]); - // save changes to repository + } + + function create_content_type(&$content) + { + $new_name =3D trim($content['content_types']['name']); + if (empty($new_name) || isset($this->fields[$new_name])) + { + $content['error_msg'] .=3D empty($new_name) ? + lang('You have to enter a name, to create a new type!!!') : + lang("type '%1' already exists !!!",$new_name); + } + else + { + // search free type character + for($i=3D97;$i<=3D122;$i++) + { + if(!$this->content_types[chr($i)]) + { + $new_type =3D chr($i); + break; + } + } + $this->content_types[$new_type] =3D array('name' =3D> $new_name); $this->save_repository(); } - = - function delete_content_type(&$content) - { - unset($this->content_types[$content['content_types']['types']]); - // save changes to repository - $this->save_repository(); - } - = - /** - * create a new custom field - */ - function create_field(&$content) - { - $new_name =3D trim($content['fields'][count($content['fields'])-1]['nam= e']); - if (empty($new_name) || isset($this->fields[$new_name])) - { - $content['error_msg'] .=3D empty($new_name) ? - lang('You have to enter a name, to create a new field!!!') : - lang("Field '%1' already exists !!!",$new_name); - } - else - { - $this->fields[$new_name] =3D $content['fields'][count($content['fields= '])-1]; - if(!$this->fields[$new_name]['label']) $this->fields[$new_name]['label= '] =3D $this->fields[$new_name]['name']; - $this->save_repository(); - } - } - = - function create_content_type(&$content) - { - $new_name =3D trim($content['content_types']['name']); - if (empty($new_name) || isset($this->fields[$new_name])) - { - $content['error_msg'] .=3D empty($new_name) ? - lang('You have to enter a name, to create a new type!!!') : - lang("type '%1' already exists !!!",$new_name); - } - else - { - // search free type character - for($i=3D97;$i<=3D122;$i++) - { - if(!$this->content_types[chr($i)]) - { - $new_type =3D chr($i); - break; - } - } - $this->content_types[$new_type] =3D array('name' =3D> $new_name); - $this->save_repository(); - } - } - - /** - * save changes to repository - */ - function save_repository() - { - //echo '

uicustomfields::save_repository() \$this->fields=3D

'; print_r($this->fields); echo "
\n"; - $this->config->value('customfields',$this->fields); - $this->config->value('types',$this->content_types); - $this->config->save_repository(); - } - = - /** - * get customfields of using application - * - * @deprecated use config::get_customfields() direct, no need to instanci= ate this UI class - * @author Cornelius Weiss - * @param boolean $all_private_too=3Dfalse should all the private fields = be returned too - * @return array with customfields - */ - function get_customfields($all_private_too=3Dfalse) - { - return config::get_customfields($this->appname,$all_private_too); - } - = - /** - * get_content_types of using application - * - * @deprecated use config::get_content_types() direct, no need to instanc= iate this UI class - * @author Cornelius Weiss - * @return array with content-types - */ - function get_content_types() - { - return config::get_content_types($this->appname); - } - - } + } + + /** + * save changes to repository + */ + function save_repository() + { + //echo '

uicustomfields::save_repository() \$this->fields=3D

'; print_r($this->fields); echo "
\n"; + $config =3D new config($this->appname); + $config->read_repository(); + $config->value('customfields',$this->fields); + $config->value('types',$this->content_types); + $config->save_repository(); + } + + /** + * get customfields of using application + * + * @deprecated use config::get_customfields() direct, no need to instancia= te this UI class + * @author Cornelius Weiss + * @param boolean $all_private_too=3Dfalse should all the private fields b= e returned too + * @return array with customfields + */ + function get_customfields($all_private_too=3Dfalse) + { + return config::get_customfields($this->appname,$all_private_too); + } + + /** + * get_content_types of using application + * + * @deprecated use config::get_content_types() direct, no need to instanci= ate this UI class + * @author Cornelius Weiss + * @return array with content-types + */ + function get_content_types() + { + return config::get_content_types($this->appname); + } + +} ------------------------------------------------------------------------------ Stay on top of everything new and different, both inside and around Java (TM) technology - register by April 22, and save $200 on the JavaOne (SM) conference, June 2-5, 2009, San Francisco. 300 plus technical and hands-on sessions. Register today. Use priority code J9JMT32. http://p.sf.net/sfu/p _______________________________________________ eGroupWare-cvs mailing list eGroupWare-cvs@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/egroupware-cvs