Fix indenting and sorting on statuses

This commit is contained in:
Nathan Gray 2011-06-13 17:43:51 +00:00
parent fe2b0d17bc
commit 418debd53c
2 changed files with 78 additions and 50 deletions

View File

@ -110,10 +110,6 @@ class timesheet_bo extends so_sql_cf
* @var array
*/
var $status_labels_config = array();
/**
* Array with substatus of label configuration
*/
var $status_labels_substatus = array();
/**
* Instance of the timesheet_tracking object
*
@ -158,48 +154,10 @@ class timesheet_bo extends so_sql_cf
$this->config_data = config::read(TIMESHEET_APP);
$this->quantity_sum = $this->config_data['quantity_sum'] == 'true';
if($this->config_data['status_labels'])
{
$this->status_labels =& $this->config_data['status_labels'];
if (!is_array($this->status_labels)) $this->status_labels= array($this->status_labels);
foreach ($this->status_labels as $status_id => $label)
{
if (!is_array($label))
{ //old values, bevor parent status
$name = $label;
$label=array();
$label['name'] = $name;
$label['parent'] = '';
}
$sort_labels[$status_id][] = $label['name'];
$this->status_labels_config[$status_id] = $label;
if ($label['parent']!='')
{
$sort = $label['parent']?$label['parent']:$status_id;
$sort_labels[$sort][$status_id]= $label['name'];
$this->status_labels_substatus[$sort][$status_id] = $status_id;
if (!isset($this->status_labels_substatus[$sort][$sort]))
{
$this->status_labels_substatus[$sort][$sort]= $sort;
}
}
}
unset($this->status_labels);
foreach ($sort_labels as $status_id => $label_sub)
{
if (!isset($this->status_labels[$status_id])) $this->status_labels[$status_id] = $this->status_labels_config[$status_id]['name'];
foreach ($sort_labels[$status_id] as $sub_status_id => $sub_label)
{
if (isset($this->status_labels_config[$sub_status_id]))
{
$level = '  ';
if (isset($this->status_labels_substatus[$sub_status_id])) $this->status_labels_substatus['2level'][$sub_status_id] = $sub_status_id ;
if (isset($this->status_labels_substatus['2level'][$status_id])) $level = '    ';
$this->status_labels[$sub_status_id]= $level.$this->status_labels_config[$sub_status_id]['name'];
}
}
}
}
// Load & process statuses
if($this->config_data['status_labels']) $this->load_statuses();
$this->today = mktime(0,0,0,date('m',$this->now),date('d',$this->now),date('Y',$this->now));
// save us in $GLOBALS['timesheet_bo'] for ExecMethod used in hooks
@ -210,6 +168,73 @@ class timesheet_bo extends so_sql_cf
$this->grants = $GLOBALS['egw']->acl->get_grants(TIMESHEET_APP);
}
/**
* Load status labels
*/
protected function load_statuses()
{
$this->status_labels =& $this->config_data['status_labels'];
if (!is_array($this->status_labels)) $this->status_labels= array($this->status_labels);
foreach ($this->status_labels as $status_id => $label)
{
if (!is_array($label))
{ //old values, before parent status
$name = $label;
$label=array();
$label['name'] = $name;
$label['parent'] = '';
}
$label['id'] = $status_id;
$this->status_labels_config[$status_id] = $label;
}
// Organise into tree structure
$map = array(
'' => array('substatus' => array())
);
foreach($this->status_labels_config as $id => &$status) {
$status['substatus'] = array();
$map[$id] = &$status;
}
foreach($this->status_labels_config as &$status) {
$map[$status['parent']]['substatus'][] = &$status;
}
$tree = $map['']['substatus'];
// Make nice selectbox labels
$this->status_labels = array();
$this->make_status_labels($tree, $this->status_labels);
// Sort config based on tree
$sorted = array();
foreach($this->status_labels as $status_id => $label) {
$sorted[$status_id] = $this->status_labels_config[$status_id];
//$sorted[$status_id]['name'] = $label;
unset($sorted[$status_id]['substatus']);
}
$this->status_labels_config = $sorted;
}
/**
* Make nice labels with leading spaces depending on depth
*
* @param statuses List of statuses to process, with sub-statuses in a 'substatus' array
* @param labels Array of labels, pass array() and labels will be built in it
* @param depth Current depth
*
* @return None, labels are built in labels parameter
*/
protected function make_status_labels($statuses, &$labels, $depth=0) {
foreach($statuses as $status) {
$labels[$status['id']] = str_pad('',$depth*12, " ",STR_PAD_LEFT).trim(str_replace(' ','',$status['name']));
//$labels[$status['id']] = str_pad('',$depth, "+------",STR_PAD_LEFT).$status['name'];
if(count($status['substatus']) > 0) {
$this->make_status_labels($status['substatus'], $labels, $depth+1);
}
}
}
/**
* get list of specified grants as uid => Username pairs
*

View File

@ -1067,14 +1067,13 @@ class timesheet_ui extends timesheet_bo
break;
case 'apply':
case 'save':
foreach($content['statis'] as $cat)
foreach($content['statis'] as &$cat)
{
$id = $cat['id'];
if (($cat ['name'] !== $this->status_labels_config[$id]) && ($cat ['name'] !== '') || ($cat ['parent'] !== $this->status_labels_config[$id]['parent']) && ($cat ['parent'] !== ''))
{
$this->status_labels[$id] = $cat['name'];
$this->status_labels_config[$id] = array(
'name' => $cat['name'],
'name' => trim(str_replace(' ', '', $cat['name'])),
'parent' => $cat['parent'],
'admin' => $cat['admin']);
$need_update = true;
@ -1083,6 +1082,8 @@ class timesheet_ui extends timesheet_bo
if ($need_update)
{
config::save_value('status_labels',$this->status_labels_config,TIMESHEET_APP);
$this->config_data = config::read(TIMESHEET_APP);
$this->load_statuses();
$msg .= lang('Status updated.');
}
if ($button == 'apply') break;
@ -1108,6 +1109,7 @@ class timesheet_ui extends timesheet_bo
}
$i = 1;
$max_id = 0;
unset($content['statis']);
foreach($this->status_labels_config as $id => $label)
{
@ -1116,11 +1118,12 @@ class timesheet_ui extends timesheet_bo
$content['statis'][$i]['parent']= $label['parent'];
$content['statis'][$i]['admin']= $label['admin'];
$i++;
$max_id = max($id, $max_id);
}
$content['statis'][$i]['name'] = '';
$content['statis'][$i]['parent'];
$content['statis'][$i]['admin'] = '';
$content['statis'][$i]['id'] = ++$id;
$content['statis'][$i]['id'] = ++$max_id;
$content['msg'] = $msg;
$preserv = $content;