private $data = array();
private $user_functions = array();
+ private $field_desc = array();
public function CSVImporter($table, $key = 'id', $do_sql = true)
{
return true;
}
+ private function getValue($line, $key, $action)
+ {
+ if (@array_key_exists($action, $line)) {
+ $value = $line[$action];
+ } elseif (is_callable($action, false)) {
+ $value = call_user_func($action, $line, $key);
+ } else {
+ $value = $action;
+ }
+ if (is_null($value) || $value == 'NULL') {
+ $value = 'NULL';
+ }
+ return $value;
+ }
+
private function makeAssoc($line, $relation)
{
$ops = array();
foreach ($relation as $key=>$ref) {
- if (@array_key_exists($ref, $line)) {
- $value = $line[$ref];
- } elseif (is_callable($ref, false)) {
- $value = call_user_func($ref, $line, $key);
- } else {
- $value = $ref;
- }
- if (is_null($value) || $value == 'NULL') {
- $value = 'NULL';
- }
- $ops[$key] = $value;
+ $ops[$key] = $this->getValue($line, $key, $ref);
}
return $ops;
}
{
$ops = array();
foreach ($relation as $key=>$ref) {
- if (@array_key_exists($ref, $line)) {
- $value = $line[$ref];
- } elseif (is_callable($ref, false)) {
- $value = call_user_func($ref, $line, $key);
- } else {
- $value = $ref;
- }
- if (is_null($value) || $value == 'NULL') {
- $value = 'NULL';
- } else {
+ $value = $this->getValue($line, $key, $ref);
+ if (!is_null($value) && $value != 'NULL') {
$value = "'" . addslashes($value) . "'";
}
$ops[$key] = "$key = $value";
return false;
}
+ public function describe($name, $desc)
+ {
+ $this->field_desc[$name] = $desc;
+ }
+
/** Handle insertion form
* @param $page PlatalPage to process
* @param $url URI of the page
*/
public function apply(&$page, $url, $fields = null)
{
- if (is_null($fields)) {
+ if (is_null($fields) || empty($fields)) {
$fields = $this->getFieldList();
}
if (is_null($fields)) {
}
$page->assign('csv_index', $this->index);
$page->assign('csv_funtions', $this->user_functions);
+ $page->assign('csv_field_desc', $this->field_desc);
$page->assign('csv_page', $next);
$page->assign('csv_path', $url);
$page->assign('csv_fields', $fields);
$page->assign('id', $id);
$list = false;
}
+ if ($action == 'massadd') {
+ $importer = new CSVImporter($this->table, $this->idfield_editable ? $this->idfield : null);
+ $fields = array();
+ foreach ($this->vars as $field=>$descr) {
+ if ($this->idfield_editable || $field != $this->idfield) {
+ $fields[] = $field;
+ $importer->describe($field, @$descr['desc']);
+ }
+ }
+ $page->assign('massadd', true);
+ $importer->apply($page, $this->pl . '/massadd', $fields);
+ $list = false;
+ }
if ($action == 'new') {
if (!$this->idfield_editable) {
$r = XDB::query("SELECT MAX({$this->idfield})+1 FROM {$this->table}");
<option value="replace" {if $smarty.request.csv_action eq 'replace'}selected="selected"{/if}>
remplacer par la nouvelle entrée
</option>
+ {if $csv_key}
<option value="update" {if $smarty.request.csv_action eq 'update'}selected="selected"{/if}>
mettre à jour les champs sélectionnés
</option>
+ {/if}
</select>
</div>
<table class="bicol">
</tr>
{foreach from=$csv_fields item=f}
<tr class="{cycle values="pair,impair"}">
- <td>{$f}</td>
+ <td>{$csv_field_desc[$f]|default:$f}</td>
<td>
<select name="csv_value[{$f}]" onchange="showValue('{$f}', this);">
<option value="" {if !$smarty.request.csv_value[$f]}selected="selected"{/if}>
<table class="bicol">
<tr>
{foreach from=$csv_fields item=f}
- <th>{$f}</th>
+ <th>{$csv_field_desc[$f]|default:$f}</th>
{/foreach}
</tr>
{foreach from=$csv_preview item=assoc}