Skip to content
Snippets Groups Projects
Commit 5c7a37a6 authored by Peter Rotich's avatar Peter Rotich Committed by Peter Rotich
Browse files

list: Add CLI import/export for lists

Add ability to import and export list items
parent d575b78f
No related branches found
No related tags found
No related merge requests found
......@@ -50,6 +50,7 @@ class DynamicFormsAjaxAPI extends AjaxController {
}
function saveFieldConfiguration($field_id) {
if (!($field = DynamicFormField::lookup($field_id)))
Http::response(404, 'No such field');
......@@ -75,13 +76,12 @@ class DynamicFormsAjaxAPI extends AjaxController {
function($a, $b) { return $a | $b; }, 0);
$field->flags = $flags | $preserve;
if (!$field->setConfiguration()) {
include STAFFINC_DIR . 'templates/dynamic-field-config.tmpl.php';
return;
}
else
if ($field->setConfiguration($_POST)) {
$field->save();
Http::response(201, 'Field successfully updated');
Http::response(201, 'Field successfully updated');
}
include STAFFINC_DIR . 'templates/dynamic-field-config.tmpl.php';
}
function deleteAnswer($entry_id, $field_id) {
......@@ -175,7 +175,7 @@ class DynamicFormsAjaxAPI extends AjaxController {
$icon = ($list->get('sort_mode') == 'SortCol')
? '<i class="icon-sort"></i>&nbsp;' : '';
if (!$valid || !$item->setConfiguration()) {
if (!$valid || !$item->setConfiguration($_POST)) {
include STAFFINC_DIR . 'templates/list-item-properties.tmpl.php';
return;
}
......
......@@ -562,6 +562,7 @@ class DynamicFormField extends VerySimpleModel {
* configuration of this field
*
* Parameters:
* vars - POST request / data
* errors - (OUT array) receives validation errors of the parsed
* configuration form
*
......@@ -570,15 +571,17 @@ class DynamicFormField extends VerySimpleModel {
* errors. If false, the errors were written into the received errors
* array.
*/
function setConfiguration(&$errors=array()) {
function setConfiguration($vars, &$errors=array()) {
$config = array();
foreach ($this->getConfigurationForm($_POST)->getFields() as $name=>$field) {
foreach ($this->getConfigurationForm($vars)->getFields() as $name=>$field) {
$config[$name] = $field->to_php($field->getClean());
$errors = array_merge($errors, $field->errors());
}
if (count($errors) === 0)
$this->set('configuration', JsonDataEncoder::encode($config));
$this->set('hint', Format::sanitize($_POST['hint']));
$this->set('hint', Format::sanitize($vars['hint']));
return count($errors) === 0;
}
......@@ -1656,6 +1659,11 @@ class TypeaheadSelectionWidget extends ChoicesWidget {
$data = $this->field->getSource();
if (isset($data[$this->name]))
return $data[$this->name];
$name = $this->field->get('name');
if (isset($data[$name]))
return $data[$name];
return parent::getValue();
}
......@@ -1665,7 +1673,10 @@ class TypeaheadSelectionWidget extends ChoicesWidget {
if (isset($data[$this->name.'_name'])) {
// Drop the extra part, if any
$v = $data[$this->name.'_name'];
$v = substr($v, 0, strrpos($v, ' — '));
$pos = strrpos($v, ' — ');
if ($pos !== false)
$v = substr($v, 0, $pos);
return trim($v);
}
return parent::getValue();
......
......@@ -244,6 +244,9 @@ class DynamicList extends VerySimpleModel implements CustomList {
function addItem($vars, &$errors) {
if (($item=$this->getItem($vars['value'])))
return $item;
$item = DynamicListItem::create(array(
'status' => 1,
'list_id' => $this->getId(),
......@@ -513,7 +516,7 @@ class DynamicList extends VerySimpleModel implements CustomList {
}
}
// 'name' and 'email' MUST be in the headers
// 'value' MUST be in the headers
if (!isset($headers['value']))
return __('CSV file must include `value` column');
......@@ -565,11 +568,11 @@ class DynamicList extends VerySimpleModel implements CustomList {
$items[] = $data;
}
$errors = array();
foreach ($items as $u) {
$vars = array_combine($keys, $u);
$item = $this->addItem($vars);
if (!$item || !$item->setConfiguration($errors, $vars))
$errors = array();
$item = $this->addItem($vars, $errors);
if (!$item || !$item->setConfiguration($vars, $errors))
return sprintf(__('Unable to import item: %s'),
print_r($vars, true));
}
......@@ -698,16 +701,19 @@ class DynamicListItem extends VerySimpleModel implements CustomListItem {
return $this->_config;
}
function setConfiguration(&$errors=array(), $source=false) {
function setConfiguration($vars, &$errors=array()) {
$config = array();
foreach ($this->getConfigurationForm($source ?: $_POST)->getFields() as $field) {
foreach ($this->getConfigurationForm($vars)->getFields() as $field) {
$config[$field->get('id')] = $field->to_php($field->getClean());
$errors = array_merge($errors, $field->errors());
}
if (count($errors) === 0)
$this->set('properties', JsonDataEncoder::encode($config));
return count($errors) === 0;
if ($errors)
return false;
$this->set('properties', JsonDataEncoder::encode($config));
return $this->save();
}
function getConfigurationForm($source=null) {
......@@ -1228,9 +1234,9 @@ class TicketStatus extends VerySimpleModel implements CustomListItem {
return $this->_settings;
}
function setConfiguration(&$errors=array()) {
function setConfiguration($vars, &$errors=array()) {
$properties = array();
foreach ($this->getConfigurationForm($_POST)->getFields() as $f) {
foreach ($this->getConfigurationForm($vars)->getFields() as $f) {
if ($this->isInternal() //Item is internal.
&& !$f->isEditable())
continue;
......
<?php
require_once dirname(__file__) . "/class.module.php";
require_once dirname(__file__) . "/../cli.inc.php";
include_once INCLUDE_DIR .'class.translation.php';
class ListManager extends Module {
var $prologue = 'CLI list manager';
var $arguments = array(
'action' => array(
'help' => 'Action to be performed',
'options' => array(
'import' => 'Import list items to the system',
'export' => 'Export list items from the system',
'show' => 'Show the lists',
),
),
);
var $options = array(
'file' => array('-f', '--file', 'metavar'=>'path',
'help' => 'File or stream to process'),
'id' => array('-ID', '--id', 'metavar'=>'id',
'help' => 'List ID'),
);
var $stream;
function run($args, $options) {
Bootstrap::connect();
$list = null;
if ($options['id'])
$list = DynamicList::lookup($options['id']);
switch ($args['action']) {
case 'import':
if (!$list)
$this->fail("List ID required for items import");
// Properly detect Macintosh style line endings
ini_set('auto_detect_line_endings', true);
if (!$options['file'])
$this->fail('CSV file to import list items from is required!');
elseif (!($this->stream = fopen($options['file'], 'rb')))
$this->fail("Unable to open input file [{$options['file']}]");
$extras = array();
$status = $list->importCsv($this->stream, $extras);
if (is_numeric($status))
$this->stderr->write("Successfully imported $status list items\n");
else
$this->fail($status);
break;
case 'export':
if (!$list)
$this->fail("List ID required for export");
$stream = $options['file'] ?: 'php://stdout';
if (!($this->stream = fopen($stream, 'c')))
$this->fail("Unable to open output file [{$options['file']}]");
fputcsv($this->stream, array('Value', 'Abbrev'));
foreach ($list->getItems() as $item)
fputcsv($this->stream, array(
(string) $item->getValue(),
$item->getAbbrev()));
break;
case 'show':
$lists = DynamicList::objects()->order_by('-type', 'name');
foreach ($lists as $list) {
$this->stdout->write(sprintf("%d %s \n",
$list->getId(),
$list->getName(),
$list->getPluralName() ?: $list->getName()
));
}
break;
default:
$this->stderr->write('Unknown action!');
}
@fclose($this->stream);
}
}
Module::register('list', 'ListManager');
?>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment