1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-01 20:30:39 +02:00

admin UI: more improvements and bugfixes, array fieldname support added; code split; preparing for: JS validation, better notifications, JS tabs

This commit is contained in:
secretr
2009-11-21 11:15:29 +00:00
parent efd184fd93
commit c424a273e1
3 changed files with 216 additions and 182 deletions

View File

@@ -9,8 +9,8 @@
* Administration UI handlers, admin helper functions * Administration UI handlers, admin helper functions
* *
* $Source: /cvs_backup/e107_0.8/e107_handlers/admin_handler.php,v $ * $Source: /cvs_backup/e107_0.8/e107_handlers/admin_handler.php,v $
* $Revision: 1.34 $ * $Revision: 1.35 $
* $Date: 2009-11-18 19:57:07 $ * $Date: 2009-11-21 11:15:29 $
* $Author: secretr $ * $Author: secretr $
*/ */
@@ -2552,29 +2552,34 @@ class e_admin_controller_ui extends e_admin_controller
*/ */
protected function convertToData(&$data) protected function convertToData(&$data)
{ {
$model = new e_model($data);
foreach ($this->getFields() as $key => $attributes) foreach ($this->getFields() as $key => $attributes)
{ {
if(!isset($data[$key])) $value = vartrue($attributes['dataPath']) ? $model->getData($attributes['dataPath']) : $model->get($data[$key]);
if(null === $value)
{ {
continue; continue;
} }
switch($attributes['type']) switch($attributes['type'])
{ {
case 'datestamp': case 'datestamp':
if(!is_numeric($data[$key])) if(!is_numeric($value))
{ {
$data[$key] = trim($data[$key]) ? e107::getDateConvert()->toTime($data[$key], 'input') : 0; $value = trim($value) ? e107::getDateConvert()->toTime($value, 'input') : 0;
} }
break; break;
case 'ip': // TODO - ask Steve if this check is required case 'ip': // TODO - ask Steve if this check is required
if(strpos($data[$key], '.') !== FALSE) if(strpos($value, '.') !== FALSE)
{ {
$data[$key] = trim($data[$key]) ? e107::getInstance()->ipEncode($data[$key]) : ''; $value = trim($value) ? e107::getInstance()->ipEncode($value) : '';
} }
break; break;
} }
} }
$data = $model->getData();
unset($model);
$this->toData($data); $this->toData($data);
} }
@@ -3620,7 +3625,7 @@ class e_admin_form_ui extends e_form
); );
$models[] = $controller->getModel(); $models[] = $controller->getModel();
return $this->createForm($forms, $models, e_AJAX_REQUEST); return $this->renderCreateForm($forms, $models, e_AJAX_REQUEST);
} }
/** /**
@@ -3651,7 +3656,7 @@ class e_admin_form_ui extends e_form
); );
$models[] = $controller->getConfig(); $models[] = $controller->getConfig();
return $this->createForm($forms, $models, e_AJAX_REQUEST); return $this->renderCreateForm($forms, $models, e_AJAX_REQUEST);
} }
/** /**
@@ -3667,8 +3672,10 @@ class e_admin_form_ui extends e_form
$controller = $this->getController(); $controller = $this->getController();
$request = $controller->getRequest(); $request = $controller->getRequest();
$tree = $controller->getTreeModel(); $id = $this->getElementId();
$options = array( $tree = $options = array();
$tree[$id] = $controller->getTreeModel();
$options[$id] = array(
'id' => $this->getElementId(), // unique string used for building element ids, REQUIRED 'id' => $this->getElementId(), // unique string used for building element ids, REQUIRED
'pid' => $controller->getPrimaryName(), // primary field name, REQUIRED 'pid' => $controller->getPrimaryName(), // primary field name, REQUIRED
//'url' => e_SELF, default //'url' => e_SELF, default
@@ -3681,7 +3688,7 @@ class e_admin_form_ui extends e_form
'fields' => $controller->getFields(), // see e_admin_ui::$fields 'fields' => $controller->getFields(), // see e_admin_ui::$fields
'fieldpref' => $controller->getFieldPref(), // see e_admin_ui::$fieldpref 'fieldpref' => $controller->getFieldPref(), // see e_admin_ui::$fieldpref
'table_pre' => '', // markup to be added before opening table element 'table_pre' => '', // markup to be added before opening table element
'table_post' => !$tree->isEmpty() ? $this->renderBatch($controller->getBatchDelete()) : '', 'table_post' => !$tree[$id]->isEmpty() ? $this->renderBatch($controller->getBatchDelete()) : '',
'fieldset_pre' => '', // markup to be added before opening fieldset element 'fieldset_pre' => '', // markup to be added before opening fieldset element
'fieldset_post' => '', // markup to be added after closing fieldset element 'fieldset_post' => '', // markup to be added after closing fieldset element
'perPage' => $controller->getPerPage(), // if 0 - no next/prev navigation 'perPage' => $controller->getPerPage(), // if 0 - no next/prev navigation
@@ -3689,7 +3696,7 @@ class e_admin_form_ui extends e_form
'field' => $controller->getQuery('field'), //current order field name, default - primary field 'field' => $controller->getQuery('field'), //current order field name, default - primary field
'asc' => $controller->getQuery('asc', 'desc'), //current 'order by' rule, default 'asc' 'asc' => $controller->getQuery('asc', 'desc'), //current 'order by' rule, default 'asc'
); );
return $this->listForm($options, $tree, $ajax); return $this->renderListForm($options, $tree, $ajax);
} }
function renderFilter($current_query = array(), $location = '', $input_options = array()) function renderFilter($current_query = array(), $location = '', $input_options = array())

View File

@@ -9,9 +9,9 @@
* Form Handler * Form Handler
* *
* $Source: /cvs_backup/e107_0.8/e107_handlers/form_handler.php,v $ * $Source: /cvs_backup/e107_0.8/e107_handlers/form_handler.php,v $
* $Revision: 1.88 $ * $Revision: 1.89 $
* $Date: 2009-11-20 05:01:31 $ * $Date: 2009-11-21 11:15:28 $
* $Author: e107coders $ * $Author: secretr $
* *
*/ */
@@ -1367,7 +1367,7 @@ class e_form
* Expected options array format: * Expected options array format:
* <code> * <code>
* <?php * <?php
* $options = array( * $form_options['myplugin'] = array(
* 'id' => 'myplugin', // unique string used for building element ids, REQUIRED * 'id' => 'myplugin', // unique string used for building element ids, REQUIRED
* 'pid' => 'primary_id', // primary field name, REQUIRED * 'pid' => 'primary_id', // primary field name, REQUIRED
* 'url' => '{e_PLUGIN}myplug/admin_config.php', // if not set, e_SELF is used * 'url' => '{e_PLUGIN}myplug/admin_config.php', // if not set, e_SELF is used
@@ -1388,82 +1388,88 @@ class e_form
* 'field' => 'field_name', //current order field name, default - primary field * 'field' => 'field_name', //current order field name, default - primary field
* 'asc' => 'desc', //current 'order by' rule, default 'asc' * 'asc' => 'desc', //current 'order by' rule, default 'asc'
* ); * );
* $list = new e_admin_tree_model($data); * $tree_models['myplugin'] = new e_admin_tree_model($data);
* </code> * </code>
* TODO - move fieldset & table generation in separate methods, needed for ajax calls * TODO - move fieldset & table generation in separate methods, needed for ajax calls
* @param array $options * @param array $form_options
* @param e_admin_tree_model $list * @param e_admin_tree_model $tree_model
* @param boolean $nocontainer don't enclose form in div container * @param boolean $nocontainer don't enclose form in div container
* @return string * @return string
*/ */
public function listForm($options, $list, $nocontainer = false) public function renderListForm($form_options, $tree_models, $nocontainer = false)
{ {
$tp = e107::getParser(); $tp = e107::getParser();
$tree = $list->getTree();
$total = $list->getTotal();
$amount = $options['perPage']; foreach ($form_options as $fid => $options)
$from = vartrue($options['from'], 0);
$field = vartrue($options['field'], $options['pid']);
$asc = strtoupper(vartrue($options['asc'], 'asc'));
$elid = $options['id'];
$query = isset($options['query']) ? $options['query'] : e_QUERY ;
$url = (isset($options['url']) ? $tp->replaceConstants($options['url'], 'abs') : e_SELF);
$formurl = $url.($query ? '?'.$query : '');
$fields = $options['fields'];
$current_fields = varset($options['fieldpref']) ? $options['fieldpref'] : array_keys($options['fields']);
$text = "
<form method='post' action='{$formurl}' id='{$elid}-list-form'>
".vartrue($options['fieldset_pre'])."
<fieldset id='{$elid}-list'>
<legend class='e-hideme'>".$options['legend']."</legend>
".vartrue($options['table_pre'])."
<table cellpadding='0' cellspacing='0' class='adminlist' id='{$elid}-list-table'>
".$this->colGroup($fields, $current_fields)."
".$this->thead($fields, $current_fields, varset($options['head_query']), varset($options['query']))."
<tbody>
";
if(!$tree)
{
$text .= "
<tr>
<td colspan='".count($current_fields)."' class='center middle'>".LAN_NO_RECORDS."</td>
</tr>
";
}
else
{ {
$tree_model = $tree_models[$fid];
$tree = $tree_model->getTree();
$total = $tree_model->getTotal();
foreach($tree as $model) $amount = $options['perPage'];
$from = vartrue($options['from'], 0);
$field = vartrue($options['field'], $options['pid']);
$asc = strtoupper(vartrue($options['asc'], 'asc'));
$elid = $fid;//$options['id'];
$query = isset($options['query']) ? $options['query'] : e_QUERY ;
$url = (isset($options['url']) ? $tp->replaceConstants($options['url'], 'abs') : e_SELF);
$formurl = $url.($query ? '?'.$query : '');
$fields = $options['fields'];
$current_fields = varset($options['fieldpref']) ? $options['fieldpref'] : array_keys($options['fields']);
$text = "
<form method='post' action='{$formurl}' id='{$elid}-list-form'>
".vartrue($options['fieldset_pre'])."
<fieldset id='{$elid}-list'>
<legend class='e-hideme'>".$options['legend']."</legend>
".vartrue($options['table_pre'])."
<table cellpadding='0' cellspacing='0' class='adminlist' id='{$elid}-list-table'>
".$this->colGroup($fields, $current_fields)."
".$this->thead($fields, $current_fields, varset($options['head_query']), varset($options['query']))."
<tbody>
";
if(!$tree)
{ {
$text .= $this->renderTableRow($fields, $current_fields, $model->getData(), $options['pid']); $text .= "
<tr>
<td colspan='".count($current_fields)."' class='center middle'>".LAN_NO_RECORDS."</td>
</tr>
";
} }
else
} {
$text .= " foreach($tree as $model)
</tbody> {
</table> $text .= $this->renderTableRow($fields, $current_fields, $model->getData(), $options['pid']);
".vartrue($options['table_post'])." }
";
}
if($tree && $amount) $text .= "
{ </tbody>
$parms = $total.",".$amount.",".$from.",".$url.'?'.($options['np_query'] ? $options['np_query'].'&amp;' : '').'from=[FROM]'; </table>
$text .= $tp->parseTemplate("{NEXTPREV={$parms}}"); ".vartrue($options['table_post'])."
} ";
$text .= "
</fieldset> if($tree && $amount)
".vartrue($options['fieldset_post'])." {
</form> $parms = $total.",".$amount.",".$from.",".$url.'?'.($options['np_query'] ? $options['np_query'].'&amp;' : '').'from=[FROM]';
"; $text .= $tp->parseTemplate("{NEXTPREV={$parms}}");
if(!$nocontainer) }
{
$text = '<div class="e-container">'.$text.'</div>'; $text .= "
</fieldset>
".vartrue($options['fieldset_post'])."
</form>
";
if(!$nocontainer)
{
$text = '<div class="e-container">'.$text.'</div>';
}
} }
return (vartrue($options['form_pre']).$text.vartrue($options['form_post'])); return (vartrue($options['form_pre']).$text.vartrue($options['form_post']));
} }
@@ -1507,7 +1513,7 @@ class e_form
* @param boolean $nocontainer don't enclose in div container * @param boolean $nocontainer don't enclose in div container
* @return string * @return string
*/ */
function createForm($forms, $models, $nocontainer = false) function renderCreateForm($forms, $models, $nocontainer = false)
{ {
$text = ''; $text = '';
foreach ($forms as $fid => $form) foreach ($forms as $fid => $form)
@@ -1523,103 +1529,9 @@ class e_form
foreach ($form['fieldsets'] as $elid => $data) foreach ($form['fieldsets'] as $elid => $data)
{ {
$elid = $form['id'].'-'.$elid; $elid = $form['id'].'-'.$elid;
$text .= vartrue($data['fieldset_pre'])." $text .= $this->renderCreateFieldset($elid, $data, $model, $nocontainer);
<fieldset id='{$elid}'>
<legend>".vartrue($data['legend'])."</legend>
".vartrue($data['table_pre'])."
<table cellpadding='0' cellspacing='0' class='adminedit'>
<colgroup span='2'>
<col class='col-label' />
<col class='col-control' />
</colgroup>
<tbody>
";
foreach($data['fields'] as $key => $att)
{
// convert aliases - not supported in edit mod
if(!$model->has($key) && $att['alias'])
{
$key = $att['field'];
}
$parms = vartrue($att['formparms'], array());
if(!is_array($parms)) parse_str($parms, $parms);
$label = vartrue($att['note']) ? '<div class="label-note">'.deftrue($att['note'], $att['note']).'</div>' : '';
$help = vartrue($att['help']) ? '<div class="field-help">'.deftrue($att['help'], $att['help']).'</div>' : '';
// type null - system (special) fields
if($att['type'] !== null && !vartrue($att['noedit']) && $key != $model->getFieldIdName())
{
$text .= "
<tr>
<td class='label'>
".defset($att['title'], $att['title']).$label."
</td>
<td class='control'>
".$this->renderElement($key, $model->getIfPosted($key), $att)."
{$help}
</td>
</tr>
";
}
//if($bckp) $model->remove($bckp);
}
$text .= "
</tbody>
</table>
".vartrue($data['table_post'])."
<div class='buttons-bar center'>
";
// After submit options
$defsubmitopt = array('list' => 'go to list', 'create' => 'create another', 'edit' => 'edit current');
$submitopt = isset($data['after_submit_options']) ? $data['after_submit_options'] : true;
if(true === $submitopt)
{
$submitopt = $defsubmitopt;
}
if($submitopt)
{
$selected = isset($data['after_submit_default']) && array_key_exists($data['after_submit_default'], $submitopt) ? $data['after_submit_default'] : '';
$text .= '
<div class="options">
After submit: '.$this->radio_multi('__after_submit_action', $submitopt, $selected, false).'
</div>
';
}
$triggers = vartrue($data['triggers'], 'auto');
if(is_string($triggers) && 'auto' === $triggers)
{
$triggers = array();
if($model->getId())
{
$triggers['submit'] = array(LAN_UPDATE, 'update', $model->getId());
}
else
{
$triggers['submit'] = array(LAN_CREATE, 'create', 0);
}
$triggers['cancel'] = array(LAN_CANCEL, 'cancel');
}
foreach ($triggers as $trigger => $tdata)
{
$text .= $this->admin_button('etrigger_'.$trigger, $tdata[0], $tdata[1]);
if(isset($tdata[2]))
{
$text .= $this->hidden($trigger.'_value', $tdata[2]);
}
}
$text .= "
</div>
</fieldset>
".vartrue($data['fieldset_post'])."
";
} }
$text .= " $text .= "
</form> </form>
"; ";
@@ -1632,6 +1544,120 @@ class e_form
return $text; return $text;
} }
function renderCreateFieldset($id, $fdata, $model, $nocontainer = false)
{
$text = vartrue($fdata['fieldset_pre'])."
<fieldset id='{$id}'>
<legend>".vartrue($fdata['legend'])."</legend>
".vartrue($fdata['table_pre'])."
<table cellpadding='0' cellspacing='0' class='adminedit'>
<colgroup span='2'>
<col class='col-label' />
<col class='col-control' />
</colgroup>
<tbody>
";
foreach($fdata['fields'] as $key => $att)
{
// convert aliases - not supported in edit mod
if($att['alias'] && !$model->hasData($key))
{
$key = $att['field'];
}
$parms = vartrue($att['formparms'], array());
if(!is_array($parms)) parse_str($parms, $parms);
$label = vartrue($att['note']) ? '<div class="label-note">'.deftrue($att['note'], $att['note']).'</div>' : '';
$help = vartrue($att['help']) ? '<div class="field-help">'.deftrue($att['help'], $att['help']).'</div>' : '';
$valPath = trim(vartrue($att['dataPath'], $key), '/');
$keyName = $key;
if(strpos($valPath, '/')) //not TRUE, cause string doesn't start with /
{
$tmp = explode('/', $valPath);
$keyName = array_shift($tmp);
foreach ($tmp as $path)
{
$keyName .= '['.$path.']';
}
}
// type null - system (special) fields
if($att['type'] !== null && !vartrue($att['noedit']) && $key != $model->getFieldIdName())
{
$text .= "
<tr>
<td class='label'>
".defset($att['title'], $att['title']).$label."
</td>
<td class='control'>
".$this->renderElement($keyName, $model->getIfPosted($valPath), $att)."
{$help}
</td>
</tr>
";
}
//if($bckp) $model->remove($bckp);
}
$text .= "
</tbody>
</table>
".vartrue($fdata['table_post'])."
<div class='buttons-bar center'>
";
// After submit options
$defsubmitopt = array('list' => 'go to list', 'create' => 'create another', 'edit' => 'edit current');
$submitopt = isset($fdata['after_submit_options']) ? $fdata['after_submit_options'] : true;
if(true === $submitopt)
{
$submitopt = $defsubmitopt;
}
if($submitopt)
{
$selected = isset($fdata['after_submit_default']) && array_key_exists($fdata['after_submit_default'], $submitopt) ? $fdata['after_submit_default'] : '';
$text .= '
<div class="options">
After submit: '.$this->radio_multi('__after_submit_action', $submitopt, $selected, false).'
</div>
';
}
$triggers = vartrue($fdata['triggers'], 'auto');
if(is_string($triggers) && 'auto' === $triggers)
{
$triggers = array();
if($model->getId())
{
$triggers['submit'] = array(LAN_UPDATE, 'update', $model->getId());
}
else
{
$triggers['submit'] = array(LAN_CREATE, 'create', 0);
}
$triggers['cancel'] = array(LAN_CANCEL, 'cancel');
}
foreach ($triggers as $trigger => $tdata)
{
$text .= $this->admin_button('etrigger_'.$trigger, $tdata[0], $tdata[1]);
if(isset($tdata[2]))
{
$text .= $this->hidden($trigger.'_value', $tdata[2]);
}
}
$text .= "
</div>
</fieldset>
".vartrue($fdata['fieldset_post'])."
";
return $text;
}
// The 2 functions below are for demonstration purposes only, and may be moved/modified before release. // The 2 functions below are for demonstration purposes only, and may be moved/modified before release.
function filterType($fieldarray) function filterType($fieldarray)
{ {

View File

@@ -9,8 +9,8 @@
* e107 Base Model * e107 Base Model
* *
* $Source: /cvs_backup/e107_0.8/e107_handlers/model_class.php,v $ * $Source: /cvs_backup/e107_0.8/e107_handlers/model_class.php,v $
* $Revision: 1.42 $ * $Revision: 1.43 $
* $Date: 2009-11-18 14:46:27 $ * $Date: 2009-11-21 11:15:29 $
* $Author: secretr $ * $Author: secretr $
*/ */
@@ -1195,9 +1195,10 @@ class e_admin_model extends e_model
*/ */
public function getIfPosted($key, $default = '', $index = null) public function getIfPosted($key, $default = '', $index = null)
{ {
if(null !== $this->getPostedData((string) $key)) $posted = $this->getPostedData((string) $key, null, $index);
if(null !== $posted)
{ {
return e107::getParser()->post_toForm($this->getPostedData((string) $key, null, $index)); return e107::getParser()->post_toForm($posted);
} }
return e107::getParser()->toForm($this->getData((string) $key, $default, $index)); return e107::getParser()->toForm($this->getData((string) $key, $default, $index));
} }