diff --git a/admin/inc/class.admin_categories.inc.php b/admin/inc/class.admin_categories.inc.php index 2d0f331fca..74c0d91484 100644 --- a/admin/inc/class.admin_categories.inc.php +++ b/admin/inc/class.admin_categories.inc.php @@ -156,9 +156,15 @@ class admin_categories { $data = $cats->id2name($content['id'],'data'); - try { - $cats->edit($content); - $msg = lang('Category saved.'); + unset($content['msg']); + if(!$content['parent']) + { + $content['parent'] = '0'; + } + try + { + $cmd = new admin_cmd_category($appname, $content, $cats->read($content['id'])); + $msg = $cmd->run(); } catch (Api\Exception\WrongUserinput $e) { @@ -169,7 +175,9 @@ class admin_categories $content['parent'] && self::$acl_add_sub || !$content['parent'] && self::$acl_add)) { - $content['id'] = $cats->add($content); + $cmd = new admin_cmd_category($appname, $content); + $cmd->run(); + $content['id'] = $cmd->cat_id; $msg = lang('Category saved.'); } else @@ -229,8 +237,8 @@ class admin_categories case 'delete': if (self::$acl_delete) { - $cats->delete($content['id'],$delete_subs,!$delete_subs); - $msg = lang('Category deleted.'); + $cmd = new admin_cmd_delete_category($content['id'], $delete_subs); + $msg = $cmd->run(); Framework::refresh_opener($msg, $refresh_app, $content['id'],'delete', $this->appname); Framework::window_close(); @@ -402,11 +410,14 @@ class admin_categories continue; } - $row['level_spacer'] = str_repeat('    ',$row['level']); + if($row['level'] >= 0) + { + $row['level_spacer'] = str_repeat('    ',$row['level']); + } if ($row['data']['icon']) $row['icon_url'] = self::icon_url($row['data']['icon']); - $row['subs'] = count($row['children']); + $row['subs'] = $row['children'] ? count($row['children']) : 0; $row['class'] = 'level'.$row['level']; if($row['owner'][0] > 0 && !$GLOBALS['egw_info']['user']['apps']['admin'] && $row['owner'][0] != $GLOBALS['egw_info']['user']['account_id']) @@ -717,7 +728,8 @@ class admin_categories { if($cats->check_perms(Acl::DELETE, $id, (boolean)$GLOBALS['egw_info']['user']['apps']['admin'])) { - $cats->delete($id,$settings == 'sub',$settings != 'sub'); + $cmd = new admin_cmd_delete_category($id, $settings == 'sub'); + $cmd->run(); $success++; } else @@ -746,7 +758,8 @@ class admin_categories array_diff($data['owner'],$ids); $data['owner'] = implode(',',array_unique($data['owner'])); - if ($cats->edit($data)) + $cmd = new admin_cmd_category($app, $data, array()); + if ($cmd->run()) { $success++; } diff --git a/admin/inc/class.admin_cmd_category.inc.php b/admin/inc/class.admin_cmd_category.inc.php new file mode 100644 index 0000000000..90151375c5 --- /dev/null +++ b/admin/inc/class.admin_cmd_category.inc.php @@ -0,0 +1,110 @@ + + * @package admin + * @copyright (c) 2018 Nathan Gray + * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License + */ + +use EGroupware\Api; + +/** + * setup command: change EGw category + * + * @property-read string $app app whos category to change (Categories->app_name) + * @property-read array $set category data to set, value of null or "" to remove + * @property-read array $old old values to record + */ +class admin_cmd_category extends admin_cmd +{ + /** + * Allow to run this command via setup-cli + */ + //const SETUP_CLI_CALLABLE = true; // need to check how to parse arguments + + /** + * Constructor + * + * @param array|string $data data array or app whos category to change + * @param array $set =null category data to set, value of null or "" to remove + * @param array $old =null old values to record + * @param array $other =null values for keys "requested", "requested_email", "comment", etc + */ + function __construct($data, array $set=null, array $old=null, $other=null) + { + + if (!is_array($data)) + { + $data = array( + 'app' => $data, + 'set' => $set, + 'old' => $old, + )+(array)$other; + } + else if ($data['appname']) + { + $this->app = $data['appname']; + } + if(!$old && $old !== NULL && $set['id']) + { + $data['old'] = Api\Categories::read($set['id']); + } + //echo __CLASS__.'::__construct()'; _debug_array($domain); + admin_cmd::__construct($data); + } + + /** + * run the command: write the configuration to the database + * + * @param boolean $check_only =false only run the checks (and throw the exceptions), but not the command itself + * @return string success message + * @throws Exception(lang('Wrong credentials to access the header.inc.php file!'),2); + * @throws Exception('header.inc.php not found!'); + */ + protected function exec($check_only=false) + { + $cats = new Api\Categories('',$this->app); + if ($check_only) + { + return $cats->check_consistency4update($this->set); + } + + // store the cat + $this->cat_id = $this->set['id'] ? $cats->edit($this->set) : $cats->add($this->set); + + // Clean data for history + $set =& $this->set; + $old =& $this->old; + unset($old['last_mod']); + unset($set['old_parent'], $set['base_url'], $set['last_mod'], $set['all_cats'], $set['no_private']); + foreach($set as $key => $value) + { + if(array_key_exists($key, $old) && $old[$key] == $value) + { + unset($set[$key]); + unset($old[$key]); + } + } + $this->set = $set; + $this->old = $old; + + return lang('Category saved.'); + } + + /** + * Return a title / string representation for a given command, eg. to display it + * + * @return string + */ + function __tostring() + { + return lang('%1 category \'%2\' %3', + lang($this->app), + Api\Categories::id2name($this->cat_id), + $this->old ? lang('edited') : lang('added') + ); + } +} diff --git a/admin/inc/class.admin_cmd_delete_category.inc.php b/admin/inc/class.admin_cmd_delete_category.inc.php new file mode 100644 index 0000000000..c48035291e --- /dev/null +++ b/admin/inc/class.admin_cmd_delete_category.inc.php @@ -0,0 +1,83 @@ + + * @package admin + * @copyright (c) 2018 Nathan Gray + * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License + */ + +use EGroupware\Api; + +/** + * setup command: delete EGw category + * + * @property-read string $app app whos category to delete (Categories->app_name) + * @property-read int $cat_id category ID to delete + * @property-read boolean $subs Delete subs as well + */ +class admin_cmd_delete_category extends admin_cmd +{ + /** + * Allow to run this command via setup-cli + */ + //const SETUP_CLI_CALLABLE = true; // need to check how to parse arguments + + /** + * Constructor + * + * @param array|int ID of category to remove + * @param boolean $subs Remove sub-categories as well + * @param array $other =null values for keys "requested", "requested_email", "comment", etc + */ + function __construct($data, $subs = true, $other=null) + { + + if(!is_array($data)) + { + $this->app = Api\Categories::id2name($data, 'appname'); + $data = array( + 'cat_id' => (int)$data, + 'subs' => $subs + )+(array)$other; + } + + //echo __CLASS__.'::__construct()'; _debug_array($domain); + admin_cmd::__construct($data); + } + + /** + * run the command: write the configuration to the database + * + * @param boolean $check_only =false only run the checks (and throw the exceptions), but not the command itself + * @return string success message + * @throws Exception(lang('Wrong credentials to access the header.inc.php file!'),2); + * @throws Exception('header.inc.php not found!'); + */ + protected function exec($check_only=false) + { + $cats = new Api\Categories('',$this->app); + if ($check_only) + { + return true; + } + + // Put this there for posterity + $this->cat_name = Api\Categories::id2name($this->cat_id); + $cats->delete($this->cat_id, $this->subs, !$this->subs); + + return lang('Category deleted.'); + } + + /** + * Return a title / string representation for a given command, eg. to display it + * + * @return string + */ + function __tostring() + { + return lang('Category \'%1\' deleted' , $this->data['cat_name']); + } +}